summaryrefslogtreecommitdiffstats
path: root/net/base/network_change_notifier_linux_unittest.cc
blob: 10dda96f341e021339bc6cc257bfdf5839fd43c5 (plain)
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
// Copyright (c) 2012 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 "net/base/network_change_notifier_linux.h"

#include "base/bind.h"
#include "base/message_loop_proxy.h"
#include "base/synchronization/waitable_event.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_object_proxy.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

using testing::_;
using testing::DoAll;
using testing::InvokeWithoutArgs;
using testing::Return;
using testing::SaveArg;

class NetworkChangeNotifierLinuxTest : public testing::Test {
 protected:
  // A subset of the NetworkManager-defined constants used in
  // the tests below. See network_change_notifier_linux.cc
  // for the full list.
  enum {
    NM_STATE_DISCONNECTED = 20,
    NM_STATE_DISCONNECTING = 30,
    NM_STATE_CONNECTED_SITE = 70,
    NM_STATE_CONNECTED_GLOBAL = 70
  };

  NetworkChangeNotifierLinuxTest()
      : initialized_(false, false) {}

  virtual void SetUp() {
    dbus::Bus::Options options;
    options.bus_type = dbus::Bus::SYSTEM;
    mock_bus_ = new dbus::MockBus(options);

    mock_object_proxy_ = new dbus::MockObjectProxy(
        mock_bus_.get(),
        "service_name",
        dbus::ObjectPath("service_path"));
    EXPECT_CALL(*mock_bus_, GetObjectProxyWithOptions(_, _, _))
        .WillOnce(Return(mock_object_proxy_.get()));

    EXPECT_CALL(*mock_object_proxy_, CallMethod(_, _, _))
        .WillOnce(SaveArg<2>(&response_callback_));
    EXPECT_CALL(*mock_object_proxy_, ConnectToSignal(_, _, _, _))
        .WillOnce(
            DoAll(
                SaveArg<2>(&signal_callback_),
                InvokeWithoutArgs(
                    this,
                    &NetworkChangeNotifierLinuxTest::Initialize)));

    notifier_.reset(NetworkChangeNotifierLinux::CreateForTest(mock_bus_.get()));

    initialized_.Wait();
  }

  void Initialize() {
    notifier_thread_proxy_ = base::MessageLoopProxy::current();
    initialized_.Signal();
  }

  void RunOnNotifierThread(const base::Closure& callback) {
    base::WaitableEvent event(false, false);
    notifier_thread_proxy_->PostTask(FROM_HERE, base::Bind(
        &RunOnNotifierThreadHelper, callback, &event));
    event.Wait();
    // Run any tasks queued on the main thread, e.g. by
    // ObserverListThreadSafe.
    MessageLoop::current()->RunAllPending();
  }

  void SendResponse(uint32 state) {
    scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
    dbus::MessageWriter writer(response.get());
    writer.AppendVariantOfUint32(state);
    RunOnNotifierThread(base::Bind(response_callback_, response.get()));
  }

  void SendSignal(uint32 state) {
    dbus::Signal signal("org.freedesktop.NetworkManager", "StateChanged");
    dbus::MessageWriter writer(&signal);
    writer.AppendUint32(state);
    RunOnNotifierThread(base::Bind(signal_callback_, &signal));
  }

  dbus::ObjectProxy::ResponseCallback response_callback_;
  dbus::ObjectProxy::SignalCallback signal_callback_;

  // Allows creating a new NetworkChangeNotifier.  Must be created before
  // |notifier_| and destroyed after it to avoid DCHECK failures.
  NetworkChangeNotifier::DisableForTest disable_for_test_;
  scoped_ptr<NetworkChangeNotifier> notifier_;

 private:
  static void RunOnNotifierThreadHelper(const base::Closure& callback,
                                        base::WaitableEvent* event) {
    callback.Run();
    event->Signal();
  }

  base::WaitableEvent initialized_;

  // Valid only after initialized_ is signaled.
  scoped_refptr<base::MessageLoopProxy> notifier_thread_proxy_;

  scoped_refptr<dbus::MockBus> mock_bus_;
  scoped_refptr<dbus::MockObjectProxy> mock_object_proxy_;
};

namespace {

class OfflineObserver : public NetworkChangeNotifier::ConnectionTypeObserver {
 public:
  OfflineObserver()
      : notification_count(0),
        last_online_value(true) {
    NetworkChangeNotifier::AddConnectionTypeObserver(this);
  }

  ~OfflineObserver() {
    NetworkChangeNotifier::RemoveConnectionTypeObserver(this);
  }

  virtual void OnConnectionTypeChanged(
      NetworkChangeNotifier::ConnectionType type) OVERRIDE {
    notification_count++;
    last_online_value = type != NetworkChangeNotifier::CONNECTION_NONE;
  }

  int notification_count;
  bool last_online_value;
};

TEST_F(NetworkChangeNotifierLinuxTest, Offline) {
  SendResponse(NM_STATE_DISCONNECTED);
  EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
}

TEST_F(NetworkChangeNotifierLinuxTest, Online) {
  SendResponse(NM_STATE_CONNECTED_GLOBAL);
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());}

TEST_F(NetworkChangeNotifierLinuxTest, OfflineThenOnline) {
  OfflineObserver observer;

  SendResponse(NM_STATE_DISCONNECTED);
  EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(0, observer.notification_count);

  SendSignal(NM_STATE_CONNECTED_GLOBAL);
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(1, observer.notification_count);
  EXPECT_TRUE(observer.last_online_value);
}

TEST_F(NetworkChangeNotifierLinuxTest, MultipleStateChanges) {
  OfflineObserver observer;

  SendResponse(NM_STATE_CONNECTED_GLOBAL);
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(0, observer.notification_count);

  SendSignal(NM_STATE_DISCONNECTED);
  EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(1, observer.notification_count);
  EXPECT_FALSE(observer.last_online_value);

  SendSignal(NM_STATE_CONNECTED_GLOBAL);
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(2, observer.notification_count);
  EXPECT_TRUE(observer.last_online_value);
}

TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOnlineState) {
  OfflineObserver observer;

  SendResponse(NM_STATE_CONNECTED_SITE);
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(0, observer.notification_count);

  SendSignal(NM_STATE_CONNECTED_GLOBAL);
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(0, observer.notification_count);
}

TEST_F(NetworkChangeNotifierLinuxTest, IgnoreContinuedOfflineState) {
  OfflineObserver observer;

  SendResponse(NM_STATE_DISCONNECTING);
  EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(0, observer.notification_count);

  SendSignal(NM_STATE_DISCONNECTED);
  EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
  EXPECT_EQ(0, observer.notification_count);
}

TEST_F(NetworkChangeNotifierLinuxTest, NullResponse) {
  RunOnNotifierThread(base::Bind(
      response_callback_, static_cast<dbus::Response*>(NULL)));
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
}

TEST_F(NetworkChangeNotifierLinuxTest, EmptyResponse) {
  scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  RunOnNotifierThread(base::Bind(response_callback_, response.get()));
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
}

TEST_F(NetworkChangeNotifierLinuxTest, InvalidResponse) {
  scoped_ptr<dbus::Response> response(dbus::Response::CreateEmpty());
  dbus::MessageWriter writer(response.get());
  writer.AppendUint16(20);  // Uint16 instead of the expected Uint32
  RunOnNotifierThread(base::Bind(response_callback_, response.get()));
  EXPECT_NE(NetworkChangeNotifier::CONNECTION_NONE,
            NetworkChangeNotifier::GetConnectionType());
}

}  // namespace
}  // namespace net