14 个版本 (9 个破坏性更新)
0.10.0 | 2024 年 7 月 31 日 |
---|---|
0.8.0 | 2024 年 6 月 27 日 |
0.6.0-beta.1 | 2023 年 12 月 1 日 |
0.5.0 | 2023 年 10 月 19 日 |
0.1.0 | 2022 年 2 月 18 日 |
#43 in 测试
每月 1,741 次下载
1.5MB
35K SLoC
关于
apollo-smith
的目标是通过对 GraphQL 语法 中所有可能的采样来生成有效的 GraphQL 文档。
我们编写 apollo-smith
以用于模糊测试,但您可能希望将其用于需要生成 GraphQL 文档的任何事物。
apollo-smith
受字节码联盟的 wasm-smith
crate 启发,以及 Nick Fitzgerald 撰写的关于在 Rust 中编写测试用例生成器的 文章。
此项目仍在进行中,有关待解决的问题,请在我们的问题跟踪器中查看 apollo-smith 标签。
Rust 版本
apollo-smith
在最新稳定版本的 Rust 上进行了测试。旧版本可能不兼容。
使用 apollo-smith
与 cargo 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-smith
与 apollo-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 }
)
许可证
根据以下任一许可证授权:
- Apache License, Version 2.0 (LICENSE-APACHE 或 https://apache.ac.cn/licenses/LICENSE-2.0)
- MIT 许可证 (LICENSE-MIT 或 https://opensource.org/licenses/MIT)
由您选择。
依赖项
~7–10MB
~177K SLoC