-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.sh
More file actions
executable file
·111 lines (101 loc) · 3.74 KB
/
tests.sh
File metadata and controls
executable file
·111 lines (101 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
set -euo pipefail
# On actual tests, this shall be set in environment to e.g. a local squid instance
MIRROR_OPENWRT="${MIRROR_OPENWRT:-https://downloads.openwrt.org}"
if [[ -z "${DIR_WORK:-}" ]]; then
DIR_WORK=$(mktemp -d "${TMPDIR:-/tmp}/adumpkTests.XXXXXXX")
trap "rm -rf ${DIR_WORK}" EXIT INT TERM KILL
fi
declare -A TESTS_DONE=()
do_test() { # $1: name
SEEN="${TESTS_DONE["$1"]:-}"
if [[ "${SEEN}" ]]; then
return "${SEEN}"
fi
echo '=============================='
echo "Test $1"
echo '------------------------------'
if "test_$1"; then
TESTS_DONE["$1"]=0
echo "PASS $1"
else
TESTS_DONE["$1"]=1
echo "FAIL $1"
fi
echo '=============================='
}
do_test_silent() {
do_test "$@" &>/dev/null
}
test_index_json() {
if [[ -z "${RELEASE_OPENWRT:-}" ]]; then
echo "Getting OpenWrt Latest Release from ${MIRROR_OPENWRT}..."
RELEASE_OPENWRT=$(
curl "${MIRROR_OPENWRT}/" |
sed -n 's|^.\+a href="releases/\([0-9.]\+\)/targets.\+$|\1|p;T;q'
)
echo "Latest OpenWrt Release is ${RELEASE_OPENWRT}"
fi
PREFIX_URL_OPENWRT="${MIRROR_OPENWRT}/releases/${RELEASE_OPENWRT}/packages/x86_64/packages/"
echo "Downloading x86_64 index"
local INDEX="${DIR_WORK}/packages.adb"
curl -o "${INDEX}" "${PREFIX_URL_OPENWRT}packages.adb"
JSON_PACKAGES="${DIR_WORK}/packages.json"
echo "Dumping with JSON output"
./adumpk.py --log fatal --json "${JSON_PACKAGES}" "${INDEX}"
echo "Peek into dumped json:"
head -c 50 "${JSON_PACKAGES}"
echo
}
test_index_integrity() {
do_test_silent index_json
local INFO_NGINX=$(jq '."packages"."nginx-full"' "${JSON_PACKAGES}")
echo "nginx-full info in the JSON:"
echo "${INFO_NGINX}"
local VERSION_NGINX=$(echo "${INFO_NGINX}" | jq -r '."version"')
local SIZE_NGINX=$(echo "${INFO_NGINX}" | jq -r '."file-size"')
local ALGORITHM_NGINX=$(echo "${INFO_NGINX}" | jq -r '."checksum"."type"')
local HASH_NGINX=$(echo "${INFO_NGINX}" | jq -r '."checksum"."value"')
echo "Downloading the package to check integrity, version ${VERSION_NGINX}, size ${SIZE_NGINX}, checksum(${ALGORITHM_NGINX}) = ${HASH_NGINX}"
APK_NGINX="${DIR_WORK}/nginx-full.apk"
curl -o "${APK_NGINX}" "${PREFIX_URL_OPENWRT}/nginx-full-${VERSION_NGINX}.apk"
local SIZE_NGINX_ACTUAL=$(stat -c '%s' "${APK_NGINX}")
local BAD=''
if [[ "${SIZE_NGINX}" != "${SIZE_NGINX_ACTUAL}" ]]; then
echo "Size of nginx-full pacakge is ${SIZE_NGINX_ACTUAL}, differing from ${SIZE_NGINX} recorded in index"
BAD=1
fi
JSON_NGINX="${DIR_WORK}/nginx-full.json"
echo "Dumping JSON info for actual package"
./adumpk.py --log fatal --json "${JSON_NGINX}" "${APK_NGINX}"
echo "Peek into dumped json:"
head -c 50 "${JSON_NGINX}"
echo
local HASH_NGINX_ACTUAL
HASH_NGINX_ACTUAL=$(jq -r '."identity"."'"${ALGORITHM_NGINX}"'"' "${JSON_NGINX}")
if [[ "${HASH_NGINX}" != "${HASH_NGINX_ACTUAL}" ]]; then
echo "${ALGORITHM_NGINX} identity hash of nginx-full differs, expected ${HASH_NGINX}, actual ${HASH_NGINX_ACTUAL}"
BAD=1
fi
echo "Checking if adumpk.py considers it a valid apk"
./adumpk.py --log fatal "${APK_NGINX}"
[[ -z "${BAD}" ]]
}
test_apk_tar() {
do_test_silent index_integrity
TAR_NGINX="${DIR_WORK}/nginx-full.tar"
./adumpk.py --log fatal --tar "${TAR_NGINX}" --tarsum "${APK_NGINX}"
local ERR="${DIR_WORK}/nginx-full.tar.err"
echo "Peek into tar:"
tar -tvf "${TAR_NGINX}" 2>"${ERR}"
[[ -s "${ERR}" ]]
}
if [[ "$#" -gt 0 ]]; then
for TEST in "$@"; do
do_test "${TEST}"
done
else
for TEST in index_json index_integrity apk_tar; do
do_test "${TEST}"
done
fi