-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors_test.go
More file actions
1366 lines (1240 loc) · 34 KB
/
errors_test.go
File metadata and controls
1366 lines (1240 loc) · 34 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2026 The Zaparoo Project Contributors.
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package pn532
import (
"errors"
"fmt"
"strings"
"syscall"
"testing"
"time"
)
func TestIsRetryable(t *testing.T) {
t.Parallel()
tests := getIsRetryableTestCases()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := IsRetryable(tt.err)
if got != tt.want {
t.Errorf("IsRetryable() = %v, want %v", got, tt.want)
}
})
}
}
//nolint:funlen // Test data table - length is acceptable for test cases
func getIsRetryableTestCases() []struct {
err error
name string
want bool
} {
return []struct {
err error
name string
want bool
}{
{
name: "nil error",
err: nil,
want: false,
},
{
name: "transport timeout retryable",
err: ErrTransportTimeout,
want: true,
},
{
name: "transport read retryable",
err: ErrTransportRead,
want: true,
},
{
name: "transport write retryable",
err: ErrTransportWrite,
want: true,
},
{
name: "communication failed retryable",
err: ErrCommunicationFailed,
want: true,
},
{
name: "no ACK not retryable",
err: ErrNoACK,
want: false,
},
{
name: "frame corrupted retryable",
err: ErrFrameCorrupted,
want: true,
},
{
name: "checksum mismatch retryable",
err: ErrChecksumMismatch,
want: true,
},
{
name: "device not found not retryable",
err: ErrDeviceNotFound,
want: false,
},
{
name: "tag not found not retryable",
err: ErrTagNotFound,
want: false,
},
{
name: "data too large not retryable",
err: ErrDataTooLarge,
want: false,
},
{
name: "invalid parameter not retryable",
err: ErrInvalidParameter,
want: false,
},
{
name: "wrapped retryable error",
err: errors.New("outer: " + ErrTransportTimeout.Error()),
want: false,
},
// New retryable error types added for sliding card use case
{
name: "tag read failed is retryable",
err: ErrTagReadFailed,
want: true,
},
{
name: "tag data corrupt is retryable",
err: ErrTagDataCorrupt,
want: true,
},
{
name: "wrapped tag read failed is retryable",
err: fmt.Errorf("block 4: %w", ErrTagReadFailed),
want: true,
},
{
name: "wrapped tag data corrupt is retryable",
err: fmt.Errorf("invalid CC: %w", ErrTagDataCorrupt),
want: true,
},
}
}
func TestIsFatal(t *testing.T) {
t.Parallel()
tests := []struct {
err error
name string
want bool
}{
{
name: "nil error",
err: nil,
want: false,
},
{
name: "transport closed is fatal",
err: ErrTransportClosed,
want: true,
},
{
name: "device not found is fatal",
err: ErrDeviceNotFound,
want: true,
},
{
name: "device not supported is fatal",
err: ErrDeviceNotSupported,
want: true,
},
{
name: "transport timeout is not fatal",
err: ErrTransportTimeout,
want: false,
},
{
name: "transport read is not fatal",
err: ErrTransportRead,
want: false,
},
{
name: "transport write is not fatal",
err: ErrTransportWrite,
want: false,
},
{
name: "tag auth failed is not fatal",
err: ErrTagAuthFailed,
want: false,
},
{
name: "command not supported is not fatal",
err: ErrCommandNotSupported,
want: false,
},
{
name: "random error is not fatal",
err: errors.New("random error"),
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := IsFatal(tt.err)
if got != tt.want {
t.Errorf("IsFatal() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsFatal_TransportError(t *testing.T) {
t.Parallel()
tests := []struct {
transport *TransportError
name string
want bool
}{
{
name: "transport error with permanent type is fatal",
transport: &TransportError{
Err: errors.New("device disconnected"),
Op: "read",
Port: "/dev/ttyUSB0",
Type: ErrorTypePermanent,
Retryable: false,
},
want: true,
},
{
name: "transport error with transient type is not fatal",
transport: &TransportError{
Err: errors.New("timeout"),
Op: "read",
Port: "/dev/ttyUSB0",
Type: ErrorTypeTransient,
Retryable: true,
},
want: false,
},
{
name: "transport error with timeout type is not fatal",
transport: &TransportError{
Err: errors.New("timeout"),
Op: "read",
Port: "/dev/ttyUSB0",
Type: ErrorTypeTimeout,
Retryable: true,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := IsFatal(tt.transport)
if got != tt.want {
t.Errorf("IsFatal() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsFatal_SyscallErrors(t *testing.T) {
t.Parallel()
tests := []struct {
err error
name string
want bool
}{
// Unix errors that indicate device disconnection
{
name: "EIO (input/output error) is fatal",
err: syscall.EIO,
want: true,
},
{
name: "ENXIO (no such device or address) is fatal",
err: syscall.ENXIO,
want: true,
},
{
name: "ENODEV (no such device) is fatal",
err: syscall.ENODEV,
want: true,
},
// Wrapped syscall errors should also be detected
{
name: "wrapped EIO is fatal",
err: fmt.Errorf("write failed: %w", syscall.EIO),
want: true,
},
{
name: "double-wrapped ENXIO is fatal",
err: fmt.Errorf("operation failed: %w", fmt.Errorf("write: %w", syscall.ENXIO)),
want: true,
},
// Non-fatal syscall errors
{
name: "EAGAIN is not fatal",
err: syscall.EAGAIN,
want: false,
},
{
name: "EINTR is not fatal",
err: syscall.EINTR,
want: false,
},
{
name: "ETIMEDOUT is not fatal",
err: syscall.ETIMEDOUT,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := IsFatal(tt.err)
if got != tt.want {
t.Errorf("IsFatal(%v) = %v, want %v", tt.err, got, tt.want)
}
})
}
}
func TestIsRetryable_TransportError(t *testing.T) {
t.Parallel()
tests := []struct {
transport *TransportError
name string
want bool
}{
{
name: "transport error retryable=true",
transport: &TransportError{
Err: errors.New("test error"),
Op: "read",
Port: "/dev/ttyUSB0",
Type: ErrorTypeTransient,
Retryable: true,
},
want: true,
},
{
name: "transport error retryable=false",
transport: &TransportError{
Err: errors.New("test error"),
Op: "write",
Port: "/dev/ttyUSB0",
Type: ErrorTypeTransient,
Retryable: false,
},
want: false,
},
{
name: "transport error with retryable underlying error but retryable=false",
transport: &TransportError{
Err: ErrTransportTimeout,
Op: "read",
Port: "/dev/ttyUSB0",
Type: ErrorTypeTimeout,
Retryable: false,
},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := IsRetryable(tt.transport)
if got != tt.want {
t.Errorf("IsRetryable() = %v, want %v", got, tt.want)
}
})
}
}
func TestNewTransportError(t *testing.T) {
t.Parallel()
tests := []struct {
err error
name string
op string
port string
errType ErrorType
}{
{
name: "basic transport error",
op: "read",
port: "/dev/ttyUSB0",
err: errors.New("permission denied"),
errType: ErrorTypePermanent,
},
{
name: "empty port",
op: "write",
port: "",
err: errors.New("connection lost"),
errType: ErrorTypeTransient,
},
{
name: "timeout error",
op: "command",
port: "ACR122U",
err: ErrTransportTimeout,
errType: ErrorTypeTimeout,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
transportErr := NewTransportError(tt.op, tt.port, tt.err, tt.errType)
if transportErr.Op != tt.op {
t.Errorf("Op = %q, want %q", transportErr.Op, tt.op)
}
if transportErr.Port != tt.port {
t.Errorf("Port = %q, want %q", transportErr.Port, tt.port)
}
if !errors.Is(transportErr.Err, tt.err) {
t.Errorf("Err = %v, want %v", transportErr.Err, tt.err)
}
if transportErr.Type != tt.errType {
t.Errorf("Type = %v, want %v", transportErr.Type, tt.errType)
}
})
}
}
func TestTransportError_Error(t *testing.T) {
t.Parallel()
tests := []struct {
name string
te *TransportError
want []string // Substrings that should be present
}{
{
name: "with port",
te: &TransportError{
Err: errors.New("connection failed"),
Op: "read",
Port: "/dev/ttyUSB0",
},
want: []string{"read", "/dev/ttyUSB0", "connection failed"},
},
{
name: "without port",
te: &TransportError{
Err: errors.New("device busy"),
Op: "write",
Port: "",
},
want: []string{"write", "device busy"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := tt.te.Error()
for _, substr := range tt.want {
if !strings.Contains(got, substr) {
t.Errorf("Error() = %q, should contain %q", got, substr)
}
}
})
}
}
func TestTransportError_Unwrap(t *testing.T) {
t.Parallel()
originalErr := errors.New("original error")
transportErr := &TransportError{
Err: originalErr,
Op: "test",
Port: "/dev/test",
}
unwrapped := transportErr.Unwrap()
if !errors.Is(unwrapped, originalErr) {
t.Errorf("Unwrap() = %v, want %v", unwrapped, originalErr)
}
}
func TestNewTimeoutError(t *testing.T) {
t.Parallel()
te := NewTimeoutError("read", "/dev/ttyUSB0")
if te.Op != "read" {
t.Errorf("Op = %q, want %q", te.Op, "read")
}
if te.Port != "/dev/ttyUSB0" {
t.Errorf("Port = %q, want %q", te.Port, "/dev/ttyUSB0")
}
if te.Type != ErrorTypeTimeout {
t.Errorf("Type = %v, want %v", te.Type, ErrorTypeTimeout)
}
if !te.Retryable {
t.Error("Retryable should be true for timeout errors")
}
}
func TestNewFrameCorruptedError(t *testing.T) {
t.Parallel()
te := NewFrameCorruptedError("read", "/dev/ttyUSB0")
if te.Op != "read" {
t.Errorf("Op = %q, want %q", te.Op, "read")
}
if te.Port != "/dev/ttyUSB0" {
t.Errorf("Port = %q, want %q", te.Port, "/dev/ttyUSB0")
}
if te.Type != ErrorTypeTransient {
t.Errorf("Type = %v, want %v", te.Type, ErrorTypeTransient)
}
if !te.Retryable {
t.Error("Retryable should be true for frame corrupted errors")
}
}
func TestNewDataTooLargeError(t *testing.T) {
t.Parallel()
te := NewDataTooLargeError("write", "/dev/ttyUSB0")
if te.Op != "write" {
t.Errorf("Op = %q, want %q", te.Op, "write")
}
if te.Port != "/dev/ttyUSB0" {
t.Errorf("Port = %q, want %q", te.Port, "/dev/ttyUSB0")
}
if te.Type != ErrorTypePermanent {
t.Errorf("Type = %v, want %v", te.Type, ErrorTypePermanent)
}
if te.Retryable {
t.Error("Retryable should be false for data too large errors")
}
}
// PN532Error Tests
func TestNewPN532Error(t *testing.T) {
t.Parallel()
tests := []struct {
name string
command string
context string
wantMessage string
errorCode byte
}{
{
name: "timeout error without context",
errorCode: 0x01,
command: "InDataExchange",
context: "",
wantMessage: "InDataExchange error 0x01 (timeout)",
},
{
name: "authentication error with context",
errorCode: 0x14,
command: "InDataExchange",
context: "authentication failure",
wantMessage: "InDataExchange error 0x14 (authentication error): authentication failure",
},
{
name: "command not supported",
errorCode: 0x81,
command: "InCommunicateThru",
context: "",
wantMessage: "InCommunicateThru error 0x81 (command not supported)",
},
{
name: "error with detailed context",
errorCode: 0x02,
command: "InListPassiveTarget",
context: "no targets found in field",
wantMessage: "InListPassiveTarget error 0x02 (CRC error): no targets found in field",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := NewPN532Error(tt.errorCode, tt.command, tt.context)
if err.ErrorCode != tt.errorCode {
t.Errorf("ErrorCode = 0x%02X, want 0x%02X", err.ErrorCode, tt.errorCode)
}
if err.Command != tt.command {
t.Errorf("Command = %q, want %q", err.Command, tt.command)
}
if err.Context != tt.context {
t.Errorf("Context = %q, want %q", err.Context, tt.context)
}
if err.Error() != tt.wantMessage {
t.Errorf("Error() = %q, want %q", err.Error(), tt.wantMessage)
}
})
}
}
//nolint:gocognit,revive // Test function with multiple field validations
func TestNewPN532ErrorWithDetails(t *testing.T) {
t.Parallel()
tests := []struct {
name string
command string
wantMessage string
bytesSent int
errorCode byte
target byte
}{
{
name: "timeout error with bytes sent and target",
errorCode: 0x01,
command: "InDataExchange",
bytesSent: 16,
target: 1,
wantMessage: "InDataExchange error 0x01 (timeout) [sent 16 bytes, target 1]",
},
{
name: "authentication error with protocol details",
errorCode: 0x14,
command: "InCommunicateThru",
bytesSent: 32,
target: 2,
wantMessage: "InCommunicateThru error 0x14 (authentication error) [sent 32 bytes, target 2]",
},
{
name: "CRC error with many bytes",
errorCode: 0x02,
command: "InDataExchange",
bytesSent: 255,
target: 1,
wantMessage: "InDataExchange error 0x02 (CRC error) [sent 255 bytes, target 1]",
},
{
name: "error with zero bytes sent (omits details)",
errorCode: 0x03,
command: "InDataExchange",
bytesSent: 0,
target: 1,
wantMessage: "InDataExchange error 0x03 (parity error)",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := NewPN532ErrorWithDetails(tt.errorCode, tt.command, tt.bytesSent, tt.target)
if err.ErrorCode != tt.errorCode {
t.Errorf("ErrorCode = 0x%02X, want 0x%02X", err.ErrorCode, tt.errorCode)
}
if err.Command != tt.command {
t.Errorf("Command = %q, want %q", err.Command, tt.command)
}
if err.BytesSent != tt.bytesSent {
t.Errorf("BytesSent = %d, want %d", err.BytesSent, tt.bytesSent)
}
if err.Target != tt.target {
t.Errorf("Target = %d, want %d", err.Target, tt.target)
}
if err.Error() != tt.wantMessage {
t.Errorf("Error() = %q, want %q", err.Error(), tt.wantMessage)
}
})
}
}
//nolint:funlen // Test data table - length is acceptable for comprehensive error type coverage
func TestPN532Error_ErrorTypeChecks(t *testing.T) {
t.Parallel()
tests := []struct {
name string
errorCode byte
wantIsCommandNotSupported bool
wantIsAuthenticationError bool
wantIsTimeoutError bool
wantIsRFError bool
}{
{
name: "command not supported",
errorCode: 0x81,
wantIsCommandNotSupported: true,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: false,
},
{
name: "authentication error",
errorCode: 0x14,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: true,
wantIsTimeoutError: false,
wantIsRFError: false,
},
{
name: "timeout error",
errorCode: 0x01,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: true,
wantIsRFError: false,
},
{
name: "other error",
errorCode: 0xFF,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: false,
},
// RF error codes
{
name: "CRC error",
errorCode: 0x02,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
{
name: "parity error",
errorCode: 0x03,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
{
name: "framing error",
errorCode: 0x05,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
{
name: "RF field not activated",
errorCode: 0x0A,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
{
name: "RF protocol error",
errorCode: 0x0B,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
{
name: "wrong context - target selection lost",
errorCode: 0x27,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
{
name: "target released by initiator",
errorCode: 0x29,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
{
name: "card disappeared",
errorCode: 0x2B,
wantIsCommandNotSupported: false,
wantIsAuthenticationError: false,
wantIsTimeoutError: false,
wantIsRFError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := NewPN532Error(tt.errorCode, "TestCommand", "")
if got := err.IsCommandNotSupported(); got != tt.wantIsCommandNotSupported {
t.Errorf("IsCommandNotSupported() = %v, want %v", got, tt.wantIsCommandNotSupported)
}
if got := err.IsAuthenticationError(); got != tt.wantIsAuthenticationError {
t.Errorf("IsAuthenticationError() = %v, want %v", got, tt.wantIsAuthenticationError)
}
if got := err.IsTimeoutError(); got != tt.wantIsTimeoutError {
t.Errorf("IsTimeoutError() = %v, want %v", got, tt.wantIsTimeoutError)
}
if got := err.IsRFError(); got != tt.wantIsRFError {
t.Errorf("IsRFError() = %v, want %v", got, tt.wantIsRFError)
}
})
}
}
func TestIsRetryable_PN532Error(t *testing.T) {
t.Parallel()
tests := []struct {
name string
errorCode byte
want bool
}{
{name: "timeout error is retryable", errorCode: 0x01, want: true},
{name: "authentication error is retryable", errorCode: 0x14, want: true},
{name: "command not supported is not retryable", errorCode: 0x81, want: false},
{name: "unknown error is not retryable", errorCode: 0xFF, want: false},
// RF errors are retryable (common during card sliding)
{name: "CRC error is retryable", errorCode: 0x02, want: true},
{name: "parity error is retryable", errorCode: 0x03, want: true},
{name: "framing error is retryable", errorCode: 0x05, want: true},
{name: "RF field not activated is retryable", errorCode: 0x0A, want: true},
{name: "RF protocol error is retryable", errorCode: 0x0B, want: true},
{name: "target released is retryable", errorCode: 0x29, want: true},
{name: "card disappeared is retryable", errorCode: 0x2B, want: true},
// Non-RF errors remain not retryable
{name: "erroneous bit count is not retryable", errorCode: 0x04, want: false},
{name: "buffer overflow is not retryable", errorCode: 0x09, want: false},
{name: "invalid parameter is not retryable", errorCode: 0x10, want: false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
err := NewPN532Error(tt.errorCode, "TestCommand", "")
got := IsRetryable(err)
if got != tt.want {
t.Errorf("IsRetryable() = %v, want %v", got, tt.want)
}
})
}
}
func TestIsCommandNotSupported(t *testing.T) {
t.Parallel()
tests := []struct {
err error
name string
want bool
}{
{
name: "PN532Error command not supported",
err: NewPN532Error(0x81, "TestCommand", ""),
want: true,
},
{
name: "PN532Error timeout",
err: NewPN532Error(0x01, "TestCommand", ""),
want: false,
},
{
name: "ErrCommandNotSupported",
err: ErrCommandNotSupported,
want: true,
},
{
name: "wrapped ErrCommandNotSupported",
err: errors.New("failed: " + ErrCommandNotSupported.Error()),
want: false, // errors.Is won't match wrapped string
},
{
name: "other error",
err: errors.New("some other error"),
want: false,
},
{
name: "nil error",
err: nil,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := IsCommandNotSupported(tt.err)
if got != tt.want {
t.Errorf("IsCommandNotSupported() = %v, want %v", got, tt.want)
}
})
}
}
func TestPN532ErrorHelperFunctions(t *testing.T) {
t.Parallel()
tests := []struct {
err error
name string
wantIsPN532AuthenticationError bool
wantIsPN532TimeoutError bool
}{
{
name: "PN532Error authentication",
err: NewPN532Error(0x14, "InDataExchange", ""),
wantIsPN532AuthenticationError: true,
wantIsPN532TimeoutError: false,
},
{
name: "PN532Error timeout",
err: NewPN532Error(0x01, "InDataExchange", ""),
wantIsPN532AuthenticationError: false,
wantIsPN532TimeoutError: true,
},
{
name: "other error",
err: errors.New("some error"),
wantIsPN532AuthenticationError: false,
wantIsPN532TimeoutError: false,
},
{
name: "nil error",
err: nil,
wantIsPN532AuthenticationError: false,
wantIsPN532TimeoutError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
if got := IsPN532AuthenticationError(tt.err); got != tt.wantIsPN532AuthenticationError {
t.Errorf("IsPN532AuthenticationError() = %v, want %v", got, tt.wantIsPN532AuthenticationError)
}
if got := IsPN532TimeoutError(tt.err); got != tt.wantIsPN532TimeoutError {
t.Errorf("IsPN532TimeoutError() = %v, want %v", got, tt.wantIsPN532TimeoutError)
}
})
}
}
// =============================================================================
// Trace Tests
// =============================================================================
func TestTraceBuffer_BasicOperations(t *testing.T) {
t.Parallel()
tb := NewTraceBuffer("UART", "/dev/ttyUSB0", 10)
// Record TX and RX
tb.RecordTX([]byte{0x00, 0x00, 0xFF, 0x02, 0xFE, 0xD4, 0x02, 0x2A, 0x00}, "Cmd 0x02")
tb.RecordRX([]byte{0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00}, "ACK")
tb.RecordRX([]byte{0x00, 0x00, 0xFF, 0x06, 0xFA, 0xD5, 0x03, 0x32, 0x01, 0x06, 0x07, 0xE8, 0x00}, "Response")
// Wrap an error
originalErr := errors.New("test error")
wrappedErr := tb.WrapError(originalErr)
// Verify it's a TraceableError
var te *TraceableError
if !errors.As(wrappedErr, &te) {
t.Fatal("WrapError should return a TraceableError")
}
// Verify trace entries
if len(te.Trace) != 3 {
t.Errorf("Expected 3 trace entries, got %d", len(te.Trace))
}
// Verify first entry is TX
if te.Trace[0].Direction != TraceTX {
t.Errorf("First entry should be TX, got %v", te.Trace[0].Direction)
}
// Verify transport and port
if te.Transport != "UART" {
t.Errorf("Transport = %q, want %q", te.Transport, "UART")
}
if te.Port != "/dev/ttyUSB0" {
t.Errorf("Port = %q, want %q", te.Port, "/dev/ttyUSB0")
}
}
func TestTraceableError_Unwrap(t *testing.T) {
t.Parallel()
originalErr := ErrNoACK
tb := NewTraceBuffer("I2C", "/dev/i2c-1", 10)
tb.RecordTX([]byte{0x01, 0x02}, "test")
wrappedErr := tb.WrapError(originalErr)
// errors.Is should work through TraceableError
if !errors.Is(wrappedErr, ErrNoACK) {
t.Error("errors.Is should match underlying error through TraceableError")
}
// Unwrap should return original error
var te *TraceableError
if errors.As(wrappedErr, &te) {