summaryrefslogtreecommitdiffstats
path: root/js/src/jsapi-tests/testStructuredClone.cpp
blob: 2604af202cb8b68c73713efc733692f803ac2eea (plain)
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "builtin/TestingFunctions.h"
#include "js/ArrayBuffer.h"  // JS::{IsArrayBufferObject,GetArrayBufferLengthAndData,NewExternalArrayBuffer}
#include "js/GlobalObject.h"        // JS_NewGlobalObject
#include "js/PropertyAndElement.h"  // JS_GetProperty, JS_SetProperty
#include "js/StructuredClone.h"

#include "jsapi-tests/tests.h"

using namespace js;

#ifdef DEBUG
// Skip test, since it will abort with an assert in buf->Init(7).
#else
BEGIN_TEST(testStructuredClone_invalidLength) {
  auto buf = js::MakeUnique<JSStructuredCloneData>(
      JS::StructuredCloneScope::DifferentProcess);
  CHECK(buf);
  CHECK(buf->Init(7));
  RootedValue clone(cx);
  JS::CloneDataPolicy policy;
  CHECK(!JS_ReadStructuredClone(cx, *buf, JS_STRUCTURED_CLONE_VERSION,
                                JS::StructuredCloneScope::DifferentProcess,
                                &clone, policy, nullptr, nullptr));
  return true;
}
END_TEST(testStructuredClone_invalidLength)
#endif

BEGIN_TEST(testStructuredClone_object) {
  JS::RootedObject g1(cx, createGlobal());
  JS::RootedObject g2(cx, createGlobal());
  CHECK(g1);
  CHECK(g2);

  JS::RootedValue v1(cx);

  {
    JSAutoRealm ar(cx, g1);
    JS::RootedValue prop(cx, JS::Int32Value(1337));

    JS::RootedObject obj(cx, JS_NewPlainObject(cx));
    v1 = JS::ObjectOrNullValue(obj);
    CHECK(v1.isObject());
    CHECK(JS_SetProperty(cx, obj, "prop", prop));
  }

  {
    JSAutoRealm ar(cx, g2);
    JS::RootedValue v2(cx);

    CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr));
    CHECK(v2.isObject());
    JS::RootedObject obj(cx, &v2.toObject());

    JS::RootedValue prop(cx);
    CHECK(JS_GetProperty(cx, obj, "prop", &prop));
    CHECK(prop.isInt32());
    CHECK(&v1.toObject() != obj);
    CHECK_EQUAL(prop.toInt32(), 1337);
  }

  return true;
}
END_TEST(testStructuredClone_object)

BEGIN_TEST(testStructuredClone_string) {
  JS::RootedObject g1(cx, createGlobal());
  JS::RootedObject g2(cx, createGlobal());
  CHECK(g1);
  CHECK(g2);

  JS::RootedValue v1(cx);

  {
    JSAutoRealm ar(cx, g1);
    JS::RootedValue prop(cx, JS::Int32Value(1337));

    v1 = JS::StringValue(JS_NewStringCopyZ(cx, "Hello World!"));
    CHECK(v1.isString());
    CHECK(v1.toString());
  }

  {
    JSAutoRealm ar(cx, g2);
    JS::RootedValue v2(cx);

    CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr));
    CHECK(v2.isString());
    CHECK(v2.toString());

    JS::RootedValue expected(
        cx, JS::StringValue(JS_NewStringCopyZ(cx, "Hello World!")));
    CHECK_SAME(v2, expected);
  }

  return true;
}
END_TEST(testStructuredClone_string)

BEGIN_TEST(testStructuredClone_externalArrayBuffer) {
  ExternalData data("One two three four");
  JS::RootedObject g1(cx, createGlobal());
  JS::RootedObject g2(cx, createGlobal());
  CHECK(g1);
  CHECK(g2);

  JS::RootedValue v1(cx);

  {
    JSAutoRealm ar(cx, g1);

    JS::RootedObject obj(
        cx, JS::NewExternalArrayBuffer(cx, data.len(), data.contents(),
                                       &ExternalData::freeCallback, &data));
    CHECK(!data.wasFreed());

    v1 = JS::ObjectOrNullValue(obj);
    CHECK(v1.isObject());
  }

  {
    JSAutoRealm ar(cx, g2);
    JS::RootedValue v2(cx);

    CHECK(JS_StructuredClone(cx, v1, &v2, nullptr, nullptr));
    CHECK(v2.isObject());

    JS::RootedObject obj(cx, &v2.toObject());
    CHECK(&v1.toObject() != obj);

    size_t len;
    bool isShared;
    uint8_t* clonedData;
    JS::GetArrayBufferLengthAndData(obj, &len, &isShared, &clonedData);

    // The contents of the two array buffers should be equal, but not the
    // same pointer.
    CHECK_EQUAL(len, data.len());
    CHECK(clonedData != data.contents());
    CHECK(strcmp(reinterpret_cast<char*>(clonedData), data.asString()) == 0);
    CHECK(!data.wasFreed());
  }

  // GC the array buffer before data goes out of scope
  v1.setNull();
  JS_GC(cx);
  JS_GC(cx);  // Trigger another to wait for background finalization to end

  CHECK(data.wasFreed());

  return true;
}
END_TEST(testStructuredClone_externalArrayBuffer)

