File size: 5,762 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
local TEST_TIMEOUT = 2
describe("http.socks module", function()
	local http_socks = require "http.socks"
	local cqueues = require "cqueues"
	local ca = require "cqueues.auxlib"
	local ce = require "cqueues.errno"
	local cs = require "cqueues.socket"
	it("works with connect constructor", function()
		assert(http_socks.connect("socks5://127.0.0.1"))
		assert(http_socks.connect("socks5h://username:password@127.0.0.1"))
	end)
	it("fails on unknown protocols", function()
		assert.has.errors(function()
			http_socks.connect("socks3://host")
		end)
	end)
	it("fails when userinfo is missing password", function()
		assert.has.errors(function()
			http_socks.connect("socks5h://user@host")
		end)
	end)
	it("has a working :clone", function()
		local socks = http_socks.connect("socks5://127.0.0.1")
		assert.same(socks, socks:clone())
	end)
	it("has a working :clone when userinfo present", function()
		local socks = http_socks.connect("socks5://user:pass@127.0.0.1")
		assert.same(socks, socks:clone())
	end)
	it("can negotiate a IPv4 connection with no auth", function()
		local s, c = ca.assert(cs.pair())
		local cq = cqueues.new()
		cq:wrap(function()
			assert(http_socks.fdopen(c):negotiate("127.0.0.1", 123))
		end)
		cq:wrap(function()
			assert.same("\5", s:read(1))
			local n = assert(s:read(1)):byte()
			local available_auth = assert(s:read(n))
			assert.same("\0", available_auth)
			assert(s:xwrite("\5\0", "n"))
			assert.same("\5\1\0\1\127\0\0\1\0\123", s:read(10))
			assert(s:xwrite("\5\0\0\1\127\0\0\1\12\34", "n"))
		end)
		assert_loop(cq, TEST_TIMEOUT)
		assert.truthy(cq:empty())
		s:close()
		c:close()
	end)
	it("can negotiate a IPv6 connection with username+password auth", function()
		local s, c = ca.assert(cs.pair())
		local cq = cqueues.new()
		cq:wrap(function()
			c = http_socks.fdopen(c)
			assert(c:add_username_password_auth("open", "sesame"))
			assert(c:negotiate("::1", 123))
			c:close()
		end)
		cq:wrap(function()
			assert.same("\5", s:read(1))
			local n = assert(s:read(1)):byte()
			local available_auth = assert(s:read(n))
			assert.same("\0\2", available_auth)
			assert(s:xwrite("\5\2", "n"))
			assert.same("\1\4open\6sesame", s:read(13))
			assert(s:xwrite("\1\0", "n"))
			assert.same("\5\1\0\4\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\0\123", s:read(22))
			assert(s:xwrite("\5\0\0\4\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\1\12\34", "n"))
			s:close()
		end)
		assert_loop(cq, TEST_TIMEOUT)
		assert.truthy(cq:empty())
	end)
	it("can negotiate a connection where peername is a domain", function()
		local s, c = ca.assert(cs.pair())
		local cq = cqueues.new()
		cq:wrap(function()
			c = http_socks.fdopen(c)
			assert(c:negotiate("127.0.0.1", 123))
			assert.same(cs.AF_UNSPEC, c.dst_family)
			assert.same("test", c.dst_host)
			assert.same(1234, c.dst_port)
			c:close()
		end)
		cq:wrap(function()
			assert.same("\5", s:read(1))
			local n = assert(s:read(1)):byte()
			local available_auth = assert(s:read(n))
			assert.same("\0", available_auth)
			assert(s:xwrite("\5\0", "n"))
			assert.same("\5\1\0\1\127\0\0\1\0\123", s:read(10))
			assert(s:xwrite("\5\0\0\3\4test\4\210", "n"))
			s:close()
		end)
		assert_loop(cq, TEST_TIMEOUT)
		assert.truthy(cq:empty())
	end)
	it("fails incorrect username+password with EACCES", function()
		local s, c = ca.assert(cs.pair())
		local cq = cqueues.new()
		cq:wrap(function()
			c = http_socks.fdopen(c)
			assert(c:add_username_password_auth("open", "sesame"))
			assert.same(ce.EACCES, select(3, c:negotiate("unused", 123)))
			c:close()
		end)
		cq:wrap(function()
			assert.same("\5", s:read(1))
			local n = assert(s:read(1)):byte()
			local available_auth = assert(s:read(n))
			assert.same("\0\2", available_auth)
			assert(s:xwrite("\5\2", "n"))
			assert.same("\1\4open\6sesame", s:read(13))
			assert(s:xwrite("\1\1", "n"))
			s:close()
		end)
		assert_loop(cq, TEST_TIMEOUT)
		assert.truthy(cq:empty())
	end)
	it("fails with correct error messages", function()
		for i, correct_errno in ipairs({
			false;
			ce.EACCES;
			ce.ENETUNREACH;
			ce.EHOSTUNREACH;
			ce.ECONNREFUSED;
			ce.ETIMEDOUT;
			ce.EOPNOTSUPP;
			ce.EAFNOSUPPORT;
		}) do
			local c, s = ca.assert(cs.pair())
			local cq = cqueues.new()
			cq:wrap(function()
				c = http_socks.fdopen(c)
				local ok, _, errno = c:negotiate("127.0.0.1", 123)
				assert.falsy(ok)
				if correct_errno then
					assert.same(correct_errno, errno)
				end
				c:close()
			end)
			cq:wrap(function()
				assert.same("\5", s:read(1))
				local n = assert(s:read(1)):byte()
				local available_auth = assert(s:read(n))
				assert.same("\0", available_auth)
				assert(s:xwrite("\5\0", "n"))
				assert.same("\5\1\0\1\127\0\0\1\0\123", s:read(10))
				assert(s:xwrite("\5" .. string.char(i), "n"))
				s:close()
			end)
			assert_loop(cq, TEST_TIMEOUT)
			assert.truthy(cq:empty())
		end
	end)
	it("fails with EAFNOSUPPORT on unknown address type", function()
		local s, c = ca.assert(cs.pair())
		local cq = cqueues.new()
		cq:wrap(function()
			c = http_socks.fdopen(c)
			local ok, _, errno = c:negotiate("127.0.0.1", 123)
			assert.falsy(ok)
			assert.same(ce.EAFNOSUPPORT, errno)
			c:close()
		end)
		cq:wrap(function()
			assert.same("\5", s:read(1))
			local n = assert(s:read(1)):byte()
			local available_auth = assert(s:read(n))
			assert.same("\0", available_auth)
			assert(s:xwrite("\5\0", "n"))
			assert.same("\5\1\0\1\127\0\0\1\0\123", s:read(10))
			assert(s:xwrite("\5\0\0\5", "n"))
			s:close()
		end)
		assert_loop(cq, TEST_TIMEOUT)
		assert.truthy(cq:empty())
	end)
	it("has a working :take_socket", function()
		local s, c = ca.assert(cs.pair())
		local socks = http_socks.fdopen(c)
		assert.same(c, socks:take_socket())
		assert.same(nil, socks:take_socket())
		s:close()
		c:close()
	end)
end)