File: synced_network_updater_impl_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (359 lines) | stat: -rw-r--r-- 15,411 bytes parent folder | download | duplicates (2)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/sync_wifi/synced_network_updater_impl.h"

#include <memory>
#include <optional>

#include "ash/public/cpp/network_config_service.h"
#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "chromeos/ash/components/dbus/shill/fake_shill_simulated_result.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_metadata_store.h"
#include "chromeos/ash/components/sync_wifi/fake_pending_network_configuration_tracker.h"
#include "chromeos/ash/components/sync_wifi/network_identifier.h"
#include "chromeos/ash/components/sync_wifi/network_test_helper.h"
#include "chromeos/ash/components/sync_wifi/pending_network_configuration_tracker_impl.h"
#include "chromeos/ash/components/sync_wifi/synced_network_metrics_logger.h"
#include "chromeos/ash/components/sync_wifi/test_data_generator.h"
#include "chromeos/ash/components/timer_factory/fake_timer_factory.h"
#include "chromeos/ash/services/network_config/cros_network_config.h"
#include "chromeos/ash/services/network_config/in_process_instance.h"
#include "chromeos/ash/services/network_config/public/cpp/cros_network_config_test_helper.h"
#include "chromeos/services/network_config/public/mojom/cros_network_config.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"

namespace ash::network_config {
namespace mojom = ::chromeos::network_config::mojom;
}

namespace ash::sync_wifi {

namespace {

const char kFredSsid[] = "Fred";
const char kMangoSsid[] = "Mango";

// Because the identifier is created with `GenerateInvalidPskNetworkId`, this is
// invalid because it is an invalid hex string.
const char kInvalidSsid[] = "-423";

const NetworkState* FindLocalNetworkById(const NetworkIdentifier& id) {
  NetworkStateHandler::NetworkStateList network_list;
  NetworkHandler::Get()->network_state_handler()->GetNetworkListByType(
      NetworkTypePattern::WiFi(), /* configured_only= */ true,
      /* visible_only= */ false, /* limit= */ 0, &network_list);
  for (const NetworkState* network : network_list) {
    if (network->GetHexSsid() == id.hex_ssid() &&
        network->security_class() == id.security_type()) {
      return network;
    }
  }
  return nullptr;
}

}  // namespace

class SyncedNetworkUpdaterImplTest : public testing::Test {
 public:
  SyncedNetworkUpdaterImplTest()
      : task_environment_(base::test::TaskEnvironment::MainThreadType::DEFAULT,
                          base::test::TaskEnvironment::TimeSource::MOCK_TIME) {
    local_test_helper_ = std::make_unique<NetworkTestHelper>();
    ash::GetNetworkConfigService(
        remote_cros_network_config_.BindNewPipeAndPassReceiver());
  }

  SyncedNetworkUpdaterImplTest(const SyncedNetworkUpdaterImplTest&) = delete;
  SyncedNetworkUpdaterImplTest& operator=(const SyncedNetworkUpdaterImplTest&) =
      delete;

  ~SyncedNetworkUpdaterImplTest() override { local_test_helper_.reset(); }

  void SetUp() override {
    testing::Test::SetUp();
    local_test_helper_->SetUp();
    network_state_helper()->ResetDevicesAndServices();
    base::RunLoop().RunUntilIdle();

    auto tracker_unique_ptr =
        std::make_unique<FakePendingNetworkConfigurationTracker>();
    tracker_ = tracker_unique_ptr.get();
    timer_factory_ = std::make_unique<ash::timer_factory::FakeTimerFactory>();
    metrics_logger_ = std::make_unique<SyncedNetworkMetricsLogger>(
        /*network_state_handler=*/nullptr,
        /*network_connection_handler=*/nullptr);
    updater_ = std::make_unique<SyncedNetworkUpdaterImpl>(
        std::move(tracker_unique_ptr), remote_cros_network_config_.get(),
        timer_factory_.get(), metrics_logger_.get());
  }

