| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 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/master_impl.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_file.h" |
| 10 #include "base/files/scoped_temp_dir.h" |
| 11 #include "mojo/edk/embedder/embedder.h" |
| 12 #include "mojo/edk/test/test_utils.h" |
| 13 #include "mojo/public/c/system/macros.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace system { |
| 18 namespace { |
| 19 |
| 20 static const char kHelloWorld[] = "hello world"; |
| 21 |
| 22 #if defined(OS_WIN) |
| 23 |
| 24 class MasterImplTest : public ::testing::Test { |
| 25 public: |
| 26 MasterImplTest() {} |
| 27 |
| 28 // Returns a ScopedHandle to a file with the magic string. |
| 29 ScopedHandle GetScopedHandle() { |
| 30 if (!temp_dir_.IsValid()) |
| 31 CHECK(temp_dir_.CreateUniqueTempDir()); |
| 32 base::FilePath unused; |
| 33 base::ScopedFILE fp( |
| 34 CreateAndOpenTemporaryFileInDir(temp_dir_.path(), &unused)); |
| 35 CHECK(fp); |
| 36 EXPECT_EQ(sizeof(kHelloWorld), |
| 37 fwrite(kHelloWorld, 1, sizeof(kHelloWorld), fp.get())); |
| 38 embedder::ScopedPlatformHandle platform_handle( |
| 39 test::PlatformHandleFromFILE(fp.Pass())); |
| 40 CHECK(platform_handle.is_valid()); |
| 41 |
| 42 MojoHandle handle; |
| 43 MojoResult wrap_result = embedder::CreatePlatformHandleWrapper( |
| 44 platform_handle.Pass(), &handle); |
| 45 CHECK(wrap_result == MOJO_RESULT_OK); |
| 46 return ScopedHandle(Handle(handle)); |
| 47 } |
| 48 |
| 49 // Check that the given ScopedHandle has a file with the magic string. |
| 50 bool CheckScopedHandle(ScopedHandle handle) { |
| 51 embedder::ScopedPlatformHandle platform_handle; |
| 52 MojoResult unwrap_result = embedder::PassWrappedPlatformHandle( |
| 53 handle.get().value(), &platform_handle); |
| 54 if (unwrap_result != MOJO_RESULT_OK) |
| 55 return false; |
| 56 base::ScopedFILE fp = |
| 57 test::FILEFromPlatformHandle(platform_handle.Pass(), "rb").Pass(); |
| 58 if (!fp) |
| 59 return false; |
| 60 rewind(fp.get()); |
| 61 char read_buffer[1000] = {}; |
| 62 if (fread(read_buffer, 1, sizeof(read_buffer), fp.get()) != |
| 63 sizeof(kHelloWorld)) { |
| 64 return false; |
| 65 } |
| 66 return std::string(read_buffer) == kHelloWorld; |
| 67 } |
| 68 |
| 69 private: |
| 70 base::ScopedTempDir temp_dir_; |
| 71 }; |
| 72 |
| 73 TEST_F(MasterImplTest, Basic) { |
| 74 MasterImpl master(base::GetCurrentProcId()); |
| 75 uint64_t token; |
| 76 master.HandleToToken(GetScopedHandle(), |
| 77 [&token](int32_t result, uint64_t t) { |
| 78 ASSERT_EQ(result, MOJO_RESULT_OK); |
| 79 token = t; |
| 80 }); |
| 81 |
| 82 ScopedHandle handle; |
| 83 master.TokenToHandle(token, |
| 84 [&handle](int32_t result, ScopedHandle h) { |
| 85 ASSERT_EQ(result, MOJO_RESULT_OK); |
| 86 handle = h.Pass(); |
| 87 }); |
| 88 |
| 89 ASSERT_TRUE(CheckScopedHandle(handle.Pass())); |
| 90 } |
| 91 |
| 92 TEST_F(MasterImplTest, TokenIsRemoved) { |
| 93 MasterImpl master(base::GetCurrentProcId()); |
| 94 uint64_t token; |
| 95 master.HandleToToken(GetScopedHandle(), |
| 96 [&token](int32_t result, uint64_t t) { |
| 97 ASSERT_EQ(result, MOJO_RESULT_OK); |
| 98 token = t; |
| 99 }); |
| 100 |
| 101 ScopedHandle handle; |
| 102 master.TokenToHandle(token, |
| 103 [&handle](int32_t result, ScopedHandle h) { |
| 104 ASSERT_EQ(result, MOJO_RESULT_OK); |
| 105 handle = h.Pass(); |
| 106 }); |
| 107 |
| 108 ASSERT_TRUE(CheckScopedHandle(handle.Pass())); |
| 109 |
| 110 master.TokenToHandle(token, |
| 111 [&handle](int32_t result, ScopedHandle h) { |
| 112 ASSERT_EQ(result, MOJO_RESULT_INVALID_ARGUMENT); |
| 113 }); |
| 114 } |
| 115 |
| 116 #endif // OS_WIN |
| 117 |
| 118 } // namespace |
| 119 } // namespace system |
| 120 } // namespace mojo |
| OLD | NEW |