1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/nearby_sharing/tachyon_ice_config_fetcher.h"
#include "base/logging.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "chrome/browser/nearby_sharing/proto/duration.pb.h"
#include "chrome/browser/nearby_sharing/proto/ice.pb.h"
#include "chrome/browser/nearby_sharing/proto/tachyon.pb.h"
#include "chrome/browser/nearby_sharing/proto/tachyon_common.pb.h"
#include "chrome/browser/nearby_sharing/proto/tachyon_enums.pb.h"
#include "components/signin/public/identity_manager/identity_test_environment.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
namespace tachyon_proto = nearbyshare::tachyon_proto;
const char kApiUrl[] =
"https://instantmessaging-pa.googleapis.com/v1/peertopeer:geticeserver";
const char kOAuthToken[] = "oauth_token";
const char kTestAccount[] = "test@test.test";
const char kIceConfigFetchedMetric[] = "Sharing.WebRtc.IceConfigFetched";
const char kResultMetric[] =
"Nearby.Connections.InstantMessaging.TachyonIceConfigFetcher.Result";
const char kFailureReasonMetric[] =
"Nearby.Connections.InstantMessaging.TachyonIceConfigFetcher.FailureReason";
const char kCacheHitMetric[] =
"Nearby.Connections.InstantMessaging.TachyonIceConfigFetcher.CacheHit";
const char kTokenFetchSuccessMetric[] =
"Nearby.Connections.InstantMessaging.TachyonIceConfigFetcher."
"OAuthTokenFetchResult";
const int kLifetimeDurationSeconds = 86400;
void CheckSuccessResponse(
const std::vector<::sharing::mojom::IceServerPtr>& ice_servers) {
ASSERT_EQ(2u, ice_servers.size());
// First response doesnt have credentials.
ASSERT_EQ(1u, ice_servers[0]->urls.size());
ASSERT_FALSE(ice_servers[0]->username);
ASSERT_FALSE(ice_servers[0]->credential);
// Second response has credentials.
ASSERT_EQ(2u, ice_servers[1]->urls.size());
ASSERT_EQ("username", ice_servers[1]->username);
ASSERT_EQ("credential", ice_servers[1]->credential);
}
} // namespace
class TachyonIceConfigFetcherTest : public testing::Test {
public:
TachyonIceConfigFetcherTest()
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME),
test_shared_loader_factory_(
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_)),
ice_config_fetcher_(identity_test_environment_.identity_manager(),
test_shared_loader_factory_) {
identity_test_environment_.MakePrimaryAccountAvailable(
kTestAccount, signin::ConsentLevel::kSignin);
}
~TachyonIceConfigFetcherTest() override = default;
std::string GetSuccessResponse() {
tachyon_proto::GetICEServerResponse response;
auto* config = response.mutable_ice_config();
config->mutable_lifetime_duration()->set_seconds(kLifetimeDurationSeconds);
auto* server1 = config->add_ice_servers();
server1->add_urls("stun:url1");
auto* server2 = config->add_ice_servers();
server2->add_urls("turn:url2?transport=udp");
server2->add_urls("turn:url3?transport=tcp");
server2->set_username("username");
server2->set_credential("credential");
std::string output;
response.SerializeToString(&output);
return output;
}
void SetOAuthTokenSuccessful(bool success) {
identity_test_environment_
.WaitForAccessTokenRequestIfNecessaryAndRespondWithToken(
success ? kOAuthToken : "", base::Time::Now());
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_;
signin::IdentityTestEnvironment identity_test_environment_;
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
TachyonIceConfigFetcher ice_config_fetcher_;
base::HistogramTester histogram_tester_;
};
TEST_F(TachyonIceConfigFetcherTest, ResponseSuccessful) {
base::RunLoop run_loop;
ice_config_fetcher_.GetIceServers(base::BindLambdaForTesting(
[&](std::vector<::sharing::mojom::IceServerPtr> ice_servers) {
CheckSuccessResponse(ice_servers);
run_loop.Quit();
}));
SetOAuthTokenSuccessful(true);
std::string response = GetSuccessResponse();
ASSERT_TRUE(test_url_loader_factory_.IsPending(kApiUrl, nullptr));
test_url_loader_factory_.AddResponse(kApiUrl, response, net::HTTP_OK);
run_loop.Run();
histogram_tester_.ExpectTotalCount(kIceConfigFetchedMetric, 1);
histogram_tester_.ExpectBucketCount(kIceConfigFetchedMetric, 2, 1);
histogram_tester_.ExpectTotalCount(kResultMetric, 1);
histogram_tester_.ExpectBucketCount(kResultMetric, 1, 1);
histogram_tester_.ExpectTotalCount(kCacheHitMetric, 1);
histogram_tester_.ExpectBucketCount(kCacheHitMetric, 0, 1);
histogram_tester_.ExpectTotalCount(kFailureReasonMetric, 0);
histogram_tester_.ExpectTotalCount(kTokenFetchSuccessMetric, 1);
histogram_tester_.ExpectBucketCount(kTokenFetchSuccessMetric, 1, 1);
}
TEST_F(TachyonIceConfigFetcherTest, ResponseError) {
base::RunLoop run_loop;
ice_config_fetcher_.GetIceServers(base::BindLambdaForTesting(
[&](std::vector<::sharing::mojom::IceServerPtr> ice_servers) {
// Makes sure that we at least return default servers in case of an
// error.
EXPECT_FALSE(ice_servers.empty());
run_loop.Quit();
}));
SetOAuthTokenSuccessful(true);
ASSERT_TRUE(test_url_loader_factory_.IsPending(kApiUrl, nullptr));
test_url_loader_factory_.AddResponse(kApiUrl, "",
net::HTTP_INTERNAL_SERVER_ERROR);
run_loop.Run();
histogram_tester_.ExpectTotalCount(kIceConfigFetchedMetric, 1);
histogram_tester_.ExpectBucketCount(kIceConfigFetchedMetric, 0, 1);
histogram_tester_.ExpectTotalCount(kResultMetric, 1);
histogram_tester_.ExpectBucketCount(kResultMetric, 0, 1);
histogram_tester_.ExpectTotalCount(kCacheHitMetric, 1);
histogram_tester_.ExpectBucketCount(kCacheHitMetric, 0, 1);
histogram_tester_.ExpectTotalCount(kFailureReasonMetric, 1);
histogram_tester_.ExpectBucketCount(kFailureReasonMetric, 500, 1);
histogram_tester_.ExpectTotalCount(kTokenFetchSuccessMetric, 1);
histogram_tester_.ExpectBucketCount(kTokenFetchSuccessMetric, 1, 1);
}
TEST_F(TachyonIceConfigFetcherTest, OverlappingCalls) {
base::RunLoop run_loop;
int counter = 2;
auto callback = [&](std::vector<::sharing::mojom::IceServerPtr> ice_servers) {
CheckSuccessResponse(ice_servers);
counter -= 1;
if (counter == 0) {
run_loop.Quit();
}
};
// First call.
ice_config_fetcher_.GetIceServers(base::BindLambdaForTesting(callback));
SetOAuthTokenSuccessful(true);
// Second call overlaps before any responses are processed.
ice_config_fetcher_.GetIceServers(base::BindLambdaForTesting(callback));
SetOAuthTokenSuccessful(true);
std::string response = GetSuccessResponse();
ASSERT_TRUE(test_url_loader_factory_.IsPending(kApiUrl, nullptr));
test_url_loader_factory_.AddResponse(kApiUrl, response, net::HTTP_OK);
run_loop.Run();
histogram_tester_.ExpectTotalCount(kIceConfigFetchedMetric, 2);
histogram_tester_.ExpectBucketCount(kIceConfigFetchedMetric, 2, 2);
histogram_tester_.ExpectTotalCount(kResultMetric, 2);
histogram_tester_.ExpectBucketCount(kResultMetric, 1, 2);
histogram_tester_.ExpectTotalCount(kCacheHitMetric, 2);
histogram_tester_.ExpectBucketCount(kCacheHitMetric, 0, 2);
histogram_tester_.ExpectTotalCount(kFailureReasonMetric, 0);
histogram_tester_.ExpectTotalCount(kTokenFetchSuccessMetric, 2);
histogram_tester_.ExpectBucketCount(kTokenFetchSuccessMetric, 1, 2);
}
TEST_F(TachyonIceConfigFetcherTest, IceServersCached) {
auto callback = [](base::RunLoop* run_loop,
std::vector<::sharing::mojom::IceServerPtr> ice_servers) {
CheckSuccessResponse(ice_servers);
run_loop->Quit();
};
std::string response = GetSuccessResponse();
// First call.
auto run_loop = std::make_unique<base::RunLoop>();
ice_config_fetcher_.GetIceServers(base::BindOnce(callback, run_loop.get()));
SetOAuthTokenSuccessful(true);
ASSERT_TRUE(test_url_loader_factory_.IsPending(kApiUrl));
test_url_loader_factory_.SimulateResponseForPendingRequest(kApiUrl, response);
// Complete first call before beginning second call
run_loop->Run();
// Second call returns cached result
run_loop.reset(new base::RunLoop());
ice_config_fetcher_.GetIceServers(base::BindOnce(callback, run_loop.get()));
ASSERT_FALSE(test_url_loader_factory_.IsPending(kApiUrl));
run_loop->Run();
// Wait until the cache has expired.
task_environment_.FastForwardBy(base::Seconds(kLifetimeDurationSeconds + 1));
// Expired cache results in fetching the servers again.
run_loop.reset(new base::RunLoop());
ice_config_fetcher_.GetIceServers(base::BindOnce(callback, run_loop.get()));
SetOAuthTokenSuccessful(true);
ASSERT_TRUE(test_url_loader_factory_.IsPending(kApiUrl));
test_url_loader_factory_.SimulateResponseForPendingRequest(kApiUrl, response);
run_loop->Run();
histogram_tester_.ExpectTotalCount(kIceConfigFetchedMetric, 2);
histogram_tester_.ExpectBucketCount(kIceConfigFetchedMetric, 2, 2);
histogram_tester_.ExpectTotalCount(kResultMetric, 2);
histogram_tester_.ExpectBucketCount(kResultMetric, 1, 2);
histogram_tester_.ExpectTotalCount(kCacheHitMetric, 3);
histogram_tester_.ExpectBucketCount(kCacheHitMetric, 0, 2);
histogram_tester_.ExpectTotalCount(kFailureReasonMetric, 0);
histogram_tester_.ExpectTotalCount(kTokenFetchSuccessMetric, 2);
histogram_tester_.ExpectBucketCount(kTokenFetchSuccessMetric, 1, 2);
}
TEST_F(TachyonIceConfigFetcherTest, OAuthTokenFailed) {
base::RunLoop run_loop;
ice_config_fetcher_.GetIceServers(base::BindLambdaForTesting(
[&](std::vector<::sharing::mojom::IceServerPtr> ice_servers) {
// Makes sure that we at least return default servers in case of an
// error.
EXPECT_FALSE(ice_servers.empty());
run_loop.Quit();
}));
SetOAuthTokenSuccessful(false);
ASSERT_EQ(0, test_url_loader_factory_.NumPending());
run_loop.Run();
histogram_tester_.ExpectTotalCount(kIceConfigFetchedMetric, 0);
histogram_tester_.ExpectTotalCount(kResultMetric, 0);
histogram_tester_.ExpectTotalCount(kCacheHitMetric, 1);
histogram_tester_.ExpectBucketCount(kCacheHitMetric, 0, 1);
histogram_tester_.ExpectTotalCount(kFailureReasonMetric, 0);
histogram_tester_.ExpectTotalCount(kTokenFetchSuccessMetric, 1);
histogram_tester_.ExpectBucketCount(kTokenFetchSuccessMetric, 0, 1);
}
TEST_F(TachyonIceConfigFetcherTest, OverlappingTokenFetch) {
base::RunLoop run_loop;
int counter = 2;
auto callback = [&](std::vector<::sharing::mojom::IceServerPtr> ice_servers) {
CheckSuccessResponse(ice_servers);
counter -= 1;
if (counter == 0) {
run_loop.Quit();
}
};
// First call.
ice_config_fetcher_.GetIceServers(base::BindLambdaForTesting(callback));
// Second call overlaps before the first has an OAuth token.
ice_config_fetcher_.GetIceServers(base::BindLambdaForTesting(callback));
// Return an OAuth token for both requests.
SetOAuthTokenSuccessful(true);
std::string response = GetSuccessResponse();
ASSERT_TRUE(test_url_loader_factory_.IsPending(kApiUrl, nullptr));
test_url_loader_factory_.AddResponse(kApiUrl, response, net::HTTP_OK);
run_loop.Run();
histogram_tester_.ExpectTotalCount(kIceConfigFetchedMetric, 2);
histogram_tester_.ExpectBucketCount(kIceConfigFetchedMetric, 2, 2);
histogram_tester_.ExpectTotalCount(kResultMetric, 2);
histogram_tester_.ExpectBucketCount(kResultMetric, 1, 2);
histogram_tester_.ExpectTotalCount(kCacheHitMetric, 2);
histogram_tester_.ExpectBucketCount(kCacheHitMetric, 0, 2);
histogram_tester_.ExpectTotalCount(kFailureReasonMetric, 0);
histogram_tester_.ExpectTotalCount(kTokenFetchSuccessMetric, 2);
histogram_tester_.ExpectBucketCount(kTokenFetchSuccessMetric, 1, 2);
}
|