File: device_test.go

package info (click to toggle)
golang-github-jochenvg-go-udev 0.0~git20171110.d6b62d5-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports
  • size: 144 kB
  • sloc: makefile: 4
file content (90 lines) | stat: -rw-r--r-- 1,805 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
// +build linux

package udev

import (
	"fmt"
	"runtime"
	"testing"
)

func ExampleDevice() {

	// Create Udev
	u := Udev{}

	// Create new Device based on subsystem and sysname
	d := u.NewDeviceFromSubsystemSysname("mem", "zero")

	// Extract information
	fmt.Printf("Sysname:%v\n", d.Sysname())
	fmt.Printf("Syspath:%v\n", d.Syspath())
	fmt.Printf("Devpath:%v\n", d.Devpath())
	fmt.Printf("Devnode:%v\n", d.Devnode())
	fmt.Printf("Subsystem:%v\n", d.Subsystem())
	fmt.Printf("Devtype:%v\n", d.Devtype())
	fmt.Printf("Sysnum:%v\n", d.Sysnum())
	fmt.Printf("IsInitialized:%v\n", d.IsInitialized())
	fmt.Printf("Driver:%v\n", d.Driver())

	// Use one of the iterators
	it := d.PropertyIterator()
	it.Each(func(item interface{}) {
		kv := item.([]string)
		_ = fmt.Sprintf("Property:%v=%v\n", kv[0], kv[1])
	})
	// Output:
	// Sysname:zero
	// Syspath:/sys/devices/virtual/mem/zero
	// Devpath:/devices/virtual/mem/zero
	// Devnode:/dev/zero
	// Subsystem:mem
	// Devtype:
	// Sysnum:
	// IsInitialized:true
	// Driver:
}

func TestDeviceZero(t *testing.T) {
	u := Udev{}
	d := u.NewDeviceFromDeviceID("c1:5")
	if d.Subsystem() != "mem" {
		t.Fail()
	}
	if d.Sysname() != "zero" {
		t.Fail()
	}
	if d.Syspath() != "/sys/devices/virtual/mem/zero" {
		t.Fail()
	}
	if d.Devnode() != "/dev/zero" {
		t.Fail()
	}
	if d.PropertyValue("SUBSYSTEM") != "mem" {
		t.Fail()
	}
	if !d.IsInitialized() {
		t.Fail()
	}
	if d.SysattrValue("subsystem") != "mem" {
		t.Fail()
	}
	// Device should have Properties
	properties := d.Properties()
	if len(properties) == 0 {
		t.Fail()
	}
	// Device should have Sysattrs
	sysattrs := d.Sysattrs()
	if len(sysattrs) == 0 {
		t.Fail()
	}
	it := d.PropertyIterator()
	it.Each(func(item interface{}) {
		_ = item.([]string)
	})
}

func TestDeviceGC(t *testing.T) {
	runtime.GC()
}