Completed SendPurchase test

This commit is contained in:
David Vennik
2022-12-27 17:54:58 +00:00
parent 89166dc86c
commit 0b7ccee0b8
3 changed files with 147 additions and 10 deletions

View File

@@ -85,8 +85,8 @@ func SendKeys(id nonce.ID, hdr, pld *pub.Key,
// their section at the top, moves the next layer header to the top and pads the
// remainder with noise, so it always looks like the first hop,
// indistinguishable.
func SendPurchase(nBytes uint64, client node.Node,
hop [5]node.Node, set signer.KeySet) types.Onion {
func SendPurchase(nBytes uint64, client *node.Node,
hop [5]*node.Node, set *signer.KeySet) types.Onion {
var replies [3]*prv.Key
for i := range replies {
@@ -101,6 +101,7 @@ func SendPurchase(nBytes uint64, client node.Node,
pubs[1] = hop[4].PayloadKey
pubs[2] = hop[3].PayloadKey
return OnionSkins{}.
Forward(hop[0].AddrPort).
OnionSkin(address.FromPubKey(hop[0].HeaderKey), set.Next()).
Forward(hop[1].AddrPort).
OnionSkin(address.FromPubKey(hop[1].HeaderKey), set.Next()).

View File

@@ -1,6 +1,7 @@
package wire
import (
"math/rand"
"reflect"
"testing"
@@ -14,6 +15,8 @@ import (
"github.com/Indra-Labs/indra/pkg/wire/confirmation"
"github.com/Indra-Labs/indra/pkg/wire/forward"
"github.com/Indra-Labs/indra/pkg/wire/layer"
"github.com/Indra-Labs/indra/pkg/wire/purchase"
"github.com/Indra-Labs/indra/pkg/wire/reply"
log2 "github.com/cybriq/proc/pkg/log"
)
@@ -68,6 +71,40 @@ func PeelConfirmation(t *testing.T, b slice.Bytes,
return
}
func PeelPurchase(t *testing.T, b slice.Bytes,
c *slice.Cursor) (pr *purchase.OnionSkin) {
var ok bool
var e error
var on types.Onion
if on, e = PeelOnion(b, c); check(e) {
t.Error(e)
t.FailNow()
}
if pr, ok = on.(*purchase.OnionSkin); !ok {
t.Error("did not unwrap expected type", reflect.TypeOf(on))
t.FailNow()
}
return
}
func PeelReply(t *testing.T, b slice.Bytes,
c *slice.Cursor) (rp *reply.OnionSkin) {
var ok bool
var e error
var on types.Onion
if on, e = PeelOnion(b, c); check(e) {
t.Error(e)
t.FailNow()
}
if rp, ok = on.(*reply.OnionSkin); !ok {
t.Error("did not unwrap expected type", reflect.TypeOf(on))
t.FailNow()
}
return
}
func TestPing(t *testing.T) {
log2.CodeLoc = true
_, ks, e := signer.New()
@@ -98,7 +135,7 @@ func TestPing(t *testing.T) {
// Forward(hop[0].AddrPort).
f0 := PeelForward(t, b, c)
if hop[0].AddrPort.String() != f0.AddrPort.String() {
t.Errorf("failed to unwrap expected: '%s', got '%s'",
t.Errorf("failed to unwrap; expected: '%s', got: '%s'",
hop[0].AddrPort.String(), f0.AddrPort.String())
t.FailNow()
}
@@ -109,7 +146,7 @@ func TestPing(t *testing.T) {
// Forward(hop[1].AddrPort).
f1 := PeelForward(t, b, c)
if hop[1].AddrPort.String() != f1.AddrPort.String() {
t.Errorf("failed to unwrap expected: '%s', got '%s'",
t.Errorf("failed to unwrap; expected: '%s', got: '%s'",
hop[1].AddrPort.String(), f1.AddrPort.String())
t.FailNow()
}
@@ -120,7 +157,7 @@ func TestPing(t *testing.T) {
// Forward(hop[2].AddrPort).
f2 := PeelForward(t, b, c)
if hop[2].AddrPort.String() != f2.AddrPort.String() {
t.Errorf("failed to unwrap expected: '%s', got '%s'",
t.Errorf("failed to unwrap; expected: '%s', got: '%s'",
hop[2].AddrPort.String(), f2.AddrPort.String())
t.FailNow()
}
@@ -131,7 +168,7 @@ func TestPing(t *testing.T) {
// Forward(client.AddrPort).
f3 := PeelForward(t, b, c)
if client.AddrPort.String() != f3.AddrPort.String() {
t.Errorf("failed to unwrap expected: '%s', got '%s'",
t.Errorf("failed to unwrap; expected: '%s', got: '%s'",
client.AddrPort.String(), f3.AddrPort.String())
t.FailNow()
}
@@ -271,3 +308,102 @@ func TestSendKeys(t *testing.T) {
}
}
func TestSendPurchase(t *testing.T) {
log2.CodeLoc = true
_, ks, e := signer.New()
if check(e) {
t.Error(e)
t.FailNow()
}
var hop [5]*node.Node
for i := range hop {
prv1, prv2 := GetTwoPrvKeys(t)
pub1, pub2 := pub.Derive(prv1), pub.Derive(prv2)
hop[i], _ = node.New(slice.GenerateRandomAddrPortIPv4(),
pub1, pub2, prv1, prv2, nil)
}
cprv1, cprv2 := GetTwoPrvKeys(t)
cpub1, cpub2 := pub.Derive(cprv1), pub.Derive(cprv2)
var client *node.Node
client, _ = node.New(slice.GenerateRandomAddrPortIPv4(),
cpub1, cpub2, cprv1, cprv2, nil)
// ciprv1, ciprv2 := GetTwoPrvKeys(t)
// cipub1, cipub2 := pub.Derive(ciprv1), pub.Derive(ciprv2)
nBytes := rand.Uint64()
on := SendPurchase(nBytes, client, hop, ks)
b := EncodeOnion(on)
c := slice.NewCursor()
// var ok bool
// Forward(hop[0].AddrPort).
f0 := PeelForward(t, b, c)
if hop[0].AddrPort.String() != f0.AddrPort.String() {
t.Errorf("failed to unwrap expected: '%s', got '%s'",
hop[0].AddrPort.String(), f0.AddrPort.String())
t.FailNow()
}
// OnionSkin(address.FromPubKey(hop[0].HeaderKey), set.Next()).
PeelOnionSkin(t, b, c).Decrypt(hop[0].HeaderPriv, b, c)
// Forward(hop[1].AddrPort).
f1 := PeelForward(t, b, c)
if hop[1].AddrPort.String() != f1.AddrPort.String() {
t.Errorf("failed to unwrap expected: '%s', got '%s'",
hop[0].AddrPort.String(), f1.AddrPort.String())
t.FailNow()
}
// OnionSkin(address.FromPubKey(hop[1].HeaderKey), set.Next()).
PeelOnionSkin(t, b, c).Decrypt(hop[1].HeaderPriv, b, c)
// Forward(hop[2].AddrPort).
f2 := PeelForward(t, b, c)
if hop[2].AddrPort.String() != f2.AddrPort.String() {
t.Errorf("failed to unwrap expected: '%s', got '%s'",
hop[1].AddrPort.String(), f1.AddrPort.String())
t.FailNow()
}
// OnionSkin(address.FromPubKey(hop[2].HeaderKey), set.Next()).
PeelOnionSkin(t, b, c).Decrypt(hop[2].HeaderPriv, b, c)
// Purchase(nBytes, prvs, pubs).
pr := PeelPurchase(t, b, c)
if pr.NBytes != nBytes {
t.Errorf("failed to retrieve original purchase nBytes")
t.FailNow()
}
// Reply(hop[3].AddrPort).
rp1 := PeelReply(t, b, c)
if rp1.AddrPort.String() != hop[3].AddrPort.String() {
t.Errorf("failed to retrieve first reply hop")
t.FailNow()
}
// OnionSkin(address.FromPubKey(hop[3].HeaderKey), replies[0]).
PeelOnionSkin(t, b, c).Decrypt(hop[3].HeaderPriv, b, c)
// Reply(hop[4].AddrPort).
rp2 := PeelReply(t, b, c)
if rp2.AddrPort.String() != hop[4].AddrPort.String() {
t.Errorf("failed to retrieve first reply hop")
t.FailNow()
}
// OnionSkin(address.FromPubKey(hop[4].HeaderKey), replies[1]).
PeelOnionSkin(t, b, c).Decrypt(hop[4].HeaderPriv, b, c)
// Reply(client.AddrPort).
rp3 := PeelReply(t, b, c)
if rp3.AddrPort.String() != client.AddrPort.String() {
t.Errorf("failed to retrieve first reply hop")
t.FailNow()
}
// OnionSkin(address.FromPubKey(client.HeaderKey), replies[2]).
PeelOnionSkin(t, b, c).Decrypt(client.HeaderPriv, b, c)
}

View File

@@ -10,11 +10,11 @@ var (
// GitRef is the gitref, as in refs/heads/branchname.
GitRef = "refs/heads/main"
// ParentGitCommit is the commit hash of the parent HEAD.
ParentGitCommit = "bf278ef76a87b4441fc682ffdcd28c358ae12af8"
ParentGitCommit = "d0f01299850523d31a346ebb568a513612c75a9a"
// BuildTime stores the time when the current binary was built.
BuildTime = "2022-12-27T17:23:23Z"
BuildTime = "2022-12-27T17:54:58Z"
// SemVer lists the (latest) git tag on the build.
SemVer = "v0.0.244"
SemVer = "v0.0.245"
// PathBase is the path base returned from runtime caller.
PathBase = "/home/loki/src/github.com/Indra-Labs/indra/"
// Major is the major number from the tag.
@@ -22,7 +22,7 @@ var (
// Minor is the minor number from the tag.
Minor = 0
// Patch is the patch version number from the tag.
Patch = 244
Patch = 245
)
// Version returns a pretty printed version information string.