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 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   * Creates a TestSuite running the test for each .msg file in the test resouce
38   * folder. Allow running of a single test from Unit testing GUIs
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                          // NXDOMAIN can be ignored
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          // String checkFile =
91          // "/org/apache/james/jdkim/bago/default_gfkresearch.com.eml";
92  
93          InputStream is = new FileInputStream(file);
94          // String msgoutFile = file.getAbsolutePath().substring(0,
95          // file.getAbsolutePath().lastIndexOf('.')) + ".out";
96  
97          pkr = getPublicRecordRetriever();
98  
99          boolean expectFailure = false;
100         // DomainKey files
101         if (getName().indexOf("dk_") != -1)
102             expectFailure = true;
103         // older spec version
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         // invalid or inapplicable
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 }