| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <string.h> | 5 #include <string.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 | 21 |
| 22 using ProcessImplTest = ProcessTestBase; | 22 using ProcessImplTest = ProcessTestBase; |
| 23 | 23 |
| 24 // This also (slightly) tests |Wait()|, since we want to have some evidence that | 24 // This also (slightly) tests |Wait()|, since we want to have some evidence that |
| 25 // we ran the specified binary (/bin/true versus /bin/false). | 25 // we ran the specified binary (/bin/true versus /bin/false). |
| 26 TEST_F(ProcessImplTest, Spawn) { | 26 TEST_F(ProcessImplTest, Spawn) { |
| 27 mojo::files::Error error; | 27 mojo::files::Error error; |
| 28 | 28 |
| 29 { | 29 { |
| 30 ProcessControllerPtr process_controller; | 30 ProcessControllerPtr process_controller; |
| 31 error = mojo::files::ERROR_INTERNAL; | 31 error = mojo::files::Error::INTERNAL; |
| 32 process()->Spawn("/bin/true", mojo::Array<mojo::String>(), | 32 process()->Spawn("/bin/true", mojo::Array<mojo::String>(), |
| 33 mojo::Array<mojo::String>(), nullptr, nullptr, nullptr, | 33 mojo::Array<mojo::String>(), nullptr, nullptr, nullptr, |
| 34 GetProxy(&process_controller), Capture(&error)); | 34 GetProxy(&process_controller), Capture(&error)); |
| 35 ASSERT_TRUE(process().WaitForIncomingResponse()); | 35 ASSERT_TRUE(process().WaitForIncomingResponse()); |
| 36 EXPECT_EQ(mojo::files::ERROR_OK, error); | 36 EXPECT_EQ(mojo::files::Error::OK, error); |
| 37 | 37 |
| 38 error = mojo::files::ERROR_INTERNAL; | 38 error = mojo::files::Error::INTERNAL; |
| 39 int32_t exit_status = 42; | 39 int32_t exit_status = 42; |
| 40 process_controller->Wait(Capture(&error, &exit_status)); | 40 process_controller->Wait(Capture(&error, &exit_status)); |
| 41 ASSERT_TRUE(process_controller.WaitForIncomingResponse()); | 41 ASSERT_TRUE(process_controller.WaitForIncomingResponse()); |
| 42 EXPECT_EQ(mojo::files::ERROR_OK, error); | 42 EXPECT_EQ(mojo::files::Error::OK, error); |
| 43 EXPECT_EQ(0, exit_status); | 43 EXPECT_EQ(0, exit_status); |
| 44 } | 44 } |
| 45 | 45 |
| 46 { | 46 { |
| 47 ProcessControllerPtr process_controller; | 47 ProcessControllerPtr process_controller; |
| 48 error = mojo::files::ERROR_INTERNAL; | 48 error = mojo::files::Error::INTERNAL; |
| 49 process()->Spawn("/bin/false", mojo::Array<mojo::String>(), | 49 process()->Spawn("/bin/false", mojo::Array<mojo::String>(), |
| 50 mojo::Array<mojo::String>(), nullptr, nullptr, nullptr, | 50 mojo::Array<mojo::String>(), nullptr, nullptr, nullptr, |
| 51 GetProxy(&process_controller), Capture(&error)); | 51 GetProxy(&process_controller), Capture(&error)); |
| 52 ASSERT_TRUE(process().WaitForIncomingResponse()); | 52 ASSERT_TRUE(process().WaitForIncomingResponse()); |
| 53 EXPECT_EQ(mojo::files::ERROR_OK, error); | 53 EXPECT_EQ(mojo::files::Error::OK, error); |
| 54 | 54 |
| 55 error = mojo::files::ERROR_INTERNAL; | 55 error = mojo::files::Error::INTERNAL; |
| 56 int32_t exit_status = 0; | 56 int32_t exit_status = 0; |
| 57 process_controller->Wait(Capture(&error, &exit_status)); | 57 process_controller->Wait(Capture(&error, &exit_status)); |
| 58 ASSERT_TRUE(process_controller.WaitForIncomingResponse()); | 58 ASSERT_TRUE(process_controller.WaitForIncomingResponse()); |
| 59 EXPECT_EQ(mojo::files::ERROR_OK, error); | 59 EXPECT_EQ(mojo::files::Error::OK, error); |
| 60 EXPECT_NE(exit_status, 0); | 60 EXPECT_NE(exit_status, 0); |
| 61 } | 61 } |
| 62 } | 62 } |
| 63 | 63 |
| 64 void QuitMessageLoop() { | 64 void QuitMessageLoop() { |
| 65 base::MessageLoop::current()->QuitWhenIdle(); | 65 base::MessageLoop::current()->QuitWhenIdle(); |
| 66 } | 66 } |
| 67 | 67 |
| 68 void RunMessageLoop() { | 68 void RunMessageLoop() { |
| 69 base::MessageLoop::current()->Run(); | 69 base::MessageLoop::current()->Run(); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 TEST_F(ProcessImplTest, SpawnRedirectStdout) { | 101 TEST_F(ProcessImplTest, SpawnRedirectStdout) { |
| 102 static const char kOutput[] = "hello mojo!"; | 102 static const char kOutput[] = "hello mojo!"; |
| 103 | 103 |
| 104 mojo::files::FilePtr file; | 104 mojo::files::FilePtr file; |
| 105 CaptureOutputFile file_impl(GetProxy(&file)); | 105 CaptureOutputFile file_impl(GetProxy(&file)); |
| 106 | 106 |
| 107 mojo::Array<mojo::String> argv; | 107 mojo::Array<mojo::String> argv; |
| 108 argv.push_back("/bin/echo"); | 108 argv.push_back("/bin/echo"); |
| 109 argv.push_back(kOutput); | 109 argv.push_back(kOutput); |
| 110 ProcessControllerPtr process_controller; | 110 ProcessControllerPtr process_controller; |
| 111 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 111 mojo::files::Error error = mojo::files::Error::INTERNAL; |
| 112 process()->Spawn("/bin/echo", argv.Pass(), mojo::Array<mojo::String>(), | 112 process()->Spawn("/bin/echo", argv.Pass(), mojo::Array<mojo::String>(), |
| 113 nullptr, file.Pass(), nullptr, GetProxy(&process_controller), | 113 nullptr, file.Pass(), nullptr, GetProxy(&process_controller), |
| 114 Capture(&error)); | 114 Capture(&error)); |
| 115 ASSERT_TRUE(process().WaitForIncomingResponse()); | 115 ASSERT_TRUE(process().WaitForIncomingResponse()); |
| 116 EXPECT_EQ(mojo::files::ERROR_OK, error); | 116 EXPECT_EQ(mojo::files::Error::OK, error); |
| 117 | 117 |
| 118 // Since |file|'s impl is on our thread, we have to spin our message loop. | 118 // Since |file|'s impl is on our thread, we have to spin our message loop. |
| 119 RunMessageLoop(); | 119 RunMessageLoop(); |
| 120 // /bin/echo adds a newline (alas, POSIX /bin/echo doesn't specify "-n"). | 120 // /bin/echo adds a newline (alas, POSIX /bin/echo doesn't specify "-n"). |
| 121 EXPECT_EQ(std::string(kOutput) + "\n", file_impl.output()); | 121 EXPECT_EQ(std::string(kOutput) + "\n", file_impl.output()); |
| 122 | 122 |
| 123 error = mojo::files::ERROR_INTERNAL; | 123 error = mojo::files::Error::INTERNAL; |
| 124 int32_t exit_status = 0; | 124 int32_t exit_status = 0; |
| 125 process_controller->Wait(Capture(&error, &exit_status)); | 125 process_controller->Wait(Capture(&error, &exit_status)); |
| 126 ASSERT_TRUE(process_controller.WaitForIncomingResponse()); | 126 ASSERT_TRUE(process_controller.WaitForIncomingResponse()); |
| 127 EXPECT_EQ(mojo::files::ERROR_OK, error); | 127 EXPECT_EQ(mojo::files::Error::OK, error); |
| 128 EXPECT_EQ(0, exit_status); | 128 EXPECT_EQ(0, exit_status); |
| 129 | 129 |
| 130 // TODO(vtl): Currently, |file| won't be closed until the process controller | 130 // TODO(vtl): Currently, |file| won't be closed until the process controller |
| 131 // is closed, even if the child has been waited on and all output read. | 131 // is closed, even if the child has been waited on and all output read. |
| 132 // Possibly this should be changed, but we currently can't since the I/O | 132 // Possibly this should be changed, but we currently can't since the I/O |
| 133 // thread doesn't have facilities for informing us that an FD will never be | 133 // thread doesn't have facilities for informing us that an FD will never be |
| 134 // readable. | 134 // readable. |
| 135 EXPECT_FALSE(file_impl.is_closed()); | 135 EXPECT_FALSE(file_impl.is_closed()); |
| 136 process_controller.reset(); | 136 process_controller.reset(); |
| 137 RunMessageLoop(); | 137 RunMessageLoop(); |
| 138 EXPECT_TRUE(file_impl.is_closed()); | 138 EXPECT_TRUE(file_impl.is_closed()); |
| 139 } | 139 } |
| 140 | 140 |
| 141 } // namespace | 141 } // namespace |
| 142 } // namespace native_support | 142 } // namespace native_support |
| OLD | NEW |