|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef jsapi_tests_tests_h |
|
8 #define jsapi_tests_tests_h |
|
9 |
|
10 #include <errno.h> |
|
11 #include <stdio.h> |
|
12 #include <stdlib.h> |
|
13 #include <string.h> |
|
14 |
|
15 #include "jsalloc.h" |
|
16 #include "jscntxt.h" |
|
17 #include "jsgc.h" |
|
18 |
|
19 #include "js/Vector.h" |
|
20 |
|
21 /* Note: Aborts on OOM. */ |
|
22 class JSAPITestString { |
|
23 js::Vector<char, 0, js::SystemAllocPolicy> chars; |
|
24 public: |
|
25 JSAPITestString() {} |
|
26 JSAPITestString(const char *s) { *this += s; } |
|
27 JSAPITestString(const JSAPITestString &s) { *this += s; } |
|
28 |
|
29 const char *begin() const { return chars.begin(); } |
|
30 const char *end() const { return chars.end(); } |
|
31 size_t length() const { return chars.length(); } |
|
32 |
|
33 JSAPITestString & operator +=(const char *s) { |
|
34 if (!chars.append(s, strlen(s))) |
|
35 abort(); |
|
36 return *this; |
|
37 } |
|
38 |
|
39 JSAPITestString & operator +=(const JSAPITestString &s) { |
|
40 if (!chars.append(s.begin(), s.length())) |
|
41 abort(); |
|
42 return *this; |
|
43 } |
|
44 }; |
|
45 |
|
46 inline JSAPITestString operator+(JSAPITestString a, const char *b) { return a += b; } |
|
47 inline JSAPITestString operator+(JSAPITestString a, const JSAPITestString &b) { return a += b; } |
|
48 |
|
49 class JSAPITest |
|
50 { |
|
51 public: |
|
52 static JSAPITest *list; |
|
53 JSAPITest *next; |
|
54 |
|
55 JSRuntime *rt; |
|
56 JSContext *cx; |
|
57 JS::Heap<JSObject *> global; |
|
58 bool knownFail; |
|
59 JSAPITestString msgs; |
|
60 JSCompartment *oldCompartment; |
|
61 |
|
62 JSAPITest() : rt(nullptr), cx(nullptr), global(nullptr), |
|
63 knownFail(false), oldCompartment(nullptr) { |
|
64 next = list; |
|
65 list = this; |
|
66 } |
|
67 |
|
68 virtual ~JSAPITest() { uninit(); } |
|
69 |
|
70 virtual bool init(); |
|
71 |
|
72 virtual void uninit() { |
|
73 if (oldCompartment) { |
|
74 JS_LeaveCompartment(cx, oldCompartment); |
|
75 oldCompartment = nullptr; |
|
76 } |
|
77 global = nullptr; |
|
78 if (cx) { |
|
79 JS::RemoveObjectRoot(cx, &global); |
|
80 JS_LeaveCompartment(cx, nullptr); |
|
81 JS_EndRequest(cx); |
|
82 JS_DestroyContext(cx); |
|
83 cx = nullptr; |
|
84 } |
|
85 if (rt) { |
|
86 destroyRuntime(); |
|
87 rt = nullptr; |
|
88 } |
|
89 } |
|
90 |
|
91 virtual const char * name() = 0; |
|
92 virtual bool run(JS::HandleObject global) = 0; |
|
93 |
|
94 #define EXEC(s) do { if (!exec(s, __FILE__, __LINE__)) return false; } while (false) |
|
95 |
|
96 bool exec(const char *bytes, const char *filename, int lineno); |
|
97 |
|
98 #define EVAL(s, vp) do { if (!evaluate(s, __FILE__, __LINE__, vp)) return false; } while (false) |
|
99 |
|
100 bool evaluate(const char *bytes, const char *filename, int lineno, JS::MutableHandleValue vp); |
|
101 |
|
102 JSAPITestString jsvalToSource(JS::HandleValue v) { |
|
103 JSString *str = JS_ValueToSource(cx, v); |
|
104 if (str) { |
|
105 JSAutoByteString bytes(cx, str); |
|
106 if (!!bytes) |
|
107 return JSAPITestString(bytes.ptr()); |
|
108 } |
|
109 JS_ClearPendingException(cx); |
|
110 return JSAPITestString("<<error converting value to string>>"); |
|
111 } |
|
112 |
|
113 JSAPITestString toSource(long v) { |
|
114 char buf[40]; |
|
115 sprintf(buf, "%ld", v); |
|
116 return JSAPITestString(buf); |
|
117 } |
|
118 |
|
119 JSAPITestString toSource(unsigned long v) { |
|
120 char buf[40]; |
|
121 sprintf(buf, "%lu", v); |
|
122 return JSAPITestString(buf); |
|
123 } |
|
124 |
|
125 JSAPITestString toSource(long long v) { |
|
126 char buf[40]; |
|
127 sprintf(buf, "%lld", v); |
|
128 return JSAPITestString(buf); |
|
129 } |
|
130 |
|
131 JSAPITestString toSource(unsigned long long v) { |
|
132 char buf[40]; |
|
133 sprintf(buf, "%llu", v); |
|
134 return JSAPITestString(buf); |
|
135 } |
|
136 |
|
137 JSAPITestString toSource(unsigned int v) { |
|
138 return toSource((unsigned long)v); |
|
139 } |
|
140 |
|
141 JSAPITestString toSource(int v) { |
|
142 return toSource((long)v); |
|
143 } |
|
144 |
|
145 JSAPITestString toSource(bool v) { |
|
146 return JSAPITestString(v ? "true" : "false"); |
|
147 } |
|
148 |
|
149 JSAPITestString toSource(JSAtom *v) { |
|
150 JS::RootedValue val(cx, JS::StringValue((JSString *)v)); |
|
151 return jsvalToSource(val); |
|
152 } |
|
153 |
|
154 JSAPITestString toSource(JSVersion v) { |
|
155 return JSAPITestString(JS_VersionToString(v)); |
|
156 } |
|
157 |
|
158 template<typename T> |
|
159 bool checkEqual(const T &actual, const T &expected, |
|
160 const char *actualExpr, const char *expectedExpr, |
|
161 const char *filename, int lineno) { |
|
162 return (actual == expected) || |
|
163 fail(JSAPITestString("CHECK_EQUAL failed: expected (") + |
|
164 expectedExpr + ") = " + toSource(expected) + |
|
165 ", got (" + actualExpr + ") = " + toSource(actual), filename, lineno); |
|
166 } |
|
167 |
|
168 // There are many cases where the static types of 'actual' and 'expected' |
|
169 // are not identical, and C++ is understandably cautious about automatic |
|
170 // coercions. So catch those cases and forcibly coerce, then use the |
|
171 // identical-type specialization. This may do bad things if the types are |
|
172 // actually *not* compatible. |
|
173 template<typename T, typename U> |
|
174 bool checkEqual(const T &actual, const U &expected, |
|
175 const char *actualExpr, const char *expectedExpr, |
|
176 const char *filename, int lineno) { |
|
177 return checkEqual(U(actual), expected, actualExpr, expectedExpr, filename, lineno); |
|
178 } |
|
179 |
|
180 #define CHECK_EQUAL(actual, expected) \ |
|
181 do { \ |
|
182 if (!checkEqual(actual, expected, #actual, #expected, __FILE__, __LINE__)) \ |
|
183 return false; \ |
|
184 } while (false) |
|
185 |
|
186 bool checkSame(jsval actualArg, jsval expectedArg, |
|
187 const char *actualExpr, const char *expectedExpr, |
|
188 const char *filename, int lineno) { |
|
189 bool same; |
|
190 JS::RootedValue actual(cx, actualArg), expected(cx, expectedArg); |
|
191 return (JS_SameValue(cx, actual, expected, &same) && same) || |
|
192 fail(JSAPITestString("CHECK_SAME failed: expected JS_SameValue(cx, ") + |
|
193 actualExpr + ", " + expectedExpr + "), got !JS_SameValue(cx, " + |
|
194 jsvalToSource(actual) + ", " + jsvalToSource(expected) + ")", filename, lineno); |
|
195 } |
|
196 |
|
197 #define CHECK_SAME(actual, expected) \ |
|
198 do { \ |
|
199 if (!checkSame(actual, expected, #actual, #expected, __FILE__, __LINE__)) \ |
|
200 return false; \ |
|
201 } while (false) |
|
202 |
|
203 #define CHECK(expr) \ |
|
204 do { \ |
|
205 if (!(expr)) \ |
|
206 return fail("CHECK failed: " #expr, __FILE__, __LINE__); \ |
|
207 } while (false) |
|
208 |
|
209 bool fail(JSAPITestString msg = JSAPITestString(), const char *filename = "-", int lineno = 0) { |
|
210 if (JS_IsExceptionPending(cx)) { |
|
211 js::gc::AutoSuppressGC gcoff(cx); |
|
212 JS::RootedValue v(cx); |
|
213 JS_GetPendingException(cx, &v); |
|
214 JS_ClearPendingException(cx); |
|
215 JSString *s = JS::ToString(cx, v); |
|
216 if (s) { |
|
217 JSAutoByteString bytes(cx, s); |
|
218 if (!!bytes) |
|
219 msg += bytes.ptr(); |
|
220 } |
|
221 } |
|
222 fprintf(stderr, "%s:%d:%.*s\n", filename, lineno, (int) msg.length(), msg.begin()); |
|
223 msgs += msg; |
|
224 return false; |
|
225 } |
|
226 |
|
227 JSAPITestString messages() const { return msgs; } |
|
228 |
|
229 static const JSClass * basicGlobalClass() { |
|
230 static const JSClass c = { |
|
231 "global", JSCLASS_GLOBAL_FLAGS, |
|
232 JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub, |
|
233 JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, nullptr, |
|
234 nullptr, nullptr, nullptr, |
|
235 JS_GlobalObjectTraceHook |
|
236 }; |
|
237 return &c; |
|
238 } |
|
239 |
|
240 protected: |
|
241 static bool |
|
242 print(JSContext *cx, unsigned argc, jsval *vp) |
|
243 { |
|
244 JS::CallArgs args = JS::CallArgsFromVp(argc, vp); |
|
245 |
|
246 for (unsigned i = 0; i < args.length(); i++) { |
|
247 JSString *str = JS::ToString(cx, args[i]); |
|
248 if (!str) |
|
249 return false; |
|
250 char *bytes = JS_EncodeString(cx, str); |
|
251 if (!bytes) |
|
252 return false; |
|
253 printf("%s%s", i ? " " : "", bytes); |
|
254 JS_free(cx, bytes); |
|
255 } |
|
256 |
|
257 putchar('\n'); |
|
258 fflush(stdout); |
|
259 args.rval().setUndefined(); |
|
260 return true; |
|
261 } |
|
262 |
|
263 bool definePrint(); |
|
264 |
|
265 static void setNativeStackQuota(JSRuntime *rt) |
|
266 { |
|
267 const size_t MAX_STACK_SIZE = |
|
268 /* Assume we can't use more than 5e5 bytes of C stack by default. */ |
|
269 #if (defined(DEBUG) && defined(__SUNPRO_CC)) || defined(JS_CPU_SPARC) |
|
270 /* |
|
271 * Sun compiler uses a larger stack space for js::Interpret() with |
|
272 * debug. Use a bigger gMaxStackSize to make "make check" happy. |
|
273 */ |
|
274 5000000 |
|
275 #else |
|
276 500000 |
|
277 #endif |
|
278 ; |
|
279 |
|
280 JS_SetNativeStackQuota(rt, MAX_STACK_SIZE); |
|
281 } |
|
282 |
|
283 virtual JSRuntime * createRuntime() { |
|
284 JSRuntime *rt = JS_NewRuntime(8L * 1024 * 1024, JS_USE_HELPER_THREADS); |
|
285 if (!rt) |
|
286 return nullptr; |
|
287 setNativeStackQuota(rt); |
|
288 return rt; |
|
289 } |
|
290 |
|
291 virtual void destroyRuntime() { |
|
292 JS_ASSERT(!cx); |
|
293 JS_ASSERT(rt); |
|
294 JS_DestroyRuntime(rt); |
|
295 } |
|
296 |
|
297 static void reportError(JSContext *cx, const char *message, JSErrorReport *report) { |
|
298 fprintf(stderr, "%s:%u:%s\n", |
|
299 report->filename ? report->filename : "<no filename>", |
|
300 (unsigned int) report->lineno, |
|
301 message); |
|
302 } |
|
303 |
|
304 virtual JSContext * createContext() { |
|
305 JSContext *cx = JS_NewContext(rt, 8192); |
|
306 if (!cx) |
|
307 return nullptr; |
|
308 JS::ContextOptionsRef(cx).setVarObjFix(true); |
|
309 JS_SetErrorReporter(cx, &reportError); |
|
310 return cx; |
|
311 } |
|
312 |
|
313 virtual const JSClass * getGlobalClass() { |
|
314 return basicGlobalClass(); |
|
315 } |
|
316 |
|
317 virtual JSObject * createGlobal(JSPrincipals *principals = nullptr); |
|
318 }; |
|
319 |
|
320 #define BEGIN_TEST(testname) \ |
|
321 class cls_##testname : public JSAPITest { \ |
|
322 public: \ |
|
323 virtual const char * name() { return #testname; } \ |
|
324 virtual bool run(JS::HandleObject global) |
|
325 |
|
326 #define END_TEST(testname) \ |
|
327 }; \ |
|
328 static cls_##testname cls_##testname##_instance; |
|
329 |
|
330 /* |
|
331 * A "fixture" is a subclass of JSAPITest that holds common definitions for a |
|
332 * set of tests. Each test that wants to use the fixture should use |
|
333 * BEGIN_FIXTURE_TEST and END_FIXTURE_TEST, just as one would use BEGIN_TEST and |
|
334 * END_TEST, but include the fixture class as the first argument. The fixture |
|
335 * class's declarations are then in scope for the test bodies. |
|
336 */ |
|
337 |
|
338 #define BEGIN_FIXTURE_TEST(fixture, testname) \ |
|
339 class cls_##testname : public fixture { \ |
|
340 public: \ |
|
341 virtual const char * name() { return #testname; } \ |
|
342 virtual bool run(JS::HandleObject global) |
|
343 |
|
344 #define END_FIXTURE_TEST(fixture, testname) \ |
|
345 }; \ |
|
346 static cls_##testname cls_##testname##_instance; |
|
347 |
|
348 /* |
|
349 * A class for creating and managing one temporary file. |
|
350 * |
|
351 * We could use the ISO C temporary file functions here, but those try to |
|
352 * create files in the root directory on Windows, which fails for users |
|
353 * without Administrator privileges. |
|
354 */ |
|
355 class TempFile { |
|
356 const char *name; |
|
357 FILE *stream; |
|
358 |
|
359 public: |
|
360 TempFile() : name(), stream() { } |
|
361 ~TempFile() { |
|
362 if (stream) |
|
363 close(); |
|
364 if (name) |
|
365 remove(); |
|
366 } |
|
367 |
|
368 /* |
|
369 * Return a stream for a temporary file named |fileName|. Infallible. |
|
370 * Use only once per TempFile instance. If the file is not explicitly |
|
371 * closed and deleted via the member functions below, this object's |
|
372 * destructor will clean them up. |
|
373 */ |
|
374 FILE *open(const char *fileName) |
|
375 { |
|
376 stream = fopen(fileName, "wb+"); |
|
377 if (!stream) { |
|
378 fprintf(stderr, "error opening temporary file '%s': %s\n", |
|
379 fileName, strerror(errno)); |
|
380 exit(1); |
|
381 } |
|
382 name = fileName; |
|
383 return stream; |
|
384 } |
|
385 |
|
386 /* Close the temporary file's stream. */ |
|
387 void close() { |
|
388 if (fclose(stream) == EOF) { |
|
389 fprintf(stderr, "error closing temporary file '%s': %s\n", |
|
390 name, strerror(errno)); |
|
391 exit(1); |
|
392 } |
|
393 stream = nullptr; |
|
394 } |
|
395 |
|
396 /* Delete the temporary file. */ |
|
397 void remove() { |
|
398 if (::remove(name) != 0) { |
|
399 fprintf(stderr, "error deleting temporary file '%s': %s\n", |
|
400 name, strerror(errno)); |
|
401 exit(1); |
|
402 } |
|
403 name = nullptr; |
|
404 } |
|
405 }; |
|
406 |
|
407 // Just a wrapper around JSPrincipals that allows static construction. |
|
408 class TestJSPrincipals : public JSPrincipals |
|
409 { |
|
410 public: |
|
411 TestJSPrincipals(int rc = 0) |
|
412 : JSPrincipals() |
|
413 { |
|
414 refcount = rc; |
|
415 } |
|
416 }; |
|
417 |
|
418 #endif /* jsapi_tests_tests_h */ |