-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
255 lines (216 loc) · 6.92 KB
/
errors_test.go
File metadata and controls
255 lines (216 loc) · 6.92 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
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
package mobilepkg_test
import (
"context"
"errors"
"os"
"path/filepath"
"testing"
"github.com/nao1215/mobilepkg"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInspectError_Error(t *testing.T) {
t.Parallel()
t.Run("formats with underlying error", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "manifest.missing",
Message: "primary manifest not found",
Err: mobilepkg.ErrManifestMissing,
}
assert.Equal(t, "mobilepkg [manifest.missing]: mobilepkg: primary manifest not found", ie.Error())
})
t.Run("formats without underlying error", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "format.unsupported",
Message: "unrecognized package format",
}
assert.Equal(t, "mobilepkg [format.unsupported]: unrecognized package format", ie.Error())
})
}
func TestInspectError_Unwrap(t *testing.T) {
t.Parallel()
t.Run("unwraps to underlying error", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "manifest.missing",
Message: "primary manifest not found",
Err: mobilepkg.ErrManifestMissing,
}
assert.ErrorIs(t, ie.Unwrap(), mobilepkg.ErrManifestMissing)
})
t.Run("unwraps to nil when no underlying error", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "unknown",
Message: "something failed",
}
assert.Nil(t, ie.Unwrap())
})
}
func TestInspectError_ErrorsIs(t *testing.T) {
t.Parallel()
t.Run("errors.Is matches ErrUnsupportedFormat through InspectError", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "format.unsupported",
Message: "file is not a valid ZIP archive",
Err: mobilepkg.ErrUnsupportedFormat,
}
assert.ErrorIs(t, ie, mobilepkg.ErrUnsupportedFormat)
})
t.Run("errors.Is matches ErrManifestMissing through InspectError", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "manifest.missing",
Message: "primary manifest not found",
Err: mobilepkg.ErrManifestMissing,
}
assert.ErrorIs(t, ie, mobilepkg.ErrManifestMissing)
})
t.Run("errors.Is matches ErrManifestCorrupt through InspectError", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "manifest.corrupt",
Message: "manifest could not be parsed",
Err: mobilepkg.ErrManifestCorrupt,
}
assert.ErrorIs(t, ie, mobilepkg.ErrManifestCorrupt)
})
t.Run("errors.Is matches ErrArchiveCorrupt through InspectError", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "archive.corrupt",
Message: "archive is damaged",
Err: mobilepkg.ErrArchiveCorrupt,
}
assert.ErrorIs(t, ie, mobilepkg.ErrArchiveCorrupt)
})
t.Run("errors.Is does not match unrelated sentinel", func(t *testing.T) {
t.Parallel()
ie := &mobilepkg.InspectError{
Code: "format.unsupported",
Message: "file is not valid",
Err: mobilepkg.ErrUnsupportedFormat,
}
assert.NotErrorIs(t, ie, mobilepkg.ErrManifestMissing)
})
}
func TestInspectError_ErrorsAs(t *testing.T) {
t.Parallel()
t.Run("errors.As extracts InspectError", func(t *testing.T) {
t.Parallel()
var err error = &mobilepkg.InspectError{
Code: "manifest.missing",
Message: "primary manifest not found",
Err: mobilepkg.ErrManifestMissing,
}
var ie *mobilepkg.InspectError
require.ErrorAs(t, err, &ie)
assert.Equal(t, "manifest.missing", ie.Code)
assert.Equal(t, "primary manifest not found", ie.Message)
})
t.Run("errors.As does not match for plain error", func(t *testing.T) {
t.Parallel()
err := errors.New("plain error")
var ie *mobilepkg.InspectError
assert.False(t, errors.As(err, &ie))
})
}
func TestInspectFile_ReturnsInspectError(t *testing.T) {
t.Parallel()
t.Run("non-zip file returns InspectError with format.unsupported", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := filepath.Join(dir, "notzip.apk")
err := os.WriteFile(path, []byte("this is not a zip"), 0o644)
require.NoError(t, err)
_, inspErr := mobilepkg.InspectFile(context.Background(), path)
require.Error(t, inspErr)
var ie *mobilepkg.InspectError
require.ErrorAs(t, inspErr, &ie)
assert.Equal(t, "format.unsupported", ie.Code)
assert.ErrorIs(t, inspErr, mobilepkg.ErrUnsupportedFormat)
})
t.Run("empty zip returns InspectError with format.unsupported", func(t *testing.T) {
t.Parallel()
dir := t.TempDir()
path := createEmptyZip(t, dir)
_, inspErr := mobilepkg.InspectFile(context.Background(), path)
require.Error(t, inspErr)
var ie *mobilepkg.InspectError
require.ErrorAs(t, inspErr, &ie)
assert.Equal(t, "format.unsupported", ie.Code)
})
t.Run("cancelled context returns context error", func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := mobilepkg.InspectFile(ctx, "any.apk")
require.Error(t, err)
assert.ErrorIs(t, err, context.Canceled)
})
}
func TestInspectFile_ErrorsIs_ManifestCorrupt(t *testing.T) {
t.Parallel()
// A fake APK with a corrupt (plain-text) AndroidManifest.xml triggers
// a manifest parse error from the internal android adapter. After
// wrapInspectError, the returned error must match ErrManifestCorrupt.
dir := t.TempDir()
apkPath := createTestAPK(t, dir) // text XML -> binary parser fails
_, err := mobilepkg.InspectFile(context.Background(), apkPath)
require.Error(t, err)
assert.ErrorIs(t, err, mobilepkg.ErrManifestCorrupt)
var ie *mobilepkg.InspectError
require.ErrorAs(t, err, &ie)
assert.Equal(t, "manifest.corrupt", ie.Code)
}
func TestSentinelErrors_AreDistinct(t *testing.T) {
t.Parallel()
sentinels := []struct {
name string
err error
}{
{"ErrUnsupportedFormat", mobilepkg.ErrUnsupportedFormat},
{"ErrManifestMissing", mobilepkg.ErrManifestMissing},
{"ErrManifestCorrupt", mobilepkg.ErrManifestCorrupt},
{"ErrArchiveCorrupt", mobilepkg.ErrArchiveCorrupt},
}
for i, a := range sentinels {
for j, b := range sentinels {
if i == j {
continue
}
assert.NotErrorIs(t, a.err, b.err, "%s should not match %s via errors.Is", a.name, b.name)
}
}
}
func TestDiagnostic_DetailField(t *testing.T) {
t.Parallel()
t.Run("Detail can hold metadata", func(t *testing.T) {
t.Parallel()
d := mobilepkg.Diagnostic{
Code: "icon.not_found",
Severity: mobilepkg.SeverityWarn,
Message: "icon not found",
Detail: map[string]string{"path": "res/icon.png"},
}
assert.Equal(t, "res/icon.png", d.Detail["path"])
})
t.Run("Detail can be nil", func(t *testing.T) {
t.Parallel()
d := mobilepkg.Diagnostic{
Code: "icon.not_found",
Severity: mobilepkg.SeverityWarn,
Message: "icon not found",
}
assert.Nil(t, d.Detail)
})
}
func TestSeverity_Values(t *testing.T) {
t.Parallel()
assert.Equal(t, mobilepkg.Severity("info"), mobilepkg.SeverityInfo)
assert.Equal(t, mobilepkg.Severity("warn"), mobilepkg.SeverityWarn)
assert.Equal(t, mobilepkg.Severity("error"), mobilepkg.SeverityError)
}