| 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 "base/posix/file_descriptor_shuffle.h" | 5 #include "base/posix/file_descriptor_shuffle.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" | 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 | 7 |
| 8 namespace { | 8 namespace { |
| 9 | 9 |
| 10 // 'Duplicated' file descriptors start at this number | 10 // 'Duplicated' file descriptors start at this number |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 int fd1; | 37 int fd1; |
| 38 int fd2; | 38 int fd2; |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 class InjectionTracer : public InjectionDelegate { | 41 class InjectionTracer : public InjectionDelegate { |
| 42 public: | 42 public: |
| 43 InjectionTracer() | 43 InjectionTracer() |
| 44 : next_duplicate_(kDuplicateBase) { | 44 : next_duplicate_(kDuplicateBase) { |
| 45 } | 45 } |
| 46 | 46 |
| 47 virtual bool Duplicate(int* result, int fd) OVERRIDE { | 47 bool Duplicate(int* result, int fd) override { |
| 48 *result = next_duplicate_++; | 48 *result = next_duplicate_++; |
| 49 actions_.push_back(Action(Action::DUPLICATE, *result, fd)); | 49 actions_.push_back(Action(Action::DUPLICATE, *result, fd)); |
| 50 return true; | 50 return true; |
| 51 } | 51 } |
| 52 | 52 |
| 53 virtual bool Move(int src, int dest) OVERRIDE { | 53 bool Move(int src, int dest) override { |
| 54 actions_.push_back(Action(Action::MOVE, src, dest)); | 54 actions_.push_back(Action(Action::MOVE, src, dest)); |
| 55 return true; | 55 return true; |
| 56 } | 56 } |
| 57 | 57 |
| 58 virtual void Close(int fd) OVERRIDE { | 58 void Close(int fd) override { actions_.push_back(Action(Action::CLOSE, fd)); } |
| 59 actions_.push_back(Action(Action::CLOSE, fd)); | |
| 60 } | |
| 61 | 59 |
| 62 const std::vector<Action>& actions() const { return actions_; } | 60 const std::vector<Action>& actions() const { return actions_; } |
| 63 | 61 |
| 64 private: | 62 private: |
| 65 int next_duplicate_; | 63 int next_duplicate_; |
| 66 std::vector<Action> actions_; | 64 std::vector<Action> actions_; |
| 67 }; | 65 }; |
| 68 | 66 |
| 69 TEST(FileDescriptorShuffleTest, Empty) { | 67 TEST(FileDescriptorShuffleTest, Empty) { |
| 70 InjectiveMultimap map; | 68 InjectiveMultimap map; |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 | 241 |
| 244 EXPECT_TRUE(PerformInjectiveMultimap(map, &tracer)); | 242 EXPECT_TRUE(PerformInjectiveMultimap(map, &tracer)); |
| 245 ASSERT_EQ(3u, tracer.actions().size()); | 243 ASSERT_EQ(3u, tracer.actions().size()); |
| 246 EXPECT_TRUE(tracer.actions()[0] == Action(Action::MOVE, 0, 1)); | 244 EXPECT_TRUE(tracer.actions()[0] == Action(Action::MOVE, 0, 1)); |
| 247 EXPECT_TRUE(tracer.actions()[1] == Action(Action::MOVE, 0, 2)); | 245 EXPECT_TRUE(tracer.actions()[1] == Action(Action::MOVE, 0, 2)); |
| 248 EXPECT_TRUE(tracer.actions()[2] == Action(Action::CLOSE, 0)); | 246 EXPECT_TRUE(tracer.actions()[2] == Action(Action::CLOSE, 0)); |
| 249 } | 247 } |
| 250 | 248 |
| 251 class FailingDelegate : public InjectionDelegate { | 249 class FailingDelegate : public InjectionDelegate { |
| 252 public: | 250 public: |
| 253 virtual bool Duplicate(int* result, int fd) OVERRIDE { | 251 bool Duplicate(int* result, int fd) override { return false; } |
| 254 return false; | |
| 255 } | |
| 256 | 252 |
| 257 virtual bool Move(int src, int dest) OVERRIDE { | 253 bool Move(int src, int dest) override { return false; } |
| 258 return false; | |
| 259 } | |
| 260 | 254 |
| 261 virtual void Close(int fd) OVERRIDE {} | 255 void Close(int fd) override {} |
| 262 }; | 256 }; |
| 263 | 257 |
| 264 TEST(FileDescriptorShuffleTest, EmptyWithFailure) { | 258 TEST(FileDescriptorShuffleTest, EmptyWithFailure) { |
| 265 InjectiveMultimap map; | 259 InjectiveMultimap map; |
| 266 FailingDelegate failing; | 260 FailingDelegate failing; |
| 267 | 261 |
| 268 EXPECT_TRUE(PerformInjectiveMultimap(map, &failing)); | 262 EXPECT_TRUE(PerformInjectiveMultimap(map, &failing)); |
| 269 } | 263 } |
| 270 | 264 |
| 271 TEST(FileDescriptorShuffleTest, NoopWithFailure) { | 265 TEST(FileDescriptorShuffleTest, NoopWithFailure) { |
| 272 InjectiveMultimap map; | 266 InjectiveMultimap map; |
| 273 FailingDelegate failing; | 267 FailingDelegate failing; |
| 274 map.push_back(InjectionArc(0, 0, false)); | 268 map.push_back(InjectionArc(0, 0, false)); |
| 275 | 269 |
| 276 EXPECT_TRUE(PerformInjectiveMultimap(map, &failing)); | 270 EXPECT_TRUE(PerformInjectiveMultimap(map, &failing)); |
| 277 } | 271 } |
| 278 | 272 |
| 279 TEST(FileDescriptorShuffleTest, Simple1WithFailure) { | 273 TEST(FileDescriptorShuffleTest, Simple1WithFailure) { |
| 280 InjectiveMultimap map; | 274 InjectiveMultimap map; |
| 281 FailingDelegate failing; | 275 FailingDelegate failing; |
| 282 map.push_back(InjectionArc(0, 1, false)); | 276 map.push_back(InjectionArc(0, 1, false)); |
| 283 | 277 |
| 284 EXPECT_FALSE(PerformInjectiveMultimap(map, &failing)); | 278 EXPECT_FALSE(PerformInjectiveMultimap(map, &failing)); |
| 285 } | 279 } |
| 286 | 280 |
| 287 } // namespace base | 281 } // namespace base |
| OLD | NEW |