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 com.android.server.uwb.data; 18 19 import static com.android.server.uwb.data.ServiceProfileData.ServiceProfileInfo.ADF_STATUS_PROVISIONED; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertTrue; 24 25 import android.platform.test.annotations.Presubmit; 26 27 import androidx.test.filters.SmallTest; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import com.android.proto.uwb.UwbConfigProto; 31 import com.android.server.uwb.data.ServiceProfileData.ServiceProfileInfo; 32 import com.android.server.uwb.util.ObjectIdentifier; 33 34 import com.google.protobuf.ByteString; 35 import com.google.protobuf.InvalidProtocolBufferException; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 import java.util.HashMap; 42 import java.util.Map; 43 import java.util.UUID; 44 45 @SmallTest 46 @RunWith(AndroidJUnit4.class) 47 @Presubmit 48 public class ServiceProfileDataTest { 49 private MockDataSource mDataSource; 50 private ServiceProfileData mServiceProfileData; 51 52 @Before setUp()53 public void setUp() { 54 mDataSource = new MockDataSource(); 55 mServiceProfileData = new ServiceProfileData(mDataSource); 56 } 57 58 @Test testSerializeData()59 public void testSerializeData() { 60 UwbConfigProto.UwbConfig.Builder builder = UwbConfigProto.UwbConfig.newBuilder(); 61 builder.setVersion(1); 62 mServiceProfileData.serializeData(builder); 63 64 UwbConfigProto.ServiceConfig serviceConfig = builder.getServiceConfig(0); 65 UUID serviceInstanceID = new UUID(100, 500); 66 67 assertEquals(serviceConfig.getServiceId(), 1); 68 assertEquals(serviceConfig.getServiceInstanceId(), serviceInstanceID.toString()); 69 assertEquals(serviceConfig.getUid(), 1); 70 assertEquals(serviceConfig.getPackageName(), "test"); 71 assertEquals(serviceConfig.getAdfStatus(), ADF_STATUS_PROVISIONED); 72 assertEquals(serviceConfig.getServiceAdfOid(), ByteString.copyFrom(new byte[] {(byte) 1})); 73 assertEquals(serviceConfig.getSecureBlob(), ByteString.EMPTY); 74 75 } 76 77 @Test testDeserializeData()78 public void testDeserializeData() throws InvalidProtocolBufferException { 79 UwbConfigProto.UwbConfig.Builder builder = UwbConfigProto.UwbConfig.newBuilder(); 80 builder.setVersion(1); 81 mServiceProfileData.serializeData(builder); 82 byte[] dataBytes = builder.build().toByteArray(); 83 UwbConfigProto.UwbConfig uwbConfig = UwbConfigProto.UwbConfig.parseFrom(dataBytes); 84 85 mServiceProfileData.deserializeData(uwbConfig); 86 assertEquals(1, mDataSource.mData.size()); 87 88 mServiceProfileData.resetData(); 89 assertNull(mDataSource.mData); 90 91 assertTrue(mServiceProfileData.hasNewDataToSerialize()); 92 93 assertEquals(mServiceProfileData.getName(), "ServiceProfileData"); 94 95 assertEquals(mServiceProfileData.getStoreFileId(), 1); 96 } 97 98 private static class MockDataSource implements ServiceProfileData.DataSource { 99 100 public Map<UUID, ServiceProfileData.ServiceProfileInfo> mData = 101 new HashMap<>(); 102 @Override toSerialize()103 public Map<UUID, ServiceProfileInfo> toSerialize() { 104 Map<UUID, ServiceProfileInfo> mServiceProfileMap = 105 new HashMap<>(); 106 UUID serviceInstanceID = new UUID(100, 500); 107 int uid = 1; 108 String packageName = "test"; 109 int serviceID = 1; 110 ServiceProfileInfo mServiceProfileInfo = 111 new ServiceProfileInfo(serviceInstanceID, uid, packageName, serviceID); 112 mServiceProfileInfo.setAdfStatus(ADF_STATUS_PROVISIONED); 113 mServiceProfileInfo.setServiceAdfOid( 114 ObjectIdentifier.fromBytes(new byte[] {(byte) 1})); 115 mServiceProfileMap.put(serviceInstanceID, mServiceProfileInfo); 116 return mServiceProfileMap; 117 } 118 119 @Override fromDeserialized( Map<UUID, ServiceProfileData.ServiceProfileInfo> serviceProfileData)120 public void fromDeserialized( 121 Map<UUID, ServiceProfileData.ServiceProfileInfo> serviceProfileData) { 122 mData = serviceProfileData; 123 } 124 125 @Override reset()126 public void reset() { 127 mData = null; 128 } 129 130 @Override hasNewDataToSerialize()131 public boolean hasNewDataToSerialize() { 132 return true; 133 } 134 } 135 } 136