1.0 KiB · text History 4b6fe3b
defmodule OhMyExOhIrcd.IRC.CloakTest do
  use ExUnit.Case, async: true

  alias OhMyExOhIrcd.IRC.Cloak

  describe "host/2" do
    test "is deterministic for the same IP" do
      assert Cloak.host("1.2.3.4", "irc.example.com") ==
               Cloak.host("1.2.3.4", "irc.example.com")
    end

    test "differs for different IPs" do
      refute Cloak.host("1.2.3.4", "irc.example.com") ==
               Cloak.host("5.6.7.8", "irc.example.com")
    end

    test "appends the cloak base domain" do
      assert String.ends_with?(Cloak.host("1.2.3.4", "irc.example.com"), ".irc.example.com")
    end

    test "does not leak the raw IP" do
      refute Cloak.host("1.2.3.4", "irc.example.com") =~ "1.2.3.4"
    end

    test "returns the real IP unchanged when cloaking is disabled" do
      assert Cloak.host("1.2.3.4", nil) == "1.2.3.4"
      assert Cloak.host("1.2.3.4", "") == "1.2.3.4"
    end

    test "works for IPv6 too" do
      assert String.ends_with?(Cloak.host("2001:db8::1", "irc.example.com"), ".irc.example.com")
    end
  end
end