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

Unified Diff: mojo/common/common_custom_types_unittest.cc

Issue 2561003002: Add mojo structs for ScopedFD and ScopedHandle. (Closed)
Patch Set: Created 4 years 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/common/common_custom_types_struct_traits_win.cc ('k') | mojo/common/invalid_typemapped_type.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/common_custom_types_unittest.cc
diff --git a/mojo/common/common_custom_types_unittest.cc b/mojo/common/common_custom_types_unittest.cc
index bf57d20307f08f76ff2750d83487954a46041c81..185a99a3d82a354efa727643d768c9b0e3474ecc 100644
--- a/mojo/common/common_custom_types_unittest.cc
+++ b/mojo/common/common_custom_types_unittest.cc
@@ -10,7 +10,9 @@
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "mojo/common/test_common_custom_types.mojom.h"
+#include "mojo/common/test_invalid_typemapped_type.mojom.h"
#include "mojo/public/cpp/bindings/binding.h"
+#include "mojo/public/cpp/bindings/lib/validation_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
@@ -179,6 +181,45 @@ class TestFileImpl : public TestFile {
mojo::Binding<TestFile> binding_;
};
+class TestPlatformHandleImpl : public TestPlatformHandle {
+ public:
+ explicit TestPlatformHandleImpl(TestPlatformHandleRequest request)
+ : binding_(this, std::move(request)) {}
+
+ // TestPlatformHandle implementation:
+ void BouncePlatformHandle(
+ ::mojo::common::mojom::PlatformHandlePtr in,
+ const BouncePlatformHandleCallback& callback) override {
+ callback.Run(std::move(in));
+ }
+
+ private:
+ mojo::Binding<TestPlatformHandle> binding_;
+};
+
+class TestInvalidTypemappedTypeImpl : public mojom::TestInvalidTypemappedType {
+ public:
+ explicit TestInvalidTypemappedTypeImpl(
+ mojom::TestInvalidTypemappedTypeRequest request)
+ : binding_(this, std::move(request)) {}
+
+ // TestInvalidTypemappedType implementation:
+ void BounceInvalidTypemappedType(
+ InvalidTypemappedType in,
+ const BounceInvalidTypemappedTypeCallback& callback) override {
+ callback.Run(std::move(in));
+ }
+
+ void BounceNullableInvalidTypemappedType(
+ InvalidTypemappedType in,
+ const BounceInvalidTypemappedTypeCallback& callback) override {
+ callback.Run(std::move(in));
+ }
+
+ private:
+ mojo::Binding<mojom::TestInvalidTypemappedType> binding_;
+};
+
class CommonCustomTypesTest : public testing::Test {
protected:
CommonCustomTypesTest() {}
@@ -190,6 +231,29 @@ class CommonCustomTypesTest : public testing::Test {
DISALLOW_COPY_AND_ASSIGN(CommonCustomTypesTest);
};
+::mojo::common::mojom::PlatformHandlePtr FileToPlatformHandle(base::File file) {
+ auto platform_handle = mojo::common::mojom::PlatformHandle::New();
+#if defined(OS_POSIX)
+ base::ScopedFD fd(file.TakePlatformFile());
+ platform_handle->set_fd(std::move(fd));
+#elif defined(OS_WIN)
+ base::win::ScopedHandle handle(file.TakePlatformFile());
+ platform_handle->set_windows_handle(std::move(handle));
+#endif
+ return platform_handle;
+}
+
+base::File PlatformHandleToFile(
+ ::mojo::common::mojom::PlatformHandlePtr handle) {
+ return base::File(
+#if defined(OS_POSIX)
+ handle->get_fd().release()
+#elif defined(OS_WIN)
+ handle->get_windows_handle().Take()
+#endif
+ );
+}
+
} // namespace
TEST_F(CommonCustomTypesTest, FilePath) {
@@ -369,6 +433,81 @@ TEST_F(CommonCustomTypesTest, InvalidFile) {
EXPECT_FALSE(file_out.IsValid());
}
+TEST_F(CommonCustomTypesTest, PlatformHandle) {
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+
+ TestPlatformHandlePtr ptr;
+ TestPlatformHandleImpl impl(GetProxy(&ptr));
+
+ base::File file(
+ temp_dir.GetPath().AppendASCII("test_file.txt"),
+ base::File::FLAG_CREATE | base::File::FLAG_WRITE | base::File::FLAG_READ);
+ const base::StringPiece test_content =
+ "A different test string to be stored in a test file";
+ file.WriteAtCurrentPos(
+ test_content.data(),
+ base::CheckedNumeric<int>(test_content.size()).ValueOrDie());
+ auto platform_handle = FileToPlatformHandle(std::move(file));
+ mojo::common::mojom::PlatformHandlePtr platform_handle_out;
+ ASSERT_TRUE(ptr->BouncePlatformHandle(std::move(platform_handle),
+ &platform_handle_out));
+ base::File file_out = PlatformHandleToFile(std::move(platform_handle_out));
+ std::vector<char> content(test_content.size());
+ ASSERT_TRUE(file_out.IsValid());
+ ASSERT_EQ(static_cast<int>(test_content.size()),
+ file_out.Read(
+ 0, content.data(),
+ base::CheckedNumeric<int>(test_content.size()).ValueOrDie()));
+ EXPECT_EQ(test_content,
+ base::StringPiece(content.data(), test_content.size()));
+}
+
+TEST_F(CommonCustomTypesTest, NullPlatformHandle) {
+ TestPlatformHandlePtr ptr;
+ TestPlatformHandleImpl impl(GetProxy(&ptr));
+
+ mojo::common::mojom::PlatformHandlePtr platform_handle_out;
+ ASSERT_TRUE(ptr->BouncePlatformHandle(
+ mojo::common::mojom::PlatformHandlePtr(), &platform_handle_out));
+ EXPECT_FALSE(platform_handle_out);
+}
+
+TEST_F(CommonCustomTypesTest, InvalidPlatformHandle) {
+ TestPlatformHandlePtr ptr;
+ TestPlatformHandleImpl impl(GetProxy(&ptr));
+
+ auto platform_handle = mojo::common::mojom::PlatformHandle::New();
+#if defined(OS_POSIX)
+ platform_handle->set_windows_handle(InvalidTypemappedType());
+#else
+ platform_handle->set_fd(InvalidTypemappedType());
+#endif
+ mojo::common::mojom::PlatformHandlePtr platform_handle_out;
+
+ mojo::internal::SerializationWarningObserverForTesting
+ serialization_warning_observer;
+ EXPECT_FALSE(ptr->BouncePlatformHandle(std::move(platform_handle),
+ &platform_handle_out));
+ EXPECT_EQ(mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
+ serialization_warning_observer.last_warning());
+ EXPECT_FALSE(platform_handle_out);
+}
+
+TEST_F(CommonCustomTypesTest, InvalidTypemappedType) {
+ mojom::TestInvalidTypemappedTypePtr ptr;
+ TestInvalidTypemappedTypeImpl impl(GetProxy(&ptr));
+
+ InvalidTypemappedType out;
+ EXPECT_TRUE(
+ ptr->BounceNullableInvalidTypemappedType(InvalidTypemappedType(), &out));
+ mojo::internal::SerializationWarningObserverForTesting
+ serialization_warning_observer;
+ EXPECT_FALSE(ptr->BounceInvalidTypemappedType(InvalidTypemappedType(), &out));
+ EXPECT_EQ(mojo::internal::VALIDATION_ERROR_UNEXPECTED_NULL_POINTER,
+ serialization_warning_observer.last_warning());
+}
+
} // namespace test
} // namespace common
} // namespace mojo
« no previous file with comments | « mojo/common/common_custom_types_struct_traits_win.cc ('k') | mojo/common/invalid_typemapped_type.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698