summaryrefslogtreecommitdiffstats
path: root/mojo/bindings
diff options
context:
space:
mode:
authorhansmuller@chromium.org <hansmuller@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-25 18:48:57 +0000
committerhansmuller@chromium.org <hansmuller@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-07-25 18:48:57 +0000
commit32c5a54e596d25d904a545df5787abeb5736ca5e (patch)
tree9cbb078039f24f063da477cc75ce7df3f1e931b0 /mojo/bindings
parent5264305d2205771fc7622ce831ee70f18611b770 (diff)
downloadchromium_src-32c5a54e596d25d904a545df5787abeb5736ca5e.zip
chromium_src-32c5a54e596d25d904a545df5787abeb5736ca5e.tar.gz
chromium_src-32c5a54e596d25d904a545df5787abeb5736ca5e.tar.bz2
Validate incoming JS Message Headers: test message parser
This is the first part of the "Validate incoming JS Message Headers" CL - https://codereview.chromium.org/406993002/ It's just the test message file parser and its sanity check. TBR=jochen@chromium.org BUG=395801 Review URL: https://codereview.chromium.org/411553003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@285632 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/bindings')
-rw-r--r--mojo/bindings/js/codec_unittests.js228
-rw-r--r--mojo/bindings/js/core_unittests.js118
-rw-r--r--mojo/bindings/js/run_js_tests.cc64
3 files changed, 0 insertions, 410 deletions
diff --git a/mojo/bindings/js/codec_unittests.js b/mojo/bindings/js/codec_unittests.js
deleted file mode 100644
index e30acb5..0000000
--- a/mojo/bindings/js/codec_unittests.js
+++ /dev/null
@@ -1,228 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-define([
- "gin/test/expect",
- "mojo/public/js/bindings/codec",
- "mojo/public/interfaces/bindings/tests/sample_service.mojom",
- ], function(expect, codec, sample) {
- testBar();
- testFoo();
- testTypes();
- testAlign();
- testUtf8();
- this.result = "PASS";
-
- function testBar() {
- var bar = new sample.Bar();
- bar.alpha = 1;
- bar.beta = 2;
- bar.gamma = 3;
- bar.type = 0x08070605;
- bar.extraProperty = "banana";
-
- var messageName = 42;
- var payloadSize = sample.Bar.encodedSize;
-
- var builder = new codec.MessageBuilder(messageName, payloadSize);
- builder.encodeStruct(sample.Bar, bar);
-
- var message = builder.finish();
-
- var expectedMemory = new Uint8Array([
- 16, 0, 0, 0,
- 2, 0, 0, 0,
- 42, 0, 0, 0,
- 0, 0, 0, 0,
-
- 16, 0, 0, 0,
- 4, 0, 0, 0,
-
- 1, 2, 3, 0,
- 5, 6, 7, 8,
- ]);
-
- var actualMemory = new Uint8Array(message.buffer.arrayBuffer);
- expect(actualMemory).toEqual(expectedMemory);
-
- var reader = new codec.MessageReader(message);
-
- expect(reader.payloadSize).toBe(payloadSize);
- expect(reader.messageName).toBe(messageName);
-
- var bar2 = reader.decodeStruct(sample.Bar);
-
- expect(bar2.alpha).toBe(bar.alpha);
- expect(bar2.beta).toBe(bar.beta);
- expect(bar2.gamma).toBe(bar.gamma);
- expect("extraProperty" in bar2).toBeFalsy();
- }
-
- function testFoo() {
- var foo = new sample.Foo();
- foo.x = 0x212B4D5;
- foo.y = 0x16E93;
- foo.a = 1;
- foo.b = 0;
- foo.c = 3; // This will get truncated to one bit.
- foo.bar = new sample.Bar();
- foo.bar.alpha = 91;
- foo.bar.beta = 82;
- foo.bar.gamma = 73;
- foo.data = [
- 4, 5, 6, 7, 8,
- ];
- foo.extra_bars = [
- new sample.Bar(), new sample.Bar(), new sample.Bar(),
- ];
- for (var i = 0; i < foo.extra_bars.length; ++i) {
- foo.extra_bars[i].alpha = 1 * i;
- foo.extra_bars[i].beta = 2 * i;
- foo.extra_bars[i].gamma = 3 * i;
- }
- foo.name = "I am a banana";
- // This is supposed to be a handle, but we fake it with an integer.
- foo.source = 23423782;
- foo.array_of_array_of_bools = [
- [true], [false, true]
- ];
- foo.array_of_bools = [
- true, false, true, false, true, false, true, true
- ];
-
-
- var messageName = 31;
- var payloadSize = 304;
-
- var builder = new codec.MessageBuilder(messageName, payloadSize);
- builder.encodeStruct(sample.Foo, foo);
-
- var message = builder.finish();
-
- var expectedMemory = new Uint8Array([
- /* 0: */ 16, 0, 0, 0, 2, 0, 0, 0,
- /* 8: */ 31, 0, 0, 0, 0, 0, 0, 0,
- /* 16: */ 96, 0, 0, 0, 15, 0, 0, 0,
- /* 24: */ 0xD5, 0xB4, 0x12, 0x02, 0x93, 0x6E, 0x01, 0,
- /* 32: */ 5, 0, 0, 0, 0, 0, 0, 0,
- /* 40: */ 72, 0, 0, 0, 0, 0, 0, 0,
- ]);
- // TODO(abarth): Test more of the message's raw memory.
- var actualMemory = new Uint8Array(message.buffer.arrayBuffer,
- 0, expectedMemory.length);
- expect(actualMemory).toEqual(expectedMemory);
-
- var expectedHandles = [
- 23423782,
- ];
-
- expect(message.handles).toEqual(expectedHandles);
-
- var reader = new codec.MessageReader(message);
-
- expect(reader.payloadSize).toBe(payloadSize);
- expect(reader.messageName).toBe(messageName);
-
- var foo2 = reader.decodeStruct(sample.Foo);
-
- expect(foo2.x).toBe(foo.x);
- expect(foo2.y).toBe(foo.y);
-
- expect(foo2.a).toBe(foo.a & 1 ? true : false);
- expect(foo2.b).toBe(foo.b & 1 ? true : false);
- expect(foo2.c).toBe(foo.c & 1 ? true : false);
-
- expect(foo2.bar).toEqual(foo.bar);
- expect(foo2.data).toEqual(foo.data);
-
- expect(foo2.extra_bars).toEqual(foo.extra_bars);
- expect(foo2.name).toBe(foo.name);
- expect(foo2.source).toEqual(foo.source);
-
- expect(foo2.array_of_bools).toEqual(foo.array_of_bools);
- }
-
- function testTypes() {
- function encodeDecode(cls, input, expectedResult, encodedSize) {
- var messageName = 42;
- var payloadSize = encodedSize || cls.encodedSize;
-
- var builder = new codec.MessageBuilder(messageName, payloadSize);
- builder.encodeStruct(cls, input)
- var message = builder.finish();
-
- var reader = new codec.MessageReader(message);
- expect(reader.payloadSize).toBe(payloadSize);
- expect(reader.messageName).toBe(messageName);
- var result = reader.decodeStruct(cls);
- expect(result).toEqual(expectedResult);
- }
- encodeDecode(codec.String, "banana", "banana", 24);
- encodeDecode(codec.Int8, -1, -1);
- encodeDecode(codec.Int8, 0xff, -1);
- encodeDecode(codec.Int16, -1, -1);
- encodeDecode(codec.Int16, 0xff, 0xff);
- encodeDecode(codec.Int16, 0xffff, -1);
- encodeDecode(codec.Int32, -1, -1);
- encodeDecode(codec.Int32, 0xffff, 0xffff);
- encodeDecode(codec.Int32, 0xffffffff, -1);
- encodeDecode(codec.Float, 1.0, 1.0);
- encodeDecode(codec.Double, 1.0, 1.0);
- }
-
- function testAlign() {
- var aligned = [
- 0, // 0
- 8, // 1
- 8, // 2
- 8, // 3
- 8, // 4
- 8, // 5
- 8, // 6
- 8, // 7
- 8, // 8
- 16, // 9
- 16, // 10
- 16, // 11
- 16, // 12
- 16, // 13
- 16, // 14
- 16, // 15
- 16, // 16
- 24, // 17
- 24, // 18
- 24, // 19
- 24, // 20
- ];
- for (var i = 0; i < aligned.length; ++i)
- expect(codec.align(i)).toBe(aligned[i]);
- }
-
- function testUtf8() {
- var str = "B\u03ba\u1f79"; // some UCS-2 codepoints
- var messageName = 42;
- var payloadSize = 24;
-
- var builder = new codec.MessageBuilder(messageName, payloadSize);
- var encoder = builder.createEncoder(8);
- encoder.encodeStringPointer(str);
- var message = builder.finish();
- var expectedMemory = new Uint8Array([
- /* 0: */ 16, 0, 0, 0, 2, 0, 0, 0,
- /* 8: */ 42, 0, 0, 0, 0, 0, 0, 0,
- /* 16: */ 8, 0, 0, 0, 0, 0, 0, 0,
- /* 24: */ 14, 0, 0, 0, 6, 0, 0, 0,
- /* 32: */ 0x42, 0xCE, 0xBA, 0xE1, 0xBD, 0xB9, 0, 0,
- ]);
- var actualMemory = new Uint8Array(message.buffer.arrayBuffer);
- expect(actualMemory.length).toEqual(expectedMemory.length);
- expect(actualMemory).toEqual(expectedMemory);
-
- var reader = new codec.MessageReader(message);
- expect(reader.payloadSize).toBe(payloadSize);
- expect(reader.messageName).toBe(messageName);
- var str2 = reader.decoder.decodeStringPointer();
- expect(str2).toEqual(str);
- }
-});
diff --git a/mojo/bindings/js/core_unittests.js b/mojo/bindings/js/core_unittests.js
deleted file mode 100644
index fa6cb4f..0000000
--- a/mojo/bindings/js/core_unittests.js
+++ /dev/null
@@ -1,118 +0,0 @@
-// Copyright 2013 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-define([
- "gin/test/expect",
- "mojo/public/js/bindings/core",
- "gc",
- ], function(expect, core, gc) {
- runWithMessagePipe(testNop);
- runWithMessagePipe(testReadAndWriteMessage);
- runWithMessagePipeWithOptions(testNop);
- runWithMessagePipeWithOptions(testReadAndWriteMessage);
- runWithDataPipe(testNop);
- runWithDataPipe(testReadAndWriteDataPipe);
- runWithDataPipeWithOptions(testNop);
- runWithDataPipeWithOptions(testReadAndWriteDataPipe);
- gc.collectGarbage(); // should not crash
- this.result = "PASS";
-
- function runWithMessagePipe(test) {
- var pipe = core.createMessagePipe();
- expect(pipe.result).toBe(core.RESULT_OK);
-
- test(pipe);
-
- expect(core.close(pipe.handle0)).toBe(core.RESULT_OK);
- expect(core.close(pipe.handle1)).toBe(core.RESULT_OK);
- }
-
- function runWithMessagePipeWithOptions(test) {
- var pipe = core.createMessagePipe({
- flags: core.CREATE_MESSAGE_PIPE_OPTIONS_FLAG_NONE
- });
- expect(pipe.result).toBe(core.RESULT_OK);
-
- test(pipe);
-
- expect(core.close(pipe.handle0)).toBe(core.RESULT_OK);
- expect(core.close(pipe.handle1)).toBe(core.RESULT_OK);
- }
-
- function runWithDataPipe(test) {
- var pipe = core.createDataPipe();
- expect(pipe.result).toBe(core.RESULT_OK);
-
- test(pipe);
-
- expect(core.close(pipe.producerHandle)).toBe(core.RESULT_OK);
- expect(core.close(pipe.consumerHandle)).toBe(core.RESULT_OK);
- }
-
- function runWithDataPipeWithOptions(test) {
- var pipe = core.createDataPipe({
- flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
- elementNumBytes: 1,
- capacityNumBytes: 64
- });
- expect(pipe.result).toBe(core.RESULT_OK);
-
- test(pipe);
-
- expect(core.close(pipe.producerHandle)).toBe(core.RESULT_OK);
- expect(core.close(pipe.consumerHandle)).toBe(core.RESULT_OK);
- }
-
- function testNop(pipe) {
- }
-
- function testReadAndWriteMessage(pipe) {
- var senderData = new Uint8Array(42);
- for (var i = 0; i < senderData.length; ++i) {
- senderData[i] = i * i;
- }
-
- var result = core.writeMessage(
- pipe.handle0, senderData, [],
- core.WRITE_MESSAGE_FLAG_NONE);
-
- expect(result).toBe(core.RESULT_OK);
-
- var read = core.readMessage(
- pipe.handle1, core.READ_MESSAGE_FLAG_NONE);
-
- expect(read.result).toBe(core.RESULT_OK);
- expect(read.buffer.byteLength).toBe(42);
- expect(read.handles.length).toBe(0);
-
- var memory = new Uint8Array(read.buffer);
- for (var i = 0; i < memory.length; ++i)
- expect(memory[i]).toBe((i * i) & 0xFF);
- }
-
- function testReadAndWriteDataPipe(pipe) {
- var senderData = new Uint8Array(42);
- for (var i = 0; i < senderData.length; ++i) {
- senderData[i] = i * i;
- }
-
- var write = core.writeData(
- pipe.producerHandle, senderData,
- core.WRITE_DATA_FLAG_ALL_OR_NONE);
-
- expect(write.result).toBe(core.RESULT_OK);
- expect(write.numBytes).toBe(42);
-
- var read = core.readData(
- pipe.consumerHandle, core.READ_DATA_FLAG_ALL_OR_NONE);
-
- expect(read.result).toBe(core.RESULT_OK);
- expect(read.buffer.byteLength).toBe(42);
-
- var memory = new Uint8Array(read.buffer);
- for (var i = 0; i < memory.length; ++i)
- expect(memory[i]).toBe((i * i) & 0xFF);
- }
-
-});
diff --git a/mojo/bindings/js/run_js_tests.cc b/mojo/bindings/js/run_js_tests.cc
deleted file mode 100644
index 976a0b1..0000000
--- a/mojo/bindings/js/run_js_tests.cc
+++ /dev/null
@@ -1,64 +0,0 @@
-// Copyright 2014 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "base/file_util.h"
-#include "base/files/file_path.h"
-#include "base/path_service.h"
-#include "gin/modules/console.h"
-#include "gin/modules/module_registry.h"
-#include "gin/modules/timer.h"
-#include "gin/test/file_runner.h"
-#include "gin/test/gtest.h"
-#include "mojo/bindings/js/core.h"
-#include "mojo/common/test/test_utils.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace mojo {
-namespace js {
-namespace {
-
-class TestRunnerDelegate : public gin::FileRunnerDelegate {
- public:
- TestRunnerDelegate() {
- AddBuiltinModule(gin::Console::kModuleName, gin::Console::GetModule);
- AddBuiltinModule(Core::kModuleName, Core::GetModule);
- }
-
- private:
- DISALLOW_COPY_AND_ASSIGN(TestRunnerDelegate);
-};
-
-void RunTest(std::string test, bool run_until_idle) {
- base::FilePath path;
- PathService::Get(base::DIR_SOURCE_ROOT, &path);
- path = path.AppendASCII("mojo")
- .AppendASCII("bindings")
- .AppendASCII("js")
- .AppendASCII(test);
- TestRunnerDelegate delegate;
- gin::RunTestFromFile(path, &delegate, run_until_idle);
-}
-
-// TODO(abarth): Should we autogenerate these stubs from GYP?
-TEST(JSTest, core) {
- RunTest("core_unittests.js", true);
-}
-
-TEST(JSTest, codec) {
- // TODO(yzshen): Remove this check once isolated tests are supported on the
- // Chromium waterfall. (http://crbug.com/351214)
- const base::FilePath test_file_path(
- test::GetFilePathForJSResource(
- "mojo/public/interfaces/bindings/tests/sample_service.mojom"));
- if (!base::PathExists(test_file_path)) {
- LOG(WARNING) << "Mojom binding files don't exist. Skipping the test.";
- return;
- }
-
- RunTest("codec_unittests.js", true);
-}
-
-} // namespace
-} // namespace js
-} // namespace mojo