Debian Codegraph @

bus/urfkill/urfkill_test.go

raw
1 /*
2 Copyright 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 urfkill
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 URSuite struct {
35 log logger.Logger
36 }
37
38 var _ = Suite(&URSuite{})
39
40 func (s *URSuite) SetUpTest(c *C) {
41 s.log = helpers.NewTestLogger(c, "debug")
42 }
43
44 func (s *URSuite) TestNew(c *C) {
45 ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(true)), nil, s.log)
46 c.Check(ur, NotNil)
47 }
48
49 // IsFlightMode returns the right state when everything works
50 func (s *URSuite) TestIsFlightMode(c *C) {
51 endp := testingbus.NewTestingEndpoint(nil, condition.Work(true), true)
52 ur := New(endp, nil, s.log)
53 state := ur.IsFlightMode()
54 c.Check(state, Equals, true)
55 callArgs := testingbus.GetCallArgs(endp)
56 c.Assert(callArgs, HasLen, 1)
57 c.Assert(callArgs[0].Member, Equals, "IsFlightMode")
58 c.Assert(callArgs[0].Args, HasLen, 0)
59 }
60
61 // IsFlightMode returns the right state when dbus fails
62 func (s *URSuite) TestIsFlightModeFail(c *C) {
63 ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), nil, s.log)
64 state := ur.IsFlightMode()
65 c.Check(state, Equals, false)
66 }
67
68 // IsFlightMode returns the right state when dbus works but delivers
69 // rubbish values
70 func (s *URSuite) TestIsFlightModeRubbishValues(c *C) {
71 ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(true), "broken"), nil, s.log)
72 state := ur.IsFlightMode()
73 c.Check(state, Equals, false)
74 }
75
76 // IsFlightMode returns the right state when dbus works but delivers a rubbish structure
77 func (s *URSuite) TestIsFlightModeRubbishStructure(c *C) {
78 ur := New(testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), nil, s.log)
79 state := ur.IsFlightMode()
80 c.Check(state, Equals, false)
81 }
82
83 // WatchFightMode sends a stream of states over the channel
84 func (s *URSuite) TestWatchFlightMode(c *C) {
85 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), false, true, false)
86 ur := New(tc, nil, s.log)
87 ch, w, err := ur.WatchFlightMode()
88 c.Assert(err, IsNil)
89 defer w.Cancel()
90 l := []bool{<-ch, <-ch, <-ch}
91 c.Check(l, DeepEquals, []bool{false, true, false})
92 }
93
94 // WatchFlightMode returns on error if the dbus call fails
95 func (s *URSuite) TestWatchFlightModeFails(c *C) {
96 ur := New(testingbus.NewTestingEndpoint(nil, condition.Work(false)), nil, s.log)
97 _, _, err := ur.WatchFlightMode()
98 c.Check(err, NotNil)
99 }
100
101 // WatchFlightMode calls close on its channel when the watch bails
102 func (s *URSuite) TestWatchFlightModeClosesOnWatchBail(c *C) {
103 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
104 ur := New(tc, nil, s.log)
105 ch, w, err := ur.WatchFlightMode()
106 c.Assert(err, IsNil)
107 defer w.Cancel()
108 _, ok := <-ch
109 c.Check(ok, Equals, false)
110 }
111
112 // WatchFlightMode survives rubbish values
113 func (s *URSuite) TestWatchFlightModeSurvivesRubbishValues(c *C) {
114 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true), "gorp")
115 ur := New(tc, nil, s.log)
116 ch, w, err := ur.WatchFlightMode()
117 c.Assert(err, IsNil)
118 defer w.Cancel()
119 _, ok := <-ch
120 c.Check(ok, Equals, false)
121 }
122
123 // GetWLANKillState returns the right state when everything works
124 func (s *URSuite) TestGetWLANKillState(c *C) {
125 ur := New(nil, testingbus.NewTestingEndpoint(nil, condition.Work(true), int32(KillswitchStateSoftBlocked)), s.log)
126 st := ur.GetWLANKillswitchState()
127 c.Check(st, Equals, KillswitchStateSoftBlocked)
128 }
129
130 // GetWLANKillswitchState returns the right state when dbus fails
131 func (s *URSuite) TestGetWLANKillswitchStateFail(c *C) {
132 ur := New(nil, testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
133 st := ur.GetWLANKillswitchState()
134 c.Check(st, Equals, KillswitchStateUnblocked)
135 }
136
137 // GetWLANKillswitchState returns the right state when dbus works but delivers rubbish values
138 func (s *URSuite) TestGetWLANKillswitchStateRubbishValues(c *C) {
139 ur := New(nil, testingbus.NewTestingEndpoint(nil, condition.Work(true), "broken"), s.log)
140 st := ur.GetWLANKillswitchState()
141 c.Check(st, Equals, KillswitchStateUnblocked)
142 }
143
144 // GetWLANKillswitchState returns the right state when dbus works but delivers a rubbish structure
145 func (s *URSuite) TestGetWLANKillswitchStateRubbishStructure(c *C) {
146 ur := New(nil, testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true), []interface{}{}), s.log)
147 st := ur.GetWLANKillswitchState()
148 c.Check(st, Equals, KillswitchStateUnblocked)
149 }
150
151 func mkWLANKillswitchStateMap(st KillswitchState) map[string]dbus.Variant {
152 m := make(map[string]dbus.Variant)
153 m["state"] = dbus.MakeVariant(int32(st))
154 return m
155 }
156
157 // WatchWLANKillswitchState sends a stream of WLAN killswitch states over the channel
158 func (s *URSuite) TestWatchWLANKillswitchState(c *C) {
159 tc := testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true),
160 []interface{}{mkWLANKillswitchStateMap(KillswitchStateUnblocked), []string{}},
161 []interface{}{mkWLANKillswitchStateMap(KillswitchStateHardBlocked), []string{}},
162 []interface{}{mkWLANKillswitchStateMap(KillswitchStateUnblocked), []string{}},
163 )
164 ur := New(nil, tc, s.log)
165 ch, w, err := ur.WatchWLANKillswitchState()
166 c.Assert(err, IsNil)
167 defer w.Cancel()
168 l := []KillswitchState{<-ch, <-ch, <-ch}
169 c.Check(l, DeepEquals, []KillswitchState{KillswitchStateUnblocked, KillswitchStateHardBlocked, KillswitchStateUnblocked})
170 }
171
172 // WatchWLANKillswitchState returns on error if the dbus call fails
173 func (s *URSuite) TestWatchWLANKillswitchStateFails(c *C) {
174 ur := New(nil, testingbus.NewTestingEndpoint(nil, condition.Work(false)), s.log)
175 _, _, err := ur.WatchWLANKillswitchState()
176 c.Check(err, NotNil)
177 }
178
179 // WatchWLANKillswitchState calls close on its channel when the watch bails
180 func (s *URSuite) TestWatchWLANKillswitchStateClosesOnWatchBail(c *C) {
181 tc := testingbus.NewTestingEndpoint(nil, condition.Work(true))
182 ur := New(nil, tc, s.log)
183 ch, w, err := ur.WatchWLANKillswitchState()
184 c.Assert(err, IsNil)
185 defer w.Cancel()
186 _, ok := <-ch
187 c.Check(ok, Equals, false)
188 }
189
190 // WatchWLANKillswitchState ignores non-WLAN-killswitch PropertiesChanged
191 func (s *URSuite) TestWatchWLANKillswitchStateIgnoresIrrelevant(c *C) {
192 tc := testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true),
193 []interface{}{map[string]dbus.Variant{"foo": dbus.MakeVariant("bar")}, []string{}},
194 []interface{}{mkWLANKillswitchStateMap(KillswitchStateUnblocked), []string{}},
195 )
196 ur := New(nil, tc, s.log)
197 ch, w, err := ur.WatchWLANKillswitchState()
198 c.Assert(err, IsNil)
199 defer w.Cancel()
200 v, ok := <-ch
201 c.Check(ok, Equals, true)
202 c.Check(v, Equals, KillswitchStateUnblocked)
203 }
204
205 // WatchWLANKillswitchState ignores rubbish WLAN killswitch state
206 func (s *URSuite) TestWatchWLANKillswitchStateIgnoresRubbishValues(c *C) {
207 tc := testingbus.NewMultiValuedTestingEndpoint(nil, condition.Work(true),
208 []interface{}{map[string]dbus.Variant{"state": dbus.MakeVariant(-12)}, []string{}},
209 []interface{}{mkWLANKillswitchStateMap(KillswitchStateSoftBlocked), []string{}},
210 )
211 ur := New(nil, tc, s.log)
212 ch, w, err := ur.WatchWLANKillswitchState()
213 c.Assert(err, IsNil)
214 defer w.Cancel()
215 v, ok := <-ch
216 c.Check(ok, Equals, true)
217 c.Check(v, Equals, KillswitchStateSoftBlocked)
218 }