  network_config::mojom::ManagedPropertiesPtr GetManagedProperties(
      const std::string& guid) {
    network_config::mojom::ManagedPropertiesPtr result;
    base::RunLoop run_loop;
    remote_cros_network_config_->GetManagedProperties(
        guid, base::BindOnce(
                  [](network_config::mojom::ManagedPropertiesPtr* result,
                     base::OnceClosure quit_closure,
                     network_config::mojom::ManagedPropertiesPtr network) {
                    *result = std::move(network);
                    std::move(quit_closure).Run();
                  },
                  &result, run_loop.QuitClosure()));
    run_loop.Run();
    return result;
  }

  FakePendingNetworkConfigurationTracker* tracker() { return tracker_; }
  ash::timer_factory::FakeTimerFactory* timer_factory() {
    return timer_factory_.get();
  }
  SyncedNetworkUpdaterImpl* updater() { return updater_.get(); }
  NetworkStateTestHelper* network_state_helper() {
    return local_test_helper_->network_state_test_helper();
  }
  NetworkIdentifier fred_network_id() { return fred_network_id_; }
  NetworkIdentifier mango_network_id() { return mango_network_id_; }
  NetworkIdentifier invalid_network_id() { return invalid_network_id_; }

 private:
  base::test::TaskEnvironment task_environment_;
  std::unique_ptr<NetworkTestHelper> local_test_helper_;
  std::unique_ptr<ash::timer_factory::FakeTimerFactory> timer_factory_;
  raw_ptr<FakePendingNetworkConfigurationTracker, DanglingUntriaged> tracker_;
  std::unique_ptr<SyncedNetworkMetricsLogger> metrics_logger_;
  std::unique_ptr<SyncedNetworkUpdaterImpl> updater_;
  mojo::Remote<chromeos::network_config::mojom::CrosNetworkConfig>
      remote_cros_network_config_;

