• 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/edk/system/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/edk/embedder/platform_handle_vector.h"
16 #include "mojo/edk/test/test_utils.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 
19 namespace mojo {
20 namespace edk {
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   ScopedPlatformHandle 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->PassPlatformHandle();
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   h = dispatcher->PassPlatformHandle();
60   EXPECT_FALSE(h.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   ScopedPlatformHandleVectorPtr handles(new PlatformHandleVector(1));
91   EXPECT_TRUE(dispatcher->EndSerialize(nullptr, nullptr, handles->data()));
92   dispatcher->CompleteTransitAndClose();
93 
94   EXPECT_TRUE(handles->at(0).is_valid());
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);
108 
109   fp = test::FILEFromPlatformHandle(dispatcher->PassPlatformHandle(), "rb");
110   EXPECT_TRUE(fp);
111 
112   rewind(fp.get());
113   char read_buffer[1000] = {};
114   EXPECT_EQ(sizeof(kFooBar),
115             fread(read_buffer, 1, sizeof(read_buffer), fp.get()));
116   EXPECT_STREQ(kFooBar, read_buffer);
117 
118   EXPECT_EQ(MOJO_RESULT_OK, dispatcher->Close());
119 }
120 
121 }  // namespace
122 }  // namespace edk
123 }  // namespace mojo
124