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 java.util.Arrays;
31  
32  import org.apache.hc.core5.http.NameValuePair;
33  import org.apache.hc.core5.http.message.BasicNameValuePair;
34  import org.hamcrest.CoreMatchers;
35  import org.junit.Assert;
36  import org.junit.Before;
37  import org.junit.Test;
38  
39  /**
40   * Unit tests for {@link DistinguishedNameParser}.
41   */
42  public class TestDistinguishedNameParser {
43  
44      private DistinguishedNameParser impl;
45  
46      @Before
47      public void setup() {
48          impl = new DistinguishedNameParser();
49      }
50  
51      @Test
52      public void testParseBasic() throws Exception {
53          Assert.assertThat(impl.parse("cn=blah, ou=yada, o=booh"),
54                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
55                          new BasicNameValuePair("cn", "blah"),
56                          new BasicNameValuePair("ou", "yada"),
57                          new BasicNameValuePair("o", "booh"))));
58      }
59  
60      @Test
61      public void testParseRepeatedElements() throws Exception {
62          Assert.assertThat(impl.parse("cn=blah, cn=yada, cn=booh"),
63                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
64                          new BasicNameValuePair("cn", "blah"),
65                          new BasicNameValuePair("cn", "yada"),
66                          new BasicNameValuePair("cn", "booh"))));
67      }
68  
69      @Test
70      public void testParseBlanks() throws Exception {
71          Assert.assertThat(impl.parse("c = pampa ,  cn  =    blah    , ou = blah , o = blah"),
72                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
73                          new BasicNameValuePair("c", "pampa"),
74                          new BasicNameValuePair("cn", "blah"),
75                          new BasicNameValuePair("ou", "blah"),
76                          new BasicNameValuePair("o", "blah"))));
77      }
78  
79      @Test
80      public void testParseQuotes() throws Exception {
81          Assert.assertThat(impl.parse("cn=\"blah\", ou=yada, o=booh"),
82                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
83                          new BasicNameValuePair("cn", "blah"),
84                          new BasicNameValuePair("ou", "yada"),
85                          new BasicNameValuePair("o", "booh"))));
86      }
87  
88      @Test
89      public void testParseQuotes2() throws Exception {
90          Assert.assertThat(impl.parse("cn=\"blah  blah\", ou=yada, o=booh"),
91                  CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
92                          new BasicNameValuePair("cn", "blah  blah"),
93                          new BasicNameValuePair("ou", "yada"),
94                          new BasicNameValuePair("o", "booh"))));
95      }
96  
97      @Test
98      public void testParseQuotes3() throws Exception {
99          Assert.assertThat(impl.parse("cn=\"blah, blah\", ou=yada, o=booh"),
100                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
101                         new BasicNameValuePair("cn", "blah, blah"),
102                         new BasicNameValuePair("ou", "yada"),
103                         new BasicNameValuePair("o", "booh"))));
104     }
105 
106     @Test
107     public void testParseEscape() throws Exception {
108         Assert.assertThat(impl.parse("cn=blah\\, blah, ou=yada, o=booh"),
109                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
110                         new BasicNameValuePair("cn", "blah, blah"),
111                         new BasicNameValuePair("ou", "yada"),
112                         new BasicNameValuePair("o", "booh"))));
113     }
114 
115     @Test
116     public void testParseUnescapedEqual() throws Exception {
117         Assert.assertThat(impl.parse("c = cn=uuh, cn=blah, ou=yada, o=booh"),
118                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
119                         new BasicNameValuePair("c", "cn=uuh"),
120                         new BasicNameValuePair("cn", "blah"),
121                         new BasicNameValuePair("ou", "yada"),
122                         new BasicNameValuePair("o", "booh"))));
123     }
124 
125     @Test
126     public void testParseInvalid() throws Exception {
127         Assert.assertThat(impl.parse("blah,blah"),
128                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
129                         new BasicNameValuePair("blah", null),
130                         new BasicNameValuePair("blah", null))));
131     }
132 
133     @Test
134     public void testParseInvalid2() throws Exception {
135         Assert.assertThat(impl.parse("cn,o=blah"),
136                 CoreMatchers.equalTo(Arrays.<NameValuePair>asList(
137                         new BasicNameValuePair("cn", null),
138                         new BasicNameValuePair("o", "blah"))));
139     }
140 
141 }