android: Add some (older) unit tests
[strongswan.git] / src / frontends / android / app / src / test / java / org / strongswan / android / test / BufferedByteWriterTest.java
1 /*
2 * Copyright (C) 2013-2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.test;
17
18 import org.junit.Test;
19 import org.strongswan.android.utils.BufferedByteWriter;
20
21 import static org.junit.Assert.assertArrayEquals;
22 import static org.junit.Assert.assertEquals;
23
24 public class BufferedByteWriterTest
25 {
26 byte[] testArray = {0, 1, 2, 3, 4, 5, 6, 7};
27
28 private void testArray(byte[] array, int offset)
29 {
30 for (int i = offset; i < array.length; i++)
31 {
32 assertEquals("value " + i, array[i], (i - offset) % 8);
33 }
34 }
35
36 private void testArray(byte[] array)
37 {
38 testArray(array, 0);
39 }
40
41 @Test
42 public void testPutByteArray()
43 {
44 BufferedByteWriter writer = new BufferedByteWriter();
45 byte[] array = writer.toByteArray();
46 assertEquals("length", array.length, 0);
47 writer.put(testArray);
48 array = writer.toByteArray();
49 assertEquals("length", array.length, testArray.length);
50 testArray(array);
51 }
52
53 @Test
54 public void testResize()
55 {
56 BufferedByteWriter writer = new BufferedByteWriter();
57 writer.put(testArray);
58 writer.put(testArray);
59 writer.put(testArray);
60 writer.put(testArray);
61 byte[] array = writer.toByteArray();
62 assertEquals("length", array.length, testArray.length * 4);
63 testArray(array);
64 writer.put(testArray);
65 array = writer.toByteArray();
66 assertEquals("length", array.length, testArray.length * 5);
67 testArray(array);
68 }
69
70 @Test
71 public void testResizeCapacity()
72 {
73 BufferedByteWriter writer = new BufferedByteWriter(4);
74 writer.put(testArray);
75 writer.put(testArray);
76 writer.put(testArray);
77 writer.put(testArray);
78 byte[] array = writer.toByteArray();
79 assertEquals("length", array.length, testArray.length * 4);
80 testArray(array);
81 writer.put(testArray);
82 array = writer.toByteArray();
83 assertEquals("length", array.length, testArray.length * 5);
84 testArray(array);
85 }
86
87 @Test
88 public void testPutByte()
89 {
90 BufferedByteWriter writer = new BufferedByteWriter();
91 writer.put((byte)1);
92 byte[] array = writer.toByteArray();
93 assertEquals("length", array.length, 1);
94 assertEquals("value", array[0], 1);
95 }
96
97 @Test
98 public void testPutLen8()
99 {
100 BufferedByteWriter writer = new BufferedByteWriter();
101 writer.putLen8(testArray);
102 byte[] array = writer.toByteArray();
103 assertEquals("length", array.length, testArray.length + 1);
104 assertEquals("value", array[0], testArray.length);
105 testArray(array, 1);
106 }
107
108 @Test
109 public void testPutLen16()
110 {
111 BufferedByteWriter writer = new BufferedByteWriter();
112 writer.putLen16(testArray);
113 byte[] array = writer.toByteArray();
114 assertEquals("length", array.length, testArray.length + 2);
115 assertEquals("value 0", array[0], 0);
116 assertEquals("value 1", array[1], testArray.length);
117 testArray(array, 2);
118 }
119
120 @Test
121 public void testPut16()
122 {
123 BufferedByteWriter writer = new BufferedByteWriter();
124 writer.put16((short)0xbeef);
125 byte[] exp = new byte[]{(byte)0xbe, (byte)0xef};
126 assertArrayEquals(exp, writer.toByteArray());
127 writer.put16((short)0xfeed);
128 exp = new byte[]{(byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
129 assertArrayEquals(exp, writer.toByteArray());
130 }
131
132 @Test
133 public void testPut16Byte()
134 {
135 BufferedByteWriter writer = new BufferedByteWriter();
136 writer.put16((byte)0xfe);
137 byte[] exp = new byte[]{0, (byte)0xfe};
138 assertArrayEquals(exp, writer.toByteArray());
139 }
140
141 @Test
142 public void testPut24Half()
143 {
144 BufferedByteWriter writer = new BufferedByteWriter();
145 writer.put24(0xbeef);
146 byte[] exp = new byte[]{0, (byte)0xbe, (byte)0xef};
147 assertArrayEquals(exp, writer.toByteArray());
148 }
149
150 @Test
151 public void testPut24Full()
152 {
153 BufferedByteWriter writer = new BufferedByteWriter();
154 writer.put24(0xbeeffe);
155 byte[] exp = new byte[]{(byte)0xbe, (byte)0xef, (byte)0xfe};
156 assertArrayEquals(exp, writer.toByteArray());
157 }
158
159 @Test
160 public void testPut24Int()
161 {
162 BufferedByteWriter writer = new BufferedByteWriter();
163 writer.put24(0xcafebeef);
164 byte[] exp = new byte[]{(byte)0xfe, (byte)0xbe, (byte)0xef};
165 assertArrayEquals(exp, writer.toByteArray());
166 }
167
168 @Test
169 public void testPut24Short()
170 {
171 BufferedByteWriter writer = new BufferedByteWriter();
172 writer.put24((short)0xfeed);
173 byte[] exp = new byte[]{0, (byte)0xfe, (byte)0xed};
174 assertArrayEquals(exp, writer.toByteArray());
175 }
176
177 @Test
178 public void testPut24Byte()
179 {
180 BufferedByteWriter writer = new BufferedByteWriter();
181 writer.put24((byte)0xfe);
182 byte[] exp = new byte[]{0, 0, (byte)0xfe};
183 assertArrayEquals(exp, writer.toByteArray());
184 }
185
186 @Test
187 public void testPut32()
188 {
189 BufferedByteWriter writer = new BufferedByteWriter();
190 writer.put32(0xbeeffeed);
191 byte[] exp = new byte[]{(byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
192 assertArrayEquals(exp, writer.toByteArray());
193 }
194
195 @Test
196 public void testPut32Short()
197 {
198 BufferedByteWriter writer = new BufferedByteWriter();
199 writer.put32((short)0xbeef);
200 byte[] exp = new byte[]{0, 0, (byte)0xbe, (byte)0xef};
201 assertArrayEquals(exp, writer.toByteArray());
202 }
203
204 @Test
205 public void testPut32Byte()
206 {
207 BufferedByteWriter writer = new BufferedByteWriter();
208 writer.put32((byte)0xfe);
209 byte[] exp = new byte[]{0, 0, 0, (byte)0xfe};
210 assertArrayEquals(exp, writer.toByteArray());
211 }
212
213 @Test
214 public void testPut64()
215 {
216 BufferedByteWriter writer = new BufferedByteWriter();
217 writer.put64(0xdeadcafebeeffeedL);
218 byte[] exp = new byte[]{(byte)0xde, (byte)0xad, (byte)0xca, (byte)0xfe, (byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
219 assertArrayEquals(exp, writer.toByteArray());
220 }
221
222 @Test
223 public void testPut64Half()
224 {
225 BufferedByteWriter writer = new BufferedByteWriter();
226 writer.put64(0xbeeffeedL);
227 byte[] exp = new byte[]{0, 0, 0, 0, (byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
228 assertArrayEquals(exp, writer.toByteArray());
229 }
230
231 @Test
232 public void testPut64Int()
233 {
234 BufferedByteWriter writer = new BufferedByteWriter();
235 writer.put64(0xbeeffeed);
236 byte[] exp = new byte[]{0, 0, 0, 0, (byte)0xbe, (byte)0xef, (byte)0xfe, (byte)0xed};
237 assertArrayEquals(exp, writer.toByteArray());
238 }
239
240 @Test
241 public void testPut64Short()
242 {
243 BufferedByteWriter writer = new BufferedByteWriter();
244 writer.put64((short)0xfeed);
245 byte[] exp = new byte[]{0, 0, 0, 0, 0, 0, (byte)0xfe, (byte)0xed};
246 assertArrayEquals(exp, writer.toByteArray());
247 }
248
249 @Test
250 public void testPut64Byte()
251 {
252 BufferedByteWriter writer = new BufferedByteWriter();
253 writer.put64((byte)0xfe);
254 byte[] exp = new byte[]{0, 0, 0, 0, 0, 0, 0, (byte)0xfe};
255 assertArrayEquals(exp, writer.toByteArray());
256 }
257 }