js/src/jsapi-tests/testMappedArrayBuffer.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 */
michael@0 4
michael@0 5 #ifdef XP_UNIX
michael@0 6 #include <fcntl.h>
michael@0 7 #include <stdio.h>
michael@0 8 #include <string.h>
michael@0 9 #include <sys/stat.h>
michael@0 10 #include <sys/types.h>
michael@0 11 #include <unistd.h>
michael@0 12
michael@0 13 #include "jsfriendapi.h"
michael@0 14 #include "js/StructuredClone.h"
michael@0 15 #include "jsapi-tests/tests.h"
michael@0 16 #include "vm/ArrayBufferObject.h"
michael@0 17
michael@0 18 const char test_data[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
michael@0 19 const char test_filename[] = "temp-bug945152_MappedArrayBuffer";
michael@0 20
michael@0 21 BEGIN_TEST(testMappedArrayBuffer_bug945152)
michael@0 22 {
michael@0 23 TempFile test_file;
michael@0 24 FILE *test_stream = test_file.open(test_filename);
michael@0 25 CHECK(fputs(test_data, test_stream) != EOF);
michael@0 26 test_file.close();
michael@0 27
michael@0 28 // Offset 0.
michael@0 29 CHECK(TestCreateObject(0, 12));
michael@0 30
michael@0 31 // Aligned offset.
michael@0 32 CHECK(TestCreateObject(8, 12));
michael@0 33
michael@0 34 // Unaligned offset.
michael@0 35 CHECK(CreateNewObject(11, 12) == nullptr);
michael@0 36
michael@0 37 // Offset + length greater than file size.
michael@0 38 CHECK(CreateNewObject(8, sizeof(test_data) - 7) == nullptr);
michael@0 39
michael@0 40 // Release the mapped content.
michael@0 41 CHECK(TestReleaseContents());
michael@0 42
michael@0 43 // Neuter mapped array buffer.
michael@0 44 CHECK(TestNeuterObject());
michael@0 45
michael@0 46 // Clone mapped array buffer.
michael@0 47 CHECK(TestCloneObject());
michael@0 48
michael@0 49 // Steal mapped array buffer contents.
michael@0 50 CHECK(TestStealContents());
michael@0 51
michael@0 52 // Transfer mapped array buffer contents.
michael@0 53 CHECK(TestTransferObject());
michael@0 54
michael@0 55 test_file.remove();
michael@0 56
michael@0 57 return true;
michael@0 58 }
michael@0 59
michael@0 60 JSObject *CreateNewObject(const int offset, const int length)
michael@0 61 {
michael@0 62 int fd = open(test_filename, O_RDONLY);
michael@0 63 void *ptr = JS_CreateMappedArrayBufferContents(fd, offset, length);
michael@0 64 close(fd);
michael@0 65 if (!ptr)
michael@0 66 return nullptr;
michael@0 67 JSObject *obj = JS_NewMappedArrayBufferWithContents(cx, length, ptr);
michael@0 68 if (!obj) {
michael@0 69 JS_ReleaseMappedArrayBufferContents(ptr, length);
michael@0 70 return nullptr;
michael@0 71 }
michael@0 72 return obj;
michael@0 73 }
michael@0 74
michael@0 75 bool VerifyObject(JS::HandleObject obj, const int offset, const int length, const bool mapped)
michael@0 76 {
michael@0 77 CHECK(obj);
michael@0 78 CHECK(JS_IsArrayBufferObject(obj));
michael@0 79 CHECK_EQUAL(JS_GetArrayBufferByteLength(obj), length);
michael@0 80 if (mapped)
michael@0 81 CHECK(JS_IsMappedArrayBufferObject(obj));
michael@0 82 else
michael@0 83 CHECK(!JS_IsMappedArrayBufferObject(obj));
michael@0 84 const char *data = reinterpret_cast<const char *>(JS_GetArrayBufferData(obj));
michael@0 85 CHECK(data);
michael@0 86 CHECK(memcmp(data, test_data + offset, length) == 0);
michael@0 87
michael@0 88 return true;
michael@0 89 }
michael@0 90
michael@0 91 bool TestCreateObject(const int offset, const int length)
michael@0 92 {
michael@0 93 JS::RootedObject obj(cx, CreateNewObject(offset, length));
michael@0 94 CHECK(VerifyObject(obj, offset, length, true));
michael@0 95
michael@0 96 return true;
michael@0 97 }
michael@0 98
michael@0 99 bool TestReleaseContents()
michael@0 100 {
michael@0 101 int fd = open(test_filename, O_RDONLY);
michael@0 102 void *ptr = JS_CreateMappedArrayBufferContents(fd, 0, 12);
michael@0 103 close(fd);
michael@0 104 if (!ptr)
michael@0 105 return false;
michael@0 106 JS_ReleaseMappedArrayBufferContents(ptr, 12);
michael@0 107
michael@0 108 return true;
michael@0 109 }
michael@0 110
michael@0 111 bool TestNeuterObject()
michael@0 112 {
michael@0 113 JS::RootedObject obj(cx, CreateNewObject(8, 12));
michael@0 114 CHECK(obj);
michael@0 115 JS_NeuterArrayBuffer(cx, obj, ChangeData);
michael@0 116 CHECK(isNeutered(obj));
michael@0 117
michael@0 118 return true;
michael@0 119 }
michael@0 120
michael@0 121 bool TestCloneObject()
michael@0 122 {
michael@0 123 JS::RootedObject obj1(cx, CreateNewObject(8, 12));
michael@0 124 CHECK(obj1);
michael@0 125 JSAutoStructuredCloneBuffer cloned_buffer;
michael@0 126 JS::RootedValue v1(cx, OBJECT_TO_JSVAL(obj1));
michael@0 127 const JSStructuredCloneCallbacks *callbacks = js::GetContextStructuredCloneCallbacks(cx);
michael@0 128 CHECK(cloned_buffer.write(cx, v1, callbacks, nullptr));
michael@0 129 JS::RootedValue v2(cx);
michael@0 130 CHECK(cloned_buffer.read(cx, &v2, callbacks, nullptr));
michael@0 131 JS::RootedObject obj2(cx, JSVAL_TO_OBJECT(v2));
michael@0 132 CHECK(VerifyObject(obj2, 8, 12, false));
michael@0 133
michael@0 134 return true;
michael@0 135 }
michael@0 136
michael@0 137 bool TestStealContents()
michael@0 138 {
michael@0 139 JS::RootedObject obj(cx, CreateNewObject(8, 12));
michael@0 140 CHECK(obj);
michael@0 141 void *contents = JS_StealArrayBufferContents(cx, obj);
michael@0 142 CHECK(contents);
michael@0 143 CHECK(memcmp(contents, test_data + 8, 12) == 0);
michael@0 144 CHECK(isNeutered(obj));
michael@0 145
michael@0 146 return true;
michael@0 147 }
michael@0 148
michael@0 149 bool TestTransferObject()
michael@0 150 {
michael@0 151 JS::RootedObject obj1(cx, CreateNewObject(8, 12));
michael@0 152 CHECK(obj1);
michael@0 153 JS::RootedValue v1(cx, OBJECT_TO_JSVAL(obj1));
michael@0 154
michael@0 155 // Create an Array of transferable values.
michael@0 156 JS::AutoValueVector argv(cx);
michael@0 157 argv.append(v1);
michael@0 158 JS::RootedObject obj(cx, JS_NewArrayObject(cx, JS::HandleValueArray::subarray(argv, 0, 1)));
michael@0 159 CHECK(obj);
michael@0 160 JS::RootedValue transferable(cx, OBJECT_TO_JSVAL(obj));
michael@0 161
michael@0 162 JSAutoStructuredCloneBuffer cloned_buffer;
michael@0 163 const JSStructuredCloneCallbacks *callbacks = js::GetContextStructuredCloneCallbacks(cx);
michael@0 164 CHECK(cloned_buffer.write(cx, v1, transferable, callbacks, nullptr));
michael@0 165 JS::RootedValue v2(cx);
michael@0 166 CHECK(cloned_buffer.read(cx, &v2, callbacks, nullptr));
michael@0 167 JS::RootedObject obj2(cx, JSVAL_TO_OBJECT(v2));
michael@0 168 CHECK(VerifyObject(obj2, 8, 12, true));
michael@0 169 CHECK(isNeutered(obj1));
michael@0 170
michael@0 171 return true;
michael@0 172 }
michael@0 173
michael@0 174 bool isNeutered(JS::HandleObject obj)
michael@0 175 {
michael@0 176 JS::RootedValue v(cx);
michael@0 177 return JS_GetProperty(cx, obj, "byteLength", &v) && v.toInt32() == 0;
michael@0 178 }
michael@0 179
michael@0 180 static void GC(JSContext *cx)
michael@0 181 {
michael@0 182 JS_GC(JS_GetRuntime(cx));
michael@0 183 // Trigger another to wait for background finalization to end.
michael@0 184 JS_GC(JS_GetRuntime(cx));
michael@0 185 }
michael@0 186
michael@0 187 END_TEST(testMappedArrayBuffer_bug945152)
michael@0 188 #endif

mercurial