Skip to content

Commit

Permalink
codec, encryption, errs: testify the tests (#5066)
Browse files Browse the repository at this point in the history
ref #4813

Testify the pkg/codec, pkg/encryption, pkg/errs tests.

Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato authored May 30, 2022
1 parent 6c9f8f6 commit 109719f
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 201 deletions.
28 changes: 10 additions & 18 deletions pkg/codec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,31 @@ package codec
import (
"testing"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
)

func TestTable(t *testing.T) {
TestingT(t)
}

var _ = Suite(&testCodecSuite{})

type testCodecSuite struct{}

func (s *testCodecSuite) TestDecodeBytes(c *C) {
func TestDecodeBytes(t *testing.T) {
key := "abcdefghijklmnopqrstuvwxyz"
for i := 0; i < len(key); i++ {
_, k, err := DecodeBytes(EncodeBytes([]byte(key[:i])))
c.Assert(err, IsNil)
c.Assert(string(k), Equals, key[:i])
require.NoError(t, err)
require.Equal(t, key[:i], string(k))
}
}

func (s *testCodecSuite) TestTableID(c *C) {
func TestTableID(t *testing.T) {
key := EncodeBytes([]byte("t\x80\x00\x00\x00\x00\x00\x00\xff"))
c.Assert(key.TableID(), Equals, int64(0xff))
require.Equal(t, int64(0xff), key.TableID())

key = EncodeBytes([]byte("t\x80\x00\x00\x00\x00\x00\x00\xff_i\x01\x02"))
c.Assert(key.TableID(), Equals, int64(0xff))
require.Equal(t, int64(0xff), key.TableID())

key = []byte("t\x80\x00\x00\x00\x00\x00\x00\xff")
c.Assert(key.TableID(), Equals, int64(0))
require.Equal(t, int64(0), key.TableID())

key = EncodeBytes([]byte("T\x00\x00\x00\x00\x00\x00\x00\xff"))
c.Assert(key.TableID(), Equals, int64(0))
require.Equal(t, int64(0), key.TableID())

key = EncodeBytes([]byte("t\x80\x00\x00\x00\x00\x00\xff"))
c.Assert(key.TableID(), Equals, int64(0))
require.Equal(t, int64(0), key.TableID())
}
29 changes: 13 additions & 16 deletions pkg/encryption/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,34 @@
package encryption

import (
"testing"
"time"

. "github.com/pingcap/check"
"github.com/stretchr/testify/require"
"github.com/tikv/pd/pkg/typeutil"
)

type testConfigSuite struct{}

var _ = Suite(&testConfigSuite{})

func (s *testConfigSuite) TestAdjustDefaultValue(c *C) {
func TestAdjustDefaultValue(t *testing.T) {
config := &Config{}
err := config.Adjust()
c.Assert(err, IsNil)
c.Assert(config.DataEncryptionMethod, Equals, methodPlaintext)
require.NoError(t, err)
require.Equal(t, methodPlaintext, config.DataEncryptionMethod)
defaultRotationPeriod, _ := time.ParseDuration(defaultDataKeyRotationPeriod)
c.Assert(config.DataKeyRotationPeriod.Duration, Equals, defaultRotationPeriod)
c.Assert(config.MasterKey.Type, Equals, masterKeyTypePlaintext)
require.Equal(t, defaultRotationPeriod, config.DataKeyRotationPeriod.Duration)
require.Equal(t, masterKeyTypePlaintext, config.MasterKey.Type)
}

func (s *testConfigSuite) TestAdjustInvalidDataEncryptionMethod(c *C) {
func TestAdjustInvalidDataEncryptionMethod(t *testing.T) {
config := &Config{DataEncryptionMethod: "unknown"}
c.Assert(config.Adjust(), NotNil)
require.NotNil(t, config.Adjust())
}

func (s *testConfigSuite) TestAdjustNegativeRotationDuration(c *C) {
func TestAdjustNegativeRotationDuration(t *testing.T) {
config := &Config{DataKeyRotationPeriod: typeutil.NewDuration(time.Duration(int64(-1)))}
c.Assert(config.Adjust(), NotNil)
require.NotNil(t, config.Adjust())
}

func (s *testConfigSuite) TestAdjustInvalidMasterKeyType(c *C) {
func TestAdjustInvalidMasterKeyType(t *testing.T) {
config := &Config{MasterKey: MasterKeyConfig{Type: "unknown"}}
c.Assert(config.Adjust(), NotNil)
require.NotNil(t, config.Adjust())
}
104 changes: 48 additions & 56 deletions pkg/encryption/crypter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,96 +19,88 @@ import (
"encoding/hex"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/encryptionpb"
"github.com/stretchr/testify/require"
)

func Test(t *testing.T) {
TestingT(t)
func TestEncryptionMethodSupported(t *testing.T) {
require.NotNil(t, CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_PLAINTEXT))
require.NotNil(t, CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_UNKNOWN))
require.Nil(t, CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_AES128_CTR))
require.Nil(t, CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_AES192_CTR))
require.Nil(t, CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_AES256_CTR))
}

type testCrypterSuite struct{}

var _ = Suite(&testCrypterSuite{})

func (s *testCrypterSuite) TestEncryptionMethodSupported(c *C) {
c.Assert(CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_PLAINTEXT), Not(IsNil))
c.Assert(CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_UNKNOWN), Not(IsNil))
c.Assert(CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_AES128_CTR), IsNil)
c.Assert(CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_AES192_CTR), IsNil)
c.Assert(CheckEncryptionMethodSupported(encryptionpb.EncryptionMethod_AES256_CTR), IsNil)
}

