Skip to content

Commit 41cea23

Browse files
fix(aes-encryption): labels.Serialize cover case when GenerateNonce errors
Signed-off-by: ivan katliarchuk <[email protected]>
1 parent ca51744 commit 41cea23

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

endpoint/labels_test.go

+30-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package endpoint
1818

1919
import (
2020
"bytes"
21+
"crypto/rand"
2122
"fmt"
2223
"testing"
2324

@@ -102,11 +103,39 @@ func (suite *LabelsSuite) TestEncryptionFailed() {
102103
log.StandardLogger().SetOutput(b)
103104

104105
_ = foo.Serialize(false, true, []byte("wrong-key"))
105-
106+
106107
suite.True(fatalCrash, "should fail if encryption key is wrong")
107108
suite.Contains(b.String(), "Failed to encrypt the text")
108109
}
109110

111+
func (suite *LabelsSuite) TestEncryptionFailedFaultyReader() {
112+
foo, err := NewLabelsFromString(suite.fooAsTextEncrypted, suite.aesKey)
113+
suite.NoError(err, "should succeed for valid label text")
114+
115+
// remove encryption nonce just for simplicity, so that we could regenerate nonce
116+
delete(foo, txtEncryptionNonce)
117+
118+
originalRandReader := rand.Reader
119+
defer func() {
120+
log.StandardLogger().ExitFunc = nil
121+
rand.Reader = originalRandReader
122+
}()
123+
124+
// Replace rand.Reader with a faulty reader
125+
rand.Reader = &faultyReader{}
126+
127+
b := new(bytes.Buffer)
128+
129+
var fatalCrash bool
130+
log.StandardLogger().ExitFunc = func(int) { fatalCrash = true }
131+
log.StandardLogger().SetOutput(b)
132+
133+
_ = foo.Serialize(false, true, suite.aesKey)
134+
135+
suite.True(fatalCrash)
136+
suite.Contains(b.String(), "Failed to generate cryptographic nonce")
137+
}
138+
110139
func (suite *LabelsSuite) TestDeserialize() {
111140
foo, err := NewLabelsFromStringPlain(suite.fooAsText)
112141
suite.NoError(err, "should succeed for valid label text")

0 commit comments

Comments
 (0)