Skip to content

Commit d4bac4a

Browse files
committed
More test and optimisation of Garlic Bridge transcoder.
1 parent 9d25c8e commit d4bac4a

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

Diff for: multiaddr_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ func TestConstructFails(t *testing.T) {
7474
"/sam3/3|2|6|6|-2|2",
7575
"/sam3/3|2|6|6|2|-2",
7676
"/sam3/3|2|6|6|2|2|7",
77+
"/sam3/a|2|6|6|2|2",
78+
"/sam3/2|a|6|6|2|2",
79+
"/sam3/2|2|a|6|2|2",
80+
"/sam3/2|2|6|a|2|2",
81+
"/sam3/2|2|6|6|a|2",
82+
"/sam3/2|2|6|6|2|a",
7783
"/sam3/3|2|6|6|2",
7884
"/sam2/8|7|6|6|2|2",
7985
"/sam2/0|8|6|6|2|2",

Diff for: transcoders.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,10 @@ func garlicExtractor(b []byte) [6]uint8 {
374374
}
375375

376376
func garlicBridgeBtS(b []byte) (string, error) {
377+
err := garlicBridgeValidate(b)
378+
if err != nil {
379+
return "", err
380+
}
377381
var rs string
378382
for i, e := range garlicExtractor(b) {
379383
rs += strconv.Itoa(int(e))
@@ -390,12 +394,6 @@ func garlicBridgeValidate(b []byte) error {
390394
}
391395
lv := garlicExtractor(b)
392396

393-
if lv[0] > 7 {
394-
return fmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check upstream.", lv[0])
395-
}
396-
if lv[1] > 7 {
397-
return fmt.Errorf("A garlic bridge can't have more than 7 hops, not %d, check downstream.", lv[1])
398-
}
399397
if lv[2] < 1 || lv[2] > 6 {
400398
return fmt.Errorf("A garlic bridge must have 1 up to 6 tunnel, not %d, check upstream.", lv[2])
401399
}

Diff for: transcoders_test.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package multiaddr
2+
3+
import "testing"
4+
5+
func TestGarlicBridge(t *testing.T) {
6+
// Simple code and decode
7+
fS := "7|7|6|6|2|2"
8+
B, err := garlicBridgeStB(fS)
9+
if err != nil {
10+
t.Error(err)
11+
}
12+
err = garlicBridgeValidate(B)
13+
if err != nil {
14+
t.Error(err)
15+
}
16+
S, err := garlicBridgeBtS(B)
17+
if err != nil {
18+
t.Error(err)
19+
}
20+
if fS != S {
21+
t.Fatalf("Got %v instead of %v.", S, fS)
22+
}
23+
24+
shouldFail := [][]byte{ // sample (7|7|6|6|2|2) : []byte{0xFF,0x6A},
25+
[]byte{0xFF, 0xEA},
26+
[]byte{0xFF, 0x7A},
27+
[]byte{0xFF, 0x7E},
28+
[]byte{0xFF, 0x7B},
29+
[]byte{0xFC, 0x4A},
30+
[]byte{0xFE, 0x0A},
31+
[]byte{0xFF, 0x7B, 0xFF},
32+
[]byte{0xFF},
33+
}
34+
for _, e := range shouldFail {
35+
S, err = garlicBridgeBtS(e)
36+
if err == nil || S != "" {
37+
t.Fatalf("Should fail but works with %v, and got %v.", e, S)
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)