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

Side by Side Diff: mojo/public/c/bindings/tests/bindings_unittest.cc

Issue 1654373002: C types and utilities to validate and access struct and message headers (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 10 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
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
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 <string>
6
7 #include "mojo/public/c/bindings/message.h"
8 #include "mojo/public/c/bindings/struct.h"
9 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
10 #include "mojo/public/cpp/system/macros.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace {
14
15 TEST(CBindingsTest, InvalidStructHeader) {
16 struct TestCase {
17 const char* test_case;
18 uint32_t size;
19 const char* name;
20 };
21
22 TestCase cases[] = {
23 {"", 0u, "zero size"},
24 {"", 4u, "Header too small"},
25 {"[u4]1 // num_bytes\n"
26 "[u4]0 // version",
27 16u, "num_bytes too small"},
28 {"[u4]20 // num_bytes\n"
29 "[u4]0 // version",
30 16u, "num_bytes bigger than buffer"},
31 };
32 for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
33 std::vector<uint8_t> data;
34 std::string parser_error_message;
35 size_t num_handles = 0u;
36 ASSERT_TRUE(mojo::test::ParseValidationTestInput(
37 cases[i].test_case, &data, &num_handles, &parser_error_message))
38 << parser_error_message;
39 EXPECT_FALSE(mojo_validate_struct_header(data.data(), cases[i].size))
40 << cases[i].name;
41 }
42 }
43
44 TEST(CBindingsTest, ValidStructHeader) {
45 struct TestCase {
46 const char* test_case;
47 uint32_t size;
48 uint32_t expected_num_bytes;
49 uint32_t expected_version;
50 };
51
52 TestCase cases[] = {
53 {"[u4]16 [u4]0", 16u, 16u, 0u},
54 {"[u4]40 [u4]0", 40u, 40u, 0u},
55 {"[u4]16 [u4]13", 16u, 16u, 13u},
56 {"[u4]16 // num_bytes\n"
57 "[u4]0 // version",
58 20u, 16u, 0u},
59 };
60
61 for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
62 std::vector<uint8_t> data;
63 std::string parser_error_message;
64 size_t num_handles = 0u;
65 ASSERT_TRUE(mojo::test::ParseValidationTestInput(
66 cases[i].test_case, &data, &num_handles, &parser_error_message))
67 << parser_error_message << " case " << i;
68 EXPECT_TRUE(mojo_validate_struct_header(data.data(), cases[i].size))
69 << " case " << i;
70 mojo_struct_header_t* header =
71 reinterpret_cast<mojo_struct_header_t*>(data.data());
72 EXPECT_EQ(header->num_bytes, cases[i].expected_num_bytes) << " case " << i;
73 EXPECT_EQ(header->version, cases[i].expected_version) << " case " << i;
74 }
75 }
76
77 TEST(CBindingsTest, InvalidMessageHeader) {
78 struct TestCase {
79 const char* test_case;
80 uint32_t size;
81 const char* name;
82 };
83
84 TestCase cases[] = {
85 {"[u4]8 // num_bytes\n"
86 "[u4]0 // version",
87 16u, "num_bytes too small"},
88 {"[u4]24 // num_bytes\n"
89 "[u4]0 // version\n"
90 "[u4]0 // name\n"
91 "[u4]0 // flags\n"
92 "[u8]0 // request id",
93 24u, "version 0 header with version 1 size"},
94 {"[u4]16 // num_bytes\n"
95 "[u4]1 // version\n"
96 "[u4]0 // name\n"
97 "[u4]0 // flags",
98 20u, "version 1 header with version 0 size"},
99 {"[u4]16 // num_bytes\n"
100 "[u4]0 // version\n"
101 "[u4]0 // name\n"
102 "[u4]1 // flags",
103 16u, "version 0 header with expect response flag"},
104 {"[u4]16 // num_bytes\n"
105 "[u4]0 // version\n"
106 "[u4]0 // name\n"
107 "[u4]2 // flags",
108 16u, "version 0 header with is response flag"},
109 {"[u4]16 // num_bytes\n"
110 "[u4]0 // version\n"
111 "[u4]0 // name\n"
112 "[u4]3 // flags",
113 16u, "version 0 header with both is/expects response flags"},
114 {"[u4]24 // num_bytes\n"
115 "[u4]1 // version\n"
116 "[u4]0 // name\n"
117 "[u4]0 // flags\n"
118 "[u8]0 // request id",
119 24u, "version 1 header without is/expects response flags"},
120 {"[u4]24 // num_bytes\n"
121 "[u4]1 // version\n"
122 "[u4]0 // name\n"
123 "[u4]3 // flags\n"
124 "[u8]0 // request id",
125 24u, "version 1 header with both is/expects response flags"},
126 };
127 for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
128 std::vector<uint8_t> data;
129 std::string parser_error_message;
130 size_t num_handles = 0u;
131 ASSERT_TRUE(mojo::test::ParseValidationTestInput(
132 cases[i].test_case, &data, &num_handles, &parser_error_message))
133 << parser_error_message << " case " << i;
134 ASSERT_TRUE(mojo_validate_struct_header(data.data(), cases[i].size))
135 << " case " << i;
136 mojo_struct_header_t* header =
137 reinterpret_cast<mojo_struct_header_t*>(data.data());
138 EXPECT_FALSE(mojo_validate_message_header(header, cases[i].size))
139 << cases[i].name << " case " << i;
140 }
141 }
142
143 TEST(CBindingsTest, ValidMessageHeader) {
144 struct TestCase {
145 const char* test_case;
146 uint32_t size;
147 };
148
149 TestCase cases[] = {
150 {"[u4]16 // num_bytes\n"
151 "[u4]0 // version\n"
152 "[u4]0 // name\n"
153 "[u4]0 // flags",
154 16u}, // version 0 header
155 {"[u4]24 // num_bytes\n"
156 "[u4]1 // version\n"
157 "[u4]0 // name\n"
158 "[u4]1 // flags\n"
159 "[u8]0 // request_id",
160 24u}, // version 1 request
161 {"[u4]24 // num_bytes\n"
162 "[u4]1 // version\n"
163 "[u4]0 // name\n"
164 "[u4]2 // flags\n"
165 "[u8]0 // request_id",
166 24u}, // version 1 response
167 };
168
169 for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
170 std::vector<uint8_t> data;
171 std::string parser_error_message;
172 size_t num_handles = 0u;
173 ASSERT_TRUE(mojo::test::ParseValidationTestInput(
174 cases[i].test_case, &data, &num_handles, &parser_error_message))
175 << parser_error_message << " case " << i;
176 ASSERT_TRUE(mojo_validate_struct_header(data.data(), cases[i].size))
177 << " case " << i;
178 mojo_struct_header_t* header =
179 reinterpret_cast<mojo_struct_header_t*>(data.data());
180 EXPECT_TRUE(mojo_validate_message_header(header, cases[i].size)) << " case "
181 << i;
182 }
183 }
184
185 } // namespace
OLDNEW
« mojo/public/c/bindings/struct.h ('K') | « mojo/public/c/bindings/tests/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698