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  
21  package org.apache.james.jspf;
22  
23  import org.apache.james.jspf.core.SPFRecordParser;
24  import org.apache.james.jspf.core.exceptions.NoneException;
25  import org.apache.james.jspf.core.exceptions.PermErrorException;
26  import org.apache.james.jspf.impl.DefaultTermsFactory;
27  import org.apache.james.jspf.parser.RFC4408SPF1Parser;
28  
29  import java.io.BufferedReader;
30  import java.io.IOException;
31  import java.io.InputStreamReader;
32  import java.util.ArrayList;
33  import java.util.Iterator;
34  import java.util.List;
35  import java.util.regex.Pattern;
36  
37  import junit.framework.Test;
38  import junit.framework.TestCase;
39  import junit.framework.TestSuite;
40  
41  public class SPF1ParserTest extends TestCase {
42  
43      public SPF1ParserTest(String name) throws IOException {
44          super(name);
45          List tests = loadTests();
46          Iterator i = tests.iterator();
47          while (i.hasNext()) {
48              SPF1RecordTestDef def = (SPF1RecordTestDef) i.next();
49              if (name.equals(def.recIn)) {
50                  data = def;
51                  break;
52              }
53          }
54          assertNotNull(data);
55          parser = new RFC4408SPF1Parser(new ConsoleLogger(), new DefaultTermsFactory(new ConsoleLogger()));
56      }
57  
58      public static Test suite() throws IOException {
59          return new SPF1RecordTestSuite();
60      }
61  
62      private SPF1RecordTestDef data;
63  
64      private SPFRecordParser parser;
65  
66      public SPF1ParserTest(SPF1RecordTestDef def, SPFRecordParser parser) {
67          super(def.recIn);
68          this.data = def;
69          this.parser = parser;
70      }
71  
72      protected void runTest() throws Throwable {
73  
74          try {
75  
76              System.out.println("testing [" + data.recIn + "]");
77  
78              parser.parse(data.recIn);
79  
80              assertEquals("Expected <" + data.errMsg + "> but was <"
81                      + "no errors" + ">", data.errMsg, "no errors");
82          } catch (NoneException e) {
83              e.printStackTrace();
84              assertNotNull(data.errMsg);
85              assertTrue("Expected <" + data.errMsg + "> but was <"
86                      + e.getMessage() + ">", !"no errors".equals(data.errMsg));
87              // assertEquals("Expected <" + data.errMsg + "> but was <"
88              // + e.getMessage() + ">", data.errMsg, e.getMessage());
89          } catch (PermErrorException e) {
90              e.printStackTrace();
91              assertNotNull(data.errMsg);
92              assertTrue("Expected <" + data.errMsg + "> but was <"
93                      + e.getMessage() + ">\n" + data.recOut + "\n"
94                      + data.recOutAuto, !"no errors".equals(data.errMsg));
95              // assertEquals("Expected <" + data.errMsg + "> but was <"
96              // + e.getMessage() + ">", data.errMsg, e.getMessage());
97          }
98  
99      }
100 
101     public static List loadTests() throws IOException {
102         List tests = new ArrayList();
103 
104         BufferedReader br = new BufferedReader(new InputStreamReader(Thread
105                 .currentThread().getContextClassLoader().getResourceAsStream(
106                         "org/apache/james/jspf/test_parser.txt")));
107 
108         String line;
109 
110         Pattern p = Pattern.compile("[ ]+");
111 
112         SPF1RecordTestDef def = null;
113 
114         while ((line = br.readLine()) != null) {
115             // skip comments and empty lines
116             if (line.length() != 0 && line.charAt(0) != '#') {
117                 String[] tokens = p.split(line, 3);
118 
119                 if (tokens.length >= 2) {
120 
121                     if ("spftest".equals(tokens[0])) {
122                         if (def != null && def.recIn != null) {
123                             tests.add(def);
124                         }
125                         def = new SPF1RecordTestDef();
126                         def.name = tokens[2];
127                     } else if ("/.*/".equals(tokens[1])
128                             || "jspf".equals(tokens[1])) {
129 
130                         if ("rec-in".equals(tokens[0])) {
131                             if (def.recIn == null)
132                                 def.recIn = tokens[2].replaceFirst(
133                                         "SPF record in:  ", "");
134                         } else if ("err-msg".equals(tokens[0])) {
135                             if (def.errMsg == null)
136                                 def.errMsg = tokens[2];
137                         } else if ("rec-out".equals(tokens[0])) {
138                             if (def.recOut == null)
139                                 def.recOut = tokens[2].replaceFirst(
140                                         "SPF record:  ", "");
141                         } else if ("rec-out-auto".equals(tokens[0])) {
142                             if (tokens.length == 3) {
143                                 if (def.recOutAuto == null)
144                                     def.recOutAuto = tokens[2];
145                             } else {
146                                 if (def.recOutAuto == null)
147                                     def.recOutAuto = "";
148                             }
149                         }
150                     }
151 
152                 } else {
153                     throw new IllegalStateException("Bad format: " + line);
154                 }
155             }
156 
157         }
158 
159         if (def != null && def.recIn != null) {
160             tests.add(def);
161         }
162 
163         br.close();
164 
165         return tests;
166     }
167 
168     static class SPF1RecordTestSuite extends TestSuite {
169 
170         public SPF1RecordTestSuite() throws IOException {
171             super();
172             List tests = loadTests();
173             Iterator i = tests.iterator();
174             SPFRecordParser parser = new RFC4408SPF1Parser(new ConsoleLogger(), new DefaultTermsFactory(new ConsoleLogger()));
175             while (i.hasNext()) {
176                 addTest(new SPF1ParserTest((SPF1RecordTestDef) i.next(), parser));
177             }
178         }
179 
180     }
181 
182     public static class SPF1RecordTestDef {
183         public String name = null;
184 
185         public String recIn = null;
186 
187         public String errMsg = null;
188 
189         public String recOutAuto = null;
190 
191         public String recOut = null;
192     }
193 
194 }