Skip to content

Commit 1582ecd

Browse files
add functions to tell if an address contains a security protocol
1 parent fc72303 commit 1582ecd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Diff for: multiaddr.go

+11
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,14 @@ func (m *multiaddr) ValueForProtocol(code int) (value string, err error) {
184184
})
185185
return
186186
}
187+
188+
func ContainsSecurityProtocol(addr Multiaddr) bool {
189+
var contains bool
190+
ForEach(addr, func(c Component) bool {
191+
if code := c.Protocol().Code; code == P_TLS || code == P_NOISE {
192+
contains = true
193+
}
194+
return true
195+
})
196+
return contains
197+
}

Diff for: multiaddr_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,33 @@ func TestComponentJSONMarshaler(t *testing.T) {
739739
t.Error("expected equal components in circular marshaling test")
740740
}
741741
}
742+
743+
func TestContainsSecurity(t *testing.T) {
744+
does := []string{
745+
"/ip4/127.0.0.1/tcp/80/tls",
746+
"/ip6/::1/tcp/4321/noise",
747+
}
748+
for _, s := range does {
749+
addr, err := NewMultiaddr(s)
750+
if err != nil {
751+
t.Fatal(err)
752+
}
753+
if !ContainsSecurityProtocol(addr) {
754+
t.Fatalf("expected %s to be recognized as a security protocol", s)
755+
}
756+
}
757+
doesnt := []string{
758+
"/ip4/127.0.0.1/tcp/80",
759+
"/ip6/::1/tcp/4321",
760+
"/ip4/127.0.0.1/udp/443/quic",
761+
}
762+
for _, s := range doesnt {
763+
addr, err := NewMultiaddr(s)
764+
if err != nil {
765+
t.Fatal(err)
766+
}
767+
if ContainsSecurityProtocol(addr) {
768+
t.Fatalf("didn't expect %s to be recognized as a security protocol", s)
769+
}
770+
}
771+
}

0 commit comments

Comments
 (0)