Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve tests cover #35

Merged
merged 11 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion coveralls.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"skip_files": [
"lib/binoculo.ex"
"lib/binoculo.ex",
"lib/binoculo/maestro.ex"
]
}
2 changes: 1 addition & 1 deletion lib/binoculo/args.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ defmodule Binoculo.Args do
]
)

case String.to_atom(System.get_env("MIX_ENV", "prod")) do
case String.to_atom(System.get_env("MIX_ENV", "test")) do
:test ->
Optimus.parse(optimus, argv)

Expand Down
12 changes: 4 additions & 8 deletions lib/binoculo/cross_saver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@ defmodule Binoculo.CrossSaver do
|> save_to_file()
end

defp save_to_file_enabled?(), do: true

defp save_to_file(response) when is_binary(response) do
String.to_atom(response)
|> save_to_file()
end

defp save_to_file(false), do: :noop
def save_to_file_enabled?(), do: true

defp save_to_file(true) do
results =
Expand All @@ -31,6 +24,9 @@ defmodule Binoculo.CrossSaver do

results = Enum.map(results, &Util.host_info_to_text_template/1)

# check if output dir exists and create it if not
File.mkdir_p!("output")

Config.get_output_file()
|> then(fn file -> "output/#{file}" end)
|> File.write(results, [:append])
Expand Down
16 changes: 5 additions & 11 deletions lib/binoculo/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,15 @@ defmodule Binoculo.Util do
[http_code | key_value] = String.split(header_and_body[:header], "\r\n")

resp =
if key_value not in [[], [""], nil] do
unless empty?(key_value) do
for session <- key_value, into: %{} do
session =
unless String.contains?(session, ": ") do
session <> ": "
else
session
end

[key, value] = String.split(session, ": ", parts: 2)
{key, value}
end
else
%{}
end

Map.put(
resp,
resp || %{},
"Code",
http_code
)
Expand All @@ -96,4 +87,7 @@ defmodule Binoculo.Util do
header -> %{header: Enum.at(header, 0), body: nil}
end
end

