From d4bac4a8ddc9cbf65e6dc952819a6f483c3a7a5f Mon Sep 17 00:00:00 2001 From: Jorropo Date: Sun, 14 Jul 2019 07:13:52 +0200 Subject: [PATCH] More test and optimisation of Garlic Bridge transcoder. --- multiaddr_test.go | 6 ++++++ transcoders.go | 10 ++++------ transcoders_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) create mode 100644 transcoders_test.go diff --git a/multiaddr_test.go b/multiaddr_test.go index 22d23c1..14f7dc3 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -74,6 +74,12 @@ func TestConstructFails(t *testing.T) { "/sam3/3|2|6|6|-2|2", "/sam3/3|2|6|6|2|-2", "/sam3/3|2|6|6|2|2|7", + "/sam3/a|2|6|6|2|2", + "/sam3/2|a|6|6|2|2", + "/sam3/2|2|a|6|2|2", + "/sam3/2|2|6|a|2|2", + "/sam3/2|2|6|6|a|2", + "/sam3/2|2|6|6|2|a", "/sam3/3|2|6|6|2", "/sam2/8|7|6|6|2|2", "/sam2/0|8|6|6|2|2", diff --git a/transcoders.go b/transcoders.go index 7fdbdda..354f50d 100644 --- a/transcoders.go +++ b/transcoders.go @@ -374,6 +374,10 @@ func garlicExtractor(b []byte) [6]uint8 { } func garlicBridgeBtS(b []byte) (string, error) { + err := garlicBridgeValidate(b) + if err != nil { + return "", err + } var rs string for i, e := range garlicExtractor(b) { rs += strconv.Itoa(int(e)) @@ -390,12 +394,6 @@ func garlicBridgeValidate(b []byte) error { } lv := garlicExtractor(b) - if lv[0] > 7 { - return fmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check upstream.", lv[0]) - } - if lv[1] > 7 { - return fmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check downstream.", lv[1]) - } if lv[2] < 1 || lv[2] > 6 { return fmt.Errorf("A garlic bridge must have 1 up to 6 tunnel, not %d, check upstream.", lv[2]) } diff --git a/transcoders_test.go b/transcoders_test.go new file mode 100644 index 0000000..b6cd8d0 --- /dev/null +++ b/transcoders_test.go @@ -0,0 +1,40 @@ +package multiaddr + +import "testing" + +func TestGarlicBridge(t *testing.T) { + // Simple code and decode + fS := "7|7|6|6|2|2" + B, err := garlicBridgeStB(fS) + if err != nil { + t.Error(err) + } + err = garlicBridgeValidate(B) + if err != nil { + t.Error(err) + } + S, err := garlicBridgeBtS(B) + if err != nil { + t.Error(err) + } + if fS != S { + t.Fatalf("Got %v instead of %v.", S, fS) + } + + shouldFail := [][]byte{ // sample (7|7|6|6|2|2) : []byte{0xFF,0x6A}, + []byte{0xFF, 0xEA}, + []byte{0xFF, 0x7A}, + []byte{0xFF, 0x7E}, + []byte{0xFF, 0x7B}, + []byte{0xFC, 0x4A}, + []byte{0xFE, 0x0A}, + []byte{0xFF, 0x7B, 0xFF}, + []byte{0xFF}, + } + for _, e := range shouldFail { + S, err = garlicBridgeBtS(e) + if err == nil || S != "" { + t.Fatalf("Should fail but works with %v, and got %v.", e, S) + } + } +}