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