1# -*- tcl -*-
2# sha1.test:  tests for the sha1 commands
3#
4# This file contains a collection of tests for one or more of the Tcl
5# built-in commands.  Sourcing this file into Tcl runs the tests and
6# generates output for errors.  No output means no errors were found.
7#
8# Copyright (c) 2001 by ActiveState Tool Corp.
9# All rights reserved.
10#
11# RCS: @(#) $Id: sha1v1.test,v 1.2 2006/10/09 21:41:42 andreas_kupries Exp $
12
13# -------------------------------------------------------------------------
14
15source [file join \
16	[file dirname [file dirname [file join [pwd] [info script]]]] \
17	devtools testutilities.tcl]
18
19testsNeedTcl     8.2
20testsNeedTcltest 1.0
21
22testing {
23    useLocal sha1v1.tcl sha1
24}
25
26# -------------------------------------------------------------------------
27# Now the package specific tests....
28# -------------------------------------------------------------------------
29
30if {[::sha1::LoadAccelerator critcl]} {
31    puts "> critcl based"
32}
33if {[::sha1::LoadAccelerator cryptkit]} {
34    puts "> cryptkit based"
35}
36if {[::sha1::LoadAccelerator trf]} {
37    puts "> Trf based"
38}
39puts "> pure Tcl"
40
41# -------------------------------------------------------------------------
42# Handle multiple implementation testing
43#
44
45array set preserve [array get ::sha1::accel]
46
47proc implementations {} {
48    variable ::sha1::accel
49    foreach {a v} [array get accel] {if {$v} {lappend r $a}}
50    lappend r tcl; set r
51}
52
53proc select_implementation {impl} {
54    variable ::sha1::accel
55    foreach e [array names accel] { set accel($e) 0 }
56    if {[string compare "tcl" $impl] != 0} {
57        set accel($impl) 1
58    }
59}
60
61proc reset_implementation {} {
62    variable ::sha1::accel
63    array set accel [array get ::preserve]
64}
65
66# -------------------------------------------------------------------------
67
68test sha1-1.0 {sha1} {
69    catch {::sha1::sha1} result
70    set result
71} "wrong # args: should be \"sha1 ?-hex? -filename file | string\""
72
73test sha1-1.1 {sha1} {
74    catch {::sha1::hmac} result
75    set result
76} "wrong # args: should be \"hmac ?-hex? -key key -filename file | string\""
77
78test sha1-1.2 {sha1} {
79    catch {::sha1::hmac key} result
80    set result
81} "wrong # args: should be \"hmac ?-hex? -key key -filename file | string\""
82
83set vectors {
84    1 "abc"
85    "a9993e364706816aba3e25717850c26c9cd0d89d"
86    2 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
87    "84983e441c3bd26ebaae4aa1f95129e5e54670f1"
88}
89foreach impl [implementations] {
90    select_implementation $impl
91    foreach {n msg expected} $vectors {
92        test sha1-$impl-2.$n "sha1 ($impl impl)" {
93            list [catch {::sha1::sha1 $msg} r] $r
94        } [list 0 $expected]
95    }
96    reset_implementation
97}
98
99set vectors {
100    1 ""     ""      "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"
101    2 "foo"  "hello" "4c883e9bc42763641bba04185d492de00de7ce2c"
102    3 "bar"  "world" "a905e79f51faa446cb5a3888b577e34577ef7fce"
103    4 "key"  "text"  "369e2959eb49450338b212748f77d8ded74847bb"
104    5 "sha1" "hmac"  "2660aeeccf432596e56f8f8260de971322e8935b"
105    6 "hmac" "sha1"  "170523fd610da92dd4b4fb948a01a8365d66511a"
106    7 "sha1" "sha1"  "5154473317173f66212fc59365233ffd9cbaab94"
107    8 "hmac" "hmac"  "9e08393f6ac829c4385930ea38567dad582d958f"
108    9 "01234567abcdefgh01234567abcdefgh01234567abcdefgh01234567abcdefgh==" "hello world"
109    "dd80c541f75064d70e53a6b7b0a45210127f484e"
110}
111foreach impl [implementations] {
112    select_implementation $impl
113    foreach {n key text expected} $vectors {
114        test sha1-$impl-3.$n "hmac ($impl impl)" {
115            list [catch {::sha1::hmac $key $text} r] $r
116        } [list 0 $expected]
117    }
118    reset_implementation
119}
120
121# -------------------------------------------------------------------------
122# RFC 2202 has a set of test vectors for HMAC-MD5 and HMAC-SHA1.
123# This is those test vectors...
124# -------------------------------------------------------------------------
125
126set vectors \
127    [list \
128         1 [string repeat \x0b 20] "Hi There" \
129         b617318655057264e28bc0b6fb378c8ef146be00 \
130         2 "Jefe" "what do ya want for nothing?" \
131         effcdf6ae5eb2fa2d27416d5f184df9c259a7c79 \
132         3 [string repeat \xaa 20] [string repeat \xdd 50] \
133         125d7342b9ac11cd91a39af48aa17b4f63f175d3 \
134         4 \
135         [binary format H* 0102030405060708090a0b0c0d0e0f10111213141516171819]\
136         [string repeat \xcd 50] \
137         4c9007f4026250c6bc8414f9bf50c86c2d7235da \
138         5 [string repeat \x0c 20] "Test With Truncation" \
139         4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 \
140         6 [string repeat \xaa 80] \
141         "Test Using Larger Than Block-Size Key - Hash Key First" \
142         aa4ae5e15272d00e95705637ce8a3b55ed402112 \
143         7 [string repeat \xaa 80] \
144         "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data" \
145         e8e99d0f45237d786d6bbaa7965c7808bbff1a91 \
146        ]
147
148foreach impl [implementations] {
149    select_implementation $impl
150    foreach {n key msg hash} $vectors {
151        test sha1-$impl-4a.$n "RFC2202 test vectors for HMAC-SHA1 ($impl)" {
152            list [catch {::sha1::hmac $key $msg} r] $r
153        } [list 0 $hash]
154    }
155    reset_implementation
156}
157
158# -------------------------------------------------------------------------
159
160test sha1-5.1 {Check hashing data that begins with hyphen} {
161    list [catch {::sha1::sha1 -hello} msg] $msg
162} {0 bd32f1769a47f98c73348c87f5d6842ccd129911}
163
164test sha1-5.2 {Check hashing data that begins with hyphen} {
165    list [catch {::sha1::sha1 -hex -- -hello} msg] $msg
166} {0 bd32f1769a47f98c73348c87f5d6842ccd129911}
167
168test sha1-5.3 {Check hashing data that begins with hyphen} {
169    list [catch {::sha1::sha1 --} msg] $msg
170} {0 e6a9fc04320a924f46c7c737432bb0389d9dd095}
171
172test sha1-5.4 {Check hashing data that begins with hyphen} {
173    list [catch {::sha1::sha1 -hex -- --} msg] $msg
174} {0 e6a9fc04320a924f46c7c737432bb0389d9dd095}
175
176test sha1-6.1 {Check hashing data that begins with hyphen} {
177    list [catch {::sha1::hmac - -hello} msg] $msg
178} {0 872c0aa5dca317c3be39a209c5aaa4d8139052b1}
179
180test sha1-6.2 {Check hashing data that begins with hyphen} {
181    list [catch {::sha1::hmac -- -hello} msg] $msg
182} {0 a0e2547c63c9de64338efb19b0c6c533968748cc}
183
184test sha1-6.3 {Check hashing data that begins with hyphen} {
185    list [catch {::sha1::hmac -hex -key -- --} msg] $msg
186} {0 d1efe5ea394610b39c10b97418278199ddd65766}
187
188test sha1-6.4 {Check hashing data that begins with hyphen} {
189    list [catch {::sha1::hmac -hex -key - --} msg] $msg
190} {0 01c134b54ab872941acfce0cf3202f16ee64fb14}
191
192# -------------------------------------------------------------------------
193
194set testfile [makeFile {} sha1[pid].data]
195
196# pattern repeatcount sha1-hash
197set vectors \
198    [list \
199         0 "\x00" 81922 a9fb4910179d5088ab606944ca0216e4403a5141 \
200         1 "\x5a" 81920 fef13bbee20792b7b2e65f15d5e4dd6ae04e2323 \
201         2 "\x01\x23\x45\x67\x89\xab\xcd\xef" 2048 \
202           846b9be26036a0b3c16a32805b5f3a85f8d0e0f5 \
203         ]
204
205foreach {n pattern repeat hash} $vectors {
206    test sha1-7.$n "file hashing" {
207        list [catch {
208            set f [open $testfile w]
209            fconfigure $f -encoding binary -translation binary
210            puts -nonewline $f [string repeat $pattern $repeat]
211            close $f
212            sha1::sha1 -hex -file $testfile
213        } msg] $msg
214    } [list 0 $hash]
215}
216
217removeFile $testfile
218
219# -------------------------------------------------------------------------
220
221testsuiteCleanup
222
223# -------------------------------------------------------------------------
224# Local Variables:
225#   mode: tcl
226#   indent-tabs-mode: nil
227# End:
228