Skip to content

Commit 5193321

Browse files
committed
getBytes() relies on the current encoding; use UTF-8 for portability
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@928757 13f79535-47bb-0310-9956-ffa450edef68
1 parent 04fe97f commit 5193321

5 files changed

Lines changed: 102 additions & 99 deletions

File tree

src/test/org/apache/commons/codec/binary/Base64Test.java

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.apache.commons.codec.binary;
1919

20+
import java.io.UnsupportedEncodingException;
2021
import java.math.BigInteger;
2122
import java.util.Arrays;
2223
import java.util.Random;
@@ -100,33 +101,33 @@ public void testCodec68() {
100101
Base64.decodeBase64(x);
101102
}
102103

103-
public void testCodeInteger1() {
104+
public void testCodeInteger1() throws UnsupportedEncodingException {
104105
String encodedInt1 = "li7dzDacuo67Jg7mtqEm2TRuOMU=";
105106
BigInteger bigInt1 = new BigInteger("85739377120809420210425962799" + "0318636601332086981");
106107

107108
assertEquals(encodedInt1, new String(Base64.encodeInteger(bigInt1)));
108-
assertEquals(bigInt1, Base64.decodeInteger(encodedInt1.getBytes()));
109+
assertEquals(bigInt1, Base64.decodeInteger(encodedInt1.getBytes("UTF-8")));
109110
}
110111

111-
public void testCodeInteger2() {
112+
public void testCodeInteger2() throws UnsupportedEncodingException {
112113
String encodedInt2 = "9B5ypLY9pMOmtxCeTDHgwdNFeGs=";
113114
BigInteger bigInt2 = new BigInteger("13936727572861167254666467268" + "91466679477132949611");
114115

115116
assertEquals(encodedInt2, new String(Base64.encodeInteger(bigInt2)));
116-
assertEquals(bigInt2, Base64.decodeInteger(encodedInt2.getBytes()));
117+
assertEquals(bigInt2, Base64.decodeInteger(encodedInt2.getBytes("UTF-8")));
117118
}
118119

119-
public void testCodeInteger3() {
120+
public void testCodeInteger3() throws UnsupportedEncodingException {
120121
String encodedInt3 = "FKIhdgaG5LGKiEtF1vHy4f3y700zaD6QwDS3IrNVGzNp2" + "rY+1LFWTK6D44AyiC1n8uWz1itkYMZF0/aKDK0Yjg==";
121122
BigInteger bigInt3 = new BigInteger("10806548154093873461951748545"
122123
+ "1196989136416448805819079363524309897749044958112417136240557"
123124
+ "4495062430572478766856090958495998158114332651671116876320938126");
124125

125126
assertEquals(encodedInt3, new String(Base64.encodeInteger(bigInt3)));
126-
assertEquals(bigInt3, Base64.decodeInteger(encodedInt3.getBytes()));
127+
assertEquals(bigInt3, Base64.decodeInteger(encodedInt3.getBytes("UTF-8")));
127128
}
128129

129-
public void testCodeInteger4() {
130+
public void testCodeInteger4() throws UnsupportedEncodingException {
130131
String encodedInt4 = "ctA8YGxrtngg/zKVvqEOefnwmViFztcnPBYPlJsvh6yKI"
131132
+ "4iDm68fnp4Mi3RrJ6bZAygFrUIQLxLjV+OJtgJAEto0xAs+Mehuq1DkSFEpP3o"
132133
+ "DzCTOsrOiS1DwQe4oIb7zVk/9l7aPtJMHW0LVlMdwZNFNNJoqMcT2ZfCPrfvYv"
@@ -139,7 +140,7 @@ public void testCodeInteger4() {
139140
+ "53542091716518238707344493641683483917");
140141

141142
assertEquals(encodedInt4, new String(Base64.encodeInteger(bigInt4)));
142-
assertEquals(bigInt4, Base64.decodeInteger(encodedInt4.getBytes()));
143+
assertEquals(bigInt4, Base64.decodeInteger(encodedInt4.getBytes("UTF-8")));
143144
}
144145

145146
public void testCodeIntegerEdgeCases() {
@@ -216,51 +217,51 @@ public void testConstructor_Int_ByteArray_Boolean_UrlSafe() {
216217
/**
217218
* Tests conditional true branch for "marker0" test.
218219
*/
219-
public void testDecodePadMarkerIndex2() {
220-
assertEquals("A", new String(Base64.decodeBase64("QQ==".getBytes())));
220+
public void testDecodePadMarkerIndex2() throws UnsupportedEncodingException {
221+
assertEquals("A", new String(Base64.decodeBase64("QQ==".getBytes("UTF-8"))));
221222
}
222223

223224
/**
224225
* Tests conditional branches for "marker1" test.
225226
*/
226-
public void testDecodePadMarkerIndex3() {
227-
assertEquals("AA", new String(Base64.decodeBase64("QUE=".getBytes())));
228-
assertEquals("AAA", new String(Base64.decodeBase64("QUFB".getBytes())));
227+
public void testDecodePadMarkerIndex3() throws UnsupportedEncodingException {
228+
assertEquals("AA", new String(Base64.decodeBase64("QUE=".getBytes("UTF-8"))));
229+
assertEquals("AAA", new String(Base64.decodeBase64("QUFB".getBytes("UTF-8"))));
229230
}
230231

231-
public void testDecodePadOnly() {
232-
assertTrue(Base64.decodeBase64("====".getBytes()).length == 0);
233-
assertEquals("", new String(Base64.decodeBase64("====".getBytes())));
232+
public void testDecodePadOnly() throws UnsupportedEncodingException {
233+
assertTrue(Base64.decodeBase64("====".getBytes("UTF-8")).length == 0);
234+
assertEquals("", new String(Base64.decodeBase64("====".getBytes("UTF-8"))));
234235
// Test truncated padding
235-
assertTrue(Base64.decodeBase64("===".getBytes()).length == 0);
236-
assertTrue(Base64.decodeBase64("==".getBytes()).length == 0);
237-
assertTrue(Base64.decodeBase64("=".getBytes()).length == 0);
238-
assertTrue(Base64.decodeBase64("".getBytes()).length == 0);
236+
assertTrue(Base64.decodeBase64("===".getBytes("UTF-8")).length == 0);
237+
assertTrue(Base64.decodeBase64("==".getBytes("UTF-8")).length == 0);
238+
assertTrue(Base64.decodeBase64("=".getBytes("UTF-8")).length == 0);
239+
assertTrue(Base64.decodeBase64("".getBytes("UTF-8")).length == 0);
239240
}
240241

241-
public void testDecodePadOnlyChunked() {
242-
assertTrue(Base64.decodeBase64("====\n".getBytes()).length == 0);
243-
assertEquals("", new String(Base64.decodeBase64("====\n".getBytes())));
242+
public void testDecodePadOnlyChunked() throws UnsupportedEncodingException {
243+
assertTrue(Base64.decodeBase64("====\n".getBytes("UTF-8")).length == 0);
244+
assertEquals("", new String(Base64.decodeBase64("====\n".getBytes("UTF-8"))));
244245
// Test truncated padding
245-
assertTrue(Base64.decodeBase64("===\n".getBytes()).length == 0);
246-
assertTrue(Base64.decodeBase64("==\n".getBytes()).length == 0);
247-
assertTrue(Base64.decodeBase64("=\n".getBytes()).length == 0);
248-
assertTrue(Base64.decodeBase64("\n".getBytes()).length == 0);
246+
assertTrue(Base64.decodeBase64("===\n".getBytes("UTF-8")).length == 0);
247+
assertTrue(Base64.decodeBase64("==\n".getBytes("UTF-8")).length == 0);
248+
assertTrue(Base64.decodeBase64("=\n".getBytes("UTF-8")).length == 0);
249+
assertTrue(Base64.decodeBase64("\n".getBytes("UTF-8")).length == 0);
249250
}
250251

251252
public void testDecodeWithWhitespace() throws Exception {
252253

253254
String orig = "I am a late night coder.";
254255

255-
byte[] encodedArray = Base64.encodeBase64(orig.getBytes());
256+
byte[] encodedArray = Base64.encodeBase64(orig.getBytes("UTF-8"));
256257
StringBuffer intermediate = new StringBuffer(new String(encodedArray));
257258

258259
intermediate.insert(2, ' ');
259260
intermediate.insert(5, '\t');
260261
intermediate.insert(10, '\r');
261262
intermediate.insert(15, '\n');
262263

263-
byte[] encodedWithWS = intermediate.toString().getBytes();
264+
byte[] encodedWithWS = intermediate.toString().getBytes("UTF-8");
264265
byte[] decodedWithWS = Base64.decodeBase64(encodedWithWS);
265266

266267
String dest = new String(decodedWithWS);
@@ -272,15 +273,15 @@ public void testDiscardWhitespace() throws Exception {
272273

273274
String orig = "I am a late night coder.";
274275

275-
byte[] encodedArray = Base64.encodeBase64(orig.getBytes());
276+
byte[] encodedArray = Base64.encodeBase64(orig.getBytes("UTF-8"));
276277
StringBuffer intermediate = new StringBuffer(new String(encodedArray));
277278

278279
intermediate.insert(2, ' ');
279280
intermediate.insert(5, '\t');
280281
intermediate.insert(10, '\r');
281282
intermediate.insert(15, '\n');
282283

283-
byte[] encodedWithWS = intermediate.toString().getBytes();
284+
byte[] encodedWithWS = intermediate.toString().getBytes("UTF-8");
284285
byte[] encodedNoWS = Base64.discardWhitespace(encodedWithWS);
285286
byte[] decodedWithWS = Base64.decodeBase64(encodedWithWS);
286287
byte[] decodedNoWS = Base64.decodeBase64(encodedNoWS);
@@ -349,7 +350,7 @@ private void testEncodeOverMaxSize(int maxSize) throws Exception {
349350

350351
public void testIgnoringNonBase64InDecode() throws Exception {
351352
assertEquals("The quick brown fox jumped over the lazy dogs.", new String(Base64
352-
.decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes())));
353+
.decodeBase64("VGhlIH@$#$@%F1aWN@#@#@@rIGJyb3duIGZve\n\r\t%#%#%#%CBqd##$#$W1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes("UTF-8"))));
353354
}
354355

355356
public void testIsArrayByteBase64() {
@@ -380,38 +381,38 @@ public void testIsUrlSafe() {
380381
assertTrue("Base64.isArrayByteBase64(whiteSpace)=true", Base64.isArrayByteBase64(whiteSpace));
381382
}
382383

383-
public void testKnownDecodings() {
384+
public void testKnownDecodings() throws UnsupportedEncodingException {
384385
assertEquals("The quick brown fox jumped over the lazy dogs.", new String(Base64
385-
.decodeBase64("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes())));
386+
.decodeBase64("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==".getBytes("UTF-8"))));
386387
assertEquals("It was the best of times, it was the worst of times.", new String(Base64
387-
.decodeBase64("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==".getBytes())));
388+
.decodeBase64("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==".getBytes("UTF-8"))));
388389
assertEquals("http://jakarta.apache.org/commmons", new String(Base64
389-
.decodeBase64("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==".getBytes())));
390+
.decodeBase64("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==".getBytes("UTF-8"))));
390391
assertEquals("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz", new String(Base64
391-
.decodeBase64("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==".getBytes())));
392+
.decodeBase64("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==".getBytes("UTF-8"))));
392393
assertEquals("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }", new String(Base64.decodeBase64("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0="
393-
.getBytes())));
394-
assertEquals("xyzzy!", new String(Base64.decodeBase64("eHl6enkh".getBytes())));
394+
.getBytes("UTF-8"))));
395+
assertEquals("xyzzy!", new String(Base64.decodeBase64("eHl6enkh".getBytes("UTF-8"))));
395396
}
396397

397-
public void testKnownEncodings() {
398+
public void testKnownEncodings() throws UnsupportedEncodingException {
398399
assertEquals("VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wZWQgb3ZlciB0aGUgbGF6eSBkb2dzLg==", new String(Base64
399-
.encodeBase64("The quick brown fox jumped over the lazy dogs.".getBytes())));
400+
.encodeBase64("The quick brown fox jumped over the lazy dogs.".getBytes("UTF-8"))));
400401
assertEquals(
401402
"YmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJs\r\nYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFo\r\nIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBibGFoIGJsYWggYmxhaCBi\r\nbGFoIGJsYWg=\r\n",
402403
new String(
403404
Base64
404405
.encodeBase64Chunked("blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah"
405-
.getBytes())));
406+
.getBytes("UTF-8"))));
406407
assertEquals("SXQgd2FzIHRoZSBiZXN0IG9mIHRpbWVzLCBpdCB3YXMgdGhlIHdvcnN0IG9mIHRpbWVzLg==", new String(Base64
407-
.encodeBase64("It was the best of times, it was the worst of times.".getBytes())));
408+
.encodeBase64("It was the best of times, it was the worst of times.".getBytes("UTF-8"))));
408409
assertEquals("aHR0cDovL2pha2FydGEuYXBhY2hlLm9yZy9jb21tbW9ucw==", new String(Base64
409-
.encodeBase64("http://jakarta.apache.org/commmons".getBytes())));
410+
.encodeBase64("http://jakarta.apache.org/commmons".getBytes("UTF-8"))));
410411
assertEquals("QWFCYkNjRGRFZUZmR2dIaElpSmpLa0xsTW1Obk9vUHBRcVJyU3NUdFV1VnZXd1h4WXlaeg==", new String(Base64
411-
.encodeBase64("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes())));
412+
.encodeBase64("AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz".getBytes("UTF-8"))));
412413
assertEquals("eyAwLCAxLCAyLCAzLCA0LCA1LCA2LCA3LCA4LCA5IH0=", new String(Base64.encodeBase64("{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }"
413-
.getBytes())));
414-
assertEquals("eHl6enkh", new String(Base64.encodeBase64("xyzzy!".getBytes())));
414+
.getBytes("UTF-8"))));
415+
assertEquals("eHl6enkh", new String(Base64.encodeBase64("xyzzy!".getBytes("UTF-8"))));
415416
}
416417

417418
public void testNonBase64Test() throws Exception {
@@ -450,7 +451,7 @@ public void testObjectDecodeWithInvalidParameter() throws Exception {
450451
public void testObjectDecodeWithValidParameter() throws Exception {
451452

452453
String original = "Hello World!";
453-
Object o = Base64.encodeBase64(original.getBytes());
454+
Object o = Base64.encodeBase64(original.getBytes("UTF-8"));
454455

455456
Base64 b64 = new Base64();
456457
Object oDecoded = b64.decode(o);
@@ -473,7 +474,7 @@ public void testObjectEncodeWithInvalidParameter() throws Exception {
473474
public void testObjectEncodeWithValidParameter() throws Exception {
474475

475476
String original = "Hello World!";
476-
Object origObj = original.getBytes();
477+
Object origObj = original.getBytes("UTF-8");
477478

478479
Base64 b64 = new Base64();
479480
Object oEncoded = b64.encode(origObj);

0 commit comments

Comments
 (0)