Skip to content

Commit e791f31

Browse files
author
Lars Gierth
committed
Add conformance testing
This patch adds a go-multiaddr CLI to aid in testing this implementation of multiaddr. It takes a multiaddr in string or packed form as input, and prints detailed information about the multiaddr. This tool can be useful beyond testing, of course. Another addition is a Makefile target for running the new multiaddr conformance test suite. This test suite lives at https://github.com/multiformats/multiaddr and is fetched to be run against our new go-multiaddr CLI. This target is to be run in CI Neither the test suite nor the CLI are complete yet. Currently the output looks like this: ``` > go run ./multiaddr /ip4/192.0.2.42/tcp/443 | jq . { "string": "/ip4/192.0.2.42/tcp/443", "stringSize": "23", "packed": "0x04c000022a0601bb", "packedSize": "8", "components": [ { "string": "/ip4/192.0.2.42", "stringSize": "15", "packed": "0x04c000022a", "packedSize": "5", "value": "192.0.2.42", "rawValue": "0xc000022a", "valueSize": "4", "protocol": "ip4", "codec": "4", "uvarint": "0x04", "lengthPrefix": "" }, { "string": "/tcp/443", "stringSize": "8", "packed": "0x0601bb", "packedSize": "3", "value": "443", "rawValue": "0x01bb", "valueSize": "2", "protocol": "tcp", "codec": "6", "uvarint": "0x06", "lengthPrefix": "" } ] } ``` And the Makefile target: ``` > make conformance go get -d -v . go build -o tmp/multiaddr/test/go-multiaddr ./multiaddr cd tmp/multiaddr/test && MULTIADDR_BIN="./go-multiaddr" go test -v === RUN TestGodog MULTIADDR_BIN="./go-multiaddr" Feature: Multiaddr Scenario: Banana # multiaddr.feature:3 Given the multiaddr /ip4/192.0.2.42/tcp/443 # main_test.go:81 -> github.com/multiformats/multiaddr/test_test.theMultiaddr Then the packed form is 0x04c000022a0601bb # main_test.go:98 -> github.com/multiformats/multiaddr/test_test.thePackedFormIs And the packed size is 8 bytes # main_test.go:105 -> github.com/multiformats/multiaddr/test_test.thePackedSizeIs And the components are: # main_test.go:126 -> github.com/multiformats/multiaddr/test_test.theComponentsAre | string | stringSize | packed | packedSize | value | valueSize | protocol | codec | uvarint | lengthPrefix | rawValue | | /ip4/192.0.2.42 | 15 | 0x04c000022a | 5 | 192.0.2.42 | 4 | ip4 | 4 | 0x04 | | 0xc000022a | | /tcp/443 | 8 | 0x0601bb | 3 | 443 | 2 | tcp | 6 | 0x06 | | 0x01bb | Scenario: Banana #2 # multiaddr.feature:12 Given the multiaddr 0x04c000022a0601bb # main_test.go:81 -> github.com/multiformats/multiaddr/test_test.theMultiaddr Then the string form is /ip4/192.0.2.42/tcp/443 # main_test.go:112 -> github.com/multiformats/multiaddr/test_test.theStringFormIs And the string size is 23 bytes # main_test.go:119 -> github.com/multiformats/multiaddr/test_test.theStringSizeIs And the components are: # main_test.go:126 -> github.com/multiformats/multiaddr/test_test.theComponentsAre | string | stringSize | packed | packedSize | value | valueSize | protocol | codec | uvarint | lengthPrefix | rawValue | | /ip4/192.0.2.42 | 15 | 0x04c000022a | 5 | 192.0.2.42 | 4 | ip4 | 4 | 0x04 | | 0xc000022a | | /tcp/443 | 8 | 0x0601bb | 3 | 443 | 2 | tcp | 6 | 0x06 | | 0x01bb | 2 scenarios (2 passed) 8 steps (8 passed) 3.187755ms --- PASS: TestGodog (0.00s) PASS ok github.com/multiformats/multiaddr/test 0.012s ```
1 parent ce21123 commit e791f31

File tree

5 files changed

+113
-1
lines changed

5 files changed

+113
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.vscode/
2+
multiaddr/multiaddr
3+
tmp/

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ install:
2020

