Skip to content

Commit 950812b

Browse files
committed
Rust: Add further source tests for tcp streams.
1 parent 3839719 commit 950812b

File tree

2 files changed

+96
-8
lines changed

2 files changed

+96
-8
lines changed

rust/ql/test/library-tests/dataflow/sources/TaintSources.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@
4646
| test.rs:470:21:470:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
4747
| test.rs:471:21:471:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
4848
| test.rs:479:21:479:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
49-
| test.rs:657:16:657:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |
49+
| test.rs:739:16:739:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |

rust/ql/test/library-tests/dataflow/sources/test.rs

Lines changed: 95 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ async fn test_std_tcpstream(case: i64) -> std::io::Result<()> { // Result<(), Bo
626626

627627
use tokio::io::AsyncWriteExt;
628628

629-
async fn test_tokio_tcpstream() -> std::io::Result<()> {
629+
async fn test_tokio_tcpstream(case: i64) -> std::io::Result<()> {
630630
// using tokio::io to fetch a web page
631631
let address = "example.com:80";
632632

@@ -637,18 +637,100 @@ async fn test_tokio_tcpstream() -> std::io::Result<()> {
637637
// send request
638638
tokio_stream.write_all(b"GET / HTTP/1.1\nHost:example.com\n\n").await?;
639639

640+
if case == 1 {
641+
// peek response
642+
let mut buffer1 = vec![0; 2 * 1024];
643+
let _ = tokio_stream.peek(&mut buffer1).await?; // $ MISSING: Alert[rust/summary/taint-sources]
644+
645+
// read response
646+
let mut buffer2 = vec![0; 2 * 1024];
647+
let n2 = tokio_stream.read(&mut buffer2).await?; // $ MISSING: Alert[rust/summary/taint-sources]
648+
649+
println!("buffer1 = {:?}", buffer1);
650+
sink(&buffer1); // $ MISSING: hasTaintFlow
651+
sink(buffer1[0]); // $ MISSING: hasTaintFlow
652+
653+
println!("buffer2 = {:?}", buffer2);
654+
sink(&buffer2); // $ MISSING: hasTaintFlow
655+
sink(buffer2[0]); // $ MISSING: hasTaintFlow
656+
657+
let buffer_string = String::from_utf8_lossy(&buffer2[..n2]);
658+
println!("string = {}", buffer_string);
659+
sink(buffer_string); // $ MISSING: hasTaintFlow
660+
} else if case == 2 {
661+
let mut buffer = [0; 2 * 1024];
662+
loop {
663+
match tokio_stream.try_read(&mut buffer) {
664+
Ok(0) => {
665+
println!("end");
666+
break;
667+
}
668+
Ok(_n) => {
669+
println!("buffer = {:?}", buffer);
670+
sink(&buffer); // $ MISSING: hasTaintFlow
671+
break; // (or we could wait for more data)
672+
}
673+
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
674+
// wait...
675+
continue;
676+
}
677+
Err(e) => {
678+
println!("error: {}", e);
679+
break;
680+
}
681+
}
682+
}
683+
} else {
684+
let mut buffer = Vec::new();
685+
loop {
686+
match tokio_stream.try_read_buf(&mut buffer) {
687+
Ok(0) => {
688+
println!("end");
689+
break;
690+
}
691+
Ok(_n) => {
692+
println!("buffer = {:?}", buffer);
693+
sink(&buffer); // $ MISSING: hasTaintFlow
694+
break; // (or we could wait for more data)
695+
}
696+
Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
697+
// wait...
698+
continue;
699+
}
700+
Err(e) => {
701+
println!("error: {}", e);
702+
break;
703+
}
704+
}
705+
}
706+
}
707+
708+
Ok(())
709+
}
710+
711+
async fn test_std_to_tokio_tcpstream() -> std::io::Result<()> {
712+
// using tokio::io to fetch a web page
713+
let address = "example.com:80";
714+
715+
// create the connection
716+
println!("connecting to {}...", address);
717+
let std_stream = std::net::TcpStream::connect(address)?;
718+
719+
// convert to tokio stream
720+
std_stream.set_nonblocking(true)?;
721+
let mut tokio_stream = tokio::net::TcpStream::from_std(std_stream)?;
722+
723+
// send request
724+
tokio_stream.write_all(b"GET / HTTP/1.1\nHost:example.com\n\n").await?;
725+
640726
// read response
641727
let mut buffer = vec![0; 32 * 1024];
642-
let n = tokio_stream.read(&mut buffer).await?; // $ MISSING: Alert[rust/summary/taint-sources]
728+
let _n = tokio_stream.read(&mut buffer).await?; // $ MISSING: Alert[rust/summary/taint-sources]
643729

644730
println!("data = {:?}", buffer);
645731
sink(&buffer); // $ MISSING: hasTaintFlow
646732
sink(buffer[0]); // $ MISSING: hasTaintFlow
647733

648-
let buffer_string = String::from_utf8_lossy(&buffer[..n]);
649-
println!("string = {}", buffer_string);
650-
sink(buffer_string); // $ MISSING: hasTaintFlow
651-
652734
Ok(())
653735
}
654736

@@ -714,7 +796,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
714796
}
715797

716798
println!("test_tokio_tcpstream...");
717-
match futures::executor::block_on(test_tokio_tcpstream()) {
799+
match futures::executor::block_on(test_tokio_tcpstream(case)) {
800+
Ok(_) => println!("complete"),
801+
Err(e) => println!("error: {}", e),
802+
}
803+
804+
println!("test_std_to_tokio_tcpstream...");
805+
match futures::executor::block_on(test_std_to_tokio_tcpstream()) {
718806
Ok(_) => println!("complete"),
719807
Err(e) => println!("error: {}", e),
720808
}

0 commit comments

Comments
 (0)