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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import os
import unittest
import http_server
from test_util import DenoTestCase, run_tests
from util import root_path, tty_capture
PERMISSIONS_PROMPT_TEST_TS = "tools/complex_permissions_test.ts"
PROMPT_PATTERN = b'⚠️'
PERMISSION_DENIED_PATTERN = b'PermissionDenied: permission denied'
@unittest.skipIf(os.name == 'nt', "Unable to test tty on Windows")
class BaseComplexPermissionTest(DenoTestCase):
def _run_deno(self, flags, args):
"Returns (return_code, stdout, stderr)."
cmd = ([self.deno_exe, "run", "--no-prompt"] + flags +
[PERMISSIONS_PROMPT_TEST_TS] + args)
return tty_capture(cmd, b'')
class BaseReadWritePermissionsTest(object):
test_type = None
def test_inside_project_dir(self):
code, _stdout, stderr = self._run_deno(
["--allow-" + self.test_type + "=" + root_path],
[self.test_type, "package.json", "tests/subdir/config.json"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
def test_outside_test_dir(self):
code, _stdout, stderr = self._run_deno([
"--allow-" + self.test_type + "=" + os.path.join(
root_path, "tests")
], [self.test_type, "package.json"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_inside_test_dir(self):
code, _stdout, stderr = self._run_deno([
"--allow-" + self.test_type + "=" + os.path.join(
root_path, "tests")
], [self.test_type, "tests/subdir/config.json"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
def test_outside_test_and_js_dir(self):
code, _stdout, stderr = self._run_deno([
"--allow-" + self.test_type + "=" + os.path.join(
root_path, "tests") + "," + os.path.join(root_path, "js")
], [self.test_type, "package.json"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_inside_test_and_js_dir(self):
code, _stdout, stderr = self._run_deno([
"--allow-" + self.test_type + "=" + os.path.join(
root_path, "tests") + "," + os.path.join(root_path, "js")
], [self.test_type, "js/dir_test.ts", "tests/subdir/config.json"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
def test_relative(self):
# Save and restore curdir
saved_curdir = os.getcwd()
os.chdir(root_path)
code, _stdout, stderr = self._run_deno(
["--allow-" + self.test_type + "=" + "./tests"],
[self.test_type, "tests/subdir/config.json"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
os.chdir(saved_curdir)
def test_no_prefix(self):
# Save and restore curdir
saved_curdir = os.getcwd()
os.chdir(root_path)
code, _stdout, stderr = self._run_deno(
["--allow-" + self.test_type + "=" + "tests"],
[self.test_type, "tests/subdir/config.json"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
os.chdir(saved_curdir)
class TestReadPermissions(BaseReadWritePermissionsTest,
BaseComplexPermissionTest):
test_type = "read"
class TestWritePermissions(BaseReadWritePermissionsTest,
BaseComplexPermissionTest):
test_type = "write"
class TestNetFetchPermissions(BaseComplexPermissionTest):
test_type = "netFetch"
def test_allow_localhost_4545(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=localhost:4545"],
[self.test_type, "http://localhost:4545"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
def test_allow_deno_land(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=deno.land"],
[self.test_type, "http://localhost:4545"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_4545_fail(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=localhost:4545"],
[self.test_type, "http://localhost:4546"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost(self):
code, _stdout, stderr = self._run_deno(["--allow-net=localhost"], [
self.test_type, "http://localhost:4545", "http://localhost:4546",
"http://localhost:4547"
])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
class TestNetDialPermissions(BaseComplexPermissionTest):
test_type = "netDial"
def test_allow_localhost_ip_4555(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=127.0.0.1:4545"], [self.test_type, "127.0.0.1:4545"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
def test_allow_deno_land(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=deno.land"], [self.test_type, "127.0.0.1:4545"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_ip_4545_fail(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=127.0.0.1:4545"], [self.test_type, "127.0.0.1:4546"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_ip(self):
code, _stdout, stderr = self._run_deno(["--allow-net=127.0.0.1"], [
self.test_type, "127.0.0.1:4545", "127.0.0.1:4546",
"127.0.0.1:4547"
])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
class TestNetListenPermissions(BaseComplexPermissionTest):
test_type = "netListen"
def test_allow_localhost_4555(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=localhost:4555"], [self.test_type, "localhost:4555"])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
def test_allow_deno_land(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=deno.land"], [self.test_type, "localhost:4545"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost_4555_fail(self):
code, _stdout, stderr = self._run_deno(
["--allow-net=localhost:4555"], [self.test_type, "localhost:4556"])
assert code == 1
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN in stderr
def test_allow_localhost(self):
code, _stdout, stderr = self._run_deno(["--allow-net=localhost"], [
self.test_type, "localhost:4555", "localhost:4556",
"localhost:4557"
])
assert code == 0
assert PROMPT_PATTERN not in stderr
assert PERMISSION_DENIED_PATTERN not in stderr
def complex_permissions_tests():
return BaseComplexPermissionTest.__subclasses__()
if __name__ == "__main__":
with http_server.spawn():
run_tests()
|