Skip to content

Commit

Permalink
chore: improve config usage
Browse files Browse the repository at this point in the history
  • Loading branch information
girorme committed May 15, 2024
1 parent df1b76c commit 32b6b7d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 20 deletions.
15 changes: 11 additions & 4 deletions lib/binoculo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 7 additions & 4 deletions lib/binoculo/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/binoculo/cross_saver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
28 changes: 19 additions & 9 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down

0 comments on commit 32b6b7d

Please sign in to comment.