From 918125404fdf222caef16f8cf3798e20266ae662 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Fri, 18 Oct 2019 19:41:28 -0500 Subject: hscript: Implement some Username logic, add tests --- hscript/user.cc | 18 ++++++++++++---- hscript/user.hh | 1 - tests/fixtures/0082-username-basic.installfile | 10 +++++++++ tests/fixtures/0083-username-duplicate.installfile | 7 +++++++ tests/fixtures/0084-username-system.installfile | 6 ++++++ tests/spec/validator.rb | 24 ++++++++++++++++++++++ 6 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 tests/fixtures/0082-username-basic.installfile create mode 100644 tests/fixtures/0083-username-duplicate.installfile create mode 100644 tests/fixtures/0084-username-system.installfile diff --git a/hscript/user.cc b/hscript/user.cc index 92fed4e..40a8022 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -107,11 +107,21 @@ bool RootPassphrase::execute(ScriptOptions options) const { Key *Username::parseFromData(const std::string &data, int lineno, int *errors, int *warnings) { - return nullptr; -} + if(data.find_first_of(' ') != std::string::npos) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "username: invalid username specified"); + return nullptr; + } -bool Username::validate(ScriptOptions) const { - return false; + if(system_names.find(data) != system_names.end()) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "username: " + data + " is a reserved system username"); + return nullptr; + } + + return new Username(lineno, data); } bool Username::execute(ScriptOptions) const { diff --git a/hscript/user.hh b/hscript/user.hh index b8d5386..d29944f 100644 --- a/hscript/user.hh +++ b/hscript/user.hh @@ -36,7 +36,6 @@ private: StringKey(_line, name) {} public: static Key *parseFromData(const std::string &, int, int*, int*); - bool validate(ScriptOptions) const override; bool execute(ScriptOptions) const override; }; diff --git a/tests/fixtures/0082-username-basic.installfile b/tests/fixtures/0082-username-basic.installfile new file mode 100644 index 0000000..9be8bd0 --- /dev/null +++ b/tests/fixtures/0082-username-basic.installfile @@ -0,0 +1,10 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +username chris +username kayla +username meg +username steph +username amanda diff --git a/tests/fixtures/0083-username-duplicate.installfile b/tests/fixtures/0083-username-duplicate.installfile new file mode 100644 index 0000000..0d85d91 --- /dev/null +++ b/tests/fixtures/0083-username-duplicate.installfile @@ -0,0 +1,7 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +username awilfox +username awilfox diff --git a/tests/fixtures/0084-username-system.installfile b/tests/fixtures/0084-username-system.installfile new file mode 100644 index 0000000..467c408 --- /dev/null +++ b/tests/fixtures/0084-username-system.installfile @@ -0,0 +1,6 @@ +network false +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / +username adm diff --git a/tests/spec/validator.rb b/tests/spec/validator.rb index d54006d..16f397a 100644 --- a/tests/spec/validator.rb +++ b/tests/spec/validator.rb @@ -53,18 +53,21 @@ RSpec.describe 'HorizonScript Validation Utility', :type => :aruba do use_fixture '0001-basic.installfile' run_validate expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) end # HorizonScript Specification, ch 3. it "handles comments" do use_fixture '0002-basic-commented.installfile' run_validate expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) end # HorizonScript Specification, ch 3. it "handles blank lines and indentation" do use_fixture '0003-basic-whitespace.installfile' run_validate expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) end it "requires keys to have values" do use_fixture '0015-keys-without-values.installfile' @@ -505,12 +508,33 @@ RSpec.describe 'HorizonScript Validation Utility', :type => :aruba do expect(last_command_started).to have_output(/error: .*duplicate.*rootpw/) end end + context "user account keys:" do + context "'username'" do + it "succeeds with multiple usernames" do + use_fixture '0082-username-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 duplicate usernames" do + use_fixture '0083-username-duplicate.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*duplicate.*username/) + end + it "fails with a system username" do + use_fixture '0084-username-system.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*username.*system/) + end + end + end context "package specifications" do # no requirements for these, but I think obvious. it "works with all types of package atoms" do use_fixture '0022-all-kinds-of-atoms.installfile' run_validate expect(last_command_started).to have_output(PARSER_SUCCESS) + expect(last_command_started).to have_output(VALIDATOR_SUCCESS) end it "does not accept invalid package atoms" do use_fixture '0023-pkginstall-invalid-modifier.installfile' -- cgit v1.2.3-70-g09d2