From e13a8ba7d53dfc18278aa6f8afa550fe49e4e226 Mon Sep 17 00:00:00 2001 From: Daniel Ruthardt Date: Fri, 14 Nov 2025 13:00:46 +0100 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=85=20Add=20tests=20for=20most=20func?= =?UTF-8?q?tions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/file/tests/copy/test | 135 +++++++++++++++++++++++ libs/file/tests/copy_owners/test | 103 +++++++++++++++++ libs/file/tests/inherit_owners/test | 103 +++++++++++++++++ libs/file/tests/is_world_accessible/test | 84 ++++++++++++++ libs/file/tests/is_world_executable/test | 45 ++++++++ libs/file/tests/is_world_readable/test | 45 ++++++++ libs/file/tests/is_world_writable/test | 45 ++++++++ libs/user/tests/get_gid/test | 54 +++++++++ libs/user/tests/get_groups/test | 75 +++++++++++++ libs/user/tests/get_home/test | 34 +++--- libs/user/tests/get_shell/test | 54 +++++++++ libs/user/tests/get_uid/test | 51 +++++++++ 12 files changed, 809 insertions(+), 19 deletions(-) create mode 100644 libs/file/tests/copy/test create mode 100644 libs/file/tests/copy_owners/test create mode 100644 libs/file/tests/inherit_owners/test create mode 100644 libs/file/tests/is_world_accessible/test create mode 100644 libs/file/tests/is_world_executable/test create mode 100644 libs/file/tests/is_world_readable/test create mode 100644 libs/file/tests/is_world_writable/test create mode 100644 libs/user/tests/get_gid/test create mode 100644 libs/user/tests/get_groups/test create mode 100644 libs/user/tests/get_shell/test create mode 100644 libs/user/tests/get_uid/test diff --git a/libs/file/tests/copy/test b/libs/file/tests/copy/test new file mode 100644 index 0000000..bc01e10 --- /dev/null +++ b/libs/file/tests/copy/test @@ -0,0 +1,135 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include file/copy +#include core/test/assert_equals +#include core/test/assert_success +#include core/test/assert_contains +#include core/test/prep_step + +function file::tests::copy::test() { + if ! command -v sudo &>/dev/null; then + apt-get update -qq && apt-get install -y -qq sudo + fi + + local sudoCommand="" + if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then + sudoCommand="sudo" + fi + + local sourceFile="/tmp/source_copy_$$" + local destDir="/tmp/dest_copy_dir_$$" + + core::test::prep_step mkdir -p "$destDir" + core::test::prep_step echo "test content" > "$sourceFile" + core::test::prep_step $sudoCommand chown root:root "$destDir" + + core::test::assert_success file::copy "$sourceFile" "$destDir/" + + local copiedFile="$destDir/$(basename "$sourceFile")" + core::test::assert_success test -f "$copiedFile" + + local copiedUser + copiedUser=$(stat -c '%U' "$copiedFile") + core::test::assert_equals "root" "$copiedUser" + + local copiedGroup + copiedGroup=$(stat -c '%G' "$copiedFile") + core::test::assert_equals "root" "$copiedGroup" + + local copiedContent + copiedContent=$(cat "$copiedFile") + core::test::assert_contains "$copiedContent" "test content" + + # Test recursive directory copy + local sourceDir="/tmp/source_copy_dir_$$" + local destDir2="/tmp/dest_copy_dir2_$$" + + core::test::prep_step mkdir -p "$sourceDir"/{sub1,sub2} + core::test::prep_step mkdir -p "$destDir2" + core::test::prep_step touch "$sourceDir"/sub1/file1 + core::test::prep_step echo "nested content" > "$sourceDir"/sub2/file2 + core::test::prep_step $sudoCommand chown root:root "$destDir2" + + core::test::assert_success file::copy "$sourceDir" "$destDir2/" -r + + local copiedDir="$destDir2/$(basename "$sourceDir")" + core::test::assert_success test -d "$copiedDir" + core::test::assert_success test -d "$copiedDir/sub1" + core::test::assert_success test -f "$copiedDir/sub1/file1" + core::test::assert_success test -f "$copiedDir/sub2/file2" + + local copiedDirUser + copiedDirUser=$(stat -c '%U' "$copiedDir") + core::test::assert_equals "root" "$copiedDirUser" + + local copiedSubdirUser + copiedSubdirUser=$(stat -c '%U' "$copiedDir/sub1") + core::test::assert_equals "root" "$copiedSubdirUser" + + local copiedFile2User + copiedFile2User=$(stat -c '%U' "$copiedDir/sub2/file2") + core::test::assert_equals "root" "$copiedFile2User" + + # Test copy with preserve flag + local sourceFile2="/tmp/source_copy2_$$" + local destDir3="/tmp/dest_copy_dir3_$$" + + core::test::prep_step mkdir -p "$destDir3" + core::test::prep_step echo "preserve test" > "$sourceFile2" + core::test::prep_step chmod 644 "$sourceFile2" + core::test::prep_step $sudoCommand chown root:root "$destDir3" + + core::test::assert_success file::copy "$sourceFile2" "$destDir3/" -p + + local copiedFile3="$destDir3/$(basename "$sourceFile2")" + core::test::assert_success test -f "$copiedFile3" + + # Test multiple file copies + local sourceFile3="/tmp/source_copy3_$$" + local sourceFile4="/tmp/source_copy4_$$" + local destDir4="/tmp/dest_copy_dir4_$$" + + core::test::prep_step mkdir -p "$destDir4" + core::test::prep_step echo "file3" > "$sourceFile3" + core::test::prep_step echo "file4" > "$sourceFile4" + core::test::prep_step $sudoCommand chown root:root "$destDir4" + + core::test::assert_success file::copy "$sourceFile3" "$destDir4/" + core::test::assert_success file::copy "$sourceFile4" "$destDir4/" + + core::test::assert_success test -f "$destDir4/$(basename "$sourceFile3")" + core::test::assert_success test -f "$destDir4/$(basename "$sourceFile4")" + + # Test deeply nested directory copy + local deepSource="/tmp/deep_source_$$" + local deepDest="/tmp/deep_dest_$$" + + core::test::prep_step mkdir -p "$deepSource"/a/b/c/d + core::test::prep_step mkdir -p "$deepDest" + core::test::prep_step touch "$deepSource"/a/b/c/d/deep_file + core::test::prep_step $sudoCommand chown root:root "$deepDest" + + core::test::assert_success file::copy "$deepSource" "$deepDest/" -r + + local copiedDeep="$deepDest/$(basename "$deepSource")" + core::test::assert_success test -f "$copiedDeep/a/b/c/d/deep_file" + + local copiedDeepFileUser + copiedDeepFileUser=$(stat -c '%U' "$copiedDeep/a/b/c/d/deep_file") + core::test::assert_equals "root" "$copiedDeepFileUser" +} diff --git a/libs/file/tests/copy_owners/test b/libs/file/tests/copy_owners/test new file mode 100644 index 0000000..873ce44 --- /dev/null +++ b/libs/file/tests/copy_owners/test @@ -0,0 +1,103 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include file/copy_owners +#include core/test/assert_equals +#include core/test/assert_success +#include core/test/prep_step + +function file::tests::copy_owners::test() { + if ! command -v sudo &>/dev/null; then + apt-get update -qq && apt-get install -y -qq sudo + fi + + local sudoCommand="" + if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then + sudoCommand="sudo" + fi + + local sourceFile="/tmp/source_$$" + local destFile="/tmp/dest_$$" + + core::test::prep_step touch "$sourceFile" "$destFile" + core::test::prep_step $sudoCommand chown root:root "$sourceFile" + + core::test::assert_success file::copy_owners "$sourceFile" "$destFile" + + local destUser + destUser=$(stat -c '%U' "$destFile") + core::test::assert_equals "root" "$destUser" + + local destGroup + destGroup=$(stat -c '%G' "$destFile") + core::test::assert_equals "root" "$destGroup" + + local sourceDir="/tmp/source_dir_$$" + local destDir="/tmp/dest_dir_$$" + + core::test::prep_step mkdir -p "$sourceDir"/{sub1,sub2} + core::test::prep_step mkdir -p "$destDir"/{sub1,sub2} + core::test::prep_step touch "$sourceDir"/sub1/file1 + core::test::prep_step touch "$destDir"/sub1/file1 + core::test::prep_step $sudoCommand chown -R root:root "$sourceDir" + + core::test::assert_success file::copy_owners "$sourceDir" "$destDir" true + + local destDirUser + destDirUser=$(stat -c '%U' "$destDir") + core::test::assert_equals "root" "$destDirUser" + + local destSubdirUser + destSubdirUser=$(stat -c '%U' "$destDir/sub1") + core::test::assert_equals "root" "$destSubdirUser" + + local destSubfileUser + destSubfileUser=$(stat -c '%U' "$destDir/sub1/file1") + core::test::assert_equals "root" "$destSubfileUser" + + local sourceDir2="/tmp/source_dir2_$$" + local destDir2="/tmp/dest_dir2_$$" + local currentUser + currentUser=$(whoami) + + core::test::prep_step mkdir -p "$sourceDir2"/{sub1,sub2} + core::test::prep_step mkdir -p "$destDir2"/{sub1,sub2} + core::test::prep_step touch "$destDir2"/sub1/file1 + core::test::prep_step $sudoCommand chown -R root:root "$sourceDir2" + core::test::prep_step $sudoCommand chown "$currentUser:$currentUser" "$destDir2"/sub1/file1 + + core::test::assert_success file::copy_owners "$sourceDir2" "$destDir2" false + + local destDir2User + destDir2User=$(stat -c '%U' "$destDir2") + core::test::assert_equals "root" "$destDir2User" + + local destSubfile2User + destSubfile2User=$(stat -c '%U' "$destDir2/sub1/file1") + core::test::assert_equals "$currentUser" "$destSubfile2User" + + local sourceFile2="/tmp/source2_$$" + local destFile2="/tmp/dest2_$$" + core::test::prep_step touch "$sourceFile2" "$destFile2" + core::test::prep_step $sudoCommand chown nobody:nogroup "$sourceFile2" 2>/dev/null || core::test::prep_step $sudoCommand chown nobody:nobody "$sourceFile2" + + core::test::assert_success file::copy_owners "$sourceFile2" "$destFile2" + + local destFile2User + destFile2User=$(stat -c '%U' "$destFile2") + core::test::assert_equals "nobody" "$destFile2User" +} diff --git a/libs/file/tests/inherit_owners/test b/libs/file/tests/inherit_owners/test new file mode 100644 index 0000000..79d3685 --- /dev/null +++ b/libs/file/tests/inherit_owners/test @@ -0,0 +1,103 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include file/inherit_owners +#include core/test/assert_equals +#include core/test/assert_success +#include core/test/prep_step + +function file::tests::inherit_owners::test() { + if ! command -v sudo &>/dev/null; then + apt-get update -qq && apt-get install -y -qq sudo + fi + + local sudoCommand="" + if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then + sudoCommand="sudo" + fi + + local testDir="/tmp/parent_dir_$$" + local testFile="$testDir/child_file" + + core::test::prep_step mkdir -p "$testDir" + core::test::prep_step $sudoCommand chown root:root "$testDir" + core::test::prep_step touch "$testFile" + + core::test::assert_success file::inherit_owners "$testFile" + + local fileUser + fileUser=$(stat -c '%U' "$testFile") + core::test::assert_equals "root" "$fileUser" + + local fileGroup + fileGroup=$(stat -c '%G' "$testFile") + core::test::assert_equals "root" "$fileGroup" + + local nestedDir="$testDir/nested" + core::test::prep_step mkdir -p "$nestedDir" + + core::test::assert_success file::inherit_owners "$nestedDir" + + local nestedUser + nestedUser=$(stat -c '%U' "$nestedDir") + core::test::assert_equals "root" "$nestedUser" + + local deepDir="/tmp/deep_parent_$$" + local level1="$deepDir/level1" + local level2="$level1/level2" + local level3="$level2/level3" + local deepFile="$level3/deep_file" + + core::test::prep_step mkdir -p "$level3" + core::test::prep_step $sudoCommand chown -R root:root "$deepDir" + core::test::prep_step touch "$deepFile" + + core::test::assert_success file::inherit_owners "$deepFile" + + local deepFileUser + deepFileUser=$(stat -c '%U' "$deepFile") + core::test::assert_equals "root" "$deepFileUser" + + local deepFileGroup + deepFileGroup=$(stat -c '%G' "$deepFile") + core::test::assert_equals "root" "$deepFileGroup" + + local parentDir2="/tmp/parent_dir2_$$" + local childDir="$parentDir2/child" + local currentUser + currentUser=$(whoami) + + core::test::prep_step mkdir -p "$parentDir2" + core::test::prep_step $sudoCommand chown nobody:nogroup "$parentDir2" 2>/dev/null || core::test::prep_step $sudoCommand chown nobody:nobody "$parentDir2" + core::test::prep_step mkdir "$childDir" + core::test::prep_step $sudoCommand chown "$currentUser:$currentUser" "$childDir" + + core::test::assert_success file::inherit_owners "$childDir" + + local childDirUser + childDirUser=$(stat -c '%U' "$childDir") + core::test::assert_equals "nobody" "$childDirUser" + + local nestedFile="$testDir/nested/file_in_nested" + core::test::prep_step touch "$nestedFile" + + core::test::assert_success file::inherit_owners "$nestedFile" + + local nestedFileUser + nestedFileUser=$(stat -c '%U' "$nestedFile") + core::test::assert_equals "root" "$nestedFileUser" +} diff --git a/libs/file/tests/is_world_accessible/test b/libs/file/tests/is_world_accessible/test new file mode 100644 index 0000000..51f6495 --- /dev/null +++ b/libs/file/tests/is_world_accessible/test @@ -0,0 +1,84 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include file/is_world_accessible +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/assert_exit_code +#include core/test/prep_step + +function file::tests::is_world_accessible::test() { + local testDir="/tmp/test_accessible_dir_$$" + local testFile="$testDir/test_file" + + core::test::prep_step mkdir -p "$testDir" + core::test::prep_step touch "$testFile" + + # Test with world-readable file in accessible directory + core::test::prep_step chmod 755 "$testDir" + core::test::prep_step chmod 644 "$testFile" + core::test::assert_success file::is_world_accessible "$testFile" + + # Test with file in directory without world access + core::test::prep_step chmod 750 "$testDir" + core::test::assert_exit_code 2 file::is_world_accessible "$testFile" + + # Test with world-readable file again + core::test::prep_step chmod 755 "$testDir" + core::test::prep_step chmod 644 "$testFile" + core::test::assert_success file::is_world_accessible "$testFile" + + # Test directory without world access + core::test::prep_step chmod 700 "$testDir" + core::test::assert_exit_code 2 file::is_world_accessible "$testDir" + + # Test deeply nested file + local nestedDir="$testDir/level1/level2/level3" + local nestedFile="$nestedDir/deep_file" + core::test::prep_step mkdir -p "$nestedDir" + core::test::prep_step touch "$nestedFile" + + core::test::prep_step chmod -R 755 "$testDir" + core::test::prep_step chmod 644 "$nestedFile" + core::test::assert_success file::is_world_accessible "$nestedFile" + + # Test with restricted parent at level1 + core::test::prep_step chmod 750 "$testDir/level1" + core::test::assert_exit_code 2 file::is_world_accessible "$nestedFile" + + # Test with restricted parent at level2 + core::test::prep_step chmod 755 "$testDir/level1" + core::test::prep_step chmod 700 "$testDir/level1/level2" + core::test::assert_exit_code 2 file::is_world_accessible "$nestedFile" + + # Test with all directories accessible + core::test::prep_step chmod -R 755 "$testDir" + core::test::prep_step chmod 644 "$nestedFile" + core::test::assert_success file::is_world_accessible "$nestedFile" + + # Test with non-world-readable file + core::test::prep_step chmod 600 "$nestedFile" + core::test::assert_exit_code 1 file::is_world_accessible "$nestedFile" + + # Test accessible directory + core::test::prep_step chmod 755 "$testDir" + core::test::assert_success file::is_world_accessible "$testDir" + + # Test non-accessible directory + core::test::prep_step chmod 700 "$testDir" + core::test::assert_exit_code 2 file::is_world_accessible "$testDir" +} diff --git a/libs/file/tests/is_world_executable/test b/libs/file/tests/is_world_executable/test new file mode 100644 index 0000000..58162e0 --- /dev/null +++ b/libs/file/tests/is_world_executable/test @@ -0,0 +1,45 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include file/is_world_executable +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/prep_step + +function file::tests::is_world_executable::test() { + local testFile="/tmp/test_executable_$$" + + core::test::prep_step touch "$testFile" + + core::test::prep_step chmod 755 "$testFile" + core::test::assert_success file::is_world_executable "$testFile" + + core::test::prep_step chmod 775 "$testFile" + core::test::assert_success file::is_world_executable "$testFile" + + core::test::prep_step chmod 705 "$testFile" + core::test::assert_success file::is_world_executable "$testFile" + + core::test::prep_step chmod 750 "$testFile" + core::test::assert_failure file::is_world_executable "$testFile" + + core::test::prep_step chmod 700 "$testFile" + core::test::assert_failure file::is_world_executable "$testFile" + + core::test::prep_step chmod 555 "$testFile" + core::test::assert_success file::is_world_executable "$testFile" +} diff --git a/libs/file/tests/is_world_readable/test b/libs/file/tests/is_world_readable/test new file mode 100644 index 0000000..9603c99 --- /dev/null +++ b/libs/file/tests/is_world_readable/test @@ -0,0 +1,45 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include file/is_world_readable +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/prep_step + +function file::tests::is_world_readable::test() { + local testFile="/tmp/test_readable_$$" + + core::test::prep_step touch "$testFile" + + core::test::prep_step chmod 644 "$testFile" + core::test::assert_success file::is_world_readable "$testFile" + + core::test::prep_step chmod 664 "$testFile" + core::test::assert_success file::is_world_readable "$testFile" + + core::test::prep_step chmod 604 "$testFile" + core::test::assert_success file::is_world_readable "$testFile" + + core::test::prep_step chmod 640 "$testFile" + core::test::assert_failure file::is_world_readable "$testFile" + + core::test::prep_step chmod 600 "$testFile" + core::test::assert_failure file::is_world_readable "$testFile" + + core::test::prep_step chmod 444 "$testFile" + core::test::assert_success file::is_world_readable "$testFile" +} diff --git a/libs/file/tests/is_world_writable/test b/libs/file/tests/is_world_writable/test new file mode 100644 index 0000000..59574d5 --- /dev/null +++ b/libs/file/tests/is_world_writable/test @@ -0,0 +1,45 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include file/is_world_writable +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/prep_step + +function file::tests::is_world_writable::test() { + local testFile="/tmp/test_writable_$$" + + core::test::prep_step touch "$testFile" + + core::test::prep_step chmod 666 "$testFile" + core::test::assert_success file::is_world_writable "$testFile" + + core::test::prep_step chmod 662 "$testFile" + core::test::assert_success file::is_world_writable "$testFile" + + core::test::prep_step chmod 606 "$testFile" + core::test::assert_success file::is_world_writable "$testFile" + + core::test::prep_step chmod 640 "$testFile" + core::test::assert_failure file::is_world_writable "$testFile" + + core::test::prep_step chmod 644 "$testFile" + core::test::assert_failure file::is_world_writable "$testFile" + + core::test::prep_step chmod 222 "$testFile" + core::test::assert_success file::is_world_writable "$testFile" +} diff --git a/libs/user/tests/get_gid/test b/libs/user/tests/get_gid/test new file mode 100644 index 0000000..c6a3acb --- /dev/null +++ b/libs/user/tests/get_gid/test @@ -0,0 +1,54 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include user/get_gid +#include core/test/assert_equals +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/prep_step + +function user::tests::get_gid::test() { + local sudoCommand="" + if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then + sudoCommand="sudo" + fi + + # Test with explicit username + core::test::assert_success user::get_gid root + core::test::assert_equals 0 "$(user::get_gid root)" + + # Test with no argument (defaults to current user) + core::test::assert_success user::get_gid + local currentGid + currentGid=$(id -g) + core::test::assert_equals "$currentGid" "$(user::get_gid)" + + # Test with $user environment variable + local testUser="root" + user=$testUser core::test::assert_success user::get_gid + user=$testUser core::test::assert_equals 0 "$(user::get_gid)" + + # Test with custom user + core::test::prep_step $sudoCommand useradd -g 5000 testuser >/dev/null 2>&1 || core::test::prep_step $sudoCommand groupadd -g 5000 testgroup >/dev/null 2>&1 && core::test::prep_step $sudoCommand useradd -g testgroup testuser >/dev/null + core::test::assert_success user::get_gid testuser + core::test::assert_equals 5000 "$(user::get_gid testuser)" + core::test::prep_step $sudoCommand userdel -r testuser >/dev/null + core::test::prep_step $sudoCommand groupdel testgroup >/dev/null 2>&1 || true + + # Test with non-existent user + core::test::assert_failure user::get_gid non_existent_user +} diff --git a/libs/user/tests/get_groups/test b/libs/user/tests/get_groups/test new file mode 100644 index 0000000..a4bb801 --- /dev/null +++ b/libs/user/tests/get_groups/test @@ -0,0 +1,75 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include user/get_groups +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/assert_contains +#include core/test/prep_step + +function user::tests::get_groups::test() { + local sudoCommand="" + if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then + sudoCommand="sudo" + fi + + # Test with explicit username + core::test::assert_success user::get_groups root + local rootGroups + rootGroups=$(user::get_groups root) + core::test::assert_contains "$rootGroups" "root" + + # Test with no argument (defaults to current user) + core::test::assert_success user::get_groups + local currentUser + currentUser=$(whoami) + local groupsWithoutArgument + groupsWithoutArgument=$(user::get_groups) + local groupsWithArgument + groupsWithArgument=$(user::get_groups "$currentUser") + core::test::assert_equals "$groupsWithArgument" "$groupsWithoutArgument" + + # Test with $user environment variable + local testUser="root" + user=$testUser core::test::assert_success user::get_groups + user=$testUser core::test::assert_contains "$(user::get_groups)" "root" + + # Test with custom user and single group + core::test::prep_step $sudoCommand useradd testuser >/dev/null + core::test::assert_success user::get_groups testuser + local testuserGroups + testuserGroups=$(user::get_groups testuser) + core::test::assert_contains "$testuserGroups" "testuser" + core::test::prep_step $sudoCommand userdel -r testuser >/dev/null + + # Test with custom user and multiple groups + core::test::prep_step $sudoCommand groupadd testgroup1 >/dev/null + core::test::prep_step $sudoCommand groupadd testgroup2 >/dev/null + core::test::prep_step $sudoCommand useradd -G testgroup1,testgroup2 testuser >/dev/null + core::test::assert_success user::get_groups testuser + local multiGroupUser + multiGroupUser=$(user::get_groups testuser) + core::test::assert_contains "$multiGroupUser" "testuser" + core::test::assert_contains "$multiGroupUser" "testgroup1" + core::test::assert_contains "$multiGroupUser" "testgroup2" + core::test::prep_step $sudoCommand userdel -r testuser >/dev/null + core::test::prep_step $sudoCommand groupdel testgroup1 >/dev/null + core::test::prep_step $sudoCommand groupdel testgroup2 >/dev/null + + # Test with non-existent user + core::test::assert_failure user::get_groups non_existent_user +} diff --git a/libs/user/tests/get_home/test b/libs/user/tests/get_home/test index 54b87d4..1044b9a 100644 --- a/libs/user/tests/get_home/test +++ b/libs/user/tests/get_home/test @@ -22,40 +22,36 @@ #include core/test/prep_step function user::tests::get_home::test() { - local sudo="" + local sudoCommand="" if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then - sudo="sudo" + sudoCommand="sudo" fi # Test with explicit username core::test::assert_success user::get_home root core::test::assert_equals /root "$(user::get_home root)" - # Test with no argument (should default to current user) - local current_user - current_user=$(whoami) - local expected_home - expected_home=$(user::get_home "$current_user") + # Test with no argument (defaults to current user) core::test::assert_success user::get_home - core::test::assert_equals "$expected_home" "$(user::get_home)" + core::test::assert_equals "$HOME" "$(user::get_home)" - # Test with $user environment variable set (should use $user instead of whoami) - local test_user="root" - user=$test_user core::test::assert_success user::get_home - user=$test_user core::test::assert_equals /root "$(user::get_home)" + # Test with $user environment variable + local testUser="root" + user=$testUser core::test::assert_success user::get_home + user=$testUser core::test::assert_equals /root "$(user::get_home)" - # Test with a custom user - core::test::prep_step $sudo useradd -m testuser >/dev/null + # Test with custom user + core::test::prep_step $sudoCommand useradd -m testuser >/dev/null core::test::assert_success user::get_home testuser core::test::assert_equals /home/testuser "$(user::get_home testuser)" - core::test::prep_step $sudo userdel -r testuser >/dev/null + core::test::prep_step $sudoCommand userdel -r testuser >/dev/null - # Test with a custom home directory - core::test::prep_step $sudo useradd -m -d /tmp/testuser testuser >/dev/null + # Test with custom home directory + core::test::prep_step $sudoCommand useradd -m -d /tmp/testuser testuser >/dev/null core::test::assert_success user::get_home testuser core::test::assert_equals /tmp/testuser "$(user::get_home testuser)" - core::test::prep_step $sudo userdel -r testuser >/dev/null + core::test::prep_step $sudoCommand userdel -r testuser >/dev/null - # Test with a non-existent user + # Test with non-existent user core::test::assert_failure user::get_home non_existent_user } diff --git a/libs/user/tests/get_shell/test b/libs/user/tests/get_shell/test new file mode 100644 index 0000000..cc36ec9 --- /dev/null +++ b/libs/user/tests/get_shell/test @@ -0,0 +1,54 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include user/get_shell +#include core/test/assert_equals +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/assert_contains +#include core/test/prep_step + +function user::tests::get_shell::test() { + local sudoCommand="" + if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then + sudoCommand="sudo" + fi + + # Test with explicit username + core::test::assert_success user::get_shell root + local rootShell + rootShell=$(user::get_shell root) + core::test::assert_contains "$rootShell" "sh" + + # Test with no argument (defaults to current user) + core::test::assert_success user::get_shell + core::test::assert_contains "$(user::get_shell)" "sh" + + # Test with $user environment variable + local testUser="root" + user=$testUser core::test::assert_success user::get_shell + user=$testUser core::test::assert_contains "$(user::get_shell)" "sh" + + # Test with custom user and shell + core::test::prep_step $sudoCommand useradd -s /bin/bash testuser >/dev/null + core::test::assert_success user::get_shell testuser + core::test::assert_equals /bin/bash "$(user::get_shell testuser)" + core::test::prep_step $sudoCommand userdel -r testuser >/dev/null + + # Test with non-existent user + core::test::assert_failure user::get_shell non_existent_user +} diff --git a/libs/user/tests/get_uid/test b/libs/user/tests/get_uid/test new file mode 100644 index 0000000..1641db2 --- /dev/null +++ b/libs/user/tests/get_uid/test @@ -0,0 +1,51 @@ +# shellcheck shell=bash +# +# Copyright 2025 The Linux Foundation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +#include user/get_uid +#include core/test/assert_equals +#include core/test/assert_success +#include core/test/assert_failure +#include core/test/prep_step + +function user::tests::get_uid::test() { + local sudoCommand="" + if [[ $EUID -ne 0 ]] && command -v sudo &>/dev/null; then + sudoCommand="sudo" + fi + + # Test with explicit username + core::test::assert_success user::get_uid root + core::test::assert_equals 0 "$(user::get_uid root)" + + # Test with no argument (defaults to current user) + core::test::assert_success user::get_uid + core::test::assert_equals $UID "$(user::get_uid)" + + # Test with $user environment variable + local testUser="root" + user=$testUser core::test::assert_success user::get_uid + user=$testUser core::test::assert_equals 0 "$(user::get_uid)" + + # Test with custom user + core::test::prep_step $sudoCommand useradd -u 5000 testuser >/dev/null + core::test::assert_success user::get_uid testuser + core::test::assert_equals 5000 "$(user::get_uid testuser)" + core::test::prep_step $sudoCommand userdel -r testuser >/dev/null + + # Test with non-existent user + core::test::assert_failure user::get_uid non_existent_user +} From b9d34c0217e61407e9bdb19e04ac1c8f703a7f69 Mon Sep 17 00:00:00 2001 From: Daniel Ruthardt Date: Fri, 14 Nov 2025 13:11:09 +0100 Subject: [PATCH 2/2] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Simplify=20preparation?= =?UTF-8?q?=20steps?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libs/user/tests/get_gid/test | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libs/user/tests/get_gid/test b/libs/user/tests/get_gid/test index c6a3acb..bada401 100644 --- a/libs/user/tests/get_gid/test +++ b/libs/user/tests/get_gid/test @@ -43,11 +43,12 @@ function user::tests::get_gid::test() { user=$testUser core::test::assert_equals 0 "$(user::get_gid)" # Test with custom user - core::test::prep_step $sudoCommand useradd -g 5000 testuser >/dev/null 2>&1 || core::test::prep_step $sudoCommand groupadd -g 5000 testgroup >/dev/null 2>&1 && core::test::prep_step $sudoCommand useradd -g testgroup testuser >/dev/null + core::test::prep_step $sudoCommand groupadd -g 5000 testgroup >/dev/null + core::test::prep_step $sudoCommand useradd -g testgroup testuser >/dev/null core::test::assert_success user::get_gid testuser core::test::assert_equals 5000 "$(user::get_gid testuser)" core::test::prep_step $sudoCommand userdel -r testuser >/dev/null - core::test::prep_step $sudoCommand groupdel testgroup >/dev/null 2>&1 || true + core::test::prep_step $sudoCommand groupdel testgroup >/dev/null # Test with non-existent user core::test::assert_failure user::get_gid non_existent_user