Skip to content

Commit

Permalink
test: add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Laisky committed Mar 9, 2023
1 parent 8469f97 commit c39f3d1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 38 deletions.
47 changes: 47 additions & 0 deletions crypto/kms/dto_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kms

import (
"testing"

"github.com/stretchr/testify/require"

gutils "github.com/Laisky/go-utils/v4"
)

func TestEncryptedItem_Unmarshal(t *testing.T) {
type args struct {
KekID uint16
DekID []byte
Ciphertext []byte
}
tests := []struct {
name string
args args
wantErr bool
}{
{"1", args{1, []byte("213123"), []byte("2342342")}, false},
{"2", args{1, []byte(gutils.RandomStringWithLength(1024)), []byte(gutils.RandomStringWithLength(1024))}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &EncryptedData{
Version: EncryptedItemVer1,
KekID: tt.args.KekID,
DekID: tt.args.DekID,
Ciphertext: tt.args.Ciphertext,
}

data, err := e.Marshal()
require.NoError(t, err)

e2 := EncryptedData{}
err = e2.Unmarshal(data)
require.NoError(t, err)

require.Equal(t, e.Version, e2.Version)
require.Equal(t, e.KekID, e2.KekID)
require.Equal(t, e.DekID, e2.DekID)
require.Equal(t, e.Ciphertext, e2.Ciphertext)
})
}
}
38 changes: 0 additions & 38 deletions crypto/kms/mem/kms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,41 +134,3 @@ func TestKMS_Decrypt(t *testing.T) {

wg.Wait()
}

func TestEncryptedItem_Unmarshal(t *testing.T) {
type args struct {
KekID uint16
DekID []byte
Ciphertext []byte
}
tests := []struct {
name string
args args
wantErr bool
}{
{"1", args{1, []byte("213123"), []byte("2342342")}, false},
{"2", args{1, []byte(gutils.RandomStringWithLength(1024)), []byte(gutils.RandomStringWithLength(1024))}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
e := &gkms.EncryptedData{
Version: gkms.EncryptedItemVer1,
KekID: tt.args.KekID,
DekID: tt.args.DekID,
Ciphertext: tt.args.Ciphertext,
}

data, err := e.Marshal()
require.NoError(t, err)

e2 := new(gkms.EncryptedData)
err = e2.Unmarshal(data)
require.NoError(t, err)

require.Equal(t, e.Version, e2.Version)
require.Equal(t, e.KekID, e2.KekID)
require.Equal(t, e.DekID, e2.DekID)
require.Equal(t, e.Ciphertext, e2.Ciphertext)
})
}
}

0 comments on commit c39f3d1

Please sign in to comment.