1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.crypto.internal.encoder

File AbstractBinaryStringEncoderTest.java

 

Code metrics

2
67
12
1
185
142
13
0.19
5.58
12
1.08

Classes

Class Line # Actions
AbstractBinaryStringEncoderTest 35 67 0% 13 2
0.9753086697.5%
 

Contributing tests

This file is covered by 24 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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   
 
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   
 
52  3 toggle @BeforeClass
53    public static void initialize() throws Exception
54    {
55  3 BYTES = TEXT.getBytes(CHARSET);
56    }
57   
 
58  3 toggle @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   
 
70  3 toggle @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   
 
77  3 toggle @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   
 
96  3 toggle @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   
 
115  12 toggle 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   
 
126  3 toggle @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   
 
138  3 toggle @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   
 
150  3 toggle @Test
151    public void testDecoderStreamNoMark() throws Exception
152    {
153  3 ByteArrayInputStream bais = new ByteArrayInputStream(ENCODED_BYTES.getBytes()) {
 
154  33 toggle @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   
 
168  3 toggle @Test
169    public void testDecoderWrappedStreamNoMark() throws Exception
170    {
171  3 ByteArrayInputStream bais = new ByteArrayInputStream(WRAPPED_ENCODED_BYTES.getBytes()) {
 
172  44 toggle @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    }