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

Unified 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, 11 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
« mojo/public/c/bindings/struct.h ('K') | « mojo/public/c/bindings/tests/BUILD.gn ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/c/bindings/tests/bindings_unittest.cc
diff --git a/mojo/public/c/bindings/tests/bindings_unittest.cc b/mojo/public/c/bindings/tests/bindings_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f30b9666ce97433cc0f940e3acb3e05e744df317
--- /dev/null
+++ b/mojo/public/c/bindings/tests/bindings_unittest.cc
@@ -0,0 +1,185 @@
+// Copyright 2016 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 <string>
+
+#include "mojo/public/c/bindings/message.h"
+#include "mojo/public/c/bindings/struct.h"
+#include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"
+#include "mojo/public/cpp/system/macros.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+TEST(CBindingsTest, InvalidStructHeader) {
+ struct TestCase {
+ const char* test_case;
+ uint32_t size;
+ const char* name;
+ };
+
+ TestCase cases[] = {
+ {"", 0u, "zero size"},
+ {"", 4u, "Header too small"},
+ {"[u4]1 // num_bytes\n"
+ "[u4]0 // version",
+ 16u, "num_bytes too small"},
+ {"[u4]20 // num_bytes\n"
+ "[u4]0 // version",
+ 16u, "num_bytes bigger than buffer"},
+ };
+ for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
+ std::vector<uint8_t> data;
+ std::string parser_error_message;
+ size_t num_handles = 0u;
+ ASSERT_TRUE(mojo::test::ParseValidationTestInput(
+ cases[i].test_case, &data, &num_handles, &parser_error_message))
+ << parser_error_message;
+ EXPECT_FALSE(mojo_validate_struct_header(data.data(), cases[i].size))
+ << cases[i].name;
+ }
+}
+
+TEST(CBindingsTest, ValidStructHeader) {
+ struct TestCase {
+ const char* test_case;
+ uint32_t size;
+ uint32_t expected_num_bytes;
+ uint32_t expected_version;
+ };
+
+ TestCase cases[] = {
+ {"[u4]16 [u4]0", 16u, 16u, 0u},
+ {"[u4]40 [u4]0", 40u, 40u, 0u},
+ {"[u4]16 [u4]13", 16u, 16u, 13u},
+ {"[u4]16 // num_bytes\n"
+ "[u4]0 // version",
+ 20u, 16u, 0u},
+ };
+
+ for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
+ std::vector<uint8_t> data;
+ std::string parser_error_message;
+ size_t num_handles = 0u;
+ ASSERT_TRUE(mojo::test::ParseValidationTestInput(
+ cases[i].test_case, &data, &num_handles, &parser_error_message))
+ << parser_error_message << " case " << i;
+ EXPECT_TRUE(mojo_validate_struct_header(data.data(), cases[i].size))
+ << " case " << i;
+ mojo_struct_header_t* header =
+ reinterpret_cast<mojo_struct_header_t*>(data.data());
+ EXPECT_EQ(header->num_bytes, cases[i].expected_num_bytes) << " case " << i;
+ EXPECT_EQ(header->version, cases[i].expected_version) << " case " << i;
+ }
+}
+
+TEST(CBindingsTest, InvalidMessageHeader) {
+ struct TestCase {
+ const char* test_case;
+ uint32_t size;
+ const char* name;
+ };
+
+ TestCase cases[] = {
+ {"[u4]8 // num_bytes\n"
+ "[u4]0 // version",
+ 16u, "num_bytes too small"},
+ {"[u4]24 // num_bytes\n"
+ "[u4]0 // version\n"
+ "[u4]0 // name\n"
+ "[u4]0 // flags\n"
+ "[u8]0 // request id",
+ 24u, "version 0 header with version 1 size"},
+ {"[u4]16 // num_bytes\n"
+ "[u4]1 // version\n"
+ "[u4]0 // name\n"
+ "[u4]0 // flags",
+ 20u, "version 1 header with version 0 size"},
+ {"[u4]16 // num_bytes\n"
+ "[u4]0 // version\n"
+ "[u4]0 // name\n"
+ "[u4]1 // flags",
+ 16u, "version 0 header with expect response flag"},
+ {"[u4]16 // num_bytes\n"
+ "[u4]0 // version\n"
+ "[u4]0 // name\n"
+ "[u4]2 // flags",
+ 16u, "version 0 header with is response flag"},
+ {"[u4]16 // num_bytes\n"
+ "[u4]0 // version\n"
+ "[u4]0 // name\n"
+ "[u4]3 // flags",
+ 16u, "version 0 header with both is/expects response flags"},
+ {"[u4]24 // num_bytes\n"
+ "[u4]1 // version\n"
+ "[u4]0 // name\n"
+ "[u4]0 // flags\n"
+ "[u8]0 // request id",
+ 24u, "version 1 header without is/expects response flags"},
+ {"[u4]24 // num_bytes\n"
+ "[u4]1 // version\n"
+ "[u4]0 // name\n"
+ "[u4]3 // flags\n"
+ "[u8]0 // request id",
+ 24u, "version 1 header with both is/expects response flags"},
+ };
+ for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
+ std::vector<uint8_t> data;
+ std::string parser_error_message;
+ size_t num_handles = 0u;
+ ASSERT_TRUE(mojo::test::ParseValidationTestInput(
+ cases[i].test_case, &data, &num_handles, &parser_error_message))
+ << parser_error_message << " case " << i;
+ ASSERT_TRUE(mojo_validate_struct_header(data.data(), cases[i].size))
+ << " case " << i;
+ mojo_struct_header_t* header =
+ reinterpret_cast<mojo_struct_header_t*>(data.data());
+ EXPECT_FALSE(mojo_validate_message_header(header, cases[i].size))
+ << cases[i].name << " case " << i;
+ }
+}
+
+TEST(CBindingsTest, ValidMessageHeader) {
+ struct TestCase {
+ const char* test_case;
+ uint32_t size;
+ };
+
+ TestCase cases[] = {
+ {"[u4]16 // num_bytes\n"
+ "[u4]0 // version\n"
+ "[u4]0 // name\n"
+ "[u4]0 // flags",
+ 16u}, // version 0 header
+ {"[u4]24 // num_bytes\n"
+ "[u4]1 // version\n"
+ "[u4]0 // name\n"
+ "[u4]1 // flags\n"
+ "[u8]0 // request_id",
+ 24u}, // version 1 request
+ {"[u4]24 // num_bytes\n"
+ "[u4]1 // version\n"
+ "[u4]0 // name\n"
+ "[u4]2 // flags\n"
+ "[u8]0 // request_id",
+ 24u}, // version 1 response
+ };
+
+ for (size_t i = 0u; i < MOJO_ARRAYSIZE(cases); ++i) {
+ std::vector<uint8_t> data;
+ std::string parser_error_message;
+ size_t num_handles = 0u;
+ ASSERT_TRUE(mojo::test::ParseValidationTestInput(
+ cases[i].test_case, &data, &num_handles, &parser_error_message))
+ << parser_error_message << " case " << i;
+ ASSERT_TRUE(mojo_validate_struct_header(data.data(), cases[i].size))
+ << " case " << i;
+ mojo_struct_header_t* header =
+ reinterpret_cast<mojo_struct_header_t*>(data.data());
+ EXPECT_TRUE(mojo_validate_message_header(header, cases[i].size)) << " case "
+ << i;
+ }
+}
+
+} // namespace
« 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