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;
21
22import android.os.Parcel;
23import android.support.test.runner.AndroidJUnit4;
24import android.test.suitebuilder.annotation.SmallTest;
25import org.junit.Test;
26import org.junit.runner.RunWith;
27
28@RunWith(AndroidJUnit4.class)
29@SmallTest
30public class LowpanChannelInfoTest {
31
32    static {
33        System.loadLibrary("frameworkslowpantestsjni");
34    }
35
36    private static native byte[] readAndWriteNative(byte[] inParcel);
37
38    public void testNativeParcelUnparcel(LowpanChannelInfo original) {
39        byte[] inParcel = marshall(original);
40        byte[] outParcel = readAndWriteNative(inParcel);
41        LowpanChannelInfo roundTrip = unmarshall(outParcel);
42
43        assertEquals(original, roundTrip);
44        assertArrayEquals(inParcel, outParcel);
45    }
46
47    @Test
48    public void testNativeParcelUnparcel() {
49        int i;
50        for (i = 1; i < 26; i++) {
51            testNativeParcelUnparcel(LowpanChannelInfo.getChannelInfoForIeee802154Page0(i));
52        }
53    }
54
55    /**
56     * Write a {@link LowpanChannelInfo} into an empty parcel and return the underlying data.
57     *
58     * @see unmarshall(byte[])
59     */
60    private static byte[] marshall(LowpanChannelInfo addr) {
61        Parcel p = Parcel.obtain();
62        addr.writeToParcel(p, /* flags */ 0);
63        p.setDataPosition(0);
64        return p.marshall();
65    }
66
67    /**
68     * Read raw bytes into a parcel, and read a {@link LowpanChannelInfo} back out of them.
69     *
70     * @see marshall(LowpanChannelInfo)
71     */
72    private static LowpanChannelInfo unmarshall(byte[] data) {
73        Parcel p = Parcel.obtain();
74        p.unmarshall(data, 0, data.length);
75        p.setDataPosition(0);
76        return LowpanChannelInfo.CREATOR.createFromParcel(p);
77    }
78}
79