Skip to content

Commit

Permalink
Use explicit character set in tests (#6150)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Jan 7, 2022
1 parent dd0db18 commit 520e114
Show file tree
Hide file tree
Showing 46 changed files with 225 additions and 159 deletions.
9 changes: 5 additions & 4 deletions cli/src/test/java/hudson/cli/PlainCLIProtocolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.nio.charset.Charset;
import org.junit.jupiter.api.Test;

public class PlainCLIProtocolTest {
Expand Down Expand Up @@ -67,7 +68,7 @@ protected void handleClose() {}
void send() throws IOException {
sendArg("command");
sendStart();
streamStdin().write("hello".getBytes());
streamStdin().write("hello".getBytes(Charset.defaultCharset()));
}

void newop() throws IOException {
Expand Down Expand Up @@ -123,7 +124,7 @@ protected void onEndStdin() throws IOException {}
protected void handleClose() {}

void send() throws IOException {
streamStdout().write("goodbye".getBytes());
streamStdout().write("goodbye".getBytes(Charset.defaultCharset()));
sendExit(2);
}

Expand Down Expand Up @@ -156,9 +157,9 @@ void newop() throws IOException {
while (server.stdin.size() == 0) {
Thread.sleep(100);
}
assertEquals("hello", server.stdin.toString());
assertEquals("hello", server.stdin.toString(Charset.defaultCharset().name()));
assertEquals("command", server.arg);
assertEquals("goodbye", client.stdout.toString());
assertEquals("goodbye", client.stdout.toString(Charset.defaultCharset().name()));
assertEquals(2, client.code);
}

Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/hudson/FilePathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -657,9 +658,9 @@ private static void assertValidateAntFileMask(String expected, FilePath d, Strin
when(con.getInputStream()).thenThrow(new ConnectException());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String message = "going ahead";
assertFalse(d.installIfNecessaryFrom(url, new StreamTaskListener(baos), message));
assertFalse(d.installIfNecessaryFrom(url, new StreamTaskListener(baos, Charset.defaultCharset()), message));
verify(con).setIfModifiedSince(123000);
String log = baos.toString();
String log = baos.toString(Charset.defaultCharset().name());
assertFalse(log, log.contains(message));
assertTrue(log, log.contains("504 Gateway Timeout"));
}
Expand Down Expand Up @@ -701,7 +702,7 @@ private InputStream someZippedContent() throws IOException {
final ZipOutputStream zip = new ZipOutputStream(buf);

zip.putNextEntry(new ZipEntry("abc"));
zip.write("abc".getBytes());
zip.write("abc".getBytes(StandardCharsets.US_ASCII));
zip.close();

return new ByteArrayInputStream(buf.toByteArray());
Expand Down
8 changes: 4 additions & 4 deletions core/src/test/java/hudson/LauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,20 @@ public Object call() throws RuntimeException {
@Issue("JENKINS-15733")
@Test public void decorateByEnv() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TaskListener l = new StreamBuildListener(baos);
TaskListener l = new StreamBuildListener(baos, Charset.defaultCharset());
Launcher base = new Launcher.LocalLauncher(l);
EnvVars env = new EnvVars("key1", "val1");
Launcher decorated = base.decorateByEnv(env);
int res = decorated.launch().envs("key2=val2").cmds(Functions.isWindows() ? new String[] {"cmd", "/q", "/c", "echo %key1% %key2%"} : new String[] {"sh", "-c", "echo $key1 $key2"}).stdout(l).join();
String log = baos.toString();
String log = baos.toString(Charset.defaultCharset().name());
assertEquals(log, 0, res);
assertTrue(log, log.contains("val1 val2"));
}

@Issue("JENKINS-18368")
@Test public void decoratedByEnvMaintainsIsUnix() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
TaskListener listener = new StreamBuildListener(output);
TaskListener listener = new StreamBuildListener(output, Charset.defaultCharset());
Launcher remoteLauncher = new Launcher.RemoteLauncher(listener, FilePath.localChannel, false);
Launcher decorated = remoteLauncher.decorateByEnv(new EnvVars());
assertFalse(decorated.isUnix());
Expand All @@ -115,7 +115,7 @@ public Object call() throws RuntimeException {
@Issue("JENKINS-18368")
@Test public void decoratedByPrefixMaintainsIsUnix() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
TaskListener listener = new StreamBuildListener(output);
TaskListener listener = new StreamBuildListener(output, Charset.defaultCharset());
Launcher remoteLauncher = new Launcher.RemoteLauncher(listener, FilePath.localChannel, false);
Launcher decorated = remoteLauncher.decorateByPrefix("test");
assertFalse(decorated.isUnix());
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/java/hudson/PluginManagerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void parseRequestedPlugins() throws Exception {
tmp.resolve("output.txt")
);
assertEquals("{other=2.0, stuff=1.2}", new LocalPluginManager(output.getParent().toFile())
.parseRequestedPlugins(new ByteArrayInputStream("<root><stuff plugin='stuff@1.0'><more plugin='other@2.0'><things plugin='stuff@1.2'/></more></stuff></root>".getBytes())).toString());
.parseRequestedPlugins(new ByteArrayInputStream("<root><stuff plugin='stuff@1.0'><more plugin='other@2.0'><things plugin='stuff@1.2'/></more></stuff></root>".getBytes(StandardCharsets.UTF_8))).toString());
}

@Issue("SECURITY-167")
Expand All @@ -82,7 +82,7 @@ public void parseInvalidRequestedPlugins() throws Exception {

PluginManager pluginManager = new LocalPluginManager(Util.createTempDir());
final IOException ex = assertThrows(IOException.class,
() -> pluginManager.parseRequestedPlugins(new ByteArrayInputStream(evilXML.getBytes())),
() -> pluginManager.parseRequestedPlugins(new ByteArrayInputStream(evilXML.getBytes(StandardCharsets.UTF_8))),
"XML contains an external entity, but no exception was thrown.");
assertThat(ex.getCause(), instanceOf(SAXException.class));
assertThat(ex.getCause().getMessage(), containsString("DOCTYPE is disallowed"));
Expand Down Expand Up @@ -156,7 +156,7 @@ private File createHpiWithManifest() throws IOException {
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(f.toPath()))) {
ZipEntry e = new ZipEntry(manifestPath);
out.putNextEntry(e);
byte[] data = SAMPLE_MANIFEST_FILE.getBytes();
byte[] data = SAMPLE_MANIFEST_FILE.getBytes(StandardCharsets.UTF_8);
out.write(data, 0, data.length);
out.closeEntry();
}
Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/hudson/UtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
Expand Down Expand Up @@ -231,7 +232,7 @@ public void testSymlink() throws Exception {
assumeFalse(Functions.isWindows());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamTaskListener l = new StreamTaskListener(baos);
StreamTaskListener l = new StreamTaskListener(baos, Charset.defaultCharset());
File d = tmp.getRoot();
try {
new FilePath(new File(d, "a")).touch(0);
Expand All @@ -245,7 +246,7 @@ public void testSymlink() throws Exception {
buf.append((char) ('0' + (i % 10)));
Util.createSymlink(d, buf.toString(), "x", l);

String log = baos.toString();
String log = baos.toString(Charset.defaultCharset().name());
if (log.length() > 0)
System.err.println("log output: " + log);

Expand Down Expand Up @@ -279,7 +280,7 @@ public void testIsSymlink() throws IOException, InterruptedException {
assumeFalse(Functions.isWindows());

ByteArrayOutputStream baos = new ByteArrayOutputStream();
StreamTaskListener l = new StreamTaskListener(baos);
StreamTaskListener l = new StreamTaskListener(baos, Charset.defaultCharset());
File d = tmp.getRoot();
try {
new FilePath(new File(d, "original")).touch(0);
Expand Down
18 changes: 13 additions & 5 deletions core/src/test/java/hudson/cli/ListJobsCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
import hudson.model.ViewTest.CompositeView;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import jenkins.model.Jenkins;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
Expand Down Expand Up @@ -154,8 +156,11 @@ private TypeSafeMatcher<ByteArrayOutputStream> empty() {

@Override
protected boolean matchesSafely(ByteArrayOutputStream item) {

return item.toString().isEmpty();
try {
return item.toString(command.getClientCharset().name()).isEmpty();
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
}

@Override
Expand All @@ -173,9 +178,12 @@ private TypeSafeMatcher<ByteArrayOutputStream> listsJobs(final String... expecte
@Override
protected boolean matchesSafely(ByteArrayOutputStream item) {

final HashSet<String> jobs = new HashSet<>(
Arrays.asList(item.toString().split(System.getProperty("line.separator")))
);
Set<String> jobs;
try {
jobs = new HashSet<>(Arrays.asList(item.toString(command.getClientCharset().name()).split(System.getProperty("line.separator"))));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}

return new HashSet<>(Arrays.asList(expected)).equals(jobs);
}
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/java/hudson/slaves/ComputerLauncherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.io.StringReader;
import java.nio.charset.Charset;
import org.apache.commons.io.output.NullOutputStream;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.Issue;
Expand Down Expand Up @@ -142,8 +143,8 @@ public class ComputerLauncherTest {

private static void assertChecked(String text, String spec) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ComputerLauncher.checkJavaVersion(new PrintStream(os), "bin/java", new BufferedReader(new StringReader(text)));
String logged = os.toString();
ComputerLauncher.checkJavaVersion(new PrintStream(os, false, Charset.defaultCharset().name()), "bin/java", new BufferedReader(new StringReader(text)));
String logged = os.toString(Charset.defaultCharset().name());
assertTrue(logged.contains(Messages.ComputerLauncher_JavaVersionResult("bin/java", spec)), logged);
}
}
2 changes: 1 addition & 1 deletion core/src/test/java/hudson/util/SecretRewriterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private String roundtrip(String before) throws Exception {
private String encryptOld(String str) throws Exception {
Cipher cipher = Secret.getCipher("AES");
cipher.init(Cipher.ENCRYPT_MODE, HistoricalSecrets.getLegacyKey());
return new String(Base64.getEncoder().encode(cipher.doFinal((str + HistoricalSecrets.MAGIC).getBytes(StandardCharsets.UTF_8))));
return Base64.getEncoder().encodeToString(cipher.doFinal((str + HistoricalSecrets.MAGIC).getBytes(StandardCharsets.UTF_8)));
}

private String encryptNew(String str) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/test/java/hudson/util/SecretTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void migrationFromLegacyKeyToConfidentialStore() throws Exception {
for (String str : new String[] {"Hello world", "", "\u0000unprintable"}) {
Cipher cipher = Secret.getCipher("AES");
cipher.init(Cipher.ENCRYPT_MODE, legacy);
String old = new String(Base64.getEncoder().encode(cipher.doFinal((str + HistoricalSecrets.MAGIC).getBytes(StandardCharsets.UTF_8))));
String old = Base64.getEncoder().encodeToString(cipher.doFinal((str + HistoricalSecrets.MAGIC).getBytes(StandardCharsets.UTF_8)));
Secret s = Secret.fromString(old);
assertEquals("secret by the old key should decrypt", str, s.getPlainText());
assertNotEquals("but when encrypting, ConfidentialKey should be in use", old, s.getEncryptedValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import hudson.Functions;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -24,7 +26,7 @@ public class RewindableRotatingFileOutputStreamTest {
public void rotation() throws IOException, InterruptedException {
File base = tmp.newFile("test.log");
RewindableRotatingFileOutputStream os = new RewindableRotatingFileOutputStream(base, 3);
PrintWriter w = new PrintWriter(os, true);
PrintWriter w = new PrintWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8), true);
for (int i = 0; i <= 4; i++) {
w.println("Content" + i);
os.rewind();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@
import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.net.HttpURLConnection;
import java.net.IDN;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
Expand Down Expand Up @@ -336,7 +337,7 @@ public static void main(String[] a) throws Exception {
// if the txt file contains entries not found in the html file, try again in a day or two
download(htmlFile, "https://www.iana.org/domains/root/db", timestamp);

BufferedReader br = new BufferedReader(new FileReader(txtFile));
BufferedReader br = Files.newBufferedReader(txtFile.toPath(), StandardCharsets.UTF_8);
String line;
final String header;
line = br.readLine(); // header
Expand Down Expand Up @@ -437,7 +438,7 @@ private static Map<String, String[]> getHtmlInfo(final File f) throws IOExceptio
// <td>Ålands landskapsregering</td>
final Pattern comment = Pattern.compile("\\s+<td>([^<]+)</td>");

final BufferedReader br = new BufferedReader(new FileReader(f));
final BufferedReader br = Files.newBufferedReader(f.toPath(), StandardCharsets.UTF_8);
String line;
while ((line = br.readLine()) != null) {
Matcher m = domain.matcher(line);
Expand Down Expand Up @@ -541,7 +542,7 @@ private static boolean isNotInRootZone(String domain) {
BufferedReader in = null;
try {
download(rootCheck, tldurl, 0L);
in = new BufferedReader(new FileReader(rootCheck));
in = Files.newBufferedReader(rootCheck.toPath(), StandardCharsets.UTF_8);
String inputLine;
while ((inputLine = in.readLine()) != null) {
if (inputLine.contains("This domain is not present in the root zone at this time.")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.junit.Assert.assertArrayEquals;

import java.nio.charset.StandardCharsets;
import org.junit.Rule;
import org.junit.Test;

Expand All @@ -15,21 +16,21 @@ public class CryptoConfidentialKeyTest {
@Test
public void decryptGetsPlainTextBack() throws Exception {
for (String str : new String[] {"Hello world", "", "\u0000"}) {
assertArrayEquals(str.getBytes(), key.decrypt().doFinal(key.encrypt().doFinal(str.getBytes())));
assertArrayEquals(str.getBytes(StandardCharsets.UTF_8), key.decrypt().doFinal(key.encrypt().doFinal(str.getBytes(StandardCharsets.UTF_8))));
}
}

@Test
public void multipleEncryptsAreIdempotent() throws Exception {
byte[] str = "Hello world".getBytes();
byte[] str = "Hello world".getBytes(StandardCharsets.UTF_8);
assertArrayEquals(key.encrypt().doFinal(str), key.encrypt().doFinal(str));
}

@Test
public void loadingExistingKey() throws Exception {
CryptoConfidentialKey key2 = new CryptoConfidentialKey("test"); // this will cause the key to be loaded from the disk
for (String str : new String[] {"Hello world", "", "\u0000"}) {
assertArrayEquals(str.getBytes(), key2.decrypt().doFinal(key.encrypt().doFinal(str.getBytes())));
assertArrayEquals(str.getBytes(StandardCharsets.UTF_8), key2.decrypt().doFinal(key.encrypt().doFinal(str.getBytes(StandardCharsets.UTF_8))));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import hudson.FilePath;
import hudson.Functions;
import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
Expand All @@ -30,14 +30,14 @@ public void roundtrip() throws Exception {

// basic roundtrip
String str = "Hello world!";
store.store(key, str.getBytes());
assertEquals(str, new String(store.load(key)));
store.store(key, str.getBytes(StandardCharsets.UTF_8));
assertEquals(str, new String(store.load(key), StandardCharsets.UTF_8));

// data storage should have some stuff
assertTrue(new File(tmp, "test").exists());
assertTrue(new File(tmp, "master.key").exists());

assertThat(FileUtils.readFileToString(new File(tmp, "test"), Charset.defaultCharset()), not(containsString("Hello"))); // the data shouldn't be a plain text, obviously
assertThat(FileUtils.readFileToString(new File(tmp, "test"), StandardCharsets.UTF_8), not(containsString("Hello"))); // the data shouldn't be a plain text, obviously

if (!Functions.isWindows()) {
assertEquals(0700, new FilePath(tmp).mode() & 0777); // should be read only
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/java/jenkins/util/io/PathRemoverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@

import hudson.Functions;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
Expand Down Expand Up @@ -454,7 +455,7 @@ private static void mkdirs(File... dirs) {

private static void touchWithFileName(File... files) throws IOException {
for (File file : files) {
try (FileWriter writer = new FileWriter(file)) {
try (Writer writer = Files.newBufferedWriter(file.toPath(), Charset.defaultCharset())) {
writer.append(file.getName()).append(System.lineSeparator());
}
assertTrue(file.isFile());
Expand Down
Loading

0 comments on commit 520e114

Please sign in to comment.