Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
mf
go-multiaddr
Commits
ed277d56
Commit
ed277d56
authored
Jan 09, 2015
by
Juan Batiz-Benet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better errs, and test parsing
parent
99cf3edc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
84 additions
and
9 deletions
+84
-9
codec.go
codec.go
+27
-9
multiaddr_test.go
multiaddr_test.go
+57
-0
No files found.
codec.go
View file @
ed277d56
...
...
@@ -32,7 +32,13 @@ func stringToBytes(s string) ([]byte, error) {
sp
=
sp
[
1
:
]
if
p
.
Size
>
0
{
a
:=
addressStringToBytes
(
p
,
sp
[
0
])
if
len
(
sp
)
<
1
{
return
nil
,
fmt
.
Errorf
(
"protocol requires address, none given: %s"
,
sp
)
}
a
,
err
:=
addressStringToBytes
(
p
,
sp
[
0
])
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to parse %s: %s %s"
,
p
.
Name
,
sp
[
0
],
err
)
}
b
=
append
(
b
,
a
...
)
sp
=
sp
[
1
:
]
}
...
...
@@ -98,26 +104,38 @@ func bytesSplit(b []byte) (ret [][]byte, err error) {
return
ret
,
nil
}
func
addressStringToBytes
(
p
*
Protocol
,
s
string
)
[]
byte
{
func
addressStringToBytes
(
p
*
Protocol
,
s
string
)
(
[]
byte
,
error
)
{
switch
p
.
Code
{
case
P_IP4
:
// ipv4
return
net
.
ParseIP
(
s
)
.
To4
()
i
:=
net
.
ParseIP
(
s
)
.
To4
()
if
i
==
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to parse ip4 addr: %s"
,
s
)
}
return
i
,
nil
case
P_IP6
:
// ipv6
return
net
.
ParseIP
(
s
)
.
To16
()
i
:=
net
.
ParseIP
(
s
)
.
To16
()
if
i
==
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to parse ip6 addr: %s"
,
s
)
}
return
i
,
nil
// tcp udp dccp sctp
case
P_TCP
,
P_UDP
,
P_DCCP
,
P_SCTP
:
b
:=
make
([]
byte
,
2
)
i
,
err
:=
strconv
.
Atoi
(
s
)
if
err
==
nil
{
binary
.
BigEndian
.
PutUint16
(
b
,
uint16
(
i
))
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"failed to parse %s addr: %s"
,
p
.
Name
,
err
)
}
if
i
>=
65536
{
return
nil
,
fmt
.
Errorf
(
"failed to parse %s addr: %s"
,
p
.
Name
,
"greater than 65536"
)
}
return
b
b
:=
make
([]
byte
,
2
)
binary
.
BigEndian
.
PutUint16
(
b
,
uint16
(
i
))
return
b
,
nil
}
return
[]
byte
{}
return
[]
byte
{}
,
fmt
.
Errorf
(
"failed to parse %s addr: unknown"
,
p
.
Name
)
}
func
addressBytesToString
(
p
*
Protocol
,
b
[]
byte
)
string
{
...
...
multiaddr_test.go
View file @
ed277d56
...
...
@@ -14,6 +14,63 @@ func newMultiaddr(t *testing.T, a string) Multiaddr {
return
m
}
func
TestConstructFails
(
t
*
testing
.
T
)
{
cases
:=
[]
string
{
"/ip4"
,
"/ip4/::1"
,
"/ip4/fdpsofodsajfdoisa"
,
"/ip6"
,
"/udp"
,
"/tcp"
,
"/sctp"
,
"/udp/65536"
,
"/tcp/65536"
,
"/udp/1234/sctp"
,
"/udp/1234/udt/1234"
,
"/udp/1234/utp/1234"
,
"/ip4/127.0.0.1/udp/jfodsajfidosajfoidsa"
,
"/ip4/127.0.0.1/udp"
,
"/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa"
,
"/ip4/127.0.0.1/tcp"
,
}
for
_
,
a
:=
range
cases
{
if
_
,
err
:=
NewMultiaddr
(
a
);
err
==
nil
{
t
.
Errorf
(
"should have failed: %s"
,
a
)
}
}
}
func
TestConstructSucceeds
(
t
*
testing
.
T
)
{
cases
:=
[]
string
{
"/ip4/1.2.3.4"
,
"/ip4/0.0.0.0"
,
"/ip6/::1"
,
"/ip6/2601:9:4f81:9700:803e:ca65:66e8:c21"
,
"/udp/0"
,
"/tcp/0"
,
"/sctp/0"
,
"/udp/1234"
,
"/tcp/1234"
,
"/sctp/1234"
,
"/udp/65535"
,
"/tcp/65535"
,
"/udp/1234/sctp/1234"
,
"/udp/1234/udt"
,
"/udp/1234/utp"
,
"/ip4/127.0.0.1/udp/1234"
,
"/ip4/127.0.0.1/udp/0"
,
"/ip4/127.0.0.1/tcp/1234"
,
"/ip4/127.0.0.1/tcp/1234/"
,
}
for
_
,
a
:=
range
cases
{
if
_
,
err
:=
NewMultiaddr
(
a
);
err
!=
nil
{
t
.
Errorf
(
"should have succeeded: %s"
,
a
)
}
}
}
func
TestEqual
(
t
*
testing
.
T
)
{
m1
:=
newMultiaddr
(
t
,
"/ip4/127.0.0.1/udp/1234"
)
m2
:=
newMultiaddr
(
t
,
"/ip4/127.0.0.1/tcp/1234"
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment