forked from future-architect/vuls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacos_test.go
169 lines (160 loc) · 4.92 KB
/
macos_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
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
package scanner
import (
"reflect"
"testing"
"github.com/future-architect/vuls/constant"
"github.com/future-architect/vuls/models"
)
func Test_parseSWVers(t *testing.T) {
tests := []struct {
name string
stdout string
pname string
pversion string
wantErr bool
}{
{
name: "Mac OS X",
stdout: `ProductName: Mac OS X
ProductVersion: 10.3
BuildVersion: 7A100`,
pname: constant.MacOSX,
pversion: "10.3",
},
{
name: "Mac OS X Server",
stdout: `ProductName: Mac OS X Server
ProductVersion: 10.6.8
BuildVersion: 10K549`,
pname: constant.MacOSXServer,
pversion: "10.6.8",
},
{
name: "MacOS",
stdout: `ProductName: macOS
ProductVersion: 13.4.1
BuildVersion: 22F82`,
pname: constant.MacOS,
pversion: "13.4.1",
},
{
name: "MacOS Server",
stdout: `ProductName: macOS Server
ProductVersion: 13.4.1
BuildVersion: 22F82`,
pname: constant.MacOSServer,
pversion: "13.4.1",
},
{
name: "ProductName error",
stdout: `ProductName: MacOS
ProductVersion: 13.4.1
BuildVersion: 22F82`,
wantErr: true,
},
{
name: "ProductVersion error",
stdout: `ProductName: macOS
ProductVersion:
BuildVersion: 22F82`,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
pname, pversion, err := parseSWVers(tt.stdout)
if (err != nil) != tt.wantErr {
t.Errorf("parseSWVers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if pname != tt.pname || pversion != tt.pversion {
t.Errorf("parseSWVers() pname: got = %s, want %s, pversion: got = %s, want %s", pname, tt.pname, pversion, tt.pversion)
}
})
}
}
func Test_macos_parseInstalledPackages(t *testing.T) {
tests := []struct {
name string
stdout string
want models.Packages
wantErr bool
}{
{
name: "happy",
stdout: `Info.plist: /Applications/Visual Studio Code.app/Contents/Info.plist
CFBundleDisplayName: Code
CFBundleName: Code
CFBundleShortVersionString: 1.80.1
CFBundleIdentifier: com.microsoft.VSCode
Info.plist: /Applications/Safari.app/Contents/Info.plist
CFBundleDisplayName: Safari
CFBundleName: Safari
CFBundleShortVersionString: 16.5.1
CFBundleIdentifier: com.apple.Safari
Info.plist: /Applications/Firefox.app/Contents/Info.plist
CFBundleDisplayName: /Applications/Firefox.app/Contents/Info.plist: Could not extract value, error: No value at that key path or invalid key path: CFBundleDisplayName
CFBundleName: Firefox
CFBundleShortVersionString: 115.0.2
CFBundleIdentifier: org.mozilla.firefox
Info.plist: /System/Applications/Maps.app/Contents/Info.plist
CFBundleDisplayName: Maps
CFBundleName: /System/Applications/Maps.app/Contents/Info.plist: Could not extract value, error: No value at that key path or invalid key path: CFBundleName
CFBundleShortVersionString: 3.0
CFBundleIdentifier: com.apple.Maps
Info.plist: /System/Applications/Contacts.app/Contents/Info.plist
CFBundleDisplayName: Contacts
CFBundleName: Contacts
CFBundleShortVersionString: /System/Applications/Contacts.app/Contents/Info.plist: Could not extract value, error: No value at that key path or invalid key path: CFBundleShortVersionString
CFBundleIdentifier: com.apple.AddressBook
Info.plist: /System/Applications/Sample.app/Contents/Info.plist
CFBundleDisplayName: /Applications/Sample.app/Contents/Info.plist: Could not extract value, error: No value at that key path or invalid key path: CFBundleDisplayName
CFBundleName: /Applications/Sample.app/Contents/Info.plist: Could not extract value, error: No value at that key path or invalid key path: CFBundleName
CFBundleShortVersionString: /Applications/Sample.app/Contents/Info.plist: Could not extract value, error: No value at that key path or invalid key path: CFBundleShortVersionString
CFBundleIdentifier: /Applications/Sample.app/Contents/Info.plist: Could not extract value, error: No value at that key path or invalid key path: CFBundleIdentifier `,
want: models.Packages{
"Code": {
Name: "Code",
Version: "1.80.1",
Repository: "com.microsoft.VSCode",
},
"Safari": {
Name: "Safari",
Version: "16.5.1",
Repository: "com.apple.Safari",
},
"Firefox": {
Name: "Firefox",
Version: "115.0.2",
Repository: "org.mozilla.firefox",
},
"Maps": {
Name: "Maps",
Version: "3.0",
Repository: "com.apple.Maps",
},
"Contacts": {
Name: "Contacts",
Version: "",
Repository: "com.apple.AddressBook",
},
"Sample": {
Name: "Sample",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := &macos{}
got, _, err := o.parseInstalledPackages(tt.stdout)
if (err != nil) != tt.wantErr {
t.Errorf("macos.parseInstalledPackages() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("macos.parseInstalledPackages() got = %v, want %v", got, tt.want)
}
})
}
}