1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  package org.apache.james.jdkim.tagvalue;
21  
22  import java.util.Set;
23  
24  import junit.framework.TestCase;
25  
26  public class TagValueTest extends TestCase {
27  
28      public void testEmpty() {
29          new TagValue("");
30      }
31  
32      public void testValid() {
33          new TagValue("v=DKIM1; p=ciao; s=cips;");
34          new TagValue("v=");
35          new TagValue("v=;");
36          assertTrue(tagValuesEquals("v=", "v=;"));
37          assertTrue(tagValuesEquals("v=", "v= ;"));
38          assertTrue(tagValuesEquals("v=", "v=\r\n ;"));
39          assertFalse(tagValuesEquals("", "v=;"));
40      }
41  
42      public void testInvalidSyntax() {
43          try {
44              new TagValue("_p=ciao; s=cips; v=DKIM1;");
45              fail("expected invalid tag exception");
46          } catch (IllegalStateException e) {
47          }
48      }
49  
50      public void testDoubleTag() {
51          try {
52              new TagValue("s=ciao; s=cips; v=DKIM1;");
53              fail("expected duplicate tag exception");
54          } catch (IllegalStateException e) {
55          }
56      }
57  
58      public void testInvalidFWS() {
59          try {
60              new TagValue("\r\n");
61              fail("we only expect WSP/FWS withing a tag-value. No FWS/WSP allowed with no tag");
62          } catch (IllegalStateException e) {
63          }
64      }
65  
66      public void testInvalidFWSSyntax() {
67          try {
68              new TagValue("p=test \r\n\r\n ");
69              fail("expecting WSP after CRLF to handle it as FWS");
70          } catch (IllegalStateException e) {
71          }
72          try {
73              new TagValue("p=\r\n\r\n test");
74              fail("expecting WSP after CRLF to handle it as FWS");
75          } catch (IllegalStateException e) {
76          }
77      }
78  
79      public void testInvalidFWSStartSyntax() {
80          try {
81              new TagValue("\r\np=ciao; s=cips; v=DKIM1;");
82              fail("\\r\\n at the beginning is not valid FWS");
83          } catch (IllegalStateException e) {
84          }
85          try {
86              new TagValue("\t\r\np=ciao; s=cips; v=DKIM1;");
87              fail("\\t\\r\\n at the beginning is not valid FWS");
88          } catch (IllegalStateException e) {
89          }
90      }
91  
92      public void testInvalidFWSEndSyntax() {
93          try {
94              new TagValue("p\r\n=ciao; s=cips; v=DKIM1;");
95              fail("\\r\\n at the end is not valid FWS");
96          } catch (IllegalStateException e) {
97          }
98          try {
99              new TagValue("p \r\n=ciao; s=cips; v=DKIM1;");
100             fail("\\r\\n at the end is not valid FWS");
101         } catch (IllegalStateException e) {
102         }
103     }
104 
105     public void testValidFWSTags() {
106         assertTrue(tagValuesEquals("\r\n\tp=ciao; s=cips; v=DKIM1;",
107                 "p=ciao;s=cips;v=DKIM1;"));
108         assertTrue(tagValuesEquals("p\r\n =ciao; s=cips; v=DKIM1;",
109                 "p=ciao;s=cips;v=DKIM1;"));
110         assertTrue(tagValuesEquals("p\r\n = \r\n\tciao; s=cips; v=DKIM1;",
111                 "p=ciao;s=cips;v=DKIM1;"));
112         assertTrue(tagValuesEquals("p\r\n = ciao; s=cips\r\n\t; v=DKIM1;",
113                 "p=ciao;s=cips;v=DKIM1;"));
114     }
115 
116     public void testNoTermination() {
117         TagValue t = new TagValue("\r\n\tp=ciao; s=cips; v=DKIM1\r\n\t");
118         assertEquals("DKIM1", t.getValue("v"));
119     }
120 
121     // spaces around the value have to be stripped
122     public void testSingleValue() {
123         TagValue t = new TagValue("\r\n\tp  =      hi\t");
124         assertEquals("hi", t.getValue("p"));
125     }
126 
127     // spaces withing the value needs to be retained.
128     public void testWSPinValue() {
129         TagValue t = new TagValue("\r\n\tp  = \r\n hi \thi hi \t hi\t");
130         assertEquals("hi \thi hi \t hi", t.getValue("p"));
131     }
132 
133     // FWS withing the value needs to be retained.
134     public void testFWSinValue() {
135         TagValue t = new TagValue("\r\n\tp  = \r\n hi \thi\r\n hi \t hi\t");
136         assertEquals("hi \thi\r\n hi \t hi", t.getValue("p"));
137     }
138 
139     public void testNoEqual() {
140         try {
141             new TagValue("\r\n\tp        hi\t");
142             fail("Expected value");
143         } catch (IllegalStateException e) {
144         }
145         try {
146             new TagValue("v=DKIM1; pciao; s=cips;");
147             fail("Expected value");
148         } catch (IllegalStateException e) {
149         }
150     }
151 
152     /**
153      * TODO currently checking with the expert group to see if this is correct
154      */
155     public void testEndingWSP() {
156         new TagValue("t=value; ");
157     }
158 
159     public void testTagSetWithEquals() {
160         TagValue tv = new TagValue("t=value; v=encoded=40value");
161         Set tags = tv.getTags();
162         assertEquals(2, tags.size());
163         assertTrue(tags.contains("t"));
164         assertTrue(tags.contains("v"));
165     }
166 
167     public boolean tagValuesEquals(String t1, String t2) {
168         TagValue tv1 = new TagValue(t1);
169         TagValue tv2 = new TagValue(t2);
170         boolean eq = tv1.equals(tv2);
171         if (eq)
172             assertTrue(tv1.hashCode() == tv2.hashCode());
173         return eq;
174     }
175 
176 }