OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #library('bcap_tests'); | |
6 | |
7 #import('dart:html'); | |
8 #import('../../../lib/unittest/unittest.dart'); | |
9 #import('../../../lib/json/json.dart'); | |
10 #import('../bcap/bcap.dart'); | |
11 | |
12 void main() { | |
13 new BcapTests().run(); | |
14 } | |
15 | |
16 class BcapTests extends UnitTestSuite { | |
17 BcapTests() : super() { } | |
18 | |
19 void setUpTestSuite() { | |
20 addTest(() { testUUID(); }); | |
21 addTest(() { testDecode(); }); | |
22 addTest(() { testPreProcess(); }); | |
23 addTest(() { testPostProcess(); }); | |
24 addTest(() { testPrePostProcessSimple(); }); | |
25 addTest(() { testPrePostProcessStruct(); }); | |
26 addTest(() { testBasicLifeCycle(); }); | |
27 addTest(() { testReviver(); }); | |
28 addTest(() { testCrossServerInvoke(); }); | |
29 addAsyncTest(() { testGrantGrant(); }, 1); | |
30 // addAsyncTest(() { testIsolateInvoke(); }, 1); | |
31 // addTest(() { testWindow(); }); | |
32 } | |
33 | |
34 void testUUID() { | |
35 var r = BcapUtil.uuidv4(); | |
36 Expect.isTrue(r.toString() == r); | |
37 Expect.equals(r.length, 36); | |
38 } | |
39 | |
40 void testDecode() { | |
41 var uuid1 = BcapUtil.uuidv4(); | |
42 var uuid2 = BcapUtil.uuidv4(); | |
43 var serCap ="urn:x-cap:$uuid1:$uuid2"; | |
44 var decoded = BcapUtil.decodeSer(serCap); | |
45 Expect.equals(2, decoded.length); | |
46 Expect.equals(uuid1, decoded[0]); | |
47 Expect.equals(uuid2, decoded[1]); | |
48 | |
49 Expect.equals(uuid1, BcapUtil.decodeInstID(serCap)); | |
50 Expect.equals(uuid2, BcapUtil.decodeCapID(serCap)); | |
51 } | |
52 | |
53 void testPreProcess() { | |
54 BcapServer server = new BcapServer(null, null); | |
55 | |
56 String ser = BcapUtil.uuidv4(); | |
57 Bcap cap = server.restore(ser); | |
58 Expect.equals("{\"value\":{\"@\":\"$ser\"}}", server.dataPreProcess(cap)); | |
59 | |
60 String ser2 = BcapUtil.uuidv4(); | |
61 Bcap cap2 = server.restore(ser2); | |
62 var somejson = [cap2]; | |
63 Expect.equals("{\"value\":[{\"@\":\"$ser2\"}]}", | |
64 server.dataPreProcess(somejson)); | |
65 | |
66 | |
67 String ser3 = BcapUtil.uuidv4(); | |
68 Bcap cap3 = server.restore(ser3); | |
69 var somejson2 = {"foo": [cap, cap3, 5]}; | |
70 Expect.equals("{\"value\":{\"foo\":[{\"@\":\"$ser\"},{\"@\":\"$ser3\"},5]}}"
, | |
71 server.dataPreProcess(somejson2)); | |
72 } | |
73 | |
74 void testPostProcess() { | |
75 BcapServer server = new BcapServer(null, null); | |
76 | |
77 String ser = BcapUtil.uuidv4(); | |
78 Bcap cap = server.restore(ser); | |
79 Bcap foo = server.dataPostProcess("{\"value\":{\"@\":\"$ser\"}}"); | |
80 Expect.equals(cap, foo); | |
81 } | |
82 | |
83 void testPrePostProcessSimple() { | |
84 BcapServer server = new BcapServer(BcapUtil.uuidv4()); | |
85 var roundTrip = Object _(Object data) { | |
86 Expect.equals(data, | |
87 server.dataPostProcess(server.dataPreProcess(data))); | |
88 }; | |
89 | |
90 roundTrip(null); | |
91 roundTrip(false); | |
92 roundTrip(true); | |
93 roundTrip(0); | |
94 roundTrip(501234); | |
95 roundTrip(''); | |
96 roundTrip("\n"); | |
97 } | |
98 | |
99 void testPrePostProcessStruct() { | |
100 BcapServer server = new BcapServer(BcapUtil.uuidv4()); | |
101 var roundTrip = Object _(Object data) { | |
102 return server.dataPostProcess(server.dataPreProcess(data)); | |
103 }; | |
104 | |
105 Expect.equals(5, roundTrip([5])[0]); | |
106 var rtObj = roundTrip({"foo": [true, false]}); | |
107 Expect.equals(true, rtObj["foo"][0]); | |
108 Expect.equals(false, rtObj["foo"][1]); | |
109 } | |
110 | |
111 void testBasicLifeCycle() { | |
112 int f(_) { return 42; } | |
113 var bc = new BcapServer(BcapUtil.uuidv4()); | |
114 var cap = bc.grantFunc(f); | |
115 | |
116 Expect.notEquals(cap, null); | |
117 var result = "1-not-set"; | |
118 cap.get_(void _(r) { result = r;}); | |
119 Expect.equals(42, result); | |
120 | |
121 bc.revoke(cap.serialize()); | |
122 var invoked = false; | |
123 var resultRevoked = "revoked-not-set"; | |
124 cap.get_(void _(r) { resultRevoked = r; invoked = true; }, | |
125 void _(e) { Expect.equals(e.status, 500); }); | |
126 Expect.equals(resultRevoked, "revoked-not-set"); | |
127 Expect.isFalse(invoked); | |
128 | |
129 var cap2 = bc.grantFunc(f); | |
130 var result2 = "2-not-set"; | |
131 Expect.notEquals(cap2.serialize(), cap.serialize()); | |
132 cap2.get_(void _(r) { result2 = r; }); | |
133 Expect.equals(42, result2); | |
134 | |
135 var cap3 = bc.grantFunc(f); | |
136 Expect.isFalse(cap2 === cap3); | |
137 Expect.notEquals(cap2.serialize(), cap3.serialize()); | |
138 bc.revoke(cap2.serialize()); | |
139 var result3 = "3-not-set"; | |
140 cap3.get_(void _(r) { result3 = r; }); | |
141 Expect.equals(result3, 42); | |
142 } | |
143 | |
144 void testReviver() { | |
145 BcapServer bc = new BcapServer(BcapUtil.uuidv4()); | |
146 bool called = false; | |
147 int f(_) { if (called) { return 84; } called = true; return 42; } | |
148 reviver(key) { | |
149 if (key == "get-f-back") { | |
150 return new BcapFunctionHandler(f); | |
151 } | |
152 return null; | |
153 } | |
154 Bcap cap1 = bc.grantFunc(f); | |
155 var result = 0; | |
156 cap1.get_(void _(r) { result = r; }); | |
157 Expect.equals(result, 42); | |
158 | |
159 var snap = bc.snapshot(); | |
160 bc.revokeAll(); | |
161 | |
162 BcapServer bc2 = new BcapServer(null, snap); | |
163 bc2.setReviver(reviver); | |
164 Bcap cap2 = bc2.grantKey("get-f-back"); | |
165 | |
166 cap2.get_(void _(r) { result = r; }); | |
167 Expect.equals(result, 84); | |
168 } | |
169 | |
170 void testCrossServerInvoke() { | |
171 String instID1 = BcapUtil.uuidv4(); | |
172 String instID2 = BcapUtil.uuidv4(); | |
173 | |
174 BcapServer bc1 = new BcapServer(instID1); | |
175 BcapServer bc2 = new BcapServer(instID2); | |
176 | |
177 int f(_) { return 29; } | |
178 | |
179 Bcap cap1 = bc1.grantFunc(f); | |
180 | |
181 BcapServer resolver2(instID) { | |
182 if (instID == instID1) { | |
183 return bc1; | |
184 } | |
185 } | |
186 bc2.setResolver(resolver2); | |
187 | |
188 Bcap cap2 = bc2.restore(cap1.serialize()); | |
189 var result = 0; | |
190 bool failed = false; | |
191 cap2.get_(void _(r) { result = r; }, | |
192 void _(e) { failed = true; }); | |
193 Expect.isFalse(failed); | |
194 Expect.equals(result, 29); | |
195 } | |
196 | |
197 void testGrantGrant() { | |
198 BcapServer bc = new BcapServer(BcapUtil.uuidv4()); | |
199 | |
200 Bcap f = bc.grantFunc((x) => bc.grantFunc((y) => x * y)); | |
201 | |
202 f.post(2, void _(g) { | |
203 g.post(3, void _(r) { | |
204 Expect.equals(6, r); | |
205 callbackDone(); | |
206 }); | |
207 }); | |
208 | |
209 } | |
210 | |
211 | |
212 /* Not implemented yet | |
213 void testIsolateInvoke() { | |
214 | |
215 var testDir = "../../tests/client/bcap"; | |
216 Window child; | |
217 | |
218 | |
219 String uuid = BcapUtil.uuidv4(); | |
220 BcapServer cs = new BcapServer(uuid); | |
221 | |
222 | |
223 Object getSeedCap() { | |
224 return cs.grantAsyncFunc(void _(arg, sk, fk) { | |
225 Expect.equals(true, false); | |
226 Expect.equals("from the child", arg); | |
227 // child.close(); | |
228 callbackDone(); | |
229 }).serialize(); | |
230 }; | |
231 | |
232 WindowTunnel tunnel = new WindowTunnel(cs, window, getSeedCap, null); | |
233 cs.setResolver(BcapServerInterface _(instID) { | |
234 return tunnel.getSendInterface(instID); | |
235 }); | |
236 | |
237 // TODO(arjun): abstract these into a method on WindowTunnel | |
238 var childUuid = BcapUtil.uuidv4(); | |
239 childUuid = "fa4e6d8a-e51b-4853-831a-846a1936f9ee"; | |
240 child = window.open("$testDir/bcap_child.html", childUuid); | |
241 tunnel.registerWindow(child, childUuid); | |
242 | |
243 | |
244 | |
245 } | |
246 | |
247 void testWindow() { | |
248 String uuid = BcapUtil.uuidv4(); | |
249 BcapServer server = new BcapServer(uuid); | |
250 | |
251 WindowTunnel tunnel; | |
252 var called = false; | |
253 Bcap cap2; | |
254 Bcap cap1 = server.grantAsyncFunc(void _(data, sk, fk) { | |
255 String uuidChild2 = "fa4e6d8a-e51b-4853-831a-846a1936f9ee"; | |
256 Window child2 = mywin.open("../../tests/client/bcap/TestPage.html", "$uuid
Child2"); | |
257 tunnel.registerWindow(child2, uuidChild2); | |
258 called = true; | |
259 cap2 = data; | |
260 }); | |
261 | |
262 Object getSeedCap() { | |
263 if (!called) { return {"first": cap1.serialize()}; } | |
264 return {"second": cap2.serialize()}; | |
265 } | |
266 | |
267 tunnel = new WindowTunnel(server, mywin, getSeedCap, null); | |
268 String uuidChild1 = "9065b6ef-41d4-4684-879e-23794f86f1fa"; | |
269 Window child1 = mywin.open("../../tests/client/bcap/TestPage.html", "$uuidCh
ild1"); | |
270 tunnel.registerWindow(child1, uuidChild1); | |
271 | |
272 server.setResolver(BcapServerInterface _(instID) { | |
273 if (instID === uuid) { return server; } | |
274 else { return tunnel.getSendInterface(instID); } | |
275 }); | |
276 } | |
277 */ | |
278 } | |
279 | |
OLD | NEW |