#!/bin/sh
# $Id$

# metadata (dnf,yum) repo

. ./sh/lib/setup
. ./sh/lib/repo-setup

ORIGREPO=
DESTINATION_REPO=
DEPENDENCY_SOLVER=3

setUp() {
    POLDEK="$POLDEK_NOCONF"
}

tearDown() {
}

testLoad()
{
    local src="m,type=metadata metadata/repo_02/"

    msg $POLDEK -s "$src" ls
    local n=$($POLDEK -s "$src" ls | grep fake_bash | wc -l)
    assertEquals "load failed" "$n" "1"
}

# a .repo file names a configuration to add, it is not an index
testRepoNotReadDirectly()
{
    local out

    out=$($POLDEK -s metadata/02.repo ls 2>&1)
    echo "$out" | grep -q "a .repo file is not read directly; use 'poldek repo add" || \
        fail "no hint about repo add: $out"
}


# sizes of 4 GiB and more become the largest size poldek keeps, as from rpm headers
testLoadLargeSizes()
{
    local dir="$TMPDIR/meta-large"
    local primary old new n

    rm -rf $dir
    mkdir -p $dir/repo
    cp -r metadata/repo_02/repodata $dir/repo/ || fail "cp failed"
    primary=$(ls $dir/repo/repodata/*-primary.xml.gz)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    zcat $primary | sed 's/package="2237" installed="0"/package="5368709120" installed="5368709120"/' | gzip -n > $dir/primary.gz
    mv $dir/primary.gz $primary
    new=$(sha256sum < $primary | cut -d' ' -f1)
    sed -i "s/>$old</>$new</" $dir/repo/repodata/repomd.xml
    n=$($POLDEK -s "m,type=metadata $dir/repo/" desc fake_bash | grep -E '^(Size|Package size): +4096.0 MB' | wc -l)
    assertEquals "sizes should be 4096.0 MB" "$n" "2"
}

# a package whose name does not fit in an index key is left out with a
# warning; tndb took such a key for a bug and --mkidx ended in abort()
testMkidxLongName()
{
    local dir="$TMPDIR/meta-long-name"
    local primary old new long out rc n

    rm -rf $dir
    mkdir -p $dir/repo $dir/idx
    cp -r metadata/repo_02/repodata $dir/repo/ || fail "cp failed"
    primary=$(ls $dir/repo/repodata/*-primary.xml.gz)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    long=$(printf '%300s' | tr ' ' a)
    zcat $primary | sed "s|<name>fake_bash</name>|<name>$long</name>|" | gzip -n > $dir/primary.gz
    mv $dir/primary.gz $primary
    new=$(sha256sum < $primary | cut -d' ' -f1)
    sed -i "s/>$old</>$new</" $dir/repo/repodata/repomd.xml

    out=$($POLDEK --st metadata -s $dir/repo/ --mkidx=$dir/idx/ --mt pndir 2>&1)
    rc=$?
    assertEquals "--mkidx failed: $out" "0" "$rc"
    echo "$out" | grep -q "^warn: $long: .*ignored" || fail "no warning: $out"

    n=$($POLDEK -s $dir/idx/ ls 2>/dev/null | grep -c '^super_kernel-')
    assertEquals "super_kernel not in the index" "1" "$n"
}

# createrepo names SHA-1 checksums "sha" or "sha1"
testLoadSha1Checksums()
{
    local dir="$TMPDIR/meta-sha1"
    local type f old new n

    for type in sha sha1; do
        rm -rf $dir
        mkdir -p $dir/repo
        cp -r metadata/repo_02/repodata $dir/repo/ || fail "cp failed"
        for f in $dir/repo/repodata/*.xml.gz; do
            old=$(sha256sum < $f | cut -d' ' -f1)
            new=$(sha1sum < $f | cut -d' ' -f1)
            sed -i "s|<checksum type=\"sha256\">$old<|<checksum type=\"$type\">$new<|" $dir/repo/repodata/repomd.xml
        done
        n=$($POLDEK -s "m,type=metadata $dir/repo/" ls | grep fake_bash | wc -l)
        assertEquals "repository with $type checksums not loaded" "1" "$n"
    done
}

# a local repository in a directory named as the cache with a suffix is not
# in the cache: its stub index goes to the cache, not into the repository
testLoadRepoBesideCache()
{
    local dir="${CACHEDIR}-repo"
    local made

    rm -rf $dir
    cachedir_clean
    mkdir -p $dir
    cp -r metadata/repo_02/repodata $dir/ || fail "cp failed"
    touch -d '1 hour ago' $dir $dir/repodata $dir/repodata/*
    $POLDEK -s "m,type=metadata $dir/" desc fake_bash > /dev/null || fail "desc failed"
    made=$(cd $dir && find . -newer repodata/repomd.xml ! -name . | sort)
    [ -z "$made" ] || fail "made in the repository: $made"
    [ -n "$(find $CACHEDIR -name 'stubindex*')" ] || fail "no stub index in the cache"
    rm -rf $dir
}

testLoadZst()
{
    local src="m,type=metadata metadata/repo_02zst/"

    msg $POLDEK -s "$src" ls
    local n=$($POLDEK -s "$src" ls | grep fake_bash | wc -l)
    assertEquals "load failed" "$n" "1"
}

# primary.xml.zst is read as it is: desc (which loads the whole source,
# unlike ls) describes its packages, and no decompressed copy of it is
# left in the cache
testLoadZstWhole()
{
    local src="m,type=metadata metadata/repo_02zst/"
    local n made

    cachedir_clean
    n=$($POLDEK -s "$src" desc fake_bash | grep -c '^Package: *fake_bash-')
    assertEquals "fake_bash not described" "1" "$n"
    made=$(find $CACHEDIR -name '*primary.xml*')
    [ -z "$made" ] || fail "a copy of primary.xml in the cache: $made"
}

# a primary.xml.zst that cannot be decompressed whole is reported and fails
# the load: no package of it is loaded
testLoadZstCut()
{
    local dir="$TMPDIR/meta-zst-cut"
    local primary old new out

    rm -rf $dir
    cachedir_clean
    mkdir -p $dir/repo
    cp -r metadata/repo_02zst/repodata $dir/repo/ || fail "cp failed"
    primary=$(ls $dir/repo/repodata/*-primary.xml.zst)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    head -c 500 $primary > $dir/primary.zst && mv $dir/primary.zst $primary
    new=$(sha256sum < $primary | cut -d' ' -f1)
    sed -i "s/>$old</>$new</" $dir/repo/repodata/repomd.xml

    out=$($POLDEK -s "m,type=metadata $dir/repo/" ls 2>&1)
    echo "$out" | grep -q 'primary.xml.zst: cannot be read whole' ||
        fail "no error for the cut primary.xml.zst: $out"
    echo "$out" | grep -q ': load failed' || fail "no load error: $out"
    echo "$out" | grep -q '^fake_bash-' && fail "a package of the cut primary.xml.zst listed: $out"
    [ -z "$(find $CACHEDIR -name '*primary.xml*')" ] ||
        fail "a copy of the cut primary.xml.zst was left in the cache"
}

# a primary.xml.zst with the whole document and a cut frame after it is
# rejected too: libxml2 gets a whole document followed by spaces
testLoadZstCutAfterDocument()
{
    local dir="$TMPDIR/meta-zst-cut-after"
    local primary old new out

    rm -rf $dir
    cachedir_clean
    mkdir -p $dir/repo
    cp -r metadata/repo_02zst/repodata $dir/repo/ || fail "cp failed"
    primary=$(ls $dir/repo/repodata/*-primary.xml.zst)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    # a frame of 1 MiB of spaces, cut after its first block of them
    head -c 1048576 /dev/zero | tr '\0' ' ' | zstd -q -c | head -c 20 >> $primary ||
        fail "cannot cut a frame"
    new=$(sha256sum < $primary | cut -d' ' -f1)
    sed -i "s/>$old</>$new</" $dir/repo/repodata/repomd.xml

    out=$($POLDEK -s "m,type=metadata $dir/repo/" ls 2>&1)
    echo "$out" | grep -q 'primary.xml.zst: cannot be read whole' ||
        fail "no error for the cut frame: $out"
    echo "$out" | grep -q '^fake_bash-' && fail "a package listed despite the cut frame: $out"
    return 0
}

# the load of primary.xml takes no stack per package; with the stack limited
# to 1 MiB, 8192 packages are enough to show one taking 256 bytes
testLoadManyPackages()
{
    local dir="$TMPDIR/meta-many"
    local primary old new n

    rm -rf $dir
    mkdir -p $dir/repo
    cp -r metadata/repo_02/repodata $dir/repo/ || fail "cp failed"
    primary=$(ls $dir/repo/repodata/*-primary.xml.gz)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    # copies of fake_bash named pkg00000 to pkg08191
    zcat $primary | awk -v n=8192 '
        /<package type="rpm">/ && !done { inpkg = 1 }
        inpkg { pkg = pkg $0 "\n" }
        inpkg && /<\/package>/ { inpkg = 0; done = 1 }
        /<\/metadata>/ {
            for (i = 0; i < n; i++) {
                p = pkg; gsub(/fake_bash/, sprintf("pkg%05d", i), p); printf "%s", p
            }
        }
        { print }' | gzip -n > $dir/primary.gz
    mv $dir/primary.gz $primary
    new=$(sha256sum < $primary | cut -d' ' -f1)
    sed -i "s/>$old</>$new</" $dir/repo/repodata/repomd.xml
    n=$( (ulimit -s 1024 && $POLDEK -s "m,type=metadata $dir/repo/" ls) | grep -c '^pkg[0-9]')
    assertEquals "packages listed" "8192" "$n"
}

# after --up fetches a changed repository, ls lists its packages, not the
# ones before the change
testUpRefreshesStubIndex()
{
    local dir="$TMPDIR/meta-up"
    local src="m,type=metadata test://$dir/"
    local primary old new n

    rm -rf $dir
    mkdir -p $dir
    cp -r metadata/repo_02/repodata $dir/ || fail "cp failed"
    $POLDEK_RAW_UP -Osource="$src" --up || fail "up failed"

    # rename fake_bash; primary.xml is named after its checksum
    primary=$(ls $dir/repodata/*-primary.xml.gz)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    zcat $primary | sed 's|<name>fake_bash</name>|<name>fake_zsh</name>|' | gzip -n > $dir/primary.gz
    new=$(sha256sum < $dir/primary.gz | cut -d' ' -f1)
    rm -f $primary
    mv $dir/primary.gz $dir/repodata/$new-primary.xml.gz
    sed -i "s/$old/$new/g" $dir/repodata/repomd.xml
    $POLDEK_RAW_UP -Osource="$src" --up || fail "up failed"

    # ls reads the stub index, which --up refreshes
    n=$($POLDEK_RAW_UP -q -Osource="$src" --cmd ls | grep -c '^fake_zsh-')
    assertEquals "ls should list the renamed package" "1" "$n"
}

# --up of a primary.xml that passes its digest but cannot be loaded writes
# no stub index, which would be an empty one
testUpUnloadableWritesNoStubIndex()
{
    local dir="$TMPDIR/meta-up-unloadable"
    local src="m,type=metadata test://$dir/"
    local primary old new stub

    rm -rf $dir
    cachedir_clean
    mkdir -p $dir
    cp -r metadata/repo_02/repodata $dir/ || fail "cp failed"
    primary=$(ls $dir/repodata/*-primary.xml.gz)
    old=$(sha256sum < $primary | cut -d' ' -f1)
    echo '<metadata' | gzip -n > $dir/primary.gz
    new=$(sha256sum < $dir/primary.gz | cut -d' ' -f1)
    rm -f $primary
    mv $dir/primary.gz $dir/repodata/$new-primary.xml.gz
    sed -i "s/$old/$new/g" $dir/repodata/repomd.xml

    $POLDEK_RAW_UP -Osource="$src" --up
    stub=$(find $CACHEDIR -name 'stubindex*')
    [ -z "$stub" ] || fail "a stub index of the unloadable primary.xml: $stub"
}

# a new repomd.xml whose primary.xml cannot be fetched fails the update
testUpFailsWithoutNewPrimary()
{
    local dir="$TMPDIR/meta-noprimary"
    local src="m,type=metadata test://$dir/"
    local op primary old new

    for op in --up --upa; do
        rm -rf $dir
        cachedir_clean
        mkdir -p $dir
        cp -r metadata/repo_02/repodata $dir/ || fail "cp failed"
        $POLDEK_RAW_UP -Osource="$src" --up || fail "up failed"

        primary=$(ls $dir/repodata/*-primary.xml.gz)
        old=$(sha256sum < $primary | cut -d' ' -f1)
        new=$(echo changed | sha256sum | cut -d' ' -f1)
        sed -i "s/$old/$new/g" $dir/repodata/repomd.xml

        $POLDEK_RAW_UP -Osource="$src" $op
        assertTrue "$op ended with exit code 0" "[ $? -ne 0 ]"
    done
}


. ./sh/lib/shunit2
