1// +build windows
2
3package ieproxy
4
5import (
6	"net/http"
7	"reflect"
8	"testing"
9)
10
11var emptyMap, catchAllMap, multipleMap, multipleMapWithCatchAll map[string]string
12
13func init() {
14	emptyMap = make(map[string]string)
15	catchAllMap = make(map[string]string)
16	catchAllMap[""] = "127.0.0.1"
17	multipleMap = make(map[string]string)
18	multipleMap["http"] = "127.0.0.1"
19	multipleMap["ftp"] = "128"
20	multipleMapWithCatchAll = make(map[string]string)
21	multipleMapWithCatchAll["http"] = "127.0.0.1"
22	multipleMapWithCatchAll["ftp"] = "128"
23	multipleMapWithCatchAll[""] = "129"
24}
25
26func TestParseRegedit(t *testing.T) {
27
28	parsingSet := []struct {
29		in  regeditValues
30		out ProxyConf
31	}{
32		{
33			in: regeditValues{},
34			out: ProxyConf{
35				Static: StaticProxyConf{
36					Protocols: emptyMap, // to prevent it being <nil>
37				},
38			},
39		},
40		{
41			in: regeditValues{
42				ProxyServer: "127.0.0.1",
43			},
44			out: ProxyConf{
45				Static: StaticProxyConf{
46					Protocols: catchAllMap,
47				},
48			},
49		},
50		{
51			in: regeditValues{
52				ProxyServer: "http=127.0.0.1;ftp=128",
53			},
54			out: ProxyConf{
55				Static: StaticProxyConf{
56					Protocols: multipleMap,
57				},
58			},
59		},
60		{
61			in: regeditValues{
62				ProxyServer: "http=127.0.0.1;ftp=128;129",
63			},
64			out: ProxyConf{
65				Static: StaticProxyConf{
66					Protocols: multipleMapWithCatchAll,
67				},
68			},
69		},
70		{
71			in: regeditValues{
72				ProxyOverride: "example.com;microsoft.com",
73			},
74			out: ProxyConf{
75				Static: StaticProxyConf{
76					Protocols: emptyMap,
77					NoProxy:   "example.com,microsoft.com",
78				},
79			},
80		},
81		{
82			in: regeditValues{
83				ProxyEnable: 1,
84			},
85			out: ProxyConf{
86				Static: StaticProxyConf{
87					Active:    true,
88					Protocols: emptyMap,
89				},
90			},
91		},
92		{
93			in: regeditValues{
94				AutoConfigURL: "localhost/proxy.pac",
95			},
96			out: ProxyConf{
97				Static: StaticProxyConf{
98					Protocols: emptyMap,
99				},
100				Automatic: ProxyScriptConf{
101					Active:           true,
102					PreConfiguredURL: "localhost/proxy.pac",
103				},
104			},
105		},
106	}
107
108	for _, p := range parsingSet {
109		out := parseRegedit(p.in)
110		if !reflect.DeepEqual(p.out, out) {
111			t.Error("Got: ", out, "Expected: ", p.out)
112		}
113	}
114}
115
116func TestOverrideEnv(t *testing.T) {
117	var callStack []string
118	pseudoSetEnv := func(key, value string) error {
119		if value != "" {
120			callStack = append(callStack, key)
121			callStack = append(callStack, value)
122		}
123		return nil
124	}
125	overrideSet := []struct {
126		in        ProxyConf
127		callStack []string
128	}{
129		{
130			callStack: []string{},
131		},
132		{
133			in: ProxyConf{
134				Static: StaticProxyConf{
135					Active:    true,
136					Protocols: catchAllMap,
137				},
138			},
139			callStack: []string{"http_proxy", "127.0.0.1", "https_proxy", "127.0.0.1"},
140		},
141		{
142			in: ProxyConf{
143				Static: StaticProxyConf{
144					Active:    false,
145					NoProxy:   "example.com,microsoft.com",
146					Protocols: catchAllMap,
147				},
148			},
149			callStack: []string{},
150		},
151		{
152			in: ProxyConf{
153				Static: StaticProxyConf{
154					Active:    true,
155					Protocols: multipleMap,
156				},
157			},
158			callStack: []string{"http_proxy", "127.0.0.1"},
159		},
160		{
161			in: ProxyConf{
162				Static: StaticProxyConf{
163					Active:    true,
164					Protocols: multipleMapWithCatchAll,
165				},
166			},
167			callStack: []string{"http_proxy", "127.0.0.1", "https_proxy", "129"},
168		},
169		{
170			in: ProxyConf{
171				Static: StaticProxyConf{
172					Active:  true,
173					NoProxy: "example.com,microsoft.com",
174				},
175			},
176			callStack: []string{"no_proxy", "example.com,microsoft.com"},
177		},
178	}
179	for _, o := range overrideSet {
180		callStack = []string{}
181		overrideEnvWithStaticProxy(o.in, pseudoSetEnv)
182		if !reflect.DeepEqual(o.callStack, callStack) {
183			t.Error("Got: ", callStack, "Expected: ", o.callStack)
184		}
185	}
186}
187
188func TestPacfile(t *testing.T) {
189	listener, err := listenAndServeWithClose("127.0.0.1:0", http.FileServer(http.Dir("pacfile_examples")))
190	serverBase := "http://" + listener.Addr().String() + "/"
191	if err != nil {
192		t.Fatal(err)
193	}
194
195	// test inactive proxy
196	proxy := ProxyScriptConf{
197		Active:           false,
198		PreConfiguredURL: serverBase + "simple.pac",
199	}
200	out := proxy.FindProxyForURL("http://google.com")
201	if out != "" {
202		t.Error("Got: ", out, "Expected: ", "")
203	}
204	proxy.Active = true
205
206	pacSet := []struct {
207		pacfile  string
208		url      string
209		expected string
210	}{
211		{
212			"direct.pac",
213			"http://google.com",
214			"",
215		},
216		{
217			"404.pac",
218			"http://google.com",
219			"",
220		},
221		{
222			"simple.pac",
223			"http://google.com",
224			"127.0.0.1:8",
225		},
226		{
227			"multiple.pac",
228			"http://google.com",
229			"127.0.0.1:8081",
230		},
231		{
232			"except.pac",
233			"http://imgur.com",
234			"localhost:9999",
235		},
236		{
237			"except.pac",
238			"http://example.com",
239			"",
240		},
241	}
242	for _, p := range pacSet {
243		proxy.PreConfiguredURL = serverBase + p.pacfile
244		out := proxy.FindProxyForURL(p.url)
245		if out != p.expected {
246			t.Error("Got: ", out, "Expected: ", p.expected)
247		}
248	}
249	listener.Close()
250}
251