Skip to content

Commit

Permalink
Delete lines that don't have contents
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorRigby committed May 9, 2019
1 parent 251cbff commit 3574006
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
23 changes: 18 additions & 5 deletions lib/vintage_net/technology/wifi.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ defmodule VintageNet.Technology.WiFi do
alias VintageNet.WiFi.{Scan, WPA2}
alias VintageNet.Interface.RawConfig

# this is used to indicate that an entire line should be deleted from the final config.
@mark_for_deletion "MARK_FOR_DELETION"

@impl true
def to_raw_config(ifname, %{type: __MODULE__, wifi: wifi_config} = config, opts) do
ifup = Keyword.fetch!(opts, :bin_ifup)
Expand Down Expand Up @@ -94,10 +97,13 @@ defmodule VintageNet.Technology.WiFi do
end

defp wifi_to_supplicant_contents(wifi, control_interface_path, regulatory_domain) do
"""
ctrl_interface=#{control_interface_path}
country=#{regulatory_domain}
""" <> into_wifi_network_config(wifi)
content =
"""
ctrl_interface=#{control_interface_path}
country=#{regulatory_domain}
""" <> into_wifi_network_config(wifi)

strip_marks(content)
end

defp key_mgmt_to_string(key) when key in [:none, :wep], do: "NONE"
Expand Down Expand Up @@ -153,7 +159,7 @@ defmodule VintageNet.Technology.WiFi do

defp into_config_string(wifi, opt_key) do
case Map.get(wifi, opt_key) do
nil -> ""
nil -> "#{@mark_for_deletion}"
opt -> wifi_opt_to_config_string(wifi, opt_key, opt)
end
end
Expand Down Expand Up @@ -224,4 +230,11 @@ defmodule VintageNet.Technology.WiFi do
{:ok, hostname} = :inet.gethostname()
to_string(hostname)
end

defp strip_marks(data) do
data
|> String.split("\n")
|> Enum.reject(&String.match?(&1, ~r(#{@mark_for_deletion})))
|> Enum.join("\n")
end
end
14 changes: 1 addition & 13 deletions test/vintage_net/config_wifi_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ defmodule VintageNet.ConfigWiFiTest do
ssid="testing"
psk=1234567890123456789012345678901234567890123456789012345678901234
key_mgmt=WPA-PSK
}
"""}
],
Expand Down Expand Up @@ -81,8 +79,6 @@ defmodule VintageNet.ConfigWiFiTest do
ssid="testing"
psk=1EE0A473A954F61007E526365D4FDC056FE2A102ED2CE77D64492A9495B83030
key_mgmt=WPA-PSK
}
"""}
],
Expand Down Expand Up @@ -126,10 +122,7 @@ defmodule VintageNet.ConfigWiFiTest do
country=00
network={
ssid="testing"
key_mgmt=NONE
}
"""}
],
Expand Down Expand Up @@ -225,7 +218,6 @@ defmodule VintageNet.ConfigWiFiTest do
psk=1234567890123456789012345678901234567890123456789012345678901234
key_mgmt=WPA-PSK
scan_ssid=1
}
"""}
],
Expand All @@ -244,7 +236,7 @@ defmodule VintageNet.ConfigWiFiTest do
assert {:ok, output} == WiFi.to_raw_config("wlan0", input, default_opts())
end

test "create a EAP network" do
test "create a basic EAP network" do
input = %{
type: VintageNet.Technology.WiFi,
wifi: %{
Expand Down Expand Up @@ -279,7 +271,6 @@ defmodule VintageNet.ConfigWiFiTest do
ssid="testing"
key_mgmt=WPA-EAP
scan_ssid=1
pairwise=CCMP TKIP
group=CCMP TKIP
eap=PEAP
Expand Down Expand Up @@ -352,21 +343,18 @@ defmodule VintageNet.ConfigWiFiTest do
ssid="first_priority"
psk=1234567890123456789012345678901234567890123456789012345678901234
key_mgmt=WPA-PSK
priority=100
}
network={
ssid="second_priority"
psk=1234567890123456789012345678901234567890123456789012345678901234
key_mgmt=WPA-PSK
priority=1
}
network={
ssid="third_priority"
psk=1234567890123456789012345678901234567890123456789012345678901234
key_mgmt=NONE
priority=0
}
"""}
Expand Down

0 comments on commit 3574006

Please sign in to comment.