aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2020-03-04 20:31:01 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2020-03-04 20:31:01 +0000
commitefec9a88d1690995eb8477c33f4e18c097a40d01 (patch)
treeefb612fef3e22c977a12b615467032ce69f72870
parent33cd1666b50d4a15f6435869faa0fbf2b8da45b5 (diff)
parentaef7b05a08581b4d8c8853c9fb1f8c8aae850a62 (diff)
downloadtradefederation-efec9a88d1690995eb8477c33f4e18c097a40d01.tar.gz
Merge "[DO NOT MERGE] Ensure the unit tests are passing when TF run as root" into pie-cts-dev
-rw-r--r--tests/src/com/android/tradefed/result/SubprocessResultsReporterTest.java4
-rw-r--r--tests/src/com/android/tradefed/util/keystore/JSONFileKeyStoreClientTest.java33
2 files changed, 27 insertions, 10 deletions
diff --git a/tests/src/com/android/tradefed/result/SubprocessResultsReporterTest.java b/tests/src/com/android/tradefed/result/SubprocessResultsReporterTest.java
index 7b9d86794..9a7c020bb 100644
--- a/tests/src/com/android/tradefed/result/SubprocessResultsReporterTest.java
+++ b/tests/src/com/android/tradefed/result/SubprocessResultsReporterTest.java
@@ -86,7 +86,9 @@ public class SubprocessResultsReporterTest {
OptionSetter setter = new OptionSetter(mReporter);
File tmpReportFile = FileUtil.createTempFile("subprocess-reporter", "unittest");
try {
- tmpReportFile.setWritable(false);
+ // Delete the file to make it non-writable. (do not use setWritable as a root tradefed
+ // process would still be able to write it)
+ tmpReportFile.delete();
setter.setOptionValue("subprocess-report-file", tmpReportFile.getAbsolutePath());
mReporter.testRunStarted("TEST", 5);
fail("Should have thrown an exception.");
diff --git a/tests/src/com/android/tradefed/util/keystore/JSONFileKeyStoreClientTest.java b/tests/src/com/android/tradefed/util/keystore/JSONFileKeyStoreClientTest.java
index f41017bbc..a5df26b97 100644
--- a/tests/src/com/android/tradefed/util/keystore/JSONFileKeyStoreClientTest.java
+++ b/tests/src/com/android/tradefed/util/keystore/JSONFileKeyStoreClientTest.java
@@ -16,26 +16,30 @@
package com.android.tradefed.util.keystore;
-import com.android.tradefed.util.FileUtil;
+import static org.junit.Assert.*;
-import junit.framework.TestCase;
+import com.android.tradefed.util.FileUtil;
import org.json.JSONObject;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
import java.io.File;
-/**
- * Unit tests for JSON File Key Store Client test.
- */
-public class JSONFileKeyStoreClientTest extends TestCase {
+/** Unit tests for JSON File Key Store Client test. */
+@RunWith(JUnit4.class)
+public class JSONFileKeyStoreClientTest {
final String mJsonData = new String("{\"key1\":\"value 1\",\"key2 \":\"foo\"}");
JSONFileKeyStoreClient mKeyStore = null;
- @Override
- protected void setUp() throws Exception {
+ @Before
+ public void setUp() throws Exception {
mKeyStore = new JSONFileKeyStoreClient();
}
+ @Test
public void testKeyStoreNullFile() throws Exception {
try {
new JSONFileKeyStoreClient(null);
@@ -45,10 +49,13 @@ public class JSONFileKeyStoreClientTest extends TestCase {
}
}
+ @Test
public void testKeyStoreFetchUnreadableFile() throws Exception {
File test = FileUtil.createTempFile("keystore", "test");
- test.setReadable(false);
try {
+ // Delete the file to make it non-readable. (do not use setReadable as a root tradefed
+ // process would still be able to write it)
+ test.delete();
new JSONFileKeyStoreClient(test);
fail("Should have thrown an exception");
} catch(KeyStoreException expected) {
@@ -59,6 +66,7 @@ public class JSONFileKeyStoreClientTest extends TestCase {
}
}
+ @Test
public void testKeyStoreFetchEmptyFile() throws Exception {
File test = FileUtil.createTempFile("keystore", "test");
try {
@@ -71,6 +79,7 @@ public class JSONFileKeyStoreClientTest extends TestCase {
}
}
+ @Test
public void testKeyStoreFetchFile() throws Exception {
File test = FileUtil.createTempFile("keystore", "test");
try {
@@ -83,11 +92,13 @@ public class JSONFileKeyStoreClientTest extends TestCase {
}
}
+ @Test
public void testContainsKeyinNullKeyStore() throws Exception {
mKeyStore.setKeyStore(null);
assertFalse("Key should not exist in null key store", mKeyStore.containsKey("test"));
}
+ @Test
public void testDoesNotContainMissingKey() throws Exception {
JSONObject data = new JSONObject(mJsonData);
mKeyStore.setKeyStore(data);
@@ -95,12 +106,14 @@ public class JSONFileKeyStoreClientTest extends TestCase {
mKeyStore.containsKey("invalid key"));
}
+ @Test
public void testContainsValidKey() throws Exception {
JSONObject data = new JSONObject(mJsonData);
mKeyStore.setKeyStore(data);
assertTrue("Failed to fetch valid key in key store", mKeyStore.containsKey("key1"));
}
+ @Test
public void testFetchMissingKey() throws Exception {
JSONObject data = new JSONObject(mJsonData);
mKeyStore.setKeyStore(data);
@@ -108,6 +121,7 @@ public class JSONFileKeyStoreClientTest extends TestCase {
mKeyStore.fetchKey("invalid key"));
}
+ @Test
public void testFetchNullKey() throws Exception {
JSONObject data = new JSONObject(mJsonData);
mKeyStore.setKeyStore(data);
@@ -115,6 +129,7 @@ public class JSONFileKeyStoreClientTest extends TestCase {
mKeyStore.fetchKey(null));
}
+ @Test
public void testFetchValidKey() throws Exception {
JSONObject data = new JSONObject(mJsonData);
mKeyStore.setKeyStore(data);