;;;; message-digest-type-test.scm -*- Scheme -*- ;;;; Kon Lovett, Jul '18 ;;;; Kon Lovett, Oct '17 (import test) (test-begin "Message Digest Type") ;;; (import scheme) (import (chicken blob)) (import (chicken memory)) ;(import (chicken format)) (import (srfi 4)) (import message-digest-type) (import message-digest-primitive) (define-constant DIGEST-LENGTH 5) (define-constant CONTEXT-SIZE 10) (let () (define the-ctx #f) (define (make-context) ;Init to 0 necessary since DIGEST-LENGTH is possibly > than ;the input size! (Actually just needs to be a known value, ;`(integer->char #xff)' would work as well.) (string->blob (make-string CONTEXT-SIZE #\nul)) ) (define (init ctx) (set! the-ctx ctx) ) (define (update ctx bytes count) ;(printf "Update Ctx: ~S Bytes: ~S Count: ~S~%" ctx bytes count) (flush-output) (assert (eq? ctx the-ctx)) (assert (not (not bytes))) (assert (< 0 count)) (assert (<= count CONTEXT-SIZE)) ; So no mem overflow (assert (blob? ctx)) (move-memory! bytes ctx count) ) (define (final ctx result) ;(printf "Final Result Size: ~S Ctx: ~S Result: ~S~%" (blob-size result) ctx result) (flush-output) (assert (eq? ctx the-ctx)) (assert (not (not result))) (assert (blob? ctx)) (assert (<= (blob-size result) DIGEST-LENGTH)) ; So no mem overflow (move-memory! ctx result DIGEST-LENGTH) ) (define mdp (make-message-digest-primitive make-context DIGEST-LENGTH init update final)) ;FIXME poor test (test-group "init & final" (let ((md (initialized-message-digest mdp))) (test-assert (message-digest? md)) (test "0000000000" (finalize-message-digest md)) (test-assert "re-init" (eq? md (initialized-message-digest md))) (test "re-done" "0000000000" (finalize-message-digest md)) ) ) ;FIXME poor test (test-group "init & final u8" (let ((md (initialized-message-digest mdp)) (resbuf (make-u8vector DIGEST-LENGTH 1)) ) (test-assert (message-digest? md)) (finalize-message-digest! md resbuf) (test #u8{0 0 0 0 0} resbuf) (test-assert "re-init" (eq? md (initialized-message-digest md))) (let ((resbuf (make-u8vector DIGEST-LENGTH 1))) (finalize-message-digest! md resbuf) (test "re-done" #u8{0 0 0 0 0} resbuf) ) ) ) ) ;;; (test-end "Message Digest Type") (test-exit)