forked from konimarti/opc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_test.go
More file actions
122 lines (116 loc) · 3.43 KB
/
code_test.go
File metadata and controls
122 lines (116 loc) · 3.43 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
package opc
import (
"fmt"
"testing"
"unsafe"
"github.com/go-ole/go-ole"
"github.com/stretchr/testify/assert"
)
type exceptionInfo struct {
wCode uint16
wReserved uint16
bstrSource *uint16
bstrDescription *uint16
bstrHelpFile *uint16
dwHelpContext uint32
pvReserved uintptr
pfnDeferredFillIn uintptr
scode uint32
rendered bool
source string
description string
helpFile string
}
func TestTryGetOPCError(t *testing.T) {
type args struct {
err error
}
tests := []struct {
name string
args args
wantErr assert.ErrorAssertionFunc
}{
{
name: "nil error",
args: args{
err: nil,
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.Nil(t, err, i...)
},
},
{
name: "non OPC error",
args: args{
err: fmt.Errorf("some error"),
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
return assert.ErrorContains(t, err, "some error", i...)
},
},
{
name: "OLE error",
args: args{
err: ole.NewError(0x80004003),
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
opcErr := TryGetOPCError(ole.NewError(0x80004003))
if opcErr == nil {
return assert.Fail(t, "expected OPC error, got nil", i...)
}
return assert.Equal(t, uintptr(0x80004003), opcErr.(*ole.OleError).Code(), i...)
},
},
{
name: "OLE Exception error",
args: args{
err: ole.NewErrorWithSubError(0x80004003, "Test Exception", ole.EXCEPINFO{}),
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
opcErr := TryGetOPCError(ole.NewErrorWithSubError(0x80004003, "Test Exception", ole.EXCEPINFO{}))
if opcErr == nil {
return assert.Fail(t, "expected OPC error, got nil", i...)
}
return assert.Equal(t, opcErr, err)
},
},
{
name: "suberror not OLE error",
args: args{
err: ole.NewErrorWithSubError(0x80004003, "Test Exception", fmt.Errorf("some suberror")),
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
opcErr := TryGetOPCError(ole.NewErrorWithSubError(0x80004003, "Test Exception", fmt.Errorf("some suberror")))
if opcErr == nil {
return assert.Fail(t, "expected OPC error, got nil", i...)
}
return assert.Equal(t, opcErr, err)
},
},
{
name: "OPC error",
args: args{
err: ole.NewErrorWithSubError(0x80004003, "Test Exception", *(*ole.EXCEPINFO)(unsafe.Pointer(&exceptionInfo{
scode: 0xC0040007,
}))),
},
wantErr: func(t assert.TestingT, err error, i ...interface{}) bool {
want := NewOPCError(0xC0040007)
return assert.Equal(t, want, err, i)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.wantErr(t, TryGetOPCError(tt.args.err), fmt.Sprintf("TryGetOPCError(%v)", tt.args.err))
})
}
}
func TestOPCError(t *testing.T) {
err := NewOPCError(0xC0040007)
assert.Equal(t, "OPC ERROR code:0xC0040007, error:The item ID is not defined in the server address space (on add or validate) or no longer exists in the server address space (for read or write).", err.Error())
err = NewOPCError(0x800706BA)
assert.Equal(t, "OPC ERROR code:0x800706BA, error:Unknown error, may be system error: The RPC server is unavailable.", err.Error())
err = NewOPCError(0x12345678)
assert.Equal(t, "OPC ERROR code:0x12345678, error:Unknown error, may be system error: winapi error #305419896", err.Error())
}