trident_cli/
lib.rs

1use anyhow::Error;
2use clap::Parser;
3use clap::Subcommand;
4
5use fehler::throws;
6
7mod command;
8
9use crate::command::FuzzCommand;
10
11#[derive(Parser)]
12#[command(
13    name = "Trident",
14    about = "Trident is Rust based fuzzer for Solana programs written using Anchor framework.",
15    version = env!("CARGO_PKG_VERSION")
16)]
17struct Cli {
18    #[clap(subcommand)]
19    command: Command,
20}
21
22#[derive(Subcommand)]
23enum Command {
24    #[command(about = "Show the HowTo message.")]
25    How,
26    #[command(
27        about = "Initialize Trident in the current Anchor workspace.",
28        override_usage = "\nTrident will skip initialization if Trident.toml already exists."
29    )]
30    Init {
31        #[arg(
32            short,
33            long,
34            required = false,
35            help = "Force Trident initialization. Trident dependencies will be updated based on the version of Trident CLI."
36        )]
37        force: bool,
38        #[arg(
39            short,
40            long,
41            required = false,
42            help = "Skip building the program before initializing Trident."
43        )]
44        skip_build: bool,
45        #[arg(
46            short,
47            long,
48            required = false,
49            help = "Specify the name of the program for which fuzz test will be generated.",
50            value_name = "FILE"
51        )]
52        program_name: Option<String>,
53        #[arg(
54            short,
55            long,
56            required = false,
57            help = "Name of the fuzz test to initialize.",
58            value_name = "NAME"
59        )]
60        test_name: Option<String>,
61    },
62    #[command(
63        about = "Run fuzz subcommands.",
64        override_usage = "With fuzz subcommands you can add new fuzz test \
65        template or you can run fuzz test on already initialzied one.\
66        \n\n\x1b[1m\x1b[4mEXAMPLE:\x1b[0m\
67        \n    trident fuzz add\
68        \n    trident fuzz run fuzz_0\
69        \n    trident fuzz debug \x1b[92m<FUZZ_TARGET>\x1b[0m \x1b[92m<SEED>\x1b[0m\
70        \n    trident fuzz refresh fuzz_0"
71    )]
72    Fuzz {
73        #[clap(subcommand)]
74        subcmd: FuzzCommand,
75    },
76    #[command(about = "Clean build target, additionally perform `anchor clean`")]
77    Clean,
78    #[command(about = "Start HTTP server to serve fuzzing dashboards")]
79    Server {
80        #[arg(
81            short,
82            long,
83            required = false,
84            help = "Directory to monitor for dashboard files",
85            value_name = "DIR",
86            default_value = ".fuzz-artifacts"
87        )]
88        directory: String,
89        #[arg(
90            short,
91            long,
92            required = false,
93            help = "Port to run the server on",
94            value_name = "PORT",
95            default_value = "8000"
96        )]
97        port: u16,
98        #[arg(
99            long,
100            required = false,
101            help = "Host to bind the server to",
102            value_name = "HOST",
103            default_value = "localhost"
104        )]
105        host: String,
106    },
107    #[command(about = "Compare two regression JSON files and identify differing iteration seeds")]
108    Compare {
109        #[arg(help = "Path to the first regression JSON file", value_name = "FILE1")]
110        file1: String,
111        #[arg(help = "Path to the second regression JSON file", value_name = "FILE2")]
112        file2: String,
113    },
114}
115
116#[throws]
117pub async fn start() {
118    let cli = Cli::parse();
119
120    match cli.command {
121        Command::How => command::howto()?,
122        Command::Fuzz { subcmd } => command::fuzz(subcmd).await?,
123        Command::Init {
124            force,
125            skip_build,
126            program_name,
127            test_name,
128        } => command::init(force, skip_build, program_name, test_name).await?,
129        Command::Clean => command::clean().await?,
130        Command::Server {
131            directory,
132            port,
133            host,
134        } => command::server(directory, port, host).await?,
135        Command::Compare { file1, file2 } => command::compare_regression(file1, file2)?,
136    }
137}