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 java.io.BufferedReader;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28
29 import junit.framework.Test;
30 import junit.framework.TestCase;
31 import junit.framework.TestSuite;
32
33 import org.apache.james.jdkim.exceptions.FailException;
34 import org.apache.james.mime4j.parser.MimeTokenStream;
35
36
37
38
39
40 public class PerlDKIMTest extends TestCase {
41
42 private File file;
43 private MockPublicKeyRecordRetriever pkr;
44
45 public PerlDKIMTest(String testName) throws IOException {
46 this(testName, PerlDKIMTestSuite.getFile(testName),
47 getPublicRecordRetriever());
48 }
49
50 public PerlDKIMTest(String name, File testFile,
51 MockPublicKeyRecordRetriever pkr) {
52 super(name);
53 this.file = testFile;
54 this.pkr = pkr;
55 }
56
57 public static MockPublicKeyRecordRetriever getPublicRecordRetriever()
58 throws IOException {
59 MockPublicKeyRecordRetriever pkr = new MockPublicKeyRecordRetriever();
60 BufferedReader fakeDNSlist = new BufferedReader(
61 new InputStreamReader(
62 new FileInputStream(
63 "main\\src\\test\\resources\\org\\apache\\james\\jdkim\\Mail-DKIM\\FAKE_DNS.dat")));
64 String line;
65 while ((line = fakeDNSlist.readLine()) != null) {
66 if (!line.startsWith("#")) {
67 int pDK = line.indexOf("._domainkey.");
68 int pSp = line.indexOf(" ");
69
70 if (line.charAt(pSp + 1) == ' ') {
71 pkr.addRecord(line.substring(0, pDK), line.substring(pDK
72 + "._domainkey.".length(), pSp), line
73 .substring(pSp + 2));
74 } else {
75 if (line.substring(pSp + 1).startsWith("~~")) {
76 pkr.addRecord(line.substring(0, pDK), line.substring(
77 pDK + "._domainkey.".length(), pSp), null);
78 } else {
79
80 }
81 }
82 }
83 }
84 return pkr;
85 }
86
87 protected void runTest() throws Throwable {
88 MimeTokenStream stream = new MimeTokenStream();
89 stream.setRecursionMode(MimeTokenStream.M_FLAT);
90
91
92
93 InputStream is = new FileInputStream(file);
94
95
96
97 pkr = getPublicRecordRetriever();
98
99 boolean expectFailure = false;
100
101 if (getName().indexOf("dk_") != -1)
102 expectFailure = true;
103
104 else if (getName().indexOf("_ietf") != -1)
105 expectFailure = true;
106 else if (getName().startsWith("multiple_1"))
107 expectFailure = true;
108 else if (getName().startsWith("no_body"))
109 expectFailure = true;
110
111 else if (getName().startsWith("badkey_"))
112 expectFailure = true;
113 else if (getName().startsWith("ignore_"))
114 expectFailure = true;
115 else if (getName().startsWith("bad_"))
116 expectFailure = true;
117
118 try {
119 new DKIMVerifier(pkr).verify(is);
120 if (expectFailure)
121 fail("Failure expected!");
122 } catch (FailException e) {
123 if (!expectFailure)
124 fail(e.getMessage());
125 }
126 }
127
128 public static Test suite() throws IOException {
129 return new PerlDKIMTestSuite();
130 }
131
132 static class PerlDKIMTestSuite extends TestSuite {
133
134 private static final File TESTS_FOLDER = new File(
135 "main\\src\\test\\resources\\org\\apache\\james\\jdkim\\Mail-DKIM\\corpus");
136
137 public PerlDKIMTestSuite() throws IOException {
138 super();
139 File dir = TESTS_FOLDER;
140 File[] files = dir.listFiles();
141
142 if (files != null)
143 for (int i = 0; i < files.length; i++) {
144 File f = files[i];
145 if (f.getName().toLowerCase().endsWith(".txt")) {
146 addTest(new PerlDKIMTest(f.getName().substring(0,
147 f.getName().length() - 4), f,
148 getPublicRecordRetriever()));
149 }
150 }
151 }
152
153 public static File getFile(String name) {
154 return new File(TESTS_FOLDER.getAbsolutePath() + File.separator
155 + name + ".txt");
156 }
157
158 }
159 }