• 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.javax.security.auth;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import java.security.Principal;
24 import java.util.Set;
25 import javax.security.auth.Subject;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.junit.runners.JUnit4;
30 import sun.security.x509.X500Name;
31 
32 /**
33  * Android does not support {@link java.lang.SecurityManager} and has its implementations stubbed.
34  * See comments in {@link java.lang.SecurityManager} for details.
35  *
36  * This test is added primarily for code coverage.
37  */
38 @RunWith(JUnit4.class)
39 public class SubjectTest {
40 
41     private Set<Principal> set;
42     private Subject subject;
43 
44     @Before
setUp()45     public void setUp() {
46         set = Set.of(new PrincipalImpl());
47         subject = new Subject(true, set, set, set);
48     }
49 
50     @Test
getPrincipals()51     public void getPrincipals() {
52         assertEquals(set, subject.getPrincipals());
53 
54         assertEquals(set, subject.getPrincipals(PrincipalImpl.class));
55         assertEquals(set, subject.getPrincipals(Principal.class));
56         // PrincipalImpl is not a subclass of X500Name.
57         assertEquals(0, subject.getPrincipals(X500Name.class).size());
58     }
59 
60     @Test
getPrivateCredentials()61     public void getPrivateCredentials() {
62         assertEquals(set, subject.getPrivateCredentials());
63 
64         assertEquals(set, subject.getPrivateCredentials(PrincipalImpl.class));
65         assertEquals(set, subject.getPrivateCredentials(Principal.class));
66         // PrincipalImpl is not a subclass of X500Name.
67         assertEquals(0, subject.getPrivateCredentials(X500Name.class).size());
68     }
69 
70     @Test
getPublicCredentials()71     public void getPublicCredentials() {
72         assertEquals(set, subject.getPublicCredentials());
73 
74         assertEquals(set, subject.getPublicCredentials(PrincipalImpl.class));
75         assertEquals(set, subject.getPublicCredentials(Principal.class));
76         // PrincipalImpl is not a subclass of X500Name.
77         assertEquals(0, subject.getPublicCredentials(X500Name.class).size());
78     }
79 
80     @Test
isReadOnly()81     public void isReadOnly() {
82         assertTrue(subject.isReadOnly());
83 
84         subject = new Subject(false, set, set, set);
85         assertFalse(subject.isReadOnly());
86     }
87 
88     @Test
setReadOnly()89     public void setReadOnly() {
90         subject = new Subject(false, set, set, set);
91         assertFalse(subject.isReadOnly());
92 
93         subject.setReadOnly();
94         assertTrue(subject.isReadOnly());
95     }
96 
97     private static final class PrincipalImpl implements Principal {
98 
99         @Override
getName()100         public String getName() {
101             return "";
102         }
103     }
104 }
105