|
| 1 | +package meg_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/multiformats/go-multiaddr" |
| 7 | + "github.com/multiformats/go-multiaddr/x/meg" |
| 8 | +) |
| 9 | + |
| 10 | +type preallocatedCapture struct { |
| 11 | + certHashes []string |
| 12 | + matcher *meg.MatchState |
| 13 | +} |
| 14 | + |
| 15 | +func preallocateCapture() *preallocatedCapture { |
| 16 | + p := &preallocatedCapture{} |
| 17 | + p.matcher = meg.PatternToMatchState( |
| 18 | + meg.Or( |
| 19 | + meg.Val(multiaddr.P_IP4), |
| 20 | + meg.Val(multiaddr.P_IP6), |
| 21 | + meg.Val(multiaddr.P_DNS), |
| 22 | + ), |
| 23 | + meg.Val(multiaddr.P_UDP), |
| 24 | + meg.Val(multiaddr.P_WEBRTC_DIRECT), |
| 25 | + meg.CaptureZeroOrMore(multiaddr.P_CERTHASH, &p.certHashes), |
| 26 | + ) |
| 27 | + return p |
| 28 | +} |
| 29 | + |
| 30 | +var matcher = preallocateCapture() |
| 31 | + |
| 32 | +func (p *preallocatedCapture) IsWebRTCDirectMultiaddr(addr multiaddr.Multiaddr) (bool, int) { |
| 33 | + found, _ := meg.Match(p.matcher, addr) |
| 34 | + return found, len(p.certHashes) |
| 35 | +} |
| 36 | + |
| 37 | +// IsWebRTCDirectMultiaddr returns whether addr is a /webrtc-direct multiaddr with the count of certhashes |
| 38 | +// in addr |
| 39 | +func IsWebRTCDirectMultiaddr(addr multiaddr.Multiaddr) (bool, int) { |
| 40 | + return matcher.IsWebRTCDirectMultiaddr(addr) |
| 41 | +} |
| 42 | + |
| 43 | +// IsWebRTCDirectMultiaddrLoop returns whether addr is a /webrtc-direct multiaddr with the count of certhashes |
| 44 | +// in addr |
| 45 | +func IsWebRTCDirectMultiaddrLoop(addr multiaddr.Multiaddr) (bool, int) { |
| 46 | + protos := [...]int{multiaddr.P_IP4, multiaddr.P_IP6, multiaddr.P_DNS, multiaddr.P_UDP, multiaddr.P_WEBRTC_DIRECT} |
| 47 | + matchProtos := [...][]int{protos[:3], {protos[3]}, {protos[4]}} |
| 48 | + certHashCount := 0 |
| 49 | + for i, c := range addr { |
| 50 | + if i >= len(matchProtos) { |
| 51 | + if c.Code() == multiaddr.P_CERTHASH { |
| 52 | + certHashCount++ |
| 53 | + } else { |
| 54 | + return false, 0 |
| 55 | + } |
| 56 | + } else { |
| 57 | + found := false |
| 58 | + for _, proto := range matchProtos[i] { |
| 59 | + if c.Code() == proto { |
| 60 | + found = true |
| 61 | + break |
| 62 | + } |
| 63 | + } |
| 64 | + if !found { |
| 65 | + return false, 0 |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + return true, certHashCount |
| 70 | +} |
| 71 | + |
| 72 | +func BenchmarkIsWebRTCDirectMultiaddr(b *testing.B) { |
| 73 | + addr := multiaddr.StringCast("/ip4/1.2.3.4/udp/1234/webrtc-direct/") |
| 74 | + |
| 75 | + b.ResetTimer() |
| 76 | + for i := 0; i < b.N; i++ { |
| 77 | + isWebRTC, count := IsWebRTCDirectMultiaddr(addr) |
| 78 | + if !isWebRTC || count != 0 { |
| 79 | + b.Fatal("unexpected result") |
| 80 | + } |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func BenchmarkIsWebRTCDirectMultiaddrLoop(b *testing.B) { |
| 85 | + addr := multiaddr.StringCast("/ip4/1.2.3.4/udp/1234/webrtc-direct/") |
| 86 | + |
| 87 | + b.ResetTimer() |
| 88 | + for i := 0; i < b.N; i++ { |
| 89 | + isWebRTC, count := IsWebRTCDirectMultiaddrLoop(addr) |
| 90 | + if !isWebRTC || count != 0 { |
| 91 | + b.Fatal("unexpected result") |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments