#!/bin/bash

# Copyright (C) 2012-2018 Marc Ferland
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

ZIP=$(which zip)
ARCHIVE="archive.zip"
YAZC="../yazc/yazc"
CMD="libtool --mode=execute $YAZC"


if [ ! -f "${ZIP}" ]; then
    echo >&2 "error: zip not found!"
    exit 1
fi

create_dummy_files() {
    for i in $(seq 0 2)
    do
        dd if=/dev/urandom of=file_${i} bs=${RANDOM} count=1
    done
    for i in $(seq 3 6)
    do
        dd if=/dev/zero of=file_${i} bs=${RANDOM} count=1
    done
}

cleanup() {
    rm -f file_[0-9]
    rm -f archive.zip
}

generate_random_string() {
    echo $(cat /dev/urandom | tr -dc "${1}" | fold -w ${2} | head -n 1)
}

test_zip_1() {
    # zip files with passwords [a-z]
    for pw in {a..z}
    do
        FILES=""
        for filen in file_{0..6}
        do
            FILES="${FILES} ${filen}"
            zip -e -P ${pw} ${ARCHIVE} ${FILES}
            for threads in $(seq 1 8)
            do
                for cset in -a -aA -aAn -aAns
                do
                    if ! ${CMD} bruteforce -t${threads} ${cset} -l1 ${ARCHIVE}; then
                        echo >&2 "yazc failed to find the password (options: -t${threads} ${cset} ${ARCHIVE})"
                        exit 1
                    fi
                done
            done
            rm ${ARCHIVE}
        done
    done
}

test_zip_2() {
    # zip files with passwords [a-z][a-z]
    for pw in {a..z}{a..z}
    do
        FILES=""
        for filen in file_{0..6}
        do
            FILES="${FILES} ${filen}"
            zip -e -P ${pw} ${ARCHIVE} ${FILES}
            for threads in $(seq 1 8)
            do
                for cset in -a -aA -aAn -aAns
                do
                    if ! ${CMD} bruteforce -t${threads} ${cset} -l2 ${ARCHIVE}; then
                        echo >&2 "yazc failed to find the password (options: -t${threads} ${cset} ${ARCHIVE})"
                        exit 1
                    fi
                done
            done
            rm ${ARCHIVE}
        done
    done
}

test_zip_6() {
    # zip files with passwords [a-z]
    count=0
    while [ $count -lt 10 ]; do
        pw="$(generate_random_string a-z 6)"
        FILES="file_0 file_1 file_2 file_3"
        zip -e -P ${pw} ${ARCHIVE} ${FILES} >/dev/null
        echo "Testing password: ${pw}"
        if ! ${CMD} bruteforce -t8 -a ${ARCHIVE}; then
            echo >&2 "yazc failed to find the password (options: -t8 -a ${ARCHIVE}, password: ${pw})"
            exit 1
        fi
        rm ${ARCHIVE}
        count=$((count+1))
    done
}

cleanup

create_dummy_files

test_zip_1
test_zip_2
test_zip_6

cleanup

exit 0
