Skip to content

Commit a0e1ba2

Browse files
author
Lars Gierth
authored
Merge pull request #89 from multiformats/feat/test
Start the conformance test suite
2 parents f96edb3 + c37bc40 commit a0e1ba2

7 files changed

Lines changed: 306 additions & 0 deletions

File tree

test/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
go-multiaddr
2+
node_modules
3+
package-lock.json

test/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
all: go-multiaddr #js-multiaddr
2+
3+
go-multiaddr:
4+
go get -d -v -t .
5+
go build -o ./go-multiaddr github.com/multiformats/go-multiaddr/multiaddr
6+
MULTIADDR_BIN="./go-multiaddr" go test
7+
8+
js-multiaddr:
9+
npm install multiaddr
10+
MULTIADDR_BIN="node_modules/.bin/multiaddr" go test
11+
12+
clean:
13+
rm -rf ./go-multiaddr ./node_modules
14+
15+
.PHONY: go-multiaddr js-multiaddr clean

test/go.mod

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/multiformats/multiaddr/test
2+
3+
require (
4+
github.com/DATA-DOG/godog v0.7.13
5+
github.com/hellomd/assistdog v0.0.0-20171107191847-c91d7a54538c
6+
github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5 // indirect
7+
github.com/mr-tron/base58 v1.1.1 // indirect
8+
github.com/multiformats/go-multiaddr v0.0.3-0.20190406155144-0460aad0fe51
9+
github.com/multiformats/go-multihash v0.0.3 // indirect
10+
github.com/rdumont/assistdog v0.0.0-20171107191847-c91d7a54538c // indirect
11+
github.com/stretchr/objx v0.2.0 // indirect
12+
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480 // indirect
13+
golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be // indirect
14+
)

test/go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
github.com/DATA-DOG/godog v0.7.13/go.mod h1:z2OZ6a3X0/YAKVqLfVzYBwFt3j6uSt3Xrqa7XTtcQE0=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/gxed/hashland/keccakpg v0.0.1/go.mod h1:kRzw3HkwxFU1mpmPP8v1WyQzwdGfmKFJ6tItnhQ67kU=
5+
github.com/gxed/hashland/murmur3 v0.0.1/go.mod h1:KjXop02n4/ckmZSnY2+HKcLud/tcmvhST0bie/0lS48=
6+
github.com/hellomd/assistdog v0.0.0-20171107191847-c91d7a54538c/go.mod h1:BTyMiZsNYbS1Oh218JPK5ipfx4GgwiIcPU5AEUZe8ys=
7+
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1/go.mod h1:pD8RvIylQ358TN4wwqatJ8rNavkEINozVn9DtGI3dfQ=
8+
github.com/minio/sha256-simd v0.0.0-20190131020904-2d45a736cd16/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
9+
github.com/minio/sha256-simd v0.0.0-20190328051042-05b4dd3047e5/go.mod h1:2FMWW+8GMoPweT6+pI63m9YE3Lmw4J71hV56Chs1E/U=
10+
github.com/mr-tron/base58 v1.1.0/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
11+
github.com/mr-tron/base58 v1.1.1/go.mod h1:xcD2VGqlgYjBdcBLw+TuYLr8afG+Hj8g2eTVqeSzSU8=
12+
github.com/multiformats/go-multiaddr v0.0.3-0.20190329021609-e12d3bf7cc72/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44=
13+
github.com/multiformats/go-multiaddr v0.0.3-0.20190406155144-0460aad0fe51/go.mod h1:xKVEak1K9cS1VdmPZW3LSIb6lgmoS58qz/pzqmAxV44=
14+
github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
15+
github.com/multiformats/go-multihash v0.0.3/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U=
16+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
17+
github.com/rdumont/assistdog v0.0.0-20171107191847-c91d7a54538c/go.mod h1:oFpHlDOoD4qVvaFxKFJwsDswl5Sa8t1yWs7LKnjN1g0=
18+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
19+
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
20+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
21+
golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
22+
golang.org/x/crypto v0.0.0-20190418165655-df01cb2cc480/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
23+
golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
24+
golang.org/x/sys v0.0.0-20190403152447-81d4e9dc473e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
25+
golang.org/x/sys v0.0.0-20190418153312-f0ce4c0180be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

test/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
8+
func main() {
9+
fmt.Fprintf(os.Stderr, "run 'go test github.com/multiformats/multiaddr/test' instead\n")
10+
os.Exit(1)
11+
}

