From 87e936195ebaa1d11475d6fb3a73958c654be977 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Thu, 4 Nov 2021 13:05:53 +0500 Subject: [PATCH] Add some tests (#828) * Add tests * Add tests * Add tests * Add tests * Fix code style * Remove unnecessary tests --- src/address/address_test.go | 114 ++++++++++++++++++++++++++++++++++++ src/config/config_test.go | 54 +++++++++++++++++ 2 files changed, 168 insertions(+) create mode 100644 src/address/address_test.go create mode 100644 src/config/config_test.go diff --git a/src/address/address_test.go b/src/address/address_test.go new file mode 100644 index 00000000..a7939e0f --- /dev/null +++ b/src/address/address_test.go @@ -0,0 +1,114 @@ +package address + +import ( + "bytes" + "crypto/ed25519" + "math/rand" + "testing" +) + +func TestAddress_Address_IsValid(t *testing.T) { + var address Address + rand.Read(address[:]) + + address[0] = 0 + + if address.IsValid() { + t.Fatal("invalid address marked as valid") + } + + address[0] = 0x03 + + if address.IsValid() { + t.Fatal("invalid address marked as valid") + } + + address[0] = 0x02 + + if !address.IsValid() { + t.Fatal("valid address marked as invalid") + } +} + +func TestAddress_Subnet_IsValid(t *testing.T) { + var subnet Subnet + rand.Read(subnet[:]) + + subnet[0] = 0 + + if subnet.IsValid() { + t.Fatal("invalid subnet marked as valid") + } + + subnet[0] = 0x02 + + if subnet.IsValid() { + t.Fatal("invalid subnet marked as valid") + } + + subnet[0] = 0x03 + + if !subnet.IsValid() { + t.Fatal("valid subnet marked as invalid") + } +} + +func TestAddress_AddrForKey(t *testing.T) { + publicKey := ed25519.PublicKey{ + 189, 186, 207, 216, 34, 64, 222, 61, 205, 18, 57, 36, 203, 181, 82, 86, + 251, 141, 171, 8, 170, 152, 227, 5, 82, 138, 184, 79, 65, 158, 110, 251, + } + + expectedAddress := Address{ + 2, 0, 132, 138, 96, 79, 187, 126, 67, 132, 101, 219, 141, 182, 104, 149, + } + + if *AddrForKey(publicKey) != expectedAddress { + t.Fatal("invalid address returned") + } +} + +func TestAddress_SubnetForKey(t *testing.T) { + publicKey := ed25519.PublicKey{ + 189, 186, 207, 216, 34, 64, 222, 61, 205, 18, 57, 36, 203, 181, 82, 86, + 251, 141, 171, 8, 170, 152, 227, 5, 82, 138, 184, 79, 65, 158, 110, 251, + } + + expectedSubnet := Subnet{3, 0, 132, 138, 96, 79, 187, 126} + + if *SubnetForKey(publicKey) != expectedSubnet { + t.Fatal("invalid subnet returned") + } +} + +func TestAddress_Address_GetKey(t *testing.T) { + address := Address{ + 2, 0, 132, 138, 96, 79, 187, 126, 67, 132, 101, 219, 141, 182, 104, 149, + } + + expectedPublicKey := ed25519.PublicKey{ + 189, 186, 207, 216, 34, 64, 222, 61, + 205, 18, 57, 36, 203, 181, 127, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + } + + if !bytes.Equal(address.GetKey(), expectedPublicKey) { + t.Fatal("invalid public key returned") + } +} + +func TestAddress_Subnet_GetKey(t *testing.T) { + subnet := Subnet{3, 0, 132, 138, 96, 79, 187, 126} + + expectedPublicKey := ed25519.PublicKey{ + 189, 186, 207, 216, 34, 64, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + 255, 255, 255, 255, 255, 255, 255, 255, + } + + if !bytes.Equal(subnet.GetKey(), expectedPublicKey) { + t.Fatal("invalid public key returned") + } +} diff --git a/src/config/config_test.go b/src/config/config_test.go new file mode 100644 index 00000000..8b6e14e1 --- /dev/null +++ b/src/config/config_test.go @@ -0,0 +1,54 @@ +package config + +import ( + "bytes" + "encoding/hex" + "testing" +) + +func TestConfig_Keys(t *testing.T) { + var nodeConfig NodeConfig + nodeConfig.NewKeys() + + publicKey1, err := hex.DecodeString(nodeConfig.PublicKey) + + if err != nil { + t.Fatal("can not decode generated public key") + } + + if len(publicKey1) == 0 { + t.Fatal("empty public key generated") + } + + privateKey1, err := hex.DecodeString(nodeConfig.PrivateKey) + + if err != nil { + t.Fatal("can not decode generated private key") + } + + if len(privateKey1) == 0 { + t.Fatal("empty private key generated") + } + + nodeConfig.NewKeys() + + publicKey2, err := hex.DecodeString(nodeConfig.PublicKey) + + if err != nil { + t.Fatal("can not decode generated public key") + } + + if bytes.Equal(publicKey2, publicKey1) { + t.Fatal("same public key generated") + } + + privateKey2, err := hex.DecodeString(nodeConfig.PrivateKey) + + if err != nil { + t.Fatal("can not decode generated private key") + } + + if bytes.Equal(privateKey2, privateKey1) { + t.Fatal("same private key generated") + } +}