diff options
author | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 02:04:08 -0500 |
---|---|---|
committer | A. Wilcox <AWilcox@Wilcox-Tech.com> | 2019-10-12 02:04:08 -0500 |
commit | c9ac06657ecf79c5c7bada112a6fc67f87fbcba1 (patch) | |
tree | bc25d100c40829d3d4a9a514a7d78985ca798adb | |
parent | c3bd806edc816ce176e507394250163aab9c7365 (diff) | |
download | horizon-c9ac06657ecf79c5c7bada112a6fc67f87fbcba1.tar.gz horizon-c9ac06657ecf79c5c7bada112a6fc67f87fbcba1.tar.bz2 horizon-c9ac06657ecf79c5c7bada112a6fc67f87fbcba1.tar.xz horizon-c9ac06657ecf79c5c7bada112a6fc67f87fbcba1.zip |
hscript: Validate 'hostname' keys and add another test
-rw-r--r-- | hscript/meta.cc | 31 | ||||
-rw-r--r-- | tests/fixtures/0032-hostname-ends-with-dot.installfile | 5 | ||||
-rw-r--r-- | tests/spec/validator.rb | 6 |
3 files changed, 41 insertions, 1 deletions
diff --git a/hscript/meta.cc b/hscript/meta.cc index 84e026e..5897c8b 100644 --- a/hscript/meta.cc +++ b/hscript/meta.cc @@ -31,7 +31,36 @@ Key *Hostname::parseFromData(const std::string data, int lineno, int *errors, bool Hostname::validate(ScriptOptions) const { /* Validate that the name is a valid machine or DNS name */ - return false; + bool any_failure = false; + std::string::size_type last_dot = 0, next_dot = 0; + + if(!isalpha(this->_value[0])) { + any_failure = true; + output_error("installfile:" + std::to_string(this->lineno()), + "hostname: must start with alphabetical character"); + } + + if(this->_value.size() > 320) { + any_failure = true; + output_error("installfile:" + std::to_string(this->lineno()), + "hostname: value too long", + "valid host names must be less than 320 characters"); + } + + do { + next_dot = this->_value.find_first_of('.', next_dot + 1); + if(next_dot == std::string::npos) { + next_dot = this->_value.size(); + } + if(next_dot - last_dot > 64) { + any_failure = true; + output_error("installfile:" + std::to_string(this->lineno()), + "hostname: component too long", + "each component must be less than 64 characters"); + } + } while(next_dot != this->_value.size()); + + return any_failure; } bool Hostname::execute(ScriptOptions) const { diff --git a/tests/fixtures/0032-hostname-ends-with-dot.installfile b/tests/fixtures/0032-hostname-ends-with-dot.installfile new file mode 100644 index 0000000..f608dde --- /dev/null +++ b/tests/fixtures/0032-hostname-ends-with-dot.installfile @@ -0,0 +1,5 @@ +network true +hostname test.machine. +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / diff --git a/tests/spec/validator.rb b/tests/spec/validator.rb index 8f6ae6c..e4ba703 100644 --- a/tests/spec/validator.rb +++ b/tests/spec/validator.rb @@ -138,6 +138,12 @@ RSpec.describe 'HorizonScript Validation Utility', :type => :aruba do run_validate expect(last_command_started).to have_output(/error: .*hostname.*/) end + # Runner.Validate.hostname. + it "with dot at end of domain" do + use_fixture '0032-hostname-ends-with-dot.installfile' + run_validate + expect(last_command_started).to_not have_output(/error: .*hostname.*/) + end end # Runner.Validate.rootpw. # Runner.Validate.rootpw.Crypt. |