Commit f7fdb365 authored by suyanlong's avatar suyanlong

🐛 Fixed unit test

parent e86d2fae
Pipeline #8154 failed with stages
......@@ -34,31 +34,31 @@ func (m *MockCryptor) EXPECT() *MockCryptorMockRecorder {
}
// Decrypt mocks base method.
func (m *MockCryptor) Decrypt(content []byte, address string) ([]byte, error) {
func (m *MockCryptor) Decrypt(content []byte, address, mode string) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Decrypt", content, address)
ret := m.ctrl.Call(m, "Decrypt", content, address, mode)
ret0, _ := ret[0].([]byte)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Decrypt indicates an expected call of Decrypt.
func (mr *MockCryptorMockRecorder) Decrypt(content, address interface{}) *gomock.Call {
func (mr *MockCryptorMockRecorder) Decrypt(content, address, mode interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Decrypt", reflect.TypeOf((*MockCryptor)(nil).Decrypt), content, address)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Decrypt", reflect.TypeOf((*MockCryptor)(nil).Decrypt), content, address, mode)
}
// Encrypt mocks base method.
func (m *MockCryptor) Encrypt(content []byte, address string) ([]byte, error) {
func (m *MockCryptor) Encrypt(content []byte, address, mode string) ([]byte, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Encrypt", content, address)
ret := m.ctrl.Call(m, "Encrypt", content, address, mode)
ret0, _ := ret[0].([]byte)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// Encrypt indicates an expected call of Encrypt.
func (mr *MockCryptorMockRecorder) Encrypt(content, address interface{}) *gomock.Call {
func (mr *MockCryptorMockRecorder) Encrypt(content, address, mode interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Encrypt", reflect.TypeOf((*MockCryptor)(nil).Encrypt), content, address)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Encrypt", reflect.TypeOf((*MockCryptor)(nil).Encrypt), content, address, mode)
}
......@@ -12,6 +12,7 @@ import (
rpcx "github.com/link33/sidecar/hub/client"
"github.com/link33/sidecar/hub/client/mock_client"
"github.com/link33/sidecar/internal/repo"
"github.com/link33/sidecar/model/pb"
)
......@@ -33,8 +34,7 @@ func TestRelayCryptor(t *testing.T) {
Ret: pubBytes2,
}
rc1, err := NewRelayCryptor(mockClient, privKey1)
require.Nil(t, err)
rc1 := NewCryptor(mockClient, nil, privKey1)
address1, err := privKey1.PublicKey().Address()
require.Nil(t, err)
......@@ -43,7 +43,7 @@ func TestRelayCryptor(t *testing.T) {
addr1 := address1.String()
keyMap2 := make(map[string][]byte)
keyMap2[addr1] = pubBytes1
rc2 := &RelayCryptor{
rc2 := &cryptor{
privKey: privKey2,
keyMap: keyMap2,
}
......@@ -53,21 +53,21 @@ func TestRelayCryptor(t *testing.T) {
mockClient.EXPECT().InvokeBVMContract(gomock.Any(), gomock.Any(), gomock.Any(), rpcx.String("")).Return(nil, fmt.Errorf("invoke bvm contract error")).AnyTimes()
content := []byte("bitxhub cryptor test")
encryptBytes, err := rc1.Encrypt(content, addr2)
encryptBytes, err := rc1.Encrypt(content, addr2, repo.RelayMode)
require.Nil(t, err)
decryptBytes, err := rc2.Decrypt(encryptBytes, addr1)
decryptBytes, err := rc2.Decrypt(encryptBytes, addr1, repo.RelayMode)
require.Nil(t, err)
require.Equal(t, decryptBytes, content)
fmt.Println(string(decryptBytes))
// encrypt with invoke bvm contract error
_, err = rc1.Encrypt(content, "")
_, err = rc1.Encrypt(content, "", repo.RelayMode)
require.NotNil(t, err)
// decrypt with wrong pubkey
rc2.keyMap[addr1] = []byte("")
_, err = rc2.Decrypt(encryptBytes, addr1)
_, err = rc2.Decrypt(encryptBytes, addr1, repo.RelayMode)
require.NotNil(t, err)
}
......@@ -86,8 +86,7 @@ func TestDirectCryptor(t *testing.T) {
require.Nil(t, err)
addr2 := address2.String()
rc1, err := NewDirectCryptor(mockAppchainMgr, privKey1)
require.Nil(t, err)
rc1 := NewCryptor(nil, mockAppchainMgr, privKey1)
address1, err := privKey1.PublicKey().Address()
require.Nil(t, err)
......@@ -96,7 +95,7 @@ func TestDirectCryptor(t *testing.T) {
addr1 := address1.String()
keyMap2 := make(map[string][]byte)
keyMap2[addr1] = pubBytes1
rc2 := &DirectCryptor{
rc2 := &cryptor{
privKey: privKey2,
keyMap: keyMap2,
}
......@@ -106,20 +105,20 @@ func TestDirectCryptor(t *testing.T) {
mockAppchainMgr.EXPECT().GetPubKeyByChainID("").Return(false, nil).AnyTimes()
content := []byte("bitxhub cryptor test")
encryptBytes, err := rc1.Encrypt(content, addr2)
encryptBytes, err := rc1.Encrypt(content, addr2, repo.DirectMode)
require.Nil(t, err)
decryptBytes, err := rc2.Decrypt(encryptBytes, addr1)
decryptBytes, err := rc2.Decrypt(encryptBytes, addr1, repo.DirectMode)
require.Nil(t, err)
require.Equal(t, decryptBytes, content)
fmt.Println(string(decryptBytes))
// encrypt with nil address
_, err = rc1.Encrypt(content, "")
_, err = rc1.Encrypt(content, "", repo.DirectMode)
require.NotNil(t, err)
// decrypt with wrong pubkey
rc2.keyMap[addr1] = []byte("")
_, err = rc2.Decrypt(encryptBytes, addr1)
_, err = rc2.Decrypt(encryptBytes, addr1, repo.DirectMode)
require.NotNil(t, err)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment