-
Notifications
You must be signed in to change notification settings - Fork 74
/
EncodingTestCase.java
70 lines (51 loc) · 1.84 KB
/
EncodingTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package org.meri.shiro.cryptographydemo;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.codec.CodecSupport;
import org.apache.shiro.codec.Hex;
import org.apache.shiro.util.ByteSource;
import org.junit.Test;
public class EncodingTestCase extends CryptoTestCase {
@Test
public void textToByteArray() {
String encodeMe = "Hello, I'm a text.";
byte[] bytes = CodecSupport.toBytes(encodeMe);
String decoded = CodecSupport.toString(bytes);
assertEquals(encodeMe, decoded);
}
@Test
public void testByteSourceHexadecimal() {
byte[] encodeMe = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
ByteSource byteSource = ByteSource.Util.bytes(encodeMe);
String hexadecimal = byteSource.toHex();
assertEquals("020406080a0c0e101214", hexadecimal);
byte[] decoded = Hex.decode(hexadecimal);
assertArrayEquals(encodeMe, decoded);
}
@Test
public void testByteSourceBase64() {
byte[] encodeMe = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
ByteSource byteSource = ByteSource.Util.bytes(encodeMe);
String base64 = byteSource.toBase64();
assertEquals("AgQGCAoMDhASFA==", base64);
byte[] decoded = Base64.decode(base64);
assertArrayEquals(encodeMe, decoded);
}
@Test
public void testStaticHexadecimal() {
byte[] encodeMe = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
String hexadecimal = Hex.encodeToString(encodeMe);
assertEquals("020406080a0c0e101214", hexadecimal);
byte[] decoded = Hex.decode(hexadecimal);
assertArrayEquals(encodeMe, decoded);
}
@Test
public void testStaticBase64() {
byte[] encodeMe = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
String base64 = Base64.encodeToString(encodeMe);
assertEquals("AgQGCAoMDhASFA==", base64);
byte[] decoded = Base64.decode(base64);
assertArrayEquals(encodeMe, decoded);
}
}