1 /* 2 * Copyright (C) 2019 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.internal.net; 18 19 import static org.mockito.Matchers.anyObject; 20 import static org.mockito.Matchers.anyString; 21 import static org.mockito.Mockito.doAnswer; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.spy; 25 26 import com.android.internal.net.ipsec.ike.utils.RandomnessFactory; 27 import com.android.internal.net.utils.Log; 28 29 import java.nio.ByteBuffer; 30 31 /** TestUtils provides utility methods to facilitate IKE unit tests */ 32 public class TestUtils { hexStringToByteArray(String hexString)33 public static byte[] hexStringToByteArray(String hexString) { 34 int len = hexString.length(); 35 if (len % 2 != 0) { 36 throw new IllegalArgumentException("Invalid Hex String"); 37 } 38 byte[] data = new byte[len / 2]; 39 for (int i = 0; i < len; i += 2) { 40 data[i / 2] = 41 (byte) 42 ((Character.digit(hexString.charAt(i), 16) << 4) 43 + Character.digit(hexString.charAt(i + 1), 16)); 44 } 45 return data; 46 } 47 hexStringToInt(String hexString)48 public static int hexStringToInt(String hexString) { 49 if (hexString.length() > 8) { 50 throw new IllegalArgumentException("Invalid hex string length for integer type"); 51 } 52 53 for (int i = hexString.length(); i < 8; i++) { 54 hexString = "0" + hexString; 55 } 56 57 return ByteBuffer.wrap(hexStringToByteArray(hexString)).getInt(); 58 } 59 stringToHexString(String s)60 public static String stringToHexString(String s) { 61 // two hex characters for each char in s 62 StringBuilder sb = new StringBuilder(s.length() * 2); 63 char[] chars = s.toCharArray(); 64 for (char c : chars) { 65 sb.append(Integer.toHexString(c)); 66 } 67 return sb.toString(); 68 } 69 makeSpyLogThrowExceptionForWtf(String tag)70 public static Log makeSpyLogThrowExceptionForWtf(String tag) { 71 Log spyLog = spy(new Log(tag, true /*logSensitive*/)); 72 73 doAnswer( 74 (invocation) -> { 75 throw new IllegalStateException((String) invocation.getArguments()[1]); 76 }) 77 .when(spyLog) 78 .wtf(anyString(), anyString()); 79 80 doAnswer( 81 (invocation) -> { 82 throw (Throwable) invocation.getArguments()[2]; 83 }) 84 .when(spyLog) 85 .wtf(anyString(), anyString(), anyObject()); 86 87 return spyLog; 88 } 89 makeSpyLogDoLogErrorForWtf(String tag)90 public static Log makeSpyLogDoLogErrorForWtf(String tag) { 91 Log spyLog = spy(new Log(tag, true /*logSensitive*/)); 92 93 doAnswer( 94 (invocation) -> { 95 spyLog.e( 96 "Mock logging WTF: " + invocation.getArguments()[0], 97 (String) invocation.getArguments()[1]); 98 return null; 99 }) 100 .when(spyLog) 101 .wtf(anyString(), anyString()); 102 103 doAnswer( 104 (invocation) -> { 105 spyLog.e( 106 "Mock logging WTF: " + invocation.getArguments()[0], 107 (String) invocation.getArguments()[1], 108 (Throwable) invocation.getArguments()[2]); 109 return null; 110 }) 111 .when(spyLog) 112 .wtf(anyString(), anyString(), anyObject()); 113 114 return spyLog; 115 } 116 createMockRandomFactory()117 public static RandomnessFactory createMockRandomFactory() { 118 RandomnessFactory rFactory = mock(RandomnessFactory.class); 119 doReturn(null).when(rFactory).getRandom(); 120 return rFactory; 121 } 122 } 123