1/* 2 * Copyright (C) 2016 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 17package com.android.server.wifi; 18 19import static org.junit.Assert.assertArrayEquals; 20import static org.junit.Assert.assertEquals; 21 22import android.content.Context; 23import android.os.UserHandle; 24import android.security.Credentials; 25import android.security.KeyStore; 26import android.test.suitebuilder.annotation.SmallTest; 27import android.util.Log; 28 29import org.junit.Before; 30import org.junit.Rule; 31import org.junit.Test; 32import org.junit.rules.TemporaryFolder; 33import org.mockito.Mock; 34 35import java.io.File; 36import java.util.Arrays; 37import java.util.HashSet; 38 39/** 40 * Unit tests for {@link com.android.server.wifi.WifiCertManager}. 41 */ 42@SmallTest 43public class WifiCertManagerTest { 44 private static final String TAG = "WifiCertManagerTest"; 45 private byte[] mConfig; 46 private String mConfigFile = ""; 47 48 @Mock private Context mContext; 49 @Rule public TemporaryFolder mTempFolder = new TemporaryFolder(); 50 51 public WifiCertManagerTest() { 52 mConfig = null; 53 } 54 55 @Before 56 public void setUp() { 57 try { 58 File configFile = mTempFolder.newFile(); 59 mConfigFile = configFile.getAbsolutePath(); 60 configFile.delete(); 61 } catch (Exception e) { 62 Log.e(TAG, "Failed to construct test", e); 63 } 64 } 65 66 /** 67 * This class is created to avoid mocking file system and KeyStore. 68 */ 69 private class TestWifiCertManager extends WifiCertManager { 70 private boolean mAffiliatedUser; 71 72 public TestWifiCertManager(Context context) { 73 super(context, mConfigFile); 74 mAffiliatedUser = false; 75 } 76 77 protected String[] listClientCertsForAllUsers() { 78 String prefix = Credentials.USER_PRIVATE_KEY; 79 String mockAnswer[] = {prefix + "abc", prefix + "def", prefix + "ghi"}; 80 return mockAnswer; 81 } 82 83 protected byte[] readConfigFile() { 84 return mConfig; 85 } 86 87 protected void writeConfigFile(byte[] payload) { 88 mConfig = payload; 89 } 90 91 protected boolean isAffiliatedUser() { 92 return mAffiliatedUser; 93 } 94 95 public void setAffiliatedUser(boolean value) { 96 mAffiliatedUser = value; 97 } 98 } 99 100 @Test 101 public void testEmptyConfigFile() { 102 WifiCertManager certManager = new WifiCertManager(mContext, mConfigFile); 103 final String[] expected = 104 KeyStore.getInstance().list( 105 Credentials.USER_PRIVATE_KEY, UserHandle.myUserId()); 106 assertArrayEquals(expected, certManager.listClientCertsForCurrentUser()); 107 } 108 109 @Test 110 public void testOperations() { 111 TestWifiCertManager certManager = new TestWifiCertManager(mContext); 112 final HashSet<String> expected1 = new HashSet<>(); 113 String prefix = Credentials.USER_PRIVATE_KEY; 114 expected1.add(prefix + "abc"); 115 expected1.add(prefix + "def"); 116 expected1.add(prefix + "ghi"); 117 118 final HashSet<String> expected2 = new HashSet<>(); 119 expected2.add(prefix + "abc"); 120 121 certManager.setAffiliatedUser(false); 122 assertEquals(expected1, 123 new HashSet<>(Arrays.asList(certManager.listClientCertsForCurrentUser()))); 124 125 certManager.hideCertFromUnaffiliatedUsers("def"); 126 certManager.hideCertFromUnaffiliatedUsers("ghi"); 127 assertEquals(expected2, 128 new HashSet<>(Arrays.asList(certManager.listClientCertsForCurrentUser()))); 129 130 certManager.setAffiliatedUser(true); 131 assertEquals(expected1, 132 new HashSet<>(Arrays.asList(certManager.listClientCertsForCurrentUser()))); 133 134 TestWifiCertManager certManager2 = new TestWifiCertManager(mContext); 135 certManager2.setAffiliatedUser(false); 136 assertEquals(expected2, 137 new HashSet<>(Arrays.asList(certManager2.listClientCertsForCurrentUser()))); 138 139 certManager2.setAffiliatedUser(true); 140 assertEquals(expected1, 141 new HashSet<>(Arrays.asList(certManager2.listClientCertsForCurrentUser()))); 142 } 143} 144