Skip to content

Commit 809dd20

Browse files
committed
Rust: Add source tests for tokio (file).
1 parent 307424e commit 809dd20

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
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:451:21:451:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
4747
| test.rs:452:21:452:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
4848
| test.rs:460:21:460:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
49-
| test.rs:471:16:471:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |
49+
| test.rs:522:16:522:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,57 @@ fn test_io_file() -> std::io::Result<()> {
466466
Ok(())
467467
}
468468

469+
async fn test_tokio_file() -> std::io::Result<()> {
470+
// --- file ---
471+
472+
let mut file = tokio::fs::File::open("file.txt").await?; // $ MISSING: Alert[rust/summary/taint-sources]
473+
474+
{
475+
let mut buffer = [0u8; 100];
476+
let _bytes = file.read(&mut buffer).await?;
477+
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt"
478+
}
479+
480+
{
481+
let mut buffer = Vec::<u8>::new();
482+
let _bytes = file.read_to_end(&mut buffer).await?;
483+
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt"
484+
}
485+
486+
{
487+
let mut buffer = String::new();
488+
let _bytes = file.read_to_string(&mut buffer).await?;
489+
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt"
490+
}
491+
492+
{
493+
let mut buffer = [0; 100];
494+
file.read_exact(&mut buffer).await?;
495+
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt"
496+
}
497+
498+
// --- misc operations ---
499+
500+
{
501+
let mut buffer = String::new();
502+
let file1 = tokio::fs::File::open("file.txt").await?; // $ MISSING: Alert[rust/summary/taint-sources]
503+
let file2 = tokio::fs::File::open("another_file.txt").await?; // $ MISSING: [rust/summary/taint-sources]
504+
let mut reader = file1.chain(file2);
505+
reader.read_to_string(&mut buffer).await?;
506+
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt" hasTaintFlow="another_file.txt"
507+
}
508+
509+
{
510+
let mut buffer = String::new();
511+
let file1 = tokio::fs::File::open("file.txt").await?; // $ MISSING: Alert[rust/summary/taint-sources]
512+
let mut reader = file1.take(100);
513+
reader.read_to_string(&mut buffer).await?;
514+
sink(&buffer); // $ MISSING: hasTaintFlow="file.txt"
515+
}
516+
517+
Ok(())
518+
}
519+
469520
#[tokio::main]
470521
async fn main() -> Result<(), Box<dyn std::error::Error>> {
471522
let case = std::env::args().nth(1).unwrap_or(String::from("1")).parse::<i64>().unwrap(); // $ Alert[rust/summary/taint-sources]
@@ -515,5 +566,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
515566
Err(e) => println!("error: {}", e),
516567
}
517568

569+
println!("test_tokio_file...");
570+
match futures::executor::block_on(test_tokio_file()) {
571+
Ok(_) => println!("complete"),
572+
Err(e) => println!("error: {}", e),
573+
}
574+
518575
Ok(())
519576
}

0 commit comments

Comments
 (0)