http_ice_config_request_unittest.cc 5.34 KB
Newer Older
sergeyu's avatar
sergeyu committed
1 2 3 4 5 6 7 8
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/http_ice_config_request.h"

#include "base/bind.h"
#include "base/logging.h"
9
#include "base/memory/ptr_util.h"
10
#include "base/message_loop/message_loop.h"
11
#include "base/run_loop.h"
12
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
13
#include "remoting/base/fake_oauth_token_getter.h"
sergeyu's avatar
sergeyu committed
14 15 16 17 18 19 20 21 22
#include "remoting/base/url_request.h"
#include "remoting/protocol/ice_config.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {
namespace protocol {

namespace {

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
const char kTestResponse[] =
    "{"
    "  \"lifetimeDuration\": \"43200.000s\","
    "  \"iceServers\": ["
    "    {"
    "      \"urls\": ["
    "        \"turns:the_server.com\""
    "      ],"
    "      \"username\": \"123\","
    "      \"credential\": \"abc\""
    "    },"
    "    {"
    "      \"urls\": ["
    "        \"stun:stun_server.com:18344\""
    "      ]"
    "    }"
    "  ]"
    "}";
const char kTestOAuthToken[] = "TestOAuthToken";

sergeyu's avatar
sergeyu committed
43 44
class FakeUrlRequest : public UrlRequest {
 public:
45 46
  FakeUrlRequest(const Result& result, bool expect_oauth_token)
      : result_(result), expect_oauth_token_(expect_oauth_token) {}
sergeyu's avatar
sergeyu committed
47 48 49
  ~FakeUrlRequest() override {}

  // UrlRequest interface.
50 51 52 53 54
  void AddHeader(const std::string& value) override {
    EXPECT_TRUE(expect_oauth_token_);
    EXPECT_EQ(value, std::string("Authorization:Bearer ") + kTestOAuthToken);
    expect_oauth_token_ = false;
  }
sergeyu's avatar
sergeyu committed
55 56 57 58 59 60 61 62

  void SetPostData(const std::string& content_type,
                   const std::string& post_data) override {
    EXPECT_EQ("application/json", content_type);
    EXPECT_EQ("", post_data);
  }

  void Start(const OnResultCallback& on_result_callback) override {
63
    EXPECT_FALSE(expect_oauth_token_);
sergeyu's avatar
sergeyu committed
64 65 66 67 68
    on_result_callback.Run(result_);
  }

 private:
  Result result_;
69
  bool expect_oauth_token_;
sergeyu's avatar
sergeyu committed
70 71 72 73 74 75 76 77 78 79 80
};

class FakeUrlRequestFactory : public UrlRequestFactory {
 public:
  FakeUrlRequestFactory() {}
  ~FakeUrlRequestFactory() override {}

  void SetResult(const std::string& url, const UrlRequest::Result& result) {
    results_[url] = result;
  }

81 82 83 84
  void set_expect_oauth_token(bool expect_oauth_token) {
    expect_oauth_token_ = expect_oauth_token;
  }

sergeyu's avatar
sergeyu committed
85
  // UrlRequestFactory interface.
86 87
  std::unique_ptr<UrlRequest> CreateUrlRequest(
      UrlRequest::Type type,
88 89
      const std::string& url,
      const net::NetworkTrafficAnnotationTag& traffic_annotation) override {
90 91 92
    EXPECT_EQ(UrlRequest::Type::GET, type);
    EXPECT_TRUE(results_.count(url));
    return base::MakeUnique<FakeUrlRequest>(results_[url], expect_oauth_token_);
sergeyu's avatar
sergeyu committed
93 94
  }

95
 private:
sergeyu's avatar
sergeyu committed
96
  std::map<std::string, UrlRequest::Result> results_;
97 98

  bool expect_oauth_token_ = false;
sergeyu's avatar
sergeyu committed
99 100 101 102 103 104 105 106 107
};

}  // namespace

static const char kTestUrl[] = "http://host/ice_config";

class HttpIceConfigRequestTest : public testing::Test {
 public:
  void OnResult(const IceConfig& config) {
108
    received_config_ = base::MakeUnique<IceConfig>(config);
sergeyu's avatar
sergeyu committed
109 110 111
  }

 protected:
112
  base::MessageLoop message_loop_;
sergeyu's avatar
sergeyu committed
113
  FakeUrlRequestFactory url_request_factory_;
114 115
  std::unique_ptr<HttpIceConfigRequest> request_;
  std::unique_ptr<IceConfig> received_config_;
sergeyu's avatar
sergeyu committed
116 117 118 119 120
};

TEST_F(HttpIceConfigRequestTest, Parse) {
  url_request_factory_.SetResult(kTestUrl,
                                 UrlRequest::Result(200, kTestResponse));
121 122
  request_.reset(
      new HttpIceConfigRequest(&url_request_factory_, kTestUrl, nullptr));
sergeyu's avatar
sergeyu committed
123 124 125 126 127
  request_->Send(
      base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
  ASSERT_FALSE(received_config_->is_null());

  EXPECT_EQ(1U, received_config_->turn_servers.size());
128
  EXPECT_EQ(1U, received_config_->stun_servers.size());
sergeyu's avatar
sergeyu committed
129 130
}

131
TEST_F(HttpIceConfigRequestTest, InvalidConfig) {
sergeyu's avatar
sergeyu committed
132 133
  url_request_factory_.SetResult(kTestUrl,
                                 UrlRequest::Result(200, "ERROR"));
134 135
  request_.reset(
      new HttpIceConfigRequest(&url_request_factory_, kTestUrl, nullptr));
sergeyu's avatar
sergeyu committed
136 137
  request_->Send(
      base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
138
  base::RunLoop().RunUntilIdle();
sergeyu's avatar
sergeyu committed
139 140 141 142 143
  EXPECT_TRUE(received_config_->is_null());
}

TEST_F(HttpIceConfigRequestTest, FailedRequest) {
  url_request_factory_.SetResult(kTestUrl, UrlRequest::Result::Failed());
144 145
  request_.reset(
      new HttpIceConfigRequest(&url_request_factory_, kTestUrl, nullptr));
sergeyu's avatar
sergeyu committed
146 147
  request_->Send(
      base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
148
  base::RunLoop().RunUntilIdle();
sergeyu's avatar
sergeyu committed
149 150 151
  EXPECT_TRUE(received_config_->is_null());
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
TEST_F(HttpIceConfigRequestTest, Authentication) {
  url_request_factory_.SetResult(kTestUrl,
                                 UrlRequest::Result(200, kTestResponse));
  url_request_factory_.set_expect_oauth_token(true);

  FakeOAuthTokenGetter token_getter(OAuthTokenGetter::SUCCESS,
                                    "user@example.com", kTestOAuthToken);
  request_ = base::MakeUnique<HttpIceConfigRequest>(&url_request_factory_,
                                                    kTestUrl, &token_getter);
  request_->Send(
      base::Bind(&HttpIceConfigRequestTest::OnResult, base::Unretained(this)));
  base::RunLoop().RunUntilIdle();
  ASSERT_TRUE(received_config_);

  EXPECT_EQ(1U, received_config_->turn_servers.size());
  EXPECT_EQ(1U, received_config_->stun_servers.size());
}

sergeyu's avatar
sergeyu committed
170 171
}  // namespace protocol
}  // namespace remoting