-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcredential_errors_test.go
More file actions
146 lines (131 loc) · 3.87 KB
/
credential_errors_test.go
File metadata and controls
146 lines (131 loc) · 3.87 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
package credentials
import (
"encoding/base64"
"sync"
"testing"
"time"
"github.com/Rocket-Rescue-Node/credentials/pb"
)
// TestCreateInvalidNodeIDLength tests the Create method with an invalid node ID length
func TestCreateInvalidNodeIDLength(t *testing.T) {
cm := NewCredentialManager([]byte("test secret"))
_, err := cm.Create(time.Now(), []byte("short"), pb.OperatorType_OT_SOLO)
if err == nil {
t.Error("Expected error for invalid node ID length, got nil")
}
}
// TestJSONUnmarshalError tests JSON unmarshaling error cases
func TestJSONUnmarshalError(t *testing.T) {
testCases := []struct {
name string
json string
}{
{"InvalidJSON", `{invalid json`},
{"InvalidNodeID", `{"node_id":"invalid","timestamp":0,"operator_type":0,"mac":""}`},
{"InvalidMac", `{"node_id":"0x1234567890123456789012345678901234567890","timestamp":0,"operator_type":0,"mac":"invalid!"}`},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ac := new(AuthenticatedCredential)
err := ac.UnmarshalJSON([]byte(tc.json))
if err == nil {
t.Errorf("Expected unmarshal error for %s, got nil", tc.name)
}
})
}
}
// TestBase64URLEncodeUsernameError tests error case for Base64URLEncodeUsername
func TestBase64URLEncodeUsernameError(t *testing.T) {
ac := &AuthenticatedCredential{
Credential: &pb.Credential{
NodeId: nil,
},
}
result := ac.Base64URLEncodeUsername()
if result != "" {
t.Error("Expected empty string for nil NodeId, got non-empty string")
}
}
// TestBase64URLDecodeErrors tests error cases for Base64URLDecode
func TestBase64URLDecodeErrors(t *testing.T) {
testCases := []struct {
name string
username string
password string
}{
{"InvalidUsername", "invalid!", "valid"},
{"InvalidPassword", "valid", "invalid!"},
{"InvalidProto",
base64.URLEncoding.EncodeToString([]byte("valid")),
base64.URLEncoding.EncodeToString([]byte("invalid proto")),
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var ac AuthenticatedCredential
err := ac.Base64URLDecode(tc.username, tc.password)
if err == nil {
t.Errorf("Expected error for %s, got nil", tc.name)
}
})
}
}
// TestVerifyErrors tests error cases for Verify method
func TestVerifyErrors(t *testing.T) {
cm := NewCredentialManager([]byte("test secret"))
// Test with invalid credential (missing fields)
invalidCred := &AuthenticatedCredential{
Credential: &pb.Credential{},
}
_, err := cm.Verify(invalidCred)
if err == nil {
t.Error("Expected error for invalid credential, got nil")
}
// Test with mismatched MAC
validCred, err := cm.Create(time.Now(), make([]byte, 20), pb.OperatorType_OT_SOLO)
if err != nil {
t.Fatalf("Failed to create valid credential: %v", err)
}
validCred.Mac = []byte("invalid mac")
_, err = cm.Verify(validCred)
if err != MismatchError {
t.Errorf("Expected MismatchError, got %v", err)
}
// Test with nil Credential field
nilFieldCred := &AuthenticatedCredential{
Credential: nil,
Mac: make([]byte, 32),
}
_, err = cm.Verify(nilFieldCred)
if err == nil {
t.Error("Expected error for nil Credential field, got nil")
}
}
// TestMemoryError simulates a memory allocation error
func TestMemoryError(t *testing.T) {
cm := &CredentialManager{
p: sync.Pool{
New: func() interface{} {
return "not a checker"
},
},
}
// Test Create method
_, err := cm.Create(time.Now(), make([]byte, 20), pb.OperatorType_OT_SOLO)
if err == nil || err.Error() != MemoryError.Error() {
t.Errorf("Expected MemoryError, got %v", err)
}
// Test Verify method
validCred := &AuthenticatedCredential{
Credential: &pb.Credential{
NodeId: make([]byte, 20),
Timestamp: time.Now().Unix(),
OperatorType: pb.OperatorType_OT_SOLO,
},
Mac: make([]byte, 32),
}
_, err = cm.Verify(validCred)
if err == nil || err.Error() != MemoryError.Error() {
t.Errorf("Expected MemoryError, got %v", err)
}
}