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.jspf.core;
21  
22  import org.apache.james.jspf.ConsoleLogger;
23  import org.apache.james.jspf.core.MacroData;
24  import org.apache.james.jspf.core.MacroExpand;
25  import org.apache.james.jspf.core.exceptions.PermErrorException;
26  
27  import junit.framework.TestCase;
28  
29  /**
30   * RFC4408 8.2. Expansion Examples
31   */
32  public class MacroExpandTest extends TestCase {
33  
34      private final class rfcIP6MacroData extends rfcIP4MacroData {
35          public String getInAddress() {
36              return "ip6";
37          }
38  
39          public String getMacroIpAddress() {
40              return "2.0.0.1.0.D.B.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.C.B.0.1";
41          }
42  
43          public String getIpAddress() {
44              return "2001:DB8::CB01";
45          }
46  
47          public String getReadableIP() {
48              return "2001:DB8::CB01";
49          }
50      }
51  
52      private class rfcIP4MacroData implements MacroData {
53          public String getCurrentSenderPart() {
54              return "strong-bad";
55          }
56  
57          public String getMailFrom() {
58              return "strong-bad@email.example.com";
59          }
60  
61          public String getHostName() {
62              return "email.example.com";
63          }
64  
65          public String getCurrentDomain() {
66              return "email.example.com";
67          }
68  
69          public String getInAddress() {
70              return "in-addr";
71          }
72  
73          public String getClientDomain() {
74              return "clientdomain";
75          }
76  
77          public String getSenderDomain() {
78              return "email.example.com";
79          }
80  
81          public String getIpAddress() {
82              return "192.0.2.3";
83          }
84          
85          public String getMacroIpAddress() {
86              return "192.0.2.3";
87          }
88  
89          public long getTimeStamp() {
90              return System.currentTimeMillis();
91          }
92  
93          public String getReadableIP() {
94              return "192.0.2.3";
95          }
96  
97          public String getReceivingDomain() {
98              return "receivingdomain";
99          }
100     }
101 
102     MacroExpand defIp4me = null;
103 
104     MacroExpand defIp6me = null;
105 
106     protected void setUp() throws Exception {
107         super.setUp();
108         defIp4me = new MacroExpand(new ConsoleLogger(), null);
109         defIp6me = new MacroExpand(new ConsoleLogger(), null);
110     }
111 
112     public void testPercS() throws PermErrorException {
113         assertEquals("strong-bad@email.example.com", defIp4me
114                 .expand("%{s}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
115     }
116 
117     public void testPercK() throws PermErrorException {
118         try {
119             defIp4me.expand("%{k}", new rfcIP4MacroData(), MacroExpand.DOMAIN);
120             fail("%{k} is not a valid expansion");
121         } catch (PermErrorException e) {
122         }
123     }
124 
125     public void testPercentAloneIsError() throws PermErrorException {
126         try {
127             defIp4me.expand("%{s}%", new rfcIP4MacroData(), MacroExpand.DOMAIN);
128             fail("invalid percent at end of line");
129         } catch (PermErrorException e) {
130         }
131     }
132 
133     public void testDoublePercent() throws PermErrorException {
134         assertEquals("%", defIp4me.expand("%%", new rfcIP4MacroData(), MacroExpand.DOMAIN));
135     }
136 
137     public void testPercO() throws PermErrorException {
138         assertEquals("email.example.com", defIp4me.expand("%{o}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
139     }
140 
141     public void testPercD() throws PermErrorException {
142         assertEquals("email.example.com", defIp4me.expand("%{d}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
143         assertEquals("email.example.com", defIp4me.expand("%{d4}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
144         assertEquals("email.example.com", defIp4me.expand("%{d3}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
145         assertEquals("example.com", defIp4me.expand("%{d2}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
146         assertEquals("com", defIp4me.expand("%{d1}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
147         assertEquals("com.example.email", defIp4me.expand("%{dr}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
148         assertEquals("example.email", defIp4me.expand("%{d2r}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
149     }
150 
151     public void testPercL() throws PermErrorException {
152         assertEquals("strong-bad", defIp4me.expand("%{l}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
153         assertEquals("strong.bad", defIp4me.expand("%{l-}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
154         assertEquals("strong-bad", defIp4me.expand("%{lr}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
155         assertEquals("bad.strong", defIp4me.expand("%{lr-}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
156         assertEquals("strong", defIp4me.expand("%{l1r-}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
157     }
158 
159     public void testExample1() throws PermErrorException {
160         assertEquals("3.2.0.192.in-addr._spf.example.com", defIp4me
161                 .expand("%{ir}.%{v}._spf.%{d2}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
162     }
163 
164     public void testExample2() throws PermErrorException {
165         assertEquals("bad.strong.lp._spf.example.com", defIp4me
166                 .expand("%{lr-}.lp._spf.%{d2}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
167     }
168 
169     public void testExample3() throws PermErrorException {
170         assertEquals("bad.strong.lp.3.2.0.192.in-addr._spf.example.com",
171                 defIp4me.expand("%{lr-}.lp.%{ir}.%{v}._spf.%{d2}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
172     }
173 
174     public void testExample4() throws PermErrorException {
175         assertEquals("3.2.0.192.in-addr.strong.lp._spf.example.com", defIp4me
176                 .expand("%{ir}.%{v}.%{l1r-}.lp._spf.%{d2}", new rfcIP4MacroData(), MacroExpand.DOMAIN));
177     }
178 
179     public void testExample5() throws PermErrorException {
180         assertEquals("example.com.trusted-domains.example.net", defIp4me
181                 .expand("%{d2}.trusted-domains.example.net", new rfcIP4MacroData(), MacroExpand.DOMAIN));
182     }
183 
184     public void testExample6_ipv6() throws PermErrorException {
185         assertEquals(
186                 "1.0.B.C.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.8.B.D.0.1.0.0.2.ip6._spf.example.com",
187                 defIp6me.expand("%{ir}.%{v}._spf.%{d2}", new rfcIP6MacroData(), MacroExpand.EXPLANATION));
188     }
189     
190     public void testLocalPartWithSpecialChars() throws PermErrorException {
191 
192         assertEquals(
193                 "+exists:CL.192.0.2.3.FR.test{$LNAME}@email.example.com.spf.test.com",
194                 defIp4me.expand("+exists:CL.%{i}.FR.%{s}.spf.test.com",
195                         new rfcIP4MacroData() {
196                             public String getMailFrom() {
197                                 return "test{$LNAME}@email.example.com";
198                             }
199 
200                             public String getCurrentSenderPart() {
201                                 return "test{$LNAME}";
202                             }
203                         }, MacroExpand.DOMAIN));
204 
205         // not sure if \ is allowed in email, but anyway make sure we correctly handle also backslash.
206         assertEquals(
207                 "+exists:CL.192.0.2.3.FR.tes\\t{$LNAME}@email.example.com.spf.test.com",
208                 defIp4me.expand("+exists:CL.%{i}.FR.%{s}.spf.test.com",
209                         new rfcIP4MacroData() {
210                             public String getMailFrom() {
211                                 return "tes\\t{$LNAME}@email.example.com";
212                             }
213 
214                             public String getCurrentSenderPart() {
215                                 return "tes\\t{$LNAME}";
216                             }
217                         }, MacroExpand.DOMAIN));
218     }
219 
220 }