File: client_linux_integration_test.go

package info (click to toggle)
golang-github-mdlayher-wifi 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 204 kB
  • sloc: makefile: 2
file content (153 lines) | stat: -rw-r--r-- 3,195 bytes parent folder | download
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
//go:build linux
// +build linux

package wifi_test

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

	"github.com/mdlayher/wifi"
)

func TestIntegrationLinuxConcurrent(t *testing.T) {
	const (
		workers    = 4
		iterations = 1000
	)

	c := testClient(t)
	ifis, err := c.Interfaces()
	if err != nil {
		t.Fatalf("failed to retrieve interfaces: %v", err)
	}
	if len(ifis) == 0 {
		t.Skip("skipping, found no WiFi interfaces")
	}

	var names []string
	for _, ifi := range ifis {
		if ifi.Name == "" || ifi.Type != wifi.InterfaceTypeStation {
			continue
		}

		names = append(names, ifi.Name)
	}

	t.Logf("workers: %d, iterations: %d, interfaces: %v",
		workers, iterations, names)

	var wg sync.WaitGroup
	wg.Add(workers)
	defer wg.Wait()

	for i := 0; i < workers; i++ {
		go func(differentI int) {
			defer wg.Done()
			execN(t, iterations, names, differentI)
		}(i)
	}
}

func execN(t *testing.T, n int, expect []string, workerID int) {
	c := testClient(t)

	names := make(map[string]int)
	for i := 0; i < n; i++ {
		ifis, err := c.Interfaces()
		if err != nil {
			panicf("[worker_id %d; iteration %d] failed to retrieve interfaces: %v", workerID, i, err)
		}

		for _, ifi := range ifis {
			if ifi.Name == "" || ifi.Type != wifi.InterfaceTypeStation {
				continue
			}

			if _, err := c.StationInfo(ifi); err != nil {
				if !errors.Is(err, os.ErrNotExist) {
					panicf("[worker_id %d; iteration %d] failed to retrieve station info for device %s: %v", workerID, i, ifi.Name, err)
				}
			}

			if _, err := c.SurveyInfo(ifi); err != nil {
				if !errors.Is(err, os.ErrNotExist) {
					panicf("[worker_id %d; iteration %d] failed to retrieve survey info for device %s: %v", workerID, i, ifi.Name, err)
				}
			}

			names[ifi.Name]++
		}
	}

	for _, e := range expect {
		nn, ok := names[e]
		if !ok {
			panicf("[worker_id %d] did not find interface %q during test", workerID, e)
		}
		if nn != n {
			panicf("[worker_id %d] wanted to find %q %d times, found %d", workerID, e, n, nn)
		}
	}
}

func testClient(t *testing.T) *wifi.Client {
	t.Helper()

	c, err := wifi.New()
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			t.Skipf("skipping, nl80211 not found: %v", err)
		}

		t.Fatalf("failed to create client: %v", err)
	}

	t.Cleanup(func() { _ = c.Close() })
	return c
}

func panicf(format string, a ...interface{}) {
	panic(fmt.Sprintf(format, a...))
}

func TestClient_AccessPoints(t *testing.T) {
	if os.Geteuid() != 0 {
		t.Skipf("skipping, must be run as root")
	}

	c, err := wifi.New()
	if err != nil {
		t.Fatalf("failed to create client: %v", err)
	}

	ifis, err := c.Interfaces()
	if err != nil {
		t.Fatalf("failed to retrieve interfaces: %v", err)
	}

	for _, ifi := range ifis {
		if ifi.Name == "" || ifi.Type != wifi.InterfaceTypeStation {
			continue
		}

		ctx, cancel := context.WithDeadline(context.Background(), time.Now().Add(30*time.Second))
		defer cancel()

		err = c.Scan(ctx, ifi)
		if err != nil {
			t.Fatalf("failed to scan access points for device %s: %v", ifi.Name, err)
		}

		_, err := c.AccessPoints(ifi)
		if err != nil {
			t.Fatalf("failed to retrieve access points for device %s: %v", ifi.Name, err)
		}

	}
}