• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net;
6 
7 import org.jni_zero.CalledByNativeForTesting;
8 
9 /** Utility functions for testing features implemented in AndroidNetworkLibrary. */
10 public class AndroidNetworkLibraryTestUtil {
11     private static int sPerHostCleartextCheckCount;
12     private static int sDefaultCleartextCheckCount;
13 
14     /** Helper for tests that simulates an app controlling cleartext traffic on M and newer. */
15     @CalledByNativeForTesting
setUpSecurityPolicyForTesting(boolean cleartextPermitted)16     public static void setUpSecurityPolicyForTesting(boolean cleartextPermitted) {
17         sDefaultCleartextCheckCount = 0;
18         sPerHostCleartextCheckCount = 0;
19         AndroidNetworkLibrary.NetworkSecurityPolicyProxy.setInstanceForTesting(
20                 new AndroidNetworkLibrary.NetworkSecurityPolicyProxy() {
21                     @Override
22                     public boolean isCleartextTrafficPermitted(String host) {
23                         ++sPerHostCleartextCheckCount;
24                         if (host.startsWith(".")) {
25                             throw new IllegalArgumentException("hostname can not start with .");
26                         }
27                         return cleartextPermitted;
28                     }
29 
30                     @Override
31                     public boolean isCleartextTrafficPermitted() {
32                         ++sDefaultCleartextCheckCount;
33                         return cleartextPermitted;
34                     }
35                 });
36     }
37 
38     @CalledByNativeForTesting
getPerHostCleartextCheckCount()39     private static int getPerHostCleartextCheckCount() {
40         return sPerHostCleartextCheckCount;
41     }
42 
43     @CalledByNativeForTesting
getDefaultCleartextCheckCount()44     private static int getDefaultCleartextCheckCount() {
45         return sDefaultCleartextCheckCount;
46     }
47 }
48