Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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/platform_handle/platform_handle_functions.h" | |
| 6 #include "mojo/public/cpp/application/application_impl.h" | |
|
viettrungluu
2016/02/02 00:07:36
nit: blank line above.
Forrest Reiling
2016/02/10 20:16:08
after I moved the platform handle stuff, that head
| |
| 7 #include "mojo/public/cpp/application/application_test_base.h" | |
| 8 #include "mojo/public/cpp/environment/logging.h" | |
| 9 #include "mojo/public/cpp/system/macros.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 namespace { | |
| 13 | |
| 14 // Exemplifies ApplicationTestBase's application testing pattern. | |
| 15 class PlatformHandleApplicationTest : public test::ApplicationTestBase { | |
| 16 public: | |
| 17 PlatformHandleApplicationTest() : ApplicationTestBase() {} | |
| 18 ~PlatformHandleApplicationTest() override {} | |
| 19 | |
| 20 private: | |
| 21 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformHandleApplicationTest); | |
| 22 }; | |
| 23 | |
| 24 TEST_F(PlatformHandleApplicationTest, WrapAndUnwrapFileDescriptor) { | |
| 25 MojoPlatformHandle original_handle; | |
| 26 MojoPlatformHandle unwrapped_handle; | |
| 27 MojoHandle wrapper; | |
| 28 MojoResult mojo_result; | |
| 29 | |
| 30 int posix_result; | |
| 31 int pipe_fds[2]; | |
| 32 | |
| 33 uint64_t write_buffer = 0xDEADBEEF; | |
| 34 uint64_t read_buffer = 0; | |
| 35 | |
| 36 posix_result = pipe(pipe_fds); | |
| 37 MOJO_CHECK(posix_result >= 0) << "Failed to open pipe: " << strerror(errno); | |
| 38 | |
| 39 /*passing second FD through wrapper*/ | |
| 40 original_handle = pipe_fds[1]; | |
| 41 | |
| 42 mojo_result = MojoCreatePlatformHandleWrapper(original_handle, &wrapper); | |
| 43 MOJO_CHECK(mojo_result == MOJO_RESULT_OK) << "Failed to wrap platform handle"; | |
| 44 | |
| 45 mojo_result = MojoExtractPlatformHandle(wrapper, &unwrapped_handle); | |
| 46 MOJO_CHECK(mojo_result == MOJO_RESULT_OK) | |
| 47 << "Failed to extract platform handle"; | |
| 48 | |
| 49 /*write to wrapped/unwrapped fd*/ | |
| 50 ssize_t bytes_written = | |
| 51 write(unwrapped_handle, &write_buffer, sizeof(write_buffer)); | |
| 52 MOJO_CHECK(bytes_written >= 0) | |
| 53 << "Failed to write to wrapped/unwrapped handle: " << strerror(errno); | |
| 54 | |
| 55 /*read from other end of pipe*/ | |
| 56 ssize_t bytes_read = read(pipe_fds[0], &read_buffer, sizeof(read_buffer)); | |
| 57 MOJO_CHECK(bytes_read >= 0) << "Failed to read to read from original pipe: " | |
| 58 << strerror(errno); | |
| 59 | |
| 60 MOJO_CHECK(bytes_read == bytes_written) | |
| 61 << "Read a different number of bytes from pipe than were written to pipe"; | |
| 62 EXPECT_EQ(write_buffer, read_buffer); | |
| 63 | |
| 64 close(pipe_fds[0]); | |
| 65 close(unwrapped_handle); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 } // namespace mojo | |
| OLD | NEW |