diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-31 19:37:01 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-31 19:37:01 -0500 |
commit | 77ef54d7a1eccacefc615fee178374760a2401a5 (patch) | |
tree | 27f1891bfe5e0a47efab39728a43dbd20bf1cb04 | |
parent | e80e8ad8578a3426b810363094243c5d822742d5 (diff) | |
download | horizon-77ef54d7a1eccacefc615fee178374760a2401a5.tar.gz horizon-77ef54d7a1eccacefc615fee178374760a2401a5.tar.bz2 horizon-77ef54d7a1eccacefc615fee178374760a2401a5.tar.xz horizon-77ef54d7a1eccacefc615fee178374760a2401a5.zip |
hscript: Implement Nameserver, add tests
-rw-r--r-- | hscript/network.cc | 47 | ||||
-rw-r--r-- | hscript/network.hh | 7 | ||||
-rw-r--r-- | hscript/script.cc | 12 | ||||
-rw-r--r-- | tests/fixtures/0183-nameserver-basic.installfile | 7 | ||||
-rw-r--r-- | tests/fixtures/0184-nameserver-invalid.installfile | 6 | ||||
-rw-r--r-- | tests/fixtures/0185-nameserver-brackets.installfile | 6 | ||||
-rw-r--r-- | tests/spec/validator_spec.rb | 18 |
7 files changed, 102 insertions, 1 deletions
diff --git a/hscript/network.cc b/hscript/network.cc index 3816415..2f4111e 100644 --- a/hscript/network.cc +++ b/hscript/network.cc @@ -28,6 +28,7 @@ using namespace Horizon::Keys; + Key *Network::parseFromData(const std::string &data, int lineno, int *errors, int *) { bool value; @@ -303,6 +304,52 @@ bool NetAddress::execute(ScriptOptions) const { } +Key *Nameserver::parseFromData(const std::string &data, int lineno, + int *errors, int *) { + char addr_buf[16]; + static const std::string valid_chars("1234567890ABCDEFabcdef:."); + + if(data.find_first_not_of(valid_chars) != std::string::npos) { + if(errors) *errors += 0; + output_error("installfile:" + std::to_string(lineno), + "nameserver: expected an IP address"); + if(data.find_first_of("[]") != std::string::npos) { + output_info("installfile:" + std::to_string(lineno), + "nameserver: hint: you don't have to enclose IPv6 " + "addresses in [] brackets"); + } + return nullptr; + } + + if(data.find(':') != std::string::npos) { + /* IPv6 */ + if(::inet_pton(AF_INET6, data.c_str(), &addr_buf) != 1) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "nameserver: '" + data + "' is not a valid IPv6 " + "address", "hint: a ':' was found, so an IPv6 " + "address was expected"); + return nullptr; + } + } else { + /* IPv4 */ + if(::inet_pton(AF_INET, data.c_str(), &addr_buf) != 1) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "nameserver: '" + data + "' is not a valid IPv4 " + "address"); + return nullptr; + } + } + + return new Nameserver(lineno, data); +} + +bool Nameserver::execute(ScriptOptions) const { + return false; +} + + Key *NetSSID::parseFromData(const std::string &data, int lineno, int *errors, int *) { std::string iface, ssid, secstr, passphrase; diff --git a/hscript/network.hh b/hscript/network.hh index 411f4b3..b556ab8 100644 --- a/hscript/network.hh +++ b/hscript/network.hh @@ -69,7 +69,12 @@ public: bool execute(ScriptOptions) const override; }; -class Nameserver : public Key { +class Nameserver : public StringKey { +private: + Nameserver(int _line, const std::string &ns) : StringKey(_line, ns) {} +public: + static Key *parseFromData(const std::string &, int, int *, int *); + bool execute(ScriptOptions) const override; }; class NetSSID : public Key { diff --git a/hscript/script.cc b/hscript/script.cc index c99abe5..6ede4c4 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -98,6 +98,7 @@ struct Script::ScriptPrivate { /*! Network addressing configuration */ std::vector< std::unique_ptr<NetAddress> > addresses; + std::vector< std::unique_ptr<Nameserver> > nses; std::vector< std::unique_ptr<NetSSID> > ssids; /*! APK repositories */ @@ -142,6 +143,10 @@ struct Script::ScriptPrivate { std::unique_ptr<NetAddress> addr(dynamic_cast<NetAddress *>(obj)); this->addresses.push_back(std::move(addr)); return true; + } else if(key_name == "nameserver") { + std::unique_ptr<Nameserver> ns(dynamic_cast<Nameserver *>(obj)); + this->nses.push_back(std::move(ns)); + return true; } else if(key_name == "netssid") { std::unique_ptr<NetSSID> ssid(dynamic_cast<NetSSID *>(obj)); this->ssids.push_back(std::move(ssid)); @@ -730,6 +735,13 @@ bool Script::validate() const { } } + /* REQ: Runner.Validate.nameserver */ + for(auto &ns : this->internal->nses) { + if(!ns->validate(this->opts)) { + failures++; + } + } + /* REQ: Runner.Validate.network.netssid */ for(auto &ssid : this->internal->ssids) { if(!ssid->validate(this->opts)) { diff --git a/tests/fixtures/0183-nameserver-basic.installfile b/tests/fixtures/0183-nameserver-basic.installfile new file mode 100644 index 0000000..fbccfe4 --- /dev/null +++ b/tests/fixtures/0183-nameserver-basic.installfile @@ -0,0 +1,7 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +nameserver 172.16.1.1 +nameserver 2600:1702:2a80:1b90::1 diff --git a/tests/fixtures/0184-nameserver-invalid.installfile b/tests/fixtures/0184-nameserver-invalid.installfile new file mode 100644 index 0000000..3a09be2 --- /dev/null +++ b/tests/fixtures/0184-nameserver-invalid.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +nameserver 0x7F000001 diff --git a/tests/fixtures/0185-nameserver-brackets.installfile b/tests/fixtures/0185-nameserver-brackets.installfile new file mode 100644 index 0000000..e41a49c --- /dev/null +++ b/tests/fixtures/0185-nameserver-brackets.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +nameserver [2600:1702:2a80:1b90::1] diff --git a/tests/spec/validator_spec.rb b/tests/spec/validator_spec.rb index c36e5a1..96a2caf 100644 --- a/tests/spec/validator_spec.rb +++ b/tests/spec/validator_spec.rb @@ -198,6 +198,24 @@ RSpec.describe 'HorizonScript validation', :type => :aruba do expect(last_command_started).to have_output(/error: .*language.*codeset/) end end + context "for 'nameserver' key" do + it "succeeds with IPv4 and IPv6 addresses" do + use_fixture '0183-nameserver-basic.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 an invalid value" do + use_fixture '0184-nameserver-invalid.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*nameserver.*expected/) + end + it "fails with brackets around IPv6 addresses" do + use_fixture '0185-nameserver-brackets.installfile' + run_validate + expect(last_command_started).to have_output(/nameserver.*brackets/) + end + end context "for 'firmware' key" do it "always supports 'false' value" do use_fixture '0112-firmware-false.installfile' |