summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXiaotian Wu <yetist@gmail.com>2023-01-03 21:31:16 +0800
committerXiaotian Wu <yetist@gmail.com>2023-10-14 20:36:35 +0800
commit73d806fa91586ff932a2586ecca4a8d625ae2d27 (patch)
tree7d6f87f5b955e9389a22c63451f84ce4654962a0
parentc9269e051583aa23bb7e92574f73624f863b4a26 (diff)
tests: add test case for kver_zimage
-rw-r--r--test/cases/functions.bats76
-rw-r--r--test/helpers/common.bash79
2 files changed, 142 insertions, 13 deletions
diff --git a/test/cases/functions.bats b/test/cases/functions.bats
index 43e3b9d..c1f4e8b 100644
--- a/test/cases/functions.bats
+++ b/test/cases/functions.bats
@@ -12,28 +12,28 @@ setup() {
}
@test "detect_compression bzip2" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'bzip2')"
run detect_compression "$tmp_img"
assert_output "bzip2"
}
@test "detect_compression cat" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'cat')"
run detect_compression "$tmp_img"
assert_output ""
}
@test "detect_compression gzip" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'gzip')"
run detect_compression "$tmp_img"
assert_output "gzip"
}
@test "detect_compression lz4" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'lz4')"
run detect_compression "$tmp_img"
assert_output --partial "==> ERROR: Newer lz4 stream format detected! This may not boot!"
@@ -41,21 +41,21 @@ setup() {
}
@test "detect_compression lz4 (legacy)" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'lz4' '-l')"
run detect_compression "$tmp_img"
assert_output "lz4 -l"
}
@test "detect_compression lzma" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'lzma')"
run detect_compression "$tmp_img"
assert_output "lzma"
}
@test "detect_compression lzop" {
- local tmp_img
+ local tmp_img=''
__check_binary "lzop"
tmp_img="$(__gen_test_image 'lzop')"
run detect_compression "$tmp_img"
@@ -63,27 +63,83 @@ setup() {
}
@test "detect_compression xz" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'xz' '--check=crc32')"
run detect_compression "$tmp_img"
assert_output "xz"
}
@test "detect_compression zstd" {
- local tmp_img
+ local tmp_img=''
tmp_img="$(__gen_test_image 'zstd' '-T0')"
run detect_compression "$tmp_img"
assert_output "zstd"
}
+@test "detect_compression zimg" {
+ local tmp_img=''
+
+ tmp_img="$(__gen_test_image 'zimg')"
+ run detect_compression "$tmp_img"
+ assert_output "zimg"
+}
+
@test "kver_x86" {
- local kernel_ver tmp_knl
+ local kernel_ver='' tmp_knl=''
kernel_ver="6.0.9-arch1-1 #1 SMP PREEMPT_DYNAMIC Wed, 16 Nov 2022 17:01:17 +0000 x86_64 GNU/Linux"
tmp_knl=$(__gen_test_kernel "$kernel_ver")
run kver_x86 "$tmp_knl"
assert_output "6.0.9-arch1-1"
}
+@test "kver_zimage gzip" {
+ local kernel_ver='' tmp_knl='' tmp_img=''
+ kernel_ver="Linux version 6.1.0-rc5-5 #1 SMP Sat, 17 Dec 2022 05:05:29 +0000 loongarch64 GNU/Linux"
+ tmp_img="$(__gen_test_zboot_kernel "$kernel_ver" 'gzip')"
+ run kver_zimage "$tmp_img"
+ assert_output "6.1.0-rc5-5"
+}
+
+@test "kver_zimage lz4" {
+ local kernel_ver='' tmp_knl='' tmp_img=''
+ kernel_ver="Linux version 6.1.0-rc5-5 #1 SMP Sat, 17 Dec 2022 05:05:29 +0000 loongarch64 GNU/Linux"
+ tmp_img="$(__gen_test_zboot_kernel "$kernel_ver" 'lz4')"
+ run kver_zimage "$tmp_img"
+ assert_output "6.1.0-rc5-5"
+}
+
+@test "kver_zimage lzma" {
+ local kernel_ver='' tmp_knl='' tmp_img=''
+ kernel_ver="Linux version 6.1.0-rc5-5 #1 SMP Sat, 17 Dec 2022 05:05:29 +0000 loongarch64 GNU/Linux"
+ tmp_img="$(__gen_test_zboot_kernel "$kernel_ver" 'lzma')"
+ run kver_zimage "$tmp_img"
+ assert_output "6.1.0-rc5-5"
+}
+
+@test "kver_zimage lzo" {
+ local kernel_ver='' tmp_knl='' tmp_img=''
+ kernel_ver="Linux version 6.1.0-rc5-5 #1 SMP Sat, 17 Dec 2022 05:05:29 +0000 loongarch64 GNU/Linux"
+ tmp_img="$(__gen_test_zboot_kernel "$kernel_ver" 'lzo')"
+ run kver_zimage "$tmp_img"
+ assert_output "6.1.0-rc5-5"
+}
+
+@test "kver_zimage xz" {
+ local kernel_ver='' tmp_knl='' tmp_img=''
+ kernel_ver="Linux version 6.1.0-rc5-5 #1 SMP Sat, 17 Dec 2022 05:05:29 +0000 loongarch64 GNU/Linux"
+ tmp_img="$(__gen_test_zboot_kernel "$kernel_ver" 'xzkern')"
+ run kver_zimage "$tmp_img"
+ assert_output "6.1.0-rc5-5"
+}
+
+@test "kver_zimage zstd" {
+ local kernel_ver='' tmp_knl='' tmp_img=''
+ kernel_ver="Linux version 6.1.0-arch1-2 #1 SMP Sat, 17 Dec 2022 05:05:29 +0000 loongarch64 GNU/Linux"
+ tmp_img="$(__gen_test_zboot_kernel "$kernel_ver" 'zstd22')"
+ run kver_zimage "$tmp_img"
+ assert_output "6.1.0-arch1-2"
+}
+
@test "add_binary script" {
local tmp_bin BUILDROOT="${BATS_RUN_TMPDIR}/buildroot.${BATS_TEST_NAME}/" interpreter="/usr/local/${BATS_TEST_NAME}.${RANDOM}" _optquiet=1
diff --git a/test/helpers/common.bash b/test/helpers/common.bash
index b6b2a22..fc954db 100644
--- a/test/helpers/common.bash
+++ b/test/helpers/common.bash
@@ -9,8 +9,15 @@ __gen_test_image() {
tmp_file="$(mktemp --tmpdir="$BATS_RUN_TMPDIR" tmp_file.XXXXXX)"
trap '{ rm -f -- "$tmp_img"; }' EXIT
trap '{ rm -f -- "$tmp_file"; }' EXIT
- echo "this is a test file" >"$tmp_file"
- bsdtar -cf - "$tmp_file" | "${compress_opts[@]}" >"$tmp_img"
+ echo "this is a test file" > "$tmp_file"
+
+ if [[ ${compress_opts[0]} == "zimg" ]]; then
+ bsdtar -cf - "$tmp_file" > "$tmp_img"
+ # write "zimg" into image at 0x04
+ printf "zimg" | dd of="$tmp_img" seek=$((0x4)) bs=1 count=4 status=none conv=notrunc
+ else
+ bsdtar -cf - "$tmp_file" | "${compress_opts[@]}" > "$tmp_img"
+ fi
rm -f -- "$tmp_file"
echo "$tmp_img"
}
@@ -31,7 +38,73 @@ __gen_test_kernel() {
echo "$tmp_knl"
}
-__check_binary() {
+# Generates a temporary zboot dummy kernel, with the passed string as kernel version and specified compression type
+__gen_test_zboot_kernel() {
+ local kernel_ver="$1"
+ local comp_type="$2"
+ local tmp_img tmp_file
+ local num count size start
+ local compress_opts
+
+ tmp_img="$(mktemp --tmpdir="$BATS_RUN_TMPDIR" tmp_img.XXXXXX)"
+ tmp_file="$(mktemp --tmpdir="$BATS_RUN_TMPDIR" tmp_file.XXXXXX)"
+
+ # generate image with random size between 256 to 1024, 4 bytes align
+ num=$(head -n 10 /dev/urandom | cksum | awk -F ' ' '{print $1}')
+ count=$((num%192*4+512))
+ dd if=/dev/zero of="$tmp_img" count="$count" bs=1 status=none
+
+ trap '{ rm -f -- "$tmp_img"; }' EXIT
+ trap '{ rm -f -- "$tmp_file"; }' EXIT
+
+ case $comp_type in
+ gzip)
+ compress_opts=("gzip")
+ ;;
+ lz4)
+ compress_opts=("lz4")
+ ;;
+ lzma)
+ compress_opts=("lzma")
+ ;;
+ lzo)
+ compress_opts=("lzop")
+ ;;
+ xzkern)
+ compress_opts=("xz" "--check=crc32")
+ ;;
+ zstd22)
+ compress_opts=("zstd" "-T0")
+ ;;
+ *)
+ echo "Compress type is not supported"
+ return 1
+ ;;
+ esac
+
+ echo "$kernel_ver" | "${compress_opts[@]}" > "$tmp_file"
+ cat "$tmp_file" >> "$tmp_img"
+ size=$(stat -c %s "$tmp_file")
+
+ # write "zimg" into image at 0x04
+ printf "zimg" | dd of="$tmp_img" seek="$((0x4))" bs=1 count=4 status=none conv=notrunc
+
+ # write COMP_TYPE string into image at 0x18
+ printf '%s' "$comp_type" | dd of="$tmp_img" seek="$((0x18))" bs=1 count=36 status=none conv=notrunc
+
+ # write compress data start offset at 0x08
+ start=$(printf "%08x" "$count")
+ echo -n -e "\\x${start:6:2}\\x${start:4:2}\\x${start:2:2}\\x${start:0:2}" | dd of="$tmp_img" seek=$((0x8)) bs=1 count=4 status=none conv=notrunc
+
+ # write compress data size offset at 0x0c
+ size=$(printf "%08x\n" "$size")
+ echo -n -e "\\x${size:6:2}\\x${size:4:2}\\x${size:2:2}\\x${size:0:2}" | dd of="$tmp_img" seek=$((0xc)) bs=1 count=4 status=none conv=notrunc
+
+ rm -f -- "$tmp_file"
+ echo "$tmp_img"
+}
+
+__check_binary(){
local binary="$1"
if ! command -v "${binary}" &>/dev/null; then
skip "${binary} not installed"