File: osinfo_test.go

package info (click to toggle)
golang-github-blackfireio-osinfo 1.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 100 kB
  • sloc: makefile: 2
file content (371 lines) | stat: -rw-r--r-- 9,944 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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package osinfo

import (
	"fmt"
	"testing"
)

func expectEqualStrings(t *testing.T, expected, actual string) {
	if expected != actual {
		t.Errorf("Expected [%v] but got [%v]", expected, actual)
	}
}

func expectEqualInts(t *testing.T, expected, actual int) {
	if expected != actual {
		t.Errorf("Expected [%v] but got [%v]", expected, actual)
	}
}

func TestAlpine(t *testing.T) {
	osRelease := `NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.8.0
PRETTY_NAME="Alpine Linux v3.8"
HOME_URL="http://alpinelinux.org"
BUG_REPORT_URL="http://bugs.alpinelinux.org"
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// Alpine has no /etc/lsb-release

	expectEqualStrings(t, "alpine", info.ID)
	expectEqualStrings(t, "3.8.0", info.Version)
	expectEqualStrings(t, "Alpine Linux", info.Name)
	expectEqualStrings(t, "", info.Codename)
}

func TestCentos(t *testing.T) {
	osRelease := `NAME="CentOS Linux"
VERSION="8 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="CentOS Linux 8 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:8"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-8"
CENTOS_MANTISBT_PROJECT_VERSION="8"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="8"

`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// CentOS has no /etc/lsb-release

	expectEqualStrings(t, "centos", info.ID)
	expectEqualStrings(t, "8", info.Version)
	expectEqualStrings(t, "CentOS Linux", info.Name)
	expectEqualStrings(t, "", info.Codename)
}

func TestDebian(t *testing.T) {
	osRelease := `PRETTY_NAME="Debian GNU/Linux 9 (stretch)"
NAME="Debian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
VERSION_CODENAME=stretch
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// Debian has no /etc/lsb-release

	expectEqualStrings(t, "debian", info.ID)
	expectEqualStrings(t, "9", info.Version)
	expectEqualStrings(t, "Debian GNU/Linux", info.Name)
	expectEqualStrings(t, "stretch", info.Codename)
}

func TestFedora(t *testing.T) {
	osRelease := `NAME=Fedora
VERSION="31 (Container Image)"
ID=fedora
VERSION_ID=31
VERSION_CODENAME=""
PLATFORM_ID="platform:f31"
PRETTY_NAME="Fedora 31 (Container Image)"
ANSI_COLOR="0;34"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:31"
HOME_URL="https://fedoraproject.org/"
DOCUMENTATION_URL="https://docs.fedoraproject.org/en-US/fedora/f31/system-administrators-guide/"
SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=31
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=31
PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
VARIANT="Container Image"
VARIANT_ID=container
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// Fedora has no /etc/lsb-release

	expectEqualStrings(t, "fedora", info.ID)
	expectEqualStrings(t, "31", info.Version)
	expectEqualStrings(t, "Fedora", info.Name)
	expectEqualStrings(t, "", info.Codename)
}

func TestGentoo(t *testing.T) {
	osRelease := `NAME=Gentoo
ID=gentoo
PRETTY_NAME="Gentoo/Linux"
ANSI_COLOR="1;32"
HOME_URL="https://www.gentoo.org/"
SUPPORT_URL="https://www.gentoo.org/support/"
BUG_REPORT_URL="https://bugs.gentoo.org/"
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// Gentoo has no /etc/lsb-release

	expectEqualStrings(t, "gentoo", info.ID)
	expectEqualStrings(t, "", info.Version)
	expectEqualStrings(t, "Gentoo", info.Name)
	expectEqualStrings(t, "", info.Codename)
}

func TestKali(t *testing.T) {
	osRelease := `PRETTY_NAME="Kali GNU/Linux Rolling"
NAME="Kali GNU/Linux"
ID=kali
VERSION="2020.2"
VERSION_ID="2020.2"
VERSION_CODENAME="kali-rolling"
ID_LIKE=debian
ANSI_COLOR="1;31"
HOME_URL="https://www.kali.org/"
SUPPORT_URL="https://forums.kali.org/"
BUG_REPORT_URL="https://bugs.kali.org/"
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// Kali has no /etc/lsb-release

	expectEqualStrings(t, "kali", info.ID)
	expectEqualStrings(t, "2020.2", info.Version)
	expectEqualStrings(t, "Kali GNU/Linux", info.Name)
	expectEqualStrings(t, "kali-rolling", info.Codename)
}

func TestManjaro(t *testing.T) {
	osRelease := `NAME="Manjaro Linux"
ID=manjaro
ID_LIKE=arch
PRETTY_NAME="Manjaro Linux"
ANSI_COLOR="1;32"
HOME_URL="https://www.manjaro.org/"
SUPPORT_URL="https://www.manjaro.org/"
BUG_REPORT_URL="https://bugs.manjaro.org/"
LOGO=manjarolinux
`
	lsbRelease := `DISTRIB_ID=ManjaroLinux
DISTRIB_RELEASE=19.0.2
DISTRIB_CODENAME=Kyria
DISTRIB_DESCRIPTION="Manjaro Linux"
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	parseEtcLSBRelease(info, lsbRelease)

	expectEqualStrings(t, "manjaro", info.ID)
	expectEqualStrings(t, "19.0.2", info.Version)
	expectEqualStrings(t, "Manjaro Linux", info.Name)
	expectEqualStrings(t, "Kyria", info.Codename)
}

