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.imsserviceentitlement.debug;
18 
19 import android.os.Build;
20 import android.os.SystemProperties;
21 import android.text.TextUtils;
22 
23 import java.util.Optional;
24 
25 /** Provides API for debugging and not allow to debug on user build. */
26 public final class DebugUtils {
27     private static final String PROP_PII_LOGGABLE = "dbg.imsse.pii_loggable";
28     private static final String PROP_BYPASS_EAP_AKA_RESPONSE = "dbg.imsse.bypass_eap_aka_response";
29     private static final String PROP_SERVER_URL_OVERRIDE = "persist.dbg.imsse.server_url";
30     private static final String BUILD_TYPE_USER = "user";
31 
DebugUtils()32     private DebugUtils() {}
33 
34     /**
35      * Tells if current build is user-debug or eng build which is debuggable.
36      *
37      * @see {@link android.os.Build.TYPE}
38      */
isDebugBuild()39     public static boolean isDebugBuild() {
40         return !BUILD_TYPE_USER.equals(Build.TYPE);
41     }
42 
43     /** Returns {@code true} if allow to print PII data for debugging. */
isPiiLoggable()44     public static boolean isPiiLoggable() {
45         if (!isDebugBuild()) {
46             return false;
47         }
48 
49         return SystemProperties.getBoolean(PROP_PII_LOGGABLE, false);
50     }
51 
52     /** Returns a non empty string if bypass EAP-AKA authentication is enabled. */
getBypassEapAkaResponse()53     public static String getBypassEapAkaResponse() {
54         if (!isDebugBuild()) {
55             return "";
56         }
57 
58         return SystemProperties.get(PROP_BYPASS_EAP_AKA_RESPONSE);
59     }
60 
61     /**
62      * Returns {@link Optional} if testing server url was set in system property.
63      */
getOverrideServerUrl()64     public static Optional<String> getOverrideServerUrl() {
65         if (!isDebugBuild()) {
66             return Optional.empty();
67         }
68 
69         String urlOverride = SystemProperties.get(PROP_SERVER_URL_OVERRIDE, "");
70         if (TextUtils.isEmpty(urlOverride)) {
71             return Optional.empty();
72         }
73 
74         return Optional.of(urlOverride);
75     }
76 }
77