Commit 71cdb178 authored by Allen Vicencio's avatar Allen Vicencio Committed by Commit Bot

Implement SmbShareFinder functions

Implements the functions of SmbShareFinder and adds tests

Bug: 757625
Change-Id: I072390d39ff060e36ce748dcf28ed98a090b7096
Reviewed-on: https://chromium-review.googlesource.com/1089852
Commit-Queue: Allen Vicencio <allenvic@chromium.org>
Reviewed-by: default avatarZentaro Kavanagh <zentaro@chromium.org>
Cr-Commit-Position: refs/heads/master@{#567056}
parent 0fa1fa16
......@@ -2145,6 +2145,7 @@ source_set("unit_tests") {
"smb_client/smb_file_system_id_test.cc",
"smb_client/smb_service_helper_unittest.cc",
"smb_client/smb_service_unittest.cc",
"smb_client/smb_share_finder_unittest.cc",
"smb_client/smb_task_queue_unittest.cc",
"smb_client/smb_url_unittest.cc",
"smb_client/temp_file_manager_unittest.cc",
......
......@@ -4,21 +4,50 @@
#include "chrome/browser/chromeos/smb_client/smb_share_finder.h"
#include <vector>
#include "base/bind.h"
#include "chrome/browser/chromeos/smb_client/discovery/mdns_host_locator.h"
#include "chrome/browser/chromeos/smb_client/smb_constants.h"
namespace chromeos {
namespace smb_client {
SmbShareFinder::SmbShareFinder(SmbProviderClient* client) : client_(client) {}
SmbShareFinder::~SmbShareFinder() = default;
void SmbShareFinder::GatherSharesInNetwork(GatherSharesResponse callback) {
NOTREACHED();
scanner_.FindHostsInNetwork(base::BindOnce(&SmbShareFinder::OnHostsFound,
AsWeakPtr(), std::move(callback)));
}
void SmbShareFinder::RegisterHostLocator(std::unique_ptr<HostLocator> locator) {
NOTREACHED();
scanner_.RegisterHostLocator(std::move(locator));
}
void SmbShareFinder::OnHostsFound(GatherSharesResponse callback,
bool success,
const HostMap& hosts) {
NOTREACHED();
if (!success) {
LOG(ERROR) << "SmbShareFinder failed to find hosts";
callback.Run(std::vector<SmbUrl>());
return;
}
if (hosts.empty()) {
callback.Run(std::vector<SmbUrl>());
return;
}
for (const auto& host : hosts) {
const std::string& host_name = host.first;
const std::string& resolved_address = host.second;
const base::FilePath server_url(kSmbSchemePrefix + resolved_address);
client_->GetShares(
server_url, base::BindOnce(&SmbShareFinder::OnSharesFound, AsWeakPtr(),
host_name, callback));
}
}
void SmbShareFinder::OnSharesFound(
......@@ -26,7 +55,23 @@ void SmbShareFinder::OnSharesFound(
GatherSharesResponse callback,
smbprovider::ErrorType error,
const smbprovider::DirectoryEntryListProto& entries) {
NOTREACHED();
if (error != smbprovider::ErrorType::ERROR_OK) {
LOG(ERROR) << "Error finding shares: " << error;
callback.Run(std::vector<SmbUrl>());
return;
}
std::vector<SmbUrl> shares;
for (const smbprovider::DirectoryEntryProto& entry : entries.entries()) {
SmbUrl url(kSmbSchemePrefix + host_name + "/" + entry.name());
if (url.IsValid()) {
shares.push_back(std::move(url));
} else {
LOG(WARNING) << "URL found is not valid";
}
}
callback.Run(shares);
}
} // namespace smb_client
......
......@@ -27,7 +27,7 @@ using GatherSharesResponse =
// available shares for each host found.
class SmbShareFinder : public base::SupportsWeakPtr<SmbShareFinder> {
public:
SmbShareFinder();
explicit SmbShareFinder(SmbProviderClient* client);
~SmbShareFinder();
// Gathers the hosts in the network using |scanner_| and gets the shares for
......@@ -52,6 +52,8 @@ class SmbShareFinder : public base::SupportsWeakPtr<SmbShareFinder> {
NetworkScanner scanner_;
SmbProviderClient* client_; // Not owned.
DISALLOW_COPY_AND_ASSIGN(SmbShareFinder);
};
......
// Copyright 2018 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/smb_client/smb_share_finder.h"
#include <algorithm>
#include <string>
#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/smb_client/discovery/in_memory_host_locator.h"
#include "chrome/browser/chromeos/smb_client/smb_constants.h"
#include "chrome/browser/chromeos/smb_client/smb_url.h"
#include "chromeos/dbus/fake_smb_provider_client.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace smb_client {
namespace {
constexpr char kDefaultHost[] = "host";
constexpr char kDefaultAddress[] = "1.2.3.4";
constexpr char kDefaultUrl[] = "smb://host/";
constexpr char kDefaultResolvedUrl[] = "smb://1.2.3.4";
} // namespace
class SmbShareFinderTest : public testing::Test {
public:
SmbShareFinderTest() {
auto host_locator = std::make_unique<InMemoryHostLocator>();
host_locator_ = host_locator.get();
fake_client_ = std::make_unique<FakeSmbProviderClient>();
share_finder_ = std::make_unique<SmbShareFinder>(fake_client_.get());
share_finder_->RegisterHostLocator(std::move(host_locator));
}
~SmbShareFinderTest() override = default;
void TearDown() override { fake_client_->ClearShares(); }
// Adds host with |hostname| and |address| as the resolved url.
void AddHost(const std::string& hostname, const std::string& address) {
host_locator_->AddHost(hostname, address);
}
// Adds the default host with the default address.
void AddDefaultHost() { AddHost(kDefaultHost, kDefaultAddress); }
// Adds |share| to the default host.
void AddShareToDefaultHost(const std::string& share) {
AddShare(kDefaultResolvedUrl, kDefaultUrl, share);
}
// Adds |share| a host. |resolved_url| will be in the format of
// "smb://1.2.3.4" and |server_url| will be in the format of "smb://host/".
void AddShare(const std::string& resolved_url,
const std::string& server_url,
const std::string& share) {
fake_client_->AddToShares(resolved_url, share);
expected_shares_.insert(server_url + share);
}
// Helper function when expecting shares to be found in the network.
void ExpectSharesFound() {
share_finder_->GatherSharesInNetwork(base::BindRepeating(
&SmbShareFinderTest::SharesFoundCallback, base::Unretained(this)));
}
// Helper function when expecting no shares to be found in the network.
void ExpectNoSharesFound() {
share_finder_->GatherSharesInNetwork(base::BindRepeating(
&SmbShareFinderTest::EmptySharesCallback, base::Unretained(this)));
}
// Helper function that expects expected_shares_ to be empty.
void ExpectAllSharesHaveBeenFound() { EXPECT_TRUE(expected_shares_.empty()); }
private:
// Removes shares discovered from expected_shares_.
void SharesFoundCallback(const std::vector<SmbUrl>& shares_found) {
EXPECT_GE(shares_found.size(), 0u);
for (const SmbUrl& url : shares_found) {
EXPECT_EQ(1u, expected_shares_.erase(url.ToString()));
}
}
void EmptySharesCallback(const std::vector<SmbUrl>& shares_found) {
EXPECT_EQ(0u, shares_found.size());
}
// Keeps track of expected shares across multiple hosts.
std::set<std::string> expected_shares_;
InMemoryHostLocator* host_locator_;
std::unique_ptr<FakeSmbProviderClient> fake_client_;
std::unique_ptr<SmbShareFinder> share_finder_;
DISALLOW_COPY_AND_ASSIGN(SmbShareFinderTest);
};
TEST_F(SmbShareFinderTest, NoSharesFoundWithNoHosts) {
ExpectNoSharesFound();
}
TEST_F(SmbShareFinderTest, NoSharesFoundWithEmptyHost) {
AddDefaultHost();
ExpectNoSharesFound();
}
TEST_F(SmbShareFinderTest, NoSharesFoundWithMultipleEmptyHosts) {
AddDefaultHost();
AddHost("host2", "4.5.6.7");
ExpectNoSharesFound();
}
TEST_F(SmbShareFinderTest, SharesFoundWithSingleHost) {
AddDefaultHost();
AddShareToDefaultHost("share1");
AddShareToDefaultHost("share2");
ExpectSharesFound();
ExpectAllSharesHaveBeenFound();
}
TEST_F(SmbShareFinderTest, SharesFoundWithMultipleHosts) {
AddDefaultHost();
AddShareToDefaultHost("share1");
const std::string host2 = "host2";
const std::string address2 = "4.5.6.7";
const std::string resolved_server_url2 = kSmbSchemePrefix + address2;
const std::string server_url2 = kSmbSchemePrefix + host2 + "/";
const std::string share2 = "share2";
AddHost(host2, address2);
AddShare(resolved_server_url2, server_url2, share2);
ExpectSharesFound();
ExpectAllSharesHaveBeenFound();
}
TEST_F(SmbShareFinderTest, SharesFoundOnOneHostWithMultipleHosts) {
AddDefaultHost();
AddShareToDefaultHost("share1");
AddHost("host2", "4.5.6.7");
ExpectSharesFound();
ExpectAllSharesHaveBeenFound();
}
} // namespace smb_client
} // namespace chromeos
......@@ -64,6 +64,8 @@ SmbUrl::SmbUrl(const std::string& raw_url) {
SmbUrl::~SmbUrl() = default;
SmbUrl::SmbUrl(SmbUrl&& smb_url) = default;
std::string SmbUrl::GetHost() const {
DCHECK(IsValid());
......
......@@ -22,6 +22,7 @@ class SmbUrl {
public:
explicit SmbUrl(const std::string& url);
~SmbUrl();
SmbUrl(SmbUrl&& smb_url);
// Returns the host of the URL which can be resolved or unresolved.
std::string GetHost() const;
......
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