#!/bin/sh # Recipe formats (BUILD_DESIGN.txt sections 3, 4 and 8; step 1): # - gl_recipe_parse: the recipe's fields, strictly (unknown keys, # missing required ones, bad names, versions, URLs, hashes, traits, # IF conditions, arguments, @include and NUL are all refused); # - gl_recipe_read: a bundle holds exactly NAME-VER/{recipe,build, # patches/FILE}, regular files and directories only, the patches # PATCHES lists, a build file for BUILD = "hooks", 16 MiB at most; # - gl_index_parse: a REPO named X-src has BUILD_DEPS, TRAITS, IF and # .grcp files; a binary index has none of them; # - PKG_TRAITS in a manifest, read by gl_read_pkg_manifest; # - grepo -r X-src indexes DIR's bundles, and syspkg -y accepts it. # Runs INSIDE the Dockerfile.alpine_root image: # # docker run --rm -v "$PWD/tests/recipe_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh FAILS=0 check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; } nocolor() { sed 's/\x1b\[[0-9;]*m//g'; } R() { OUT=$( ("$@"; echo "rc=$?") 2>&1 | nocolor) RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1) echo "$OUT" | sed '/^rc=/d; s/^/ /' } has() { echo "$OUT" | grep -qF -- "$1"; } lacks() { ! has "$1"; } rc() { [ "$RC" = "$1" ]; } hr() { echo; echo "== $*"; } export PATH=/app:$PATH T=/tmp/rt mkdir -p $T SHA=sha256:$(printf '%064d' 0 | tr 0 a) # probe parse FILE | read BUNDLE | index REPO FILE | manifest GPKG # prints "status=N" and the parsed fields. cat > $T/probe.lua <<'EOF' package.path = "/app/?.lua;" .. package.path local ffi = require("ffi") require("lib/glacier_cdef") local pkg = ffi.load("/app/lib/glacier/libglacier_pkg.so") local ist = ffi.load("/app/lib/glacier/libglacier_istoreutils.so") local mode, a, b = arg[1], arg[2], arg[3] local function slurp(p) local f = assert(io.open(p, "rb")); local s = f:read("*a"); f:close(); return s end local function show(k, v) if v ~= nil then io.write(k, "=[", ffi.string(v), "]\n") end end if mode == "parse" or mode == "read" then local r = ffi.new("gl_recipe_t") local st if mode == "parse" then local s = slurp(a) st = pkg.gl_recipe_parse(s, #s, r) else st = ist.gl_recipe_read(a, r) end print("status=" .. tonumber(st)) if st == 0 then for _, k in ipairs({ "name", "ver", "repo", "desc", "maint", "copyright", "sources", "patches", "build", "configure", "traits", "trait_args", "deps", "build_deps", "confs" }) do show(k, r[k]) end pkg.gl_recipe_free(r) end elseif mode == "index" then local s = slurp(b) local idx = ffi.new("gl_index_t") local st = pkg.gl_index_parse(s, #s, a, idx) print("status=" .. tonumber(st)) if st == 0 then for k = 0, tonumber(idx.npkgs) - 1 do local p = idx.pkgs[k] show("file", p.file); show("deps", p.deps); show("confs", p.confs) show("build_deps", p.build_deps); show("traits", p.traits) end pkg.gl_index_free(idx) end elseif mode == "manifest" then local m = ffi.new("gl_pkg_t") local st = ist.gl_read_pkg_manifest(a, m) print("status=" .. tonumber(st)) if st == 0 then if m.pkg_traits == nil then print("traits=NULL") else show("traits", m.pkg_traits) end ist.gl_free_pkg(m) end end EOF probe() { R luajit $T/probe.lua "$@"; } # A good recipe; variants are made from it with sed. cat > $T/good < $T/minimal < $T/bad probe parse $T/bad check "refused: $1" has "status=3" } bad "an unknown key" 's/^REPO = "extra";/REPO = "extra"; SOURCE = "x";/' bad "a misspelt optional key" 's/^BUILD_DEPS/BUILDDEPS/' bad "no NAME" '/^NAME/d' bad "no SOURCES" '/^SOURCES/,/^);/d' bad "an empty SOURCES" '/^SOURCES/,/^);/c SOURCES = ();' bad "no DESC" '/^DESC/d' bad "an empty MAINT" 's/^MAINT = .*/MAINT = "";/' bad "a control character in DESC" 's/^DESC = .*/DESC = "a\\tb";/' bad "a name with a slash" 's/^NAME = .*/NAME = "a\/b";/' bad "NAME = .." 's/^NAME = .*/NAME = "..";/' bad "a two-part version" 's/^VER = .*/VER = "8.9";/' bad "a version with a leading zero" 's/^VER = .*/VER = "8.09.0";/' bad "a version with a tag" 's/^VER = .*/VER = "8.9.0-rc1";/' bad "REPO naming a -src repository" 's/^REPO = .*/REPO = "extra-src";/' bad "an http:// source" 's|https://curl.se|http://curl.se|' bad "a source with no file name" 's|curl-8.9.0.tar.gz|curl/|' bad "a source whose file name is .." 's|curl-8.9.0.tar.gz|..|' bad "two sources with one file name" 's|extra-data.tar.gz|curl-8.9.0.tar.gz|' bad "a source without SHA256" "s|SHA256 = \"$SHA\"; },|},|" bad "an uppercase hash" "/curl.se/s/$SHA/sha256:$(printf '%064d' 0 | tr 0 A)/" bad "a short hash" "/curl.se/s/$SHA/sha256:abc/" bad "an unknown member in a source" 's|SHA256 = "\(sha256:[a-f]*\)"; },|SHA256 = "\1"; MIRROR = "x"; },|' bad "a patch name with a slash" 's|01-a.patch|../a.patch|' bad "a patch listed twice" 's|02-b.patch|01-a.patch|' bad "an unknown BUILD" 's/^BUILD = .*/BUILD = "scons";/' bad "BUILD = \"\"" 's/^BUILD = .*/BUILD = "";/' bad "a configure argument with a newline" 's|--disable-static|--a\\nb|' bad "a configure argument with a tab" 's|--disable-static|--a\\tb|' bad "an empty configure argument" 's|"--disable-static"|""|' bad "an uppercase trait" 's/NAME = "ldap"/NAME = "LDAP"/' bad "a trait name with a dash" 's/NAME = "ldap"/NAME = "no-ldap"/' bad "a trait declared twice" 's/NAME = "ldap"/NAME = "ssl"/' bad "a trait without DEFAULT" 's/ DEFAULT = false;//' bad "a DEFAULT that is not a boolean" 's/DEFAULT = false/DEFAULT = 0/' bad "an unknown member in a trait" 's/DEFAULT = false;/DEFAULT = false; USE = true;/' bad "an IF naming no trait" 's/IF = "ssl"/IF = "tls"/' bad "an IF naming no trait, negated" 's/IF = "-ldap"/IF = "-tls"/' bad "an IF that is not a string" 's/IF = "ssl"/IF = true/' bad "a dependency with an unsafe name" 's/"zlib"/"..\/zlib"/' bad "a dependency without VER" 's/{ NAME = "zlib"; VER = "1.3.0"; }/{ NAME = "zlib"; }/' bad "an unknown member in a dependency" 's/VER = "1.3.0"; }/VER = "1.3.0"; OPTIONAL = true; }/' # The included file is a valid part of a recipe, so only the @include # refusal itself can stop it. echo 'CONFS = ();' > $T/inc bad "an @include line" "/^CONFS/d; 1i @include \"$T/inc\"" bad "an indented @include line" "/^CONFS/d; 1i \\ @include \"$T/inc\"" bad "SOURCES as a string" '/^SOURCES/,/^);/c SOURCES = "https://a/b.tar.gz";' { sed '/^TRAITS/,/^);/d; /IF =/d; s/, { NAME = "libressl"; VER = "3.9.0"; IF = "ssl"; }//' $T/good | grep -v BUILD_DEPS printf 'TRAITS = (' i=0; sep="" while [ $i -lt 33 ]; do printf '%s { NAME = "t%d"; DEFAULT = false; }' "$sep" $i; sep=","; i=$((i+1)); done printf ' );\n' } > $T/many probe parse $T/many check "refused: 33 traits" has "status=3" sed 's/ t32.*DEFAULT = false; }/ );/; s/, { NAME = "t32"; DEFAULT = false; }//' $T/many > $T/32 probe parse $T/32 check "32 traits parse" has "status=0" printf 'NAME = "a\0b";\n' > $T/nul probe parse $T/nul check "refused: a NUL byte" has "status=3" head -c $((1024*1024+1)) /dev/zero | tr '\0' ' ' > $T/huge cat $T/minimal >> $T/huge probe parse $T/huge check "refused: more than 1 MiB" has "status=3" hr "gl_recipe_read" # mkb DIR -> bundle at $T/b.grcp, made from a tree in $T/tree/DIR mkb() { (cd $T/tree && tar czf $T/b.grcp "$@"); } fresh() { rm -rf $T/tree && mkdir -p $T/tree/curl-8.9.0/patches cp $T/good $T/tree/curl-8.9.0/recipe echo a > $T/tree/curl-8.9.0/patches/01-a.patch echo b > $T/tree/curl-8.9.0/patches/02-b.patch } fresh; mkb curl-8.9.0 probe read $T/b.grcp check "a good bundle reads" has "status=0" check "...with its recipe" has "name=[curl]" fresh; (cd $T/tree && tar czf $T/b.grcp curl-8.9.0/recipe curl-8.9.0/patches/01-a.patch curl-8.9.0/patches/02-b.patch) probe read $T/b.grcp check "no directory entries: still reads" has "status=0" fresh; printf 'configure() { :; }\n' > $T/tree/curl-8.9.0/build; mkb curl-8.9.0 probe read $T/b.grcp check "a build file is allowed" has "status=0" fresh; sed -i 's/^BUILD = .*/BUILD = "hooks";/' $T/tree/curl-8.9.0/recipe; mkb curl-8.9.0 probe read $T/b.grcp check "refused: BUILD = hooks without a build file" has "status=2" printf 'compile() { :; }\n' > $T/tree/curl-8.9.0/build; mkb curl-8.9.0 probe read $T/b.grcp check "BUILD = hooks with a build file reads" has "status=0" badb() { probe read $T/b.grcp; check "refused: $1" has "status=${2:-2}"; } fresh; rm $T/tree/curl-8.9.0/patches/02-b.patch; ln -s /etc/shadow $T/tree/curl-8.9.0/patches/02-b.patch; mkb curl-8.9.0 badb "a symlink (as a listed patch)" fresh; ln $T/tree/curl-8.9.0/patches/01-a.patch $T/tree/curl-8.9.0/build; mkb curl-8.9.0 badb "a hardlink" fresh; echo x > $T/tree/curl-8.9.0/README; mkb curl-8.9.0 badb "an extra file at the top" fresh; mkdir $T/tree/curl-8.9.0/files; mkb curl-8.9.0 badb "an extra directory" fresh; mkdir $T/tree/curl-8.9.0/patches/sub; mkb curl-8.9.0 badb "a directory inside patches/" fresh; echo c > $T/tree/curl-8.9.0/patches/03-c.patch; mkb curl-8.9.0 badb "a patch PATCHES does not list" fresh; rm $T/tree/curl-8.9.0/patches/02-b.patch; mkb curl-8.9.0 badb "a listed patch missing" fresh; rm $T/tree/curl-8.9.0/recipe; mkb curl-8.9.0 badb "no recipe" fresh; mv $T/tree/curl-8.9.0 $T/tree/curl-9.0.0; mkb curl-9.0.0 badb "a top directory that is not NAME-VER" fresh; mkdir -p $T/tree/other; echo x > $T/tree/other/recipe; mkb curl-8.9.0 other badb "two top directories" fresh; (cd $T/tree && tar czf $T/b.grcp curl-8.9.0 --transform 's|^curl-8.9.0/patches/02-b.patch|curl-8.9.0/../02-b.patch|') badb "a .. entry" fresh; (cd $T/tree && tar czf $T/b.grcp -P curl-8.9.0 --transform 's|^curl-8.9.0/patches/02-b.patch|/tmp/02-b.patch|') badb "an absolute entry" fresh; (cd $T/tree && tar czf $T/b.grcp curl-8.9.0 curl-8.9.0/patches/01-a.patch) badb "an entry twice" fresh; mkfifo $T/tree/curl-8.9.0/build; mkb curl-8.9.0 badb "a fifo" fresh; head -c $((16*1024*1024)) /dev/zero > $T/tree/curl-8.9.0/patches/02-b.patch; mkb curl-8.9.0 badb "more than 16 MiB in all" echo junk > $T/b.grcp badb "junk" 1 fresh; sed -i 's/^REPO = .*/REPO = "extra"; COLOR = "red";/' $T/tree/curl-8.9.0/recipe; mkb curl-8.9.0 badb "a malformed recipe" 3 hr "-src indexes" cat > $T/src.idx < $T/bad.idx probe index $2 $T/bad.idx check "refused: $1" has "status=1" } badi "no BUILD_DEPS" extra-src 's/BUILD_DEPS = .*//' badi "no TRAITS" extra-src 's/TRAITS = .*/}/' badi "a .gpkg FILE" extra-src 's/curl-8.9.0.grcp/curl-8.9.0.gpkg/' badi "an IF naming no trait" extra-src 's/IF = "ssl"/IF = "tls"/' badi "ON in an index trait" extra-src 's/DEFAULT = true;/DEFAULT = true; ON = [ "x" ];/' badi "REPO = -src alone" -src 's/"extra-src"/"-src"/' # The same entry in a binary index: every recipe field is malformed. sed 's/extra-src/extra/; s/grcp/gpkg/; s/, { NAME = "libressl"; VER = "3.9.0"; IF = "ssl"; }//; s/ IF = "-ldap";//' $T/src.idx > $T/bin.idx probe index extra $T/bin.idx check "refused: BUILD_DEPS and TRAITS in a binary index" has "status=1" sed -i 's/BUILD_DEPS = .*//; s/TRAITS = .*/}/' $T/bin.idx cp $T/bin.idx $T/good-bin.idx sed -i 's/{ NAME = "zlib"; VER = "1.3.0"; }/{ NAME = "zlib"; VER = "1.3.0"; IF = "ssl"; }/' $T/bin.idx probe index extra $T/bin.idx check "refused: IF in a binary index" has "status=1" probe index extra $T/good-bin.idx check "...without them, it is a good binary index" has "status=0" check "...whose build deps and traits are empty" sh -c 'echo "$0" | grep -q "^build_deps=\[\]" && echo "$0" | grep -q "^traits=\[\]"' "$OUT" hr "PKG_TRAITS" mkman() { rm -rf $T/m && mkdir -p $T/m/hi-1.0.0/files printf 'PKG_NAME = "hi"; PKG_VER = "1.0.0"; PKG_REPO = "extra";\nPKG_DEPS = (); PKG_CONFS = ();\n%s\n' "$1" > $T/m/hi-1.0.0/manifest.gpm.cfg tar czf $T/m.gpkg -C $T/m hi-1.0.0 } mkman 'PKG_TRAITS = [ "ssl", "-ldap" ];' probe manifest $T/m.gpkg check "PKG_TRAITS is read" sh -c 'echo "$0" | tr "\n" "|" | grep -qF "traits=[ssl|-ldap|]"' "$OUT" mkman '' probe manifest $T/m.gpkg check "...and absent, NULL" has "traits=NULL" mkman 'PKG_TRAITS = [ ];' probe manifest $T/m.gpkg check "...an empty list, empty" has "traits=[]" for v in '[ "ssl", "-ssl" ]' '[ "SSL" ]' '[ "--ssl" ]' '[ "" ]' '"ssl"' '[ 1 ]'; do mkman "PKG_TRAITS = $v;" probe manifest $T/m.gpkg check "refused: PKG_TRAITS = $v" has "status=2" done hr "grepo -r X-src" K=/etc/glacier/keys D=/srv/extra-src mkdir -p $D && chmod 755 /srv $D gsign -G -p $K/srckey.pub -s /root/srckey.sec -c srckey >/dev/null fresh; (cd $T/tree && tar czf $D/curl-8.9.0.grcp curl-8.9.0) rm -rf $T/tree/hi-1.0.0 && mkdir -p $T/tree/hi-1.0.0 && cp $T/minimal $T/tree/hi-1.0.0/recipe (cd $T/tree && tar czf $D/hi-1.0.0.grcp hi-1.0.0) cp /dev/null $D/ignored.gpkg R grepo -s /root/srckey.sec -r extra-src $D check "grepo indexes a -src repository" rc 0 check "...both bundles" has "2 packages" probe index extra-src $D/repo.idx check "...an index gl_index_parse accepts" has "status=0" check "...with the recipe's relations and traits" sh -c 'echo "$0" | tr "\n" "|" | grep -qF "deps=[zlib 1.3.0|libressl 3.9.0 ssl|]|confs=[curl-old 0.0.0|]|build_deps=[perl 5.40.0 -ldap|]|traits=[ssl 1|ldap 0|]"' "$OUT" check "...each bundle's hash" grep -qF "SHA256 = \"sha256:$(sha256sum $D/hi-1.0.0.grcp | cut -c1-64)\"" $D/repo.idx mv $D/hi-1.0.0.grcp $D/hi-2.0.0.grcp R grepo -s /root/srckey.sec -r extra-src $D check "refused: a misnamed bundle" has "[x] $D/hi-2.0.0.grcp must be named hi-1.0.0.grcp, as its recipe says." rm $D/hi-2.0.0.grcp sed 's/REPO = "extra"/REPO = "community"/' $T/minimal > $T/tree/hi-1.0.0/recipe (cd $T/tree && tar czf $D/hi-1.0.0.grcp hi-1.0.0) R grepo -s /root/srckey.sec -r extra-src $D check "refused: a recipe of another repository" has "[x] $D/hi-1.0.0.grcp builds community packages, not extra." echo junk > $D/hi-1.0.0.grcp R grepo -s /root/srckey.sec -r extra-src $D check "refused: a bundle that can't be read" has "[x] $D/hi-1.0.0.grcp is not a readable recipe bundle." rm $D/hi-1.0.0.grcp R grepo -s /root/srckey.sec -r extra-src $D hr "syspkg -y with a -src repository" syspkg -n >/dev/null printf 'repos = (\n\t{ name = "extra-src"; url = "file:///srv/extra-src"; keys = [ "srckey" ]; }\n);\n' > /etc/glacier/repos.conf chmod 644 /etc/glacier/repos.conf R syspkg -y check "syspkg -y accepts a -src index" rc 0 check "...and stores it" cmp $D/repo.idx /glacier/sys/repos/extra-src.idx echo if [ $FAILS -eq 0 ]; then echo "ALL PASSED"; else echo "$FAILS FAILED"; fi [ $FAILS -eq 0 ]