Revamped failures for Test_basic() and Test_Basic_Lookup().
Added: -Test_NewI2PAddrFromString() -Test_I2PAddr() -Test_DestHashFromString() -Test_I2PAddrToBytes() with sub-tests.
This commit is contained in:
273
I2PAddr_test.go
273
I2PAddr_test.go
@ -3,8 +3,6 @@ package i2pkeys
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -21,9 +19,7 @@ func Test_Basic(t *testing.T) {
|
|||||||
fmt.Println("\tAttaching to SAM at " + yoursam)
|
fmt.Println("\tAttaching to SAM at " + yoursam)
|
||||||
keys, err := NewDestination()
|
keys, err := NewDestination()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
t.Fatal(err.Error())
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fmt.Println(keys.String())
|
fmt.Println(keys.String())
|
||||||
}
|
}
|
||||||
@ -33,206 +29,127 @@ func Test_Basic_Lookup(t *testing.T) {
|
|||||||
fmt.Println("\tAttaching to SAM at " + yoursam)
|
fmt.Println("\tAttaching to SAM at " + yoursam)
|
||||||
keys, err := Lookup("idk.i2p")
|
keys, err := Lookup("idk.i2p")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err.Error())
|
t.Fatal(err.Error())
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
fmt.Println(keys.String())
|
fmt.Println(keys.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_NewI2PAddrFromString(t *testing.T) {
|
func Test_NewI2PAddrFromString(t *testing.T) {
|
||||||
addr, err := NewI2PAddrFromString(validI2PAddrB64)
|
t.Run("Valid base64 address", func(t *testing.T) {
|
||||||
|
addr, err := NewI2PAddrFromString(validI2PAddrB64)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewI2PAddrFromString failed for valid address: '%v'", err)
|
||||||
|
}
|
||||||
|
if addr.Base64() != validI2PAddrB64 {
|
||||||
|
t.Errorf("NewI2PAddrFromString returned incorrect address. Got '%s', want '%s'", addr.Base64(), validI2PAddrB64)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
t.Run("Invalid address", func(t *testing.T) {
|
||||||
t.Errorf("NewI2PAddrFromString failed for valid address: %v", err)
|
invalidAddr := "not-a-valid-address"
|
||||||
t.Fail()
|
_, err := NewI2PAddrFromString(invalidAddr)
|
||||||
return
|
if err == nil {
|
||||||
}
|
t.Error("NewI2PAddrFromString should have failed for invalid address")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if addr.Base64() != validI2PAddrB64 {
|
t.Run("Base32 address", func(t *testing.T) {
|
||||||
t.Errorf("NewI2PAddrFromString returned incorrect address. Got %s, want %s", addr.Base64(), validI2PAddrB64)
|
_, err := NewI2PAddrFromString(validI2PAddrB32)
|
||||||
t.Fail()
|
if err == nil {
|
||||||
return
|
t.Error("NewI2PAddrFromString should have failed for base32 address")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
|
||||||
invalidAddr := "not-a-valid-address"
|
t.Run("Empty address", func(t *testing.T) {
|
||||||
_, err = NewI2PAddrFromString(invalidAddr)
|
_, err := NewI2PAddrFromString("")
|
||||||
|
if err == nil {
|
||||||
|
t.Error("NewI2PAddrFromString should have failed for empty address")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if err == nil {
|
t.Run("Address with .i2p suffix", func(t *testing.T) { //CHECK
|
||||||
t.Error("NewI2PAddrFromString should have failed for invalid address")
|
addr, err := NewI2PAddrFromString(validI2PAddrB64 + ".i2p")
|
||||||
t.Fail()
|
if err != nil {
|
||||||
return
|
t.Fatalf("NewI2PAddrFromString failed for address with .i2p suffix: '%v'", err)
|
||||||
}
|
}
|
||||||
return
|
if addr.Base64() != validI2PAddrB64 {
|
||||||
|
t.Errorf("NewI2PAddrFromString returned incorrect address. Got '%s', want '%s'", addr.Base64(), validI2PAddrB64)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_I2PAddrBase32(t *testing.T) {
|
func Test_I2PAddr(t *testing.T) {
|
||||||
addr := I2PAddr(validI2PAddrB64)
|
addr := I2PAddr(validI2PAddrB64)
|
||||||
base32 := addr.Base32()
|
base32 := addr.Base32()
|
||||||
|
|
||||||
if !strings.HasSuffix(base32, ".b32.i2p") {
|
t.Run("Base32 suffix", func(t *testing.T) {
|
||||||
t.Errorf("Base32 address should end with .b32.i2p, got %s", base32)
|
if !strings.HasSuffix(base32, ".b32.i2p") {
|
||||||
t.Fail()
|
t.Errorf("Base32 address should end with .b32.i2p, got %s", base32)
|
||||||
return
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
if len(base32) != 60 {
|
t.Run("Base32 length", func(t *testing.T) {
|
||||||
t.Errorf("Base32 address should be 60 characters long, got %d", len(base32))
|
if len(base32) != 60 {
|
||||||
t.Fail()
|
t.Errorf("Base32 address should be 60 characters long, got %d", len(base32))
|
||||||
return
|
}
|
||||||
}
|
})
|
||||||
}
|
|
||||||
|
|
||||||
func Test_LoadKeysIncompat(t *testing.T) {
|
|
||||||
testKeys := "testpub\ntestpriv"
|
|
||||||
r := strings.NewReader(testKeys)
|
|
||||||
|
|
||||||
keys, err := LoadKeysIncompat(r)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("LoadKeysIncompat failed: %v", err)
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if keys.Address != I2PAddr("testpub") {
|
|
||||||
t.Errorf("LoadKeysIncompat returned incorrect public key. Got %s, want %s", keys.Address, "testpub")
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if keys.Both != "testpriv" {
|
|
||||||
t.Errorf("LoadKeysIncompat returned incorrect private key. Got %s, want %s", keys.Both, "testpriv")
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_StoreKeysIncompat(t *testing.T) {
|
|
||||||
keys := I2PKeys{Address: I2PAddr("testpub"), Both: "testpriv"}
|
|
||||||
var buf bytes.Buffer
|
|
||||||
|
|
||||||
err := StoreKeysIncompat(keys, &buf)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("StoreKeysIncompat failed: %v", err)
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := "testpub\ntestpriv"
|
|
||||||
if buf.String() != expected {
|
|
||||||
t.Errorf("StoreKeysIncompat wrote incorrect data. Got %s, want %s", buf.String(), expected)
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_DestHashFromString(t *testing.T) {
|
func Test_DestHashFromString(t *testing.T) {
|
||||||
|
t.Run("Valid hash", func(t *testing.T) {
|
||||||
|
hash, err := DestHashFromString(validI2PAddrB32)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("DestHashFromString failed for valid hash: '%v'", err)
|
||||||
|
}
|
||||||
|
if hash.String() != validI2PAddrB32 {
|
||||||
|
t.Errorf("DestHashFromString returned incorrect hash. Got '%s', want '%s'", hash.String(), validI2PAddrB32)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
hash, err := DestHashFromString(validI2PAddrB32)
|
t.Run("Invalid hash", func(t *testing.T) {
|
||||||
|
invalidHash := "not-a-valid-hash"
|
||||||
|
_, err := DestHashFromString(invalidHash)
|
||||||
|
if err == nil {
|
||||||
|
t.Error("DestHashFromString should have failed for invalid hash")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if err != nil {
|
t.Run("Empty hash", func(t *testing.T) {
|
||||||
t.Errorf("DestHashFromString failed for valid hash: %v", err)
|
_, err := DestHashFromString("")
|
||||||
t.Fail()
|
if err == nil {
|
||||||
return
|
t.Error("DestHashFromString should have failed for empty hash")
|
||||||
}
|
}
|
||||||
|
})
|
||||||
if hash.String() != validI2PAddrB32 {
|
|
||||||
t.Errorf("DestHashFromString returned incorrect hash. Got %s, want %s", hash.String(), validI2PAddrB32)
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
invalidHash := "not-a-valid-hash"
|
|
||||||
_, err = DestHashFromString(invalidHash)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
t.Error("DestHashFromString should have failed for invalid hash")
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Test_I2PAddrToBytes(t *testing.T) {
|
func Test_I2PAddrToBytes(t *testing.T) {
|
||||||
addr := I2PAddr(validI2PAddrB64)
|
addr := I2PAddr(validI2PAddrB64)
|
||||||
|
|
||||||
decodedBytes, err := addr.ToBytes()
|
t.Run("ToBytes and back", func(t *testing.T) {
|
||||||
if err != nil {
|
decodedBytes, err := addr.ToBytes()
|
||||||
t.Fatalf("ToBytes failed: %v", err)
|
if err != nil {
|
||||||
}
|
t.Fatalf("ToBytes failed: '%v'", err)
|
||||||
|
}
|
||||||
|
|
||||||
//encode back to i2p-base64
|
encodedString := i2pB64enc.EncodeToString(decodedBytes)
|
||||||
encodedString := i2pB64enc.EncodeToString(decodedBytes)
|
if encodedString != validI2PAddrB64 {
|
||||||
|
t.Errorf("Round-trip encoding/decoding failed. Got '%s', want '%s'", encodedString, validI2PAddrB64)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if encodedString != validI2PAddrB64 {
|
t.Run("Direct decoding comparison", func(t *testing.T) {
|
||||||
t.Errorf("Round-trip encoding/decoding failed. Got %s, want %s", encodedString, validI2PAddrB64)
|
decodedBytes, err := addr.ToBytes()
|
||||||
}
|
if err != nil {
|
||||||
|
t.Fatalf("ToBytes failed: '%v'", err)
|
||||||
|
}
|
||||||
|
|
||||||
//try decoding directly using i2p-base64
|
directlyDecoded, err := i2pB64enc.DecodeString(validI2PAddrB64)
|
||||||
directlyDecoded, err := i2pB64enc.DecodeString(validI2PAddrB64)
|
if err != nil {
|
||||||
if err != nil {
|
t.Fatalf("Failed to decode test string using i2pB64enc: '%v'", err)
|
||||||
t.Fatalf("Failed to decode test string using i2pB64enc: %v", err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if !bytes.Equal(decodedBytes, directlyDecoded) {
|
if !bytes.Equal(decodedBytes, directlyDecoded) {
|
||||||
t.Errorf("Mismatch between ToBytes result and direct decoding. ToBytes len: %d, Direct decoding len: %d", len(decodedBytes), len(directlyDecoded))
|
t.Errorf("Mismatch between ToBytes result and direct decoding. ToBytes len: '%d', Direct decoding len: '%d'", len(decodedBytes), len(directlyDecoded))
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
func TestKeyGenerationAndHandling(t *testing.T) {
|
|
||||||
// Generate new keys
|
|
||||||
keys, err := NewDestination()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to generate new I2P keys: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test LoadKeysIncompat
|
|
||||||
r := strings.NewReader(keys.Address.Base64() + "\n" + keys.Both)
|
|
||||||
loadedKeys, err := LoadKeysIncompat(r)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("LoadKeysIncompat failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if loadedKeys.Address != keys.Address {
|
|
||||||
t.Errorf("LoadKeysIncompat returned incorrect public key. Got %s, want %s", loadedKeys.Address, keys.Address)
|
|
||||||
}
|
|
||||||
|
|
||||||
if loadedKeys.Both != keys.Both {
|
|
||||||
t.Errorf("LoadKeysIncompat returned incorrect private key. Got %s, want %s", loadedKeys.Both, keys.Both)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test StoreKeysIncompat
|
|
||||||
var buf bytes.Buffer
|
|
||||||
err = StoreKeysIncompat(*keys, &buf)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("StoreKeysIncompat failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
expected := keys.Address.Base64() + "\n" + keys.Both
|
|
||||||
if buf.String() != expected {
|
|
||||||
t.Errorf("StoreKeysIncompat wrote incorrect data. Got %s, want %s", buf.String(), expected)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test StoreKeys
|
|
||||||
tmpfile, err := ioutil.TempFile("", "test_keys_*.txt")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to create temp file: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Remove(tmpfile.Name()) // clean up
|
|
||||||
|
|
||||||
err = StoreKeys(*keys, tmpfile.Name())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("StoreKeys failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the file contents
|
|
||||||
content, err := ioutil.ReadFile(tmpfile.Name())
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to read temp file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if string(content) != expected {
|
|
||||||
t.Errorf("StoreKeys wrote incorrect data. Got %s, want %s", string(content), expected)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user