2121
script:
2222
- bash <(curl -s https://raw.githubusercontent.com/ipfs/ci-helpers/master/travis-ci/run-standard-tests.sh)
23-
23+
- make conformance
2424

2525
cache:
2626
directories:

Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ deps: gx covertools
1212
publish:
1313
gx-go rewrite --undo
1414

15+
conformance: tmp/multiaddr
16+
go get -d -v .
17+
go build -o tmp/multiaddr/test/go-multiaddr ./multiaddr
18+
cd tmp/multiaddr/test && MULTIADDR_BIN="./go-multiaddr" go test -v
19+
20+
tmp/multiaddr:
21+
mkdir -p tmp/
22+
git clone https://github.com/multiformats/multiaddr tmp/multiaddr/
23+
# TODO(lgierth): drop this once multiaddr test suite is merged
24+
git --work-tree=tmp/multiaddr/ --git-dir=tmp/multiaddr/.git checkout feat/test
25+
26+
clean:
27+
rm -rf tmp/
28+
29+
.PHONY: gx covertools deps publish conformance clean

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore:
2+
- "multiaddr"

multiaddr/main.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package main
2+
3+
import (
4+
"encoding/hex"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"strings"
9+
10+
maddr "github.com/multiformats/go-multiaddr"
11+
)
12+
13+
var (
14+
flagHelp bool
15+
)
16+
17+
func main() {
18+
flag.Usage = func() {
19+
usage := `usage: %s [options] ADDR
20+
21+
Print details about the given multiaddr.
22+
23+
Options:
24+
`
25+
fmt.Fprintf(os.Stderr, usage, os.Args[0])
26+
flag.PrintDefaults()
27+
}
28+
29+
flag.BoolVar(&flagHelp, "h", false, "display help message")
30+
flag.Parse()
31+
32+
if flagHelp || len(flag.Args()) == 0 {
33+
flag.Usage()
34+
os.Exit(0)
35+
}
36+
37+
addrStr := flag.Args()[0]
38+
var addr maddr.Multiaddr
39+
var err error
40+
if strings.HasPrefix(addrStr, "0x") {
41+
addrBytes, err := hex.DecodeString(addrStr[2:])
42+
if err != nil {
43+
fmt.Fprintf(os.Stderr, "parse error: %s\n", err)
44+
os.Exit(1)
45+
}
46+
addr, err = maddr.NewMultiaddrBytes(addrBytes)
47+
} else {
48+
addr, err = maddr.NewMultiaddr(addrStr)
49+
}
50+
if err != nil {
51+
fmt.Fprintf(os.Stderr, "parse error: %s\n", err)
52+
os.Exit(1)
53+
}
54+
55+
infoCommand(addr)
56+
}
57+
58+
func infoCommand(addr maddr.Multiaddr) {
59+
var compsJson []string
60+
maddr.ForEach(addr, func(comp maddr.Component) bool {
61+
lengthPrefix := ""
62+
if comp.Protocol().Size == maddr.LengthPrefixedVarSize {
63+
lengthPrefix = "0x" + hex.EncodeToString(maddr.CodeToVarint(len(comp.RawValue())))
64+
}
65+
66+
compsJson = append(compsJson, `{`+
67+
fmt.Sprintf(`"string": "%s", `, comp.String())+
68+
fmt.Sprintf(`"stringSize": "%d", `, len(comp.String()))+
69+
fmt.Sprintf(`"packed": "0x%x", `, comp.Bytes())+
70+
fmt.Sprintf(`"packedSize": "%d", `, len(comp.Bytes()))+
71+
fmt.Sprintf(`"value": %#v, `, comp.Value())+
72+
fmt.Sprintf(`"rawValue": "0x%x", `, comp.RawValue())+
73+
fmt.Sprintf(`"valueSize": "%d", `, len(comp.RawValue()))+
74+
fmt.Sprintf(`"protocol": "%s", `, comp.Protocol().Name)+
75+
fmt.Sprintf(`"codec": "%d", `, comp.Protocol().Code)+
76+
fmt.Sprintf(`"uvarint": "0x%x", `, comp.Protocol().VCode)+
77+
fmt.Sprintf(`"lengthPrefix": "%s"`, lengthPrefix)+
78+
`}`)
79+
return true
80+
})
81+
82+
addrJson := `{
83+
"string": "%[1]s",
84+
"stringSize": "%[2]d",
85+
"packed": "0x%[3]x",
86+
"packedSize": "%[4]d",
87+
"components": [
88+
%[5]s
89+
]
90+
}`
91+
fmt.Fprintf(os.Stdout, addrJson+"\n",
92+
addr.String(), len(addr.String()), addr.Bytes(), len(addr.Bytes()), strings.Join(compsJson, ",\n "))
93+
}

0 commit comments

Comments
 (0)