  NetworkIdentifier fred_network_id_ = GeneratePskNetworkId(kFredSsid);
  NetworkIdentifier mango_network_id_ = GeneratePskNetworkId(kMangoSsid);
  NetworkIdentifier invalid_network_id_ =
      GenerateInvalidPskNetworkId(kInvalidSsid);
};

TEST_F(SyncedNetworkUpdaterImplTest, TestAdd_OneNetwork) {
  base::HistogramTester histogram_tester;
  sync_pb::WifiConfigurationSpecifics specifics =
      GenerateTestWifiSpecifics(fred_network_id());
  NetworkIdentifier id = NetworkIdentifier::FromProto(specifics);
  updater()->AddOrUpdateNetwork(specifics);
  EXPECT_TRUE(tracker()->GetPendingUpdateById(id));
  base::RunLoop().RunUntilIdle();
  const NetworkState* network = FindLocalNetworkById(id);
  ASSERT_TRUE(network);
  EXPECT_FALSE(network->hidden_ssid());
  EXPECT_FALSE(tracker()->GetPendingUpdateById(id));
  EXPECT_TRUE(
      NetworkHandler::Get()->network_metadata_store()->GetIsConfiguredBySync(
          network->guid()));
  histogram_tester.ExpectBucketCount(kApplyResultHistogram, true, 1);
}

TEST_F(SyncedNetworkUpdaterImplTest, TestUpdate_UnsetFields) {
  base::HistogramTester histogram_tester;
  sync_pb::WifiConfigurationSpecifics specifics =
      GenerateTestWifiSpecifics(fred_network_id());
  NetworkIdentifier id = NetworkIdentifier::FromProto(specifics);
  updater()->AddOrUpdateNetwork(specifics);
  EXPECT_TRUE(tracker()->GetPendingUpdateById(id));
  base::RunLoop().RunUntilIdle();
  network_config::mojom::ManagedPropertiesPtr network =
      GetManagedProperties(FindLocalNetworkById(id)->guid());
  EXPECT_TRUE(network->priority->active_value);
  EXPECT_TRUE(network->type_properties->get_wifi()->auto_connect->active_value);

  specifics.set_automatically_connect(
      sync_pb::WifiConfigurationSpecifics_AutomaticallyConnectOption::
          WifiConfigurationSpecifics_AutomaticallyConnectOption_AUTOMATICALLY_CONNECT_UNSPECIFIED);
  specifics.set_is_preferred(
      sync_pb::WifiConfigurationSpecifics_IsPreferredOption::
          WifiConfigurationSpecifics_IsPreferredOption_IS_PREFERRED_UNSPECIFIED);
  updater()->AddOrUpdateNetwork(specifics);
  EXPECT_TRUE(tracker()->GetPendingUpdateById(id));
  base::RunLoop().RunUntilIdle();
  network = GetManagedProperties(FindLocalNetworkById(id)->guid());
  // Verify that the previous values for priority and autoconnect were not
  // overwritten.
  EXPECT_TRUE(network->priority->active_value);
  EXPECT_TRUE(network->type_properties->get_wifi()->auto_connect->active_value);
}

TEST_F(SyncedNetworkUpdaterImplTest, TestAdd_ThenRemove) {
  base::HistogramTester histogram_tester;
  EXPECT_FALSE(FindLocalNetworkById(fred_network_id()));
  updater()->AddOrUpdateNetwork(GenerateTestWifiSpecifics(fred_network_id()));
  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(FindLocalNetworkById(fred_network_id()));
  updater()->RemoveNetwork(fred_network_id());
  PendingNetworkConfigurationUpdate* update =
      tracker()->GetPendingUpdateById(fred_network_id());
  EXPECT_TRUE(update);
  EXPECT_FALSE(update->specifics());
  base::RunLoop().RunUntilIdle();
  EXPECT_FALSE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_FALSE(FindLocalNetworkById(fred_network_id()));
  histogram_tester.ExpectBucketCount(kApplyResultHistogram, true, 2);
}

TEST_F(SyncedNetworkUpdaterImplTest, TestAdd_TwoNetworks) {
  base::HistogramTester histogram_tester;
  updater()->AddOrUpdateNetwork(GenerateTestWifiSpecifics(fred_network_id()));
  updater()->AddOrUpdateNetwork(GenerateTestWifiSpecifics(mango_network_id()));
  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_TRUE(tracker()->GetPendingUpdateById(mango_network_id()));
  base::RunLoop().RunUntilIdle();

  const NetworkState* fred_network = FindLocalNetworkById(fred_network_id());
  ASSERT_TRUE(fred_network);
  const NetworkState* mango_network = FindLocalNetworkById(mango_network_id());
  ASSERT_TRUE(mango_network);
  EXPECT_TRUE(
      NetworkHandler::Get()->network_metadata_store()->GetIsConfiguredBySync(
          fred_network->guid()));
  EXPECT_TRUE(
      NetworkHandler::Get()->network_metadata_store()->GetIsConfiguredBySync(
          mango_network->guid()));

  EXPECT_FALSE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_FALSE(tracker()->GetPendingUpdateById(mango_network_id()));
  histogram_tester.ExpectBucketCount(kApplyResultHistogram, true, 2);
}

TEST_F(SyncedNetworkUpdaterImplTest, TestFailToAdd_Error) {
  base::HistogramTester histogram_tester;
  network_state_helper()->manager_test()->SetSimulateConfigurationResult(
      FakeShillSimulatedResult::kFailure);

  updater()->AddOrUpdateNetwork(GenerateTestWifiSpecifics(fred_network_id()));
  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));

  base::RunLoop().RunUntilIdle();

  EXPECT_FALSE(FindLocalNetworkById(fred_network_id()));

  // The tracker should no longer be tracking the update because it reached the
  // max failed number of attempts.
  EXPECT_FALSE(tracker()->GetPendingUpdateById(fred_network_id()));
  // Our test tracker holds on to the number of completed attempts after an
  // update has been removed, and that should be equal to kMaxRetries (3).
  EXPECT_EQ(3, tracker()->GetCompletedAttempts(fred_network_id()));

  histogram_tester.ExpectBucketCount(kApplyResultHistogram, false, 1);
  histogram_tester.ExpectBucketCount(
      kApplyFailureReasonHistogram, ApplyNetworkFailureReason::kFailedToAdd, 3);
}

