1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.crypto.internal.encoder; |
21 |
|
|
22 |
|
import java.io.ByteArrayInputStream; |
23 |
|
import java.io.ByteArrayOutputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.io.InputStream; |
26 |
|
import java.io.OutputStream; |
27 |
|
|
28 |
|
import org.junit.BeforeClass; |
29 |
|
import org.junit.Test; |
30 |
|
import org.xwiki.crypto.BinaryStringEncoder; |
31 |
|
|
32 |
|
import static org.hamcrest.CoreMatchers.equalTo; |
33 |
|
import static org.junit.Assert.assertThat; |
34 |
|
|
|
|
| 97.5% |
Uncovered Elements: 2 (81) |
Complexity: 13 |
Complexity Density: 0.19 |
|
35 |
|
public abstract class AbstractBinaryStringEncoderTest |
36 |
|
{ |
37 |
|
private static final String CHARSET = "UTF-8"; |
38 |
|
|
39 |
|
private static final String TEXT = "Kryptographie (von griechisch: \u03ba\u03c1\u03c5\u03c0\u03c4\u03cc\u03c2," |
40 |
|
+ " \u201everborgen\u201c und \u03b3\u03c1\u03ac\u03c6\u03b5\u03b9\u03bd," |
41 |
|
+ " \u201eschreiben\u201c) ist die Wissenschaft der Verschl\u00fcsselung von" |
42 |
|
+ " Informationen."; |
43 |
|
|
44 |
|
private static byte[] BYTES; |
45 |
|
|
46 |
|
protected String ENCODED_BYTES; |
47 |
|
|
48 |
|
protected String WRAPPED_ENCODED_BYTES; |
49 |
|
|
50 |
|
protected BinaryStringEncoder encoder; |
51 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
52 |
3 |
@BeforeClass... |
53 |
|
public static void initialize() throws Exception |
54 |
|
{ |
55 |
3 |
BYTES = TEXT.getBytes(CHARSET); |
56 |
|
} |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
58 |
3 |
@Test... |
59 |
|
public void testEncode() throws Exception |
60 |
|
{ |
61 |
3 |
assertThat(encoder.encode(BYTES), equalTo(ENCODED_BYTES)); |
62 |
3 |
assertThat(encoder.encode(BYTES, 64), equalTo(WRAPPED_ENCODED_BYTES)); |
63 |
3 |
assertThat(encoder.encode(BYTES, 0, BYTES.length), |
64 |
|
equalTo(ENCODED_BYTES)); |
65 |
3 |
assertThat(encoder.encode(BYTES, 0, BYTES.length, 64), |
66 |
|
equalTo(WRAPPED_ENCODED_BYTES)); |
67 |
|
|
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
4-
|
|
70 |
3 |
@Test... |
71 |
|
public void testDecode() throws Exception |
72 |
|
{ |
73 |
3 |
assertThat(encoder.decode(ENCODED_BYTES), equalTo(BYTES)); |
74 |
3 |
assertThat(encoder.decode(WRAPPED_ENCODED_BYTES), equalTo(BYTES)); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
4-
|
|
77 |
3 |
@Test... |
78 |
|
public void testEncoderStream() throws Exception |
79 |
|
{ |
80 |
3 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
81 |
3 |
OutputStream encos = encoder.getEncoderOutputStream(baos); |
82 |
|
|
83 |
3 |
encos.write(BYTES, 0, 17); |
84 |
3 |
encos.write(BYTES, 17, 7); |
85 |
3 |
encos.write(BYTES, 24, 1); |
86 |
3 |
encos.write(BYTES, 25, 1); |
87 |
3 |
encos.write(BYTES, 26, 1); |
88 |
3 |
encos.write(BYTES, 27, 1); |
89 |
3 |
encos.write(BYTES, 28, 1); |
90 |
3 |
encos.write(BYTES, 29, BYTES.length - 29); |
91 |
3 |
encos.close(); |
92 |
|
|
93 |
3 |
assertThat(baos.toString(), equalTo(ENCODED_BYTES)); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
4-
|
|
96 |
3 |
@Test... |
97 |
|
public void testEncoderWrappedStream() throws Exception |
98 |
|
{ |
99 |
3 |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
100 |
3 |
OutputStream encos = encoder.getEncoderOutputStream(baos, 64); |
101 |
|
|
102 |
3 |
encos.write(BYTES, 0, 17); |
103 |
3 |
encos.write(BYTES, 17, 7); |
104 |
3 |
encos.write(BYTES, 24, 1); |
105 |
3 |
encos.write(BYTES, 25, 1); |
106 |
3 |
encos.write(BYTES, 26, 1); |
107 |
3 |
encos.write(BYTES, 27, 1); |
108 |
3 |
encos.write(BYTES, 28, 1); |
109 |
3 |
encos.write(BYTES, 29, BYTES.length - 29); |
110 |
3 |
encos.close(); |
111 |
|
|
112 |
3 |
assertThat(baos.toString(), equalTo(WRAPPED_ENCODED_BYTES)); |
113 |
|
} |
114 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 2 |
Complexity Density: 0.33 |
|
115 |
12 |
private int readAll(InputStream is, byte[] out) throws IOException... |
116 |
|
{ |
117 |
12 |
int readLen, len = 0; |
118 |
12 |
int blen = 17; |
119 |
? |
while( (readLen = is.read(out, len, blen)) > 0 ) { |
120 |
120 |
len += readLen; |
121 |
|
} |
122 |
12 |
is.close(); |
123 |
12 |
return len; |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
126 |
3 |
@Test... |
127 |
|
public void testDecoderStream() throws Exception |
128 |
|
{ |
129 |
3 |
ByteArrayInputStream bais = new ByteArrayInputStream(ENCODED_BYTES.getBytes()); |
130 |
3 |
InputStream decis = encoder.getDecoderInputStream(bais); |
131 |
3 |
byte[] buf = new byte[187]; |
132 |
3 |
assertThat(readAll(decis, buf), equalTo(BYTES.length)); |
133 |
3 |
byte[] buf2 = new byte[BYTES.length]; |
134 |
3 |
System.arraycopy(buf, 0, buf2, 0, BYTES.length); |
135 |
3 |
assertThat(buf2, equalTo(BYTES)); |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
138 |
3 |
@Test... |
139 |
|
public void testDecoderWrappedStream() throws Exception |
140 |
|
{ |
141 |
3 |
ByteArrayInputStream bais = new ByteArrayInputStream(WRAPPED_ENCODED_BYTES.getBytes()); |
142 |
3 |
InputStream decis = encoder.getDecoderInputStream(bais); |
143 |
3 |
byte[] buf = new byte[187]; |
144 |
3 |
assertThat(readAll(decis, buf), equalTo(BYTES.length)); |
145 |
3 |
byte[] buf2 = new byte[BYTES.length]; |
146 |
3 |
System.arraycopy(buf, 0, buf2, 0, BYTES.length); |
147 |
3 |
assertThat(buf2, equalTo(BYTES)); |
148 |
|
} |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
150 |
3 |
@Test... |
151 |
|
public void testDecoderStreamNoMark() throws Exception |
152 |
|
{ |
153 |
3 |
ByteArrayInputStream bais = new ByteArrayInputStream(ENCODED_BYTES.getBytes()) { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
154 |
33 |
@Override... |
155 |
|
public boolean markSupported() |
156 |
|
{ |
157 |
33 |
return false; |
158 |
|
} |
159 |
|
}; |
160 |
3 |
InputStream decis = encoder.getDecoderInputStream(bais); |
161 |
3 |
byte[] buf = new byte[187]; |
162 |
3 |
assertThat(readAll(decis, buf), equalTo(BYTES.length)); |
163 |
3 |
byte[] buf2 = new byte[BYTES.length]; |
164 |
3 |
System.arraycopy(buf, 0, buf2, 0, BYTES.length); |
165 |
3 |
assertThat(buf2, equalTo(BYTES)); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
4-
|
|
168 |
3 |
@Test... |
169 |
|
public void testDecoderWrappedStreamNoMark() throws Exception |
170 |
|
{ |
171 |
3 |
ByteArrayInputStream bais = new ByteArrayInputStream(WRAPPED_ENCODED_BYTES.getBytes()) { |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
172 |
44 |
@Override... |
173 |
|
public boolean markSupported() |
174 |
|
{ |
175 |
44 |
return false; |
176 |
|
} |
177 |
|
}; |
178 |
3 |
InputStream decis = encoder.getDecoderInputStream(bais); |
179 |
3 |
byte[] buf = new byte[187]; |
180 |
3 |
assertThat(readAll(decis, buf), equalTo(BYTES.length)); |
181 |
3 |
byte[] buf2 = new byte[BYTES.length]; |
182 |
3 |
System.arraycopy(buf, 0, buf2, 0, BYTES.length); |
183 |
3 |
assertThat(buf2, equalTo(BYTES)); |
184 |
|
} |
185 |
|
} |