1 /* 2 * Copyright (C) 2019 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.dynsystem; 18 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.mock; 21 22 import android.content.Context; 23 24 import androidx.test.filters.SmallTest; 25 import androidx.test.platform.app.InstrumentationRegistry; 26 import androidx.test.runner.AndroidJUnit4; 27 28 import org.json.JSONException; 29 import org.junit.Assert; 30 import org.junit.BeforeClass; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.io.ByteArrayInputStream; 35 import java.io.IOException; 36 import java.net.URL; 37 import java.net.URLConnection; 38 import java.net.URLStreamHandler; 39 40 /** 41 * A test for KeyRevocationList.java 42 */ 43 @RunWith(AndroidJUnit4.class) 44 public class KeyRevocationListTest { 45 46 private static final String TAG = "KeyRevocationListTest"; 47 48 private static Context sContext; 49 50 private static String sBlocklistJsonString; 51 52 @BeforeClass setUpClass()53 public static void setUpClass() throws Exception { 54 sContext = InstrumentationRegistry.getInstrumentation().getContext(); 55 sBlocklistJsonString = 56 sContext.getString(com.android.dynsystem.tests.R.string.blocklist_json_string); 57 } 58 59 @Test 60 @SmallTest testFromJsonString()61 public void testFromJsonString() throws JSONException { 62 KeyRevocationList blocklist; 63 blocklist = KeyRevocationList.fromJsonString(sBlocklistJsonString); 64 Assert.assertNotNull(blocklist); 65 Assert.assertFalse(blocklist.mEntries.isEmpty()); 66 blocklist = KeyRevocationList.fromJsonString("{}"); 67 Assert.assertNotNull(blocklist); 68 Assert.assertTrue(blocklist.mEntries.isEmpty()); 69 } 70 71 @Test 72 @SmallTest testFromUrl()73 public void testFromUrl() throws IOException, JSONException { 74 URLConnection mockConnection = mock(URLConnection.class); 75 doReturn(new ByteArrayInputStream(sBlocklistJsonString.getBytes())) 76 .when(mockConnection).getInputStream(); 77 URL mockUrl = new URL( 78 "http", // protocol 79 "foo.bar", // host 80 80, // port 81 "baz", // file 82 new URLStreamHandler() { 83 @Override 84 protected URLConnection openConnection(URL url) { 85 return mockConnection; 86 } 87 }); 88 URL mockBadUrl = new URL( 89 "http", // protocol 90 "foo.bar", // host 91 80, // port 92 "baz", // file 93 new URLStreamHandler() { 94 @Override 95 protected URLConnection openConnection(URL url) throws IOException { 96 throw new IOException(); 97 } 98 }); 99 100 KeyRevocationList blocklist = KeyRevocationList.fromUrl(mockUrl); 101 Assert.assertNotNull(blocklist); 102 Assert.assertFalse(blocklist.mEntries.isEmpty()); 103 104 blocklist = null; 105 try { 106 blocklist = KeyRevocationList.fromUrl(mockBadUrl); 107 // Up should throw, down should be unreachable 108 Assert.fail("Expected IOException not thrown"); 109 } catch (IOException e) { 110 // This is expected, do nothing 111 } 112 Assert.assertNull(blocklist); 113 } 114 115 @Test 116 @SmallTest testIsRevoked()117 public void testIsRevoked() { 118 KeyRevocationList blocklist = new KeyRevocationList(); 119 blocklist.addEntry("key1", "REVOKED", "reason for key1"); 120 121 KeyRevocationList.RevocationStatus revocationStatus = 122 blocklist.getRevocationStatusForKey("key1"); 123 Assert.assertNotNull(revocationStatus); 124 Assert.assertEquals(revocationStatus.mReason, "reason for key1"); 125 126 revocationStatus = blocklist.getRevocationStatusForKey("key2"); 127 Assert.assertNull(revocationStatus); 128 129 Assert.assertTrue(blocklist.isRevoked("key1")); 130 Assert.assertFalse(blocklist.isRevoked("key2")); 131 } 132 } 133