Skip to content

Commit dcc488c

Browse files
committed
Rust: Clean up the sources test.
1 parent 008dd3f commit dcc488c

File tree

2 files changed

+146
-110
lines changed

2 files changed

+146
-110
lines changed

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,30 @@
2020
| test.rs:74:26:74:37 | ...::get | Flow source 'RemoteSource' of type remote (DEFAULT). |
2121
| test.rs:77:26:77:37 | ...::get | Flow source 'RemoteSource' of type remote (DEFAULT). |
2222
| test.rs:80:24:80:35 | ...::get | Flow source 'RemoteSource' of type remote (DEFAULT). |
23-
| test.rs:112:35:112:46 | send_request | Flow source 'RemoteSource' of type remote (DEFAULT). |
23+
| test.rs:112:31:112:42 | send_request | Flow source 'RemoteSource' of type remote (DEFAULT). |
2424
| test.rs:119:31:119:42 | send_request | Flow source 'RemoteSource' of type remote (DEFAULT). |
25-
| test.rs:205:31:205:43 | ...::read | Flow source 'FileSource' of type file (DEFAULT). |
26-
| test.rs:210:31:210:38 | ...::read | Flow source 'FileSource' of type file (DEFAULT). |
27-
| test.rs:215:22:215:39 | ...::read_to_string | Flow source 'FileSource' of type file (DEFAULT). |
28-
| test.rs:221:22:221:25 | path | Flow source 'FileSource' of type file (DEFAULT). |
29-
| test.rs:222:27:222:35 | file_name | Flow source 'FileSource' of type file (DEFAULT). |
30-
| test.rs:228:22:228:34 | ...::read_link | Flow source 'FileSource' of type file (DEFAULT). |
31-
| test.rs:243:22:243:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
32-
| test.rs:249:22:249:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
33-
| test.rs:255:22:255:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
34-
| test.rs:261:9:261:22 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
35-
| test.rs:265:17:265:30 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
36-
| test.rs:271:20:271:38 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
37-
| test.rs:304:50:304:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
38-
| test.rs:310:50:310:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
39-
| test.rs:317:50:317:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
40-
| test.rs:324:50:324:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
41-
| test.rs:331:56:331:69 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
42-
| test.rs:338:50:338:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
43-
| test.rs:345:50:345:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
44-
| test.rs:351:50:351:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
45-
| test.rs:360:25:360:43 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
46-
| test.rs:361:25:361:43 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
47-
| test.rs:369:25:369:43 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
48-
| test.rs:377:22:377:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
49-
| test.rs:386:16:386:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |
25+
| test.rs:209:22:209:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
26+
| test.rs:215:22:215:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
27+
| test.rs:221:22:221:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
28+
| test.rs:227:22:227:35 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
29+
| test.rs:233:9:233:22 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
30+
| test.rs:237:17:237:30 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
31+
| test.rs:244:50:244:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
32+
| test.rs:250:46:250:59 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
33+
| test.rs:257:50:257:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
34+
| test.rs:264:50:264:63 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
35+
| test.rs:271:56:271:69 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
36+
| test.rs:278:46:278:59 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
37+
| test.rs:285:46:285:59 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
38+
| test.rs:291:46:291:59 | ...::stdin | Flow source 'StdInSource' of type stdin (DEFAULT). |
39+
| test.rs:303:31:303:43 | ...::read | Flow source 'FileSource' of type file (DEFAULT). |
40+
| test.rs:308:31:308:38 | ...::read | Flow source 'FileSource' of type file (DEFAULT). |
41+
| test.rs:313:22:313:39 | ...::read_to_string | Flow source 'FileSource' of type file (DEFAULT). |
42+
| test.rs:319:22:319:25 | path | Flow source 'FileSource' of type file (DEFAULT). |
43+
| test.rs:320:27:320:35 | file_name | Flow source 'FileSource' of type file (DEFAULT). |
44+
| test.rs:326:22:326:34 | ...::read_link | Flow source 'FileSource' of type file (DEFAULT). |
45+
| test.rs:336:20:336:38 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
46+
| test.rs:370:21:370:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
47+
| test.rs:371:21:371:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
48+
| test.rs:379:21:379:39 | ...::open | Flow source 'FileSource' of type file (DEFAULT). |
49+
| test.rs:390:16:390:29 | ...::args | Flow source 'CommandLineArgs' of type commandargs (DEFAULT). |

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

