View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  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,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.client5.http.ssl;
29  
30  import static org.hamcrest.MatcherAssert.assertThat;
31  
32  import java.util.Arrays;
33  
34  import org.apache.hc.core5.http.NameValuePair;
35  import org.apache.hc.core5.http.message.BasicNameValuePair;
36  import org.hamcrest.CoreMatchers;
37  import org.junit.jupiter.api.BeforeEach;
38  import org.junit.jupiter.api.Test;
39  
40  /**
41   * Unit tests for {@link DistinguishedNameParser}.
42   */
43  public class TestDistinguishedNameParser {
44  
45      private DistinguishedNameParser impl;
46  
47      @BeforeEach
48      public void setup() {
49          impl = new DistinguishedNameParser();
50      }
51  
52      @Test
53      public void testParseBasic() throws Exception {
54          assertThat(impl.parse("cn=blah, ou=yada, o=booh"),
55                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
56                          new BasicNameValuePair("cn", "blah"),
57                          new BasicNameValuePair("ou", "yada"),
58                          new BasicNameValuePair("o", "booh"))));
59      }
60  
61      @Test
62      public void testParseRepeatedElements() throws Exception {
63          assertThat(impl.parse("cn=blah, cn=yada, cn=booh"),
64                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
65                          new BasicNameValuePair("cn", "blah"),
66                          new BasicNameValuePair("cn", "yada"),
67                          new BasicNameValuePair("cn", "booh"))));
68      }
69  
70      @Test
71      public void testParseBlanks() throws Exception {
72          assertThat(impl.parse("c = pampa ,  cn  =    blah    , ou = blah , o = blah"),
73                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
74                          new BasicNameValuePair("c", "pampa"),
75                          new BasicNameValuePair("cn", "blah"),
76                          new BasicNameValuePair("ou", "blah"),
77                          new BasicNameValuePair("o", "blah"))));
78      }
79  
80      @Test
81      public void testParseQuotes() throws Exception {
82          assertThat(impl.parse("cn=\"blah\", ou=yada, o=booh"),
83                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
84                          new BasicNameValuePair("cn", "blah"),
85                          new BasicNameValuePair("ou", "yada"),
86                          new BasicNameValuePair("o", "booh"))));
87      }
88  
89      @Test
90      public void testParseQuotes2() throws Exception {
91          assertThat(impl.parse("cn=\"blah  blah\", ou=yada, o=booh"),
92                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
93                          new BasicNameValuePair("cn", "blah  blah"),
94                          new BasicNameValuePair("ou", "yada"),
95                          new BasicNameValuePair("o", "booh"))));
96      }
97  
98      @Test
99      public void testParseQuotes3() throws Exception {
100         assertThat(impl.parse("cn=\"blah, blah\", ou=yada, o=booh"),
101                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
102                         new BasicNameValuePair("cn", "blah, blah"),
103                         new BasicNameValuePair("ou", "yada"),
104                         new BasicNameValuePair("o", "booh"))));
105     }
106 
107     @Test
108     public void testParseEscape() throws Exception {
109         assertThat(impl.parse("cn=blah\\, blah, ou=yada, o=booh"),
110                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
111                         new BasicNameValuePair("cn", "blah, blah"),
112                         new BasicNameValuePair("ou", "yada"),
113                         new BasicNameValuePair("o", "booh"))));
114     }
115 
116     @Test
117     public void testParseUnescapedEqual() throws Exception {
118         assertThat(impl.parse("c = cn=uuh, cn=blah, ou=yada, o=booh"),
119                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
120                         new BasicNameValuePair("c", "cn=uuh"),
121                         new BasicNameValuePair("cn", "blah"),
122                         new BasicNameValuePair("ou", "yada"),
123                         new BasicNameValuePair("o", "booh"))));
124     }
125 
126     @Test
127     public void testParseInvalid() throws Exception {
128         assertThat(impl.parse("blah,blah"),
129                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
130                         new BasicNameValuePair("blah", null),
131                         new BasicNameValuePair("blah", null))));
132     }
133 
134     @Test
135     public void testParseInvalid2() throws Exception {
136         assertThat(impl.parse("cn,o=blah"),
137                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
138                         new BasicNameValuePair("cn", null),
139                         new BasicNameValuePair("o", "blah"))));
140     }
141 
142 }