File: triggerwatch_test.go

package info (click to toggle)
snapd 2.72-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 80,412 kB
  • sloc: sh: 16,506; ansic: 16,211; python: 11,213; makefile: 1,919; exp: 190; awk: 58; xml: 22
file content (351 lines) | stat: -rw-r--r-- 8,637 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
// -*- Mode: Go; indent-tabs-mode: t -*-

/*
 * Copyright (C) 2020 Canonical Ltd
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package triggerwatch_test

import (
	"errors"
	"fmt"
	"sync"
	"testing"
	"time"

	. "gopkg.in/check.v1"

	"github.com/snapcore/snapd/cmd/snap-bootstrap/triggerwatch"
	"github.com/snapcore/snapd/osutil/udev/netlink"
	"github.com/snapcore/snapd/testtime"
)

// Hook up check.v1 into the "go test" runner
func Test(t *testing.T) { TestingT(t) }

type triggerwatchSuite struct{}

var _ = Suite(&triggerwatchSuite{})

type mockTriggerDevice struct {
	sync.Mutex

	waitForTriggerCalls int
	closeCalls          int
	ev                  *triggerwatch.KeyEvent
	// closes when the device has been waited for
	waitedC chan struct{}
	// closes when the device has been closed
	closedC chan struct{}
}

func newMockTriggerDevice(ev *triggerwatch.KeyEvent) *mockTriggerDevice {
	return &mockTriggerDevice{
		ev:      ev,
		waitedC: make(chan struct{}),
		closedC: make(chan struct{}),
	}

}
func (m *mockTriggerDevice) WaitForTrigger(n chan triggerwatch.KeyEvent) {
	m.withLocked(func() {
		defer close(m.waitedC)
		m.waitForTriggerCalls++
		if m.ev != nil {
			ev := *m.ev
			ev.Dev = m
			n <- ev
		}
	})
}

func (m *mockTriggerDevice) String() string { return "mock-device" }
func (m *mockTriggerDevice) Close() {
	defer close(m.closedC)
	m.closeCalls++
}

func (m *mockTriggerDevice) withLocked(f func()) {
	m.Lock()
	defer m.Unlock()
	f()
}

type mockTrigger struct {
	f               triggerwatch.TriggerCapabilityFilter
	d               *mockTriggerDevice
	unlistedDevices map[string]*mockTriggerDevice

	err error

	findMatchingCalls int
	openCalls         int
}

func (m *mockTrigger) FindMatchingDevices(f triggerwatch.TriggerCapabilityFilter) ([]triggerwatch.TriggerDevice, error) {
	m.findMatchingCalls++

	m.f = f
	if m.err != nil {
		return nil, m.err
	}
	if m.d != nil {
		return []triggerwatch.TriggerDevice{m.d}, nil
	}
	return nil, nil
}

func (m *mockTrigger) Open(filter triggerwatch.TriggerCapabilityFilter, node string) (triggerwatch.TriggerDevice, error) {
	m.openCalls++
	device, ok := m.unlistedDevices[node]
	if !ok {
		return nil, errors.New("Not found")
	} else {
		return device, nil
	}
}

const testTriggerTimeout = 5 * time.Millisecond
const testDeviceTimeout = 2 * time.Millisecond

func (s *triggerwatchSuite) TestNoDevsWaitKey(c *C) {
	md := newMockTriggerDevice(&triggerwatch.KeyEvent{})
	mi := &mockTrigger{d: md}
	restore := triggerwatch.MockInput(mi)
	defer restore()

	err := triggerwatch.Wait(testTriggerTimeout, testDeviceTimeout)
	c.Assert(err, IsNil)
	c.Assert(mi.findMatchingCalls, Equals, 1)
	<-md.closedC
	c.Assert(md.waitForTriggerCalls, Equals, 1)
	c.Assert(md.closeCalls, Equals, 1)
}

func (s *triggerwatchSuite) TestNoDevsWaitKeyTimeout(c *C) {
	md := newMockTriggerDevice(nil)
	mi := &mockTrigger{d: md}
	restore := triggerwatch.MockInput(mi)
	defer restore()

	err := triggerwatch.Wait(testTriggerTimeout, testDeviceTimeout)
	c.Assert(err, Equals, triggerwatch.ErrTriggerNotDetected)
	c.Assert(mi.findMatchingCalls, Equals, 1)
	<-md.closedC
	md.withLocked(func() {
		c.Assert(md.waitForTriggerCalls, Equals, 1)
		c.Assert(md.closeCalls, Equals, 1)
	})
}

func (s *triggerwatchSuite) TestNoDevsWaitNoMatching(c *C) {
	mi := &mockTrigger{}
	restore := triggerwatch.MockInput(mi)
	defer restore()

	err := triggerwatch.Wait(testTriggerTimeout, testDeviceTimeout)
	c.Assert(err, Equals, triggerwatch.ErrNoMatchingInputDevices)
}

func (s *triggerwatchSuite) TestNoDevsWaitMatchingError(c *C) {
	mi := &mockTrigger{err: fmt.Errorf("failed")}
	restore := triggerwatch.MockInput(mi)
	defer restore()

	err := triggerwatch.Wait(testTriggerTimeout, testDeviceTimeout)
	c.Assert(err, ErrorMatches, "cannot list trigger devices: failed")
}

func (s *triggerwatchSuite) TestChecksInput(c *C) {
	restore := triggerwatch.MockInput(nil)
	defer restore()

	c.Assert(func() { triggerwatch.Wait(testTriggerTimeout, testDeviceTimeout) },
		Panics, "trigger is unset")
}

func (s *triggerwatchSuite) TestUdevEvent(c *C) {
	nodepath := "/dev/input/event0"
	devpath := "/devices/SOMEBUS/input/input0/event0"

	md := newMockTriggerDevice(&triggerwatch.KeyEvent{})
	mi := &mockTrigger{
		unlistedDevices: map[string]*mockTriggerDevice{
			"/dev/input/event0": md,
		},
	}
	restore := triggerwatch.MockInput(mi)
	defer restore()

	events := []netlink.UEvent{
		{
			Action: netlink.ADD,
			KObj:   devpath,
			Env: map[string]string{
				"SUBSYSTEM": "input",
				"DEVNAME":   nodepath,
				"DEVPATH":   devpath,
			},
		},
	}
	restoreUevents := triggerwatch.MockUEvent(events)
	defer restoreUevents()

	err := triggerwatch.Wait(testTriggerTimeout, testDeviceTimeout)
	c.Assert(err, IsNil)
	c.Assert(mi.findMatchingCalls, Equals, 1)

	<-md.closedC
	c.Assert(mi.openCalls, Equals, 1)
	md.withLocked(func() {
		c.Assert(md.waitForTriggerCalls, Equals, 1)
		c.Assert(md.closeCalls, Equals, 1)
	})
}

func (s *triggerwatchSuite) TestUdevEventNoKeyEvent(c *C) {
	nodepath := "/dev/input/event0"
	devpath := "/devices/SOMEBUS/input/input0/event0"

	md := newMockTriggerDevice(nil)
	mi := &mockTrigger{
		unlistedDevices: map[string]*mockTriggerDevice{
			"/dev/input/event0": md,
		},
	}
	restore := triggerwatch.MockInput(mi)
	defer restore()

	events := []netlink.UEvent{
		{
			Action: netlink.ADD,
			KObj:   devpath,
			Env: map[string]string{
				"SUBSYSTEM": "input",
				"DEVNAME":   nodepath,
				"DEVPATH":   devpath,
			},
		},
	}
	restoreUevents := triggerwatch.MockUEvent(events)
	defer restoreUevents()

	err := triggerwatch.Wait(testTriggerTimeout, testDeviceTimeout)
	c.Assert(err, Equals, triggerwatch.ErrTriggerNotDetected)
	c.Assert(mi.findMatchingCalls, Equals, 1)

	<-md.closedC
	c.Assert(mi.openCalls, Equals, 1)
	md.withLocked(func() {
		c.Assert(md.waitForTriggerCalls, Equals, 1)
		c.Assert(md.closeCalls, Equals, 1)
	})
}

func (s *triggerwatchSuite) TestWaitMoreKeyboards(c *C) {
	nodepath := "/dev/input/event0"
	devpath := "/devices/SOMEBUS/input/input0/event0"

	md := newMockTriggerDevice(nil)
	md2 := newMockTriggerDevice(&triggerwatch.KeyEvent{})
	mi := &mockTrigger{
		d: md,
		unlistedDevices: map[string]*mockTriggerDevice{
			nodepath: md2,
		},
	}
	restore := triggerwatch.MockInput(mi)
	defer restore()

	uevents := make(chan netlink.UEvent)
	restore = triggerwatch.MockUEventChannel(uevents)
	defer restore()

	timersReadyC := make(chan struct{})

	var timers []*testtime.TestTimer
	restore = triggerwatch.MockTimeAfter(func(d time.Duration) <-chan time.Time {
		if d < 0 {
			panic("Timer with negative duration")
		}

		if len(timers) > 2 {
			panic("unexpected timer, already mocked 2 timers")
		}

		tm := testtime.NewTimer(d)
		timers = append(timers, tm)
		// we are expecting 2 times, one for trigger timeout, and one for device
		// timeout
		if len(timers) == 2 {
			close(timersReadyC)
		}
		return tm.ExpiredC()
	})
	defer restore()

	advanceTime := func(d time.Duration) {
		for _, timer := range timers {
			timer.Elapse(d)
		}
	}

	waitResult := make(chan error)
	triggerTimeout := 10 * time.Second
	devWaitTimeout := 2 * time.Second
	go func() {
		err := triggerwatch.Wait(triggerTimeout, devWaitTimeout)
		waitResult <- err
	}()

	// timers have been created, code goes into for{ select {} } loop
	<-timersReadyC

	// advance the test timers, must be inside (dev wait, trigger) timeouts
	advanceTime(devWaitTimeout + time.Second)

	// send a mock event
	uevents <- netlink.UEvent{
		Action: netlink.ADD,
		KObj:   devpath,
		Env: map[string]string{
			"SUBSYSTEM": "input",
			"DEVNAME":   nodepath,
			"DEVPATH":   devpath,
		},
	}

	<-md.waitedC
	<-md2.waitedC

	select {
	case err := <-waitResult:
		c.Assert(err, IsNil)
		<-md.closedC
		<-md2.closedC
		c.Check(mi.findMatchingCalls, Equals, 1)
		md.withLocked(func() {
			c.Check(md.waitForTriggerCalls, Equals, 1)
			c.Check(md.closeCalls, Equals, 1)
		})
		md2.withLocked(func() {
			c.Check(md2.waitForTriggerCalls, Equals, 1)
			c.Check(md2.closeCalls, Equals, 1)
		})
	case <-time.After(time.Second):
		c.Errorf("Wait did not finish")
	}
}