1+ #![ cfg( all( windows, feature = "windows_unix_domain_sockets" ) ) ]
2+ #![ unstable( feature = "windows_unix_domain_sockets" , issue = "none" ) ]
3+
4+ use std:: io:: { Read , Write } ;
5+ use std:: os:: windows:: net:: { UnixListener , UnixStream } ;
6+ use std:: path:: Path ;
7+ use std:: thread;
8+
9+ #[ test]
10+ fn smoke_bind_connect ( ) {
11+ let tmp = std:: env:: temp_dir ( ) ;
12+ let sock_path = tmp. join ( "rust-test-uds.sock" ) ;
13+
14+ // 1. 绑定
15+ let listener = UnixListener :: bind ( & sock_path) . expect ( "bind failed" ) ;
16+
17+ // 2. 并发连接
18+ let tx = thread:: spawn ( move || {
19+ let mut stream = UnixStream :: connect ( & sock_path) . expect ( "connect failed" ) ;
20+ stream. write_all ( b"hello" ) . expect ( "write failed" ) ;
21+ } ) ;
22+
23+ // 3. 接受
24+ let ( mut stream, _) = listener. accept ( ) . expect ( "accept failed" ) ;
25+ let mut buf = [ 0 ; 5 ] ;
26+ stream. read_exact ( & mut buf) . expect ( "read failed" ) ;
27+ assert_eq ! ( & buf, b"hello" ) ;
28+
29+ tx. join ( ) . unwrap ;
30+
31+ // 4. 清理
32+ drop ( listener) ;
33+ let _ = std:: fs:: remove_file ( & sock_path) ;
34+ }
35+
36+ #[ test]
37+ fn echo ( ) {
38+ let tmp = std:: env:: temp_dir ( ) ;
39+ let sock_path = tmp. join ( "rust-test-uds-echo.sock" ) ;
40+
41+ let listener = UnixListener :: bind ( & sock_path) . unwrap ( ) ;
42+
43+ let tx = thread:: spawn ( move || {
44+ let ( mut stream, _) = listener. accept ( ) . unwrap ;
45+ let mut buf = [ 0 ; 1024 ] ;
46+ loop {
47+ let n = match stream. read ( & mut buf) {
48+ Ok ( 0 ) => return ,
49+ Ok ( n) => n,
50+ Err ( e) => panic ! ( "read error: {}" , e) ,
51+ } ;
52+ stream. write_all ( & buf[ ..n] ) . unwrap ;
53+ }
54+ } ) ;
55+
56+ let mut client = UnixStream :: connect ( & sock_path) . unwrap ;
57+ client. write_all ( b"echo" ) . unwrap ;
58+ let mut buf = [ 0 ; 4 ] ;
59+ client. read_exact ( & mut buf) . unwrap ;
60+ assert_eq ! ( & buf, b"echo" ) ;
61+
62+ drop ( client) ;
63+ tx. join ( ) . unwrap ;
64+ let _ = std:: fs:: remove_file ( & sock_path) ;
65+ }
66+
67+ #[ test]
68+ fn path_too_long ( ) {
69+ let long = "\\ \\ ?\\ " . to_string ( ) + & "a" . repeat ( 300 ) ;
70+ assert ! ( UnixListener :: bind( long) . is_err( ) ) ;
71+ }
0 commit comments