fix(aes-encryption): labels.Serialize cover case when GenerateNonce errors

Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
This commit is contained in:
ivan katliarchuk 2024-12-29 13:10:02 +00:00
parent ca517446cc
commit 41cea23fff
No known key found for this signature in database
GPG Key ID: 601CDBBBB76E47BE

View File

@ -18,6 +18,7 @@ package endpoint
import (
"bytes"
"crypto/rand"
"fmt"
"testing"
@ -102,11 +103,39 @@ func (suite *LabelsSuite) TestEncryptionFailed() {
log.StandardLogger().SetOutput(b)
_ = foo.Serialize(false, true, []byte("wrong-key"))
suite.True(fatalCrash, "should fail if encryption key is wrong")
suite.Contains(b.String(), "Failed to encrypt the text")
}
func (suite *LabelsSuite) TestEncryptionFailedFaultyReader() {
foo, err := NewLabelsFromString(suite.fooAsTextEncrypted, suite.aesKey)
suite.NoError(err, "should succeed for valid label text")
// remove encryption nonce just for simplicity, so that we could regenerate nonce
delete(foo, txtEncryptionNonce)
originalRandReader := rand.Reader
defer func() {
log.StandardLogger().ExitFunc = nil
rand.Reader = originalRandReader
}()
// Replace rand.Reader with a faulty reader
rand.Reader = &faultyReader{}
b := new(bytes.Buffer)
var fatalCrash bool
log.StandardLogger().ExitFunc = func(int) { fatalCrash = true }
log.StandardLogger().SetOutput(b)
_ = foo.Serialize(false, true, suite.aesKey)
suite.True(fatalCrash)
suite.Contains(b.String(), "Failed to generate cryptographic nonce")
}
func (suite *LabelsSuite) TestDeserialize() {
foo, err := NewLabelsFromStringPlain(suite.fooAsText)
suite.NoError(err, "should succeed for valid label text")