14 个版本 (9 个破坏性更新)

0.10.0 2024 年 7 月 31 日
0.8.0 2024 年 6 月 27 日
0.6.0-beta.12023 年 12 月 1 日
0.5.0 2023 年 10 月 19 日
0.1.0 2022 年 2 月 18 日

#43 in 测试

Download history 137/week @ 2024-05-01 146/week @ 2024-05-08 84/week @ 2024-05-15 102/week @ 2024-05-22 147/week @ 2024-05-29 133/week @ 2024-06-05 124/week @ 2024-06-12 437/week @ 2024-06-19 469/week @ 2024-06-26 259/week @ 2024-07-03 187/week @ 2024-07-10 384/week @ 2024-07-17 366/week @ 2024-07-24 607/week @ 2024-07-31 289/week @ 2024-08-07 424/week @ 2024-08-14

每月 1,741 次下载

MIT/Apache

1.5MB
35K SLoC

Rust 27K SLoC // 0.0% comments GraphQL 8K SLoC // 0.0% comments Python 3 SLoC

apollo-smith

GraphQL 语言的测试用例生成器。

Crates.io Download docs.rs docs

关于

apollo-smith 的目标是通过对 GraphQL 语法 中所有可能的采样来生成有效的 GraphQL 文档。

我们编写 apollo-smith 以用于模糊测试,但您可能希望将其用于需要生成 GraphQL 文档的任何事物。

apollo-smith 受字节码联盟的 wasm-smith crate 启发,以及 Nick Fitzgerald 撰写的关于在 Rust 中编写测试用例生成器的 文章

此项目仍在进行中,有关待解决的问题,请在我们的问题跟踪器中查看 apollo-smith 标签

Rust 版本

apollo-smith 在最新稳定版本的 Rust 上进行了测试。旧版本可能不兼容。

使用 apollo-smithcargo fuzz

使用 cargo fuzz 定义新目标

$ cargo fuzz add my_apollo_smith_fuzz_target

并将 apollo-smith 添加到您的 Cargo.toml

## fuzz/Cargo.toml

[dependencies]
apollo-smith = "0.10.0"

然后,您可以使用 fuzz_target 以及 arbitrary crate 来使用它

// fuzz/fuzz_targets/my_apollo_smith_fuzz_target.rs

#![no_main]

use libfuzzer_sys::fuzz_target;
use arbitrary::Unstructured;
use apollo_smith::DocumentBuilder;

fuzz_target!(|input: &[u8]| {
    let mut u = Unstructured::new(input);
    let gql_doc = DocumentBuilder::new(&mut u)?;
    let document = gql_doc.finish();
    let document_str = String::from(document);


});

并使用以下命令进行模糊测试

$ cargo +nightly fuzz run my_apollo_smith_fuzz_target

使用 apollo-smithapollo-parser

您可以使用 apollo-parser 来生成在 apollo-smith 中有效的操作。

use std::fs;

use apollo_parser::Parser;
use apollo_smith::{Document, DocumentBuilder};

use libfuzzer_sys::arbitrary::{Result, Unstructured};

/// This generate an arbitrary valid GraphQL operation
pub fn generate_valid_operation(input: &[u8]) -> Result<String> {
    let parser = Parser::new(&fs::read_to_string("supergraph.graphql").expect("cannot read file"));

    let tree = parser.parse();
    if tree.errors().next().is_some() {
        panic!("cannot parse the graphql file");
    }

    let mut u = Unstructured::new(input);

    // Convert `apollo_parser::Document` into `apollo_smith::Document`.
    let apollo_smith_doc = Document::try_from(tree.document()).unwrap();

    // Create a `DocumentBuilder` given an existing document to match a schema.
    let mut gql_doc = DocumentBuilder::with_document(&mut u, apollo_smith_doc)?;
    let operation_def = gql_doc.operation_definition()?.unwrap();

    Ok(operation_def.into())
}

限制

  • 递归对象类型尚未支持(例如: myType { inner: myType }

许可证

根据以下任一许可证授权:

由您选择。

依赖项

~7–10MB
~177K SLoC