1 /* 2 * Copyright (C) 2020 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 static org.junit.Assert.assertArrayEquals; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertFalse; 22 23 import androidx.test.filters.SmallTest; 24 import androidx.test.runner.AndroidJUnit4; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 29 import java.util.ArrayList; 30 import java.util.Arrays; 31 import java.util.concurrent.TimeUnit; 32 33 /** 34 * A simple class that tests dependencies to java standard tools from the 35 * Network stack. These tests are not meant to be comprehensive tests of 36 * the relevant APIs : such tests belong in the relevant test suite for 37 * these dependencies. Instead, this just makes sure coverage is present 38 * by calling the methods in the exact way (or a representative way of how) 39 * they are called in the network stack. 40 */ 41 @RunWith(AndroidJUnit4.class) 42 @SmallTest 43 public class DependenciesTest { 44 // Used to in ipmemorystore's RegularMaintenanceJobService to convert 45 // 24 hours into seconds 46 @Test testTimeUnit()47 public void testTimeUnit() { 48 final int hours = 24; 49 final long inSeconds = TimeUnit.HOURS.toMillis(hours); 50 assertEquals(inSeconds, hours * 60 * 60 * 1000); 51 } 52 makeTrivialArray(final int size)53 private byte[] makeTrivialArray(final int size) { 54 final byte[] src = new byte[size]; 55 for (int i = 0; i < size; ++i) { 56 src[i] = (byte) i; 57 } 58 return src; 59 } 60 61 // Used in ApfFilter to find an IP address from a byte array 62 @Test testArrays()63 public void testArrays() { 64 final int size = 128; 65 final byte[] src = makeTrivialArray(size); 66 67 // Test copy 68 final int copySize = 16; 69 final int offset = 24; 70 final byte[] expected = new byte[copySize]; 71 for (int i = 0; i < copySize; ++i) { 72 expected[i] = (byte) (offset + i); 73 } 74 75 final byte[] copy = Arrays.copyOfRange(src, offset, offset + copySize); 76 assertArrayEquals(expected, copy); 77 assertArrayEquals(new byte[0], Arrays.copyOfRange(src, size, size)); 78 } 79 80 // Used mainly in the Dhcp code 81 @Test testCopyOf()82 public void testCopyOf() { 83 final byte[] src = makeTrivialArray(128); 84 final byte[] copy = Arrays.copyOf(src, src.length); 85 assertArrayEquals(src, copy); 86 assertFalse(src == copy); 87 88 assertArrayEquals(new byte[0], Arrays.copyOf(src, 0)); 89 90 final int excess = 16; 91 final byte[] biggerCopy = Arrays.copyOf(src, src.length + excess); 92 for (int i = src.length; i < src.length + excess; ++i) { 93 assertEquals(0, biggerCopy[i]); 94 } 95 for (int i = src.length - 1; i >= 0; --i) { 96 assertEquals(src[i], biggerCopy[i]); 97 } 98 } 99 100 // Used mainly in DnsUtils but also various other places 101 @Test testAsList()102 public void testAsList() { 103 final int size = 24; 104 final Object[] src = new Object[size]; 105 final ArrayList<Object> expected = new ArrayList<>(size); 106 for (int i = 0; i < size; ++i) { 107 final Object o = new Object(); 108 src[i] = o; 109 expected.add(o); 110 } 111 assertEquals(expected, Arrays.asList(src)); 112 } 113 } 114