34997fe6e920a299e80883834bb768f56f6e1977
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / sso / test / JU_AAFSSO.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.sso.test;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.hamcrest.CoreMatchers.nullValue;
26 import static org.junit.Assert.assertThat;
27
28 import java.io.ByteArrayInputStream;
29 import java.io.File;
30 import java.io.IOException;
31
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.aaf.cadi.CadiException;
36 import org.onap.aaf.cadi.PropAccess;
37 import org.onap.aaf.cadi.config.Config;
38 import org.onap.aaf.cadi.sso.AAFSSO;
39
40 public class JU_AAFSSO {
41
42         private static final String resourceDirString = "src/test/resources";
43         private static final String aafDir = resourceDirString + "/aaf";
44
45         private ByteArrayInputStream inStream;
46
47         @Before
48         public void setup() {
49                 System.setProperty("user.home", aafDir);
50
51                 // Simulate user input
52                 inStream = new ByteArrayInputStream("test\npassword".getBytes());
53                 System.setIn(inStream);
54         }
55
56         @After
57         public void tearDown() {
58                 recursiveDelete(new File(aafDir));
59         }
60
61         @Test
62         public void test() throws IOException, CadiException {
63                 AAFSSO sso;
64                 String[] args;
65
66                 args = new String[] {
67                                 "-login",
68                                 "-noexit",
69                 };
70                 sso = new AAFSSO(args);
71                 
72                 assertThat(new File(aafDir).exists(), is(true));
73                 assertThat(new File(aafDir + "/.aaf").exists(), is(true));
74                 assertThat(new File(aafDir + "/.aaf/keyfile").exists(), is(true));
75                 assertThat(new File(aafDir + "/.aaf/sso.out").exists(), is(true));
76                 assertThat(sso.loginOnly(), is(true));
77                 
78                 assertThat(new File(aafDir + "/.aaf/sso.props").exists(), is(false));
79                 sso.writeFiles();
80                 assertThat(new File(aafDir + "/.aaf/sso.props").exists(), is(true));
81                 
82                 sso.setLogDefault();
83                 sso.setStdErrDefault();
84
85                 inStream.reset();
86                 args = new String[] {
87                                 "-logout",
88                                 "\\*",
89                                 "-noexit",
90                 };
91                 sso = new AAFSSO(args);
92
93                 assertThat(new File(aafDir).exists(), is(true));
94                 assertThat(new File(aafDir + "/.aaf").exists(), is(true));
95                 assertThat(new File(aafDir + "/.aaf/keyfile").exists(), is(true));
96                 assertThat(new File(aafDir + "/.aaf/sso.out").exists(), is(true));
97                 assertThat(sso.loginOnly(), is(false));
98
99                 PropAccess access = sso.access();
100                 assertThat(sso.enc_pass(), is(access.getProperty(Config.AAF_APPPASS)));
101                 assertThat(sso.user(), is(access.getProperty(Config.AAF_APPID)));
102
103                 sso.addProp("key", "value");
104                 assertThat(sso.err(), is(nullValue()));
105                 
106                 assertThat(sso.useX509(), is(false));
107
108                 sso.close();
109                 sso.close();
110         }
111
112         private void recursiveDelete(File file) {
113                 for (File f : file.listFiles()) {
114                         if (f.isDirectory()) {
115                                 recursiveDelete(f);
116                         }
117                         f.delete();
118                 }
119                 file.delete();
120         }
121
122 }