1 /*
2  * Copyright 2023 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.federatedcompute.services;
18 
19 import static com.android.federatedcompute.services.common.Constants.ISOLATED_TRAINING_SERVICE_NAME;
20 
21 import static org.junit.Assert.assertNotNull;
22 
23 import android.content.Context;
24 
25 import androidx.test.core.app.ApplicationProvider;
26 
27 import com.android.federatedcompute.internal.util.AbstractServiceBinder;
28 import com.android.federatedcompute.services.training.aidl.IIsolatedTrainingService;
29 
30 import org.junit.Test;
31 
32 public class IsolatedServiceBinderTest {
33 
34     private final Context mContext = ApplicationProvider.getApplicationContext();
35 
36     @Test
testFcpServiceBindingByName()37     public void testFcpServiceBindingByName() {
38         AbstractServiceBinder<IIsolatedTrainingService> serviceBinder =
39                 AbstractServiceBinder.getServiceBinderByServiceName(
40                         mContext,
41                         ISOLATED_TRAINING_SERVICE_NAME,
42                         mContext.getPackageName(),
43                         IIsolatedTrainingService.Stub::asInterface);
44 
45         final IIsolatedTrainingService service = serviceBinder.getService(Runnable::run);
46         assertNotNull(service);
47     }
48 
49     /**
50      * Test the isolated service binding implementation, specifically, we bind to services through
51      * the bindIsolatedService() API but with the shared isolated process flag omitted.
52      */
53     @Test
testIsolatedProcessBinding()54     public void testIsolatedProcessBinding() {
55         AbstractServiceBinder<IIsolatedTrainingService> serviceBinder =
56                 AbstractServiceBinder.getIsolatedServiceBinderByServiceName(
57                         mContext,
58                         ISOLATED_TRAINING_SERVICE_NAME,
59                         mContext.getPackageName(),
60                         "testSharedIsolatedProcessBinding",
61                         0,
62                         IIsolatedTrainingService.Stub::asInterface);
63 
64         final IIsolatedTrainingService service = serviceBinder.getService(Runnable::run);
65         assertNotNull(service);
66     }
67 
68     /**
69      * Test the isolated service binding implementation, specifically, we bind to services through
70      * the bindIsolatedService() API but with the shared isolated process flag included.
71      */
72     @Test
testSharedIsolatedProcessBinding()73     public void testSharedIsolatedProcessBinding() {
74         AbstractServiceBinder<IIsolatedTrainingService> serviceBinder =
75                 AbstractServiceBinder.getIsolatedServiceBinderByServiceName(
76                         mContext,
77                         ISOLATED_TRAINING_SERVICE_NAME,
78                         mContext.getPackageName(),
79                         "testSharedIsolatedProcessBinding",
80                         Context.BIND_SHARED_ISOLATED_PROCESS,
81                         IIsolatedTrainingService.Stub::asInterface);
82 
83         final IIsolatedTrainingService service = serviceBinder.getService(Runnable::run);
84         assertNotNull(service);
85     }
86 }
87