BEGIN_TEST(testStructuredClone_externalArrayBufferDifferentThreadOrProcess) {
  CHECK(testStructuredCloneCopy(JS::StructuredCloneScope::SameProcess));
  CHECK(testStructuredCloneCopy(JS::StructuredCloneScope::DifferentProcess));
  return true;
}

bool testStructuredCloneCopy(JS::StructuredCloneScope scope) {
  ExternalData data("One two three four");
  JS::RootedObject buffer(
      cx, JS::NewExternalArrayBuffer(cx, data.len(), data.contents(),
                                     &ExternalData::freeCallback, &data));
  CHECK(buffer);
  CHECK(!data.wasFreed());

  JS::RootedValue v1(cx, JS::ObjectValue(*buffer));
  JS::RootedValue v2(cx);
  CHECK(clone(scope, v1, &v2));
  JS::RootedObject bufferOut(cx, v2.toObjectOrNull());
  CHECK(bufferOut);
  CHECK(JS::IsArrayBufferObject(bufferOut));

  size_t len;
  bool isShared;
  uint8_t* clonedData;
  JS::GetArrayBufferLengthAndData(bufferOut, &len, &isShared, &clonedData);

  // Cloning should copy the data, so the contents of the two array buffers
  // should be equal, but not the same pointer.
  CHECK_EQUAL(len, data.len());
  CHECK(clonedData != data.contents());
  CHECK(strcmp(reinterpret_cast<char*>(clonedData), data.asString()) == 0);
  CHECK(!data.wasFreed());

  buffer = nullptr;
  bufferOut = nullptr;
  v1.setNull();
  v2.setNull();
  JS_GC(cx);
  JS_GC(cx);
  CHECK(data.wasFreed());

  return true;
}

bool clone(JS::StructuredCloneScope scope, JS::HandleValue v1,
           JS::MutableHandleValue v2) {
  JSAutoStructuredCloneBuffer clonedBuffer(scope, nullptr, nullptr);
  CHECK(clonedBuffer.write(cx, v1));
  CHECK(clonedBuffer.read(cx, v2));
  return true;
}
END_TEST(testStructuredClone_externalArrayBufferDifferentThreadOrProcess)

struct StructuredCloneTestPrincipals final : public JSPrincipals {
  uint32_t rank;

  explicit StructuredCloneTestPrincipals(uint32_t rank, int32_t rc = 1)
      : rank(rank) {
    this->refcount = rc;
  }

  bool write(JSContext* cx, JSStructuredCloneWriter* writer) override {
    return JS_WriteUint32Pair(writer, rank, 0);
  }

  bool isSystemOrAddonPrincipal() override { return true; }

  static bool read(JSContext* cx, JSStructuredCloneReader* reader,
                   JSPrincipals** outPrincipals) {
    uint32_t rank;
    uint32_t unused;
    if (!JS_ReadUint32Pair(reader, &rank, &unused)) {
      return false;
    }

    *outPrincipals = new StructuredCloneTestPrincipals(rank);
    return !!*outPrincipals;
  }

  static void destroy(JSPrincipals* p) {
    auto p1 = static_cast<StructuredCloneTestPrincipals*>(p);
    delete p1;
  }

  static uint32_t getRank(JSPrincipals* p) {
    if (!p) {
      return 0;
    }
    return static_cast<StructuredCloneTestPrincipals*>(p)->rank;
  }

  static bool subsumes(JSPrincipals* a, JSPrincipals* b) {
    return getRank(a) > getRank(b);
  }

  static JSSecurityCallbacks securityCallbacks;

