Commit b072eceb authored by tsepez@chromium.org's avatar tsepez@chromium.org

Pass data down pipes in mojo fuzzing tests.

Also try to touch (potentially corrupt) arrays, but do not insist on specific
values being present.

I also tidy up the JS a bit, getting rid of some underscores and double-quotes
that crept in despite what the style guide says.

R=viettrungluu@chromium.org

Review URL: https://codereview.chromium.org/267343006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269955 0039d316-1c4b-4281-b951-d872f2087c98
parent 61f91832
...@@ -20,6 +20,11 @@ ...@@ -20,6 +20,11 @@
namespace mojo { namespace mojo {
namespace js { namespace js {
// Global value updated by some checks to prevent compilers from optimizing
// reads out of existence.
uint32 g_waste_accumulator = 0;
namespace { namespace {
// Negative numbers with different values in each byte, the last of // Negative numbers with different values in each byte, the last of
...@@ -108,6 +113,30 @@ void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) { ...@@ -108,6 +113,30 @@ void CheckSampleEchoArgs(const js_to_cpp::EchoArgs& arg) {
EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>()); EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>());
} }
void CheckDataPipe(MojoHandle data_pipe_handle) {
char buffer[100];
uint32_t buffer_size = static_cast<uint32_t>(sizeof(buffer));
MojoResult result = MojoReadData(
data_pipe_handle, buffer, &buffer_size, MOJO_READ_DATA_FLAG_NONE);
EXPECT_EQ(MOJO_RESULT_OK, result);
EXPECT_EQ(64u, buffer_size);
for (int i = 0; i < 64; ++i) {
EXPECT_EQ(i, buffer[i]);
}
}
void CheckCorruptedString(const mojo::String& arg) {
// The values don't matter so long as all accesses are within bounds.
std::string name = arg.To<std::string>();
for (size_t i = 0; i < name.length(); ++i)
g_waste_accumulator += name[i];
}
void CheckCorruptedStringArray(const mojo::Array<mojo::String>& string_array) {
for (size_t i = 0; i < string_array.size(); ++i)
CheckCorruptedString(string_array[i]);
}
// Base Provider implementation class. It's expected that tests subclass and // Base Provider implementation class. It's expected that tests subclass and
// override the appropriate Provider functions. When test is done quit the // override the appropriate Provider functions. When test is done quit the
// run_loop(). // run_loop().
...@@ -203,6 +232,7 @@ class EchoCppSideConnection : public CppSideConnection { ...@@ -203,6 +232,7 @@ class EchoCppSideConnection : public CppSideConnection {
EXPECT_EQ(-1, arg2.si16()); EXPECT_EQ(-1, arg2.si16());
EXPECT_EQ(-1, arg2.si8()); EXPECT_EQ(-1, arg2.si8());
EXPECT_EQ(std::string("going"), arg2.name().To<std::string>()); EXPECT_EQ(std::string("going"), arg2.name().To<std::string>());
CheckDataPipe(arg2.data_handle().get().value());
} }
virtual void TestFinished() OVERRIDE { virtual void TestFinished() OVERRIDE {
...@@ -233,8 +263,13 @@ class BitFlipCppSideConnection : public CppSideConnection { ...@@ -233,8 +263,13 @@ class BitFlipCppSideConnection : public CppSideConnection {
js_side_->BitFlip(BuildSampleEchoArgs()); js_side_->BitFlip(BuildSampleEchoArgs());
} }
virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg) OVERRIDE {
// TODO(tsepez): How to check, may be corrupt in various ways. if (arg.is_null())
return;
CheckCorruptedString(arg.name());
CheckCorruptedStringArray(arg.string_array());
if (arg.data_handle().is_valid())
CheckDataPipe(arg.data_handle().get().value());
} }
virtual void TestFinished() OVERRIDE { virtual void TestFinished() OVERRIDE {
......
...@@ -2,13 +2,15 @@ ...@@ -2,13 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
define("mojo/apps/js/test/js_to_cpp_unittest", [ define('mojo/apps/js/test/js_to_cpp_unittest', [
"mojo/apps/js/test/js_to_cpp.mojom", 'console',
"mojo/public/js/bindings/connection", 'mojo/apps/js/test/js_to_cpp.mojom',
"mojo/public/js/bindings/connector", 'mojo/public/js/bindings/connection',
"mojo/public/js/bindings/core", 'mojo/public/js/bindings/connector',
], function (jsToCpp, connection, connector, core) { 'mojo/public/js/bindings/core',
], function (console, jsToCpp, connection, connector, core) {
var retainedConnection; var retainedConnection;
var senderData;
var BAD_VALUE = 13; var BAD_VALUE = 13;
var DATA_PIPE_PARAMS = { var DATA_PIPE_PARAMS = {
flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE, flags: core.CREATE_DATA_PIPE_OPTIONS_FLAG_NONE,
...@@ -29,11 +31,11 @@ define("mojo/apps/js/test/js_to_cpp_unittest", [ ...@@ -29,11 +31,11 @@ define("mojo/apps/js/test/js_to_cpp_unittest", [
JsSideConnection.prototype.echo = function (numIterations, arg) { JsSideConnection.prototype.echo = function (numIterations, arg) {
var arg2; var arg2;
var data_pipe1; var dataPipe1;
var data_pipe2; var dataPipe2;
var i; var i;
var message_pipe1; var messagePipe1;
var message_pipe2; var messagePipe2;
// Ensure expected negative values are negative. // Ensure expected negative values are negative.
if (arg.si64 > 0) if (arg.si64 > 0)
...@@ -49,39 +51,42 @@ define("mojo/apps/js/test/js_to_cpp_unittest", [ ...@@ -49,39 +51,42 @@ define("mojo/apps/js/test/js_to_cpp_unittest", [
arg.si8 = BAD_VALUE; arg.si8 = BAD_VALUE;
for (i = 0; i < numIterations; ++i) { for (i = 0; i < numIterations; ++i) {
data_pipe1 = core.createDataPipe(DATA_PIPE_PARAMS); dataPipe1 = core.createDataPipe(DATA_PIPE_PARAMS);
data_pipe2 = core.createDataPipe(DATA_PIPE_PARAMS); dataPipe2 = core.createDataPipe(DATA_PIPE_PARAMS);
message_pipe1 = core.createMessagePipe(); messagePipe1 = core.createMessagePipe();
message_pipe2 = core.createMessagePipe(); messagePipe2 = core.createMessagePipe();
arg.data_handle = data_pipe1.consumerHandle; arg.data_handle = dataPipe1.consumerHandle;
arg.message_handle = message_pipe1.handle1; arg.message_handle = messagePipe1.handle1;
arg2 = new jsToCpp.EchoArgs(); arg2 = new jsToCpp.EchoArgs();
arg2.si64 = -1; arg2.si64 = -1;
arg2.si32 = -1; arg2.si32 = -1;
arg2.si16 = -1; arg2.si16 = -1;
arg2.si8 = -1; arg2.si8 = -1;
arg2.name = "going"; arg2.name = 'going';
arg2.data_handle = data_pipe2.consumerHandle; arg2.data_handle = dataPipe2.consumerHandle;
arg2.message_handle = message_pipe2.handle1; arg2.message_handle = messagePipe2.handle1;
writeDataPipe(dataPipe1, senderData);
writeDataPipe(dataPipe2, senderData);
this.cppSide_.echoResponse(arg, arg2); this.cppSide_.echoResponse(arg, arg2);
core.close(data_pipe1.producerHandle); core.close(dataPipe1.producerHandle);
core.close(data_pipe2.producerHandle); core.close(dataPipe2.producerHandle);
core.close(message_pipe1.handle0); core.close(messagePipe1.handle0);
core.close(message_pipe2.handle0); core.close(messagePipe2.handle0);
} }
this.cppSide_.testFinished(); this.cppSide_.testFinished();
}; };
JsSideConnection.prototype.bitFlip = function (arg) { JsSideConnection.prototype.bitFlip = function (arg) {
var iteration = 0; var iteration = 0;
var data_pipe; var dataPipe;
var message_pipe; var messagePipe;
var stopSignalled = false; var stopSignalled = false;
var proto = connector.Connector.prototype; var proto = connector.Connector.prototype;
proto.realAccept = proto.accept; proto.realAccept = proto.accept;
proto.accept = function (message) { proto.accept = function (message) {
var offset = iteration / 8; var offset = iteration / 8;
...@@ -96,22 +101,45 @@ define("mojo/apps/js/test/js_to_cpp_unittest", [ ...@@ -96,22 +101,45 @@ define("mojo/apps/js/test/js_to_cpp_unittest", [
stopSignalled = true; stopSignalled = true;
return false; return false;
}; };
while (!stopSignalled) { while (!stopSignalled) {
data_pipe = core.createDataPipe(DATA_PIPE_PARAMS); dataPipe = core.createDataPipe(DATA_PIPE_PARAMS);
message_pipe = core.createMessagePipe(); messagePipe = core.createMessagePipe();
arg.data_handle = data_pipe.consumerHandle; writeDataPipe(dataPipe, senderData);
arg.message_handle = message_pipe.handle1; arg.data_handle = dataPipe.consumerHandle;
arg.message_handle = messagePipe.handle1;
this.cppSide_.bitFlipResponse(arg); this.cppSide_.bitFlipResponse(arg);
core.close(data_pipe.producerHandle); core.close(dataPipe.producerHandle);
core.close(message_pipe.handle0); core.close(messagePipe.handle0);
iteration += 1; iteration += 1;
} }
proto.accept = proto.realAccept; proto.accept = proto.realAccept;
proto.realAccept = null; proto.realAccept = null;
this.cppSide_.testFinished(); this.cppSide_.testFinished();
}; };
function writeDataPipe(pipe, data) {
var writeResult = core.writeData(
pipe.producerHandle, data, core.WRITE_DATA_FLAG_ALL_OR_NONE);
if (writeResult.result != core.RESULT_OK) {
console.log('ERROR: Write result was ' + writeResult.result);
return false;
}
if (writeResult.numBytes != data.length) {
console.log('ERROR: Write length was ' + writeResult.numBytes);
return false;
}
return true;
}
return function(handle) { return function(handle) {
var i;
senderData = new Uint8Array(DATA_PIPE_PARAMS.capacityNumBytes);
for (i = 0; i < senderData.length; ++i)
senderData[i] = i;
retainedConnection = new connection.Connection(handle, JsSideConnection, retainedConnection = new connection.Connection(handle, JsSideConnection,
jsToCpp.CppSideProxy); jsToCpp.CppSideProxy);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment