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" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 rewind(fp.get()); | 52 rewind(fp.get()); |
53 char read_buffer[1000] = {}; | 53 char read_buffer[1000] = {}; |
54 EXPECT_EQ(sizeof(kHelloWorld), | 54 EXPECT_EQ(sizeof(kHelloWorld), |
55 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); | 55 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); |
56 EXPECT_STREQ(kHelloWorld, read_buffer); | 56 EXPECT_STREQ(kHelloWorld, read_buffer); |
57 | 57 |
58 // Try getting the handle again. (It should fail cleanly.) | 58 // Try getting the handle again. (It should fail cleanly.) |
59 h = dispatcher->PassPlatformHandle(); | 59 h = dispatcher->PassPlatformHandle(); |
60 EXPECT_FALSE(h.is_valid()); | 60 EXPECT_FALSE(h.is_valid()); |
61 | 61 |
62 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 62 Dispatcher::RequestContext request_context; |
| 63 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close(&request_context)); |
63 } | 64 } |
64 | 65 |
65 TEST(PlatformHandleDispatcherTest, Serialization) { | 66 TEST(PlatformHandleDispatcherTest, Serialization) { |
66 base::ScopedTempDir temp_dir; | 67 base::ScopedTempDir temp_dir; |
67 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | 68 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); |
68 | 69 |
69 static const char kFooBar[] = "foo bar"; | 70 static const char kFooBar[] = "foo bar"; |
70 | 71 |
71 base::FilePath unused; | 72 base::FilePath unused; |
72 base::ScopedFILE fp( | 73 base::ScopedFILE fp( |
73 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); | 74 CreateAndOpenTemporaryFileInDir(temp_dir.path(), &unused)); |
74 EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get())); | 75 EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get())); |
75 | 76 |
76 scoped_refptr<PlatformHandleDispatcher> dispatcher = | 77 scoped_refptr<PlatformHandleDispatcher> dispatcher = |
77 PlatformHandleDispatcher::Create( | 78 PlatformHandleDispatcher::Create( |
78 test::PlatformHandleFromFILE(std::move(fp))); | 79 test::PlatformHandleFromFILE(std::move(fp))); |
79 | 80 |
| 81 Dispatcher::RequestContext request_context; |
80 uint32_t num_bytes = 0; | 82 uint32_t num_bytes = 0; |
81 uint32_t num_ports = 0; | 83 uint32_t num_ports = 0; |
82 uint32_t num_handles = 0; | 84 uint32_t num_handles = 0; |
83 EXPECT_TRUE(dispatcher->BeginTransit()); | 85 EXPECT_TRUE(dispatcher->BeginTransit(&request_context)); |
84 dispatcher->StartSerialize(&num_bytes, &num_ports, &num_handles); | 86 dispatcher->StartSerialize(&num_bytes, &num_ports, &num_handles); |
85 | 87 |
86 EXPECT_EQ(0u, num_bytes); | 88 EXPECT_EQ(0u, num_bytes); |
87 EXPECT_EQ(0u, num_ports); | 89 EXPECT_EQ(0u, num_ports); |
88 EXPECT_EQ(1u, num_handles); | 90 EXPECT_EQ(1u, num_handles); |
89 | 91 |
90 ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector(1)); | 92 ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector(1)); |
91 EXPECT_TRUE(dispatcher->EndSerialize(nullptr, nullptr, handles->data())); | 93 EXPECT_TRUE(dispatcher->EndSerialize(nullptr, nullptr, handles->data())); |
92 dispatcher->CompleteTransitAndClose(); | 94 dispatcher->CompleteTransitAndClose(&request_context); |
93 | 95 |
94 EXPECT_TRUE(handles->at(0).is_valid()); | 96 EXPECT_TRUE(handles->at(0).is_valid()); |
95 | 97 |
96 ScopedPlatformHandle handle = dispatcher->PassPlatformHandle(); | 98 ScopedPlatformHandle handle = dispatcher->PassPlatformHandle(); |
97 EXPECT_FALSE(handle.is_valid()); | 99 EXPECT_FALSE(handle.is_valid()); |
98 | 100 |
99 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dispatcher->Close()); | 101 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dispatcher->Close(&request_context)); |
100 | 102 |
101 dispatcher = static_cast<PlatformHandleDispatcher*>( | 103 dispatcher = static_cast<PlatformHandleDispatcher*>( |
102 Dispatcher::Deserialize(Dispatcher::Type::PLATFORM_HANDLE, nullptr, | 104 Dispatcher::Deserialize(Dispatcher::Type::PLATFORM_HANDLE, nullptr, |
103 num_bytes, nullptr, num_ports, handles->data(), | 105 num_bytes, nullptr, num_ports, handles->data(), |
104 1).get()); | 106 1).get()); |
105 | 107 |
106 EXPECT_FALSE(handles->at(0).is_valid()); | 108 EXPECT_FALSE(handles->at(0).is_valid()); |
107 EXPECT_TRUE(dispatcher->GetType() == Dispatcher::Type::PLATFORM_HANDLE); | 109 EXPECT_TRUE(dispatcher->GetType() == Dispatcher::Type::PLATFORM_HANDLE); |
108 | 110 |
109 fp = test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), "rb"); | 111 fp = test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), "rb"); |
110 EXPECT_TRUE(fp); | 112 EXPECT_TRUE(fp); |
111 | 113 |
112 rewind(fp.get()); | 114 rewind(fp.get()); |
113 char read_buffer[1000] = {}; | 115 char read_buffer[1000] = {}; |
114 EXPECT_EQ(sizeof(kFooBar), | 116 EXPECT_EQ(sizeof(kFooBar), |
115 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); | 117 fread(read_buffer, 1, sizeof(read_buffer), fp.get())); |
116 EXPECT_STREQ(kFooBar, read_buffer); | 118 EXPECT_STREQ(kFooBar, read_buffer); |
117 | 119 |
118 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close()); | 120 EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close(&request_context)); |
119 } | 121 } |
120 | 122 |
121 } // namespace | 123 } // namespace |
122 } // namespace edk | 124 } // namespace edk |
123 } // namespace mojo | 125 } // namespace mojo |
OLD | NEW |