# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=tcl:et:sw=4:ts=4:sts=4

package require tcltest 2
namespace import tcltest::*

set pwd [file dirname [file normalize $argv0]]

source ../port_test_autoconf.tcl
package require macports 1.0

array set ui_options {}
#set ui_options(ports_debug)   yes
#set ui_options(ports_verbose) yes
mportinit ui_options

package require portchecksum 1.0


test parse_checksum {
    Parse checksum unit test.
} -body {
    set all_dist_files {file file2}
    set checksums_str {file md5 1111 file2 sha1 3333}

    set res [portchecksum::parse_checksums $checksums_str]
    if {$res != "yes"} {return "FAIL: error in checksum parse"}
    if {$checksums_array(file) != "md5 1111"} {
        return "FAIL: invalid file options"
    }
    if {$checksums_array(file2) != "sha1 3333"} {
        return "FAIL: invalid file options"
    }
    return "Parse checksum successful."
} -result "Parse checksum successful."


test calc_md5 {
    Calculate md5 unit test. Based on a file it creates.
} -body {
    set fd [open $pwd/file w+]
    puts $fd "test.file"
    close $fd

    set res [portchecksum::calc_md5 $pwd/file]
    if {$res != "9f70ecc1095ff10df81be6b5f218328d"} {
        return "FAIL: unexpected md5"
    }
    return "Calc md5 successful."

} -cleanup {
    file delete -force $pwd/file
} -result "Calc md5 successful."


test calc_sha1 {
    Calculate sha1 unit test.
} -body {
    set fd [open $pwd/file w+]
    puts $fd "test.file"
    close $fd

    set res [portchecksum::calc_sha1 $pwd/file]
    if {$res != "5560df60ff202ca8b8c3dcf51ad650b78e859261"} {
        return "FAIL: unexpected sha1"
    }
    return "Calc sha1 successful."

} -cleanup {
    file delete -force $pwd/file
} -result "Calc sha1 successful."


test calc_rmd160 {
    Calculate rmd160 unit test.
} -body {
    set fd [open $pwd/file w+]
    puts $fd "test.file"
    close $fd

    set res [portchecksum::calc_rmd160 $pwd/file]
    if {$res != "5aee5d12fe536e2e288e9f1daafd84f1bc17c3e6"} {
        return "FAIL: unexpected rmd160"
    }
    return "Calc rmd160 successful."

} -cleanup {
    file delete -force $pwd/file
} -result "Calc rmd160 successful."


test calc_sha256 {
    Calculate sha256 unit test.
} -body {
    set fd [open $pwd/file w+]
    puts $fd "test.file"
    close $fd

    set res [portchecksum::calc_sha256 $pwd/file]
    if {$res != "2f686816f2a80e8efcc4ef40ac4e898d27ce4205a61ee422d56f8c5e8b46612e"} {
        return "FAIL: unexpected sha1"
    }
    return "Calc sha256 successful."

} -cleanup {
    file delete -force $pwd/file
} -result "Calc sha256 successful."

test verify_checksum_format_md5 {
    Test that we properly verify an MD5 checksum
} -body {
    set checksum "9f70ecc1095ff10df81be6b5f218328d"
    if {[portchecksum::verify_checksum_format "md5" $checksum] != 1} {
        return "FAIL: did not verify $checksum as MD5"
    }
    return "Verify MD5 successful"
} -result "Verify MD5 successful"

test verify_checksum_format_sha1 {
    Test that we properly verify an SHA1 checksum
} -body {
    set checksum "5560df60ff202ca8b8c3dcf51ad650b78e859261"
    if {[portchecksum::verify_checksum_format "sha1" $checksum] != 1} {
        return "FAIL: did not verify $checksum as SHA1"
    }
    return "Verify SHA1 successful"
} -result "Verify SHA1 successful"

test verify_checksum_format_rmd160 {
    Test that we properly verify an rmd160 checksum
} -body {
    set checksum "5aee5d12fe536e2e288e9f1daafd84f1bc17c3e6"
    if {[portchecksum::verify_checksum_format "rmd160" $checksum] != 1} {
        return "FAIL: did not verify $checksum as rmd160"
    }
    return "Verify rmd160 successful"
} -result "Verify rmd160 successful"

test verify_checksum_format_sha256 {
    Test that we properly verify an SHA256 checksum
} -body {
    set checksum "2f686816f2a80e8efcc4ef40ac4e898d27ce4205a61ee422d56f8c5e8b46612e"
    if {[portchecksum::verify_checksum_format "sha256" $checksum] != 1} {
        return "FAIL: did not verify $checksum as sha256"
    }
    return "Verify sha256 successful"
} -result "Verify sha256 successful"

test verify_unknown_checksum_format {
    Test that we respond as expected to an unknown checksum format
} -body {
    set checksum "thisisnotread"
    if {[portchecksum::verify_checksum_format "lol22" $checksum] != -1} {
        return "FAIL: did not respond as expected to an unknown checksum"
    }
    return "Verify unknown successful"
} -result "Verify unknown successful"



# test checksum_start


test checksum_main {
    Checksum main unit test. Checks for empty files.
} -body {
    set all_dist_files {file file2}
    set checksum.skip yes

    if {[portchecksum::checksum_main] != 0} {
        return "FAIL: checksum not skipped"
    }

    set checksum.skip no
    set distpath $pwd/dpath

    file mkdir $distpath
    close [open $distpath/file w]
    close [open $distpath/file2 w]
    array set checksums_array {
        file {md5 d41d8cd98f00b204e9800998ecf8427e}
        file2 {sha1 da39a3ee5e6b4b0d3255bfef95601890afd80709}
    }

    if {[portchecksum::checksum_main] != 0} {
        return "FAIL: incorrect checksum"
    }
    return "Checksum main successful."

} -cleanup {
    file delete -force $distpath
    file delete -force file
    file delete -force file2
} -result "Checksum main successful."


cleanupTests
