Skip to content

Commit

Permalink
完善单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed May 22, 2024
1 parent 675949b commit cab8408
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 4 deletions.
2 changes: 0 additions & 2 deletions aes/aes.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func DecryptCTR(key cryptox.Bytes, iv cryptox.Bytes, padding cryptox.Padding, bs
}

// EncryptGCM uses gcm mode to encrypt bs.
// NOTICE: This is an experimental function, and we haven't tested it enough yet, so be careful when using it.
func EncryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes, bs cryptox.Bytes) (cryptox.Bytes, error) {
block, _, err := newBlock(key)
if err != nil {
Expand All @@ -199,7 +198,6 @@ func EncryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes
}

// DecryptGCM uses gcm mode to decrypt bs.
// NOTICE: This is an experimental function, and we haven't tested it enough yet, so be careful when using it.
func DecryptGCM(key cryptox.Bytes, nonce cryptox.Bytes, additional cryptox.Bytes, bs cryptox.Bytes) (cryptox.Bytes, error) {
block, _, err := newBlock(key)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ func TestParseHex(t *testing.T) {
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestFromBase64$
func TestFromBase64(t *testing.T) {
// go test -v -cover -count=1 -test.cpu=1 -run=^TestParseBase64$
func TestParseBase64(t *testing.T) {
cases := map[string]string{
"": "",
"MTIz": "123",
Expand Down
83 changes: 83 additions & 0 deletions rsa/private_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,89 @@ func TestPrivateKey(t *testing.T) {
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptPKCS1v15$
func TestPrivateKeyDecryptPKCS1v15(t *testing.T) {
publicKey := newTestPublicKey(t)
privateKey := newTestPrivateKey(t)

cases := []string{
"", "123", "你好,世界",
}

for _, msg := range cases {
encrypted, err := publicKey.EncryptPKCS1v15(cryptox.Bytes(msg))
if err != nil {
t.Fatal(err)
}

decrypted, err := privateKey.DecryptPKCS1v15(encrypted)
if err != nil {
t.Fatal(err)
}

if string(decrypted) != msg {
t.Fatalf("decrypted %s != msg %s", decrypted, msg)
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptPKCS1v15SessionKey$
func TestPrivateKeyDecryptPKCS1v15SessionKey(t *testing.T) {
publicKey := newTestPublicKey(t)
privateKey := newTestPrivateKey(t)

cases := []string{
"", "123", "你好,世界",
}

for _, msg := range cases {
encrypted, err := publicKey.EncryptPKCS1v15(cryptox.Bytes(msg))
if err != nil {
t.Fatal(err)
}

sessionKey := cryptox.GenerateBytes(32)
if err = privateKey.DecryptPKCS1v15SessionKey(encrypted, sessionKey); err != nil {
t.Fatal(err)
}

decrypted, err := privateKey.DecryptPKCS1v15(encrypted)
if err != nil {
t.Fatal(err)
}

if string(decrypted) != msg {
t.Fatalf("decrypted %s != msg %s", decrypted, msg)
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeyDecryptOAEP$
func TestPrivateKeyDecryptOAEP(t *testing.T) {
publicKey := newTestPublicKey(t)
privateKey := newTestPrivateKey(t)

cases := []string{
"", "123", "你好,世界",
}

for _, msg := range cases {
encrypted, err := publicKey.EncryptOAEP(cryptox.Bytes(msg), cryptox.Bytes(msg))
if err != nil {
t.Fatal(err)
}

decrypted, err := privateKey.DecryptOAEP(encrypted, cryptox.Bytes(msg))
if err != nil {
t.Fatal(err)
}

if string(decrypted) != msg {
t.Fatalf("decrypted %s != msg %s", decrypted, msg)
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestPrivateKeySignPKCS1v15$
func TestPrivateKeySignPKCS1v15(t *testing.T) {
publicKey := newTestPublicKey(t)
Expand Down
44 changes: 44 additions & 0 deletions rsa/public_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,47 @@ func TestPublicKeyEncryptOAEP(t *testing.T) {
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestPublicKeyVerifyPKCS1v15$
func TestPublicKeyVerifyPKCS1v15(t *testing.T) {
publicKey := newTestPublicKey(t)
privateKey := newTestPrivateKey(t)

cases := []string{
"d41d8cd98f00b204e9800998ecf8427e", "202cb962ac59075b964b07152d234b70", "dbefd3ada018615b35588a01e216ae6e",
}

for _, msg := range cases {
signature, err := privateKey.SignPKCS1v15(cryptox.Bytes(msg))
if err != nil {
t.Fatal(err)
}

err = publicKey.VerifyPKCS1v15(cryptox.Bytes(msg), signature)
if err != nil {
t.Fatal(err)
}
}
}

// go test -v -cover -count=1 -test.cpu=1 -run=^TestPublicKeyVerifyPSS$
func TestPublicKeyVerifyPSS(t *testing.T) {
publicKey := newTestPublicKey(t)
privateKey := newTestPrivateKey(t)

cases := []string{
"d41d8cd98f00b204e9800998ecf8427e", "202cb962ac59075b964b07152d234b70", "dbefd3ada018615b35588a01e216ae6e",
}

for _, msg := range cases {
signature, err := privateKey.SignPSS(cryptox.Bytes(msg), 0)
if err != nil {
t.Fatal(err)
}

err = publicKey.VerifyPSS(cryptox.Bytes(msg), signature, 0)
if err != nil {
t.Fatal(err)
}
}
}

0 comments on commit cab8408

Please sign in to comment.