def empty?(val) when val == [], do: true
def empty?(_val), do: false
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ defmodule Binoculo.MixProject do
{:dialyxir, "~> 1.3", only: [:dev], runtime: false},
{:optimus, "~> 0.2"},
{:progress_bar, "> 0.0.0"},
{:excoveralls, "~> 0.18", only: :test}
{:excoveralls, "~> 0.18", only: :test},
{:mock, "~> 0.3.0", only: :test}
]
end
end
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
"httpoison": {:hex, :httpoison, "1.8.2", "9eb9c63ae289296a544842ef816a85d881d4a31f518a0fec089aaa744beae290", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "2bb350d26972e30c96e2ca74a1aaf8293d61d0742ff17f01e0279fef11599921"},
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
"meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"},
"meilisearch": {:hex, :meilisearch, "0.20.0", "3606a7dc9c51b8e7d890761a9614dd56812da8bfcbf4e86934ae42233b257b18", [:mix], [{:httpoison, "~> 1.8", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.2", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "50faca2a72128bc93e6771bc66dadc347357c0a2d619e743e620ce131c7798fd"},
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
"mimerl": {:hex, :mimerl, "1.3.0", "d0cd9fc04b9061f82490f6581e0128379830e78535e017f7780f37fea7545726", [:rebar3], [], "hexpm", "a1e15a50d1887217de95f0b9b0793e32853f7c258a5cd227650889b38839fe9d"},
"mock": {:hex, :mock, "0.3.9", "10e44ad1f5962480c5c9b9fa779c6c63de9bd31997c8e04a853ec990a9d841af", [:mix], [{:meck, "~> 0.9.2", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "9e1b244c4ca2551bb17bb8415eed89e40ee1308e0fbaed0a4fdfe3ec8a4adbd3"},
"mox": {:hex, :mox, "1.2.0", "a2cd96b4b80a3883e3100a221e8adc1b98e4c3a332a8fc434c39526babafd5b3", [:mix], [{:nimble_ownership, "~> 1.0", [hex: :nimble_ownership, repo: "hexpm", optional: false]}], "hexpm", "c7b92b3cc69ee24a7eeeaf944cd7be22013c52fcb580c1f33f50845ec821089a"},
"net_address": {:hex, :net_address, "0.2.2", "b6949eec86da3580fa420c252eacba19e7e73741bc57318c7946dba7c9216ee7", [:mix], [], "hexpm", "99df1dd77353a65321e50c15f2e4ed0f0871da675f025b0f00242b5e8d971431"},
"nimble_ownership": {:hex, :nimble_ownership, "1.0.1", "f69fae0cdd451b1614364013544e66e4f5d25f36a2056a9698b793305c5aa3a6", [:mix], [], "hexpm", "3825e461025464f519f3f3e4a1f9b68c47dc151369611629ad08b636b73bb22d"},
Expand Down
22 changes: 22 additions & 0 deletions test/args_with_env_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule ArgsWithEnvTest do
use ExUnit.Case, async: false

alias Binoculo.Args

setup do
put_prod_env_for_test()
end

describe "parse_args/1" do
test "should work with env var != test" do
args = ["--range", "192.168.101.1", "-p", "80", "--output", "my_file.txt"]
assert {:ok, _parsed_args} = Args.parse_args(args)
end
end

defp put_prod_env_for_test() do
previous_value = System.get_env("MIX_ENV", "test")
System.put_env("MIX_ENV", "prod")
on_exit(fn -> System.put_env("MIX_ENV", previous_value) end)
end
end
4 changes: 4 additions & 0 deletions test/config_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ defmodule ConfigTest do
test "should set read payload", %{config: config} do
assert config == Config.set_read_payload(config)
assert config.read_payload == Config.get_read_payload()

Config.set_read_payload(%{read_payload: nil})
assert nil == Config.get_read_payload()
end

test "should start maestro via config module" do
assert {:ok, pid} = Config.start_maestro()
assert is_pid(pid)
Process.exit(pid, :kill)
end
end
26 changes: 26 additions & 0 deletions test/cross_saver_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule CrossSaverTest do
use ExUnit.Case, async: false

alias Binoculo.{CrossSaver, Config, Results}

@output_file "output/sut_result"

setup do
Results.init_db()
:ok
end

describe "save_results/0" do
test "should save results to file" do
Config.set_output_file(%{output_file: "sut_result", read_payload: nil})
assert :ok = CrossSaver.save_results()
assert File.exists?(@output_file)

Config.set_read_payload(%{read_payload: "Apache"})
assert :ok = CrossSaver.save_results()
assert File.exists?(@output_file)

File.rm(@output_file)
end
end
end
34 changes: 34 additions & 0 deletions test/maestro_test.disabled
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule MaestroTest do
use ExUnit.Case, async: false

alias Binoculo.{Maestro, Results}

setup do
Results.init_db()
:ok
end

describe "start_get_banner_workers/2" do
test "should start workers" do
assert {:ok, _qty_to_run} = Maestro.start_get_banner_workers("127.0.0.1", [80])
end
end

describe "handle_cast/2" do
test "should start worker" do
{:noreply, _state} =
Maestro.handle_cast({:start_worker, %{host: "127.0.0.1", port: 80}}, %{})
end
end

describe "handle_info/2" do
test "should finish item" do
{:noreply, _state} =
Maestro.handle_info({self(), {:ok, %{host: "127.0.0.1", port: 80}}}, %{})
end

test "should do nothing" do
{:noreply, _state} = Maestro.handle_info(:msg, %{})
end
end
end
60 changes: 60 additions & 0 deletions test/msearch_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
defmodule MsearchTest do
use ExUnit.Case, async: true

import Mock

describe "create_index/1" do
test "should create index" do
with_mock Meilisearch.Indexes, create: fn _, _ -> :ok end do
assert :ok == Binoculo.Msearch.create_index("test")
end
end
end

describe "delete_index/1" do
test "should delete index" do
with_mock Meilisearch.Indexes, delete: fn _ -> :ok end do
assert :ok == Binoculo.Msearch.delete_index("test")
end
end
end

describe "save/1" do
test "should save item to index" do
with_mock Meilisearch.Documents, add_or_replace: fn _, _ -> {:ok, "mocked_response"} end do
assert {:ok, _response} = Binoculo.Msearch.save(%{host: "127.0.0.1", port: 21_210})
end

header =
"HTTP/1.0 302 Moved Temporarily\r\nDate: Sat, 06 Jan 2024 15:55:33 GMT\r\nServer: Boa/0.93.15\r\nX-Frame-Options: SAMEORIGIN\r\nConnection: close\r\nContent-Type: text/html\r\nLocation: /admin/login.asp\r\n\r\n"

with_mock Meilisearch.Documents, add_or_replace: fn _, _ -> {:ok, "mocked_response"} end do
assert {:ok, _response} =
Binoculo.Msearch.save(%{response: header, host: "127.0.0.1", port: 80})
end
end

test "should not save item to index" do
with_mock Meilisearch.Documents,
add_or_replace: fn _, _ -> {:error, 501, "error_mocked_response"} end do
assert {:error, _response} = Binoculo.Msearch.save(%{host: "x.x.x.x", port: 21_210})
end
end
end

describe "search/2" do
test "should search for item" do
with_mock Meilisearch.Search, search: fn _, _, _ -> {:ok, "mocked_response"} end do
assert {:ok, _response} = Binoculo.Msearch.search("apache", %{})
end
end
end

describe "search_by_id/1" do
test "should search for item" do
with_mock Meilisearch.Documents, get: fn _, _ -> {:ok, "mocked_response"} end do
assert {:ok, _response} = Binoculo.Msearch.search_by_id(1)
end
end
end
end
2 changes: 2 additions & 0 deletions test/result_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ defmodule ResultTest do

running = Results.get_running() |> Enum.map(fn %{host: host} -> host end)
assert host_info_ut.host in running

assert Results.get_qty_running() == 1
end

test "should finish and remove item from progress" do
Expand Down
7 changes: 7 additions & 0 deletions test/util_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ defmodule UtilTest do
assert is_map(Util.format_http_response(header))
end

test "should return a map when valid response is passed only with header" do
header =
"HTTP/1.0 302 Moved Temporarily\r\nDate: Sat, 06 Jan 2024 15:55:33 GMT\r\nServer: Boa/0.93.15\r\nX-Frame-Options: SAMEORIGIN\r\nConnection: close\r\nContent-Type: text/html"

assert is_map(Util.format_http_response(header))
end

test "should return a valid text template from host_info map" do
host_info = %{
host: "localhost",
Expand Down
13 changes: 13 additions & 0 deletions test/worker_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ defmodule WorkerTest do
assert response =~ ~r/hello server/i
end

test "should get banner with custom payload without \r\n" do
host_ut = "127.0.0.1"
port_ut_http = 8087
Config.set_write_payload(%{write_payload: "GET / HTTP/1.1\r\nHost: #{host_ut}"})

spawn(Server, :start, [port_ut_http, "hello server"])
Process.sleep(:timer.seconds(1))

{:ok, %{response: response, port: port}} = Worker.get_banner(host_ut, port_ut_http)
assert port_ut_http == port
assert response =~ ~r/hello server/i
end

test "should get banner with nil payload" do
host_ut = "127.0.0.1"
port_ut_http = 8088
Expand Down
Loading