1 /* 2 * Copyright (C) 2018 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.net.watchlist; 18 19 import android.content.Context; 20 import android.os.Binder; 21 import android.os.ParcelFileDescriptor; 22 import android.os.RemoteException; 23 import android.os.ShellCommand; 24 import android.provider.Settings; 25 26 import java.io.InputStream; 27 import java.io.PrintWriter; 28 29 /** 30 * Network watchlist shell commands class, to provide a way to set temporary watchlist config for 31 * testing in shell, so CTS / GTS can use it to verify if watchlist feature is working properly. 32 */ 33 class NetworkWatchlistShellCommand extends ShellCommand { 34 35 final Context mContext; 36 final NetworkWatchlistService mService; 37 NetworkWatchlistShellCommand(NetworkWatchlistService service, Context context)38 NetworkWatchlistShellCommand(NetworkWatchlistService service, Context context) { 39 mContext = context; 40 mService = service; 41 } 42 43 @Override onCommand(String cmd)44 public int onCommand(String cmd) { 45 if (cmd == null) { 46 return handleDefaultCommands(cmd); 47 } 48 49 final PrintWriter pw = getOutPrintWriter(); 50 try { 51 switch(cmd) { 52 case "set-test-config": 53 return runSetTestConfig(); 54 case "force-generate-report": 55 return runForceGenerateReport(); 56 default: 57 return handleDefaultCommands(cmd); 58 } 59 } catch (Exception e) { 60 pw.println("Exception: " + e); 61 } 62 return -1; 63 } 64 65 /** 66 * Method to get fd from input xml path, and set it as temporary watchlist config. 67 */ runSetTestConfig()68 private int runSetTestConfig() throws RemoteException { 69 final PrintWriter pw = getOutPrintWriter(); 70 try { 71 final String configXmlPath = getNextArgRequired(); 72 final ParcelFileDescriptor pfd = openFileForSystem(configXmlPath, "r"); 73 if (pfd == null) { 74 pw.println("Error: can't open input file " + configXmlPath); 75 return -1; 76 } 77 try (InputStream inputStream = new ParcelFileDescriptor.AutoCloseInputStream(pfd)) { 78 WatchlistConfig.getInstance().setTestMode(inputStream); 79 } 80 pw.println("Success!"); 81 } catch (Exception ex) { 82 pw.println("Error: " + ex.toString()); 83 return -1; 84 } 85 return 0; 86 } 87 runForceGenerateReport()88 private int runForceGenerateReport() throws RemoteException { 89 final PrintWriter pw = getOutPrintWriter(); 90 final long ident = Binder.clearCallingIdentity(); 91 try { 92 // Reset last report time 93 if (WatchlistConfig.getInstance().isConfigSecure()) { 94 pw.println("Error: Cannot force generate report under production config"); 95 return -1; 96 } 97 Settings.Global.putLong(mContext.getContentResolver(), 98 Settings.Global.NETWORK_WATCHLIST_LAST_REPORT_TIME, 0L); 99 mService.forceReportWatchlistForTest(System.currentTimeMillis()); 100 pw.println("Success!"); 101 } catch (Exception ex) { 102 pw.println("Error: " + ex); 103 return -1; 104 } finally { 105 Binder.restoreCallingIdentity(ident); 106 } 107 return 0; 108 } 109 110 @Override onHelp()111 public void onHelp() { 112 final PrintWriter pw = getOutPrintWriter(); 113 pw.println("Network watchlist manager commands:"); 114 pw.println(" help"); 115 pw.println(" Print this help text."); 116 pw.println(" set-test-config your_watchlist_config.xml"); 117 pw.println(" Set network watchlist test config file."); 118 pw.println(" force-generate-report"); 119 pw.println(" Force generate watchlist test report."); 120 } 121 } 122