| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/system/platform_handle_dispatcher.h" | 5 #include "mojo/edk/system/platform_handle_dispatcher.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 13 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 15 #include "mojo/edk/test/test_utils.h" | 16 #include "mojo/edk/test/test_utils.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace mojo { | 19 namespace mojo { |
| 19 namespace edk { | 20 namespace edk { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 TEST(PlatformHandleDispatcherTest, Basic) { | 23 TEST(PlatformHandleDispatcherTest, Basic) { |
| 23 base::ScopedTempDir temp_dir; | 24 base::ScopedTempDir temp_dir; |
| 24 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 25 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 54 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); | 55 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); |
| 55 EXPECT_STREQ(kHelloWorld, read_buffer); | 56 EXPECT_STREQ(kHelloWorld, read_buffer); |
| 56 | 57 |
| 57 // Try getting the handle again. (It should fail cleanly.) | 58 // Try getting the handle again. (It should fail cleanly.) |
| 58 h = dispatcher->PassPlatformHandle(); | 59 h = dispatcher->PassPlatformHandle(); |
| 59 EXPECT_FALSE(h.is_valid()); | 60 EXPECT_FALSE(h.is_valid()); |
| 60 | 61 |
| 61 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 62 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 62 } | 63 } |
| 63 | 64 |
| 64 TEST(PlatformHandleDispatcherTest, CreateEquivalentDispatcherAndClose) { | 65 TEST(PlatformHandleDispatcherTest, Serialization) { |
| 65 base::ScopedTempDir temp_dir; | 66 base::ScopedTempDir temp_dir; |
| 66 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 67 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 67 | 68 |
| 68 static const char kFooBar[] = "foo bar"; | 69 static const char kFooBar[] = "foo bar"; |
| 69 | 70 |
| 70 base::FilePath unused; | 71 base::FilePath unused; |
| 71 base::ScopedFILE fp( | 72 base::ScopedFILE fp( |
| 72 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 73 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 73 EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get())); | 74 EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get())); |
| 74 | 75 |
| 75 scoped_refptr<PlatformHandleDispatcher> dispatcher = | 76 scoped_refptr<PlatformHandleDispatcher> dispatcher = |
| 76 PlatformHandleDispatcher::Create( | 77 PlatformHandleDispatcher::Create( |
| 77 test::PlatformHandleFromFILE(std::move(fp))); | 78 test::PlatformHandleFromFILE(std::move(fp))); |
| 78 | 79 |
| 79 DispatcherTransport transport( | 80 uint32_t num_bytes = 0; |
| 80 test::DispatcherTryStartTransport(dispatcher.get())); | 81 uint32_t num_ports = 0; |
| 81 EXPECT_TRUE(transport.is_valid()); | 82 uint32_t num_handles = 0; |
| 82 EXPECT_EQ(Dispatcher::Type::PLATFORM_HANDLE, transport.GetType()); | 83 EXPECT_TRUE(dispatcher->BeginTransit()); |
| 83 EXPECT_FALSE(transport.IsBusy()); | 84 dispatcher->StartSerialize(&num_bytes, &num_ports, &num_handles); |
| 84 | 85 |
| 85 scoped_refptr<Dispatcher> generic_dispatcher = | 86 EXPECT_EQ(0u, num_bytes); |
| 86 transport.CreateEquivalentDispatcherAndClose(); | 87 EXPECT_EQ(0u, num_ports); |
| 87 ASSERT_TRUE(generic_dispatcher); | 88 EXPECT_EQ(1u, num_handles); |
| 88 | 89 |
| 89 transport.End(); | 90 ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector(1)); |
| 90 EXPECT_TRUE(dispatcher->HasOneRef()); | 91 EXPECT_TRUE(dispatcher->EndSerialize(nullptr, nullptr, handles->data())); |
| 91 dispatcher = nullptr; | 92 dispatcher->CompleteTransitAndClose(); |
| 92 | 93 |
| 93 ASSERT_EQ(Dispatcher::Type::PLATFORM_HANDLE, generic_dispatcher->GetType()); | 94 EXPECT_TRUE(handles->at(0).is_valid()); |
| 94 dispatcher = static_cast<PlatformHandleDispatcher*>(generic_dispatcher.get()); | 95 |
| 96 ScopedPlatformHandle handle = dispatcher->PassPlatformHandle(); |
| 97 EXPECT_FALSE(handle.is_valid()); |
| 98 |
| 99 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dispatcher->Close()); |
| 100 |
| 101 dispatcher = static_cast<PlatformHandleDispatcher*>( |
| 102 Dispatcher::Deserialize(Dispatcher::Type::PLATFORM_HANDLE, nullptr, |
| 103 num_bytes, nullptr, num_ports, handles->data(), |
| 104 1).get()); |
| 105 |
| 106 EXPECT_FALSE(handles->at(0).is_valid()); |
| 107 EXPECT_TRUE(dispatcher->GetType() == Dispatcher::Type::PLATFORM_HANDLE); |
| 95 | 108 |
| 96 fp = test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), "rb"); | 109 fp = test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), "rb"); |
| 97 EXPECT_TRUE(fp); | 110 EXPECT_TRUE(fp); |
| 98 | 111 |
| 99 rewind(fp.get()); | 112 rewind(fp.get()); |
| 100 char read_buffer[1000] = {}; | 113 char read_buffer[1000] = {}; |
| 101 EXPECT_EQ(sizeof(kFooBar), | 114 EXPECT_EQ(sizeof(kFooBar), |
| 102 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); | 115 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); |
| 103 EXPECT_STREQ(kFooBar, read_buffer); | 116 EXPECT_STREQ(kFooBar, read_buffer); |
| 104 | 117 |
| 105 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 118 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 106 } | 119 } |
| 107 | 120 |
| 108 } // namespace | 121 } // namespace |
| 109 } // namespace edk | 122 } // namespace edk |
| 110 } // namespace mojo | 123 } // namespace mojo |
| OLD | NEW |