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