Skip to content

Commit b471413

Browse files
committed
add b64 tests
1 parent 0c2b025 commit b471413

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

server/src/test/java/password/pwm/util/java/StringUtilTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,95 @@ public void whitespaceInsertAndStrip()
4444
Assert.assertEquals( original, stripped );
4545

4646
}
47+
48+
private static byte[] makeB64inputByteArray()
49+
{
50+
final int byteLength = 256;
51+
final byte[] inputArray = new byte[byteLength];
52+
53+
byte nextByte = 0;
54+
for ( int i = 0; i < byteLength; i++ )
55+
{
56+
inputArray[i] = nextByte;
57+
nextByte += i;
58+
}
59+
60+
return inputArray;
61+
}
62+
63+
private static final String B64_TEST = "AAABAwYKDxUcJC03Qk5baXiImau+0uf9FCxFX3qWs9HwEDFTdpq/5Qw0XYey3gs5aJjJ+y5il80EPHWv6iZjoeAgYaPm"
64+
+ "Km+1/ESN1yJuuwlYqPlLnvJHnfRMpf9athNx0DCR81a6H4XsVL0nkv5r2Ui4KZsOgvdt5FzVT8pGw0HAQMFDxkrPVdxk7XcCjhupOMhZ634Sp"
65+
+ "z3UbAWfOtZzEbBQ8ZM22n8lzHQdx3Iey3ko2Ik77qJXDcR8Ne+qZiPhoGAh46ZqL/W8hE0X4q57SRjouYteMgfdtIxlPxr207GQcFEzFvrfxa"
66+
+ "yUfWdSPisZCPjp287Ct62knJWPioaDgQ==";
67+
68+
private static final String B64_TEST_URL_SAFE = "AAABAwYKDxUcJC03Qk5baXiImau-0uf9FCxFX3qWs9HwEDFTdpq_5Qw0XYey3gs5aJjJ-y5il80EPHWv6iZjoeAgYaPm"
69+
+ "Km-1_ESN1yJuuwlYqPlLnvJHnfRMpf9athNx0DCR81a6H4XsVL0nkv5r2Ui4KZsOgvdt5FzVT8pGw0HAQMFDxkrPVdxk7XcCjhupOMhZ634Sp"
70+
+ "z3UbAWfOtZzEbBQ8ZM22n8lzHQdx3Iey3ko2Ik77qJXDcR8Ne-qZiPhoGAh46ZqL_W8hE0X4q57SRjouYteMgfdtIxlPxr207GQcFEzFvrfxa"
71+
+ "yUfWdSPisZCPjp287Ct62knJWPioaDgQ==";
72+
73+
private static final String B64_TEST_GZIP = "H4sIAAAAAAAAAAEAAf/+AAABAwYKDxUcJC03Qk5baXiImau+0uf9FCxFX3qWs9HwEDFTdpq/5Qw0XYey3gs5aJjJ+y5il"
74+
+ "80EPHWv6iZjoeAgYaPmKm+1/ESN1yJuuwlYqPlLnvJHnfRMpf9athNx0DCR81a6H4XsVL0nkv5r2Ui4KZsOgvdt5FzVT8pGw0HAQMFDxkrPVdx"
75+
+ "k7XcCjhupOMhZ634Spz3UbAWfOtZzEbBQ8ZM22n8lzHQdx3Iey3ko2Ik77qJXDcR8Ne+qZiPhoGAh46ZqL/W8hE0X4q57SRjouYteMgfdtIxlP"
76+
+ "xr207GQcFEzFvrfxayUfWdSPisZCPjp287Ct62knJWPioaDgR3bmXcAAQAA";
77+
78+
private static final String B64_TEST_GZIP_URL_SAFE = "H4sIAAAAAAAAAAEAAf_-AAABAwYKDxUcJC03Qk5baXiImau-0uf9FCxFX3qWs9HwEDFTdpq_5Qw0XYey3gs5aJjJ-y5il"
79+
+ "80EPHWv6iZjoeAgYaPmKm-1_ESN1yJuuwlYqPlLnvJHnfRMpf9athNx0DCR81a6H4XsVL0nkv5r2Ui4KZsOgvdt5FzVT8pGw0HAQMFDxkrPVdx"
80+
+ "k7XcCjhupOMhZ634Spz3UbAWfOtZzEbBQ8ZM22n8lzHQdx3Iey3ko2Ik77qJXDcR8Ne-qZiPhoGAh46ZqL_W8hE0X4q57SRjouYteMgfdtIxlP"
81+
+ "xr207GQcFEzFvrfxayUfWdSPisZCPjp287Ct62knJWPioaDgR3bmXcAAQAA";
82+
83+
@Test
84+
public void base64TestEncode() throws Exception
85+
{
86+
final String b64string = StringUtil.base64Encode( makeB64inputByteArray() );
87+
Assert.assertEquals( B64_TEST, b64string );
88+
}
89+
90+
@Test
91+
public void base64TestDecode() throws Exception
92+
{
93+
final byte[] b64array = StringUtil.base64Decode( B64_TEST );
94+
Assert.assertArrayEquals( makeB64inputByteArray(), b64array );
95+
}
96+
97+
@Test
98+
public void base64TestEncodeUrlSafe() throws Exception
99+
{
100+
final String b64string = StringUtil.base64Encode( makeB64inputByteArray(), StringUtil.Base64Options.URL_SAFE );
101+
Assert.assertEquals( B64_TEST_URL_SAFE, b64string );
102+
}
103+
104+
@Test
105+
public void base64TestDecodeUrlSafe() throws Exception
106+
{
107+
final byte[] b64array = StringUtil.base64Decode( B64_TEST_URL_SAFE, StringUtil.Base64Options.URL_SAFE );
108+
Assert.assertArrayEquals( makeB64inputByteArray(), b64array );
109+
}
110+
111+
@Test
112+
public void base64TestEncodeGzipAndUrlSafe() throws Exception
113+
{
114+
final String b64string = StringUtil.base64Encode( makeB64inputByteArray(), StringUtil.Base64Options.URL_SAFE, StringUtil.Base64Options.GZIP );
115+
Assert.assertEquals( B64_TEST_GZIP_URL_SAFE, b64string );
116+
}
117+
118+
@Test
119+
public void base64TestDecodeGzipAndUrlSafe() throws Exception
120+
{
121+
final byte[] b64array = StringUtil.base64Decode( B64_TEST_GZIP_URL_SAFE, StringUtil.Base64Options.URL_SAFE, StringUtil.Base64Options.GZIP );
122+
Assert.assertArrayEquals( makeB64inputByteArray(), b64array );
123+
}
124+
125+
@Test
126+
public void base64TestEncodeGzip() throws Exception
127+
{
128+
final String b64string = StringUtil.base64Encode( makeB64inputByteArray(), StringUtil.Base64Options.GZIP );
129+
Assert.assertEquals( B64_TEST_GZIP, b64string );
130+
}
131+
132+
@Test
133+
public void base64TestDecodeGzip() throws Exception
134+
{
135+
final byte[] b64array = StringUtil.base64Decode( B64_TEST_GZIP, StringUtil.Base64Options.GZIP );
136+
Assert.assertArrayEquals( makeB64inputByteArray(), b64array );
137+
}
47138
}

0 commit comments

Comments
 (0)