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

Side by Side 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, 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « mojo/public/bindings/tests/DEPS ('k') | mojo/test/data/messages/message_data » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // 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
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/file_util.h"
6 #include "base/files/file_path.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/string_piece.h"
9 #include "base/strings/string_util.h"
10 #include "mojo/public/bindings/lib/message_builder.h"
11 #include "mojo/public/bindings/lib/message_internal.h"
12 #include "mojo/public/cpp/bindings/message.h"
13
14 // This file is used to generate various files corresponding to mojo
15 // messages. The various binding implementations can parse these to verify they
16 // correctly decode messages.
17 //
18 // The output consists of each byte of the message encoded in a hex string with
19 // a newline after it.
20 namespace mojo {
21 namespace {
22
23 std::string BinaryToHex(const base::StringPiece& piece) {
24 std::string result;
25 result.reserve(piece.size() * 5);
26 for (size_t i = 0; i < piece.size(); ++i) {
27 result.append("0X");
viettrungluu 2014/04/01 22:11:43 Also, I suggest just using StringAppendF. (As a s
28 result.append(base::HexEncode(piece.data() + i, 1));
29 result.push_back('\n');
30 }
31 return result;
32 }
33
34 // Generates a message of type MessageData. The message uses the name 21,
35 // with 4 bytes of payload: 0x9, 0x8, 0x7, 0x6.
36 void GenerateMessageDataMessage(const base::FilePath& path) {
viettrungluu 2014/04/01 22:11:43 I think this should be factored into several piece
37 internal::MessageBuilder builder(static_cast<uint32_t>(21),
38 static_cast<size_t>(4));
39 char* data = static_cast<char*>(builder.buffer()->Allocate(4));
40 DCHECK(data);
41 data[0] = 9;
42 data[1] = 8;
43 data[2] = 7;
44 data[3] = 6;
45
46 Message message;
47 builder.Finish(&message);
48 std::string string_message;
49 const std::string hex_message(BinaryToHex(
50 base::StringPiece(reinterpret_cast<const char*>(message.data()),
51 message.data_num_bytes())));
52 CHECK_EQ(static_cast<int>(hex_message.size()),
53 base::WriteFile(path, hex_message.data(), hex_message.size()));
54 }
55
56 } // namespace
57 } // namespace mojo
58
59 int main(int argc, char** argv) {
60 mojo::GenerateMessageDataMessage(
61 base::FilePath(FILE_PATH_LITERAL("message_data.txt")));
62 return 0;
63 }
OLDNEW
« 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