Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Unified Diff: mojo/public/bindings/tests/message_generator.cc

Issue 191293018: Adds a generator for writing messages to disk (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: common_lib Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/bindings/tests/DEPS ('k') | mojo/test/data/messages/message_data » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/bindings/tests/message_generator.cc
diff --git a/mojo/public/bindings/tests/message_generator.cc b/mojo/public/bindings/tests/message_generator.cc
new file mode 100644
index 0000000000000000000000000000000000000000..171a62cbfedf20bcee8ddfd027d8d0521b01da95
--- /dev/null
+++ b/mojo/public/bindings/tests/message_generator.cc
@@ -0,0 +1,63 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
viettrungluu 2014/04/01 22:02:50 Three things: - The contents of mojo/public/bindin
viettrungluu 2014/04/01 22:11:43 On second thought, maybe it should be under public
+// 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_number_conversions.h"
+#include "base/strings/string_piece.h"
+#include "base/strings/string_util.h"
+#include "mojo/public/bindings/lib/message_builder.h"
+#include "mojo/public/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;
+ result.reserve(piece.size() * 5);
+ for (size_t i = 0; i < piece.size(); ++i) {
+ result.append("0X");
viettrungluu 2014/04/01 22:11:43 Also, I suggest just using StringAppendF. (As a s
+ result.append(base::HexEncode(piece.data() + i, 1));
+ result.push_back('\n');
+ }
+ return result;
+}
+
+// Generates a message of type MessageData. The message uses the name 21,
+// with 4 bytes of payload: 0x9, 0x8, 0x7, 0x6.
+void GenerateMessageDataMessage(const base::FilePath& path) {
viettrungluu 2014/04/01 22:11:43 I think this should be factored into several piece
+ 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);
+ std::string string_message;
+ 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(), hex_message.size()));
+}
+
+} // namespace
+} // namespace mojo
+
+int main(int argc, char** argv) {
+ mojo::GenerateMessageDataMessage(
+ base::FilePath(FILE_PATH_LITERAL("message_data.txt")));
+ return 0;
+}
« no previous file with comments | « mojo/public/bindings/tests/DEPS ('k') | mojo/test/data/messages/message_data » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698