forked from cloudflare/stpyv8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_Context.py
153 lines (112 loc) · 4.71 KB
/
test_Context.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import STPyV8
class TestContext(unittest.TestCase):
def testEval(self):
with STPyV8.JSContext() as context:
self.assertEqual(2, context.eval("1+1"))
self.assertEqual('Hello world', context.eval("'Hello ' + 'world'"))
def testMultiNamespace(self):
self.assertTrue(not bool(STPyV8.JSContext.inContext))
self.assertTrue(not bool(STPyV8.JSContext.entered))
class Global:
name = "global"
g = Global()
with STPyV8.JSContext(g) as ctxt:
self.assertTrue(ctxt)
self.assertTrue(bool(STPyV8.JSContext.inContext))
self.assertEqual(g.name, str(STPyV8.JSContext.entered.locals.name))
class Local:
name = "local"
l = Local()
with STPyV8.JSContext(l):
self.assertTrue(bool(STPyV8.JSContext.inContext))
self.assertEqual(l.name, str(STPyV8.JSContext.entered.locals.name))
self.assertTrue(bool(STPyV8.JSContext.inContext))
self.assertEqual(g.name, str(STPyV8.JSContext.current.locals.name))
self.assertTrue(not bool(STPyV8.JSContext.entered))
self.assertTrue(not bool(STPyV8.JSContext.inContext))
def testMultiContext(self):
with STPyV8.JSContext() as ctxt0:
ctxt0.securityToken = "password"
global0 = ctxt0.locals
global0.custom = 1234
self.assertEqual(1234, int(global0.custom))
with STPyV8.JSContext() as ctxt1:
ctxt1.securityToken = ctxt0.securityToken
global1 = ctxt1.locals
global1.custom = 1234
with ctxt0:
self.assertEqual(1234, int(global0.custom))
self.assertEqual(1234, int(global1.custom))
def testPromiseResolved(self):
with STPyV8.JSContext() as ctxt:
ctxt.eval("""
var message;
let done = true;
const isItDoneYet = new Promise((resolve, reject) => {
if (done) {
const workDone = 'Here is the thing I built'
resolve(workDone)
} else {
const why = 'Still working on something else'
reject(why)
}
})
const checkIfItsDone = () => {
isItDoneYet.then(ok => {
message = ok;
}).catch(err => {
message = err;
})
}
checkIfItsDone()
""")
self.assertEqual('Here is the thing I built', ctxt.locals.message)
def testPromiseRejected(self):
with STPyV8.JSContext() as ctxt:
ctxt.eval("""
var message;
let done = false;
const isItDoneYet = new Promise((resolve, reject) => {
if (done) {
const workDone = 'Here is the thing I built'
resolve(workDone)
} else {
const why = 'Still working on something else'
reject(why)
}
})
const checkIfItsDone = () => {
isItDoneYet.then(ok => {
message = ok;
}).catch(err => {
message = err;
})
}
checkIfItsDone()
""")
self.assertEqual('Still working on something else', ctxt.locals.message)
def testSecurityChecks(self):
with STPyV8.JSContext() as env1:
env1.securityToken = "foo"
# Create a function in env1.
env1.eval("spy = function(){return spy;}")
spy = env1.locals.spy
self.assertTrue(isinstance(spy, STPyV8.JSFunction))
# Create another function accessing global objects.
env1.eval("spy2 = function(){return 123;}")
spy2 = env1.locals.spy2
self.assertTrue(isinstance(spy2, STPyV8.JSFunction))
# Switch to env2 in the same domain and invoke spy on env2.
env2 = STPyV8.JSContext()
env2.securityToken = "foo"
with env2:
result = spy.apply(env2.locals)
self.assertTrue(isinstance(result, STPyV8.JSFunction))
env2.securityToken = "bar"
# FIXME
# Call cross_domain_call, it should throw an exception
# with env2:
# self.assertRaises(STPyV8.JSError, spy2.apply, env2.locals)