1 /*
2  * Copyright (C) 2021 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.connectivity;
18 
19 import android.annotation.NonNull;
20 import android.security.LegacyVpnProfileStore;
21 
22 import com.android.internal.annotations.VisibleForTesting;
23 
24 /**
25  * Mockable indirection to the actual profile store.
26  * @hide
27  */
28 public class VpnProfileStore {
29     /**
30      * Stores the profile under the alias in the profile database. Existing profiles by the
31      * same name will be replaced.
32      * @param alias The name of the profile
33      * @param profile The profile.
34      * @return true if the profile was successfully added. False otherwise.
35      * @hide
36      */
37     @VisibleForTesting
put(@onNull String alias, @NonNull byte[] profile)38     public boolean put(@NonNull String alias, @NonNull byte[] profile) {
39         return LegacyVpnProfileStore.put(alias, profile);
40     }
41 
42     /**
43      * Retrieves a profile by the name alias from the profile database.
44      * @param alias Name of the profile to retrieve.
45      * @return The unstructured blob, that is the profile that was stored using
46      *         LegacyVpnProfileStore#put or with
47      *         android.security.Keystore.put(Credentials.VPN + alias).
48      *         Returns null if no profile was found.
49      * @hide
50      */
51     @VisibleForTesting
get(@onNull String alias)52     public byte[] get(@NonNull String alias) {
53         return LegacyVpnProfileStore.get(alias);
54     }
55 
56     /**
57      * Removes a profile by the name alias from the profile database.
58      * @param alias Name of the profile to be removed.
59      * @return True if a profile was removed. False if no such profile was found.
60      * @hide
61      */
62     @VisibleForTesting
remove(@onNull String alias)63     public boolean remove(@NonNull String alias) {
64         return LegacyVpnProfileStore.remove(alias);
65     }
66 
67     /**
68      * Lists the vpn profiles stored in the database.
69      * @return An array of strings representing the aliases stored in the profile database.
70      *         The return value may be empty but never null.
71      * @hide
72      */
73     @VisibleForTesting
list(@onNull String prefix)74     public @NonNull String[] list(@NonNull String prefix) {
75         return LegacyVpnProfileStore.list(prefix);
76     }
77 }
78