Commit 09ffb174 authored by Allen Vicencio's avatar Allen Vicencio Committed by Commit Bot

Add Name Resolution for NetworkScanner

Adds caching of hosts found within NetworkScanner and a method
ResolveHost for name resolution

Bug: 757625
Change-Id: I96a76e5c62ffc9f99bc34ad1f145f07e3f5dccf3
Reviewed-on: https://chromium-review.googlesource.com/1050803Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Commit-Queue: Allen Vicencio <allenvic@chromium.org>
Cr-Commit-Position: refs/heads/master@{#558395}
parent e38b172e
......@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/strings/string_util.h"
#include "chrome/browser/chromeos/smb_client/discovery/host_locator.h"
namespace chromeos {
......@@ -53,6 +54,17 @@ void NetworkScanner::RegisterHostLocator(std::unique_ptr<HostLocator> locator) {
locators_.push_back(std::move(locator));
}
std::string NetworkScanner::ResolveHost(const std::string& host) const {
DCHECK(find_hosts_returned_);
const auto& host_iter = found_hosts_.find(base::ToLowerASCII(host));
if (host_iter == found_hosts_.end()) {
return "";
}
return host_iter->second;
}
void NetworkScanner::OnHostsFound(uint32_t request_id,
bool success,
const HostMap& host_map) {
......@@ -102,7 +114,11 @@ void NetworkScanner::FireCallbackIfFinished(uint32_t request_id) {
RequestInfo info = std::move(request_iter->second);
requests_.erase(request_iter);
std::move(info.callback).Run(true /* success */, info.hosts_found);
// Save the found hosts for name resolution.
found_hosts_ = std::move(info.hosts_found);
find_hosts_returned_ = true;
std::move(info.callback).Run(true /* success */, found_hosts_);
}
}
......
......@@ -31,7 +31,10 @@ struct RequestInfo {
};
// NetworkScanner discovers SMB hosts in the local network by querying
// registered HostLocators and aggregating their results.
// registered HostLocators and aggregating their results. RegisterHostLocator is
// used to register HostLocators that are responsible for finding hosts.
// FindHostsInNetwork is called to get a list of discoverable hosts in the
// network. ResolveHost is used to get the IP address of a given host.
class NetworkScanner : public base::SupportsWeakPtr<NetworkScanner> {
public:
NetworkScanner();
......@@ -40,12 +43,19 @@ class NetworkScanner : public base::SupportsWeakPtr<NetworkScanner> {
// Query the registered HostLocators and return all the hosts found.
// |callback| is called once all the HostLocators have responded with their
// results. If there are no locators, the callback is fired immediately with
// an empty result and success set to false.
// an empty result and success set to false. Once this call has returned, the
// hosts found are cached locally and are resolvable individually through
// ResolveHost().
void FindHostsInNetwork(FindHostsCallback callback);
// Registeres a |locator| to be queried when FindHostsInNetwork() is called.
void RegisterHostLocator(std::unique_ptr<HostLocator> locator);
// Resolves |host| to an address using the cached results of
// FindHostsInNetwork(). FindHostsInNetwork() has to be called beforehand. If
// no address is found, this returns an empty string.
std::string ResolveHost(const std::string& host) const;
private:
// Callback handler for HostLocator::FindHosts().
void OnHostsFound(uint32_t request_id, bool success, const HostMap& host_map);
......@@ -72,6 +82,14 @@ class NetworkScanner : public base::SupportsWeakPtr<NetworkScanner> {
uint32_t next_request_id_ = 0;
// Hosts that are found from FindHostsInNetwork(). This is cached for name
// resolution when calling ResolveHost().
HostMap found_hosts_;
// True if FindHostsInNetwork() has been called and returned results
// regardless if any hosts are found.
bool find_hosts_returned_ = false;
DISALLOW_COPY_AND_ASSIGN(NetworkScanner);
};
......
......@@ -54,6 +54,18 @@ class NetworkScannerTest : public testing::Test {
scanner_.FindHostsInNetwork(base::BindOnce(&ExpectFailure));
}
void ExpectResolvedHostEquals(const std::string& expected,
const std::string& host) {
EXPECT_EQ(expected, scanner_.ResolveHost(host));
}
// Registers |hosts| with a host locator and call FindHostsInNetwork() which
// caches the results.
void RegisterAndCacheHosts(const HostMap& hosts) {
RegisterHostLocatorWithHosts(hosts);
ExpectHostMapEqual(hosts);
}
private:
NetworkScanner scanner_;
......@@ -134,5 +146,35 @@ TEST_F(NetworkScannerTest, ShouldResolveMultipleHostsWithSameAddress) {
ExpectHostMapEqual(expected);
}
TEST_F(NetworkScannerTest, ResolveHostReturnsEmptyStringIfNoHostFound) {
HostMap hosts;
// Register a hostlocator with no hosts.
RegisterAndCacheHosts(hosts);
// Returns an empty string since host could not be resolved.
ExpectResolvedHostEquals("", "server");
}
TEST_F(NetworkScannerTest, ResolveHostResolvesHostsFound) {
HostMap hosts;
hosts["share1"] = "1.2.3.4";
hosts["share2"] = "4.5.6.7";
RegisterAndCacheHosts(hosts);
ExpectResolvedHostEquals("1.2.3.4", "share1");
ExpectResolvedHostEquals("4.5.6.7", "share2");
// Returns an empty string since host could not be resolved.
ExpectResolvedHostEquals("", "share3");
}
TEST_F(NetworkScannerTest, ResolveHostWithUppercaseHost) {
HostMap hosts;
hosts["share1"] = "1.2.3.4";
RegisterAndCacheHosts(hosts);
ExpectResolvedHostEquals("1.2.3.4", "SHARE1");
}
} // namespace smb_client
} // 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