@@ -974,3 +974,66 @@ func TestHTTPPath(t *testing.T) {
974
974
require .Equal (t , "tmp/bar" , string (component .RawValue ()))
975
975
})
976
976
}
977
+
978
+ func FuzzSplitRoundtrip (f * testing.F ) {
979
+ for _ , v := range good {
980
+ f .Add (v )
981
+ }
982
+ otherMultiaddr := StringCast ("/udp/1337" )
983
+
984
+ f .Fuzz (func (t * testing.T , addrStr string ) {
985
+ addr , err := NewMultiaddr (addrStr )
986
+ if err != nil {
987
+ t .Skip () // Skip inputs that are not valid multiaddrs
988
+ }
989
+
990
+ // Test SplitFirst
991
+ first , rest := SplitFirst (addr )
992
+ joined := Join (first , rest )
993
+ require .Equal (t , addr , joined , "SplitFirst and Join should round-trip" )
994
+
995
+ // Test SplitLast
996
+ rest , last := SplitLast (addr )
997
+ joined = Join (rest , last )
998
+ require .Equal (t , addr , joined , "SplitLast and Join should round-trip" )
999
+
1000
+ p := addr .Protocols ()
1001
+ if len (p ) == 0 {
1002
+ t .Skip ()
1003
+ }
1004
+
1005
+ tryPubMethods := func (a Multiaddr ) {
1006
+ if a == nil {
1007
+ return
1008
+ }
1009
+ _ = a .Equal (otherMultiaddr )
1010
+ _ = a .Bytes ()
1011
+ _ = a .String ()
1012
+ _ = a .Protocols ()
1013
+ _ = a .Encapsulate (otherMultiaddr )
1014
+ _ = a .Decapsulate (otherMultiaddr )
1015
+ _ , _ = a .ValueForProtocol (P_TCP )
1016
+ }
1017
+
1018
+ for _ , proto := range p {
1019
+ splitFunc := func (c Component ) bool {
1020
+ return c .Protocol ().Code == proto .Code
1021
+ }
1022
+ beforeC , after := SplitFirst (addr )
1023
+ joined = Join (beforeC , after )
1024
+ require .Equal (t , addr , joined )
1025
+ tryPubMethods (after )
1026
+
1027
+ before , afterC := SplitLast (addr )
1028
+ joined = Join (before , afterC )
1029
+ require .Equal (t , addr , joined )
1030
+ tryPubMethods (before )
1031
+
1032
+ before , after = SplitFunc (addr , splitFunc )
1033
+ joined = Join (before , after )
1034
+ require .Equal (t , addr , joined )
1035
+ tryPubMethods (before )
1036
+ tryPubMethods (after )
1037
+ }
1038
+ })
1039
+ }
0 commit comments