USING: io.sockets io.sockets.private sequences math tools.test
namespaces accessors kernel destructors calendar io.timeouts
io.encodings.utf8 io concurrency.promises threads
-io.streams.string ;
+io.streams.string present ;
IN: io.sockets.tests
+[ T{ local f "/tmp/foo" } ] [ "/tmp/foo" <local> ] unit-test
[ T{ inet4 f f 0 } ] [ f 0 <inet4> ] unit-test
-[ T{ inet6 f f 0 } ] [ f 0 <inet6> ] unit-test
+[ T{ inet6 f f 0 1 } ] [ f 1 <inet6> ] unit-test
[ T{ inet f "google.com" f } ] [ "google.com" f <inet> ] unit-test
[ T{ inet f "google.com" 80 } ] [ "google.com" 0 <inet> 80 with-port ] unit-test
[ T{ inet4 f "8.8.8.8" 0 } ] [ "8.8.8.8" 0 <inet4> ] unit-test
[ T{ inet4 f "8.8.8.8" 53 } ] [ "8.8.8.8" 0 <inet4> 53 with-port ] unit-test
-[ T{ inet6 f "5:5:5:5:6:6:6:6" 12 } ] [ "5:5:5:5:6:6:6:6" 0 <inet6> 12 with-port ] unit-test
+[ T{ inet6 f "5:5:5:5:6:6:6:6" 0 12 } ] [ "5:5:5:5:6:6:6:6" 0 <inet6> 12 with-port ] unit-test
+[ T{ inet6 f "fe80::1" 1 80 } ] [ T{ ipv6 f "fe80::1" 1 } 80 with-port ] unit-test
+
+: test-sockaddr ( addrspec -- )
+ [ dup make-sockaddr ] keep parse-sockaddr assert= ;
+
+[ ] [ T{ inet4 f "8.8.8.8" 53 } test-sockaddr ] unit-test
+[ ] [ T{ inet6 f "5:5:5:5:6:6:6:6" 0 12 } test-sockaddr ] unit-test
+[ ] [ T{ inet6 f "fe80:0:0:0:0:0:0:1" 1 80 } test-sockaddr ] unit-test
[ T{ inet f "google.com" 80 } ] [ "google.com" 80 with-port ] unit-test
+! Test present on addrspecs
+[ "4.4.4.4:12" ] [ "4.4.4.4" 12 <inet4> present ] unit-test
+[ "::1:12" ] [ "::1" 12 <inet6> present ] unit-test
+[ "fe80::1%1:12" ] [ "fe80::1" 1 12 inet6 boa present ] unit-test
+
[ B{ 1 2 3 4 } ]
[ "1.2.3.4" T{ inet4 } inet-pton ] unit-test
M: inet4 protocol drop 0 ;
-TUPLE: ipv6 { host ?string read-only } ;
+TUPLE: ipv6
+{ host ?string read-only }
+{ scope-id integer read-only } ;
-C: <ipv6> ipv6
+: <ipv6> ( host -- ipv6 ) 0 ipv6 boa ;
M: ipv6 inet-ntop ( data addrspec -- str )
drop 16 memory>byte-array 2 <groups> [ be> >hex ] map ":" join ;
AF_INET6 >>family
swap
[ port>> htons >>port ]
- [ host>> "::" or ]
- [ inet-pton >>addr ] tri ;
+ [ [ host>> "::" or ] keep inet-pton >>addr ]
+ [ scope-id>> >>scopeid ]
+ tri ;
M: ipv6 parse-sockaddr
- [ addr>> ] dip inet-ntop <ipv6> ;
+ [ [ addr>> ] dip inet-ntop ] [ drop scopeid>> ] 2bi
+ ipv6 boa ;
+
+M: ipv6 present
+ [ host>> ] [ scope-id>> ] bi
+ [ number>string "%" glue ] unless-zero ;
TUPLE: inet6 < ipv6 { port integer read-only } ;
-C: <inet6> inet6
+: <inet6> ( host port -- inet6 ) [ 0 ] dip inet6 boa ;
-M: ipv6 with-port [ host>> ] dip <inet6> ;
+M: ipv6 with-port
+ [ [ host>> ] [ scope-id>> ] bi ] dip
+ inet6 boa ;
M: inet6 parse-sockaddr
[ call-next-method ] [ drop port>> ntohs ] 2bi with-port ;
M: inet6 present
- [ host>> ] [ port>> number>string ] bi ":" glue ;
+ [ call-next-method ] [ port>> number>string ] bi ":" glue ;
M: inet6 protocol drop 0 ;