this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add structure fuzzer

garrison bda258ba 9cfbcd6e

+68 -1
+2 -1
lib/encoding/structure.ex
··· 47 47 48 48 defp encode([field | fields_rest], header_acc, body_acc, last_id) do 49 49 {field_id, field_value} = field 50 - assert field_id > last_id 50 + # TODO: allows duplicate fields with id=0 51 + if last_id != 0, do: assert field_id > last_id 51 52 52 53 id_diff_enc = encode_varint(field_id - last_id) 53 54 {field_head, field_body} = encode_field(field_value)
+66
test/encoding_test.exs
··· 68 68 end 69 69 end 70 70 71 + defmodule StructureFuzz do 72 + def run(seed, iterations) do 73 + :rand.seed(:exsss, seed) 74 + 75 + Enum.each(1..iterations, fn _i -> 76 + iteration() 77 + end) 78 + end 79 + 80 + defp iteration do 81 + fields = random_fields() 82 + 83 + bin = Structure.pack(fields) 84 + result = Structure.unpack(bin) 85 + 86 + assert fields == result 87 + end 88 + 89 + defp random_fields do 90 + count = Enum.random(1..100) 91 + do_build_fields(count, [], -1) 92 + end 93 + 94 + defp do_build_fields(0, acc, _last_id) do 95 + Enum.reverse(acc) 96 + end 97 + 98 + defp do_build_fields(count, acc, last_id) do 99 + id = last_id + random_id_diff() 100 + value = random_value() 101 + 102 + count = count - 1 103 + acc = [{id, value} | acc] 104 + do_build_fields(count, acc, id) 105 + end 106 + 107 + defp random_id_diff do 108 + case Enum.random(1..4) do 109 + 1 -> 1 110 + 2 -> Enum.random(1..127) 111 + 3 -> Enum.random(1..256) 112 + 4 -> Enum.random(1..1_000_000_000) 113 + end 114 + # TODO 115 + 1 116 + end 117 + 118 + defp random_value do 119 + case Enum.random(1..10) do 120 + 1 -> nil 121 + 2 -> false 122 + 3 -> true 123 + 4 -> 0 124 + 5 -> (2 ** 64) - 1 125 + 6 -> -(2 ** 64 - 1) 126 + 7 -> Enum.random(1..((2 ** 64) - 1)) 127 + 8 -> -Enum.random(1..((2 ** 64))) 128 + _ -> nil 129 + end 130 + end 131 + end 132 + 71 133 describe "structure" do 134 + test "fuzz" do 135 + StructureFuzz.run(100, 1000) 136 + end 137 + 72 138 test "encode" do 73 139 fields = [ 74 140 {2, 123},