Robin Lee | cfe2669 | 2016-04-05 12:19:06 +0100 | [diff] [blame] | 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 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.os.Parcel; |
| 20 | import android.test.suitebuilder.annotation.SmallTest; |
| 21 | |
| 22 | import junit.framework.TestCase; |
| 23 | |
| 24 | import static org.junit.Assert.assertArrayEquals; |
| 25 | |
| 26 | public class UidRangeTest extends TestCase { |
| 27 | |
| 28 | static { |
| 29 | System.loadLibrary("servicestestsjni"); |
| 30 | } |
| 31 | |
| 32 | private static native byte[] readAndWriteNative(byte[] inParcel); |
| 33 | private static native int getStart(byte[] inParcel); |
| 34 | private static native int getStop(byte[] inParcel); |
| 35 | |
| 36 | @SmallTest |
| 37 | public void testNativeParcelUnparcel() { |
| 38 | UidRange original = new UidRange(1234, Integer.MAX_VALUE); |
| 39 | |
| 40 | byte[] inParcel = marshall(original); |
| 41 | byte[] outParcel = readAndWriteNative(inParcel); |
| 42 | UidRange roundTrip = unmarshall(outParcel); |
| 43 | |
| 44 | assertEquals(original, roundTrip); |
| 45 | assertArrayEquals(inParcel, outParcel); |
| 46 | } |
| 47 | |
| 48 | @SmallTest |
| 49 | public void testIndividualNativeFields() { |
| 50 | UidRange original = new UidRange(0x11115678, 0x22224321); |
| 51 | byte[] originalBytes = marshall(original); |
| 52 | |
| 53 | assertEquals(original.start, getStart(originalBytes)); |
| 54 | assertEquals(original.stop, getStop(originalBytes)); |
| 55 | } |
| 56 | |
| 57 | @SmallTest |
| 58 | public void testSingleItemUidRangeAllowed() { |
| 59 | new UidRange(123, 123); |
| 60 | new UidRange(0, 0); |
| 61 | new UidRange(Integer.MAX_VALUE, Integer.MAX_VALUE); |
| 62 | } |
| 63 | |
| 64 | @SmallTest |
| 65 | public void testNegativeUidsDisallowed() { |
| 66 | try { |
| 67 | new UidRange(-2, 100); |
| 68 | fail("Exception not thrown for negative start UID"); |
| 69 | } catch (IllegalArgumentException expected) { |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | new UidRange(-200, -100); |
| 74 | fail("Exception not thrown for negative stop UID"); |
| 75 | } catch (IllegalArgumentException expected) { |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @SmallTest |
| 80 | public void testStopLessThanStartDisallowed() { |
| 81 | final int x = 4195000; |
| 82 | try { |
| 83 | new UidRange(x, x - 1); |
| 84 | fail("Exception not thrown for negative-length UID range"); |
| 85 | } catch (IllegalArgumentException expected) { |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Write a {@link UidRange} into an empty parcel and return the underlying data. |
| 91 | * |
| 92 | * @see unmarshall(byte[]) |
| 93 | */ |
| 94 | private static byte[] marshall(UidRange range) { |
| 95 | Parcel p = Parcel.obtain(); |
| 96 | range.writeToParcel(p, /* flags */ 0); |
| 97 | p.setDataPosition(0); |
| 98 | return p.marshall(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Read raw bytes into a parcel, and read a {@link UidRange} back out of them. |
| 103 | * |
| 104 | * @see marshall(UidRange) |
| 105 | */ |
| 106 | private static UidRange unmarshall(byte[] data) { |
| 107 | Parcel p = Parcel.obtain(); |
| 108 | p.unmarshall(data, 0, data.length); |
| 109 | p.setDataPosition(0); |
| 110 | return UidRange.CREATOR.createFromParcel(p); |
| 111 | } |
| 112 | } |