Lines changed: 120 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ async fn test_hyper_http(case: i64) -> Result<(), Box<dyn std::error::Error>> {
106106

107107
// make the request
108108
println!("sending request...");
109-
if (case == 0) {
109+
if case == 0 {
110110
// simple flow case
111111
let request = http::Request::builder().uri(url).body(String::from(""))?;
112-
let mut response = sender.send_request(request).await?; // $ Alert[rust/summary/taint-sources]
112+
let response = sender.send_request(request).await?; // $ Alert[rust/summary/taint-sources]
113113
sink(&response); // $ hasTaintFlow=request
114114
sink(response); // $ hasTaintFlow=request
115115
return Ok(())
@@ -198,44 +198,10 @@ async fn test_hyper_http(case: i64) -> Result<(), Box<dyn std::error::Error>> {
198198
Ok(())
199199
}
200200

201-
use std::fs;
202-
203-
fn test_fs() -> Result<(), Box<dyn std::error::Error>> {
204-
{
205-
let buffer: Vec<u8> = std::fs::read("file.bin")?; // $ Alert[rust/summary/taint-sources]
206-
sink(buffer); // $ hasTaintFlow="file.bin"
207-
}
208-
209-
{
210-
let buffer: Vec<u8> = fs::read("file.bin")?; // $ Alert[rust/summary/taint-sources]
211-
sink(buffer); // $ hasTaintFlow="file.bin"
212-
}
213-
214-
{
215-
let buffer = fs::read_to_string("file.txt")?; // $ Alert[rust/summary/taint-sources]
216-
sink(buffer); // $ hasTaintFlow="file.txt"
217-
}
218-
219-
for entry in fs::read_dir("directory")? {
220-
let e = entry?;
221-
let path = e.path(); // $ Alert[rust/summary/taint-sources]
222-
let file_name = e.file_name(); // $ Alert[rust/summary/taint-sources]
223-
sink(path); // $ hasTaintFlow
224-
sink(file_name); // $ hasTaintFlow
225-
}
226-
227-
{
228-
let target = fs::read_link("symlink.txt")?; // $ Alert[rust/summary/taint-sources]
229-
sink(target); // $ hasTaintFlow="symlink.txt"
230-
}
231-
232-
Ok(())
233-
}
234-
235201
use std::io::Read;
236202
use std::io::BufRead;
237203

238-
fn test_io_fs() -> std::io::Result<()> {
204+
fn test_io_stdin() -> std::io::Result<()> {
239205
// --- stdin ---
240206

241207
{
@@ -256,46 +222,20 @@ fn test_io_fs() -> std::io::Result<()> {
256222
sink(&buffer); // $ hasTaintFlow
257223
}
258224

259-
{
260-
let mut buffer = [0; 100];
261-
std::io::stdin().read_exact(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
262-
sink(&buffer); // $ hasTaintFlow
263-
}
264-
265-
for byte in std::io::stdin().bytes() { // $ Alert[rust/summary/taint-sources]
266-
sink(byte); // $ hasTaintFlow
267-
}
268-
269-
// --- file ---
270-
271-
let mut file = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
272-
273-
{
274-
let mut buffer = [0u8; 100];
275-
let _bytes = file.read(&mut buffer)?;
276-
sink(&buffer); // $ hasTaintFlow="file.txt"
277-
}
278-
279-
{
280-
let mut buffer = Vec::<u8>::new();
281-
let _bytes = file.read_to_end(&mut buffer)?;
282-
sink(&buffer); // $ hasTaintFlow="file.txt"
283-
}
284-
285225
{
286226
let mut buffer = String::new();
287-
let _bytes = file.read_to_string(&mut buffer)?;
288-
sink(&buffer); // $ hasTaintFlow="file.txt"
227+
let _bytes = std::io::stdin().lock().read_to_string(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
228+
sink(&buffer); // $ hasTaintFlow
289229
}
290230

291231
{
292232
let mut buffer = [0; 100];
293-
file.read_exact(&mut buffer)?;
294-
sink(&buffer); // $ hasTaintFlow="file.txt"
233+
std::io::stdin().read_exact(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
234+
sink(&buffer); // $ hasTaintFlow
295235
}
296236

297-
for byte in file.bytes() {
298-
sink(byte); // $ hasTaintFlow="file.txt"
237+
for byte in std::io::stdin().bytes() { // $ Alert[rust/summary/taint-sources]
238+
sink(byte); // $ hasTaintFlow
299239
}
300240

301241
// --- BufReader ---
@@ -307,7 +247,7 @@ fn test_io_fs() -> std::io::Result<()> {
307247
}
308248

309249
{
310-
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
250+
let reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
311251
let data = reader.buffer();
312252
sink(&data); // $ hasTaintFlow
313253
}
@@ -324,73 +264,169 @@ fn test_io_fs() -> std::io::Result<()> {
324264
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
325265
reader.read_until(b',', &mut buffer)?;
326266
sink(&buffer); // $ hasTaintFlow
267+
sink(buffer[0]); // $ hasTaintFlow
327268
}
328269

329270
{
330-
let mut buffer = Vec::<u8>::new();
331271
let mut reader_split = std::io::BufReader::new(std::io::stdin()).split(b','); // $ Alert[rust/summary/taint-sources]
332272
while let Some(chunk) = reader_split.next() {
333273
sink(chunk.unwrap()); // $ MISSING: hasTaintFlow
334274
}
335275
}
336276

337277
{
338-
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
278+
let reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
339279
for line in reader.lines() {
340280
sink(line); // $ hasTaintFlow
341281
}
342282
}
343283

344284
{
345-
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
285+
let reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
346286
let line = reader.lines().nth(1).unwrap();
347287
sink(line.unwrap().clone()); // $ MISSING: hasTaintFlow
348288
}
349289

350290
{
351-
let mut reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
291+
let reader = std::io::BufReader::new(std::io::stdin()); // $ Alert[rust/summary/taint-sources]
352292
let lines: Vec<_> = reader.lines().collect();
353293
sink(lines[1].as_ref().unwrap().clone()); // $ MISSING: hasTaintFlow
354294
}
355295

296+
Ok(())
297+
}
298+
299+
use std::fs;
300+
301+
fn test_fs() -> Result<(), Box<dyn std::error::Error>> {
302+
{
303+
let buffer: Vec<u8> = std::fs::read("file.bin")?; // $ Alert[rust/summary/taint-sources]
304+
sink(buffer); // $ hasTaintFlow="file.bin"
305+
}
306+
307+
{
308+
let buffer: Vec<u8> = fs::read("file.bin")?; // $ Alert[rust/summary/taint-sources]
309+
sink(buffer); // $ hasTaintFlow="file.bin"
310+
}
311+
312+
{
313+
let buffer = fs::read_to_string("file.txt")?; // $ Alert[rust/summary/taint-sources]
314+
sink(buffer); // $ hasTaintFlow="file.txt"
315+
}
316+
317+
for entry in fs::read_dir("directory")? {
318+
let e = entry?;
319+
let path = e.path(); // $ Alert[rust/summary/taint-sources]
320+
let file_name = e.file_name(); // $ Alert[rust/summary/taint-sources]
321+
sink(path); // $ hasTaintFlow
322+
sink(file_name); // $ hasTaintFlow
323+
}
324+
325+
{
326+
let target = fs::read_link("symlink.txt")?; // $ Alert[rust/summary/taint-sources]
327+
sink(target); // $ hasTaintFlow="symlink.txt"
328+
}
329+
330+
Ok(())
331+
}
332+
333+
fn test_io_file() -> std::io::Result<()> {
334+
// --- file ---
335+
336+
let mut file = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
337+
338+
{
339+
let mut buffer = [0u8; 100];
340+
let _bytes = file.read(&mut buffer)?;
341+
sink(&buffer); // $ hasTaintFlow="file.txt"
342+
}
343+
344+
{
345+
let mut buffer = Vec::<u8>::new();
346+
let _bytes = file.read_to_end(&mut buffer)?;
347+
sink(&buffer); // $ hasTaintFlow="file.txt"
348+
}
349+
350+
{
351+
let mut buffer = String::new();
352+
let _bytes = file.read_to_string(&mut buffer)?;
353+
sink(&buffer); // $ hasTaintFlow="file.txt"
354+
}
355+
356+
{
357+
let mut buffer = [0; 100];
358+
file.read_exact(&mut buffer)?;
359+
sink(&buffer); // $ hasTaintFlow="file.txt"
360+
}
361+
362+
for byte in file.bytes() {
363+
sink(byte); // $ hasTaintFlow="file.txt"
364+
}
365+
356366
// --- misc operations ---
357367

358368
{
359369
let mut buffer = String::new();
360-
let mut file1 = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
361-
let mut file2 = std::fs::File::open("another_file.txt")?; // $ Alert[rust/summary/taint-sources]
370+
let file1 = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
371+
let file2 = std::fs::File::open("another_file.txt")?; // $ Alert[rust/summary/taint-sources]
362372
let mut reader = file1.chain(file2);
363373
reader.read_to_string(&mut buffer)?;
364374
sink(&buffer); // $ hasTaintFlow="file.txt" hasTaintFlow="another_file.txt"
365375
}
366376

367377
{
368378
let mut buffer = String::new();
369-
let mut file1 = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
379+
let file1 = std::fs::File::open("file.txt")?; // $ Alert[rust/summary/taint-sources]
370380
let mut reader = file1.take(100);
371381
reader.read_to_string(&mut buffer)?;
372382
sink(&buffer); // $ hasTaintFlow="file.txt"
373383
}
374384

375-
{
376-
let mut buffer = String::new();
377-
let _bytes = std::io::stdin().lock().read_to_string(&mut buffer)?; // $ Alert[rust/summary/taint-sources]
378-
sink(&buffer); // $ hasTaintFlow
379-
}
380-
381385
Ok(())
382386
}
383387

384388
#[tokio::main]
385389
async fn main() -> Result<(), Box<dyn std::error::Error>> {
386390
let case = std::env::args().nth(1).unwrap_or(String::from("1")).parse::<i64>().unwrap(); // $ Alert[rust/summary/taint-sources]
387391

392+
println!("test_env_vars...");
393+
test_env_vars();
394+
395+
/*println!("test_env_args...");
396+
test_env_args();*/
397+
398+
println!("test_env_dirs...");
399+
test_env_dirs();
400+
401+
/*println!("test_reqwest...");
402+
match futures::executor::block_on(test_reqwest()) {
403+
Ok(_) => println!("complete"),
404+
Err(e) => println!("error: {}", e),
405+
}*/
406+
388407
println!("test_hyper_http...");
389408
match futures::executor::block_on(test_hyper_http(case)) {
390409
Ok(_) => println!("complete"),
391410
Err(e) => println!("error: {}", e),
392411
}
393-
println!("");
412+
413+
/*println!("test_io_stdin...");
414+
match test_io_stdin() {
415+
Ok(_) => println!("complete"),
416+
Err(e) => println!("error: {}", e),
417+
}*/
418+
419+
println!("test_fs...");
420+
match test_fs() {
421+
Ok(_) => println!("complete"),
422+
Err(e) => println!("error: {}", e),
423+
}
424+
425+
println!("test_io_file...");
426+
match test_io_file() {
427+
Ok(_) => println!("complete"),
428+
Err(e) => println!("error: {}", e),
429+
}
394430

395431
Ok(())
396432
}

0 commit comments

Comments
 (0)