Merge "[OOM CERT-SERVICE-API] Add support for URI, IP, E-mail in SANs"
[oom/platform/cert-service.git] / certServiceClient / src / test / java / org / onap / oom / certservice / client / configuration / validation / UriValidatorTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-client
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. 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.oom.certservice.client.configuration.validation;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import org.junit.jupiter.params.ParameterizedTest;
26 import org.junit.jupiter.params.provider.ValueSource;
27
28 class UriValidatorTest {
29
30     /**
31      * scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
32      */
33
34     @ParameterizedTest
35     @ValueSource(strings = {"http:/", "http:", "http://", "h4ttp://"})
36     void shouldTrueForValidScheme(String uri) {
37         assertThat(UriValidator.isValidUri(uri)).isTrue();
38     }
39
40     @ParameterizedTest
41     @ValueSource(strings = {"example.com", "www.example.com", "0.0.0.0", "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"})
42     void shouldFalseForUriWithoutScheme(String uri) {
43         assertThat(UriValidator.isValidUri(uri)).isFalse();
44     }
45
46     @ParameterizedTest
47     @ValueSource(strings = {"*http://", "_http://", "?http://", "4http://"})
48     void shouldFalseForUriWithInvalidScheme(String uri) {
49         assertThat(UriValidator.isValidUri(uri)).isFalse();
50     }
51
52     /**
53      * authority   = [ userinfo "@" ] host [ ":" port ]
54      * <p>
55      * userinfo    = *( unreserved / pct-encoded / sub-delims / ":" )
56      * <p>
57      * host        = IP-literal / IPv4address / reg-name
58      */
59
60     @ParameterizedTest
61     @ValueSource(strings = {
62         "http://user:password@example.com",
63         "http://user@example.com",
64         "http://user:password:test@example.com",
65         "http://user-info:password@example.com"})
66     void shouldTrueForValidUserInAuthority(String uri) {
67         assertThat(UriValidator.isValidUri(uri)).isTrue();
68     }
69
70     @ParameterizedTest
71     @ValueSource(strings = {
72         "http://user:password",
73         "http://user:password:test:"})
74     void shouldFalseForMissingHostInAuthority(String uri) {
75         assertThat(UriValidator.isValidUri(uri)).isFalse();
76     }
77
78     @ParameterizedTest
79     @ValueSource(strings = {
80         "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]/test",
81         "https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]/",
82         "http://8.8.8.8/",
83         "http://8.8.8.8/test"})
84     void shouldTrueForUriContainsIP(String uri) {
85         assertThat(UriValidator.isValidUri(uri)).isTrue();
86     }
87
88     @ParameterizedTest
89     @ValueSource(strings = {
90         "http://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:443/test",
91         "https://[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443/",
92         "http://8.8.8.8:8080/test",
93         "https://8.8.8.8:443/"})
94     void shouldTrueForUriContainsIPAndPort(String uri) {
95         assertThat(UriValidator.isValidUri(uri)).isTrue();
96     }
97
98     @ParameterizedTest
99     @ValueSource(strings = {
100         "http:/path.to.file",
101         "http:/file",
102         "http:/ptah/to/file"})
103     void shouldTrueForMissingAuthority(String uri) {
104         assertThat(UriValidator.isValidUri(uri)).isTrue();
105     }
106
107     /**
108      * PATH QUERY FRAGMENT
109      */
110
111     @ParameterizedTest
112     @ValueSource(strings = {
113         "http://example.com/path/to/file",
114         "http://example.com/path",
115         "http://example.com/",})
116     void shouldTrueForPathWithAuthority(String uri) {
117         assertThat(UriValidator.isValidUri(uri)).isTrue();
118     }
119
120     @ParameterizedTest
121     @ValueSource(strings = {
122         "http:/path/to/file",
123         "http:/path",
124         "http:/",})
125     void shouldTrueForPathWithoutAuthority(String uri) {
126         assertThat(UriValidator.isValidUri(uri)).isTrue();
127     }
128
129
130     @ParameterizedTest
131     @ValueSource(strings = {
132         "http://example.com/test.txt?test=test1&test2=test3#onap",
133         "http://example.com?",
134         "http://example.com?test=tes1&#",
135         "http://example.com#onap"})
136     void shouldTrueForUriWithQueryAndFragmentInPath(String uri) {
137         assertThat(UriValidator.isValidUri(uri)).isTrue();
138     }
139
140     @ParameterizedTest
141     @ValueSource(strings = {
142         "http://example.com/test.txt?#onap#?",
143         "http://example.com?##",
144         "http://www.example.com/file%GF.html"})
145     void shouldFalseForUriWithWrongQueryOrWrongFragmentInPath(String uri) {
146         assertThat(UriValidator.isValidUri(uri)).isFalse();
147     }
148
149     @ParameterizedTest
150     @ValueSource(strings = {
151         "ftp://ftp.is.co.za/rfc/rfc1808.txt",
152         "http://www.ietf.org/rfc/rfc2396.txt",
153         "ldap://[2001:db8::7]/c=GB?objectClass?one",
154         "mailto:John.Doe@example.com",
155         "news:comp.infosystems.www.servers.unix",
156         "tel:+1-816-555-1212",
157         "telnet://192.0.2.16:80/",
158         "urn:oasis:names:specification:docbook:dtd:xml:4.1.2"})
159     void shouldTrueForRFC3986Examples(String uri) {
160         assertThat(UriValidator.isValidUri(uri)).isTrue();
161     }
162 }