Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 93a68398 authored by Nick Kralevich's avatar Nick Kralevich
Browse files

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
parent a0a59122
Loading
Loading
Loading
Loading
+16 −5
Original line number Diff line number Diff line
@@ -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);
+41 −0
Original line number Diff line number Diff line
/*
 * 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);
    }
}