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 com.android.server.wifi.nan;
18
19import static org.hamcrest.core.IsEqual.equalTo;
20
21import android.net.wifi.nan.TlvBufferUtils;
22import android.test.suitebuilder.annotation.SmallTest;
23
24import org.junit.Rule;
25import org.junit.Test;
26import org.junit.rules.ErrorCollector;
27import org.junit.rules.ExpectedException;
28
29/**
30 * Unit test harness for WifiNanManager class.
31 */
32@SmallTest
33public class TlvBufferUtilsTest {
34    @Rule
35    public ErrorCollector collector = new ErrorCollector();
36
37    @Rule
38    public ExpectedException thrown = ExpectedException.none();
39
40    /*
41     * TlvBufferUtils Tests
42     */
43
44    @Test
45    public void testTlvBuild() {
46        TlvBufferUtils.TlvConstructor tlv11 = new TlvBufferUtils.TlvConstructor(1, 1);
47        tlv11.allocate(15);
48        tlv11.putByte(0, (byte) 2);
49        tlv11.putByteArray(2, new byte[] {
50                0, 1, 2 });
51
52        collector.checkThat("tlv11-correct-construction",
53                utilAreArraysEqual(tlv11.getArray(), tlv11.getActualLength(), new byte[] {
54                        0, 1, 2, 2, 3, 0, 1, 2 }, 8),
55                equalTo(true));
56
57        TlvBufferUtils.TlvConstructor tlv01 = new TlvBufferUtils.TlvConstructor(0, 1);
58        tlv01.allocate(15);
59        tlv01.putByte(0, (byte) 2);
60        tlv01.putByteArray(2, new byte[] {
61                0, 1, 2 });
62
63        collector.checkThat("tlv01-correct-construction",
64                utilAreArraysEqual(tlv01.getArray(), tlv01.getActualLength(), new byte[] {
65                        1, 2, 3, 0, 1, 2 }, 6),
66                equalTo(true));
67    }
68
69    @Test
70    public void testTlvIterate() {
71        TlvBufferUtils.TlvConstructor tlv22 = new TlvBufferUtils.TlvConstructor(2, 2);
72        tlv22.allocate(18);
73        tlv22.putInt(0, 2);
74        tlv22.putShort(2, (short) 3);
75        tlv22.putZeroLengthElement(55);
76
77        TlvBufferUtils.TlvIterable tlv22It = new TlvBufferUtils.TlvIterable(2, 2, tlv22.getArray(),
78                tlv22.getActualLength());
79        int count = 0;
80        for (TlvBufferUtils.TlvElement tlv : tlv22It) {
81            if (count == 0) {
82                collector.checkThat("tlv22-correct-iteration-mType", tlv.mType, equalTo(0));
83                collector.checkThat("tlv22-correct-iteration-mLength", tlv.mLength, equalTo(4));
84                collector.checkThat("tlv22-correct-iteration-DATA", tlv.getInt(), equalTo(2));
85            } else if (count == 1) {
86                collector.checkThat("tlv22-correct-iteration-mType", tlv.mType, equalTo(2));
87                collector.checkThat("tlv22-correct-iteration-mLength", tlv.mLength, equalTo(2));
88                collector.checkThat("tlv22-correct-iteration-DATA", (int) tlv.getShort(),
89                        equalTo(3));
90            } else if (count == 2) {
91                collector.checkThat("tlv22-correct-iteration-mType", tlv.mType, equalTo(55));
92                collector.checkThat("tlv22-correct-iteration-mLength", tlv.mLength, equalTo(0));
93            } else {
94                collector.checkThat("Invalid number of iterations in loop - tlv22", true,
95                        equalTo(false));
96            }
97            ++count;
98        }
99        if (count != 3) {
100            collector.checkThat("Invalid number of iterations outside loop - tlv22", true,
101                    equalTo(false));
102        }
103
104        TlvBufferUtils.TlvConstructor tlv02 = new TlvBufferUtils.TlvConstructor(0, 2);
105        tlv02.allocate(15);
106        tlv02.putByte(0, (byte) 2);
107        tlv02.putString(0, "ABC");
108
109        TlvBufferUtils.TlvIterable tlv02It = new TlvBufferUtils.TlvIterable(0, 2, tlv02.getArray(),
110                tlv02.getActualLength());
111        count = 0;
112        for (TlvBufferUtils.TlvElement tlv : tlv02It) {
113            if (count == 0) {
114                collector.checkThat("tlv02-correct-iteration-mLength", tlv.mLength, equalTo(1));
115                collector.checkThat("tlv02-correct-iteration-DATA", (int) tlv.getByte(),
116                        equalTo(2));
117            } else if (count == 1) {
118                collector.checkThat("tlv02-correct-iteration-mLength", tlv.mLength, equalTo(3));
119                collector.checkThat("tlv02-correct-iteration-DATA", tlv.getString().equals("ABC"),
120                        equalTo(true));
121            } else {
122                collector.checkThat("Invalid number of iterations in loop - tlv02", true,
123                        equalTo(false));
124            }
125            ++count;
126        }
127        if (count != 2) {
128            collector.checkThat("Invalid number of iterations outside loop - tlv02", true,
129                    equalTo(false));
130        }
131    }
132
133    @Test
134    public void testTlvInvalidSizeT1L0() {
135        thrown.expect(IllegalArgumentException.class);
136        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(1, 0);
137    }
138
139    @Test
140    public void testTlvInvalidSizeTm3L2() {
141        thrown.expect(IllegalArgumentException.class);
142        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(-3, 2);
143    }
144
145    @Test
146    public void testTlvInvalidSizeT1Lm2() {
147        thrown.expect(IllegalArgumentException.class);
148        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(1, -2);
149    }
150
151    @Test
152    public void testTlvInvalidSizeT1L3() {
153        thrown.expect(IllegalArgumentException.class);
154        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(1, 3);
155    }
156
157    @Test
158    public void testTlvInvalidSizeT3L1() {
159        thrown.expect(IllegalArgumentException.class);
160        TlvBufferUtils.TlvConstructor tlv10 = new TlvBufferUtils.TlvConstructor(3, 1);
161    }
162
163    @Test
164    public void testTlvItInvalidSizeT1L0() {
165        final byte[] dummy = {
166                0, 1, 2 };
167        final int dummyLength = 3;
168        thrown.expect(IllegalArgumentException.class);
169        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(1, 0, dummy,
170                dummyLength);
171    }
172
173    @Test
174    public void testTlvItInvalidSizeTm3L2() {
175        final byte[] dummy = {
176                0, 1, 2 };
177        final int dummyLength = 3;
178        thrown.expect(IllegalArgumentException.class);
179        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(-3, 2, dummy,
180                dummyLength);
181    }
182
183    @Test
184    public void testTlvItInvalidSizeT1Lm2() {
185        final byte[] dummy = {
186                0, 1, 2 };
187        final int dummyLength = 3;
188        thrown.expect(IllegalArgumentException.class);
189        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(1, -2, dummy,
190                dummyLength);
191    }
192
193    @Test
194    public void testTlvItInvalidSizeT1L3() {
195        final byte[] dummy = {
196                0, 1, 2 };
197        final int dummyLength = 3;
198        thrown.expect(IllegalArgumentException.class);
199        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(1, 3, dummy,
200                dummyLength);
201    }
202
203    @Test
204    public void testTlvItInvalidSizeT3L1() {
205        final byte[] dummy = {
206                0, 1, 2 };
207        final int dummyLength = 3;
208        thrown.expect(IllegalArgumentException.class);
209        TlvBufferUtils.TlvIterable tlvIt10 = new TlvBufferUtils.TlvIterable(3, 1, dummy,
210                dummyLength);
211    }
212
213    /*
214     * Utilities
215     */
216
217    private static boolean utilAreArraysEqual(byte[] x, int xLength, byte[] y, int yLength) {
218        if (xLength != yLength) {
219            return false;
220        }
221
222        if (x != null && y != null) {
223            for (int i = 0; i < xLength; ++i) {
224                if (x[i] != y[i]) {
225                    return false;
226                }
227            }
228        } else if (xLength != 0) {
229            return false; // invalid != invalid
230        }
231
232        return true;
233    }
234}
235