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 17 package com.android.tradefed.util.keystore; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNull; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.fail; 24 25 import com.android.tradefed.util.FileUtil; 26 27 import org.json.JSONObject; 28 import org.junit.Before; 29 import org.junit.Test; 30 import org.junit.runner.RunWith; 31 import org.junit.runners.JUnit4; 32 33 import java.io.File; 34 35 /** Unit tests for JSON File Key Store Client test. */ 36 @RunWith(JUnit4.class) 37 public class JSONFileKeyStoreClientTest { 38 final String mJsonData = new String("{\"key1\":\"value 1\",\"key2 \":\"foo\"}"); 39 JSONFileKeyStoreClient mKeyStore = null; 40 41 @Before setUp()42 public void setUp() throws Exception { 43 mKeyStore = new JSONFileKeyStoreClient(); 44 } 45 46 @Test testKeyStoreNullFile()47 public void testKeyStoreNullFile() throws Exception { 48 try { 49 new JSONFileKeyStoreClient(null); 50 fail("Key store should not be available for null file"); 51 } catch (KeyStoreException e) { 52 // Expected. 53 } 54 } 55 56 @Test testKeyStoreFetchUnreadableFile()57 public void testKeyStoreFetchUnreadableFile() throws Exception { 58 File test = FileUtil.createTempFile("keystore", "test"); 59 try { 60 // Delete the file to make it non-readable. (do not use setReadable as a root tradefed 61 // process would still be able to write it) 62 test.delete(); 63 new JSONFileKeyStoreClient(test); 64 fail("Should have thrown an exception"); 65 } catch(KeyStoreException expected) { 66 assertEquals(String.format("Unable to read the JSON key store file %s", test), 67 expected.getMessage()); 68 } finally { 69 FileUtil.deleteFile(test); 70 } 71 } 72 73 @Test testKeyStoreFetchEmptyFile()74 public void testKeyStoreFetchEmptyFile() throws Exception { 75 File test = FileUtil.createTempFile("keystore", "test"); 76 try { 77 new JSONFileKeyStoreClient(test); 78 fail("Should have thrown an exception"); 79 } catch(KeyStoreException expected) { 80 // expected 81 } finally { 82 FileUtil.deleteFile(test); 83 } 84 } 85 86 @Test testKeyStoreFetchFile()87 public void testKeyStoreFetchFile() throws Exception { 88 File test = FileUtil.createTempFile("keystore", "test"); 89 try { 90 FileUtil.writeToFile(mJsonData, test); 91 JSONFileKeyStoreClient keystore = new JSONFileKeyStoreClient(test); 92 assertTrue(keystore.isAvailable()); 93 assertEquals("value 1", keystore.fetchKey("key1")); 94 } finally { 95 FileUtil.deleteFile(test); 96 } 97 } 98 99 @Test testContainsKeyinNullKeyStore()100 public void testContainsKeyinNullKeyStore() throws Exception { 101 mKeyStore.setKeyStore(null); 102 assertFalse("Key should not exist in null key store", mKeyStore.containsKey("test")); 103 } 104 105 @Test testDoesNotContainMissingKey()106 public void testDoesNotContainMissingKey() throws Exception { 107 JSONObject data = new JSONObject(mJsonData); 108 mKeyStore.setKeyStore(data); 109 assertFalse("Missing key should not exist in key store", 110 mKeyStore.containsKey("invalid key")); 111 } 112 113 @Test testContainsValidKey()114 public void testContainsValidKey() throws Exception { 115 JSONObject data = new JSONObject(mJsonData); 116 mKeyStore.setKeyStore(data); 117 assertTrue("Failed to fetch valid key in key store", mKeyStore.containsKey("key1")); 118 } 119 120 @Test testFetchMissingKey()121 public void testFetchMissingKey() throws Exception { 122 JSONObject data = new JSONObject(mJsonData); 123 mKeyStore.setKeyStore(data); 124 assertNull("Missing key should not exist in key store", 125 mKeyStore.fetchKey("invalid key")); 126 } 127 128 @Test testFetchNullKey()129 public void testFetchNullKey() throws Exception { 130 JSONObject data = new JSONObject(mJsonData); 131 mKeyStore.setKeyStore(data); 132 assertNull("Null key should not exist in key store", 133 mKeyStore.fetchKey(null)); 134 } 135 136 @Test testFetchValidKey()137 public void testFetchValidKey() throws Exception { 138 JSONObject data = new JSONObject(mJsonData); 139 mKeyStore.setKeyStore(data); 140 assertEquals("value 1", mKeyStore.fetchKey("key1")); 141 } 142 143 @Test testSetKey()144 public void testSetKey() throws Exception { 145 JSONObject data = new JSONObject(mJsonData); 146 mKeyStore.setKeyStore(data); 147 mKeyStore.setKey("key1", "value 2"); 148 assertEquals("value 2", mKeyStore.fetchKey("key1")); 149 } 150 } 151