| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/pairing/fake_controller_pairing_controller.h" | 5 #include "components/pairing/fake_controller_pairing_controller.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 } | 30 } |
| 31 | 31 |
| 32 FakeControllerPairingController::~FakeControllerPairingController() { | 32 FakeControllerPairingController::~FakeControllerPairingController() { |
| 33 RemoveObserver(this); | 33 RemoveObserver(this); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void FakeControllerPairingController::ApplyConfig(const std::string& config) { | 36 void FakeControllerPairingController::ApplyConfig(const std::string& config) { |
| 37 typedef std::vector<std::string> Tokens; | 37 typedef std::vector<std::string> Tokens; |
| 38 | 38 |
| 39 base::StringPairs kv_pairs; | 39 base::StringPairs kv_pairs; |
| 40 CHECK(base::SplitStringIntoKeyValuePairs(config, ':', ',', &kv_pairs)) | 40 // Wrong config format. |
| 41 << "Wrong config format."; | 41 CHECK(base::SplitStringIntoKeyValuePairs(config, ':', ',', &kv_pairs)); |
| 42 std::map<std::string, std::string> dict(kv_pairs.begin(), kv_pairs.end()); | 42 std::map<std::string, std::string> dict(kv_pairs.begin(), kv_pairs.end()); |
| 43 | 43 |
| 44 if (dict.count("async_duration")) { | 44 if (dict.count("async_duration")) { |
| 45 int ms = 0; | 45 int ms = 0; |
| 46 CHECK(base::StringToInt(dict["async_duration"], &ms)) | 46 // Wrong 'async_duration' format. |
| 47 << "Wrong 'async_duration' format."; | 47 CHECK(base::StringToInt(dict["async_duration"], &ms)); |
| 48 async_duration_ = base::TimeDelta::FromMilliseconds(ms); | 48 async_duration_ = base::TimeDelta::FromMilliseconds(ms); |
| 49 } else { | 49 } else { |
| 50 async_duration_ = base::TimeDelta::FromMilliseconds(3000); | 50 async_duration_ = base::TimeDelta::FromMilliseconds(3000); |
| 51 } | 51 } |
| 52 | 52 |
| 53 should_fail_on_connecting_ = | 53 should_fail_on_connecting_ = |
| 54 dict.count("fail_connecting") && (dict["fail_connecting"] == "1"); | 54 dict.count("fail_connecting") && (dict["fail_connecting"] == "1"); |
| 55 | 55 |
| 56 enrollment_should_fail_ = | 56 enrollment_should_fail_ = |
| 57 dict.count("fail_enrollment") && (dict["fail_enrollment"] == "1"); | 57 dict.count("fail_enrollment") && (dict["fail_enrollment"] == "1"); |
| 58 | 58 |
| 59 if (dict.count("connection_lost")) { | 59 if (dict.count("connection_lost")) { |
| 60 Tokens lost_begin_end = base::SplitString( | 60 Tokens lost_begin_end = base::SplitString( |
| 61 dict["connection_lost"], "-", base::KEEP_WHITESPACE, | 61 dict["connection_lost"], "-", base::KEEP_WHITESPACE, |
| 62 base::SPLIT_WANT_NONEMPTY); | 62 base::SPLIT_WANT_NONEMPTY); |
| 63 CHECK_EQ(2u, lost_begin_end.size()) << "Wrong 'connection_lost' format."; | 63 // Wrong 'connection_lost' format. |
| 64 CHECK_EQ(2u, lost_begin_end.size()); |
| 64 int begin = 0; | 65 int begin = 0; |
| 65 int end = 0; | 66 int end = 0; |
| 67 // Wrong 'connection_lost' format. |
| 66 CHECK(base::StringToInt(lost_begin_end[0], &begin) && | 68 CHECK(base::StringToInt(lost_begin_end[0], &begin) && |
| 67 base::StringToInt(lost_begin_end[1], &end)) | 69 base::StringToInt(lost_begin_end[1], &end)); |
| 68 << "Wrong 'connection_lost' format."; | 70 // Wrong 'connection_lost' interval. |
| 69 CHECK((begin == 0 && end == 0) || | 71 CHECK((begin == 0 && end == 0) || |
| 70 (STAGE_WAITING_FOR_CODE_CONFIRMATION <= begin && begin <= end && | 72 (STAGE_WAITING_FOR_CODE_CONFIRMATION <= begin && begin <= end && |
| 71 end <= STAGE_HOST_ENROLLMENT_ERROR)) | 73 end <= STAGE_HOST_ENROLLMENT_ERROR)); |
| 72 << "Wrong 'connection_lost' interval."; | |
| 73 connection_lost_begin_ = static_cast<Stage>(begin); | 74 connection_lost_begin_ = static_cast<Stage>(begin); |
| 74 connection_lost_end_ = static_cast<Stage>(end); | 75 connection_lost_end_ = static_cast<Stage>(end); |
| 75 } else { | 76 } else { |
| 76 connection_lost_begin_ = connection_lost_end_ = STAGE_NONE; | 77 connection_lost_begin_ = connection_lost_end_ = STAGE_NONE; |
| 77 } | 78 } |
| 78 | 79 |
| 79 if (!dict.count("discovery")) { | 80 if (!dict.count("discovery")) { |
| 80 dict["discovery"] = | 81 dict["discovery"] = |
| 81 "F-Device_1~F-Device_5~F-Device_3~L-Device_3~L-Device_1~F-Device_1"; | 82 "F-Device_1~F-Device_5~F-Device_3~L-Device_3~L-Device_1~F-Device_1"; |
| 82 } | 83 } |
| 83 base::StringPairs events; | 84 base::StringPairs events; |
| 85 // Wrong 'discovery' format. |
| 84 CHECK( | 86 CHECK( |
| 85 base::SplitStringIntoKeyValuePairs(dict["discovery"], '-', '~', &events)) | 87 base::SplitStringIntoKeyValuePairs(dict["discovery"], '-', '~', &events)); |
| 86 << "Wrong 'discovery' format."; | |
| 87 DiscoveryScenario scenario; | 88 DiscoveryScenario scenario; |
| 88 for (const auto& event : events) { | 89 for (const auto& event : events) { |
| 89 const std::string& type = event.first; | 90 const std::string& type = event.first; |
| 90 const std::string& device_id = event.second; | 91 const std::string& device_id = event.second; |
| 91 CHECK(type == "F" || type == "L" || type == "N") | 92 // Wrong discovery event type. |
| 92 << "Wrong discovery event type."; | 93 CHECK(type == "F" || type == "L" || type == "N"); |
| 93 CHECK(!device_id.empty() || type == "N") << "Empty device ID."; | 94 // Empty device ID. |
| 95 CHECK(!device_id.empty() || type == "N"); |
| 94 scenario.push_back(DiscoveryEvent( | 96 scenario.push_back(DiscoveryEvent( |
| 95 type == "F" ? DEVICE_FOUND : type == "L" ? DEVICE_LOST : NOTHING_FOUND, | 97 type == "F" ? DEVICE_FOUND : type == "L" ? DEVICE_LOST : NOTHING_FOUND, |
| 96 device_id)); | 98 device_id)); |
| 97 } | 99 } |
| 98 SetDiscoveryScenario(scenario); | 100 SetDiscoveryScenario(scenario); |
| 99 | 101 |
| 100 preset_confirmation_code_ = dict["code"]; | 102 preset_confirmation_code_ = dict["code"]; |
| 103 // Wrong 'code' format. |
| 101 CHECK(preset_confirmation_code_.empty() || | 104 CHECK(preset_confirmation_code_.empty() || |
| 102 (preset_confirmation_code_.length() == 6 && | 105 (preset_confirmation_code_.length() == 6 && |
| 103 preset_confirmation_code_.find_first_not_of("0123456789") == | 106 preset_confirmation_code_.find_first_not_of("0123456789") == |
| 104 std::string::npos)) | 107 std::string::npos)); |
| 105 << "Wrong 'code' format."; | |
| 106 } | 108 } |
| 107 | 109 |
| 108 void FakeControllerPairingController::SetShouldFailOnConnecting() { | 110 void FakeControllerPairingController::SetShouldFailOnConnecting() { |
| 109 should_fail_on_connecting_ = true; | 111 should_fail_on_connecting_ = true; |
| 110 } | 112 } |
| 111 | 113 |
| 112 void FakeControllerPairingController::SetShouldLoseConnection(Stage stage_begin, | 114 void FakeControllerPairingController::SetShouldLoseConnection(Stage stage_begin, |
| 113 Stage stage_end) { | 115 Stage stage_end) { |
| 114 connection_lost_begin_ = stage_begin; | 116 connection_lost_begin_ = stage_begin; |
| 115 connection_lost_end_ = stage_end; | 117 connection_lost_end_ = stage_end; |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 next_stage = STAGE_HOST_CONNECTION_LOST; | 339 next_stage = STAGE_HOST_CONNECTION_LOST; |
| 338 } | 340 } |
| 339 if (next_stage != STAGE_NONE) | 341 if (next_stage != STAGE_NONE) |
| 340 ChangeStageLater(next_stage); | 342 ChangeStageLater(next_stage); |
| 341 } | 343 } |
| 342 | 344 |
| 343 void FakeControllerPairingController::DiscoveredDevicesListChanged() { | 345 void FakeControllerPairingController::DiscoveredDevicesListChanged() { |
| 344 } | 346 } |
| 345 | 347 |
| 346 } // namespace pairing_chromeos | 348 } // namespace pairing_chromeos |
| OLD | NEW |