summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Kralevich <nnk@google.com>2010-03-19 16:57:21 -0700
committerNick Kralevich <nnk@google.com>2010-03-19 16:57:21 -0700
commit93a68398b661c02d6c417a2a04e64a6750a9a119 (patch)
treef8a9d195781f95f682202e1cf979abcf0646095e
parenta0a59122ebb7f1c134e8b8f9c0c90b7d90b86279 (diff)
downloadframeworks_base-93a68398b661c02d6c417a2a04e64a6750a9a119.zip
frameworks_base-93a68398b661c02d6c417a2a04e64a6750a9a119.tar.gz
frameworks_base-93a68398b661c02d6c417a2a04e64a6750a9a119.tar.bz2
Unittests for EntropyService. Make EntropyService more testable.
I've been meaning to write these tests for a long time... Use "runtest frameworks-services" to run these tests. Change-Id: I3a3cb7eda547f4a790f38be884b4a583426c7326
-rw-r--r--services/java/com/android/server/EntropyService.java21
-rw-r--r--services/tests/servicestests/src/com/android/server/EntropyServiceTest.java41
2 files changed, 57 insertions, 5 deletions
diff --git a/services/java/com/android/server/EntropyService.java b/services/java/com/android/server/EntropyService.java
index 0a53e9c..81ae26f 100644
--- a/services/java/com/android/server/EntropyService.java
+++ b/services/java/com/android/server/EntropyService.java
@@ -48,14 +48,15 @@ import android.util.Slog;
* instead of periodically.
*/
public class EntropyService extends Binder {
- private static final String ENTROPY_FILENAME = getSystemDir() + "/entropy.dat";
private static final String TAG = "EntropyService";
private static final int ENTROPY_WHAT = 1;
private static final int ENTROPY_WRITE_PERIOD = 3 * 60 * 60 * 1000; // 3 hrs
- private static final String RANDOM_DEV = "/dev/urandom";
private static final long START_TIME = System.currentTimeMillis();
private static final long START_NANOTIME = System.nanoTime();
+ private final String randomDevice;
+ private final String entropyFile;
+
/**
* Handler that periodically updates the entropy on disk.
*/
@@ -72,6 +73,16 @@ public class EntropyService extends Binder {
};
public EntropyService() {
+ this(getSystemDir() + "/entropy.dat", "/dev/urandom");
+ }
+
+ /** Test only interface, not for public use */
+ public EntropyService(String entropyFile, String randomDevice) {
+ if (randomDevice == null) { throw new NullPointerException("randomDevice"); }
+ if (entropyFile == null) { throw new NullPointerException("entropyFile"); }
+
+ this.randomDevice = randomDevice;
+ this.entropyFile = entropyFile;
loadInitialEntropy();
addDeviceSpecificEntropy();
writeEntropy();
@@ -85,7 +96,7 @@ public class EntropyService extends Binder {
private void loadInitialEntropy() {
try {
- RandomBlock.fromFile(ENTROPY_FILENAME).toFile(RANDOM_DEV);
+ RandomBlock.fromFile(entropyFile).toFile(randomDevice);
} catch (IOException e) {
Slog.w(TAG, "unable to load initial entropy (first boot?)", e);
}
@@ -93,7 +104,7 @@ public class EntropyService extends Binder {
private void writeEntropy() {
try {
- RandomBlock.fromFile(RANDOM_DEV).toFile(ENTROPY_FILENAME);
+ RandomBlock.fromFile(randomDevice).toFile(entropyFile);
} catch (IOException e) {
Slog.w(TAG, "unable to write entropy", e);
}
@@ -116,7 +127,7 @@ public class EntropyService extends Binder {
private void addDeviceSpecificEntropy() {
PrintWriter out = null;
try {
- out = new PrintWriter(new FileOutputStream(RANDOM_DEV));
+ out = new PrintWriter(new FileOutputStream(randomDevice));
out.println("Copyright (C) 2009 The Android Open Source Project");
out.println("All Your Randomness Are Belong To Us");
out.println(START_TIME);
diff --git a/services/tests/servicestests/src/com/android/server/EntropyServiceTest.java b/services/tests/servicestests/src/com/android/server/EntropyServiceTest.java
new file mode 100644
index 0000000..636ba21
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/EntropyServiceTest.java
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server;
+
+import android.content.Context;
+import android.os.FileUtils;
+import android.test.AndroidTestCase;
+
+import java.io.File;
+
+/**
+ * Tests for {@link com.android.server.EntropyService}
+ */
+public class EntropyServiceTest extends AndroidTestCase {
+
+ public void testInitialWrite() throws Exception {
+ File dir = getContext().getDir("testInitialWrite", Context.MODE_PRIVATE);
+ File file = File.createTempFile("testInitialWrite", "dat", dir);
+ file.deleteOnExit();
+ assertEquals(0, FileUtils.readTextFile(file, 0, null).length());
+
+ // The constructor has the side effect of writing to file
+ new EntropyService("/dev/null", file.getCanonicalPath());
+
+ assertTrue(FileUtils.readTextFile(file, 0, null).length() > 0);
+ }
+}