mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 03:27:44 +00:00
fuzz: Move a duplicate function into fuzz_common
This commit is contained in:
parent
5f9f610e3d
commit
fddf301a52
3 changed files with 31 additions and 48 deletions
|
@ -4,6 +4,8 @@
|
||||||
// file that was distributed with this source code.
|
// file that was distributed with this source code.
|
||||||
|
|
||||||
use libc::{close, dup, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};
|
use libc::{close, dup, dup2, pipe, STDERR_FILENO, STDOUT_FILENO};
|
||||||
|
use rand::prelude::SliceRandom;
|
||||||
|
use rand::Rng;
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
@ -272,3 +274,26 @@ pub fn compare_result(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn generate_random_string(max_length: usize) -> String {
|
||||||
|
let mut rng = rand::thread_rng();
|
||||||
|
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||||
|
.chars()
|
||||||
|
.collect();
|
||||||
|
let invalid_utf8 = [0xC3, 0x28]; // Invalid UTF-8 sequence
|
||||||
|
let mut result = String::new();
|
||||||
|
|
||||||
|
for _ in 0..rng.gen_range(1..=max_length) {
|
||||||
|
if rng.gen_bool(0.9) {
|
||||||
|
let ch = valid_utf8.choose(&mut rng).unwrap();
|
||||||
|
result.push(*ch);
|
||||||
|
} else {
|
||||||
|
let ch = invalid_utf8.choose(&mut rng).unwrap();
|
||||||
|
if let Some(c) = char::from_u32(*ch as u32) {
|
||||||
|
result.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
|
@ -14,32 +14,11 @@ use std::{env, ffi::OsString};
|
||||||
|
|
||||||
mod fuzz_common;
|
mod fuzz_common;
|
||||||
use crate::fuzz_common::CommandResult;
|
use crate::fuzz_common::CommandResult;
|
||||||
use crate::fuzz_common::{compare_result, generate_and_run_uumain, run_gnu_cmd};
|
use crate::fuzz_common::{
|
||||||
|
compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd,
|
||||||
|
};
|
||||||
static CMD_PATH: &str = "expr";
|
static CMD_PATH: &str = "expr";
|
||||||
|
|
||||||
fn generate_random_string(max_length: usize) -> String {
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
||||||
.chars()
|
|
||||||
.collect();
|
|
||||||
let invalid_utf8 = [0xC3, 0x28]; // Invalid UTF-8 sequence
|
|
||||||
let mut result = String::new();
|
|
||||||
|
|
||||||
for _ in 0..rng.gen_range(1..=max_length) {
|
|
||||||
if rng.gen_bool(0.9) {
|
|
||||||
let ch = valid_utf8.choose(&mut rng).unwrap();
|
|
||||||
result.push(*ch);
|
|
||||||
} else {
|
|
||||||
let ch = invalid_utf8.choose(&mut rng).unwrap();
|
|
||||||
if let Some(c) = char::from_u32(*ch as u32) {
|
|
||||||
result.push(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
fn generate_expr(max_depth: u32) -> String {
|
fn generate_expr(max_depth: u32) -> String {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
let ops = ["+", "-", "*", "/", "%", "<", ">", "=", "&", "|"];
|
let ops = ["+", "-", "*", "/", "%", "<", ">", "=", "&", "|"];
|
||||||
|
|
|
@ -14,7 +14,9 @@ use std::ffi::OsString;
|
||||||
|
|
||||||
mod fuzz_common;
|
mod fuzz_common;
|
||||||
use crate::fuzz_common::CommandResult;
|
use crate::fuzz_common::CommandResult;
|
||||||
use crate::fuzz_common::{compare_result, generate_and_run_uumain, run_gnu_cmd};
|
use crate::fuzz_common::{
|
||||||
|
compare_result, generate_and_run_uumain, generate_random_string, run_gnu_cmd,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Clone)]
|
#[derive(PartialEq, Debug, Clone)]
|
||||||
enum ArgType {
|
enum ArgType {
|
||||||
|
@ -29,29 +31,6 @@ enum ArgType {
|
||||||
|
|
||||||
static CMD_PATH: &str = "test";
|
static CMD_PATH: &str = "test";
|
||||||
|
|
||||||
fn generate_random_string(max_length: usize) -> String {
|
|
||||||
let mut rng = rand::thread_rng();
|
|
||||||
let valid_utf8: Vec<char> = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
|
||||||
.chars()
|
|
||||||
.collect();
|
|
||||||
let invalid_utf8 = [0xC3, 0x28]; // Invalid UTF-8 sequence
|
|
||||||
let mut result = String::new();
|
|
||||||
|
|
||||||
for _ in 0..rng.gen_range(1..=max_length) {
|
|
||||||
if rng.gen_bool(0.9) {
|
|
||||||
let ch = valid_utf8.choose(&mut rng).unwrap();
|
|
||||||
result.push(*ch);
|
|
||||||
} else {
|
|
||||||
let ch = invalid_utf8.choose(&mut rng).unwrap();
|
|
||||||
if let Some(c) = char::from_u32(*ch as u32) {
|
|
||||||
result.push(c);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct TestArg {
|
struct TestArg {
|
||||||
arg: String,
|
arg: String,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue