diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-14 07:36:33 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-14 07:36:33 -0500 |
commit | 37005da8fbecc64c2942ac03f9189b219be69104 (patch) | |
tree | f1c6b98c03d00a6ee7a07fc2626c60969281f894 | |
parent | 5593f1f14f62e9474a0f14d0b863d1331468c522 (diff) | |
download | horizon-37005da8fbecc64c2942ac03f9189b219be69104.tar.gz horizon-37005da8fbecc64c2942ac03f9189b219be69104.tar.bz2 horizon-37005da8fbecc64c2942ac03f9189b219be69104.tar.xz horizon-37005da8fbecc64c2942ac03f9189b219be69104.zip |
hscript: Add NetSSID parsing and tests
-rw-r--r-- | hscript/network.cc | 83 | ||||
-rw-r--r-- | tests/fixtures/0062-netssid-simple-none.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0063-netssid-simple-wep.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0064-netssid-simple-wpa.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0065-netssid-spaces-none.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0066-netssid-spaces-wep.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0067-netssid-spaces-wpa.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0068-netssid-invalid-iface.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0069-netssid-unquoted.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0070-netssid-syntax-error.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0071-netssid-missing-pw.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0072-netssid-missing-ssid.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0073-netssid-invalid-type.installfile | 7 | ||||
-rw-r--r-- | tests/spec/validator.rb | 68 |
14 files changed, 222 insertions, 2 deletions
diff --git a/hscript/network.cc b/hscript/network.cc index 809891f..09c5510 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -12,6 +12,7 @@ #include <algorithm> #include <arpa/inet.h> +#include <net/if.h> #include "network.hh" #include "util/output.hh" @@ -223,8 +224,86 @@ bool NetAddress::execute(ScriptOptions) const { Key *NetSSID::parseFromData(const std::string &data, int lineno, int *errors, int *warnings) { - std::string iface, ssid, passphrase; - return new NetSSID(lineno, iface, ssid, SecurityType::None, passphrase); + std::string iface, ssid, secstr, passphrase; + SecurityType type; + std::string::size_type start, pos, next; + + /* Since SSIDs can have spaces in them, we can't just naively count + * spaces to figure a count of elements. We have to do all the hard + * parsing up front. :( */ + start = data.find_first_of(' '); + if(start == std::string::npos) { + /* ok this is just ridiculous then */ + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netssid: at least three elements expected"); + return nullptr; + } + + iface = data.substr(0, start); + if(iface.length() > IFNAMSIZ) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netssid: interface name '" + iface + "' is invalid", + "interface names must be 16 characters or less"); + return nullptr; + } + + if(data[start + 1] != '"') { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netssid: malformed SSID", "SSIDs must be quoted"); + return nullptr; + } + + pos = data.find_first_of('"', start + 2); + if(pos == std::string::npos) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netssid: unterminated SSID"); + return nullptr; + } + + ssid = data.substr(start + 2, pos - start - 2); + + if(data.length() < pos + 5) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netssid: security type expected"); + return nullptr; + } + start = data.find_first_of(' ', pos + 1); + next = pos = data.find_first_of(' ', start + 1); + /* pos may be npos if type is none. that is fine. */ + if(pos != std::string::npos) { + next = pos - start - 1; + } + secstr = data.substr(start + 1, next); + if(secstr == "none") { + type = SecurityType::None; + } else if(secstr == "wep") { + type = SecurityType::WEP; + } else if(secstr == "wpa") { + type = SecurityType::WPA; + } else { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netssid: unknown security type '" + secstr + "'", + "expected one of 'none', 'wep', or 'wpa'"); + return nullptr; + } + + if(type != SecurityType::None) { + if(pos == std::string::npos || data.length() < pos + 2) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "netssid: expected passphrase for security type '" + + secstr + "'"); + return nullptr; + } + passphrase = data.substr(pos + 1); + } + return new NetSSID(lineno, iface, ssid, type, passphrase); } bool NetSSID::validate(ScriptOptions options) const { diff --git a/tests/fixtures/0062-netssid-simple-none.installfile b/tests/fixtures/0062-netssid-simple-none.installfile new file mode 100644 index 0000000..4894284 --- /dev/null +++ b/tests/fixtures/0062-netssid-simple-none.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "h" none diff --git a/tests/fixtures/0063-netssid-simple-wep.installfile b/tests/fixtures/0063-netssid-simple-wep.installfile new file mode 100644 index 0000000..dac79f3 --- /dev/null +++ b/tests/fixtures/0063-netssid-simple-wep.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "Hophop" wep omgitsarabbit diff --git a/tests/fixtures/0064-netssid-simple-wpa.installfile b/tests/fixtures/0064-netssid-simple-wpa.installfile new file mode 100644 index 0000000..c49c428 --- /dev/null +++ b/tests/fixtures/0064-netssid-simple-wpa.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "Fox5G" wpa shhsekrit diff --git a/tests/fixtures/0065-netssid-spaces-none.installfile b/tests/fixtures/0065-netssid-spaces-none.installfile new file mode 100644 index 0000000..b30af63 --- /dev/null +++ b/tests/fixtures/0065-netssid-spaces-none.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "MotoVAP M91729SC046V" none diff --git a/tests/fixtures/0066-netssid-spaces-wep.installfile b/tests/fixtures/0066-netssid-spaces-wep.installfile new file mode 100644 index 0000000..9d68a9c --- /dev/null +++ b/tests/fixtures/0066-netssid-spaces-wep.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "Wiehe Investigation Agency" wep with spaces diff --git a/tests/fixtures/0067-netssid-spaces-wpa.installfile b/tests/fixtures/0067-netssid-spaces-wpa.installfile new file mode 100644 index 0000000..946b5af --- /dev/null +++ b/tests/fixtures/0067-netssid-spaces-wpa.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "The New Fox 5G" wpa shh, sekrit! diff --git a/tests/fixtures/0068-netssid-invalid-iface.installfile b/tests/fixtures/0068-netssid-invalid-iface.installfile new file mode 100644 index 0000000..8b0a30a --- /dev/null +++ b/tests/fixtures/0068-netssid-invalid-iface.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlP20p36s15f10d16 "h" none diff --git a/tests/fixtures/0069-netssid-unquoted.installfile b/tests/fixtures/0069-netssid-unquoted.installfile new file mode 100644 index 0000000..227aecf --- /dev/null +++ b/tests/fixtures/0069-netssid-unquoted.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 h none diff --git a/tests/fixtures/0070-netssid-syntax-error.installfile b/tests/fixtures/0070-netssid-syntax-error.installfile new file mode 100644 index 0000000..0f3c15b --- /dev/null +++ b/tests/fixtures/0070-netssid-syntax-error.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "h none diff --git a/tests/fixtures/0071-netssid-missing-pw.installfile b/tests/fixtures/0071-netssid-missing-pw.installfile new file mode 100644 index 0000000..4b0309d --- /dev/null +++ b/tests/fixtures/0071-netssid-missing-pw.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 "h" wep diff --git a/tests/fixtures/0072-netssid-missing-ssid.installfile b/tests/fixtures/0072-netssid-missing-ssid.installfile new file mode 100644 index 0000000..9a6eabb --- /dev/null +++ b/tests/fixtures/0072-netssid-missing-ssid.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +netssid wlan0 diff --git a/tests/fixtures/0073-netssid-invalid-type.installfile b/tests/fixtures/0073-netssid-invalid-type.installfile new file mode 100644 index 0000000..4734e3f --- /dev/null +++ b/tests/fixtures/0073-netssid-invalid-type.installfile @@ -0,0 +1,7 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +# wap as typo for wpa +netssid wlan0 "h" wap hlang diff --git a/tests/spec/validator.rb b/tests/spec/validator.rb index 96cf572..fb17fa6 100644 --- a/tests/spec/validator.rb +++ b/tests/spec/validator.rb @@ -365,6 +365,74 @@ RSpec.describe 'HorizonScript Validation Utility', :type => :aruba do expect(last_command_started).to have_output(/error: .*netaddress.*addresses/) end end + context "for 'netssid' key" do + it "succeeds with simple SSID, no security" do + use_fixture '0062-netssid-simple-none.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "succeeds with simple SSID, WEP security" do + use_fixture '0063-netssid-simple-wep.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "succeeds with simple SSID, WPA security" do + use_fixture '0064-netssid-simple-wpa.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "succeeds with complex SSID, no security" do + use_fixture '0065-netssid-spaces-none.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "succeeds with complex SSID, WEP security" do + use_fixture '0066-netssid-spaces-wep.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "succeeds with complex SSID, WPA security" do + use_fixture '0067-netssid-spaces-wpa.installfile' + run_validate + expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) + end + it "fails with invalid interface name" do + use_fixture '0068-netssid-invalid-iface.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*netssid.*interface/) + end + it "fails with raw / unquoted SSID" do + use_fixture '0069-netssid-unquoted.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*netssid.*quote/) + end + it "fails with SSID without terminating quote" do + use_fixture '0070-netssid-syntax-error.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*netssid.*unterminated/) + end + it "fails with missing passphrase" do + use_fixture '0071-netssid-missing-pw.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*netssid.*expected/) + end + it "fails with missing SSID" do + use_fixture '0072-netssid-missing-ssid.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*netssid.*expected/) + end + it "fails with invalid security type" do + use_fixture '0073-netssid-invalid-type.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*netssid.*security/) + end + end context "for 'repository' key" do it "succeeds with basic repositories" do use_fixture '0055-repository-basic.installfile' |