Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/public/platform/native/platform_handle_private.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <string.h> | |
| 9 #include <sys/types.h> | |
| 10 #include <unistd.h> | |
| 11 | |
| 12 #include "mojo/public/cpp/application/application_impl.h" | |
| 13 #include "mojo/public/cpp/application/application_test_base.h" | |
| 14 #include "mojo/public/cpp/environment/logging.h" | |
| 15 #include "mojo/public/cpp/system/macros.h" | |
| 16 | |
| 17 | |
| 18 namespace mojo { | |
| 19 namespace { | |
| 20 | |
| 21 // Exemplifies ApplicationTestBase's application testing pattern. | |
|
viettrungluu
2016/02/26 00:14:21
This comment seems kind of pointless.
Forrest Reiling
2016/03/02 23:18:39
Looks like copy-pasta from the example I used, I'l
| |
| 22 class PlatformHandlePrivateApplicationTest : public test::ApplicationTestBase { | |
| 23 public: | |
| 24 PlatformHandlePrivateApplicationTest() : ApplicationTestBase() {} | |
| 25 ~PlatformHandlePrivateApplicationTest() override {} | |
| 26 | |
| 27 private: | |
| 28 MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformHandlePrivateApplicationTest); | |
| 29 }; | |
| 30 | |
| 31 TEST_F(PlatformHandlePrivateApplicationTest, WrapAndUnwrapFileDescriptor) { | |
| 32 MojoPlatformHandle original_handle; | |
|
viettrungluu
2016/02/26 00:14:21
(Probably to -1.)
Forrest Reiling
2016/03/02 23:18:40
Done.
| |
| 33 MojoPlatformHandle unwrapped_handle; | |
| 34 MojoHandle wrapper; | |
|
viettrungluu
2016/02/26 00:14:21
You should initialize these (probably to MOJO_HAND
Forrest Reiling
2016/03/02 23:18:40
Done.
| |
| 35 | |
| 36 int pipe_fds[2] = {-1, -1}; | |
| 37 | |
| 38 uint64_t write_buffer = 0xDEADBEEF; | |
| 39 uint64_t read_buffer = 0; | |
| 40 | |
| 41 ASSERT_GE(pipe(pipe_fds), 0) << strerror(errno); | |
|
viettrungluu
2016/02/26 00:14:21
ASSERT_EQ
(the specification says that pipe() ret
Forrest Reiling
2016/03/02 23:18:40
Done.
| |
| 42 | |
| 43 // Pass second FD through wrapper. | |
| 44 original_handle = pipe_fds[1]; | |
| 45 | |
| 46 EXPECT_EQ(MOJO_RESULT_OK, | |
| 47 MojoCreatePlatformHandleWrapper(original_handle, &wrapper)); | |
| 48 EXPECT_EQ(MOJO_RESULT_OK, | |
| 49 MojoExtractPlatformHandle(wrapper, &unwrapped_handle)); | |
| 50 | |
| 51 // Write to wrapped/unwrapped FD. | |
| 52 ssize_t bytes_written = | |
| 53 write(unwrapped_handle, &write_buffer, sizeof(write_buffer)); | |
| 54 MOJO_CHECK(bytes_written >= 0) | |
|
viettrungluu
2016/02/26 00:14:21
EXPECT_GE
Really, you want
EXPECT_EQ(sizeof(writ
Forrest Reiling
2016/03/02 23:18:40
Done.
| |
| 55 << "Failed to write to wrapped/unwrapped handle: " << strerror(errno); | |
|
viettrungluu
2016/02/26 00:14:21
These messages are mostly overkill; I'd skip them.
Forrest Reiling
2016/03/02 23:18:40
Done.
| |
| 56 | |
| 57 // Read from other end of pipe. | |
| 58 ssize_t bytes_read = read(pipe_fds[0], &read_buffer, sizeof(read_buffer)); | |
| 59 MOJO_CHECK(bytes_read >= 0) << "Failed to read to read from original pipe: " | |
|
viettrungluu
2016/02/26 00:14:21
This seems kind of redundant.
Forrest Reiling
2016/03/02 23:18:39
replaced with symmetric check to the one above (E
| |
| 60 << strerror(errno); | |
| 61 | |
| 62 MOJO_CHECK(bytes_read == bytes_written) | |
|
viettrungluu
2016/02/26 00:14:21
EXPECT_EQ() (and this will cover the above check).
Forrest Reiling
2016/03/02 23:18:40
Done.
| |
| 63 << "Read a different number of bytes from pipe than were written to pipe"; | |
| 64 EXPECT_EQ(write_buffer, read_buffer); | |
| 65 | |
| 66 close(pipe_fds[0]); | |
|
viettrungluu
2016/02/26 00:14:22
ASSERT_EQ(0, close(...));
(or maybe EXPECT_EQ)
Forrest Reiling
2016/03/02 23:18:40
Done.
| |
| 67 close(unwrapped_handle); | |
| 68 MojoClose(wrapper); | |
|
viettrungluu
2016/02/26 00:14:21
ASSERT_EQ(MOJO_RESULT_OK, MojoClose(wrapper));
Forrest Reiling
2016/03/02 23:18:39
Done.
| |
| 69 } | |
| 70 | |
| 71 } // namespace | |
| 72 } // namespace mojo | |
| OLD | NEW |