Skip to content

Commit

Permalink
add functions to tell if an address contains a security protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jul 24, 2021
1 parent c693710 commit 25e552b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions multiaddr.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,14 @@ func (m *multiaddr) ValueForProtocol(code int) (value string, err error) {
})
return
}

func ContainsSecurityProtocol(addr Multiaddr) bool {
var contains bool
ForEach(addr, func(c Component) bool {
if code := c.Protocol().Code; code == P_TLS || code == P_NOISE {
contains = true
}
return true
})
return contains
}
30 changes: 30 additions & 0 deletions multiaddr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,33 @@ func TestComponentJSONMarshaler(t *testing.T) {
t.Error("expected equal components in circular marshaling test")
}
}

func TestContainsSecurity(t *testing.T) {
does := []string{
"/ip4/127.0.0.1/tcp/80/tls",
"/ip6/::1/tcp/4321/noise",
}
for _, s := range does {
addr, err := NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
if !ContainsSecurityProtocol(addr) {
t.Fatalf("expected %s to be recognized as a security protocol", s)
}
}
doesnt := []string{
"/ip4/127.0.0.1/tcp/80",
"/ip6/::1/tcp/4321",
"/ip4/127.0.0.1/udp/443/quic",
}
for _, s := range doesnt {
addr, err := NewMultiaddr(s)
if err != nil {
t.Fatal(err)
}
if ContainsSecurityProtocol(addr) {
t.Fatalf("didn't expect %s to be recognized as a security protocol", s)
}
}
}

0 comments on commit 25e552b

Please sign in to comment.