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