Added broken tests for keys (TODO)

This commit is contained in:
Haris Khan
2024-09-15 00:46:32 -04:00
parent dc8ef52d46
commit acbf68bc58

View File

@ -3,6 +3,8 @@ package i2pkeys
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"strings"
"testing"
)
@ -153,3 +155,115 @@ func Test_I2PAddrToBytes(t *testing.T) {
}
})
}
func Test_KeyGenerationAndHandling(t *testing.T) {
// Generate new keys
keys, err := NewDestination()
if err != nil {
t.Fatalf("Failed to generate new I2P keys: %v", err)
}
parts := strings.Split(keys.String(), " ")
var pubString, privString string
if len(parts) == 2 {
pubString = parts[0]
privString = parts[1]
} else {
t.Fatalf("keys.String() does not contain two keys separated by a space.")
}
fmt.Printf("pubString: %s\n", pubString)
fmt.Printf("privString: %s\n", privString)
// Test LoadKeysIncompat
r := strings.NewReader(pubString + "\n" + privString)
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)
}
}
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
}
}