1 /*
2  * Copyright (C) 2019 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 com.android.server.net.ipmemorystore;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.net.ipmemorystore.NetworkAttributes;
22 import android.net.networkstack.aidl.quirks.IPv6ProvisioningLossQuirk;
23 import android.os.Build;
24 
25 import androidx.test.filters.SmallTest;
26 
27 import com.android.testutils.DevSdkIgnoreRule;
28 import com.android.testutils.DevSdkIgnoreRunner;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.lang.reflect.Field;
34 import java.net.Inet4Address;
35 import java.net.UnknownHostException;
36 import java.util.Arrays;
37 
38 /** Unit tests for {@link NetworkAttributes}. */
39 @SmallTest
40 @RunWith(DevSdkIgnoreRunner.class)
41 @DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.R)
42 public class NetworkAttributesTest {
43     private static final String WEIGHT_FIELD_NAME_PREFIX = "WEIGHT_";
44     private static final float EPSILON = 0.0001f;
45 
46     // This is running two tests to make sure the total weight is the sum of all weights. To be
47     // sure this is not fireproof, but you'd kind of need to do it on purpose to pass.
48     @Test
testTotalWeight()49     public void testTotalWeight() throws IllegalAccessException, UnknownHostException {
50         // Make sure that TOTAL_WEIGHT is equal to the sum of the fields starting with WEIGHT_
51         float sum = 0f;
52         final Field[] fieldList = NetworkAttributes.class.getDeclaredFields();
53         for (final Field field : fieldList) {
54             if (!field.getName().startsWith(WEIGHT_FIELD_NAME_PREFIX)) continue;
55             field.setAccessible(true);
56             sum += (float) field.get(null);
57         }
58         assertEquals(sum, NetworkAttributes.TOTAL_WEIGHT, EPSILON);
59 
60         final IPv6ProvisioningLossQuirk ipv6ProvisioningLossQuirk =
61                 new IPv6ProvisioningLossQuirk(3, System.currentTimeMillis() + 7_200_000);
62         // Use directly the constructor with all attributes, and make sure that when compared
63         // to itself the score is a clean 1.0f.
64         final NetworkAttributes na =
65                 new NetworkAttributes(
66                         (Inet4Address) Inet4Address.getByAddress(new byte[] {1, 2, 3, 4}),
67                         System.currentTimeMillis() + 7_200_000,
68                         "some hint",
69                         Arrays.asList(Inet4Address.getByAddress(new byte[] {5, 6, 7, 8}),
70                                 Inet4Address.getByAddress(new byte[] {9, 0, 1, 2})),
71                         98, ipv6ProvisioningLossQuirk);
72         assertEquals(1.0f, na.getNetworkGroupSamenessConfidence(na), EPSILON);
73     }
74 }
75