Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions libs/file/tests/copy/test
Original file line number Diff line number Diff line change
@@ -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"
}
103 changes: 103 additions & 0 deletions libs/file/tests/copy_owners/test
Original file line number Diff line number Diff line change
@@ -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"
}
103 changes: 103 additions & 0 deletions libs/file/tests/inherit_owners/test
Original file line number Diff line number Diff line change
@@ -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"
}
Loading