File: smb_share_finder_unittest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (448 lines) | stat: -rw-r--r-- 15,397 bytes parent folder | download | duplicates (6)
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2018 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/ash/smb_client/smb_share_finder.h"

#include <algorithm>
#include <string>

#include "ash/constants/ash_features.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "chrome/browser/ash/smb_client/discovery/in_memory_host_locator.h"
#include "chrome/browser/ash/smb_client/smb_constants.h"
#include "chrome/browser/ash/smb_client/smb_url.h"
#include "chromeos/ash/components/dbus/smbprovider/fake_smb_provider_client.h"
#include "chromeos/ash/components/dbus/upstart/fake_upstart_client.h"
#include "chromeos/ash/components/dbus/upstart/upstart_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash::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() {
    SetupShareFinderTest(true /* should_run_synchronously */);
  }
  SmbShareFinderTest(const SmbShareFinderTest&) = delete;
  SmbShareFinderTest& operator=(const SmbShareFinderTest&) = delete;
  ~SmbShareFinderTest() override = default;

  // When `StopJob()` is called, increments `stop_job_called_counter` and
  // changes the status which indicates whether smbproviderd is stopped or not.
  bool OnStopJobCalled(int32_t* stop_job_called_counter,
                       const std::string& job_name,
                       const std::vector<std::string>& env) {
    if (job_name == kSmbProviderdUpstartJobName) {
      *stop_job_called_counter += 1;
      fake_client_->OnStopJobCalled();
    }
    return true;
  }

 protected:
  void TearDown() override {
    fake_client_->ClearShares();
    ash::UpstartClient::Shutdown();
  }

  // Adds host with |hostname| and |address| as the resolved url.
  void AddHost(const std::string& hostname, const std::string& address) {
    net::IPAddress ip_address;
    EXPECT_TRUE(ip_address.AssignFromIPLiteral(address));
    host_locator_->AddHost(hostname, ip_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);
  }

  // Adds a share lookup failure for |resolved_url|.
  void AddShareFailure(const std::string& resolved_url,
                       smbprovider::ErrorType error) {
    fake_client_->AddGetSharesFailure(resolved_url, error);
  }

  // Helper function when expecting shares to be found in the network.
  void StartDiscoveryWhileExpectingSharesFound() {
    share_finder_->GatherSharesInNetwork(
        base::BindOnce(&SmbShareFinderTest::HostsDiscoveredCallback,
                       base::Unretained(this)),
        base::BindOnce(&SmbShareFinderTest::SharesFoundCallback,
                       base::Unretained(this)));
    EXPECT_TRUE(discovery_callback_called_);
  }

  // Helper function when expecting no shares to be found in the network.
  void ExpectNoSharesFound() {
    StartDiscoveryWhileExpectingEmptyShares();
    EXPECT_TRUE(discovery_callback_called_);
  }

  // Helper function to call SmbShareFinder::GatherSharesInNetwork. Asserts that
  // there are no shares discovered from the EmptySharesCallback.
  void StartDiscoveryWhileExpectingEmptyShares() {
    share_finder_->GatherSharesInNetwork(
        base::BindOnce(&SmbShareFinderTest::HostsDiscoveredCallback,
                       base::Unretained(this)),
        base::BindOnce(&SmbShareFinderTest::EmptySharesCallback,
                       base::Unretained(this)));
  }

  // Helper function to call SmbShareFinder::GatherSharesInNetwork. Asserts that
  // shares are found, but does not remove them.
  void StartDiscoveryWhileGatheringShares() {
    share_finder_->GatherSharesInNetwork(
        base::BindOnce(&SmbShareFinderTest::HostsDiscoveredCallback,
                       base::Unretained(this)),
        base::BindOnce(&SmbShareFinderTest::SharesFoundSizeCallback,
                       base::Unretained(this)));
  }

  // Helper function that expects expected_shares_ to be empty.
  void ExpectAllSharesHaveBeenFound() {
    EXPECT_TRUE(expected_shares_.empty());
    EXPECT_TRUE(share_discovery_callback_called_);
  }

  // Helper function that expects |url| to resolve to |expected|.
  void ExpectResolvedHost(const SmbUrl& url, const std::string& expected) {
    EXPECT_EQ(expected, share_finder_->GetResolvedUrl(url).ToString());
  }

  void ExpectDiscoveryCalled(int32_t expected) {
    EXPECT_EQ(expected, discovery_callback_counter_);
  }

  void FinishHostDiscoveryOnHostLocator() { host_locator_->RunCallback(); }

  void FinishShareDiscoveryOnSmbProviderClient() {
    fake_client_->RunStoredReadDirCallback();
  }

  void SetupShareFinderTest(bool should_run_synchronously) {
    ash::UpstartClient::InitializeFake();

    auto host_locator =
        std::make_unique<InMemoryHostLocator>(should_run_synchronously);
    host_locator_ = host_locator.get();

    // If re-initializing the client, ensure the old client is destroyed first.
    fake_client_.reset();
    fake_client_ =
        std::make_unique<FakeSmbProviderClient>(should_run_synchronously);
    share_finder_ = std::make_unique<SmbShareFinder>(fake_client_.get());

    share_finder_->RegisterHostLocator(std::move(host_locator));
  }

 private:
  void HostsDiscoveredCallback() {
    discovery_callback_called_ = true;
    ++discovery_callback_counter_;
  }

  // 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()));
    }
    share_discovery_callback_called_ = true;
  }

  void SharesFoundSizeCallback(const std::vector<SmbUrl>& shares_found) {
    EXPECT_GE(shares_found.size(), 0u);
    share_discovery_callback_called_ = true;
  }

  void EmptySharesCallback(const std::vector<SmbUrl>& shares_found) {
    EXPECT_EQ(0u, shares_found.size());
    share_discovery_callback_called_ = true;
  }

  bool discovery_callback_called_ = false;
  bool share_discovery_callback_called_ = false;

  // Keeps track of expected shares across multiple hosts.
  std::set<std::string> expected_shares_;

  int32_t discovery_callback_counter_ = 0;

  base::test::SingleThreadTaskEnvironment task_environment_;
  raw_ptr<InMemoryHostLocator, DanglingUntriaged> host_locator_;
  std::unique_ptr<FakeSmbProviderClient> fake_client_;
  std::unique_ptr<SmbShareFinder> share_finder_;
};

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");

  StartDiscoveryWhileExpectingSharesFound();
  ExpectAllSharesHaveBeenFound();
}

