-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfs_linux_test.go
265 lines (234 loc) · 5.71 KB
/
fs_linux_test.go
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
// Copyright 2018 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package sysfs
import (
"bytes"
"io/ioutil"
"net"
"os"
"sync"
"testing"
"time"
)
func TestEpollEvent_String(t *testing.T) {
if s := (epollIN | epollOUT).String(); s != "IN|OUT" {
t.Fatal(s)
}
if s := (epollERR | epollEvent(0x1000)).String(); s != "ERR|0x1000" {
t.Fatal(s)
}
if s := epollEvent(0).String(); s != "0" {
t.Fatal(s)
}
}
func TestAddFd_Zero(t *testing.T) {
// We assume this is a bad file descriptor.
ev := getListener(t)
const flags = epollET | epollPRI
if err := ev.addFd(0xFFFFFFFF, make(chan time.Time), flags); err == nil || err.Error() != "bad file descriptor" {
t.Fatal("expected failure", err)
}
}
func TestAddFd_File(t *testing.T) {
// listen cannot listen to a file.
ev := getListener(t)
f, err := ioutil.TempFile("", "periph_fs")
if err != nil {
t.Fatal(err)
}
defer func() {
if err2 := os.Remove(f.Name()); err2 != nil {
t.Fatal(err2)
}
}()
const flags = epollET | epollPRI
if err = ev.addFd(f.Fd(), make(chan time.Time), flags); isWSL() {
if err != nil {
t.Fatalf("repeated addFd() fail silently on WSL, but got error: %v", err)
}
} else {
if err == nil || err.Error() != "operation not permitted" {
t.Fatal("expected failure", err)
}
}
}
func TestManual_Listen_Pipe(t *testing.T) {
start := time.Now()
ev := getListener(t)
r, w, err := os.Pipe()
if err != nil {
t.Fatal(err)
}
c := make(chan time.Time)
// Pipes do not support epollPRI, so use epollIN instead.
const flags = epollET | epollIN
if err = ev.addFd(r.Fd(), c, flags); err != nil {
t.Fatal(err)
}
notExpectChan(t, c, "should not have produced an event")
// Produce a single event.
if _, err = w.Write([]byte("foo")); err != nil {
t.Fatal(err)
}
expectChan(t, c, start)
notExpectChan(t, c, "should have produced a single event")
buf := [8]byte{}
n := 0
if n, err = r.Read(buf[:]); err != nil {
t.Fatal(err)
} else if n != 3 {
t.Fatal("expected foo")
}
notExpectChan(t, c, "no event for read")
// Produce one or two events.
if _, err = w.Write([]byte("bar")); err != nil {
t.Fatal(err)
}
if _, err = w.Write([]byte("baz")); err != nil {
t.Fatal(err)
}
expectChan(t, c, start)
// It's a race condition between EpollWait() and reading back from the
// channel.
select {
case <-c:
default:
}
if n, err = r.Read(buf[:]); err != nil {
t.Fatal(err)
} else if n != 6 {
t.Fatal("expected foo")
}
notExpectChan(t, c, "no event for read")
if err = ev.removeFd(r.Fd()); err != nil {
t.Fatal(err)
}
}
func TestManual_Listen_Socket(t *testing.T) {
start := time.Now()
ev := getListener(t)
ln, err := net.ListenTCP("tcp4", &net.TCPAddr{IP: net.IPv4(127, 0, 0, 1)})
if err != nil {
t.Fatal(err)
}
defer func() {
if err = ln.Close(); err != nil {
t.Fatal(err)
}
}()
conn, err := net.DialTCP("tcp4", nil, ln.Addr().(*net.TCPAddr))
if err != nil {
t.Fatal(err)
}
defer func() {
if err = conn.Close(); err != nil {
t.Fatal(err)
}
}()
recv, err := ln.Accept()
if err != nil {
t.Fatal(err)
}
defer func() {
if err = recv.Close(); err != nil {
t.Fatal(err)
}
}()
f, err := recv.(*net.TCPConn).File()
if err != nil {
t.Fatal(err)
}
// This channel needs to be buffered since there's going to be an even
// immediately triggered.
c := make(chan time.Time, 1)
// TODO(maruel): Sockets do support epollPRI on out-of-band data. This would
// make this test a bit more similar to testing a GPIO sysfs file descriptor.
const flags = epollET | epollIN
if err = ev.addFd(f.Fd(), c, flags); err != nil {
t.Fatal(err)
}
notExpectChan(t, c, "starting should not produce an event")
// Produce one or two events.
// It's a race condition between EpollWait() and reading back from the
// channel.
if _, err = conn.Write([]byte("bar\n")); err != nil {
t.Fatal(err)
}
if _, err = conn.Write([]byte("baz\n")); err != nil {
t.Fatal(err)
}
expectChan(t, c, start)
// It's a race condition between EpollWait() and reading back from the
// channel.
select {
case <-c:
default:
}
// Empty the buffer.
var buf [16]byte
expected := "bar\nbaz\n"
n := 0
if n, err = recv.Read(buf[:]); n != len(expected) || err != nil {
t.Fatal(n, err)
}
if s := string(buf[:len(expected)]); s != expected {
t.Fatal(s)
}
// Produce one event.
if _, err = conn.Write([]byte("foo\n")); err != nil {
t.Fatal(err)
}
expectChan(t, c, start)
// This is part of https://github.com/google/periph/issues/323
//notExpectChan(t, c, "should have produced a single event")
// Instead consume any extraneous event.
select {
case <-c:
default:
}
if err = ev.removeFd(f.Fd()); err != nil {
t.Fatal(err)
}
}
//
// getListener returns a preinitialized eventsListener.
//
// Note: This object creates a goroutine once initialized that will leak.
func getListener(t *testing.T) *eventsListener {
ev := &eventsListener{}
if err := ev.init(); err != nil {
t.Fatal(err)
}
return ev
}
func expectChan(t *testing.T, c <-chan time.Time, start time.Time) {
select {
case v := <-c:
if v.Before(start) {
t.Fatal("received an timestamp that was too early", v, start)
}
case <-time.After(5 * time.Second):
t.Fatal("timed out after 5 seconds, waiting for an event")
}
}
func notExpectChan(t *testing.T, c <-chan time.Time, errmsg string) {
select {
case <-c:
t.Fatal(errmsg)
default:
}
}
var (
wslOnce sync.Once
isWSLValue bool
)
// isWSL returns true if running under Windows Subsystem for Linux.
func isWSL() bool {
wslOnce.Do(func() {
if c, err := ioutil.ReadFile("/proc/sys/kernel/osrelease"); err == nil {
isWSLValue = bytes.Contains(c, []byte("Microsoft"))
}
})
return isWSLValue
}