forked from davecheney/gpio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio_linux_test.go
49 lines (44 loc) · 1.11 KB
/
gpio_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
package gpio_test
import (
"testing"
"github.com/davecheney/gpio"
"github.com/davecheney/gpio/rpi"
)
func TestOpenPin(t *testing.T) {
checkRoot(t)
pin, err := gpio.OpenPin(rpi.GPIO_P1_22, gpio.ModeInput)
if err != nil {
t.Fatal(err)
}
err = pin.Close()
if err != nil {
t.Fatal(err)
}
}
// test opening pins from a non priviledged user fails
func TestOpenPinUnpriv(t *testing.T) {
checkNotRoot(t)
pin, err := gpio.OpenPin(rpi.GPIO_P1_22, gpio.ModeInput)
if err == nil {
pin.Close()
t.Fatalf("OpenPin is expected to fail for non priv user")
}
}
func TestSetDirection(t *testing.T) {
checkRoot(t)
pin, err := gpio.OpenPin(rpi.GPIO_P1_22, gpio.ModeInput)
if err != nil {
t.Fatal(err)
}
defer pin.Close()
if dir, err := pin.Mode(), pin.Err(); dir != gpio.ModeInput || err != nil {
t.Fatalf("pin.Mode(): expected %v %v , got %v %v", gpio.ModeInput, nil, dir, err)
}
pin.SetMode(gpio.ModeOutput)
if pin.Err() != nil {
t.Fatal(err)
}
if dir, err := pin.Mode(), pin.Err(); dir != gpio.ModeOutput || err != nil {
t.Fatalf("pin.Mode(): expected %v %v , got %v %v", gpio.ModeOutput, nil, dir, err)
}
}