github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/key_test.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package p2p 19 20 import ( 21 "testing" 22 23 "github.com/stretchr/testify/assert" 24 ) 25 26 func TestGenerateKey(t *testing.T) { 27 for i := 1; i < 20; i++ { 28 pub1, priv1, err := GenerateKey() 29 assert.NoError(t, err) 30 pub2, priv2, err := GenerateKey() 31 assert.NoError(t, err) 32 assert.NotEqual(t, pub1, pub2) 33 assert.NotEqual(t, priv1, priv2) 34 } 35 } 36 37 func TestDecodePublicKey(t *testing.T) { 38 expectedKey := PublicKey{0x4, 0x95, 0xb, 0x1a, 0xba, 0x3f, 0xff, 0xaa, 0xff, 0x5b, 0x81, 0x76, 0xe2, 0x55, 0xb4, 0x37, 0xc3, 0xba, 0xcf, 0x8e, 0xad, 0xc4, 0x70, 0x60, 0xe, 0xa5, 0xfd, 0xe6, 0x25, 0x2a, 0x23, 0x33} 39 publicKey, err := DecodePublicKey("04950b1aba3fffaaff5b8176e255b437c3bacf8eadc470600ea5fde6252a2333") 40 assert.NoError(t, err) 41 assert.Equal(t, expectedKey, publicKey) 42 } 43 44 func TestEncryptDecrypt(t *testing.T) { 45 pub1, priv1, _ := GenerateKey() 46 pub2, priv2, _ := GenerateKey() 47 48 msg := "Please hide me in ciphertext" 49 ciphertext, err := priv1.Encrypt(pub2, []byte(msg)) 50 assert.NoError(t, err) 51 52 plaintext, err := priv2.Decrypt(pub1, ciphertext) 53 assert.NoError(t, err) 54 assert.Equal(t, msg, string(plaintext)) 55 }