Skip to content

Commit eb79c3c

Browse files
committed
Add some nil test cases to the crypto pkg
Signed-off-by: Tiago Scolari <[email protected]
1 parent cb02db4 commit eb79c3c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

client/internal/crypto/helpers_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package crypto_test
22

33
import (
4+
"reflect"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -33,6 +34,14 @@ func TestPayloadMethods(t *testing.T) {
3334
protoMsg: &runtimev1.DecryptResponse{},
3435
inputData: []byte("test data"),
3536
},
37+
"with nil proto message": {
38+
protoMsg: nilValue[runtimev1.EncryptRequest](),
39+
inputData: []byte("test data"),
40+
},
41+
"with nil input data": {
42+
protoMsg: &runtimev1.EncryptRequest{},
43+
inputData: nil,
44+
},
3645
}
3746

3847
for name, tc := range testCases {
@@ -57,6 +66,11 @@ func TestPayloadMethods(t *testing.T) {
5766
require.Failf(t, "unsupported proto message type", "the type was %T", r)
5867
}
5968

69+
if reflect.ValueOf(tc.protoMsg).IsNil() {
70+
assert.Nil(t, outputPayload.GetData(), "the payload should be nil")
71+
return
72+
}
73+
6074
assert.Equal(t, tc.inputData, outputPayload.GetData(), "payload should match the input")
6175
})
6276
}
@@ -79,6 +93,16 @@ func TestSetOptions(t *testing.T) {
7993
KeyName: "testing",
8094
},
8195
},
96+
"with nil proto message": {
97+
protoMsg: nilValue[runtimev1.EncryptRequest](),
98+
options: &runtimev1.EncryptRequestOptions{
99+
KeyName: "testing",
100+
},
101+
},
102+
"with nil options": {
103+
protoMsg: &runtimev1.EncryptRequest{},
104+
options: nilValue[runtimev1.EncryptRequestOptions](),
105+
},
82106
}
83107

84108
for name, tc := range testCases {
@@ -96,6 +120,11 @@ func TestSetOptions(t *testing.T) {
96120
require.Failf(t, "unsupported proto message type", "the type was %T", r)
97121
}
98122

123+
if reflect.ValueOf(tc.protoMsg).IsNil() {
124+
assert.Nil(t, outputOptions, "the payload should be nil")
125+
return
126+
}
127+
99128
assert.Equal(t, tc.options, outputOptions, "options should be persisted")
100129
})
101130
}
@@ -117,6 +146,12 @@ func TestReset(t *testing.T) {
117146
"DecryptResponse": {
118147
protoMsg: &runtimev1.DecryptResponse{Payload: &commonv1.StreamPayload{Data: []byte("test data")}},
119148
},
149+
"with nil proto message": {
150+
protoMsg: nilValue[runtimev1.EncryptRequest](),
151+
},
152+
"with nil payload": {
153+
protoMsg: &runtimev1.EncryptRequest{Payload: nil},
154+
},
120155
}
121156

122157
for name, tc := range testCases {
@@ -144,3 +179,7 @@ func TestReset(t *testing.T) {
144179
})
145180
}
146181
}
182+
183+
func nilValue[T any]() *T {
184+
return nil
185+
}

0 commit comments

Comments
 (0)