1 /* 2 * Copyright 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.adservices.ondevicepersonalization; 18 19 import static org.junit.Assert.assertArrayEquals; 20 import static org.junit.Assert.assertEquals; 21 import static org.junit.Assert.assertNull; 22 23 import android.adservices.ondevicepersonalization.aidl.IDataAccessService; 24 import android.adservices.ondevicepersonalization.aidl.IDataAccessServiceCallback; 25 import android.os.Bundle; 26 import android.os.RemoteException; 27 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 import androidx.test.filters.SmallTest; 30 31 import com.android.ondevicepersonalization.internal.util.ByteArrayParceledSlice; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.util.HashMap; 37 import java.util.HashSet; 38 import java.util.Set; 39 40 /** 41 * Unit Tests of RemoteData API. 42 */ 43 @SmallTest 44 @RunWith(AndroidJUnit4.class) 45 public class RemoteDataTest { 46 KeyValueStore mRemoteData = new RemoteDataImpl( 47 IDataAccessService.Stub.asInterface( 48 new RemoteDataService())); 49 50 @Test testLookupSuccess()51 public void testLookupSuccess() throws Exception { 52 assertArrayEquals(new byte[] {1, 2, 3}, mRemoteData.get("a")); 53 assertArrayEquals(new byte[] {7, 8, 9}, mRemoteData.get("c")); 54 assertNull(mRemoteData.get("e")); 55 } 56 57 @Test testLookupError()58 public void testLookupError() { 59 // Triggers an expected error in the mock service. 60 assertNull(mRemoteData.get("z")); 61 } 62 63 @Test testKeysetSuccess()64 public void testKeysetSuccess() { 65 Set<String> expectedResult = new HashSet<>(); 66 expectedResult.add("a"); 67 expectedResult.add("b"); 68 expectedResult.add("c"); 69 70 assertEquals(expectedResult, mRemoteData.keySet()); 71 } 72 73 public static class RemoteDataService extends IDataAccessService.Stub { 74 HashMap<String, byte[]> mContents = new HashMap<String, byte[]>(); 75 RemoteDataService()76 public RemoteDataService() { 77 mContents.put("a", new byte[] {1, 2, 3}); 78 mContents.put("b", new byte[] {4, 5, 6}); 79 mContents.put("c", new byte[] {7, 8, 9}); 80 } 81 82 @Override onRequest( int operation, Bundle params, IDataAccessServiceCallback callback)83 public void onRequest( 84 int operation, 85 Bundle params, 86 IDataAccessServiceCallback callback) { 87 88 if (operation == Constants.DATA_ACCESS_OP_REMOTE_DATA_KEYSET) { 89 Bundle result = new Bundle(); 90 result.putSerializable(Constants.EXTRA_RESULT, 91 new HashSet<>(mContents.keySet())); 92 try { 93 callback.onSuccess(result); 94 } catch (RemoteException e) { 95 // Ignored. 96 } 97 return; 98 } 99 100 if (operation != Constants.DATA_ACCESS_OP_REMOTE_DATA_LOOKUP) { 101 throw new IllegalArgumentException("op: " + operation); 102 } 103 104 String key = params.getString(Constants.EXTRA_LOOKUP_KEYS); 105 if (key == null) { 106 throw new NullPointerException("key"); 107 } 108 109 if (key.equals("z")) { 110 // Raise expected error. 111 try { 112 callback.onError(Constants.STATUS_INTERNAL_ERROR); 113 } catch (RemoteException e) { 114 // Ignored. 115 } 116 return; 117 } 118 byte[] value = null; 119 if (mContents.containsKey(key)) { 120 value = mContents.get(key); 121 } 122 Bundle result = new Bundle(); 123 result.putParcelable(Constants.EXTRA_RESULT, new ByteArrayParceledSlice(value)); 124 try { 125 callback.onSuccess(result); 126 } catch (RemoteException e) { 127 // Ignored. 128 } 129 } 130 131 @Override logApiCallStats(int apiName, long latencyMillis, int responseCode)132 public void logApiCallStats(int apiName, long latencyMillis, int responseCode) {} 133 } 134 } 135