diff options
author | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-04 00:43:22 +0000 |
---|---|---|
committer | sky@chromium.org <sky@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-04 00:43:22 +0000 |
commit | 15429abf3b71f29b1bb21d493aa0a7220242b39b (patch) | |
tree | 9fd3e1401cf34bb1f634097429913c427e08cadb | |
parent | f9e52b4c6e147209fa625bdd6fc1ce781aa0f912 (diff) | |
download | chromium_src-15429abf3b71f29b1bb21d493aa0a7220242b39b.zip chromium_src-15429abf3b71f29b1bb21d493aa0a7220242b39b.tar.gz chromium_src-15429abf3b71f29b1bb21d493aa0a7220242b39b.tar.bz2 |
Adds a generator for writing messages to disk
The plan with this is to use the generator to create binary test
files. We'll write bindings tests that read these files and verify
everything is sane.
BUG=none
TEST=none
R=darin@chromium.org
Review URL: https://codereview.chromium.org/191293018
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@261605 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | mojo/mojo.gyp | 17 | ||||
-rw-r--r-- | mojo/public/data/bindings/tests/message_data | 25 | ||||
-rw-r--r-- | mojo/tools/message_generator.cc | 63 |
3 files changed, 105 insertions, 0 deletions
diff --git a/mojo/mojo.gyp b/mojo/mojo.gyp index caf318d..ed9658b 100644 --- a/mojo/mojo.gyp +++ b/mojo/mojo.gyp @@ -35,6 +35,7 @@ 'mojo_common_unittests', 'mojo_js', 'mojo_js_unittests', + 'mojo_message_generator', 'mojo_pepper_container_app', 'mojo_public_test_utils', 'mojo_public_bindings_unittests', @@ -518,6 +519,22 @@ 'bindings/js/run_js_tests.cc', ], }, + { + 'target_name': 'mojo_message_generator', + 'type': 'executable', + 'dependencies': [ + '../base/base.gyp:base', + '../testing/gtest.gyp:gtest', + 'mojo_bindings', + 'mojo_common_lib', + 'mojo_environment_chromium', + 'mojo_system', + 'mojo_system_impl', + ], + 'sources': [ + 'tools/message_generator.cc', + ], + }, ], 'conditions': [ ['OS=="android"', { diff --git a/mojo/public/data/bindings/tests/message_data b/mojo/public/data/bindings/tests/message_data new file mode 100644 index 0000000..b288878 --- /dev/null +++ b/mojo/public/data/bindings/tests/message_data @@ -0,0 +1,25 @@ +// File generated by mojo_message_generator. +0X10 +0X00 +0X00 +0X00 +0X02 +0X00 +0X00 +0X00 +0X15 +0X00 +0X00 +0X00 +0X00 +0X00 +0X00 +0X00 +0X09 +0X08 +0X07 +0X06 +0X00 +0X00 +0X00 +0X00 diff --git a/mojo/tools/message_generator.cc b/mojo/tools/message_generator.cc new file mode 100644 index 0000000..86fcab2 --- /dev/null +++ b/mojo/tools/message_generator.cc @@ -0,0 +1,63 @@ +// 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/strings/string_util.h" +#include "base/strings/stringprintf.h" +#include "mojo/public/cpp/bindings/lib/message_builder.h" +#include "mojo/public/cpp/bindings/lib/message_internal.h" +#include "mojo/public/cpp/bindings/message.h" + +// This file is used to generate various files corresponding to mojo +// messages. The various binding implementations can parse these to verify they +// correctly decode messages. +// +// The output consists of each byte of the message encoded in a hex string with +// a newline after it. +namespace mojo { +namespace { + +std::string BinaryToHex(const base::StringPiece& piece) { + std::string result("// File generated by mojo_message_generator.\n");; + result.reserve(result.size() + (piece.size() * 5)); + for (size_t i = 0; i < piece.size(); ++i) + base::StringAppendF(&result, "0X%.2X\n", static_cast<int>(piece.data()[i])); + return result; +} + +void WriteMessageToFile(const Message& message, const base::FilePath& path) { + const std::string hex_message(BinaryToHex( + base::StringPiece(reinterpret_cast<const char*>(message.data()), + message.data_num_bytes()))); + CHECK_EQ(static_cast<int>(hex_message.size()), + base::WriteFile(path, hex_message.data(), + static_cast<int>(hex_message.size()))); +} + +// Generates a message of type MessageData. The message uses the name 21, +// with 4 bytes of payload: 0x9, 0x8, 0x7, 0x6. +void GenerateMessageDataMessage() { + internal::MessageBuilder builder(static_cast<uint32_t>(21), + static_cast<size_t>(4)); + char* data = static_cast<char*>(builder.buffer()->Allocate(4)); + DCHECK(data); + data[0] = 9; + data[1] = 8; + data[2] = 7; + data[3] = 6; + + Message message; + builder.Finish(&message); + WriteMessageToFile(message, + base::FilePath(FILE_PATH_LITERAL("message_data"))); +} + +} // namespace +} // namespace mojo + +int main(int argc, char** argv) { + mojo::GenerateMessageDataMessage(); + return 0; +} |