Enhancements for the aai-common library
[aai/aai-common.git] / aai-aaf-auth / src / test / java / org / onap / aai / aaf / auth / CertUtilTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-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 package org.onap.aai.aaf.auth;
22
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25
26 import javax.servlet.http.HttpServletRequest;
27 import java.io.File;
28 import java.io.FileInputStream;
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.Properties;
32
33 import static org.easymock.EasyMock.*;
34 import static org.junit.Assert.assertTrue;
35
36 /**
37  * The Class CertUtilTest
38  */
39 public class CertUtilTest extends AAISetup {
40
41     @Test
42     public void testCadiCertIssuers() throws IOException {
43         String propFile = System.getProperty("BUNDLECONFIG_DIR") + "/aaf/cadi.properties";
44         Properties cadiProperties = new Properties();
45         cadiProperties.load(new FileInputStream(new File(propFile)));
46
47         List<String> issuersList = CertUtil.getCadiCertIssuers(cadiProperties);
48         assertTrue("issuersList isn't populated", !issuersList.isEmpty());
49
50         int x = issuersList.get(0).indexOf(" ");
51         assertTrue("issuer contains spaces", x < 0);
52     }
53
54     @Test
55     public void testAaiSslClientOuHeader() {
56
57         HttpServletRequest mockRequest = createMock(HttpServletRequest.class);
58         expect(mockRequest.getHeader(CertUtil.AAI_SSL_CLIENT_OU_HDR)).andReturn("m55555@org.onap.com:TEST").times(1, 4);
59         expect(mockRequest.getHeader(CertUtil.AAI_SSL_CLIENT_CN_HDR)).andReturn("CN").times(1, 2);
60         expect(mockRequest.getHeader(CertUtil.AAI_SSL_CLIENT_O_HDR)).andReturn("O").times(1, 2);
61         expect(mockRequest.getHeader(CertUtil.AAI_SSL_CLIENT_L_HDR)).andReturn("L").times(1, 2);
62         expect(mockRequest.getHeader(CertUtil.AAI_SSL_CLIENT_ST_HDR)).andReturn("ST").times(1, 2);
63         expect(mockRequest.getHeader(CertUtil.AAI_SSL_CLIENT_C_HDR)).andReturn("C").times(1, 2);
64
65         replay(mockRequest);
66         String ou = CertUtil.getAaiSslClientOuHeader(mockRequest);
67         assertTrue("OU Header value is not as expected", ou.equals("m55555@org.onap.com:TEST"));
68
69         assertTrue("Unexpected isHaProxy() return value", CertUtil.isHaProxy(mockRequest));
70
71         String mechId = CertUtil.getMechId(mockRequest);
72         assertTrue("mechid value is not as expected", mechId.equals("m55555@org.onap.com"));
73
74     }
75
76     @Test
77     public void testBuildUserChain() {
78
79         // aaf.userchain.pattern=<AAF-ID>:${aaf.userchain.service.reference}:${aaf.userchain.auth.type}:AS
80         String aafUserChainPattern = "<AAF-ID>:org.onap.haproxy:X509:AS";
81         String mechid = "m11111@onap.org";
82         String result = CertUtil.buildUserChainHeader(mechid, aafUserChainPattern);
83
84         assertTrue("user chain value is not as expected", "m11111@onap.org:org.onap.haproxy:X509:AS".equals(result));
85
86     }
87 }