mirror of
https://github.com/emmansun/gmsm.git
synced 2025-05-12 03:56:17 +08:00
supplement unit test cases
This commit is contained in:
parent
be29c32fe5
commit
24765d0e35
@ -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
60
sm4/cbc_cipher_test.go
Normal 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])
|
||||
})
|
||||
}
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user