From 0ce654968d0d45f1d01aab034789950529139afc Mon Sep 17 00:00:00 2001 From: kevin Date: Wed, 30 Sep 2020 15:36:13 +0800 Subject: [PATCH] add more tests --- core/codec/dh_test.go | 6 +++++ core/codec/hmac_test.go | 19 +++++++++++++ core/codec/rsa_test.go | 59 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 core/codec/hmac_test.go create mode 100644 core/codec/rsa_test.go diff --git a/core/codec/dh_test.go b/core/codec/dh_test.go index 56f37412..1bb77f8d 100644 --- a/core/codec/dh_test.go +++ b/core/codec/dh_test.go @@ -71,3 +71,9 @@ func TestDiffieHellmanMiddleManAttack(t *testing.T) { assert.Nil(t, err) assert.Equal(t, string(src), string(decryptedSrc)) } + +func TestKeyBytes(t *testing.T) { + key, err := GenerateKey() + assert.Nil(t, err) + assert.True(t, len(key.Bytes()) > 0) +} diff --git a/core/codec/hmac_test.go b/core/codec/hmac_test.go new file mode 100644 index 00000000..62cbfee2 --- /dev/null +++ b/core/codec/hmac_test.go @@ -0,0 +1,19 @@ +package codec + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestHmac(t *testing.T) { + ret := Hmac([]byte("foo"), "bar") + assert.Equal(t, "f9320baf0249169e73850cd6156ded0106e2bb6ad8cab01b7bbbebe6d1065317", + fmt.Sprintf("%x", ret)) +} + +func TestHmacBase64(t *testing.T) { + ret := HmacBase64([]byte("foo"), "bar") + assert.Equal(t, "+TILrwJJFp5zhQzWFW3tAQbiu2rYyrAbe7vr5tEGUxc=", ret) +} diff --git a/core/codec/rsa_test.go b/core/codec/rsa_test.go new file mode 100644 index 00000000..dbe9fd1d --- /dev/null +++ b/core/codec/rsa_test.go @@ -0,0 +1,59 @@ +package codec + +import ( + "encoding/base64" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/tal-tech/go-zero/core/fs" +) + +const ( + priKey = `-----BEGIN RSA PRIVATE KEY----- +MIICXQIBAAKBgQC4TJk3onpqb2RYE3wwt23J9SHLFstHGSkUYFLe+nl1dEKHbD+/ +Zt95L757J3xGTrwoTc7KCTxbrgn+stn0w52BNjj/kIE2ko4lbh/v8Fl14AyVR9ms +fKtKOnhe5FCT72mdtApr+qvzcC3q9hfXwkyQU32pv7q5UimZ205iKSBmgQIDAQAB +AoGAM5mWqGIAXj5z3MkP01/4CDxuyrrGDVD5FHBno3CDgyQa4Gmpa4B0/ywj671B +aTnwKmSmiiCN2qleuQYASixes2zY5fgTzt+7KNkl9JHsy7i606eH2eCKzsUa/s6u +WD8V3w/hGCQ9zYI18ihwyXlGHIgcRz/eeRh+nWcWVJzGOPUCQQD5nr6It/1yHb1p +C6l4fC4xXF19l4KxJjGu1xv/sOpSx0pOqBDEX3Mh//FU954392rUWDXV1/I65BPt +TLphdsu3AkEAvQJ2Qay/lffFj9FaUrvXuftJZ/Ypn0FpaSiUh3Ak3obBT6UvSZS0 +bcYdCJCNHDtBOsWHnIN1x+BcWAPrdU7PhwJBAIQ0dUlH2S3VXnoCOTGc44I1Hzbj +Rc65IdsuBqA3fQN2lX5vOOIog3vgaFrOArg1jBkG1wx5IMvb/EnUN2pjVqUCQCza +KLXtCInOAlPemlCHwumfeAvznmzsWNdbieOZ+SXVVIpR6KbNYwOpv7oIk3Pfm9sW +hNffWlPUKhW42Gc+DIECQQDmk20YgBXwXWRM5DRPbhisIV088N5Z58K9DtFWkZsd +OBDT3dFcgZONtlmR1MqZO0pTh30lA4qovYj3Bx7A8i36 +-----END RSA PRIVATE KEY-----` + pubKey = `-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4TJk3onpqb2RYE3wwt23J9SHL +FstHGSkUYFLe+nl1dEKHbD+/Zt95L757J3xGTrwoTc7KCTxbrgn+stn0w52BNjj/ +kIE2ko4lbh/v8Fl14AyVR9msfKtKOnhe5FCT72mdtApr+qvzcC3q9hfXwkyQU32p +v7q5UimZ205iKSBmgQIDAQAB +-----END PUBLIC KEY-----` + testBody = `this is the content` + encryptedBody = `49e7bc15640e5d927fd3f129b749536d0755baf03a0f35fc914ff1b7b8ce659e5fe3a598442eb908c5995e28bacd3d76e4420bb05b6bfc177040f66c6976f680f7123505d626ab96a9db1151f45c93bc0262db9087b9fb6801715f76f902e644a20029262858f05b0d10540842204346ac1d6d8f29cc5d47dab79af75d922ef2` +) + +func TestCryption(t *testing.T) { + enc, err := NewRsaEncrypter([]byte(pubKey)) + assert.Nil(t, err) + ret, err := enc.Encrypt([]byte(testBody)) + assert.Nil(t, err) + + file, err := fs.TempFilenameWithText(priKey) + assert.Nil(t, err) + dec, err := NewRsaDecrypter(file) + assert.Nil(t, err) + actual, err := dec.Decrypt(ret) + assert.Nil(t, err) + assert.Equal(t, testBody, string(actual)) + + actual, err = dec.DecryptBase64(base64.StdEncoding.EncodeToString(ret)) + assert.Nil(t, err) + assert.Equal(t, testBody, string(actual)) +} + +func TestBadPubKey(t *testing.T) { + _, err := NewRsaEncrypter([]byte("foo")) + assert.Equal(t, ErrPublicKey, err) +}