Commit a59f8213 authored by Kartik Hegde's avatar Kartik Hegde Committed by Commit Bot

network_diagnostics: Expand util functions

Add new util functions needed by HttpsLatency routine.

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

Change-Id: Ieae113b0df25f6b55658d701fd4b39b81948bd91
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2442329
Commit-Queue: Kartik Hegde <khegde@chromium.org>
Reviewed-by: default avatarSteven Bennetts <stevenjb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#812891}
parent 4e326068
...@@ -18,6 +18,8 @@ namespace network_diagnostics { ...@@ -18,6 +18,8 @@ namespace network_diagnostics {
namespace util { namespace util {
const char kGenerate204Path[] = "/generate_204";
namespace { namespace {
// Returns |num_prefixes| prefixes of size |length|, where no two entries are // Returns |num_prefixes| prefixes of size |length|, where no two entries are
...@@ -78,6 +80,27 @@ std::vector<std::string> GetRandomHostsWithFixedHosts(int num_random_hosts, ...@@ -78,6 +80,27 @@ std::vector<std::string> GetRandomHostsWithFixedHosts(int num_random_hosts,
return hosts; return hosts;
} }
std::vector<std::string> GetRandomHostsWithScheme(int num_hosts,
int prefix_length,
std::string scheme) {
std::vector<std::string> hosts = GetRandomHosts(num_hosts, prefix_length);
for (auto& host : hosts) {
host = scheme + host;
}
return hosts;
}
std::vector<std::string> GetRandomHostsWithSchemeAndGenerate204Path(
int num_hosts,
int prefix_length,
std::string scheme) {
std::vector<std::string> hosts = GetRandomHosts(num_hosts, prefix_length);
for (auto& host : hosts) {
host = scheme + host + kGenerate204Path;
}
return hosts;
}
Profile* GetUserProfile() { Profile* GetUserProfile() {
return ProfileManager::GetPrimaryUserProfile(); return ProfileManager::GetPrimaryUserProfile();
} }
......
...@@ -15,6 +15,9 @@ namespace network_diagnostics { ...@@ -15,6 +15,9 @@ namespace network_diagnostics {
namespace util { namespace util {
// Generate 204 path.
extern const char kGenerate204Path[];
// Returns the Gstatic host suffix. Network diagnostic routines attach a random // Returns the Gstatic host suffix. Network diagnostic routines attach a random
// prefix to |kGstaticHostSuffix| to get a complete hostname. // prefix to |kGstaticHostSuffix| to get a complete hostname.
const char* GetGstaticHostSuffix(); const char* GetGstaticHostSuffix();
...@@ -35,6 +38,17 @@ std::vector<std::string> GetRandomHosts(int num_hosts, int prefix_length); ...@@ -35,6 +38,17 @@ std::vector<std::string> GetRandomHosts(int num_hosts, int prefix_length);
std::vector<std::string> GetRandomHostsWithFixedHosts(int num_random_hosts, std::vector<std::string> GetRandomHostsWithFixedHosts(int num_random_hosts,
int prefix_length); int prefix_length);
// Similar to GetRandomHosts, but with a |scheme| prepended to the hosts.
std::vector<std::string> GetRandomHostsWithScheme(int num_hosts,
int prefix_length,
std::string scheme);
// Similar to GetRandomHostsWithScheme, but with the 204 path appended to hosts.
std::vector<std::string> GetRandomHostsWithSchemeAndGenerate204Path(
int num_hosts,
int prefix_length,
std::string scheme);
// Returns the profile associated with this account. // Returns the profile associated with this account.
Profile* GetUserProfile(); Profile* GetUserProfile();
......
...@@ -7,19 +7,26 @@ ...@@ -7,19 +7,26 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/strings/string_util.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
namespace chromeos { namespace chromeos {
namespace network_diagnostics { namespace network_diagnostics {
namespace {
const char kHttpsScheme[] = "https://";
} // namespace
TEST(NetworkDiagnosticsUtilTest, TestGetRandomString) { TEST(NetworkDiagnosticsUtilTest, TestGetRandomString) {
int length = 8; int length = 8;
auto random_string = util::GetRandomString(length); auto random_string = util::GetRandomString(length);
// Ensure that the length equals |length| and all characters are in between // Ensure that the length equals |length| and all characters are in between
// 'a'-'z', inclusive. // 'a'-'z', inclusive.
ASSERT_EQ(length, random_string.size()); EXPECT_EQ(length, random_string.size());
for (char const& c : random_string) { for (char const& c : random_string) {
ASSERT_TRUE(c >= 'a' && c <= 'z'); EXPECT_TRUE(c >= 'a' && c <= 'z');
} }
} }
...@@ -30,8 +37,43 @@ TEST(NetworkDiagnosticsUtilTest, TestGetRandomHosts) { ...@@ -30,8 +37,43 @@ TEST(NetworkDiagnosticsUtilTest, TestGetRandomHosts) {
util::GetRandomHosts(num_hosts, prefix_length); util::GetRandomHosts(num_hosts, prefix_length);
// Ensure |random_hosts| has unique entries. // Ensure |random_hosts| has unique entries.
std::sort(random_hosts.begin(), random_hosts.end()); std::sort(random_hosts.begin(), random_hosts.end());
ASSERT_TRUE(std::adjacent_find(random_hosts.begin(), random_hosts.end()) == EXPECT_TRUE(std::adjacent_find(random_hosts.begin(), random_hosts.end()) ==
random_hosts.end());
}
TEST(NetworkDiagnosticsUtilTest, TestGetRandomHostsWithScheme) {
int num_hosts = 10;
int prefix_length = 8;
std::vector<std::string> random_hosts =
util::GetRandomHostsWithScheme(num_hosts, prefix_length, kHttpsScheme);
// Ensure |random_hosts| has unique entries.
std::sort(random_hosts.begin(), random_hosts.end());
EXPECT_TRUE(std::adjacent_find(random_hosts.begin(), random_hosts.end()) ==
random_hosts.end()); random_hosts.end());
// Ensure hosts in |random_hosts| start with |kHttpsScheme|.
for (const auto& host : random_hosts) {
EXPECT_TRUE(host.rfind(kHttpsScheme, 0) == 0);
}
}
TEST(NetworkDiagnosticsUtilTest,
TestGetRandomHostsWithSchemeAndGenerate204Path) {
int num_hosts = 10;
int prefix_length = 8;
std::vector<std::string> random_hosts =
util::GetRandomHostsWithSchemeAndGenerate204Path(num_hosts, prefix_length,
kHttpsScheme);
// Ensure |random_hosts| has unique entries.
std::sort(random_hosts.begin(), random_hosts.end());
EXPECT_TRUE(std::adjacent_find(random_hosts.begin(), random_hosts.end()) ==
random_hosts.end());
// Ensure:
// (1) hosts in |random_hosts| start with |kHttpsScheme|.
// (2) hosts in |random_hosts| end with |kGenerate204Path|.
for (const auto& host : random_hosts) {
EXPECT_TRUE(host.rfind(kHttpsScheme, 0) == 0);
EXPECT_TRUE(base::EndsWith(host, util::kGenerate204Path));
}
} }
} // namespace network_diagnostics } // namespace network_diagnostics
......
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