| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <gtest/gtest.h> | 5 #include <gtest/gtest.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 virtual void StartRegistryTest(ProcessProxyRegistry* registry) = 0; | 34 virtual void StartRegistryTest(ProcessProxyRegistry* registry) = 0; |
| 35 | 35 |
| 36 protected: | 36 protected: |
| 37 pid_t pid_; | 37 pid_t pid_; |
| 38 }; | 38 }; |
| 39 | 39 |
| 40 class RegistryTestRunner : public TestRunner { | 40 class RegistryTestRunner : public TestRunner { |
| 41 public: | 41 public: |
| 42 virtual ~RegistryTestRunner() {} | 42 virtual ~RegistryTestRunner() {} |
| 43 | 43 |
| 44 virtual void SetupExpectations(pid_t pid) OVERRIDE { | 44 virtual void SetupExpectations(pid_t pid) override { |
| 45 pid_ = pid; | 45 pid_ = pid; |
| 46 left_to_check_index_[0] = 0; | 46 left_to_check_index_[0] = 0; |
| 47 left_to_check_index_[1] = 0; | 47 left_to_check_index_[1] = 0; |
| 48 // We consider that a line processing has started if a value in | 48 // We consider that a line processing has started if a value in |
| 49 // left_to_check__[index] is set to 0, thus -2. | 49 // left_to_check__[index] is set to 0, thus -2. |
| 50 lines_left_ = 2 * kTestLineNum - 2; | 50 lines_left_ = 2 * kTestLineNum - 2; |
| 51 expected_line_ = kTestLineExpected; | 51 expected_line_ = kTestLineExpected; |
| 52 } | 52 } |
| 53 | 53 |
| 54 // Method to test validity of received input. We will receive two streams of | 54 // Method to test validity of received input. We will receive two streams of |
| 55 // the same data. (input will be echoed twice by the testing process). Each | 55 // the same data. (input will be echoed twice by the testing process). Each |
| 56 // stream will contain the same string repeated |kTestLineNum| times. So we | 56 // stream will contain the same string repeated |kTestLineNum| times. So we |
| 57 // have to match 2 * |kTestLineNum| lines. The problem is the received lines | 57 // have to match 2 * |kTestLineNum| lines. The problem is the received lines |
| 58 // from different streams may be interleaved (e.g. we may receive | 58 // from different streams may be interleaved (e.g. we may receive |
| 59 // abc|abcdef|defgh|gh). To deal with that, we allow to test received text | 59 // abc|abcdef|defgh|gh). To deal with that, we allow to test received text |
| 60 // against two lines. The lines MUST NOT have two same characters for this | 60 // against two lines. The lines MUST NOT have two same characters for this |
| 61 // algorithm to work. | 61 // algorithm to work. |
| 62 virtual void OnSomeRead(pid_t pid, const std::string& type, | 62 virtual void OnSomeRead(pid_t pid, const std::string& type, |
| 63 const std::string& output) OVERRIDE { | 63 const std::string& output) override { |
| 64 EXPECT_EQ(type, kStdoutType); | 64 EXPECT_EQ(type, kStdoutType); |
| 65 EXPECT_EQ(pid_, pid); | 65 EXPECT_EQ(pid_, pid); |
| 66 | 66 |
| 67 bool valid = true; | 67 bool valid = true; |
| 68 for (size_t i = 0; i < output.length(); i++) { | 68 for (size_t i = 0; i < output.length(); i++) { |
| 69 // The character output[i] should be next in at least one of the lines we | 69 // The character output[i] should be next in at least one of the lines we |
| 70 // are testing. | 70 // are testing. |
| 71 valid = (ProcessReceivedCharacter(output[i], 0) || | 71 valid = (ProcessReceivedCharacter(output[i], 0) || |
| 72 ProcessReceivedCharacter(output[i], 1)); | 72 ProcessReceivedCharacter(output[i], 1)); |
| 73 EXPECT_TRUE(valid) << "Received: " << output; | 73 EXPECT_TRUE(valid) << "Received: " << output; |
| 74 } | 74 } |
| 75 | 75 |
| 76 if (!valid || TestSucceeded()) { | 76 if (!valid || TestSucceeded()) { |
| 77 base::MessageLoop::current()->PostTask(FROM_HERE, | 77 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 78 base::MessageLoop::QuitClosure()); | 78 base::MessageLoop::QuitClosure()); |
| 79 } | 79 } |
| 80 } | 80 } |
| 81 | 81 |
| 82 virtual void StartRegistryTest(ProcessProxyRegistry* registry) OVERRIDE { | 82 virtual void StartRegistryTest(ProcessProxyRegistry* registry) override { |
| 83 for (int i = 0; i < kTestLineNum; i++) { | 83 for (int i = 0; i < kTestLineNum; i++) { |
| 84 EXPECT_TRUE(registry->SendInput(pid_, kTestLineToSend)); | 84 EXPECT_TRUE(registry->SendInput(pid_, kTestLineToSend)); |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 private: | 88 private: |
| 89 bool ProcessReceivedCharacter(char received, size_t stream) { | 89 bool ProcessReceivedCharacter(char received, size_t stream) { |
| 90 if (stream >= arraysize(left_to_check_index_)) | 90 if (stream >= arraysize(left_to_check_index_)) |
| 91 return false; | 91 return false; |
| 92 bool success = left_to_check_index_[stream] < expected_line_.length() && | 92 bool success = left_to_check_index_[stream] < expected_line_.length() && |
| (...skipping 18 matching lines...) Expand all Loading... |
| 111 | 111 |
| 112 size_t left_to_check_index_[2]; | 112 size_t left_to_check_index_[2]; |
| 113 size_t lines_left_; | 113 size_t lines_left_; |
| 114 std::string expected_line_; | 114 std::string expected_line_; |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 class RegistryNotifiedOnProcessExitTestRunner : public TestRunner { | 117 class RegistryNotifiedOnProcessExitTestRunner : public TestRunner { |
| 118 public: | 118 public: |
| 119 virtual ~RegistryNotifiedOnProcessExitTestRunner() {} | 119 virtual ~RegistryNotifiedOnProcessExitTestRunner() {} |
| 120 | 120 |
| 121 virtual void SetupExpectations(pid_t pid) OVERRIDE { | 121 virtual void SetupExpectations(pid_t pid) override { |
| 122 output_received_ = false; | 122 output_received_ = false; |
| 123 pid_ = pid; | 123 pid_ = pid; |
| 124 } | 124 } |
| 125 | 125 |
| 126 virtual void OnSomeRead(pid_t pid, const std::string& type, | 126 virtual void OnSomeRead(pid_t pid, const std::string& type, |
| 127 const std::string& output) OVERRIDE { | 127 const std::string& output) override { |
| 128 EXPECT_EQ(pid_, pid); | 128 EXPECT_EQ(pid_, pid); |
| 129 if (!output_received_) { | 129 if (!output_received_) { |
| 130 output_received_ = true; | 130 output_received_ = true; |
| 131 EXPECT_EQ(type, "stdout"); | 131 EXPECT_EQ(type, "stdout"); |
| 132 EXPECT_EQ(output, "p"); | 132 EXPECT_EQ(output, "p"); |
| 133 base::KillProcess(pid_, 0 , true); | 133 base::KillProcess(pid_, 0 , true); |
| 134 return; | 134 return; |
| 135 } | 135 } |
| 136 EXPECT_EQ("exit", type); | 136 EXPECT_EQ("exit", type); |
| 137 base::MessageLoop::current()->PostTask(FROM_HERE, | 137 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 138 base::MessageLoop::QuitClosure()); | 138 base::MessageLoop::QuitClosure()); |
| 139 } | 139 } |
| 140 | 140 |
| 141 virtual void StartRegistryTest(ProcessProxyRegistry* registry) OVERRIDE { | 141 virtual void StartRegistryTest(ProcessProxyRegistry* registry) override { |
| 142 EXPECT_TRUE(registry->SendInput(pid_, "p")); | 142 EXPECT_TRUE(registry->SendInput(pid_, "p")); |
| 143 } | 143 } |
| 144 | 144 |
| 145 private: | 145 private: |
| 146 bool output_received_; | 146 bool output_received_; |
| 147 }; | 147 }; |
| 148 | 148 |
| 149 class SigIntTestRunner : public TestRunner { | 149 class SigIntTestRunner : public TestRunner { |
| 150 public: | 150 public: |
| 151 virtual ~SigIntTestRunner() {} | 151 virtual ~SigIntTestRunner() {} |
| 152 | 152 |
| 153 virtual void SetupExpectations(pid_t pid) OVERRIDE { | 153 virtual void SetupExpectations(pid_t pid) override { |
| 154 pid_ = pid; | 154 pid_ = pid; |
| 155 } | 155 } |
| 156 | 156 |
| 157 virtual void OnSomeRead(pid_t pid, const std::string& type, | 157 virtual void OnSomeRead(pid_t pid, const std::string& type, |
| 158 const std::string& output) OVERRIDE { | 158 const std::string& output) override { |
| 159 EXPECT_EQ(pid_, pid); | 159 EXPECT_EQ(pid_, pid); |
| 160 // We may receive ^C on stdout, but we don't care about that, as long as we | 160 // We may receive ^C on stdout, but we don't care about that, as long as we |
| 161 // eventually received exit event. | 161 // eventually received exit event. |
| 162 if (type == "exit") { | 162 if (type == "exit") { |
| 163 base::MessageLoop::current()->PostTask(FROM_HERE, | 163 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 164 base::MessageLoop::QuitClosure()); | 164 base::MessageLoop::QuitClosure()); |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 virtual void StartRegistryTest(ProcessProxyRegistry* registry) OVERRIDE { | 168 virtual void StartRegistryTest(ProcessProxyRegistry* registry) override { |
| 169 // Send SingInt and verify the process exited. | 169 // Send SingInt and verify the process exited. |
| 170 EXPECT_TRUE(registry->SendInput(pid_, "\003")); | 170 EXPECT_TRUE(registry->SendInput(pid_, "\003")); |
| 171 } | 171 } |
| 172 }; | 172 }; |
| 173 | 173 |
| 174 } // namespace | 174 } // namespace |
| 175 | 175 |
| 176 class ProcessProxyTest : public testing::Test { | 176 class ProcessProxyTest : public testing::Test { |
| 177 public: | 177 public: |
| 178 ProcessProxyTest() {} | 178 ProcessProxyTest() {} |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 245 } |
| 246 | 246 |
| 247 // Test verifies that \003 message send to process is processed as SigInt. | 247 // Test verifies that \003 message send to process is processed as SigInt. |
| 248 // Timing out on the waterfall: http://crbug.com/115064 | 248 // Timing out on the waterfall: http://crbug.com/115064 |
| 249 TEST_F(ProcessProxyTest, DISABLED_SigInt) { | 249 TEST_F(ProcessProxyTest, DISABLED_SigInt) { |
| 250 test_runner_.reset(new SigIntTestRunner()); | 250 test_runner_.reset(new SigIntTestRunner()); |
| 251 RunTest(); | 251 RunTest(); |
| 252 } | 252 } |
| 253 | 253 |
| 254 } // namespace chromeos | 254 } // namespace chromeos |
| OLD | NEW |