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 LowpanBeaconInfoTest { 31 32 static { 33 System.loadLibrary("frameworkslowpantestsjni"); 34 } 35 36 private static native byte[] readAndWriteNative(byte[] inParcel); 37 38 public void testNativeParcelUnparcel(LowpanBeaconInfo original) { 39 byte[] inParcel = marshall(original); 40 byte[] outParcel = readAndWriteNative(inParcel); 41 LowpanBeaconInfo roundTrip = unmarshall(outParcel); 42 43 assertEquals(original, roundTrip); 44 assertArrayEquals(inParcel, outParcel); 45 } 46 47 @Test 48 public void testNativeParcelUnparcel() { 49 testNativeParcelUnparcel( 50 new LowpanBeaconInfo.Builder() 51 .setName("TestNet1") 52 .setPanid(0x1234) 53 .setXpanid( 54 new byte[] { 55 (byte) 0x00, 56 (byte) 0x11, 57 (byte) 0x22, 58 (byte) 0x33, 59 (byte) 0x44, 60 (byte) 0x55, 61 (byte) 0x66, 62 (byte) 0x77 63 }) 64 .setType(LowpanInterface.NETWORK_TYPE_THREAD_V1) 65 .setChannel(15) 66 .setBeaconAddress( 67 new byte[] { 68 (byte) 0x88, 69 (byte) 0x99, 70 (byte) 0xaa, 71 (byte) 0xbb, 72 (byte) 0xcc, 73 (byte) 0xdd, 74 (byte) 0xee, 75 (byte) 0xff 76 }) 77 .build()); 78 79 testNativeParcelUnparcel( 80 new LowpanBeaconInfo.Builder() 81 .setName("TestNet2") 82 .setPanid(0x5678) 83 .setXpanid( 84 new byte[] { 85 (byte) 0x88, 86 (byte) 0x99, 87 (byte) 0xaa, 88 (byte) 0xbb, 89 (byte) 0xcc, 90 (byte) 0xdd, 91 (byte) 0xee, 92 (byte) 0xff 93 }) 94 .setType("bork-bork-bork") 95 .setChannel(16) 96 .setBeaconAddress( 97 new byte[] { 98 (byte) 0x00, 99 (byte) 0x11, 100 (byte) 0x22, 101 (byte) 0x33, 102 (byte) 0x44, 103 (byte) 0x55, 104 (byte) 0x66, 105 (byte) 0x77 106 }) 107 .setFlag(LowpanBeaconInfo.FLAG_CAN_ASSIST) 108 .build()); 109 } 110 111 /** 112 * Write a {@link LowpanBeaconInfo} into an empty parcel and return the underlying data. 113 * 114 * @see unmarshall(byte[]) 115 */ 116 private static byte[] marshall(LowpanBeaconInfo addr) { 117 Parcel p = Parcel.obtain(); 118 addr.writeToParcel(p, /* flags */ 0); 119 p.setDataPosition(0); 120 return p.marshall(); 121 } 122 123 /** 124 * Read raw bytes into a parcel, and read a {@link LowpanBeaconInfo} back out of them. 125 * 126 * @see marshall(LowpanBeaconInfo) 127 */ 128 private static LowpanBeaconInfo unmarshall(byte[] data) { 129 Parcel p = Parcel.obtain(); 130 p.unmarshall(data, 0, data.length); 131 p.setDataPosition(0); 132 return LowpanBeaconInfo.CREATOR.createFromParcel(p); 133 } 134} 135