TEST_F(SmbShareFinderTest, ErroWrithSingleHost) {
  AddDefaultHost();
  AddShareFailure(kDefaultResolvedUrl, smbprovider::ErrorType::ERROR_FAILED);

  StartDiscoveryWhileExpectingSharesFound();
  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);

  StartDiscoveryWhileExpectingSharesFound();
  ExpectAllSharesHaveBeenFound();
}

TEST_F(SmbShareFinderTest, SharesFoundWithMultipleHostsAndLastFailed) {
  AddDefaultHost();
  AddShareToDefaultHost("share1");

  const std::string host2 = "host2";
  const std::string address2 = "4.5.6.7";
  const std::string resolved_server_url2 = kSmbSchemePrefix + address2;
  AddHost(host2, address2);
  // Note: This assumes that hosts will be queried for shares in lexicographical
  // order. This is currently true because SmbShareFinder receives hosts in an
  // ordered std::map<>.
  AddShareFailure(resolved_server_url2,
                  smbprovider::ErrorType::ERROR_SMB1_UNSUPPORTED);

  StartDiscoveryWhileExpectingSharesFound();
  ExpectAllSharesHaveBeenFound();
}

TEST_F(SmbShareFinderTest, SharesFoundOnOneHostWithMultipleHosts) {
  AddDefaultHost();
  AddShareToDefaultHost("share1");

  AddHost("host2", "4.5.6.7");

  StartDiscoveryWhileExpectingSharesFound();
  ExpectAllSharesHaveBeenFound();
}

TEST_F(SmbShareFinderTest, ResolvesHostToOriginalUrlIfNoHostFound) {
  const std::string url = std::string(kDefaultUrl) + "share";
  SmbUrl smb_url(url);

  // Trigger the NetworkScanner to scan the network with its HostLocators.
  StartDiscoveryWhileExpectingSharesFound();

  ExpectResolvedHost(smb_url, url);
}

TEST_F(SmbShareFinderTest, ResolvesHost) {
  AddDefaultHost();

  // Trigger the NetworkScanner to scan the network with its HostLocators.
  StartDiscoveryWhileExpectingSharesFound();

  SmbUrl url(std::string(kDefaultUrl) + "share");
  ExpectResolvedHost(url, std::string(kDefaultResolvedUrl) + "/share");
}

TEST_F(SmbShareFinderTest, ResolvesHostWithMultipleHosts) {
  AddDefaultHost();
  AddHost("host2", "4.5.6.7");

  // Trigger the NetworkScanner to scan the network with its HostLocators.
  StartDiscoveryWhileExpectingSharesFound();

  SmbUrl url("smb://host2/share");
  ExpectResolvedHost(url, "smb://4.5.6.7/share");
}

