diff --git a/lib/binoculo.ex b/lib/binoculo.ex index 8cd9c66..ace2450 100644 --- a/lib/binoculo.ex +++ b/lib/binoculo.ex @@ -14,10 +14,17 @@ defmodule Binoculo do write_payload = get_in(parsed_args, [Access.key!(:options), Access.key!(:write)]) read_payload = get_in(parsed_args, [Access.key!(:options), Access.key!(:read)]) - # TODO: improve config set to pipe configs above - Config.set_output_file(output) - Config.set_write_payload(write_payload) - Config.set_read_payload(read_payload) + config = %{ + output_file: output, + write_payload: write_payload, + read_payload: read_payload + } + + config + |> Config.set_output_file() + |> Config.set_write_payload() + |> Config.set_read_payload() + Config.start_maestro() {:ok, qty_to_run} = Maestro.start_get_banner_workers(host_notation, ports) diff --git a/lib/binoculo/config.ex b/lib/binoculo/config.ex index 8cac916..eee607b 100644 --- a/lib/binoculo/config.ex +++ b/lib/binoculo/config.ex @@ -18,25 +18,28 @@ defmodule Binoculo.Config do {:ok, state} end - def set_output_file(file_name) do - file_name = "output/#{file_name}" + def set_output_file(%{output_file: file_name} = config) do + file_name = "#{file_name}" GenServer.cast(__MODULE__, {:set_output_file, file_name}) + config end def get_output_file() do GenServer.call(__MODULE__, :get_output_file) end - def set_write_payload(payload) do + def set_write_payload(%{write_payload: payload} = config) do GenServer.cast(__MODULE__, {:set_write_payload, payload}) + config end def get_write_payload() do GenServer.call(__MODULE__, :get_write_payload) end - def set_read_payload(payload) do + def set_read_payload(%{read_payload: payload} = config) do GenServer.cast(__MODULE__, {:set_read_payload, payload}) + config end def get_read_payload() do diff --git a/lib/binoculo/cross_saver.ex b/lib/binoculo/cross_saver.ex index 0651e9a..f09770c 100644 --- a/lib/binoculo/cross_saver.ex +++ b/lib/binoculo/cross_saver.ex @@ -32,6 +32,7 @@ defmodule Binoculo.CrossSaver do results = Enum.map(results, &Util.host_info_to_text_template/1) Config.get_output_file() + |> then(fn file -> "output/#{file}" end) |> File.write(results, [:append]) end diff --git a/test/config_test.exs b/test/config_test.exs index 6359b02..8ff8b99 100644 --- a/test/config_test.exs +++ b/test/config_test.exs @@ -3,19 +3,29 @@ defmodule ConfigTest do alias Binoculo.Config - test "should set output file" do - assert :ok == Config.set_output_file("test.txt") - assert "output/test.txt" == Config.get_output_file() + setup_all do + config = %{ + output_file: "result.txt", + write_payload: "GET / HTTP/1.1\r\n\r\n", + read_payload: "HTTP/1.1 200 OK\r\n\r\n" + } + + {:ok, config: config} + end + + test "should set output file", %{config: config} do + assert config == Config.set_output_file(config) + assert config.output_file == Config.get_output_file() end - test "should set write payload" do - assert :ok == Config.set_write_payload("GET / HTTP/1.1\r\n\r\n") - assert "GET / HTTP/1.1\r\n\r\n" == Config.get_write_payload() + test "should set write payload", %{config: config} do + assert config == Config.set_write_payload(config) + assert config.write_payload == Config.get_write_payload() end - test "should set read payload" do - assert :ok == Config.set_read_payload("HTTP/1.1 200 OK\r\n\r\n") - assert "HTTP/1.1 200 OK\r\n\r\n" == Config.get_read_payload() + test "should set read payload", %{config: config} do + assert config == Config.set_read_payload(config) + assert config.read_payload == Config.get_read_payload() end test "should start maestro via config module" do diff --git a/test/worker_test.exs b/test/worker_test.exs index 838e786..86fc808 100644 --- a/test/worker_test.exs +++ b/test/worker_test.exs @@ -59,7 +59,7 @@ defmodule WorkerTest do test "should get banner with custom payload" do host_ut = "127.0.0.1" port_ut_http = 8089 - Config.set_write_payload("GET / HTTP/1.1\r\nHost: #{host_ut}\r\n\r\n") + Config.set_write_payload(%{write_payload: "GET / HTTP/1.1\r\nHost: #{host_ut}\r\n\r\n"}) spawn(Server, :start, [port_ut_http, "hello server"]) Process.sleep(:timer.seconds(1)) @@ -72,7 +72,7 @@ defmodule WorkerTest do test "should get banner with nil payload" do host_ut = "127.0.0.1" port_ut_http = 8088 - Config.set_write_payload(nil) + Config.set_write_payload(%{write_payload: nil}) spawn(Server, :start, [port_ut_http, "hello server"]) Process.sleep(:timer.seconds(1)) @@ -85,7 +85,7 @@ defmodule WorkerTest do test "should get errors sending payload after socket close" do host_ut = "127.0.0.1" port_ut_http = 8081 - Config.set_write_payload("foobar") + Config.set_write_payload(%{write_payload: "foobar"}) spawn(Server, :start, [port_ut_http, "hello server"]) Process.sleep(:timer.seconds(1))