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;
21  
22  import org.apache.james.jdkim.api.SignatureRecord;
23  import org.apache.james.jdkim.tagvalue.SignatureRecordImpl;
24  
25  import junit.framework.TestCase;
26  
27  public class SignatureRecordImplTest extends TestCase {
28  
29      public void testQPDecode() {
30          assertEquals("", SignatureRecordImpl.dkimQuotedPrintableDecode(""));
31          assertEquals("@", SignatureRecordImpl.dkimQuotedPrintableDecode("=40"));
32          assertEquals("\r\n", SignatureRecordImpl
33                  .dkimQuotedPrintableDecode("=0D=0A"));
34          assertEquals("\0CIAO\0", SignatureRecordImpl
35                  .dkimQuotedPrintableDecode("=00CIAO=00"));
36          assertEquals("thisisatest", SignatureRecordImpl
37                  .dkimQuotedPrintableDecode("this\r\n\tis\r\n a\r\n  \t test"));
38      }
39  
40      public void testQPWhiteSpaces() {
41          assertEquals("thisisatest", SignatureRecordImpl
42                  .dkimQuotedPrintableDecode("this is a test"));
43          assertEquals("thisisatest", SignatureRecordImpl
44                  .dkimQuotedPrintableDecode("this\r\n is a test"));
45      }
46  
47      public void testQPInvalid() {
48          try {
49              SignatureRecordImpl.dkimQuotedPrintableDecode("=");
50              fail("invalid sequence parsed.");
51          } catch (IllegalArgumentException e) {
52          }
53          try {
54              SignatureRecordImpl.dkimQuotedPrintableDecode("==");
55              fail("invalid sequence parsed.");
56          } catch (IllegalArgumentException e) {
57          }
58          try {
59              SignatureRecordImpl.dkimQuotedPrintableDecode("=2 3");
60              fail("invalid sequence parsed.");
61          } catch (IllegalArgumentException e) {
62          }
63          try {
64              SignatureRecordImpl.dkimQuotedPrintableDecode("=3");
65              fail("invalid sequence parsed.");
66          } catch (IllegalArgumentException e) {
67          }
68          try {
69              SignatureRecordImpl.dkimQuotedPrintableDecode("=3a");
70              fail("invalid sequence parsed.");
71          } catch (IllegalArgumentException e) {
72          }
73          try {
74              SignatureRecordImpl.dkimQuotedPrintableDecode("==20");
75              fail("invalid sequence parsed.");
76          } catch (IllegalArgumentException e) {
77          }
78          try {
79              SignatureRecordImpl.dkimQuotedPrintableDecode("=20=");
80              fail("invalid sequence parsed.");
81          } catch (IllegalArgumentException e) {
82          }
83          try {
84              SignatureRecordImpl.dkimQuotedPrintableDecode("=3x");
85              fail("invalid sequence parsed.");
86          } catch (IllegalArgumentException e) {
87          }
88          try {
89              SignatureRecordImpl.dkimQuotedPrintableDecode("this\r\nis a test");
90              fail("invalid sequence parsed.");
91          } catch (IllegalArgumentException e) {
92          }
93      }
94  
95      // TODO check bytes > 128
96      /*
97       * public void test8bit() {
98       * assertEquals("PROVA\144CIAO\144",Main.dkimQuotedPrintableDecode("PROVA=90CIAO=90")); }
99       */
100 
101     /*
102      * when we moved from Sun's Base64 to CommonsCodec the decoding changed
103      * behaviour. it does no more fail on bad encoded data. public void
104      * testWrongBase64Encoding() { SignatureRecord sr = new
105      * SignatureRecordImpl("v=1; bh=0012=GG; b==GG;"); try { sr.getBodyHash();
106      * fail("expected failure"); } catch (Exception e) {
107      * assertTrue(e.getMessage().toLowerCase().contains("decod")); } try {
108      * sr.getSignature(); fail("expected failure"); } catch (Exception e) {
109      * assertTrue(e.getMessage().toLowerCase().contains("decod")); } }
110      */
111 
112     public void testWrongHashSyntaxes() {
113         SignatureRecord sr = new SignatureRecordImpl("v=1; a=nothyphenedword;");
114         try {
115             sr.getHashAlgo();
116             fail("expected failure");
117         } catch (Exception e) {
118             assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
119         }
120         try {
121             sr.getHashMethod();
122             fail("expected failure");
123         } catch (Exception e) {
124             assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
125         }
126         try {
127             sr.getHashAlgo();
128             fail("expected failure");
129         } catch (Exception e) {
130             assertTrue(e.getMessage().toLowerCase().indexOf("hash") != -1);
131         }
132     }
133 
134     public void testExpired() {
135         SignatureRecord sr = new SignatureRecordImpl(
136                 "v=1; c=simple; h=from:to; s=select; d=example.com; a=rsa-sha1; x=0; bh=abcdef; b=1235345987;");
137         try {
138             sr.validate();
139             fail("expected failure");
140         } catch (Exception e) {
141             assertTrue(e.getMessage().indexOf("expired") != -1);
142         }
143     }
144 
145 }