1 : #include "tests.h"
2 : #include "jsdbgapi.h"
3 : #include "jsobjinlines.h"
4 :
5 : JSPrincipals *sCurrentGlobalPrincipals = NULL;
6 :
7 : JSPrincipals *
8 25 : ObjectPrincipalsFinder(JSObject *)
9 : {
10 25 : return sCurrentGlobalPrincipals;
11 : }
12 :
13 : static const JSSecurityCallbacks seccb = {
14 : NULL,
15 : NULL,
16 : ObjectPrincipalsFinder,
17 : NULL
18 : };
19 :
20 : JSPrincipals *sOriginPrincipalsInErrorReporter = NULL;
21 :
22 : static void
23 4 : ErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
24 : {
25 4 : sOriginPrincipalsInErrorReporter = report->originPrincipals;
26 4 : }
27 :
28 : JSPrincipals prin1 = { 1 };
29 : JSPrincipals prin2 = { 1 };
30 :
31 4 : BEGIN_TEST(testOriginPrincipals)
32 : {
33 1 : JS_SetSecurityCallbacks(rt, &seccb);
34 :
35 : /*
36 : * Currently, the only way to set a non-trivial originPrincipal is to use
37 : * JS_EvaluateUCScriptForPrincipalsVersionOrigin. This does not expose the
38 : * compiled script, so we can only test nested scripts.
39 : */
40 :
41 1 : CHECK(testOuter("function f() {return 1}; f;"));
42 1 : CHECK(testOuter("function outer() { return (function () {return 2}); }; outer();"));
43 1 : CHECK(testOuter("eval('(function() {return 3})');"));
44 1 : CHECK(testOuter("(function (){ return eval('(function() {return 4})'); })()"));
45 1 : CHECK(testOuter("(function (){ return eval('(function() { return eval(\"(function(){return 5})\") })()'); })()"));
46 1 : CHECK(testOuter("new Function('return 6')"));
47 1 : CHECK(testOuter("function f() { return new Function('return 7') }; f();"));
48 1 : CHECK(testOuter("eval('new Function(\"return 8\")')"));
49 1 : CHECK(testOuter("(new Function('return eval(\"(function(){return 9})\")'))()"));
50 1 : CHECK(testOuter("(function(){return function(){return 10}}).bind()()"));
51 1 : CHECK(testOuter("var e = eval; (function() { return e('(function(){return 11})') })()"));
52 :
53 1 : JS_SetErrorReporter(cx, ErrorReporter);
54 1 : CHECK(testError("eval(-)"));
55 1 : CHECK(testError("-"));
56 1 : CHECK(testError("new Function('x', '-')"));
57 1 : CHECK(testError("eval('new Function(\"x\", \"-\")')"));
58 :
59 : /*
60 : * NB: uncaught exceptions, when reported, have nothing on the stack so
61 : * both the filename and originPrincipals are null. E.g., this would fail:
62 : *
63 : * CHECK(testError("throw 3"));
64 : */
65 1 : return true;
66 : }
67 :
68 : bool
69 26 : eval(const char *asciiChars, JSPrincipals *principals, JSPrincipals *originPrincipals, jsval *rval)
70 : {
71 26 : size_t len = strlen(asciiChars);
72 26 : jschar *chars = new jschar[len+1];
73 1200 : for (size_t i = 0; i < len; ++i)
74 1174 : chars[i] = asciiChars[i];
75 26 : chars[len] = 0;
76 :
77 : bool ok = JS_EvaluateUCScriptForPrincipalsVersionOrigin(cx, global,
78 : principals,
79 : originPrincipals,
80 : chars, len, "", 0, rval,
81 26 : JSVERSION_DEFAULT);
82 26 : delete[] chars;
83 26 : return ok;
84 : }
85 :
86 : bool
87 11 : testOuter(const char *asciiChars)
88 : {
89 11 : CHECK(testInner(asciiChars, &prin1, &prin1));
90 11 : CHECK(testInner(asciiChars, &prin1, &prin2));
91 11 : return true;
92 : }
93 :
94 : bool
95 22 : testInner(const char *asciiChars, JSPrincipals *principal, JSPrincipals *originPrincipal)
96 : {
97 22 : sCurrentGlobalPrincipals = principal;
98 :
99 : jsval rval;
100 22 : CHECK(eval(asciiChars, principal, originPrincipal, &rval));
101 :
102 22 : JSScript *script = JS_GetFunctionScript(cx, JSVAL_TO_OBJECT(rval)->toFunction());
103 22 : CHECK(JS_GetScriptPrincipals(script) == principal);
104 22 : CHECK(JS_GetScriptOriginPrincipals(script) == originPrincipal);
105 :
106 22 : return true;
107 : }
108 :
109 : bool
110 4 : testError(const char *asciiChars)
111 : {
112 : jsval rval;
113 4 : CHECK(!eval(asciiChars, &prin1, &prin2 /* = originPrincipals */, &rval));
114 4 : CHECK(JS_ReportPendingException(cx));
115 4 : CHECK(sOriginPrincipalsInErrorReporter == &prin2);
116 4 : return true;
117 : }
118 2 : END_TEST(testOriginPrincipals)
|