Home | History | Annotate | Download | only in keystore
      1 /*
      2  * Copyright (C) 2017 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 package com.android.tradefed.util.keystore;
     17 
     18 import static org.junit.Assert.assertNotNull;
     19 import static org.junit.Assert.assertNotSame;
     20 import static org.junit.Assert.assertSame;
     21 
     22 import com.android.tradefed.config.OptionSetter;
     23 import com.android.tradefed.util.FileUtil;
     24 
     25 import org.junit.After;
     26 import org.junit.Before;
     27 import org.junit.Test;
     28 import org.junit.runner.RunWith;
     29 import org.junit.runners.JUnit4;
     30 
     31 import java.io.File;
     32 
     33 /** Unit tests for {@link JSONFileKeyStoreFactory}. */
     34 @RunWith(JUnit4.class)
     35 public class JSONFileKeyStoreFactoryTest {
     36 
     37     private final String mJsonData = new String("{\"key1\":\"value 1\",\"key2 \":\"foo\"}");
     38     private JSONFileKeyStoreFactory mFactory;
     39     private File mJsonFile;
     40 
     41     @Before
     42     public void setUp() throws Exception {
     43         mFactory = new JSONFileKeyStoreFactory();
     44         mJsonFile = FileUtil.createTempFile("json-keystore", ".json");
     45         FileUtil.writeToFile(mJsonData, mJsonFile);
     46     }
     47 
     48     @After
     49     public void tearDown() {
     50         FileUtil.recursiveDelete(mJsonFile);
     51     }
     52 
     53     /**
     54      * Test that if the underlying JSON keystore has not changed, the client returned is the same.
     55      */
     56     @Test
     57     public void testLoadKeyStore_same() throws Exception {
     58         OptionSetter setter = new OptionSetter(mFactory);
     59         setter.setOptionValue("json-key-store-file", mJsonFile.getAbsolutePath());
     60         IKeyStoreClient client = mFactory.createKeyStoreClient();
     61         assertNotNull(client);
     62         IKeyStoreClient client2 = mFactory.createKeyStoreClient();
     63         assertSame(client, client2);
     64     }
     65 
     66     /** Test that if the last modified flag has changed, we reload a new client. */
     67     @Test
     68     public void testLoadKeyStore_modified() throws Exception {
     69         OptionSetter setter = new OptionSetter(mFactory);
     70         setter.setOptionValue("json-key-store-file", mJsonFile.getAbsolutePath());
     71         IKeyStoreClient client = mFactory.createKeyStoreClient();
     72         assertNotNull(client);
     73         mJsonFile.setLastModified(mJsonFile.lastModified() + 5000l);
     74         IKeyStoreClient client2 = mFactory.createKeyStoreClient();
     75         assertNotNull(client2);
     76         assertNotSame(client, client2);
     77     }
     78 
     79     /**
     80      * Test that if the underlying file is not accessible anymore for any reason, we rely on the
     81      * cache.
     82      */
     83     @Test
     84     public void testLoadKeyStore_null() throws Exception {
     85         OptionSetter setter = new OptionSetter(mFactory);
     86         setter.setOptionValue("json-key-store-file", mJsonFile.getAbsolutePath());
     87         IKeyStoreClient client = mFactory.createKeyStoreClient();
     88         assertNotNull(client);
     89         FileUtil.deleteFile(mJsonFile);
     90         IKeyStoreClient client2 = mFactory.createKeyStoreClient();
     91         assertNotNull(client2);
     92         assertSame(client, client2);
     93     }
     94 }
     95