Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Simplify frequency macro in fuzzer
Browse files Browse the repository at this point in the history
  • Loading branch information
obrok committed Sep 6, 2018
1 parent 519bb66 commit ec99b4c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 42 deletions.
75 changes: 38 additions & 37 deletions cloak/lib/compliance/query_generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ defmodule Cloak.Compliance.QueryGenerator do
# -------------------------------------------------------------------

defp generate_scaffold(tables, complexity) do
frequency(complexity, %{
3 => %Scaffold{from: {:table, Enum.random(tables)}, complexity: complexity},
1 => %Scaffold{from: {:subquery, generate_scaffold(tables, div(complexity, 2))}, complexity: div(complexity, 2)},
1 => %Scaffold{
from: {:join, generate_scaffold(tables, div(complexity, 3)), generate_scaffold(tables, div(complexity, 3))},
complexity: div(complexity, 3)
}
})
frequency(complexity, [
{3, %Scaffold{from: {:table, Enum.random(tables)}, complexity: complexity}},
{1, %Scaffold{from: {:subquery, generate_scaffold(tables, div(complexity, 2))}, complexity: div(complexity, 2)}},
{1,
%Scaffold{
from: {:join, generate_scaffold(tables, div(complexity, 3)), generate_scaffold(tables, div(complexity, 3))},
complexity: div(complexity, 3)
}}
])
|> put_in([Lens.key(:aggregate?)], boolean())
end

Expand Down Expand Up @@ -251,17 +252,17 @@ defmodule Cloak.Compliance.QueryGenerator do
end

defp where(scaffold) do
frequency(scaffold.complexity, %{
1 => {:where, nil, [where_condition(scaffold.complexity)]},
1 => empty()
})
frequency(scaffold.complexity, [
{1, {:where, nil, [where_condition(scaffold.complexity)]}},
{1, empty()}
])
end

defp where_condition(complexity) do
frequency(complexity, %{
2 => simple_condition(),
1 => {:and, nil, [where_condition(div(complexity, 2)), where_condition(div(complexity, 2))]}
})
frequency(complexity, [
{2, simple_condition()},
{1, {:and, nil, [where_condition(div(complexity, 2)), where_condition(div(complexity, 2))]}}
])
end

defp simple_condition(), do: {:=, nil, [constant(), constant()]}
Expand Down Expand Up @@ -306,47 +307,47 @@ defmodule Cloak.Compliance.QueryGenerator do
end

defp order_by_elements(scaffold, {:select, _, items}) do
frequency(scaffold.complexity, %{
1 => [],
1 => [{:integer, :rand.uniform(length(items)), []}]
})
frequency(scaffold.complexity, [
{1, []},
{1, [{:integer, :rand.uniform(length(items)), []}]}
])
end

defp sample_users(%{select_user_id?: false}), do: empty()

defp sample_users(scaffold) do
frequency(scaffold.complexity, %{
1 => empty(),
1 => {:sample_users, sample_users_size(scaffold.complexity), []}
})
frequency(scaffold.complexity, [
{1, empty()},
{1, {:sample_users, sample_users_size(scaffold.complexity), []}}
])
end

defp limit(_scaffold, _order_by = {:empty, _, _}), do: empty()

defp limit(scaffold, _order_by) do
frequency(scaffold.complexity, %{
1 => empty(),
1 => {:limit, :rand.uniform(100), []}
})
frequency(scaffold.complexity, [
{1, empty()},
{1, {:limit, :rand.uniform(100), []}}
])
end

defp offset(%{select_user_id?: true}, _order_by, _limit = {:empty, _, _}), do: empty()

defp offset(_scaffold, _order_by = {:empty, _, _}, _limit), do: empty()

defp offset(scaffold, _order_by, _limit) do
frequency(scaffold.complexity, %{
1 => empty(),
1 => {:offset, :rand.uniform(100), []}
})
frequency(scaffold.complexity, [
{1, empty()},
{1, {:offset, :rand.uniform(100), []}}
])
end

defp sample_users_size(complexity) do
frequency(complexity, %{
1 => :rand.uniform() * 100,
1 => :rand.uniform() * 10,
1 => :rand.uniform()
})
frequency(complexity, [
{1, :rand.uniform() * 100},
{1, :rand.uniform() * 10},
{1, :rand.uniform()}
])
end

# -------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions cloak/lib/compliance/query_generator/generation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ defmodule Cloak.Compliance.QueryGenerator.Generation do
Select a random option from the given list of `{weight, option}` pairs. The options will be evaluated lazily. The list
will be logically cut off after a total weight of `complexity`.
"""
defmacro frequency(complexity, {:%{}, _line, options}) do
defmacro frequency(complexity, options) do
options =
Enum.map(options, fn {key, val} ->
{key,
Enum.map(options, fn {weight, val} ->
{weight,
quote do
fn -> unquote(val) end
end}
Expand All @@ -33,6 +33,6 @@ defmodule Cloak.Compliance.QueryGenerator.Generation do
pick_option(options, random)
end

def pick_option([{frequency, option} | _], number) when number < frequency, do: option.()
def pick_option([{frequency, _} | options], number), do: pick_option(options, number - frequency)
defp pick_option([{frequency, option} | _], number) when number < frequency, do: option.()
defp pick_option([{frequency, _} | options], number), do: pick_option(options, number - frequency)
end

0 comments on commit ec99b4c

Please sign in to comment.