-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathhandle_test.go
More file actions
197 lines (172 loc) · 5.1 KB
/
Copy pathhandle_test.go
File metadata and controls
197 lines (172 loc) · 5.1 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
// Copyright (c) 2015-2025 The libusb developers. All rights reserved.
// Project site: https://github.com/gotmc/libusb
// Use of this source code is governed by a MIT-style license that
// can be found in the LICENSE.txt file for the project.
package libusb
import (
"testing"
)
func TestDeviceHandleNilChecks(t *testing.T) {
// Test with completely nil handle
var dh *DeviceHandle
// Test StringDescriptor with nil handle
_, err := dh.StringDescriptor(0, 0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"StringDescriptor should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test StringDescriptorASCII with nil handle
_, err = dh.StringDescriptorASCII(0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"StringDescriptorASCII should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test Close with nil handle
err = dh.Close()
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"Close should return errorInvalidParam for nil handle, got %v", err,
)
}
// Test Configuration with nil handle
_, err = dh.Configuration()
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"Configuration should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test SetConfiguration with nil handle
err = dh.SetConfiguration(1)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"SetConfiguration should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test ClaimInterface with nil handle
err = dh.ClaimInterface(0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"ClaimInterface should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test ReleaseInterface with nil handle
err = dh.ReleaseInterface(0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"ReleaseInterface should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test SetInterfaceAltSetting with nil handle
err = dh.SetInterfaceAltSetting(0, 0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"SetInterfaceAltSetting should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test ResetDevice with nil handle
err = dh.ResetDevice()
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"ResetDevice should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test KernelDriverActive with nil handle
_, err = dh.KernelDriverActive(0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"KernelDriverActive should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test DetachKernelDriver with nil handle
err = dh.DetachKernelDriver(0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"DetachKernelDriver should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test AttachKernelDriver with nil handle
err = dh.AttachKernelDriver(0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"AttachKernelDriver should return errorInvalidParam for nil handle, got %v",
err,
)
}
// Test SetAutoDetachKernelDriver with nil handle
err = dh.SetAutoDetachKernelDriver(true)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"SetAutoDetachKernelDriver should return errorInvalidParam for nil handle, got %v",
err,
)
}
}
func TestDeviceHandleWithNilInternalPointer(t *testing.T) {
// Test with handle struct but nil internal pointer
dh := &DeviceHandle{libusbDeviceHandle: nil}
// Test StringDescriptor
_, err := dh.StringDescriptor(0, 0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"StringDescriptor should return errorInvalidParam for nil internal pointer, got %v",
err,
)
}
// Test StringDescriptorASCII
_, err = dh.StringDescriptorASCII(0)
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"StringDescriptorASCII should return errorInvalidParam for nil internal pointer, got %v",
err,
)
}
// Test Close
err = dh.Close()
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"Close should return errorInvalidParam for nil internal pointer, got %v",
err,
)
}
// Test Configuration
_, err = dh.Configuration()
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"Configuration should return errorInvalidParam for nil internal pointer, got %v",
err,
)
}
}
func TestDeviceHandleCloseIdempotent(t *testing.T) {
// Create a handle with non-nil pointer (will be invalid but that's ok for this test)
dh := &DeviceHandle{}
// Close should set the pointer to nil
err := dh.Close()
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf(
"Close should return errorInvalidParam for nil internal pointer, got %v",
err,
)
}
// Verify the handle is now nil
if dh.libusbDeviceHandle != nil {
t.Error("Close should set internal pointer to nil")
}
// Second close should still be safe
err = dh.Close()
if err == nil || err != ErrorCode(errorInvalidParam) {
t.Errorf("Second Close should return errorInvalidParam, got %v", err)
}
}