• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "mojo/core/platform_handle_dispatcher.h"
6 
7 #include <stdio.h>
8 #include <utility>
9 
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_file.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/memory/ref_counted.h"
15 #include "mojo/core/test/test_utils.h"
16 #include "mojo/public/cpp/system/platform_handle.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace mojo {
20 namespace core {
21 namespace {
22 
TEST(PlatformHandleDispatcherTest,Basic)23 TEST(PlatformHandleDispatcherTest, Basic) {
24   base::ScopedTempDir temp_dir;
25   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
26 
27   static const char kHelloWorld[] = "hello world";
28 
29   base::FilePath unused;
30   base::ScopedFILE fp(
31       CreateAndOpenTemporaryFileInDir(temp_dir.GetPath(), &unused));
32   ASSERT_TRUE(fp);
33   EXPECT_EQ(sizeof(kHelloWorld),
34             fwrite(kHelloWorld, 1, sizeof(kHelloWorld), fp.get()));
35 
36   PlatformHandle h = test::PlatformHandleFromFILE(std::move(fp));
37   EXPECT_FALSE(fp);
38   ASSERT_TRUE(h.is_valid());
39 
40   scoped_refptr<PlatformHandleDispatcher> dispatcher =
41       PlatformHandleDispatcher::Create(std::move(h));
42   EXPECT_FALSE(h.is_valid());
43   EXPECT_EQ(Dispatcher::Type::PLATFORM_HANDLE, dispatcher->GetType());
44 
45   h = dispatcher->TakePlatformHandle();
46   EXPECT_TRUE(h.is_valid());
47 
48   fp = test::FILEFromPlatformHandle(std::move(h), "rb");
49   EXPECT_FALSE(h.is_valid());
50   EXPECT_TRUE(fp);
51 
52   rewind(fp.get());
53   char read_buffer[1000] = {};
54   EXPECT_EQ(sizeof(kHelloWorld),
55             fread(read_buffer, 1, sizeof(read_buffer), fp.get()));
56   EXPECT_STREQ(kHelloWorld, read_buffer);
57 
58   // Try getting the handle again. (It should fail cleanly.)
59   auto internal_handle = dispatcher->TakePlatformHandle();
60   EXPECT_FALSE(internal_handle.is_valid());
61 
62   EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
63 }
64 
TEST(PlatformHandleDispatcherTest,Serialization)65 TEST(PlatformHandleDispatcherTest, Serialization) {
66   base::ScopedTempDir temp_dir;
67   ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
68 
69   static const char kFooBar[] = "foo bar";
70 
71   base::FilePath unused;
72   base::ScopedFILE fp(
73       CreateAndOpenTemporaryFileInDir(temp_dir.GetPath(), &unused));
74   EXPECT_EQ(sizeof(kFooBar), fwrite(kFooBar, 1, sizeof(kFooBar), fp.get()));
75 
76   scoped_refptr<PlatformHandleDispatcher> dispatcher =
77       PlatformHandleDispatcher::Create(
78           test::PlatformHandleFromFILE(std::move(fp)));
79 
80   uint32_t num_bytes = 0;
81   uint32_t num_ports = 0;
82   uint32_t num_handles = 0;
83   EXPECT_TRUE(dispatcher->BeginTransit());
84   dispatcher->StartSerialize(&num_bytes, &num_ports, &num_handles);
85 
86   EXPECT_EQ(0u, num_bytes);
87   EXPECT_EQ(0u, num_ports);
88   EXPECT_EQ(1u, num_handles);
89 
90   PlatformHandle received_handle;
91   EXPECT_TRUE(dispatcher->EndSerialize(nullptr, nullptr, &received_handle));
92 
93   dispatcher->CompleteTransitAndClose();
94 
95   EXPECT_TRUE(received_handle.is_valid());
96 
97   PlatformHandle handle = dispatcher->TakePlatformHandle();
98   EXPECT_FALSE(handle.is_valid());
99 
100   EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, dispatcher->Close());
101 
102   dispatcher = static_cast<PlatformHandleDispatcher*>(
103       Dispatcher::Deserialize(Dispatcher::Type::PLATFORM_HANDLE, nullptr,
104                               num_bytes, nullptr, num_ports, &received_handle,
105                               1u)
106           .get());
107 
108   EXPECT_FALSE(received_handle.is_valid());
109   EXPECT_TRUE(dispatcher->GetType() == Dispatcher::Type::PLATFORM_HANDLE);
110 
111   fp = test::FILEFromPlatformHandle(dispatcher->TakePlatformHandle(), "rb");
112   EXPECT_TRUE(fp);
113 
114   rewind(fp.get());
115   char read_buffer[1000] = {};
116   EXPECT_EQ(sizeof(kFooBar),
117             fread(read_buffer, 1, sizeof(read_buffer), fp.get()));
118   EXPECT_STREQ(kFooBar, read_buffer);
119 
120   EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
121 }
122 
123 }  // namespace
124 }  // namespace core
125 }  // namespace mojo
126