1 /*
2  * Copyright (C) 2010 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.server;
18 
19 import static org.junit.Assert.assertArrayEquals;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertFalse;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.content.Context;
25 
26 import androidx.test.InstrumentationRegistry;
27 import androidx.test.runner.AndroidJUnit4;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.io.File;
34 import java.nio.file.Files;
35 import java.util.Arrays;
36 
37 /**
38  * Tests for {@link com.android.server.EntropyMixer}
39  */
40 @RunWith(AndroidJUnit4.class)
41 public class EntropyMixerTest {
42 
43     private static final int SEED_FILE_SIZE = EntropyMixer.SEED_FILE_SIZE;
44 
45     private Context context;
46     private File seedFile;
47     private File randomReadDevice;
48     private File randomWriteDevice;
49 
50     @Before
setUp()51     public void setUp() throws Exception {
52         context = InstrumentationRegistry.getTargetContext();
53         seedFile = createTempFile("entropy.dat");
54         randomReadDevice = createTempFile("urandomRead");
55         randomWriteDevice = createTempFile("urandomWrite");
56     }
57 
createTempFile(String prefix)58     private File createTempFile(String prefix) throws Exception {
59         File file = File.createTempFile(prefix, null);
60         file.deleteOnExit();
61         return file;
62     }
63 
repeatByte(byte b, int length)64     private byte[] repeatByte(byte b, int length) {
65         byte[] data = new byte[length];
66         Arrays.fill(data, b);
67         return data;
68     }
69 
70     // Test initializing the EntropyMixer when the seed file doesn't exist yet.
71     @Test
testInitFirstBoot()72     public void testInitFirstBoot() throws Exception {
73         seedFile.delete();
74 
75         byte[] urandomInjectedData = repeatByte((byte) 0x01, SEED_FILE_SIZE);
76         Files.write(randomReadDevice.toPath(), urandomInjectedData);
77 
78         // The constructor should have the side effect of writing to
79         // randomWriteDevice and creating seedFile.
80         new EntropyMixer(context, seedFile, randomReadDevice, randomWriteDevice);
81 
82         // Since there was no old seed file, the data that was written to
83         // randomWriteDevice should contain only device-specific information.
84         assertTrue(isDeviceSpecificInfo(Files.readAllBytes(randomWriteDevice.toPath())));
85 
86         // The seed file should have been created.
87         validateSeedFile(seedFile, new byte[0], urandomInjectedData);
88     }
89 
90     // Test initializing the EntropyMixer when the seed file already exists.
91     @Test
testInitNonFirstBoot()92     public void testInitNonFirstBoot() throws Exception {
93         byte[] previousSeed = repeatByte((byte) 0x01, SEED_FILE_SIZE);
94         Files.write(seedFile.toPath(), previousSeed);
95 
96         byte[] urandomInjectedData = repeatByte((byte) 0x02, SEED_FILE_SIZE);
97         Files.write(randomReadDevice.toPath(), urandomInjectedData);
98 
99         // The constructor should have the side effect of writing to
100         // randomWriteDevice and updating seedFile.
101         new EntropyMixer(context, seedFile, randomReadDevice, randomWriteDevice);
102 
103         // The data that was written to randomWriteDevice should consist of the
104         // previous seed followed by the device-specific information.
105         byte[] dataWrittenToUrandom = Files.readAllBytes(randomWriteDevice.toPath());
106         byte[] firstPartWritten = Arrays.copyOf(dataWrittenToUrandom, SEED_FILE_SIZE);
107         byte[] secondPartWritten =
108                 Arrays.copyOfRange(
109                         dataWrittenToUrandom, SEED_FILE_SIZE, dataWrittenToUrandom.length);
110         assertArrayEquals(previousSeed, firstPartWritten);
111         assertTrue(isDeviceSpecificInfo(secondPartWritten));
112 
113         // The seed file should have been updated.
114         validateSeedFile(seedFile, previousSeed, urandomInjectedData);
115     }
116 
isDeviceSpecificInfo(byte[] data)117     private boolean isDeviceSpecificInfo(byte[] data) {
118         return new String(data).startsWith(EntropyMixer.DEVICE_SPECIFIC_INFO_HEADER);
119     }
120 
validateSeedFile(File seedFile, byte[] previousSeed, byte[] urandomInjectedData)121     private void validateSeedFile(File seedFile, byte[] previousSeed, byte[] urandomInjectedData)
122             throws Exception {
123         final int unhashedLen = SEED_FILE_SIZE - 32;
124         byte[] newSeed = Files.readAllBytes(seedFile.toPath());
125         assertEquals(SEED_FILE_SIZE, newSeed.length);
126         assertEquals(SEED_FILE_SIZE, urandomInjectedData.length);
127         assertFalse(Arrays.equals(newSeed, previousSeed));
128         // The new seed should consist of the first SEED_FILE_SIZE - 32 bytes
129         // that were read from urandom, followed by a 32-byte hash that should
130         // *not* be the same as the last 32 bytes that were read from urandom.
131         byte[] firstPart = Arrays.copyOf(newSeed, unhashedLen);
132         byte[] secondPart = Arrays.copyOfRange(newSeed, unhashedLen, SEED_FILE_SIZE);
133         byte[] firstPartInjected = Arrays.copyOf(urandomInjectedData, unhashedLen);
134         byte[] secondPartInjected =
135                 Arrays.copyOfRange(urandomInjectedData, unhashedLen, SEED_FILE_SIZE);
136         assertArrayEquals(firstPart, firstPartInjected);
137         assertFalse(Arrays.equals(secondPart, secondPartInjected));
138     }
139 }
140