TEST_F(SyncedNetworkUpdaterImplTest, TestFailToAdd_Timeout) {
  base::HistogramTester histogram_tester;
  network_state_helper()->manager_test()->SetSimulateConfigurationResult(
      FakeShillSimulatedResult::kTimeout);

  updater()->AddOrUpdateNetwork(GenerateTestWifiSpecifics(fred_network_id()));
  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));

  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_EQ(0, tracker()->GetCompletedAttempts(fred_network_id()));

  timer_factory()->FireAll();

  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_EQ(1, tracker()->GetCompletedAttempts(fred_network_id()));

  timer_factory()->FireAll();

  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_EQ(2, tracker()->GetCompletedAttempts(fred_network_id()));

  timer_factory()->FireAll();

  EXPECT_EQ(3, tracker()->GetCompletedAttempts(fred_network_id()));
  EXPECT_FALSE(tracker()->GetPendingUpdateById(fred_network_id()));

  histogram_tester.ExpectBucketCount(kApplyResultHistogram, false, 1);
  histogram_tester.ExpectBucketCount(kApplyFailureReasonHistogram,
                                     ApplyNetworkFailureReason::kTimedout, 3);
}

TEST_F(SyncedNetworkUpdaterImplTest, TestFailToAdd_Timeout_ThenSucceed) {
  base::HistogramTester histogram_tester;
  network_state_helper()->manager_test()->SetSimulateConfigurationResult(
      FakeShillSimulatedResult::kTimeout);

  updater()->AddOrUpdateNetwork(GenerateTestWifiSpecifics(fred_network_id()));
  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));

  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_EQ(0, tracker()->GetCompletedAttempts(fred_network_id()));

  timer_factory()->FireAll();

  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_EQ(1, tracker()->GetCompletedAttempts(fred_network_id()));

  network_state_helper()->manager_test()->SetSimulateConfigurationResult(
      FakeShillSimulatedResult::kSuccess);

  timer_factory()->FireAll();
  base::RunLoop().RunUntilIdle();

  EXPECT_FALSE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_TRUE(FindLocalNetworkById(fred_network_id()));

  histogram_tester.ExpectBucketCount(kApplyResultHistogram, false, 0);
  histogram_tester.ExpectBucketCount(kApplyResultHistogram, true, 1);
  histogram_tester.ExpectBucketCount(kApplyFailureReasonHistogram,
                                     ApplyNetworkFailureReason::kTimedout, 2);
}

TEST_F(SyncedNetworkUpdaterImplTest, TestFailToRemove) {
  base::HistogramTester histogram_tester;
  network_state_helper()->profile_test()->SetSimulateDeleteResult(
      FakeShillSimulatedResult::kFailure);
  updater()->AddOrUpdateNetwork(GenerateTestWifiSpecifics(fred_network_id()));
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(FindLocalNetworkById(fred_network_id()));
  EXPECT_FALSE(tracker()->GetPendingUpdateById(fred_network_id()));

  updater()->RemoveNetwork(fred_network_id());
  EXPECT_TRUE(tracker()->GetPendingUpdateById(fred_network_id()));

  base::RunLoop().RunUntilIdle();

  EXPECT_TRUE(FindLocalNetworkById(fred_network_id()));
  EXPECT_FALSE(tracker()->GetPendingUpdateById(fred_network_id()));
  EXPECT_EQ(3, tracker()->GetCompletedAttempts(fred_network_id()));

  histogram_tester.ExpectBucketCount(kApplyResultHistogram, false, 1);
  histogram_tester.ExpectBucketCount(kApplyFailureReasonHistogram,
                                     ApplyNetworkFailureReason::kFailedToRemove,
                                     3);
}

// Regression test for b/268408833
TEST_F(SyncedNetworkUpdaterImplTest, InvalidSsid) {
  base::HistogramTester histogram_tester;
  updater()->AddOrUpdateNetwork(
      GenerateTestWifiSpecifics(invalid_network_id()));

  EXPECT_EQ(0, tracker()->GetCompletedAttempts(invalid_network_id()));
  EXPECT_TRUE(tracker()->GetPendingUpdateById(invalid_network_id()));
  histogram_tester.ExpectBucketCount(kApplyGenerateLocalNetworkConfigHistogram,
                                     false, 1);
}

}  // namespace ash::sync_wifi