Index: mojo/public/platform/native/platform_handle_private_apptest.cc |
diff --git a/mojo/public/platform/native/platform_handle_private_apptest.cc b/mojo/public/platform/native/platform_handle_private_apptest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..49ffe835ad30beb5c64bb21fa74e583731fc75f5 |
--- /dev/null |
+++ b/mojo/public/platform/native/platform_handle_private_apptest.cc |
@@ -0,0 +1,72 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "mojo/public/platform/native/platform_handle_private.h" |
+ |
+#include <stdint.h> |
+#include <string.h> |
+#include <sys/types.h> |
+#include <unistd.h> |
+ |
+#include "mojo/public/cpp/application/application_impl.h" |
+#include "mojo/public/cpp/application/application_test_base.h" |
+#include "mojo/public/cpp/environment/logging.h" |
+#include "mojo/public/cpp/system/macros.h" |
+ |
+ |
+namespace mojo { |
+namespace { |
+ |
+// 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
|
+class PlatformHandlePrivateApplicationTest : public test::ApplicationTestBase { |
+ public: |
+ PlatformHandlePrivateApplicationTest() : ApplicationTestBase() {} |
+ ~PlatformHandlePrivateApplicationTest() override {} |
+ |
+ private: |
+ MOJO_DISALLOW_COPY_AND_ASSIGN(PlatformHandlePrivateApplicationTest); |
+}; |
+ |
+TEST_F(PlatformHandlePrivateApplicationTest, WrapAndUnwrapFileDescriptor) { |
+ MojoPlatformHandle original_handle; |
viettrungluu
2016/02/26 00:14:21
(Probably to -1.)
Forrest Reiling
2016/03/02 23:18:40
Done.
|
+ MojoPlatformHandle unwrapped_handle; |
+ 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.
|
+ |
+ int pipe_fds[2] = {-1, -1}; |
+ |
+ uint64_t write_buffer = 0xDEADBEEF; |
+ uint64_t read_buffer = 0; |
+ |
+ 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.
|
+ |
+ // Pass second FD through wrapper. |
+ original_handle = pipe_fds[1]; |
+ |
+ EXPECT_EQ(MOJO_RESULT_OK, |
+ MojoCreatePlatformHandleWrapper(original_handle, &wrapper)); |
+ EXPECT_EQ(MOJO_RESULT_OK, |
+ MojoExtractPlatformHandle(wrapper, &unwrapped_handle)); |
+ |
+ // Write to wrapped/unwrapped FD. |
+ ssize_t bytes_written = |
+ write(unwrapped_handle, &write_buffer, sizeof(write_buffer)); |
+ 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.
|
+ << "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.
|
+ |
+ // Read from other end of pipe. |
+ ssize_t bytes_read = read(pipe_fds[0], &read_buffer, sizeof(read_buffer)); |
+ 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
|
+ << strerror(errno); |
+ |
+ 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.
|
+ << "Read a different number of bytes from pipe than were written to pipe"; |
+ EXPECT_EQ(write_buffer, read_buffer); |
+ |
+ 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.
|
+ close(unwrapped_handle); |
+ 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.
|
+} |
+ |
+} // namespace |
+} // namespace mojo |