1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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 }