func TestOpenSUSE(t *testing.T) {
	osRelease := `NAME="openSUSE Leap"
VERSION="15.1"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.1"
PRETTY_NAME="openSUSE Leap 15.1"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.1"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// OpenSUSE has no /etc/lsb-release

	expectEqualStrings(t, "opensuse-leap", info.ID)
	expectEqualStrings(t, "15.1", info.Version)
	expectEqualStrings(t, "openSUSE Leap", info.Name)
	expectEqualStrings(t, "", info.Codename)
}

func TestOracle(t *testing.T) {
	osRelease := `NAME="Oracle Linux Server"
VERSION="8.1"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.1"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.1"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:1:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://bugzilla.oracle.com/"

ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.1
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.1
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	// Oracle has no /etc/lsb-release

	expectEqualStrings(t, "ol", info.ID)
	expectEqualStrings(t, "8.1", info.Version)
	expectEqualStrings(t, "Oracle Linux Server", info.Name)
	expectEqualStrings(t, "", info.Codename)
}

func TestUbuntu(t *testing.T) {
	osRelease := `NAME="Ubuntu"
VERSION="19.10 (Eoan Ermine)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 19.10"
VERSION_ID="19.10"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=eoan
UBUNTU_CODENAME=eoan
`
	lsbRelease := `DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=19.10
DISTRIB_CODENAME=eoan
DISTRIB_DESCRIPTION="Ubuntu 19.10"
`

	info := new(OSInfo)
	parseEtcOSRelease(info, osRelease)
	parseEtcLSBRelease(info, lsbRelease)

	expectEqualStrings(t, "ubuntu", info.ID)
	expectEqualStrings(t, "19.10", info.Version)
	expectEqualStrings(t, "Ubuntu", info.Name)
	expectEqualStrings(t, "eoan", info.Codename)
}

func TestMacOSSierra(t *testing.T) {
	info := new(OSInfo)
	productVersion := "10.12.6"
	buildVersion := "16G1815"
	err := parseMacSWVers(info, productVersion, buildVersion)
	if err != nil {
		t.Error(err)
	}

	expectEqualStrings(t, "10.12.6", info.Version)
	expectEqualStrings(t, "Sierra", info.Codename)
	expectEqualStrings(t, "16G1815", info.Build)
}

func TestFreeBSD(t *testing.T) {
	info := new(OSInfo)
	unameV := "FreeBSD 12.0-RELEASE r341666 GENERIC"
	err := parseFreeBSDUname(info, unameV)
	if err != nil {
		t.Error(err)
	}

	expectEqualStrings(t, "12.0-RELEASE", info.Version)
	expectEqualStrings(t, "r341666", info.Build)
	expectEqualStrings(t, "FreeBSD", info.Name)
}

func expectRegistryString(t *testing.T, expected string, id string, regOutput string) {
	result, err := extractRegistryString(id, regOutput)
	if err != nil {
		t.Error(err)
	}
	expectEqualStrings(t, expected, result)
}

func expectRegistryInt(t *testing.T, expected int, id string, regOutput string) {
	result, err := extractRegistryInt(id, regOutput)
	if err != nil {
		t.Error(err)
	}
	expectEqualInts(t, expected, result)
}

func TestWindowsExtractProductName(t *testing.T) {
	expectRegistryString(t, "Windows 10 Pro", "ProductName", `
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
    ProductName    REG_SZ    Windows 10 Pro
`)
}

func TestWindowsExtractCurrentVersion(t *testing.T) {
	expectRegistryString(t, "6.3", "CurrentVersion", `
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
    CurrentVersion    REG_SZ    6.3
`)
}

func TestWindowsExtractMajorVersion(t *testing.T) {
	expectRegistryInt(t, 10, "CurrentMajorVersionNumber", `
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
    CurrentMajorVersionNumber    REG_DWORD    0xa
`)
}

func TestWindowsExtractMinorVersion(t *testing.T) {
	expectRegistryInt(t, 0, "CurrentMinorVersionNumber", `
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
    CurrentMinorVersionNumber    REG_DWORD    0x0
`)
}

func TestWindowsExtractCurrentBuild(t *testing.T) {
	expectRegistryString(t, "18362", "CurrentBuild", `
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion
    CurrentBuild    REG_SZ    18362
`)
}

func TestDemonstrate(t *testing.T) {
	info, err := GetOSInfo()
	if err != nil {
		t.Errorf("Error while getting OS info: %v", err)
	}

	fmt.Printf("Family:       %v\n", info.Family)
	fmt.Printf("Architecture: %v\n", info.Architecture)
	fmt.Printf("ID:           %v\n", info.ID)
	fmt.Printf("Name:         %v\n", info.Name)
	fmt.Printf("Codename:     %v\n", info.Codename)
	fmt.Printf("Version:      %v\n", info.Version)
	fmt.Printf("Build:        %v\n", info.Build)
}