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