-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patherror_test.go
96 lines (86 loc) · 3.31 KB
/
error_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
// Copyright (c) 2022 Tailscale Inc & AUTHORS. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build windows
package wingoes
import (
"syscall"
"testing"
"golang.org/x/sys/windows"
)
type hrTestCase struct {
hr HRESULT
expectFacility hrFacility // only valid when both expectNT and expectCustomer are false
expectCode hrCode // only valid when both expectNT and expectCustomer are false
expectSucceeded bool
expectNT bool
expectCustomer bool
}
var hrTestCases = []hrTestCase{
hrTestCase{hrS_OK, 0, 0, true, false, false},
hrTestCase{hrTYPE_E_WRONGTYPEKIND, 2, 0x802A, false, false, false},
hrTestCase{HRESULT(-((0xC0000022 ^ 0xFFFFFFFF) + 1)) | hrFacilityNTBit, 0, 0, false, true, false},
hrTestCase{HRESULT(-((((syscall.APPLICATION_ERROR + 1) | hrFailBit) ^ 0xFFFFFFFF) + 1)), 0, 0, false, false, true},
hrTestCase{HRESULT(-((((syscall.APPLICATION_ERROR + 1) | hrFailBit | hrFacilityNTBit) ^ 0xFFFFFFFF) + 1)), 0, 0, false, false, true},
}
func TestHRESULT(t *testing.T) {
for _, tc := range hrTestCases {
hr := tc.hr
if hr.Succeeded() != tc.expectSucceeded {
t.Errorf("hr 0x%08X Succeeded() got %v, want %v", uint32(hr), hr.Succeeded(), tc.expectSucceeded)
}
if hr.Failed() == tc.expectSucceeded {
t.Errorf("hr 0x%08X Failed() got %v, want %v", uint32(hr), hr.Failed(), !tc.expectSucceeded)
}
if hr.isNT() != tc.expectNT {
t.Errorf("hr 0x%08X isNT() got %v, want %v", uint32(hr), hr.isNT(), tc.expectNT)
}
if hr.isCustomer() != tc.expectCustomer {
t.Errorf("hr 0x%08X isCustomer() got %v, want %v", uint32(hr), hr.isCustomer(), tc.expectCustomer)
}
if !hr.isNT() && !hr.isCustomer() {
if hr.facility() != tc.expectFacility {
t.Errorf("hr 0x%08X facility() got %v, want %v", uint32(hr), hr.facility(), tc.expectFacility)
}
if hr.code() != tc.expectCode {
t.Errorf("hr 0x%08X code() got %v, want %v", uint32(hr), hr.code(), tc.expectCode)
}
}
}
}
type errorTestCase struct {
code any
expectNewErrorOK bool
expectHRESULT bool
expectErrno bool
expectNTStatus bool
}
var errorTestCases = []errorTestCase{
errorTestCase{int64(0), false, false, false, false},
errorTestCase{hrS_OK, true, true, true, true},
errorTestCase{hrE_POINTER, true, true, false, false},
errorTestCase{hrE_NOTIMPL, true, true, true, false},
errorTestCase{windows.STATUS_ACCESS_DENIED, true, true, true, true},
errorTestCase{windows.ERROR_ACCESS_DENIED, true, true, true, false},
errorTestCase{Error(hrE_UNEXPECTED), true, true, true, false},
}
func TestNewError(t *testing.T) {
for _, tc := range errorTestCases {
err, ok := NewError(tc.code)
if ok != tc.expectNewErrorOK {
t.Errorf("NewError(%#v) ok got %v, want %v", tc.code, ok, tc.expectNewErrorOK)
}
if !ok {
continue
}
if tc.expectHRESULT != err.IsAvailableAsHRESULT() {
t.Errorf("NewError(%#v) HRESULT got %v, want %v", tc.code, err.IsAvailableAsHRESULT(), tc.expectHRESULT)
}
if tc.expectErrno != err.IsAvailableAsErrno() {
t.Errorf("NewError(%#v) Errno got %v, want %v", tc.code, err.IsAvailableAsErrno(), tc.expectErrno)
}
if tc.expectNTStatus != err.IsAvailableAsNTStatus() {
t.Errorf("NewError(%#v) NTStatus got %v, want %v", tc.code, err.IsAvailableAsNTStatus(), tc.expectNTStatus)
}
}
}