• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 libcore.java.security;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23 
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import java.security.CodeSigner;
32 import java.security.CodeSource;
33 import java.security.KeyStore;
34 import java.security.Timestamp;
35 import java.security.cert.Certificate;
36 import java.security.cert.X509Certificate;
37 import java.util.Date;
38 import java.util.Enumeration;
39 import java.util.List;
40 
41 import sun.security.provider.certpath.X509CertPath;
42 
43 @RunWith(JUnit4.class)
44 public class CodeSourceTest {
45 
46 
47     private static final String PATH = "file://invalid_cert_path";
48 
49     private CodeSource codeSource;
50 
51     @Before
setUp()52     public void setUp() throws Exception {
53         CodeSigner codeSigner = createCodeSigner();
54         codeSource = new CodeSource(new URL(PATH), new CodeSigner[] { codeSigner });
55     }
56 
createCodeSigner()57     private static CodeSigner createCodeSigner() throws Exception {
58         KeyStore keyStore = KeyStore.getInstance("AndroidCAStore");
59         keyStore.load(null);
60         // Get a X509Certificate from the keyStore
61         X509Certificate cert = null;
62         for (Enumeration<String> aliases = keyStore.aliases(); aliases.hasMoreElements(); ) {
63             String alias = aliases.nextElement();
64             Certificate certificate = keyStore.getCertificate(alias);
65             assertTrue(certificate instanceof X509Certificate);
66             cert = (X509Certificate) certificate;
67             break;
68         }
69 
70         assertNotNull(cert);
71         X509CertPath certPath = new X509CertPath(List.of(cert));
72         return new CodeSigner(certPath, new Timestamp(new Date(), certPath));
73     }
74 
75     @Test
testGetCerificates()76     public void testGetCerificates() {
77         assertNull(codeSource.getCertificates());
78     }
79 
80     @Test
testGetCodeSigners()81     public void testGetCodeSigners() {
82         assertNull(codeSource.getCodeSigners());
83     }
84 
85     @Test
testGetLocation()86     public void testGetLocation() throws MalformedURLException {
87         assertEquals(new URL(PATH), codeSource.getLocation());
88     }
89 
90     @Test
testImplies()91     public void testImplies() {
92         assertTrue(codeSource.implies(null));
93         assertTrue(codeSource.implies(codeSource));
94     }
95 }
96