supplement unit test cases

This commit is contained in:
Sun Yimin 2022-07-18 10:13:53 +08:00 committed by GitHub
parent be29c32fe5
commit 24765d0e35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 2 deletions

View File

@ -559,7 +559,7 @@ func TestMarshalPrivateKey(t *testing.T) {
t.Fatalf("%d: MarshalPrivateKey returned: %s", i, err)
}
decodedSM2PrivateKey, _, err := pkcs8.ParsePrivateKey(der, tt.password)
decodedSM2PrivateKey, err := pkcs8.ParsePKCS8PrivateKeySM2(der, tt.password)
if err != nil {
t.Fatalf("%d: ParsePKCS8PrivateKey returned: %s", i, err)
}

60
sm4/cbc_cipher_test.go Normal file
View File

@ -0,0 +1,60 @@
//go:build (amd64 && !generic) || (arm64 && !generic)
// +build amd64,!generic arm64,!generic
package sm4
import (
"crypto/cipher"
"testing"
)
// cbcMode is an interface for block ciphers using cipher block chaining.
type cbcMode interface {
cipher.BlockMode
SetIV([]byte)
}
func TestSetIV(t *testing.T) {
key := make([]byte, 16)
iv := make([]byte, 16)
c, err := NewCipher(key)
if err != nil {
t.Fatal(err)
}
decrypter := cipher.NewCBCDecrypter(c, iv)
cbc, ok := decrypter.(cbcMode)
if !ok {
t.Fatalf("it's not cbc")
}
shouldPanic(t, func() {
cbc.SetIV(iv[1:])
})
cbc.SetIV(iv[:])
}
func TestCryptBlocks(t *testing.T) {
key := make([]byte, 16)
iv := make([]byte, 16)
src := make([]byte, 32)
c, err := NewCipher(key)
if err != nil {
t.Fatal(err)
}
decrypter := cipher.NewCBCDecrypter(c, iv)
// test len(src) == 0
decrypter.CryptBlocks(nil, nil)
// cipher: input not full blocks
shouldPanic(t, func() {
decrypter.CryptBlocks(src, src[1:])
})
// cipher: output smaller than input
shouldPanic(t, func() {
decrypter.CryptBlocks(src[1:], src)
})
// cipher: invalid buffer overlap
shouldPanic(t, func() {
decrypter.CryptBlocks(src[1:17], src[2:18])
})
}

View File

@ -78,7 +78,7 @@ func TestEncryptMasterPrivateKeyMarshalASN1(t *testing.T) {
if err != nil {
t.Fatal(err)
}
masterKey2 := new(SignMasterPrivateKey)
masterKey2 := new(EncryptMasterPrivateKey)
err = masterKey2.UnmarshalASN1(der)
if err != nil {
t.Fatal(err)