Skip to content

Commit

Permalink
test: Add fuzzing
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Dygalo <[email protected]>
  • Loading branch information
Stranger6667 committed Sep 14, 2024
1 parent 90f2b11 commit 1d48eaf
Show file tree
Hide file tree
Showing 14 changed files with 342 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
26 changes: 26 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "benchmark-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.4"
jsonschema = { path = "../crates/jsonschema/" }
serde_json = "1"

[workspace]
members = ["."]

[profile.release]
debug = true

[[bin]]
name = "compile"
path = "fuzz_targets/compile.rs"
test = false
doc = false
bench = false
95 changes: 95 additions & 0 deletions fuzz/dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# JSON Schema keywords
"$schema"
"$id"
"type"
"properties"
"required"
"additionalProperties"
"items"
"allOf"
"anyOf"
"oneOf"
"not"
"definitions"
"$ref"
"enum"
"const"
"multipleOf"
"maximum"
"exclusiveMaximum"
"minimum"
"exclusiveMinimum"
"maxLength"
"minLength"
"pattern"
"maxItems"
"minItems"
"uniqueItems"
"maxProperties"
"minProperties"
"dependencies"
"propertyNames"
"if"
"then"
"else"
"format"

# Data types
"object"
"array"
"string"
"number"
"integer"
"boolean"
"null"

# Common patterns
"^[a-zA-Z0-9_]+$"
"^[0-9]{4}-[0-9]{2}-[0-9]{2}$"

# Special characters
"{"
"}"
"["
"]"
":"
","

# Common values
"true"
"false"
"null"
"0"
"1"
"-1"
"3.14"

# URI references
"#"
"#/definitions/"
"#/properties/"

# Format specifiers
"date-time"
"date"
"time"
"email"
"idn-email"
"hostname"
"idn-hostname"
"ipv4"
"ipv6"
"uri"
"uri-reference"
"iri"
"iri-reference"
"uuid"

# Common property names
"id"
"name"
"title"
"description"
"default"
"examples"
"$comment"
9 changes: 9 additions & 0 deletions fuzz/fuzz_targets/compile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]
use libfuzzer_sys::fuzz_target;

fuzz_target!(|data: &[u8]| {
let Ok(schema) = serde_json::from_slice(data) else {
return;
};
let _ = jsonschema::compile(&schema);
});
14 changes: 14 additions & 0 deletions fuzz/seeds/compile/array
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"items": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"value": { "type": "string" }
},
"required": ["id", "value"]
},
"minItems": 1,
"uniqueItems": true
}
16 changes: 16 additions & 0 deletions fuzz/seeds/compile/combinators
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"allOf": [
{ "properties": { "name": { "type": "string" } } },
{ "properties": { "age": { "type": "integer", "minimum": 0 } } }
],
"anyOf": [
{ "properties": { "email": { "type": "string", "format": "email" } } },
{ "properties": { "phone": { "type": "string", "pattern": "^\\+?[0-9]{10,14}$" } } }
],
"oneOf": [
{ "properties": { "type": { "const": "user" } } },
{ "properties": { "type": { "const": "admin" } } }
]
}
13 changes: 13 additions & 0 deletions fuzz/seeds/compile/enum
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"color": { "enum": ["red", "green", "blue"] },
"version": { "const": "1.0" },
"features": {
"type": "array",
"items": { "type": "string" }
}
},
"required": ["color", "version"]
}
18 changes: 18 additions & 0 deletions fuzz/seeds/compile/nested-object
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"user": {
"type": "object",
"properties": {
"id": { "type": "integer" },
"username": { "type": "string", "pattern": "^[a-z0-9_]{3,16}$" }
},
"required": ["id", "username"]
}
},
"patternProperties": {
"^data_": { "type": "string" }
},
"additionalProperties": false
}
10 changes: 10 additions & 0 deletions fuzz/seeds/compile/object
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "age"]
}
34 changes: 34 additions & 0 deletions fuzz/seeds/compile/ref-combo
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"positiveInteger": {
"type": "integer",
"minimum": 1
},
"nonEmptyString": {
"type": "string",
"minLength": 1
}
},
"properties": {
"id": { "$ref": "#/definitions/positiveInteger" },
"name": { "$ref": "#/definitions/nonEmptyString" },
"tags": {
"type": "array",
"items": { "$ref": "#/definitions/nonEmptyString" },
"uniqueItems": true
},
"metadata": {
"type": "object",
"propertyNames": { "$ref": "#/definitions/nonEmptyString" },
"additionalProperties": {
"oneOf": [
{ "$ref": "#/definitions/positiveInteger" },
{ "$ref": "#/definitions/nonEmptyString" }
]
}
}
},
"required": ["id", "name"]
}
16 changes: 16 additions & 0 deletions fuzz/seeds/compile/ref-external
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"user": { "$ref": "https://example.com/schemas/user.json" },
"order": {
"type": "object",
"properties": {
"id": { "type": "string" },
"items": { "$ref": "https://example.com/schemas/order-items.json" }
},
"required": ["id", "items"]
}
},
"required": ["user", "order"]
}
29 changes: 29 additions & 0 deletions fuzz/seeds/compile/ref-local
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"address": {
"type": "object",
"properties": {
"street": { "type": "string" },
"city": { "type": "string" },
"country": { "type": "string" }
},
"required": ["street", "city", "country"]
},
"person": {
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 },
"address": { "$ref": "#/definitions/address" }
},
"required": ["name", "age", "address"]
}
},
"properties": {
"user": { "$ref": "#/definitions/person" },
"emergencyContact": { "$ref": "#/definitions/person" }
},
"required": ["user", "emergencyContact"]
}
37 changes: 37 additions & 0 deletions fuzz/seeds/compile/ref-nested
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"dimension": {
"type": "number",
"minimum": 0
},
"rectangle": {
"type": "object",
"properties": {
"width": { "$ref": "#/definitions/dimension" },
"height": { "$ref": "#/definitions/dimension" }
},
"required": ["width", "height"]
},
"circle": {
"type": "object",
"properties": {
"radius": { "$ref": "#/definitions/dimension" }
},
"required": ["radius"]
}
},
"properties": {
"shapes": {
"type": "array",
"items": {
"oneOf": [
{ "$ref": "#/definitions/rectangle" },
{ "$ref": "#/definitions/circle" }
]
}
}
},
"required": ["shapes"]
}
21 changes: 21 additions & 0 deletions fuzz/seeds/compile/ref-recursive
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"definitions": {
"person": {
"type": "object",
"properties": {
"name": { "type": "string" },
"children": {
"type": "array",
"items": { "$ref": "#/definitions/person" }
}
},
"required": ["name"]
}
},
"properties": {
"familyTree": { "$ref": "#/definitions/person" }
},
"required": ["familyTree"]
}

0 comments on commit 1d48eaf

Please sign in to comment.