1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net.wifi.aware;
18
19import static org.hamcrest.core.IsEqual.equalTo;
20
21import android.test.suitebuilder.annotation.SmallTest;
22
23import org.junit.Rule;
24import org.junit.Test;
25import org.junit.rules.ErrorCollector;
26
27import java.nio.BufferOverflowException;
28import java.util.ArrayList;
29import java.util.List;
30
31/**
32 * Unit test harness for TlvBufferUtils class.
33 */
34@SmallTest
35public class TlvBufferUtilsTest {
36    @Rule
37    public ErrorCollector collector = new ErrorCollector();
38
39    /*
40     * TlvBufferUtils Tests
41     */
42
43    @Test
44    public void testTlvBuild() {
45        TlvBufferUtils.TlvConstructor tlv11 = new TlvBufferUtils.TlvConstructor(1, 1);
46        tlv11.allocate(15);
47        tlv11.putByte(0, (byte) 2);
48        tlv11.putByteArray(2, new byte[] {
49                0, 1, 2 });
50
51        collector.checkThat("tlv11-correct-construction",
52                tlv11.getArray(), equalTo(new byte[]{0, 1, 2, 2, 3, 0, 1, 2}));
53
54        TlvBufferUtils.TlvConstructor tlv01 = new TlvBufferUtils.TlvConstructor(0, 1);
55        tlv01.allocate(15);
56        tlv01.putByte(0, (byte) 2);
57        tlv01.putByteArray(2, new byte[] {
58                0, 1, 2 });
59
60        collector.checkThat("tlv01-correct-construction",
61                tlv01.getArray(), equalTo(new byte[] {1, 2, 3, 0, 1, 2 }));
62
63        collector.checkThat("tlv11-valid",
64                TlvBufferUtils.isValid(tlv11.getArray(), 1, 1),
65                equalTo(true));
66        collector.checkThat("tlv01-valid",
67                TlvBufferUtils.isValid(tlv01.getArray(), 0, 1),
68                equalTo(true));
69    }
70
71    /**
72     * Verify that can build a valid TLV from a List of byte[].
73     */
74    @Test
75    public void testTlvListOperations() {
76        byte[] entry1 = { 1, 2, 3 };
77        byte[] entry2 = { 4, 5 };
78        byte[] entry3 = new byte[0];
79        List<byte[]> data = new ArrayList<>();
80        data.add(entry1);
81        data.add(entry2);
82        data.add(entry3);
83        data.add(null); // zero-length should work
84
85        TlvBufferUtils.TlvConstructor tlv01 = new TlvBufferUtils.TlvConstructor(0, 1);
86        tlv01.allocateAndPut(data);
87        byte[] tlvData = tlv01.getArray();
88        List<byte[]> parsedList = new TlvBufferUtils.TlvIterable(0, 1, tlvData).toList();
89
90        collector.checkThat("tlvData-correct-length", tlvData.length,
91                equalTo(entry1.length + 1 + entry2.length + 1 + entry3.length + 1 + 1));
92        collector.checkThat("parsedList-correct-length", parsedList.size(), equalTo(4));
93        collector.checkThat("parsedList-entry1", parsedList.get(0), equalTo(entry1));
94        collector.checkThat("parsedList-entry2", parsedList.get(1), equalTo(entry2));
95        collector.checkThat("parsedList-entry3", parsedList.get(2), equalTo(entry3));
96        collector.checkThat("parsedList-entry4", parsedList.get(3), equalTo(new byte[0]));
97    }
98
99    /**
100     * Verify that can parse a (correctly formatted) byte array to a list.
101     */
102    @Test
103    public void testTlvParseToList() {
104        byte[] validTlv01 = { 0, 1, 55, 2, 33, 66, 0 };
105
106        List<byte[]> parsedList = new TlvBufferUtils.TlvIterable(0, 1, validTlv01).toList();
107
108        collector.checkThat("parsedList-entry1", parsedList.get(0), equalTo(new byte[0]));
109        collector.checkThat("parsedList-entry2", parsedList.get(1), equalTo(new byte[] { 55 }));
110        collector.checkThat("parsedList-entry3", parsedList.get(2), equalTo(new byte[] { 33, 66 }));
111        collector.checkThat("parsedList-entry4", parsedList.get(3), equalTo(new byte[0]));
112    }
113
114    /**
115     * Verify that an exception is thrown when trying to parse an invalid array.
116     */
117    @Test(expected = BufferOverflowException.class)
118    public void testTlvParseToListError() {
119        byte[] invalidTlv01 = { 0, 1, 55, 2, 55, 66, 3 }; // bad data
120
121        List<byte[]> data = new TlvBufferUtils.TlvIterable(0, 1, invalidTlv01).toList();
122    }
123
124    @Test
125    public void testTlvIterate() {
126        final String ascii = "ABC";
127        final String nonAscii = "何かもっと複雑な";
128
129        TlvBufferUtils.TlvConstructor tlv22 = new TlvBufferUtils.TlvConstructor(2, 2);
130        tlv22.allocate(18);
131        tlv22.putInt(0, 2);
132        tlv22.putShort(2, (short) 3);
133        tlv22.putZeroLengthElement(55);
134
135        TlvBufferUtils.TlvIterable tlv22It = new TlvBufferUtils.TlvIterable(2, 2, tlv22.getArray());
136        int count = 0;
137        for (TlvBufferUtils.TlvElement tlv : tlv22It) {
138            if (count == 0) {
139                collector.checkThat("tlv22-correct-iteration-mType", tlv.type, equalTo(0));
140                collector.checkThat("tlv22-correct-iteration-mLength", tlv.length, equalTo(4));
141                collector.checkThat("tlv22-correct-iteration-DATA", tlv.getInt(), equalTo(2));
142            } else if (count == 1) {
143                collector.checkThat("tlv22-correct-iteration-mType", tlv.type, equalTo(2));
144                collector.checkThat("tlv22-correct-iteration-mLength", tlv.length, equalTo(2));
145                collector.checkThat("tlv22-correct-iteration-DATA", (int) tlv.getShort(),
146                        equalTo(3));
147            } else if (count == 2) {
148                collector.checkThat("tlv22-correct-iteration-mType", tlv.type, equalTo(55));
149                collector.checkThat("tlv22-correct-iteration-mLength", tlv.length, equalTo(0));
150            } else {
151                collector.checkThat("Invalid number of iterations in loop - tlv22", true,
152                        equalTo(false));
153            }
154            ++count;
155        }
156        if (count != 3) {
157            collector.checkThat("Invalid number of iterations outside loop - tlv22", true,
158                    equalTo(false));
159        }
160
161        TlvBufferUtils.TlvConstructor tlv02 = new TlvBufferUtils.TlvConstructor(0, 2);
162        tlv02.allocate(100);
163        tlv02.putByte(0, (byte) 2);
164        tlv02.putString(0, ascii);
165        tlv02.putString(0, nonAscii);
166
167        TlvBufferUtils.TlvIterable tlv02It = new TlvBufferUtils.TlvIterable(0, 2, tlv02.getArray());
168        count = 0;
169        for (TlvBufferUtils.TlvElement tlv : tlv02It) {
170            if (count == 0) {
171                collector.checkThat("tlv02-correct-iteration-mLength", tlv.length, equalTo(1));
172                collector.checkThat("tlv02-correct-iteration-DATA", (int) tlv.getByte(),
173                        equalTo(2));
174            } else if (count == 1) {
175                collector.checkThat("tlv02-correct-iteration-mLength", tlv.length,
176                        equalTo(ascii.length()));
177                collector.checkThat("tlv02-correct-iteration-DATA", tlv.getString().equals(ascii),
178                        equalTo(true));
179            } else if (count == 2) {
180                collector.checkThat("tlv02-correct-iteration-mLength", tlv.length,
181                        equalTo(nonAscii.getBytes().length));
182                collector.checkThat("tlv02-correct-iteration-DATA",
183                        tlv.getString().equals(nonAscii), equalTo(true));
184            } else {
185                collector.checkThat("Invalid number of iterations in loop - tlv02", true,
186                        equalTo(false));
187            }
188            ++count;
189        }
190        collector.checkThat("Invalid number of iterations outside loop - tlv02", count,
191                equalTo(3));
192
193        collector.checkThat("tlv22-valid",
194                TlvBufferUtils.isValid(tlv22.getArray(), 2, 2),
195                equalTo(true));
196        collector.checkThat("tlv02-valid",
197                TlvBufferUtils.isValid(tlv02.getArray(), 0, 2),
198                equalTo(true));
199    }
200
201    @Test(expected = IllegalArgumentException.class)
202    public void testTlvInvalidSizeT1L0() {
203        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(1, 0);
204    }
205
206    @Test(expected = IllegalArgumentException.class)
207    public void testTlvInvalidSizeTm3L2() {
208        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(-3, 2);
209    }
210
211    @Test(expected = IllegalArgumentException.class)
212    public void testTlvInvalidSizeT1Lm2() {
213        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(1, -2);
214    }
215
216    @Test(expected = IllegalArgumentException.class)
217    public void testTlvInvalidSizeT1L3() {
218        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(1, 3);
219    }
220
221    @Test(expected = IllegalArgumentException.class)
222    public void testTlvInvalidSizeT3L1() {
223        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(3, 1);
224    }
225
226    @Test(expected = IllegalArgumentException.class)
227    public void testTlvItInvalidSizeT1L0() {
228        final byte[] dummy = {
229                0, 1, 2 };
230        final int dummyLength = 3;
231        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(1, 0, dummy);
232    }
233
234    @Test(expected = IllegalArgumentException.class)
235    public void testTlvItInvalidSizeTm3L2() {
236        final byte[] dummy = {
237                0, 1, 2 };
238        final int dummyLength = 3;
239        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(-3, 2, dummy);
240    }
241
242    @Test(expected = IllegalArgumentException.class)
243    public void testTlvItInvalidSizeT1Lm2() {
244        final byte[] dummy = {
245                0, 1, 2 };
246        final int dummyLength = 3;
247        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(1, -2, dummy);
248    }
249
250    @Test(expected = IllegalArgumentException.class)
251    public void testTlvItInvalidSizeT1L3() {
252        final byte[] dummy = {
253                0, 1, 2 };
254        final int dummyLength = 3;
255        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(1, 3, dummy);
256    }
257
258    @Test(expected = IllegalArgumentException.class)
259    public void testTlvItInvalidSizeT3L1() {
260        final byte[] dummy = {
261                0, 1, 2 };
262        final int dummyLength = 3;
263        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(3, 1, dummy);
264    }
265
266    /**
267     * Validate that a malformed byte array fails the TLV validity test.
268     */
269    @Test
270    public void testTlvInvalidByteArray() {
271        TlvBufferUtils.TlvConstructor tlv01 = new TlvBufferUtils.TlvConstructor(0, 1);
272        tlv01.allocate(15);
273        tlv01.putByte(0, (byte) 2);
274        tlv01.putByteArray(2, new byte[]{0, 1, 2});
275
276        byte[] array = tlv01.getArray();
277        array[0] = 10;
278
279        collector.checkThat("tlv01-invalid",
280                TlvBufferUtils.isValid(array, 0, 1), equalTo(false));
281    }
282}
283