Commit 5d9bc66b authored by Kartik Hegde's avatar Kartik Hegde Committed by Commit Bot

network_diagnostics: Add util functions to share across routines

Add utility functions that can be shared by routines. This avoids
duplicating code per routine.

TEST=unit_tests --gtest_filter=NetworkDiagnosticsUtilTest*
BUG=chromium:956783

Change-Id: I41cb02956db7b1ee0045c7d7e775ec0a16ee97a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2420913Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Commit-Queue: Kartik Hegde <khegde@chromium.org>
Cr-Commit-Position: refs/heads/master@{#809113}
parent ef9f7762
......@@ -1887,6 +1887,8 @@ source_set("chromeos") {
"net/network_diagnostics/network_diagnostics.h",
"net/network_diagnostics/network_diagnostics_routine.cc",
"net/network_diagnostics/network_diagnostics_routine.h",
"net/network_diagnostics/network_diagnostics_util.cc",
"net/network_diagnostics/network_diagnostics_util.h",
"net/network_diagnostics/signal_strength_routine.cc",
"net/network_diagnostics/signal_strength_routine.h",
"net/network_health/network_health.cc",
......@@ -3387,6 +3389,7 @@ source_set("unit_tests") {
"net/network_diagnostics/lan_connectivity_routine_unittest.cc",
"net/network_diagnostics/network_diagnostics_routine_unittest.cc",
"net/network_diagnostics/network_diagnostics_unittest.cc",
"net/network_diagnostics/network_diagnostics_util_unittest.cc",
"net/network_diagnostics/signal_strength_routine_unittest.cc",
"net/network_health/network_health_unittest.cc",
"net/network_portal_detector_impl_unittest.cc",
......
// Copyright 2020 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 "chrome/browser/chromeos/net/network_diagnostics/network_diagnostics_util.h"
#include <algorithm>
#include <string>
#include <vector>
#include "base/no_destructor.h"
#include "base/rand_util.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "components/session_manager/core/session_manager.h"
#include "content/public/browser/storage_partition.h"
namespace chromeos {
namespace network_diagnostics {
namespace {
// Returns |num_prefixes| prefixes of size |length|, where no two entries are
// equal.
std::vector<std::string> GetRandomPrefixes(size_t num_prefixes, int length) {
std::vector<std::string> random_prefixes;
while (random_prefixes.size() != num_prefixes) {
std::string prefix = GetRandomString(length);
// Check that the prefix doesn't already exist.
if (std::find(std::begin(random_prefixes), std::end(random_prefixes),
prefix) == std::end(random_prefixes)) {
random_prefixes.push_back(prefix);
}
}
return random_prefixes;
}
} // namespace
const char* GetGstaticHostSuffix() {
static const char* gstatic_host_suffix = "-ccd-testing-v4.metric.gstatic.com";
return gstatic_host_suffix;
}
const std::vector<std::string>& GetFixedHosts() {
static base::NoDestructor<std::vector<std::string>> fixed_hostnames(
{"www.google.com", "mail.google.com", "drive.google.com",
"accounts.google.com", "plus.google.com", "groups.google.com"});
return *fixed_hostnames;
}
std::string GetRandomString(int length) {
std::string prefix;
for (int i = 0; i < length; i++) {
prefix += ('a' + base::RandInt(0, 25));
}
return prefix;
}
std::vector<std::string> GetRandomHosts(int num_hosts, int prefix_length) {
std::vector<std::string> random_hosts;
std::vector<std::string> random_prefixes =
GetRandomPrefixes(num_hosts, prefix_length);
DCHECK(random_prefixes.size() == num_hosts);
for (int i = 0; i < num_hosts; i++) {
random_hosts.push_back(random_prefixes[i] + GetGstaticHostSuffix());
}
return random_hosts;
}
std::vector<std::string> GetRandomHostsWithFixedHosts(int num_random_hosts,
int prefix_length) {
std::vector<std::string> hosts = GetFixedHosts();
std::vector<std::string> random_hosts =
GetRandomHosts(num_random_hosts, prefix_length);
hosts.insert(hosts.end(), random_hosts.begin(), random_hosts.end());
return hosts;
}
Profile* GetUserProfile() {
// Use sign-in profile if user has not logged in
if (session_manager::SessionManager::Get()->IsUserSessionBlocked()) {
return ProfileHelper::GetSigninProfile();
}
// Use primary profile if user is logged in
return ProfileManager::GetPrimaryUserProfile();
}
} // namespace network_diagnostics
} // namespace chromeos
// Copyright 2020 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.
#ifndef CHROME_BROWSER_CHROMEOS_NET_NETWORK_DIAGNOSTICS_NETWORK_DIAGNOSTICS_UTIL_H_
#define CHROME_BROWSER_CHROMEOS_NET_NETWORK_DIAGNOSTICS_NETWORK_DIAGNOSTICS_UTIL_H_
#include <string>
#include <vector>
class Profile;
namespace chromeos {
namespace network_diagnostics {
// Returns the Gstatic host suffix. Network diagnostic routines attach a random
// prefix to |kGstaticHostSuffix| to get a complete hostname.
const char* GetGstaticHostSuffix();
// Returns the list representing a fixed set of hostnames used by routines.
const std::vector<std::string>& GetFixedHosts();
// Returns a string of length |length|. Contains characters 'a'-'z', inclusive.
std::string GetRandomString(int length);
// Returns |num_hosts| random hosts, each suffixed with |kGstaticHostSuffix| and
// prefixed with a random string of length |prefix_length|.
std::vector<std::string> GetRandomHosts(int num_hosts, int prefix_length);
// Similar to GetRandomHosts(), but the fixed hosts are prepended to the list.
// The total number of hosts in this list is GetFixedHosts().size() +
// num_random_hosts.
std::vector<std::string> GetRandomHostsWithFixedHosts(int num_random_hosts,
int prefix_length);
// Returns the profile associated with this account.
Profile* GetUserProfile();
} // namespace network_diagnostics
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_NET_NETWORK_DIAGNOSTICS_NETWORK_DIAGNOSTICS_UTIL_H_
// Copyright 2020 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 "chrome/browser/chromeos/net/network_diagnostics/network_diagnostics_util.h"
#include <string>
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace network_diagnostics {
TEST(NetworkDiagnosticsUtilTest, TestGetRandomString) {
int length = 8;
auto random_string = GetRandomString(length);
// Ensure that the length equals |length| and all characters are in between
// 'a'-'z', inclusive.
ASSERT_EQ(length, random_string.size());
for (char const& c : random_string) {
ASSERT_TRUE(c >= 'a' && c <= 'z');
}
}
TEST(NetworkDiagnosticsUtilTest, TestGetRandomHosts) {
int num_hosts = 10;
int prefix_length = 8;
std::vector<std::string> random_hosts =
GetRandomHosts(num_hosts, prefix_length);
// Ensure |random_hosts| has unique entries.
std::sort(random_hosts.begin(), random_hosts.end());
ASSERT_TRUE(std::adjacent_find(random_hosts.begin(), random_hosts.end()) ==
random_hosts.end());
}
} // namespace network_diagnostics
} // namespace chromeos
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment