| 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 | 9 |
| 9 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 11 #include "base/files/scoped_file.h" | 12 #include "base/files/scoped_file.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 13 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "mojo/edk/test/test_utils.h" | 15 #include "mojo/edk/test/test_utils.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace mojo { | 18 namespace mojo { |
| 18 namespace edk { | 19 namespace edk { |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 TEST(PlatformHandleDispatcherTest, Basic) { | 22 TEST(PlatformHandleDispatcherTest, Basic) { |
| 22 base::ScopedTempDir temp_dir; | 23 base::ScopedTempDir temp_dir; |
| 23 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 24 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 24 | 25 |
| 25 static const char kHelloWorld[] = "hello world"; | 26 static const char kHelloWorld[] = "hello world"; |
| 26 | 27 |
| 27 base::FilePath unused; | 28 base::FilePath unused; |
| 28 base::ScopedFILE fp( | 29 base::ScopedFILE fp( |
| 29 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 30 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 30 ASSERT_TRUE(fp); | 31 ASSERT_TRUE(fp); |
| 31 EXPECT_EQ(sizeof(kHelloWorld), | 32 EXPECT_EQ(sizeof(kHelloWorld), |
| 32 fwrite(kHelloWorld, 1, sizeof(kHelloWorld), fp.get())); | 33 fwrite(kHelloWorld, 1, sizeof(kHelloWorld), fp.get())); |
| 33 | 34 |
| 34 ScopedPlatformHandle h(test::PlatformHandleFromFILE(fp.Pass())); | 35 ScopedPlatformHandle h(test::PlatformHandleFromFILE(std::move(fp))); |
| 35 EXPECT_FALSE(fp); | 36 EXPECT_FALSE(fp); |
| 36 ASSERT_TRUE(h.is_valid()); | 37 ASSERT_TRUE(h.is_valid()); |
| 37 | 38 |
| 38 scoped_refptr<PlatformHandleDispatcher> dispatcher = | 39 scoped_refptr<PlatformHandleDispatcher> dispatcher = |
| 39 PlatformHandleDispatcher::Create(h.Pass()); | 40 PlatformHandleDispatcher::Create(std::move(h)); |
| 40 EXPECT_FALSE(h.is_valid()); | 41 EXPECT_FALSE(h.is_valid()); |
| 41 EXPECT_EQ(Dispatcher::Type::PLATFORM_HANDLE, dispatcher->GetType()); | 42 EXPECT_EQ(Dispatcher::Type::PLATFORM_HANDLE, dispatcher->GetType()); |
| 42 | 43 |
| 43 h = dispatcher->PassPlatformHandle().Pass(); | 44 h = dispatcher->PassPlatformHandle(); |
| 44 EXPECT_TRUE(h.is_valid()); | 45 EXPECT_TRUE(h.is_valid()); |
| 45 | 46 |
| 46 fp = test::FILEFromPlatformHandle(h.Pass(), "rb").Pass(); | 47 fp = test::FILEFromPlatformHandle(std::move(h), "rb"); |
| 47 EXPECT_FALSE(h.is_valid()); | 48 EXPECT_FALSE(h.is_valid()); |
| 48 EXPECT_TRUE(fp); | 49 EXPECT_TRUE(fp); |
| 49 | 50 |
| 50 rewind(fp.get()); | 51 rewind(fp.get()); |
| 51 char read_buffer[1000] = {}; | 52 char read_buffer[1000] = {}; |
| 52 EXPECT_EQ(sizeof(kHelloWorld), | 53 EXPECT_EQ(sizeof(kHelloWorld), |
| 53 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); | 54 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); |
| 54 EXPECT_STREQ(kHelloWorld, read_buffer); | 55 EXPECT_STREQ(kHelloWorld, read_buffer); |
| 55 | 56 |
| 56 // Try getting the handle again. (It should fail cleanly.) | 57 // Try getting the handle again. (It should fail cleanly.) |
| 57 h = dispatcher->PassPlatformHandle().Pass(); | 58 h = dispatcher->PassPlatformHandle(); |
| 58 EXPECT_FALSE(h.is_valid()); | 59 EXPECT_FALSE(h.is_valid()); |
| 59 | 60 |
| 60 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 61 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 61 } | 62 } |
| 62 | 63 |
| 63 TEST(PlatformHandleDispatcherTest, CreateEquivalentDispatcherAndClose) { | 64 TEST(PlatformHandleDispatcherTest, CreateEquivalentDispatcherAndClose) { |
| 64 base::ScopedTempDir temp_dir; | 65 base::ScopedTempDir temp_dir; |
| 65 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 66 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 66 | 67 |
| 67 static const char kFooBar[] = "foo bar"; | 68 static const char kFooBar[] = "foo bar"; |
| 68 | 69 |
| 69 base::FilePath unused; | 70 base::FilePath unused; |
| 70 base::ScopedFILE fp( | 71 base::ScopedFILE fp( |
| 71 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 72 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
| 72 EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get())); | 73 EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get())); |
| 73 | 74 |
| 74 scoped_refptr<PlatformHandleDispatcher> dispatcher = | 75 scoped_refptr<PlatformHandleDispatcher> dispatcher = |
| 75 PlatformHandleDispatcher::Create( | 76 PlatformHandleDispatcher::Create( |
| 76 test::PlatformHandleFromFILE(fp.Pass())); | 77 test::PlatformHandleFromFILE(std::move(fp))); |
| 77 | 78 |
| 78 DispatcherTransport transport( | 79 DispatcherTransport transport( |
| 79 test::DispatcherTryStartTransport(dispatcher.get())); | 80 test::DispatcherTryStartTransport(dispatcher.get())); |
| 80 EXPECT_TRUE(transport.is_valid()); | 81 EXPECT_TRUE(transport.is_valid()); |
| 81 EXPECT_EQ(Dispatcher::Type::PLATFORM_HANDLE, transport.GetType()); | 82 EXPECT_EQ(Dispatcher::Type::PLATFORM_HANDLE, transport.GetType()); |
| 82 EXPECT_FALSE(transport.IsBusy()); | 83 EXPECT_FALSE(transport.IsBusy()); |
| 83 | 84 |
| 84 scoped_refptr<Dispatcher> generic_dispatcher = | 85 scoped_refptr<Dispatcher> generic_dispatcher = |
| 85 transport.CreateEquivalentDispatcherAndClose(); | 86 transport.CreateEquivalentDispatcherAndClose(); |
| 86 ASSERT_TRUE(generic_dispatcher); | 87 ASSERT_TRUE(generic_dispatcher); |
| 87 | 88 |
| 88 transport.End(); | 89 transport.End(); |
| 89 EXPECT_TRUE(dispatcher->HasOneRef()); | 90 EXPECT_TRUE(dispatcher->HasOneRef()); |
| 90 dispatcher = nullptr; | 91 dispatcher = nullptr; |
| 91 | 92 |
| 92 ASSERT_EQ(Dispatcher::Type::PLATFORM_HANDLE, generic_dispatcher->GetType()); | 93 ASSERT_EQ(Dispatcher::Type::PLATFORM_HANDLE, generic_dispatcher->GetType()); |
| 93 dispatcher = static_cast<PlatformHandleDispatcher*>(generic_dispatcher.get()); | 94 dispatcher = static_cast<PlatformHandleDispatcher*>(generic_dispatcher.get()); |
| 94 | 95 |
| 95 fp = test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), | 96 fp = test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), "rb"); |
| 96 "rb").Pass(); | |
| 97 EXPECT_TRUE(fp); | 97 EXPECT_TRUE(fp); |
| 98 | 98 |
| 99 rewind(fp.get()); | 99 rewind(fp.get()); |
| 100 char read_buffer[1000] = {}; | 100 char read_buffer[1000] = {}; |
| 101 EXPECT_EQ(sizeof(kFooBar), | 101 EXPECT_EQ(sizeof(kFooBar), |
| 102 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); | 102 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); |
| 103 EXPECT_STREQ(kFooBar, read_buffer); | 103 EXPECT_STREQ(kFooBar, read_buffer); |
| 104 | 104 |
| 105 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 105 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 } // namespace | 108 } // namespace |
| 109 } // namespace edk | 109 } // namespace edk |
| 110 } // namespace mojo | 110 } // namespace mojo |
| OLD | NEW |