Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/signing/KeyStoreHelperTest.java

raw
1 /*
2 * Copyright (C) 2011 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.ide.common.signing;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23
24 import com.android.annotations.NonNull;
25 import com.android.annotations.Nullable;
26 import com.android.utils.ILogger;
27
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.TemporaryFolder;
31
32 import java.io.File;
33 import java.security.PrivateKey;
34 import java.security.cert.X509Certificate;
35 import java.util.Calendar;
36
37 public class KeyStoreHelperTest {
38
39 @Rule
40 public TemporaryFolder mTemporaryFolder = new TemporaryFolder();
41
42 @Test
43 public void testCreateAndCheckKey() throws Exception {
44 File tempFolder = mTemporaryFolder.newFolder();
45 File keystoreFile = new File(tempFolder, "debug.keystore");
46 keystoreFile.deleteOnExit();
47
48 FakeLogger fakeLogger = new FakeLogger();
49
50 // "now" is just slightly before the key was created
51 long now = System.currentTimeMillis();
52
53 // create the keystore
54 KeystoreHelper.createDebugStore(null, keystoreFile, "android", "android", "AndroidDebugKey",
55 fakeLogger);
56
57 // read the key back
58 CertificateInfo certificateInfo = KeystoreHelper.getCertificateInfo(
59 null, keystoreFile, "android", "android", "AndroidDebugKey");
60
61 assertNotNull(certificateInfo);
62
63 assertEquals("", fakeLogger.getErr());
64
65 PrivateKey key = certificateInfo.getKey();
66 assertNotNull(key);
67
68 X509Certificate certificate = certificateInfo.getCertificate();
69 assertNotNull(certificate);
70
71 // The "not-after" (a.k.a. expiration) date should be after "now"
72 Calendar c = Calendar.getInstance();
73 c.setTimeInMillis(now);
74 assertTrue(certificate.getNotAfter().compareTo(c.getTime()) > 0);
75
76 // It should be valid after 1 year from now (adjust by a second since the 'now' time
77 // doesn't exactly match the creation time... 1 second should be enough.)
78 c.add(Calendar.DAY_OF_YEAR, 365);
79 c.add(Calendar.SECOND, -1);
80 assertTrue("1 year expiration failed",
81 certificate.getNotAfter().compareTo(c.getTime()) > 0);
82
83 // and 30 years from now
84 c.add(Calendar.DAY_OF_YEAR, 29 * 365);
85 // remove 1 hour to handle for PST/PDT issue
86 c.add(Calendar.HOUR_OF_DAY, -1);
87 assertTrue("30 year expiration failed",
88 certificate.getNotAfter().compareTo(c.getTime()) > 0);
89
90 // however expiration date should be passed in 30 years + a few hours
91 c.add(Calendar.HOUR, 5);
92 assertFalse("30 year and few hours expiration failed",
93 certificate.getNotAfter().compareTo(c.getTime()) > 0);
94 }
95
96 private static class FakeLogger implements ILogger {
97 private String mOut = "";
98 private String mErr = "";
99
100 public String getOut() {
101 return mOut;
102 }
103
104 public String getErr() {
105 return mErr;
106 }
107
108 @Override
109 public void error(@Nullable Throwable t, @Nullable String msgFormat, Object... args) {
110 String message = msgFormat != null ?
111 String.format(msgFormat, args) :
112 t != null ? t.getClass().getCanonicalName() : "ERROR!";
113 mErr += message + "\n";
114 }
115
116 @Override
117 public void warning(@NonNull String msgFormat, Object... args) {
118 String message = String.format(msgFormat, args);
119 mOut += message + "\n";
120 }
121
122 @Override
123 public void info(@NonNull String msgFormat, Object... args) {
124 String message = String.format(msgFormat, args);
125 mOut += message + "\n";
126 }
127
128 @Override
129 public void verbose(@NonNull String msgFormat, Object... args) {
130 String message = String.format(msgFormat, args);
131 mOut += message + "\n";
132 }
133 }
134 }