|
| 1 | +%% Copyright (c) 2015-2020, Loïc Hoguin <essen@ninenines.eu> |
| 2 | +%% |
| 3 | +%% Permission to use, copy, modify, and/or distribute this software for any |
| 4 | +%% purpose with or without fee is hereby granted, provided that the above |
| 5 | +%% copyright notice and this permission notice appear in all copies. |
| 6 | +%% |
| 7 | +%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | +%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | +%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 10 | +%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | +%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 12 | +%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 13 | +%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 14 | + |
| 15 | +-module(gun_autobahn). |
| 16 | +-compile(export_all). |
| 17 | +-compile(nowarn_export_all). |
| 18 | + |
| 19 | +autobahn_fuzzingserver() -> |
| 20 | + N = get_case_count(), |
| 21 | + run_cases(0, N), |
| 22 | + terminate(). |
| 23 | + |
| 24 | +get_case_count() -> |
| 25 | + {Pid, MRef, StreamRef} = connect("/getCaseCount"), |
| 26 | + receive |
| 27 | + {gun_ws, Pid, StreamRef, {text, N}} -> |
| 28 | + close(Pid, MRef), |
| 29 | + binary_to_integer(N); |
| 30 | + _Msg -> |
| 31 | + terminate(), |
| 32 | + error(failed) |
| 33 | + end. |
| 34 | + |
| 35 | +run_cases(Total, Total) -> |
| 36 | + ok; |
| 37 | +run_cases(N, Total) -> |
| 38 | + {Pid, MRef, StreamRef} = connect(["/runCase?case=", integer_to_binary(N + 1), "&agent=Gun"]), |
| 39 | + loop(Pid, MRef, StreamRef), |
| 40 | + update_reports(), |
| 41 | + run_cases(N + 1, Total). |
| 42 | + |
| 43 | +loop(Pid, MRef, StreamRef) -> |
| 44 | + receive |
| 45 | + {gun_ws, Pid, StreamRef, close} -> |
| 46 | + gun:ws_send(Pid, StreamRef, close), |
| 47 | + loop(Pid, MRef, StreamRef); |
| 48 | + {gun_ws, Pid, StreamRef, {close, Code, _}} -> |
| 49 | + gun:ws_send(Pid, StreamRef, {close, Code, <<>>}), |
| 50 | + loop(Pid, MRef, StreamRef); |
| 51 | + {gun_ws, Pid, StreamRef, Frame} -> |
| 52 | + gun:ws_send(Pid, StreamRef, Frame), |
| 53 | + loop(Pid, MRef, StreamRef); |
| 54 | + {gun_down, Pid, ws, _, _} -> |
| 55 | + close(Pid, MRef); |
| 56 | + {'DOWN', MRef, process, Pid, normal} -> |
| 57 | + close(Pid, MRef); |
| 58 | + _Msg -> |
| 59 | + close(Pid, MRef) |
| 60 | + end. |
| 61 | + |
| 62 | +update_reports() -> |
| 63 | + {Pid, MRef, StreamRef} = connect("/updateReports?agent=Gun"), |
| 64 | + receive |
| 65 | + {gun_ws, Pid, StreamRef, close} -> |
| 66 | + close(Pid, MRef) |
| 67 | + after 5000 -> |
| 68 | + error(failed) |
| 69 | + end. |
| 70 | + |
| 71 | +connect(Path) -> |
| 72 | + {ok, Pid} = gun:open("fuzzingserver", 9001, #{retry => 0}), |
| 73 | + {ok, http} = gun:await_up(Pid), |
| 74 | + MRef = monitor(process, Pid), |
| 75 | + StreamRef = gun:ws_upgrade(Pid, Path, [], #{compress => true}), |
| 76 | + receive |
| 77 | + {gun_upgrade, Pid, StreamRef, [<<"websocket">>], _} -> |
| 78 | + ok; |
| 79 | + _Msg -> |
| 80 | + terminate(), |
| 81 | + error(failed) |
| 82 | + end, |
| 83 | + {Pid, MRef, StreamRef}. |
| 84 | + |
| 85 | +close(Pid, MRef) -> |
| 86 | + demonitor(MRef), |
| 87 | + gun:close(Pid), |
| 88 | + gun:flush(Pid). |
| 89 | + |
| 90 | +terminate() -> |
| 91 | + ok. |
0 commit comments