func (s *testCrypterSuite) TestKeyLength(c *C) {
func TestKeyLength(t *testing.T) {
_, err := KeyLength(encryptionpb.EncryptionMethod_PLAINTEXT)
c.Assert(err, Not(IsNil))
require.NotNil(t, err)
_, err = KeyLength(encryptionpb.EncryptionMethod_UNKNOWN)
c.Assert(err, Not(IsNil))
require.NotNil(t, err)
length, err := KeyLength(encryptionpb.EncryptionMethod_AES128_CTR)
c.Assert(err, IsNil)
c.Assert(length, Equals, 16)
require.NoError(t, err)
require.Equal(t, 16, length)
length, err = KeyLength(encryptionpb.EncryptionMethod_AES192_CTR)
c.Assert(err, IsNil)
c.Assert(length, Equals, 24)
require.NoError(t, err)
require.Equal(t, 24, length)
length, err = KeyLength(encryptionpb.EncryptionMethod_AES256_CTR)
c.Assert(err, IsNil)
c.Assert(length, Equals, 32)
require.NoError(t, err)
require.Equal(t, 32, length)
}

func (s *testCrypterSuite) TestNewIv(c *C) {
func TestNewIv(t *testing.T) {
ivCtr, err := NewIvCTR()
c.Assert(err, IsNil)
c.Assert([]byte(ivCtr), HasLen, ivLengthCTR)
require.NoError(t, err)
require.Len(t, []byte(ivCtr), ivLengthCTR)
ivGcm, err := NewIvGCM()
c.Assert(err, IsNil)
c.Assert([]byte(ivGcm), HasLen, ivLengthGCM)
require.NoError(t, err)
require.Len(t, []byte(ivGcm), ivLengthGCM)
}

func testNewDataKey(c *C, method encryptionpb.EncryptionMethod) {
_, key, err := NewDataKey(method, uint64(123))
c.Assert(err, IsNil)
length, err := KeyLength(method)
c.Assert(err, IsNil)
c.Assert(key.Key, HasLen, length)
c.Assert(key.Method, Equals, method)
c.Assert(key.WasExposed, IsFalse)
c.Assert(key.CreationTime, Equals, uint64(123))
func TestNewDataKey(t *testing.T) {
for _, method := range []encryptionpb.EncryptionMethod{
encryptionpb.EncryptionMethod_AES128_CTR,
encryptionpb.EncryptionMethod_AES192_CTR,
encryptionpb.EncryptionMethod_AES256_CTR,
} {
_, key, err := NewDataKey(method, uint64(123))
require.NoError(t, err)
length, err := KeyLength(method)
require.NoError(t, err)
require.Len(t, key.Key, length)
require.Equal(t, method, key.Method)
require.False(t, key.WasExposed)
require.Equal(t, uint64(123), key.CreationTime)
}
}

func (s *testCrypterSuite) TestNewDataKey(c *C) {
testNewDataKey(c, encryptionpb.EncryptionMethod_AES128_CTR)
testNewDataKey(c, encryptionpb.EncryptionMethod_AES192_CTR)
testNewDataKey(c, encryptionpb.EncryptionMethod_AES256_CTR)
}

func (s *testCrypterSuite) TestAesGcmCrypter(c *C) {
func TestAesGcmCrypter(t *testing.T) {
key, err := hex.DecodeString("ed568fbd8c8018ed2d042a4e5d38d6341486922d401d2022fb81e47c900d3f07")
c.Assert(err, IsNil)
require.NoError(t, err)
plaintext, err := hex.DecodeString(
"5c873a18af5e7c7c368cb2635e5a15c7f87282085f4b991e84b78c5967e946d4")
c.Assert(err, IsNil)
require.NoError(t, err)
// encrypt
ivBytes, err := hex.DecodeString("ba432b70336c40c39ba14c1b")
c.Assert(err, IsNil)
require.NoError(t, err)
iv := IvGCM(ivBytes)
ciphertext, err := aesGcmEncryptImpl(key, plaintext, iv)
c.Assert(err, IsNil)
c.Assert([]byte(iv), HasLen, ivLengthGCM)
c.Assert(
hex.EncodeToString(ciphertext),
Equals,
require.NoError(t, err)
require.Len(t, []byte(iv), ivLengthGCM)
require.Equal(
t,
"bbb9b49546350880cf55d4e4eaccc831c506a4aeae7f6cda9c821d4cb8cfc269dcdaecb09592ef25d7a33b40d3f02208",
hex.EncodeToString(ciphertext),
)
// decrypt
plaintext2, err := AesGcmDecrypt(key, ciphertext, iv)
c.Assert(err, IsNil)
c.Assert(bytes.Equal(plaintext2, plaintext), IsTrue)
require.NoError(t, err)
require.True(t, bytes.Equal(plaintext2, plaintext))
// Modify ciphertext to test authentication failure. We modify the beginning of the ciphertext,
// which is the real ciphertext part, not the tag.
fakeCiphertext := make([]byte, len(ciphertext))
copy(fakeCiphertext, ciphertext)
// ignore overflow
fakeCiphertext[0] = ciphertext[0] + 1
_, err = AesGcmDecrypt(key, fakeCiphertext, iv)
c.Assert(err, Not(IsNil))
require.NotNil(t, err)
}
Loading

0 comments on commit 109719f

Please sign in to comment.