# -*- tcl -*-
# sha256.test:  tests for the sha256 commands
#
# This file contains a collection of tests for one or more of the Tcl
# built-in commands.  Sourcing this file into Tcl runs the tests and
# generates output for errors.  No output means no errors were found.

# -------------------------------------------------------------------------

source [file join \
	[file dirname [file dirname [file join [pwd] [info script]]]] \
	devtools testutilities.tcl]

testsNeedTcl     8.5
testsNeedTcltest 1.0

testing {
    if {[useTcllibC]} {
        useLocalKeep sha256.tcl sha256 ::sha2
    } else {
        useLocal     sha256.tcl sha256 ::sha2
    }
    TestAccelInit                      ::sha2
}

# -------------------------------------------------------------------------
# Now the package specific tests....
# -------------------------------------------------------------------------

test sha256-1.0 {sha256 usage} {
    catch {::sha2::sha256} result
    set result
} "wrong # args: should be \"::sha2::sha256 ?-hex|-bin? -filename file | -channel channel | string\""

test sha224-1.0 {sha224 usage} {
    catch {::sha2::sha224} result
    set result
} "wrong # args: should be \"::sha2::sha224 ?-hex|-bin? -filename file | -channel channel | string\""

# -------------------------------------------------------------------------
# Digest

# FIPS 180-2 test vectors SHA-256
set vectorsD \
    [list \
         "abc" \
         "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad" \
         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" \
         "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1" \
         [string repeat a 1000000] \
         "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0" \
         \
         "x" 2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881 \
        ]

# FIPS 180-2 test vectors SHA-224
set vectorsDT \
    [list \
         "abc" \
         "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7" \
         "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" \
         "75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525" \
         [string repeat a 1000000] \
         "20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67" ]

# -------------------------------------------------------------------------

TestAccelDo sha2 impl {
    set n 0
    foreach {msg hash} $vectorsD {
        test sha256-${impl}-2.$n {FIPS-180-2 test vectors for SHA-256} {
            list [catch {::sha2::sha256 $msg} r] $r
        } [list 0 $hash] ; # {}
        incr n
    }

    set n 0
    foreach {msg hash} $vectorsDT {
        test sha224-${impl}-2.$n {FIPS-180-2 test vectors for SHA-224} {
            list [catch {::sha2::sha224 $msg} r] $r
        } [list 0 $hash] ; # {}
        incr n
    }

    set text "\uFFFE\u0000\u0001\u0002"
    set hash "091190c96cd3aa8b0db5c92ff856b174156fa695d47dde3f9e08438f2e9c4740"

    test sha256-$impl-5.0 "sha256 unicode hash \"$text\"" -body {
        ::sha2::sha256 -hex [encoding convertto utf-8 $text]
    } -result $hash ; # {}

    set hash "64bb944266bd95aa08d9a65bf40f79f233e4f229dcc68254a45381de"

    test sha224-$impl-5.0 "sha224 unicode hash \"$text\"" -body {
        ::sha2::sha224 -hex [encoding convertto utf-8 $text]
    } -result $hash ; # {}

    unset text hash
}

# -------------------------------------------------------------------------

TestAccelExit sha2
testsuiteCleanup

# -------------------------------------------------------------------------
# Local Variables:
#   mode: tcl
#   indent-tabs-mode: nil
# End:
