1/*
2 * Copyright (C) 2017 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.lowpan;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertArrayEquals;
21import static org.junit.Assert.assertFalse;
22import static org.junit.Assert.assertTrue;
23
24import android.os.Parcel;
25import android.support.test.runner.AndroidJUnit4;
26import android.test.suitebuilder.annotation.SmallTest;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30@RunWith(AndroidJUnit4.class)
31@SmallTest
32public class LowpanIdentityTest {
33
34    static {
35        System.loadLibrary("frameworkslowpantestsjni");
36    }
37
38    private static native byte[] readAndWriteNative(byte[] inParcel);
39
40    public void testNativeParcelUnparcel(LowpanIdentity original) {
41        byte[] inParcel = marshall(original);
42        byte[] outParcel = readAndWriteNative(inParcel);
43        LowpanIdentity roundTrip = unmarshall(outParcel);
44
45        assertEquals(original, roundTrip);
46        assertEquals(original.hashCode(), roundTrip.hashCode());
47        assertEquals(original.getName(), roundTrip.getName());
48        assertArrayEquals(inParcel, outParcel);
49    }
50
51    @Test
52    public void testNativeParcelUnparcel1() {
53        testNativeParcelUnparcel(
54                new LowpanIdentity.Builder()
55                        .setName("TestNet1")
56                        .setPanid(0x1234)
57                        .setXpanid(
58                                new byte[] {
59                                    (byte) 0x00,
60                                    (byte) 0x11,
61                                    (byte) 0x22,
62                                    (byte) 0x33,
63                                    (byte) 0x44,
64                                    (byte) 0x55,
65                                    (byte) 0x66,
66                                    (byte) 0x77
67                                })
68                        .setType(LowpanInterface.NETWORK_TYPE_THREAD_V1)
69                        .setChannel(15)
70                        .build());
71    }
72
73    @Test
74    public void testNativeParcelUnparcel2() {
75        testNativeParcelUnparcel(
76                new LowpanIdentity.Builder()
77                        .setName("TestNet2")
78                        .setPanid(0x5678)
79                        .setXpanid(
80                                new byte[] {
81                                    (byte) 0x88,
82                                    (byte) 0x99,
83                                    (byte) 0xaa,
84                                    (byte) 0xbb,
85                                    (byte) 0xcc,
86                                    (byte) 0xdd,
87                                    (byte) 0xee,
88                                    (byte) 0xff
89                                })
90                        .setType("bork-bork-bork")
91                        .setChannel(16)
92                        .build());
93    }
94
95    @Test
96    public void testNativeParcelUnparcel3() {
97        testNativeParcelUnparcel(new LowpanIdentity.Builder().setName("TestNet3").build());
98    }
99
100    @Test
101    public void testNativeParcelUnparcel4() {
102        testNativeParcelUnparcel(new LowpanIdentity.Builder().build());
103    }
104
105    @Test
106    public void testNativeParcelUnparcel5() {
107        testNativeParcelUnparcel(
108                new LowpanIdentity.Builder()
109                        .setRawName(
110                                new byte[] {
111                                    (byte) 0x66,
112                                    (byte) 0x6F,
113                                    (byte) 0x6F,
114                                    (byte) 0xC2,
115                                    (byte) 0xAD,
116                                    (byte) 0xCD,
117                                    (byte) 0x8F,
118                                    (byte) 0xE1,
119                                    (byte) 0xA0,
120                                    (byte) 0x86,
121                                    (byte) 0xE1,
122                                    (byte) 0xA0,
123                                    (byte) 0x8B
124                                })
125                        .build());
126    }
127
128    @Test
129    public void testStringPrep1() {
130        LowpanIdentity identity =
131                new LowpanIdentity.Builder()
132                        .setRawName(
133                                new byte[] {
134                                    (byte) 0x66,
135                                    (byte) 0x6F,
136                                    (byte) 0x6F,
137                                    (byte) 0x20,
138                                    (byte) 0xC2,
139                                    (byte) 0xAD,
140                                    (byte) 0xCD,
141                                    (byte) 0x8F,
142                                    (byte) 0xE1,
143                                    (byte) 0xA0,
144                                    (byte) 0x86,
145                                    (byte) 0xE1,
146                                    (byte) 0xA0,
147                                    (byte) 0x8B
148                                })
149                        .build();
150
151        assertFalse(identity.isNameValid());
152    }
153
154    @Test
155    public void testStringPrep2() {
156        LowpanIdentity identity =
157                new LowpanIdentity.Builder()
158                        .setRawName(
159                                new byte[] {
160                                    (byte) 0x66, (byte) 0x6F, (byte) 0x6F, (byte) 0x20, (byte) 0x6F
161                                })
162                        .build();
163
164        assertEquals("foo o", identity.getName());
165        assertTrue(identity.isNameValid());
166    }
167
168    @Test
169    public void testStringPrep3() {
170        LowpanIdentity identity = new LowpanIdentity.Builder().setName("foo o").build();
171
172        assertTrue(identity.isNameValid());
173        assertEquals("foo o", identity.getName());
174    }
175
176    /**
177     * Write a {@link LowpanIdentity} into an empty parcel and return the underlying data.
178     *
179     * @see unmarshall(byte[])
180     */
181    private static byte[] marshall(LowpanIdentity addr) {
182        Parcel p = Parcel.obtain();
183        addr.writeToParcel(p, /* flags */ 0);
184        p.setDataPosition(0);
185        return p.marshall();
186    }
187
188    /**
189     * Read raw bytes into a parcel, and read a {@link LowpanIdentity} back out of them.
190     *
191     * @see marshall(LowpanIdentity)
192     */
193    private static LowpanIdentity unmarshall(byte[] data) {
194        Parcel p = Parcel.obtain();
195        p.unmarshall(data, 0, data.length);
196        p.setDataPosition(0);
197        return LowpanIdentity.CREATOR.createFromParcel(p);
198    }
199}
200