OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/protocol/http_ice_config_request.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "remoting/base/url_request.h" |
| 10 #include "remoting/protocol/ice_config.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace remoting { |
| 14 namespace protocol { |
| 15 |
| 16 namespace { |
| 17 |
| 18 class FakeUrlRequest : public UrlRequest { |
| 19 public: |
| 20 FakeUrlRequest(const Result& result) : result_(result) {} |
| 21 ~FakeUrlRequest() override {} |
| 22 |
| 23 // UrlRequest interface. |
| 24 void AddHeader(const std::string& value) override { NOTREACHED(); } |
| 25 |
| 26 void SetPostData(const std::string& content_type, |
| 27 const std::string& post_data) override { |
| 28 EXPECT_EQ("application/json", content_type); |
| 29 EXPECT_EQ("", post_data); |
| 30 } |
| 31 |
| 32 void Start(const OnResultCallback& on_result_callback) override { |
| 33 on_result_callback.Run(result_); |
| 34 } |
| 35 |
| 36 private: |
| 37 Result result_; |
| 38 }; |
| 39 |
| 40 class FakeUrlRequestFactory : public UrlRequestFactory { |
| 41 public: |
| 42 FakeUrlRequestFactory() {} |
| 43 ~FakeUrlRequestFactory() override {} |
| 44 |
| 45 void SetResult(const std::string& url, const UrlRequest::Result& result) { |
| 46 results_[url] = result; |
| 47 } |
| 48 |
| 49 // UrlRequestFactory interface. |
| 50 scoped_ptr<UrlRequest> CreateUrlRequest(UrlRequest::Type type, |
| 51 const std::string& url) override { |
| 52 EXPECT_EQ(UrlRequest::Type::POST, type); |
| 53 CHECK(results_.count(url)); |
| 54 return make_scoped_ptr(new FakeUrlRequest(results_[url])); |
| 55 } |
| 56 |
| 57 std::map<std::string, UrlRequest::Result> results_; |
| 58 }; |
| 59 |
| 60 bool operator==(const cricket::ProtocolAddress& a, |
| 61 const cricket::ProtocolAddress& b) { |
| 62 return a.address == b.address && a.proto == b.proto && a.secure == b.secure; |
| 63 } |
| 64 bool operator==(const cricket::RelayServerConfig& a, |
| 65 const cricket::RelayServerConfig& b) { |
| 66 if (a.ports.size() != b.ports.size()) |
| 67 return false; |
| 68 for (size_t i = 0; i < a.ports.size(); ++i) { |
| 69 if (!(a.ports[i] == b.ports[i])) |
| 70 return false; |
| 71 } |
| 72 return a.type == b.type && |
| 73 a.credentials.username == b.credentials.username && |
| 74 a.credentials.password == b.credentials.password; |
| 75 } |
| 76 |
| 77 } // namespace |
| 78 |
| 79 static const char kTestUrl[] = "http://host/ice_config"; |
| 80 |
| 81 class HttpIceConfigRequestTest : public testing::Test { |
| 82 public: |
| 83 void OnResult(const IceConfig& config) { |
| 84 received_config_ = make_scoped_ptr(new IceConfig(config)); |
| 85 } |
| 86 |
| 87 protected: |
| 88 FakeUrlRequestFactory url_request_factory_; |
| 89 scoped_ptr<HttpIceConfigRequest> request_; |
| 90 scoped_ptr<IceConfig> received_config_; |
| 91 }; |
| 92 |
| 93 TEST_F(HttpIceConfigRequestTest, Parse) { |
| 94 const char kTestResponse[] = |
| 95 "{" |
| 96 " \"lifetimeDuration\": \"43200.000s\"," |
| 97 " \"iceServers\": [" |
| 98 " {" |
| 99 " \"urls\": [" |
| 100 " \"turn:8.8.8.8:19234\"," |
| 101 " \"turn:[2001:4860:4860::8888]:333\"," |
| 102 " \"turn:[2001:4860:4860::8888]\"," |
| 103 " \"turn:[2001:4860:4860::8888]:333?transport=tcp\"," |
| 104 " \"turns:the_server.com\"," |
| 105 " \"turns:the_server.com?transport=udp\"" |
| 106 " ]," |
| 107 " \"username\": \"123\"," |
| 108 " \"credential\": \"abc\"" |
| 109 " }," |
| 110 " {" |
| 111 " \"urls\": [" |
| 112 " \"stun:stun_server.com:18344\"," |
| 113 " \"stun:1.2.3.4\"" |
| 114 " ]" |
| 115 " }" |
| 116 " ]" |
| 117 "}"; |
| 118 url_request_factory_.SetResult(kTestUrl, |
| 119 UrlRequest::Result(200, kTestResponse)); |
| 120 request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl)); |
| 121 request_->Send( |
| 122 base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this))); |
| 123 ASSERT_FALSE(received_config_->is_null()); |
| 124 |
| 125 // lifetimeDuration in the config is set to 12 hours. HttpIceConfigRequest |
| 126 // substracts 1 hour. Verify that |expiration_time| is in the rage from 10 to |
| 127 // 12 hours from now. |
| 128 EXPECT_TRUE(base::Time::Now() + base::TimeDelta::FromHours(10) < |
| 129 received_config_->expiration_time); |
| 130 EXPECT_TRUE(received_config_->expiration_time < |
| 131 base::Time::Now() + base::TimeDelta::FromHours(12)); |
| 132 |
| 133 EXPECT_EQ(6U, received_config_->turn_servers.size()); |
| 134 EXPECT_TRUE(cricket::RelayServerConfig("8.8.8.8", 19234, "123", "abc", |
| 135 cricket::PROTO_UDP, false) == |
| 136 received_config_->turn_servers[0]); |
| 137 EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 333, "123", |
| 138 "abc", cricket::PROTO_UDP, false) == |
| 139 received_config_->turn_servers[1]); |
| 140 EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 3478, "123", |
| 141 "abc", cricket::PROTO_UDP, false) == |
| 142 received_config_->turn_servers[2]); |
| 143 EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 333, "123", |
| 144 "abc", cricket::PROTO_TCP, false) == |
| 145 received_config_->turn_servers[3]); |
| 146 EXPECT_TRUE(cricket::RelayServerConfig("the_server.com", 5349, "123", "abc", |
| 147 cricket::PROTO_TCP, true) == |
| 148 received_config_->turn_servers[4]); |
| 149 EXPECT_TRUE(cricket::RelayServerConfig("the_server.com", 5349, "123", "abc", |
| 150 cricket::PROTO_UDP, true) == |
| 151 received_config_->turn_servers[5]); |
| 152 |
| 153 EXPECT_EQ(2U, received_config_->stun_servers.size()); |
| 154 EXPECT_EQ(rtc::SocketAddress("stun_server.com", 18344), |
| 155 received_config_->stun_servers[0]); |
| 156 EXPECT_EQ(rtc::SocketAddress("1.2.3.4", 3478), |
| 157 received_config_->stun_servers[1]); |
| 158 } |
| 159 |
| 160 // Verify that we can still proceed if some servers cannot be parsed. |
| 161 TEST_F(HttpIceConfigRequestTest, ParsePartiallyInvalid) { |
| 162 const char kTestResponse[] = |
| 163 "{" |
| 164 " \"lifetimeDuration\": \"43200.000s\"," |
| 165 " \"iceServers\": [" |
| 166 " {" |
| 167 " \"urls\": [" |
| 168 " \"InvalidURL\"," |
| 169 " \"turn:[2001:4860:4860::8888]:333\"" |
| 170 " ]," |
| 171 " \"username\": \"123\"," |
| 172 " \"credential\": \"abc\"" |
| 173 " }," |
| 174 " \"42\"" |
| 175 " ]" |
| 176 "}"; |
| 177 url_request_factory_.SetResult(kTestUrl, |
| 178 UrlRequest::Result(200, kTestResponse)); |
| 179 request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl)); |
| 180 request_->Send( |
| 181 base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this))); |
| 182 ASSERT_FALSE(received_config_->is_null()); |
| 183 |
| 184 // Config should be already expired because it couldn't be parsed. |
| 185 EXPECT_TRUE(received_config_->expiration_time < base::Time::Now()); |
| 186 |
| 187 EXPECT_EQ(1U, received_config_->turn_servers.size()); |
| 188 EXPECT_TRUE(cricket::RelayServerConfig("2001:4860:4860::8888", 333, "123", |
| 189 "abc", cricket::PROTO_UDP, false) == |
| 190 received_config_->turn_servers[0]); |
| 191 } |
| 192 |
| 193 TEST_F(HttpIceConfigRequestTest, NotParseable) { |
| 194 url_request_factory_.SetResult(kTestUrl, |
| 195 UrlRequest::Result(200, "ERROR")); |
| 196 request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl)); |
| 197 request_->Send( |
| 198 base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this))); |
| 199 EXPECT_TRUE(received_config_->is_null()); |
| 200 } |
| 201 |
| 202 TEST_F(HttpIceConfigRequestTest, FailedRequest) { |
| 203 url_request_factory_.SetResult(kTestUrl, UrlRequest::Result::Failed()); |
| 204 request_.reset(new HttpIceConfigRequest(&url_request_factory_, kTestUrl)); |
| 205 request_->Send( |
| 206 base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this))); |
| 207 EXPECT_TRUE(received_config_->is_null()); |
| 208 } |
| 209 |
| 210 } // namespace protocol |
| 211 } // namespace remoting |
OLD | NEW |