1 /*
2  * Copyright (C) 2022 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.nearby.cts;
18 
19 import static android.nearby.DataElement.DataType.PRIVATE_IDENTITY;
20 import static android.nearby.DataElement.DataType.PROVISIONED_IDENTITY;
21 import static android.nearby.DataElement.DataType.PUBLIC_IDENTITY;
22 import static android.nearby.DataElement.DataType.SALT;
23 import static android.nearby.DataElement.DataType.TRUSTED_IDENTITY;
24 import static android.nearby.DataElement.DataType.TX_POWER;
25 
26 import static com.google.common.truth.Truth.assertThat;
27 
28 import android.nearby.DataElement;
29 import android.os.Build;
30 import android.os.Parcel;
31 
32 import androidx.annotation.RequiresApi;
33 import androidx.test.ext.junit.runners.AndroidJUnit4;
34 import androidx.test.filters.SdkSuppress;
35 
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 
39 import java.util.Arrays;
40 
41 @RunWith(AndroidJUnit4.class)
42 @RequiresApi(Build.VERSION_CODES.TIRAMISU)
43 public class DataElementTest {
44 
45     private static final int KEY = 1;
46     private static final byte[] VALUE = new byte[]{1, 1, 1, 1};
47 
48     @Test
49     @SdkSuppress(minSdkVersion = 32, codeName = "T")
testBuilder()50     public void testBuilder() {
51         DataElement dataElement = new DataElement(KEY, VALUE);
52 
53         assertThat(dataElement.getKey()).isEqualTo(KEY);
54         assertThat(Arrays.equals(dataElement.getValue(), VALUE)).isTrue();
55     }
56 
57     @Test
58     @SdkSuppress(minSdkVersion = 32, codeName = "T")
testWriteParcel()59     public void testWriteParcel() {
60         DataElement dataElement = new DataElement(KEY, VALUE);
61 
62         Parcel parcel = Parcel.obtain();
63         dataElement.writeToParcel(parcel, 0);
64         parcel.setDataPosition(0);
65         DataElement elementFromParcel = DataElement.CREATOR.createFromParcel(
66                 parcel);
67         parcel.recycle();
68 
69         assertThat(elementFromParcel.getKey()).isEqualTo(KEY);
70         assertThat(Arrays.equals(elementFromParcel.getValue(), VALUE)).isTrue();
71     }
72 
73     @Test
74     @SdkSuppress(minSdkVersion = 32, codeName = "T")
describeContents()75     public void describeContents() {
76         DataElement dataElement = new DataElement(KEY, VALUE);
77         assertThat(dataElement.describeContents()).isEqualTo(0);
78     }
79 
80     @Test
81     @SdkSuppress(minSdkVersion = 32, codeName = "T")
testCreatorNewArray()82     public void testCreatorNewArray() {
83         DataElement[] elements =
84                 DataElement.CREATOR.newArray(2);
85         assertThat(elements.length).isEqualTo(2);
86     }
87 
88     @Test
89     @SdkSuppress(minSdkVersion = 32, codeName = "T")
testEquals()90     public void testEquals() {
91         DataElement dataElement = new DataElement(KEY, VALUE);
92         DataElement dataElement2 = new DataElement(KEY, VALUE);
93 
94         assertThat(dataElement.equals(dataElement2)).isTrue();
95     }
96 
97     @Test
98     @SdkSuppress(minSdkVersion = 32, codeName = "T")
testIsIdentity()99     public void testIsIdentity() {
100         DataElement privateIdentity = new DataElement(PRIVATE_IDENTITY, new byte[]{1, 2, 3});
101         DataElement trustedIdentity = new DataElement(TRUSTED_IDENTITY, new byte[]{1, 2, 3});
102         DataElement publicIdentity = new DataElement(PUBLIC_IDENTITY, new byte[]{1, 2, 3});
103         DataElement provisionedIdentity =
104                 new DataElement(PROVISIONED_IDENTITY, new byte[]{1, 2, 3});
105 
106         DataElement salt = new DataElement(SALT, new byte[]{1, 2, 3});
107         DataElement txPower = new DataElement(TX_POWER, new byte[]{1, 2, 3});
108 
109         assertThat(privateIdentity.isIdentityDataType()).isTrue();
110         assertThat(trustedIdentity.isIdentityDataType()).isTrue();
111         assertThat(publicIdentity.isIdentityDataType()).isTrue();
112         assertThat(provisionedIdentity.isIdentityDataType()).isTrue();
113         assertThat(salt.isIdentityDataType()).isFalse();
114         assertThat(txPower.isIdentityDataType()).isFalse();
115     }
116 
117     @Test
118     @SdkSuppress(minSdkVersion = 32, codeName = "T")
test_notEquals()119     public void test_notEquals() {
120         DataElement dataElement = new DataElement(KEY, VALUE);
121         DataElement dataElement2 = new DataElement(KEY, new byte[]{1, 2, 1, 1});
122         DataElement dataElement3 = new DataElement(6, VALUE);
123 
124         assertThat(dataElement.equals(dataElement2)).isFalse();
125         assertThat(dataElement.equals(dataElement3)).isFalse();
126     }
127 }
128