summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/btr/proxy/selector/pac/PacScriptMethodsTest.java
blob: 0d69a1ef3b32aa17720f2f98a9d8723505d088cb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.btr.proxy.selector.pac;

import static org.junit.Assert.assertEquals;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;

import org.junit.Test;

import com.btr.proxy.TestUtil;

/*****************************************************************************
 * Tests for the global PAC script methods that are used as context inside of 
 * the scripts. 
 * @author Bernd Rosstauscher (proxyvole@rosstauscher.de) Copyright 2009
 ****************************************************************************/

public class PacScriptMethodsTest {
	
	/*************************************************************************
	 * Get a methods implementation with a calendar for date and time base tests
	 * set to a hardcoded data.
	 * Current date for all tests  is: 15. December 1994 12:00.00 
	 * its a Thursday 
	 ************************************************************************/
	
	private PacScriptMethods buildParser() {
		PacScriptMethods result = new PacScriptMethods();

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, 1994);
		cal.set(Calendar.MONTH, Calendar.DECEMBER);
		cal.set(Calendar.DAY_OF_MONTH, 15);
		cal.set(Calendar.HOUR_OF_DAY, 12);
		cal.set(Calendar.MINUTE, 00);
		cal.set(Calendar.SECOND, 00);
		cal.set(Calendar.MILLISECOND, 00);
		result.setCurrentTime(cal);
		
		return result;
	}

	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testDnsDomainIs() {
		assertEquals(true, buildParser().dnsDomainIs("host1.unit-test.invalid", "unit-test.invalid"));
	}
	
	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testDnsDomainLevels() {
		assertEquals(2, buildParser().dnsDomainLevels(TestUtil.HTTP_TEST_URI.toString()));
	}
	
	/*************************************************************************
	 * Test method
	 * @throws UnknownHostException on resolve error.
	 ************************************************************************/
	@Test
	public void testDnsResolve() throws UnknownHostException {
		InetAddress adr = Inet4Address.getLocalHost();
		assertEquals(adr.getHostAddress(), buildParser().dnsResolve(adr.getHostName()));
	}
	
	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testIsInNet() {
		assertEquals(true, buildParser().isInNet("192.168.0.122", "192.168.0.0", "255.255.255.0"));
	}
	
	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testIsInNet2() {
		assertEquals(true, buildParser().isInNet("10.13.75.47", "10.13.72.0", "255.255.252.0"));
	}
	
	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testIsPlainHostName() {
		assertEquals(false, buildParser().isPlainHostName("host1.unit-test.invalid"));
		assertEquals(true, buildParser().isPlainHostName("host1"));
	}
	
	/*************************************************************************
	 * Test method
	 * @throws UnknownHostException on resolve error.
	 ************************************************************************/
	@Test
	public void testIsResolveable() throws UnknownHostException {
		InetAddress adr = Inet4Address.getLocalHost();
		assertEquals(true, buildParser().isResolvable(adr.getHostName()));
	}

	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testLocalHostOrDomainIs() {
		assertEquals(true, buildParser().localHostOrDomainIs("host1.unit-test.invalid", "host1.unit-test.invalid"));
	}

	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testShExpMatch() {
		assertEquals(true, buildParser().shExpMatch("host1.unit-test.invalid", "host1.unit-test.*"));
		assertEquals(true, buildParser().shExpMatch("host1.unit-test.invalid", "*.unit-test.invalid"));
	}	
	
	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testWeekdayRange() {
		assertEquals(true, buildParser().weekdayRange("MON", "SUN", "GMT"));
		assertEquals(true, buildParser().weekdayRange("SUN", "SAT", null));
		assertEquals(false, buildParser().weekdayRange("MON", "WED", null));
	}	

	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testDateRange() {
		assertEquals(true, buildParser().dateRange(15, "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().dateRange(15, "DEC", "undefined", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().dateRange(15, "DEC", 1994, "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().dateRange(15, 17, "undefined", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().dateRange("OCT", "JAN", "undefined", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().dateRange(1994, 1994, "undefined", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().dateRange(1, "DEC", 1994, 1, "JAN", 1995, "GTM"));
		
		assertEquals(false, buildParser().dateRange(16, "DEC", 1994, 1, "JAN", 1995, "GTM"));
	}	

	/*************************************************************************
	 * Test method
	 ************************************************************************/
	@Test
	public void testTimeRange() {
		assertEquals(true, buildParser().timeRange(12, "undefined", "undefined", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().timeRange(11, 13, "undefined", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().timeRange(11, 13, "gmt", "undefined", "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().timeRange(11, 30, 13, 30, "undefined", "undefined", "undefined"));
		assertEquals(true, buildParser().timeRange(11, 30, 15, 13, 30, 15, "undefined"));
		assertEquals(true, buildParser().timeRange(11, 30, 15, 13, 30, 15, "GMT"));

		assertEquals(false, buildParser().timeRange(12, 50, 00, 9, 30, 00, "GMT"));
	}	
		
}