Debian Codegraph @

bus/networkmanager/networkmanager_test.go

raw
1 /*
2 Copyright 2013-2015 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package networkmanager
18
19 import (
20 "testing"
21
22 "github.com/godbus/dbus/v5"
23 . "gopkg.in/check.v1"
24
25 testingbus "gitlab.com/ubports/development/core/lomiri-push-service/bus/testing"
26 "gitlab.com/ubports/development/core/lomiri-push-service/logger"
27 helpers "gitlab.com/ubports/development/core/lomiri-push-service/testing"
28 "gitlab.com/ubports/development/core/lomiri-push-service/testing/condition"
29 )
30
31 // hook up gocheck
32 func Test(t *testing.T) { TestingT(t) }
33
34 type NMSuite struct {
35 log logger.Logger
36 }
37
38 var _ = Suite(&NMSuite{})
39
40 func (s *NMSuite) SetUpTest(c *C) {
41 s.log = helpers.NewTestLogger(c, "debug")
42 }
43
44 // TestNames checks that networkmanager.State objects serialize
45 // correctly, to a point.
46 func (s *NMSuite) TestNames(c *C) {
47 var i State
48 for i = 0; i < _max_state; i += 10 {
49 c.Check(names[i], Equals, i.String())
50 }
51 i = _max_state
52 c.Check(i.String(), Equals, "Unknown")
53 }
54
55 // TestNew doesn't test much at all. If this fails, all is wrong in the world.
56 func (s *NMSuite) TestNew(c *C) {
57 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true)), s.log)
58 c.Check(nm, NotNil)
59 }
60
61 // GetState returns the right state when everything works
62 func (s *NMSuite) TestGetState(c *C) {
63 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), uint32(ConnectedGlobal)), s.log)
64 state := nm.GetState()
65 c.Check(state, Equals, ConnectedGlobal)
66 }
67
68 // GetState returns the right state when dbus fails
69 func (s *NMSuite) TestGetStateFail(c *C) {
70 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false), uint32(ConnectedGlobal)), s.log)
71 state := nm.GetState()
72 c.Check(state, Equals, Unknown)
73 }
74
75 // GetState returns the right state when dbus works but delivers rubbish values
76 func (s *NMSuite) TestGetStateRubbishValues(c *C) {
77 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "Unknown"), s.log)
78 state := nm.GetState()
79 c.Check(state, Equals, Unknown)
80 }
81
82 // GetState returns the right state when dbus works but delivers a rubbish structure
83 func (s *NMSuite) TestGetStateRubbishStructure(c *C) {
84 nm := New(testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), s.log)
85 state := nm.GetState()
86 c.Check(state, Equals, Unknown)
87 }
88
89 // WatchState sends a stream of States over the channel
90 func (s *NMSuite) TestWatchState(c *C) {
91 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), uint32(Unknown), uint32(Asleep), uint32(ConnectedGlobal))
92 nm := New(tc, s.log)
93 ch, w, err := nm.WatchState()
94 c.Assert(err, IsNil)
95 defer w.Cancel()
96 l := []State{<-ch, <-ch, <-ch}
97 c.Check(l, DeepEquals, []State{Unknown, Asleep, ConnectedGlobal})
98 }
99
100 // WatchState returns on error if the dbus call fails
101 func (s *NMSuite) TestWatchStateFails(c *C) {
102 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
103 _, _, err := nm.WatchState()
104 c.Check(err, NotNil)
105 }
106
107 // WatchState calls close on its channel when the watch bails
108 func (s *NMSuite) TestWatchStateClosesOnWatchBail(c *C) {
109 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
110 nm := New(tc, s.log)
111 ch, w, err := nm.WatchState()
112 c.Assert(err, IsNil)
113 defer w.Cancel()
114 _, ok := <-ch
115 c.Check(ok, Equals, false)
116 }
117
118 // WatchState survives rubbish values
119 func (s *NMSuite) TestWatchStateSurvivesRubbishValues(c *C) {
120 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), "a")
121 nm := New(tc, s.log)
122 ch, w, err := nm.WatchState()
123 c.Assert(err, IsNil)
124 defer w.Cancel()
125 _, ok := <-ch
126 c.Check(ok, Equals, false)
127 }
128
129 // GetPrimaryConnection returns the right state when everything works
130 func (s *NMSuite) TestGetPrimaryConnection(c *C) {
131 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), dbus.ObjectPath("/a/1")), s.log)
132 con := nm.GetPrimaryConnection()
133 c.Check(con, Equals, "/a/1")
134 }
135
136 // GetPrimaryConnection returns the right state when dbus fails
137 func (s *NMSuite) TestGetPrimaryConnectionFail(c *C) {
138 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
139 con := nm.GetPrimaryConnection()
140 c.Check(con, Equals, "")
141 }
142
143 // GetPrimaryConnection returns the right state when dbus works but delivers rubbish values
144 func (s *NMSuite) TestGetPrimaryConnectionRubbishValues(c *C) {
145 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "broken"), s.log)
146 con := nm.GetPrimaryConnection()
147 c.Check(con, Equals, "")
148 }
149
150 // GetPrimaryConnection returns the right state when dbus works but delivers a rubbish structure
151 func (s *NMSuite) TestGetPrimaryConnectionRubbishStructure(c *C) {
152 nm := New(testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), s.log)
153 con := nm.GetPrimaryConnection()
154 c.Check(con, Equals, "")
155 }
156
157 func mkPriConMap(priCon string) map[string]dbus.Variant {
158 m := make(map[string]dbus.Variant)
159 m["PrimaryConnection"] = dbus.MakeVariant(dbus.ObjectPath(priCon))
160 return m
161 }
162
163 // WatchPrimaryConnection sends a stream of Connections over the channel
164 func (s *NMSuite) TestWatchPrimaryConnection(c *C) {
165 tc := testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true),
166 []interface{}{mkPriConMap("/a/1"), []string{}},
167 []interface{}{mkPriConMap("/b/2"), []string{}},
168 []interface{}{mkPriConMap("/c/3"), []string{}})
169 nm := New(tc, s.log)
170 ch, w, err := nm.WatchPrimaryConnection()
171 c.Assert(err, IsNil)
172 defer w.Cancel()
173 l := []string{<-ch, <-ch, <-ch}
174 c.Check(l, DeepEquals, []string{"/a/1", "/b/2", "/c/3"})
175 }
176
177 // WatchPrimaryConnection returns on error if the dbus call fails
178 func (s *NMSuite) TestWatchPrimaryConnectionFails(c *C) {
179 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
180 _, _, err := nm.WatchPrimaryConnection()
181 c.Check(err, NotNil)
182 }
183
184 // WatchPrimaryConnection calls close on its channel when the watch bails
185 func (s *NMSuite) TestWatchPrimaryConnectionClosesOnWatchBail(c *C) {
186 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
187 nm := New(tc, s.log)
188 ch, w, err := nm.WatchPrimaryConnection()
189 c.Assert(err, IsNil)
190 defer w.Cancel()
191 _, ok := <-ch
192 c.Check(ok, Equals, false)
193 }
194
195 // WatchPrimaryConnection ignores non-PrimaryConnection PropertyChanged
196 func (s *NMSuite) TestWatchPrimaryConnectionIgnoresIrrelephant(c *C) {
197 tc := testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true),
198 []interface{}{
199 map[string]dbus.Variant{"foo": dbus.MakeVariant("bar")},
200 []string{},
201 },
202 []interface{}{
203 map[string]dbus.Variant{"PrimaryConnection": dbus.MakeVariant(dbus.ObjectPath("42"))},
204 []string{},
205 },
206 )
207 nm := New(tc, s.log)
208 ch, w, err := nm.WatchPrimaryConnection()
209 c.Assert(err, IsNil)
210 defer w.Cancel()
211 v, ok := <-ch
212 c.Check(ok, Equals, true)
213 c.Check(v, Equals, "42")
214 }
215
216 // WatchPrimaryConnection ignores rubbish PrimaryConnections
217 func (s *NMSuite) TestWatchPrimaryConnectionIgnoresRubbishValues(c *C) {
218 tc := testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true),
219 []interface{}{
220 map[string]dbus.Variant{"PrimaryConnection": dbus.MakeVariant(-12)},
221 []string{},
222 },
223 []interface{}{
224 map[string]dbus.Variant{"PrimaryConnection": dbus.MakeVariant(dbus.ObjectPath("42"))},
225 []string{},
226 },
227 )
228 nm := New(tc, s.log)
229 ch, w, err := nm.WatchPrimaryConnection()
230 c.Assert(err, IsNil)
231 defer w.Cancel()
232 v, ok := <-ch
233 c.Check(ok, Equals, true)
234 c.Check(v, Equals, "42")
235 }
236
237 // GetWirelessEnabled returns the right state when everything works
238 func (s *NMSuite) TestGetWirelessEnabled(c *C) {
239 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), false), s.log)
240 en := nm.GetWirelessEnabled()
241 c.Check(en, Equals, false)
242 }
243
244 // GetWirelessEnabled returns the right state when dbus fails
245 func (s *NMSuite) TestGetWirelessEnabledFail(c *C) {
246 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
247 en := nm.GetWirelessEnabled()
248 c.Check(en, Equals, true)
249 }
250
251 // GetWirelessEnabled returns the right state when dbus works but delivers rubbish values
252 func (s *NMSuite) TestGetWirelessEnabledRubbishValues(c *C) {
253 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "broken"), s.log)
254 en := nm.GetWirelessEnabled()
255 c.Check(en, Equals, true)
256 }
257
258 // GetWirelessEnabled returns the right state when dbus works but delivers a rubbish structure
259 func (s *NMSuite) TestGetWirelessEnabledRubbishStructure(c *C) {
260 nm := New(testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), s.log)
261 en := nm.GetWirelessEnabled()
262 c.Check(en, Equals, true)
263 }
264
265 func mkWirelessEnMap(en bool) map[string]dbus.Variant {
266 m := make(map[string]dbus.Variant)
267 m["WirelessEnabled"] = dbus.MakeVariant(en)
268 return m
269 }
270
271 // WatchWirelessEnabled sends a stream of wireless enabled states over the channel
272 func (s *NMSuite) TestWatchWirelessEnabled(c *C) {
273 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true),
274 mkWirelessEnMap(true),
275 mkWirelessEnMap(false),
276 mkWirelessEnMap(true),
277 )
278 nm := New(tc, s.log)
279 ch, w, err := nm.WatchWirelessEnabled()
280 c.Assert(err, IsNil)
281 defer w.Cancel()
282 l := []bool{<-ch, <-ch, <-ch}
283 c.Check(l, DeepEquals, []bool{true, false, true})
284 }
285
286 // WatchWirelessEnabled returns on error if the dbus call fails
287 func (s *NMSuite) TestWatchWirelessEnabledFails(c *C) {
288 nm := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
289 _, _, err := nm.WatchWirelessEnabled()
290 c.Check(err, NotNil)
291 }
292
293 // WatchWirelessEnabled calls close on its channel when the watch bails
294 func (s *NMSuite) TestWatchWirelessEnabledClosesOnWatchBail(c *C) {
295 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
296 nm := New(tc, s.log)
297 ch, w, err := nm.WatchWirelessEnabled()
298 c.Assert(err, IsNil)
299 defer w.Cancel()
300 _, ok := <-ch
301 c.Check(ok, Equals, false)
302 }
303
304 // WatchWirelessEnabled survives rubbish values
305 func (s *NMSuite) TestWatchWirelessEnabledSurvivesRubbishValues(c *C) {
306 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), "gorp")
307 nm := New(tc, s.log)
308 ch, w, err := nm.WatchWirelessEnabled()
309 c.Assert(err, IsNil)
310 defer w.Cancel()
311 _, ok := <-ch
312 c.Check(ok, Equals, false)
313 }
314
315 // WatchWirelessEnabled ignores non-WirelessEnabled PropertyChanged
316 func (s *NMSuite) TestWatchWirelessEnabledIgnoresIrrelephant(c *C) {
317 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true),
318 map[string]dbus.Variant{"foo": dbus.MakeVariant("bar")},
319 map[string]dbus.Variant{"WirelessEnabled": dbus.MakeVariant(true)},
320 )
321 nm := New(tc, s.log)
322 ch, w, err := nm.WatchWirelessEnabled()
323 c.Assert(err, IsNil)
324 defer w.Cancel()
325 v, ok := <-ch
326 c.Check(ok, Equals, true)
327 c.Check(v, Equals, true)
328 }
329
330 // WatchWirelessEnabled ignores rubbish WirelessEnabled
331 func (s *NMSuite) TestWatchWirelessEnabledIgnoresRubbishValues(c *C) {
332 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true),
333 map[string]dbus.Variant{"WirelessEnabled": dbus.MakeVariant(-12)},
334 map[string]dbus.Variant{"WirelessEnabled": dbus.MakeVariant(false)},
335 )
336 nm := New(tc, s.log)
337 ch, w, err := nm.WatchWirelessEnabled()
338 c.Assert(err, IsNil)
339 defer w.Cancel()
340 v, ok := <-ch
341 c.Check(ok, Equals, true)
342 c.Check(v, Equals, false)
343 }