TEST_F(SmbShareFinderTest, TestNonEmptyDiscoveryWithNonEmptyShareCallback) {
  SetupShareFinderTest(false /* should_run_synchronously */);

  AddDefaultHost();

  // Start discovery twice before host discovery is compeleted.
  StartDiscoveryWhileExpectingEmptyShares();
  StartDiscoveryWhileExpectingEmptyShares();

  // Assert discovery callback has not been called.
  ExpectDiscoveryCalled(0 /* expected */);

  FinishHostDiscoveryOnHostLocator();

  ExpectDiscoveryCalled(2 /* expected */);
}

TEST_F(SmbShareFinderTest, TestEmptyDiscoveryWithNonEmptyShareCallback) {
  SetupShareFinderTest(false /* should_run_synchronously */);

  AddDefaultHost();
  AddShareToDefaultHost("share1");
  AddShareToDefaultHost("share2");

  // Makes call to start discovery once. Share discovery will not run and be in
  // a pending state.
  StartDiscoveryWhileGatheringShares();

  FinishHostDiscoveryOnHostLocator();

  ExpectDiscoveryCalled(1 /* expected */);

  // Host discovery will complete immediately while share discoveries will
  // remain pending.
  StartDiscoveryWhileExpectingSharesFound();

  ExpectDiscoveryCalled(2 /* expected */);

  // Run shares callback.
  FinishShareDiscoveryOnSmbProviderClient();

  ExpectAllSharesHaveBeenFound();
}

TEST_F(SmbShareFinderTest, StopSmbproviderdUpstartJob) {
  // Enable running smbproviderd on-demand.
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeature(features::kSmbproviderdOnDemand);

  AddDefaultHost();
  AddShareToDefaultHost("share1");

  // Sets the counter, which is incremented when `StopJob()` is called.
  int32_t stop_job_called_counter = 0;
  ash::FakeUpstartClient::Get()->set_stop_job_cb(
      base::BindRepeating(&SmbShareFinderTest::OnStopJobCalled,
                          base::Unretained(this), &stop_job_called_counter));

  StartDiscoveryWhileExpectingSharesFound();
  ExpectAllSharesHaveBeenFound();

  // Checks that `StopJob()` is called only once.
  EXPECT_EQ(stop_job_called_counter, 1);
}

TEST_F(SmbShareFinderTest, StopSmbproviderdUpstartJobNoShares) {
  // Enable running smbproviderd on-demand.
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeature(features::kSmbproviderdOnDemand);

  AddDefaultHost();

  // Sets the counter, which is incremented when `StopJob()` is called.
  int32_t stop_job_called_counter = 0;
  ash::FakeUpstartClient::Get()->set_stop_job_cb(
      base::BindRepeating(&SmbShareFinderTest::OnStopJobCalled,
                          base::Unretained(this), &stop_job_called_counter));
  ExpectNoSharesFound();

  // Checks that `StopJob()` is called even when no shares are found.
  EXPECT_EQ(stop_job_called_counter, 1);
}

TEST_F(SmbShareFinderTest, StopSmbproviderdOnlyAfterLastServerScanned) {
  // Enable running smbproviderd on-demand.
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndEnableFeature(features::kSmbproviderdOnDemand);

  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);

  // Sets the counter, which is incremented when `StopJob()` is called.
  int32_t stop_job_called_counter = 0;
  ash::FakeUpstartClient::Get()->set_stop_job_cb(
      base::BindRepeating(&SmbShareFinderTest::OnStopJobCalled,
                          base::Unretained(this), &stop_job_called_counter));

  StartDiscoveryWhileExpectingSharesFound();
  ExpectAllSharesHaveBeenFound();

  // Checks that `StopJob()` is called only once even when multiple hosts
  // exist.
  EXPECT_EQ(stop_job_called_counter, 1);
}

TEST_F(SmbShareFinderTest, StopSmbproviderdUpstartJobFeatureDisabled) {
  // Disable running smbproviderd on-demand.
  base::test::ScopedFeatureList scoped_feature_list;
  scoped_feature_list.InitAndDisableFeature(features::kSmbproviderdOnDemand);

  AddDefaultHost();
  AddShareToDefaultHost("share1");

  // Sets the counter, which is incremented when `StopJob()` is called.
  int32_t stop_job_called_counter = 0;
  ash::FakeUpstartClient::Get()->set_stop_job_cb(
      base::BindRepeating(&SmbShareFinderTest::OnStopJobCalled,
                          base::Unretained(this), &stop_job_called_counter));

  StartDiscoveryWhileExpectingSharesFound();
  ExpectAllSharesHaveBeenFound();

  // Checks that `StopJob()` is not called when `kSmbproviderdOnDemand` is
  // disabled.
  EXPECT_EQ(stop_job_called_counter, 0);
}

}  // namespace ash::smb_client