-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchid_darwin_test.go
76 lines (68 loc) · 1.93 KB
/
touchid_darwin_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
package touchid
import (
"fmt"
"testing"
"time"
)
func TestAuth(t *testing.T) {
tests := []struct {
name string
deviceType DeviceType
reason string
wantErr bool
}{
{"DeviceTypeAny", DeviceTypeAny, "Confirm Action", false},
{"DeviceTypeBiometrics", DeviceTypeBiometrics, "Confirm Action", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
success, err := Auth(tt.deviceType, tt.reason)
if (err != nil) != tt.wantErr {
t.Errorf("Auth() error = %v, wantErr %v", err, tt.wantErr)
return
}
if err == nil && !success {
t.Errorf("Auth() success = %v, want true", success)
}
fmt.Printf("%s: Authentication %s\n", tt.name, map[bool]string{true: "successful", false: "failed"}[success])
})
}
}
func TestSerialAuth(t *testing.T) {
deviceType := DeviceTypeBiometrics
reason := "Confirm Action"
timeout := 10 * time.Second
// First authentication
success, err := SerialAuth(deviceType, reason, timeout)
if err != nil {
t.Fatalf("SerialAuth() error = %v", err)
}
if !success {
t.Fatalf("SerialAuth() success = %v, want true", success)
}
fmt.Println("First authentication successful")
// Second authentication (should not prompt)
start := time.Now()
success, err = SerialAuth(deviceType, reason, timeout)
if err != nil {
t.Fatalf("SerialAuth() error = %v", err)
}
if !success {
t.Fatalf("SerialAuth() success = %v, want true", success)
}
if time.Since(start) > time.Second {
t.Errorf("SerialAuth() took too long, expected instant response")
}
fmt.Println("Second authentication successful (no prompt)")
// Wait for timeout to expire
time.Sleep(timeout)
// Third authentication (should prompt again)
success, err = SerialAuth(deviceType, reason, timeout)
if err != nil {
t.Fatalf("SerialAuth() error = %v", err)
}
if !success {
t.Fatalf("SerialAuth() success = %v, want true", success)
}
fmt.Println("Third authentication successful (after timeout)")
}