test/main_test.go

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
package main_test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
"testing"
11+
12+
godog "github.com/DATA-DOG/godog"
13+
gherkin "github.com/DATA-DOG/godog/gherkin"
14+
assistdog "github.com/hellomd/assistdog"
15+
16+
_ "github.com/multiformats/go-multiaddr"
17+
)
18+
19+
var multiaddrCmd = []string{"go", "run", "github.com/multiformats/go-multiaddr/multiaddr"}
20+
var assist *assistdog.Assist
21+
22+
func init() {
23+
bin := os.Getenv("MULTIADDR_BIN")
24+
if len(bin) > 0 {
25+
multiaddrCmd = strings.Split(bin, " ")
26+
}
27+
28+
assist = assistdog.NewDefault()
29+
}
30+
31+
func TestGodog(t *testing.T) {
32+
fmt.Printf(`MULTIADDR_BIN="%s"`+"\n", strings.Join(multiaddrCmd, " "))
33+
34+
status := godog.RunWithOptions("godog", func(s *godog.Suite) {
35+
FeatureContext(s)
36+
}, godog.Options{
37+
Format: "pretty",
38+
Paths: []string{"."},
39+
StopOnFailure: true,
40+
Strict: true,
41+
})
42+
43+
if status != 0 {
44+
t.Errorf("exit status: %d", status)
45+
}
46+
}
47+
48+
func FeatureContext(s *godog.Suite) {
49+
s.Step(`^the multiaddr (.+)$`, theMultiaddr)
50+
s.Step(`^the packed form is (.+)$`, thePackedFormIs)
51+
s.Step(`^the packed size is (.+) bytes$`, thePackedSizeIs)
52+
s.Step(`^the string form is (.+)$`, theStringFormIs)
53+
s.Step(`^the string size is (.+) bytes$`, theStringSizeIs)
54+
s.Step(`^the components are:$`, theComponentsAre)
55+
}
56+
57+
var theMaddr multiaddr
58+
59+
type multiaddr struct {
60+
String string
61+
StringSize string
62+
Packed string
63+
PackedSize string
64+
Components []component
65+
}
66+
67+
type component struct {
68+
String string
69+
StringSize string
70+
Packed string
71+
PackedSize string
72+
Value string
73+
RawValue string
74+
ValueSize string
75+
Protocol string
76+
Codec string
77+
Uvarint string
78+
LengthPrefix string
79+
}
80+
81+
func theMultiaddr(addr string) error {
82+
theMaddr = multiaddr{}
83+
84+
cmd := exec.Command(multiaddrCmd[0], append(multiaddrCmd[1:], addr)...)
85+
cmd.Stdout = &bytes.Buffer{}
86+
cmd.Stderr = &bytes.Buffer{}
87+
if err := cmd.Run(); err != nil {
88+
return fmt.Errorf("command error: %s - stderr: %s", err, cmd.Stderr.(*bytes.Buffer).String())
89+
}
90+
91+
stdout := cmd.Stdout.(*bytes.Buffer)
92+
if err := json.Unmarshal(stdout.Bytes(), &theMaddr); err != nil {
93+
return err
94+
}
95+
return nil
96+
}
97+
98+
func thePackedFormIs(packed string) error {
99+
if theMaddr.Packed != packed {
100+
return fmt.Errorf("expected %s, got %s", packed, theMaddr.Packed)
101+
}
102+
return nil
103+
}
104+
105+
func thePackedSizeIs(size string) error {
106+
if theMaddr.PackedSize != size {
107+
return fmt.Errorf("expected %s, got %s", size, theMaddr.PackedSize)
108+
}
109+
return nil
110+
}
111+
112+
func theStringFormIs(str string) error {
113+
if theMaddr.String != str {
114+
return fmt.Errorf("expected %s, got %s", str, theMaddr.String)
115+
}
116+
return nil
117+
}
118+
119+
func theStringSizeIs(size string) error {
120+
if theMaddr.StringSize != size {
121+
return fmt.Errorf("expected %s, got %s", size, theMaddr.StringSize)
122+
}
123+
return nil
124+
}
125+
126+
func theComponentsAre(tbl *gherkin.DataTable) error {
127+
table, err := assist.ParseSlice(tbl)
128+
if err != nil {
129+
return err
130+
}
131+
132+
actual := len(theMaddr.Components)
133+
expected := len(table)
134+
if actual != expected {
135+
return fmt.Errorf("expected %d row, got %d", expected, actual)
136+
}
137+
138+
for i, _ := range table {
139+
actual, ok := table[i]["string"]
140+
if ok {
141+
expected := theMaddr.Components[i].String
142+
if actual != expected {
143+
return fmt.Errorf("expected string '%s', got '%s'", expected, actual)
144+
}
145+
}
146+
actual, ok = table[i]["stringSize"]
147+
if ok {
148+
expected := theMaddr.Components[i].StringSize
149+
if actual != expected {
150+
return fmt.Errorf("expected stringSize '%s', got '%s'", expected, actual)
151+
}
152+
}
153+
actual, ok = table[i]["packed"]
154+
if ok {
155+
expected := theMaddr.Components[i].Packed
156+
if actual != expected {
157+
return fmt.Errorf("expected packed '%s', got '%s'", expected, actual)
158+
}
159+
}
160+
actual, ok = table[i]["packedSize"]
161+
if ok {
162+
expected := theMaddr.Components[i].PackedSize
163+
if actual != expected {
164+
return fmt.Errorf("expected packedSize '%s', got '%s'", expected, actual)
165+
}
166+
}
167+
actual, ok = table[i]["value"]
168+
if ok {
169+
expected := theMaddr.Components[i].Value
170+
if actual != expected {
171+
return fmt.Errorf("expected value '%s', got '%s'", expected, actual)
172+
}
173+
}
174+
actual, ok = table[i]["rawValue"]
175+
if ok {
176+
expected := theMaddr.Components[i].RawValue
177+
if actual != expected {
178+
return fmt.Errorf("expected rawValue '%s', got '%s'", expected, actual)
179+
}
180+
}
181+
actual, ok = table[i]["valueSize"]
182+
if ok {
183+
expected := theMaddr.Components[i].ValueSize
184+
if actual != expected {
185+
return fmt.Errorf("expected valueSize '%s', got '%s'", expected, actual)
186+
}
187+
}
188+
actual, ok = table[i]["protocol"]
189+
if ok {
190+
expected := theMaddr.Components[i].Protocol
191+
if actual != expected {
192+
return fmt.Errorf("expected protocol '%s', got '%s'", expected, actual)
193+
}
194+
}
195+
actual, ok = table[i]["codec"]
196+
if ok {
197+
expected := theMaddr.Components[i].Codec
198+
if actual != expected {
199+
return fmt.Errorf("expected codec '%s', got '%s'", expected, actual)
200+
}
201+
}
202+
actual, ok = table[i]["uvarint"]
203+
if ok {
204+
expected := theMaddr.Components[i].Uvarint
205+
if actual != expected {
206+
return fmt.Errorf("expected uvarint '%s', got '%s'", expected, actual)
207+
}
208+
}
209+
actual, ok = table[i]["lengthPrefix"]
210+
if ok {
211+
expected := theMaddr.Components[i].LengthPrefix
212+
if actual != expected {
213+
return fmt.Errorf("expected lengthPrefix '%s', got '%s'", expected, actual)
214+
}
215+
}
216+
}
217+
218+
return nil
219+
}

