From f1ef45bd84b57b40b701e094a9b49daac3761f9f Mon Sep 17 00:00:00 2001 From: "A. Wilcox" Date: Sat, 19 Oct 2019 20:01:14 -0500 Subject: hscript: Implement UserGroups, add tests --- hscript/script.cc | 3 ++- hscript/user.cc | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 3 deletions(-) (limited to 'hscript') diff --git a/hscript/script.cc b/hscript/script.cc index 8dbf9e1..0d1c10a 100644 --- a/hscript/script.cc +++ b/hscript/script.cc @@ -597,9 +597,10 @@ bool Script::validate() const { }) ) { output_error("installfile:" + std::to_string(group->lineno()), - "usergroups: group specified twice"); + "usergroups: duplicate group name specified"); failures++; } + seen_groups.insert(these.begin(), these.end()); } /* REQ: Runner.Validate.usergroups.Count */ diff --git a/hscript/user.cc b/hscript/user.cc index 04258e4..ec59a67 100644 --- a/hscript/user.cc +++ b/hscript/user.cc @@ -298,11 +298,46 @@ bool UserIcon::execute(ScriptOptions) const { Key *UserGroups::parseFromData(const std::string &data, int lineno, int *errors, int *warnings) { - return nullptr; + /* REQ: Runner.Validate.usergroups.Validity */ + const std::string::size_type sep = data.find_first_of(' '); + if(sep == std::string::npos || data.length() == sep + 1) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "usergroups: at least one group is required", + "expected format is: usergroups [user] [group(,...)]"); + return nullptr; + } + + std::set group_set; + char next_group[17]; + std::istringstream stream(data.substr(sep + 1)); + while(stream.getline(next_group, 17, ',')) { + std::string group(next_group); + /* REQ: Runner.Validate.usergroups.Group */ + if(system_groups.find(group) == system_groups.end()) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "usergroups: group name '" + group + "' is invalid", + "group is not a recognised system group"); + return nullptr; + } + group_set.insert(group); + } + /* REQ: Runner.Validate.usergroups.Group */ + if(stream.fail() && !stream.eof()) { + if(errors) *errors += 1; + output_error("installfile:" + std::to_string(lineno), + "usergroups: group name exceeds maximum length", + "groups may only be 16 characters or less"); + return nullptr; + } + + return new UserGroups(lineno, data.substr(0, sep), group_set); } bool UserGroups::validate(ScriptOptions) const { - return false; + /* All validation is done in parsing stage */ + return true; } bool UserGroups::execute(ScriptOptions) const { -- cgit v1.2.3-70-g09d2