Skip to content

Commit

Permalink
More test and optimisation of Garlic Bridge transcoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Jul 14, 2019
1 parent 9d25c8e commit d4bac4a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 6 deletions.
6 changes: 6 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 4 additions & 6 deletions transcoders.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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])
}
Expand Down
40 changes: 40 additions & 0 deletions transcoders_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit d4bac4a

Please sign in to comment.