forked from multiformats/go-multiaddr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmeg_test.go
More file actions
72 lines (64 loc) · 1.59 KB
/
meg_test.go
File metadata and controls
72 lines (64 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package multiaddr
import (
"net/netip"
"testing"
"github.com/aperturerobotics/go-multiaddr/x/meg"
)
func TestMatchAndCaptureMultiaddr(t *testing.T) {
m := StringCast("/ip4/1.2.3.4/udp/8231/quic-v1/webtransport/certhash/b2uaraocy6yrdblb4sfptaddgimjmmpy/certhash/zQmbWTwYGcmdyK9CYfNBcfs9nhZs17a6FQ4Y8oea278xx41")
var udpPort string
var certhashes []string
found, _ := m.Match(
meg.Or(
meg.Val(P_IP4),
meg.Val(P_IP6),
),
meg.CaptureString(P_UDP, &udpPort),
meg.Val(P_QUIC_V1),
meg.Val(P_WEBTRANSPORT),
meg.CaptureZeroOrMoreStrings(P_CERTHASH, &certhashes),
)
if !found {
t.Fatal("failed to match")
}
if udpPort != "8231" {
t.Fatal("unexpected value")
}
if len(certhashes) != 2 {
t.Fatal("Didn't capture all certhashes")
}
{
m, c := SplitLast(m)
if c.Value() != certhashes[1] {
t.Fatal("unexpected value. Expected", c.RawValue(), "but got", []byte(certhashes[1]))
}
_, c = SplitLast(m)
if c.Value() != certhashes[0] {
t.Fatal("unexpected value. Expected", c.RawValue(), "but got", []byte(certhashes[0]))
}
}
}
func TestCaptureAddrPort(t *testing.T) {
m := StringCast("/ip4/1.2.3.4/udp/8231/quic-v1/webtransport")
var addrPort netip.AddrPort
var network string
found, err := m.Match(
CaptureAddrPort(&network, &addrPort),
meg.ZeroOrMore(meg.Any),
)
if err != nil {
t.Fatal("error", err)
}
if !found {
t.Fatal("failed to match")
}
if !addrPort.IsValid() {
t.Fatal("failed to capture addrPort")
}
if network != "udp" {
t.Fatal("unexpected network", network)
}
if addrPort.String() != "1.2.3.4:8231" {
t.Fatal("unexpected ipPort", addrPort)
}
}