  static StructuredCloneTestPrincipals testPrincipals;
};

JSSecurityCallbacks StructuredCloneTestPrincipals::securityCallbacks = {
    nullptr,  // contentSecurityPolicyAllows
    subsumes};

BEGIN_TEST(testStructuredClone_SavedFrame) {
  JS_SetSecurityCallbacks(cx,
                          &StructuredCloneTestPrincipals::securityCallbacks);
  JS_InitDestroyPrincipalsCallback(cx, StructuredCloneTestPrincipals::destroy);
  JS_InitReadPrincipalsCallback(cx, StructuredCloneTestPrincipals::read);

  auto testPrincipals = new StructuredCloneTestPrincipals(42, 0);
  CHECK(testPrincipals);

  auto DONE = (JSPrincipals*)0xDEADBEEF;

  struct {
    const char* name;
    JSPrincipals* principals;
  } principalsToTest[] = {
      {"IsSystem", &js::ReconstructedSavedFramePrincipals::IsSystem},
      {"IsNotSystem", &js::ReconstructedSavedFramePrincipals::IsNotSystem},
      {"testPrincipals", testPrincipals},
      {"nullptr principals", nullptr},
      {"DONE", DONE}};

  const char* FILENAME = "filename.js";

  for (auto* pp = principalsToTest; pp->principals != DONE; pp++) {
    fprintf(stderr, "Testing with principals '%s'\n", pp->name);

    JS::RealmOptions options;
    JS::RootedObject g(cx,
                       JS_NewGlobalObject(cx, getGlobalClass(), pp->principals,
                                          JS::FireOnNewGlobalHook, options));
    CHECK(g);
    JSAutoRealm ar(cx, g);

    CHECK(js::DefineTestingFunctions(cx, g, false, false));

    JS::RootedValue srcVal(cx);
    CHECK(
        evaluate("(function one() {                      \n"   // 1
                 "  return (function two() {             \n"   // 2
                 "    return (function three() {         \n"   // 3
                 "      return saveStack();              \n"   // 4
                 "    }());                              \n"   // 5
                 "  }());                                \n"   // 6
                 "}());                                  \n",  // 7
                 FILENAME, 1, &srcVal));

    CHECK(srcVal.isObject());
    JS::RootedObject srcObj(cx, &srcVal.toObject());

    CHECK(srcObj->is<js::SavedFrame>());
    JS::Rooted<js::SavedFrame*> srcFrame(cx, &srcObj->as<js::SavedFrame>());

    CHECK(srcFrame->getPrincipals() == pp->principals);

    JS::RootedValue destVal(cx);
    CHECK(JS_StructuredClone(cx, srcVal, &destVal, nullptr, nullptr));

    CHECK(destVal.isObject());
    JS::RootedObject destObj(cx, &destVal.toObject());

    CHECK(destObj->is<js::SavedFrame>());
    JS::Handle<js::SavedFrame*> destFrame = destObj.as<js::SavedFrame>();

    size_t framesCopied = 0;
    for (JS::Handle<js::SavedFrame*> f :
         js::SavedFrame::RootedRange(cx, destFrame)) {
      framesCopied++;

      CHECK(f != srcFrame);

      if (pp->principals == testPrincipals) {
        // We shouldn't get a pointer to the same
        // StructuredCloneTestPrincipals instance since we should have
        // serialized and then deserialized it into a new instance.
        CHECK(f->getPrincipals() != pp->principals);

        // But it should certainly have the same rank.
        CHECK(StructuredCloneTestPrincipals::getRank(f->getPrincipals()) ==
              StructuredCloneTestPrincipals::getRank(pp->principals));
      } else {
        // For our singleton principals, we should always get the same
        // pointer back.
        CHECK(js::ReconstructedSavedFramePrincipals::is(pp->principals) ||
              pp->principals == nullptr);
        CHECK(f->getPrincipals() == pp->principals);
      }

      CHECK(EqualStrings(f->getSource(), srcFrame->getSource()));
      CHECK(f->getLine() == srcFrame->getLine());
      CHECK(f->getColumn() == srcFrame->getColumn());
      CHECK(EqualStrings(f->getFunctionDisplayName(),
                         srcFrame->getFunctionDisplayName()));

      srcFrame = srcFrame->getParent();
    }

    // Four function frames + one global frame.
    CHECK(framesCopied == 4);
  }

  return true;
}
END_TEST(testStructuredClone_SavedFrame)