-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux_test.go
More file actions
194 lines (170 loc) · 5.58 KB
/
Copy pathlinux_test.go
File metadata and controls
194 lines (170 loc) · 5.58 KB
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
//go:build linux
package main
// The Linux reader against a filesystem built for the purpose.
//
// The parsers are tested everywhere; this is the part that has to go and
// find the files, and it can only be tested on the system it was written
// for. It runs as an ordinary user, which is the case that matters: the
// interesting behaviour is what happens when the files are locked.
import (
"os"
"path/filepath"
"strings"
"testing"
)
// pointAt builds a fake set of folders and makes the reader look at them
// instead of the real ones.
func pointAt(t *testing.T) string {
t.Helper()
root := t.TempDir()
was := [...]any{sysClassNet, networkManagerFolder, iwdFolder, wpaSupplicantPaths}
t.Cleanup(func() {
sysClassNet = was[0].(string)
networkManagerFolder = was[1].(string)
iwdFolder = was[2].(string)
wpaSupplicantPaths = was[3].([]string)
})
sysClassNet = filepath.Join(root, "sys/class/net")
networkManagerFolder = filepath.Join(root, "etc/NetworkManager/system-connections")
iwdFolder = filepath.Join(root, "var/lib/iwd")
wpaSupplicantPaths = []string{filepath.Join(root, "etc/wpa_supplicant.conf")}
for _, folder := range []string{sysClassNet, networkManagerFolder, iwdFolder} {
if err := os.MkdirAll(folder, 0o755); err != nil {
t.Fatal(err)
}
}
return root
}
func write(t *testing.T, path, body string, mode os.FileMode) {
t.Helper()
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, []byte(body), mode); err != nil {
t.Fatal(err)
}
}
func TestReadingAWholeLinuxMachine(t *testing.T) {
pointAt(t)
// A wireless card, which is what makes an empty list mean something.
if err := os.MkdirAll(filepath.Join(sysClassNet, "wlan0/wireless"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(filepath.Join(sysClassNet, "eth0"), 0o755); err != nil {
t.Fatal(err)
}
write(t, filepath.Join(networkManagerFolder, "Casa Martinez.nmconnection"),
"[connection]\nid=Casa Martinez\ntype=wifi\n\n[wifi]\nssid=Casa Martinez\nhidden=true\n\n[wifi-security]\nkey-mgmt=wpa-psk\n",
0o600)
write(t, filepath.Join(iwdFolder, "Aeropuerto.open"), "", 0o600)
write(t, filepath.Join(iwdFolder, "main.conf"), "not a network", 0o644)
write(t, wpaSupplicantPaths[0],
"network={\n\tssid=\"Oficina\"\n\tscan_ssid=1\n\tkey_mgmt=WPA-PSK\n}\n", 0o644)
m, err := readMachine()
if err != nil {
t.Fatal(err)
}
if m.HasWireless != Yes {
t.Error("a card with a wireless folder in it was not found")
}
if len(m.Adapters) != 1 {
t.Errorf("found %d cards, want 1: the ethernet one is not wireless", len(m.Adapters))
}
want := map[string]bool{"Casa Martinez": false, "Aeropuerto": false, "Oficina": false}
for _, n := range m.Networks {
if _, expected := want[n.SSID]; !expected {
t.Errorf("an unexpected network turned up: %q", n.SSID)
continue
}
want[n.SSID] = true
}
for name, found := range want {
if !found {
t.Errorf("%q was never found", name)
}
}
// The three sources have to keep what each of them knows.
for _, n := range m.Networks {
switch n.SSID {
case "Casa Martinez":
if n.Hidden != Yes {
t.Error("NetworkManager said hidden=true")
}
case "Aeropuerto":
if n.Open != Yes {
t.Error("an iwd .open file is an open network")
}
case "Oficina":
if n.Hidden != Yes {
t.Error("scan_ssid=1 means the network is hidden")
}
}
}
}
// The case that makes this worth writing. NetworkManager locks its files
// because they hold passwords, but the folder is not locked and the files
// are named after the networks, so the list of places you have been is
// readable by anybody with an account on the machine.
func TestALockedFileStillGivesUpItsName(t *testing.T) {
if os.Geteuid() == 0 {
t.Skip("running as root, where nothing is locked and the point is lost")
}
pointAt(t)
path := filepath.Join(networkManagerFolder, "Hotel Ibis Barcelona.nmconnection")
write(t, path, "[connection]\nid=Hotel Ibis Barcelona\ntype=wifi\n", 0o600)
if err := os.Chmod(path, 0o000); err != nil {
t.Fatal(err)
}
m, err := readMachine()
if err != nil {
t.Fatal(err)
}
if len(m.Networks) != 1 || m.Networks[0].SSID != "Hotel Ibis Barcelona" {
t.Fatalf("the name was lost with the contents: %+v", m.Networks)
}
// And having read it by name only has to be said, because none of the
// other fields were answered and the report must not imply they were.
if len(m.Gaps) == 0 {
t.Error("a file read by name alone was not reported as a gap")
}
if m.Networks[0].Hidden != Unknown {
t.Error("a locked file cannot say whether the network is hidden")
}
}
func TestAMachineWithNoWirelessCard(t *testing.T) {
pointAt(t)
if err := os.MkdirAll(filepath.Join(sysClassNet, "eth0"), 0o755); err != nil {
t.Fatal(err)
}
m, err := readMachine()
if err != nil {
t.Fatal(err)
}
if m.HasWireless != No {
t.Errorf("wireless came out %v on a machine with only ethernet", m.HasWireless)
}
r := inspect(m, false)
if findingNamed(r, "no wireless card") == nil {
t.Error("it should say so rather than report an empty list")
}
}
// Folders that are simply not there are not a problem worth reporting: most
// machines have one of these three, not all of them.
func TestAbsentFoldersAreNotGaps(t *testing.T) {
pointAt(t)
if err := os.RemoveAll(iwdFolder); err != nil {
t.Fatal(err)
}
if err := os.RemoveAll(networkManagerFolder); err != nil {
t.Fatal(err)
}
m, err := readMachine()
if err != nil {
t.Fatal(err)
}
for _, gap := range m.Gaps {
if strings.Contains(gap, "iwd") || strings.Contains(gap, "NetworkManager") {
t.Errorf("a folder that does not exist was reported as unreadable: %q", gap)
}
}
}