From 47558ea71d523fb71307d394be4bfad8da4664d8 Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sat, 12 Oct 2019 00:15:48 -0500 Subject: hscript: Parse 'mount' key, add tests to ensure --- hscript/disk.cc | 32 ++++++++++++++++++++++ tests/fixtures/0027-mount-invalid-dev.installfile | 5 ++++ tests/fixtures/0028-mount-non-absolute.installfile | 5 ++++ tests/fixtures/0029-mount-too-many.installfile | 5 ++++ tests/fixtures/0030-mount-too-few.installfile | 5 ++++ tests/spec/validator.rb | 30 ++++++++++++++++++++ 6 files changed, 82 insertions(+) create mode 100644 tests/fixtures/0027-mount-invalid-dev.installfile create mode 100644 tests/fixtures/0028-mount-non-absolute.installfile create mode 100644 tests/fixtures/0029-mount-too-many.installfile create mode 100644 tests/fixtures/0030-mount-too-few.installfile diff --git a/hscript/disk.cc b/hscript/disk.cc index d491430..093c111 100644 --- a/hscript/disk.cc +++ b/hscript/disk.cc @@ -20,14 +20,46 @@ using namespace Horizon::Keys; Key *Mount::parseFromData(const std::string data, int lineno, int *errors, int *warnings) { std::string dev, where, opt; + std::string::size_type where_pos, opt_pos; + bool any_failure = false; + long spaces = std::count(data.cbegin(), data.cend(), ' '); if(spaces < 1 || spaces > 2) { if(errors) *errors += 1; + /* Don't bother with any_failure, because this is immediately fatal. */ output_error("installfile:" + std::to_string(lineno), "mount: expected either 2 or 3 elements, got: " + std::to_string(spaces), ""); return nullptr; } + + where_pos = data.find_first_of(' '); + opt_pos = data.find_first_of(' ', where_pos); + + dev = data.substr(0, where_pos); + where = data.substr(where_pos + 1, opt_pos); + if(opt_pos != std::string::npos) { + opt = data.substr(opt_pos); + } + + if(dev.compare(0, 4, "/dev")) { + if(errors) *errors += 1; + any_failure = true; + output_error("installfile:" + std::to_string(lineno), + "mount: element 1: expected device node", + "'" + dev + "' is not a valid device node"); + } + + if(where[0] != '/') { + if(errors) *errors += 1; + any_failure = true; + output_error("installfile:" + std::to_string(lineno), + "mount: element 2: expected absolute path", + "'" + where + "' is not a valid absolute path"); + } + + if(any_failure) return nullptr; + return new Mount(lineno, dev, where, opt); } diff --git a/tests/fixtures/0027-mount-invalid-dev.installfile b/tests/fixtures/0027-mount-invalid-dev.installfile new file mode 100644 index 0000000..22c65df --- /dev/null +++ b/tests/fixtures/0027-mount-invalid-dev.installfile @@ -0,0 +1,5 @@ +network true +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount nonexistant / diff --git a/tests/fixtures/0028-mount-non-absolute.installfile b/tests/fixtures/0028-mount-non-absolute.installfile new file mode 100644 index 0000000..b7932ac --- /dev/null +++ b/tests/fixtures/0028-mount-non-absolute.installfile @@ -0,0 +1,5 @@ +network true +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 usr diff --git a/tests/fixtures/0029-mount-too-many.installfile b/tests/fixtures/0029-mount-too-many.installfile new file mode 100644 index 0000000..6b1b221 --- /dev/null +++ b/tests/fixtures/0029-mount-too-many.installfile @@ -0,0 +1,5 @@ +network true +hostname test.machine +pkginstall adelie-base +rootpw $6$gumtLGmHwOVIRpQR$2M9PUO24hy5mofzWWf9a.YLbzOgOlUby1g0hDj.wG67E2wrrvys59fq02PPdxBdbgkLZFtjfEx6MHZwMBamwu/ +mount /dev/sda1 / relatime Cat diff --git a/tests/fixtures/0030-mount-too-few.installfile b/tests/fixtures/0030-mount-too-few.installfile new file mode 100644 index 0000000..0f11c37 --- /dev/null +++ b/tests/fixtures/0030-mount-too-few.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 497db58..b1cd2ab 100644 --- a/tests/spec/validator.rb +++ b/tests/spec/validator.rb @@ -152,6 +152,36 @@ RSpec.describe 'HorizonScript Validation Utility', :type => :aruba do run_validate expect(last_command_started).to have_output(/error: .*mount.*/) end + # Runner.Validate.mount. + it "fails with too many values in 'mount' tuple" do + use_fixture '0029-mount-too-many.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*mount.*elements/) + end + # Runner.Validate.mount. + it "fails with too few values in 'mount' tuple" do + use_fixture '0030-mount-too-few.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*mount.*elements/) + end + # Runner.Validate.mount.Block. + it "fails with a 'mount' value that has no block device" do + use_fixture '0027-mount-invalid-dev.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*mount.*device/) + end + # Runner.Validate.mount.Point. + it "fails with a 'mount' value that has an invalid mountpoint" do + use_fixture '0028-mount-non-absolute.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*mount.*path/) + end + # Runner.Validate.mount.Unique. + it "fails with two root 'mount' keys" do + use_fixture '0021-duplicate-root-mount.installfile' + run_validate + expect(last_command_started).to have_output(/error: .*mount.*duplicate/) + end end context "unique keys" do # Runner.Validate.network. -- cgit v1.2.3-60-g2f50