test/multiaddr.feature

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Feature: Multiaddr
2+
3+
Scenario: Simple string form
4+
Given the multiaddr /ip4/192.0.2.42/tcp/443
5+
Then the packed form is 0x04c000022a0601bb
6+
And the packed size is 8 bytes
7+
And the components are:
8+
| string | stringSize | packed | packedSize | value | valueSize | protocol | codec | uvarint | lengthPrefix | rawValue |
9+
| /ip4/192.0.2.42 | 15 | 0x04c000022a | 5 | 192.0.2.42 | 4 | ip4 | 4 | 0x04 | | 0xc000022a |
10+
| /tcp/443 | 8 | 0x0601bb | 3 | 443 | 2 | tcp | 6 | 0x06 | | 0x01bb |
11+
12+
Scenario: Simple packed form
13+
Given the multiaddr 0x04c000022a0601bb
14+
Then the string form is /ip4/192.0.2.42/tcp/443
15+
And the string size is 23 bytes
16+
And the components are:
17+
| string | stringSize | packed | packedSize | value | valueSize | protocol | codec | uvarint | lengthPrefix | rawValue |
18+
| /ip4/192.0.2.42 | 15 | 0x04c000022a | 5 | 192.0.2.42 | 4 | ip4 | 4 | 0x04 | | 0xc000022a |
19+
| /tcp/443 | 8 | 0x0601bb | 3 | 443 | 2 | tcp | 6 | 0x06 | | 0x01bb |

0 commit comments

Comments
 (0)