Changeset 35343 in project


Ignore:
Timestamp:
03/26/18 02:40:32 (4 months ago)
Author:
kon
Message:

common phase assertions, change raw-update test to reflect actual usage

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/4/message-digest/trunk/tests/message-digest-test.scm

    r35338 r35343  
    2727(define-constant BLOCK-LENGTH-DEFAULT 4)
    2828
    29 (define short-test-filename "alpha.txt")
     29(define SHORT-TEST-FILE-NAME "alpha.txt")
     30(define SHORT-TEST-FILE-LENGTH 26)
     31
     32(define just-once
     33  (let ((x #t))
     34    (lambda ()
     35      (let (
     36        (res (and x simple-src)) )
     37        (set! x #f)
     38        res ) ) ) )
    3039
    3140;;
     
    3342(test-begin "Message Digest")
    3443
    35 ;
    3644(test 'hex-string (message-digest-result-form 'hex))
    3745
    3846;Tests defaults
    3947(test-group "Chunk Read (port)"
    40   (let ((siz (message-digest-chunk-size))
    41         (in (open-input-file short-test-filename)))
    42     (let ((rdr ((message-digest-chunk-port-read-maker) in)))
    43       (let ((res (rdr)))
    44         (test-assert "First chunk type" (blob? res))
    45         (test "First chunk size" 26 (blob-size res)) )
     48  (let (
     49    (siz (message-digest-chunk-size))
     50    (in (open-input-file SHORT-TEST-FILE-NAME)) )
     51    (let* (
     52      (rdr ((message-digest-chunk-port-read-maker) in))
     53      (res (rdr)) )
     54      (test-assert "First chunk type" (blob? res))
     55      (test "First chunk size" SHORT-TEST-FILE-LENGTH (blob-size res))
    4656      (test-assert "No more chunk" (not (rdr))) )
    4757    (close-input-port in) )
     
    5060(test-group "Make Primitive"
    5161
    52   (define (init ctx) (void))
    53   (define (update ctx bytes count) (void))
    54   (define (final ctx result) (void))
    55 
    56   (let ((mdp (make-message-digest-primitive context-size digest-length init update final)))
     62        (define the-ctx #f)
     63
     64  (define (init ctx)
     65    (set! the-ctx ctx) )
     66
     67  (define (update ctx bytes count)
     68    (assert (eq? ctx the-ctx))
     69    (assert (not (not bytes)))
     70    (assert (< 0 count))
     71    (void) )
     72
     73  (define (final ctx result)
     74    (assert (eq? ctx the-ctx))
     75    (assert (not (not result)))
     76    (void) )
     77
     78  (let (
     79    (mdp (make-message-digest-primitive context-size digest-length init update final)) )
    5780    (test-assert (message-digest-primitive? mdp))
    5881    (test context-size (message-digest-primitive-context-info mdp))
     
    6487    (test-assert (symbol? (message-digest-primitive-name mdp))) )
    6588
    66   ;; don't bother testing the non-optional arguments again
    67 
    68   (let ((mdp (make-message-digest-primitive context-size digest-length init update final 'foo)))
     89  ;;don't bother testing the non-optional arguments again
     90
     91  (let (
     92    (mdp
     93      (make-message-digest-primitive context-size digest-length init update final 'foo)) )
    6994    (test BLOCK-LENGTH-DEFAULT (message-digest-primitive-block-length mdp))
    7095    (test 'foo (message-digest-primitive-name mdp)) )
    7196
    72   (let ((mdp (make-message-digest-primitive context-size digest-length init update final block-length)))
     97  (let (
     98    (mdp
     99      (make-message-digest-primitive context-size digest-length init update final block-length)) )
    73100    (test block-length (message-digest-primitive-block-length mdp))
    74101    (test-assert (symbol? (message-digest-primitive-name mdp))) )
    75102
    76   (let ((mdp (make-message-digest-primitive context-size digest-length init update final block-length 'foo)))
     103  (let (
     104    (mdp
     105      (make-message-digest-primitive context-size digest-length init update final block-length 'foo)) )
    77106    (test block-length (message-digest-primitive-block-length mdp))
    78107    (test 'foo (message-digest-primitive-name mdp)) )
     
    86115  (define (init ctx)
    87116    ;(printf "  Init Ctx: ~S~%" ctx)
    88     (assert (pointer? ctx))
    89     (set! the-ctx ctx) )
     117    (set! the-ctx ctx)
     118    (assert (pointer? ctx)) )
    90119
    91120  (define (update ctx bytes count)
    92121        ;(printf "Update Ctx: ~S Bytes: ~S Count: ~S~%" ctx bytes count)
    93     (assert (pointer? ctx))
    94     (assert (eq? ctx the-ctx))
     122    (assert (eq? ctx the-ctx))
     123    (assert (not (not bytes)))
     124    (assert (< 0 count))
     125    (assert (<= count context-size))  ; So no mem overflow
     126    (assert (pointer? ctx))
    95127    (assert (blob? bytes))
    96128    (assert (<= count (blob-size bytes)))
    97     (assert (>= context-size count))  ; So no mem overflow
    98129    (move-memory! bytes ctx count) )
    99130
    100131  (define (final ctx result)
    101132    ;(printf " Final Ctx: ~S Result: ~S~%" ctx result)
    102     (assert (pointer? ctx))
    103     (assert (eq? ctx the-ctx))
     133    (assert (eq? ctx the-ctx))
     134    (assert (not (not result)))
     135    (assert (pointer? ctx))
    104136    (assert (or (blob? result) (string? result)))
    105137    ; So no mem overflow
     
    107139    (move-memory! ctx result digest-length) )
    108140
    109   (let ((mdp (make-message-digest-primitive context-size digest-length init update final)))
    110     (let ((md (initialize-message-digest mdp)))
     141  (let (
     142    (mdp (make-message-digest-primitive context-size digest-length init update final)) )
     143    (let (
     144      (md (initialize-message-digest mdp)) )
    111145      (test-assert (message-digest? md))
    112146      (test-assert (message-digest-update-string md simple-src))
    113147      (test simple-res (finalize-message-digest md)) )
    114     (let ((md (initialize-message-digest mdp))
    115           (blb (make-blob 100)) )
     148    (let (
     149      (md (initialize-message-digest mdp))
     150      (blb (make-blob 100)) )
    116151      (message-digest-update-string md simple-src)
    117152      (test-assert (finalize-message-digest! md blb))
    118153      (print "result = " blb) )
    119     (let ((md (initialize-message-digest mdp))
    120           (str (make-string 100 #\space)) )
     154    (let (
     155      (md (initialize-message-digest mdp))
     156      (str (make-string 100 #\space)) )
    121157      (message-digest-update-string md simple-src)
    122158      (test-assert (finalize-message-digest! md str))
    123159      (print "result = " #\" str #\") )
    124     (let ((md (initialize-message-digest mdp))
    125           (vec (make-u8vector 100 0)) )
     160    (let (
     161      (md (initialize-message-digest mdp))
     162      (vec (make-u8vector 100 0)) )
    126163      (message-digest-update-string md simple-src)
    127164      (test-assert (finalize-message-digest! md vec))
     
    133170        (define the-ctx #f)
    134171
    135   (define (make-context) (make-blob context-size))
    136 
    137   (define (init ctx)
    138     #;(printf "  Init Ctx: ~S~%" ctx)
    139     (assert (blob? ctx))
    140     (set! the-ctx ctx) )
     172  (define (make-context)
     173    (make-blob context-size) )
     174
     175  (define (init ctx)
     176    ;(printf "  Init Ctx: ~S~%" ctx)
     177    (set! the-ctx ctx)
     178    (assert (blob? ctx)) )
    141179
    142180  (define (update ctx bytes count)
    143181    ;(printf "Update Ctx: ~S Bytes: ~S Count: ~S~%" ctx bytes count) (flush-output)
    144     (assert (blob? ctx))
    145     (assert (eq? ctx the-ctx))
     182    (assert (eq? ctx the-ctx))
     183    (assert (not (not bytes)))
     184    (assert (< 0 count))
     185    (assert (<= count context-size))  ; So no mem overflow
     186    (assert (blob? ctx))
    146187    (assert (blob? bytes))
    147188    (assert (<= count (blob-size bytes)))
    148     (assert (>= context-size count))  ; So no mem overflow
    149189    (move-memory! bytes ctx count) )
    150190
    151191  (define (final ctx result)
    152192    ;(printf " Final Result Size: ~S Ctx: ~S Result: ~S~%" (blob-size result) ctx result) (flush-output)
    153     (assert (blob? ctx))
    154     (assert (eq? ctx the-ctx))
     193    (assert (eq? ctx the-ctx))
     194    (assert (not (not result)))
     195    (assert (blob? ctx))
    155196    (assert (blob? result))
    156     (assert (= digest-length (blob-size result)))  ; So no mem overflow
     197    (assert (<= (blob-size result) digest-length))  ; So no mem overflow
    157198    (move-memory! ctx result digest-length) )
    158199
    159   (let ((mdp (make-message-digest-primitive make-context digest-length init update final)))
    160     (let ((md (initialize-message-digest mdp)))
    161       (test-assert (message-digest? md))
    162       (test-assert (message-digest-update-string md simple-src))
    163       (test simple-res (finalize-message-digest md)) ) )
     200  (let* (
     201    (mdp (make-message-digest-primitive make-context digest-length init update final))
     202    (md (initialize-message-digest mdp)) )
     203    (test-assert (message-digest? md))
     204    (test-assert (message-digest-update-string md simple-src))
     205    (test simple-res (finalize-message-digest md)) )
    164206)
    165207
    166208(let ()
     209
     210        (define the-ctx #f)
    167211
    168212  (define (make-context)
     
    172216    (string->blob (make-string context-size #\nul)) )
    173217
    174   (define (init ctx) (void))
     218  (define (init ctx)
     219    (set! the-ctx ctx) )
    175220
    176221  (define (update ctx bytes count)
    177222    ;(printf "Update Ctx: ~S Bytes: ~S Count: ~S~%" ctx bytes count) (flush-output)
    178     (assert (>= context-size count))  ; So no mem overflow
     223    (assert (eq? ctx the-ctx))
     224    (assert (not (not bytes)))
     225    (assert (< 0 count))
     226    (assert (<= count context-size))  ; So no mem overflow
     227    (assert (blob? ctx))
    179228    (move-memory! bytes ctx count) )
    180229
    181230  (define (final ctx result)
    182231    ;(printf " Final Result Size: ~S Ctx: ~S Result: ~S~%" (blob-size result) ctx result) (flush-output)
    183     (assert (= digest-length (blob-size result)))  ; So no mem overflow
     232    (assert (eq? ctx the-ctx))
     233    (assert (not (not result)))
     234    (assert (blob? ctx))
     235    (assert (<= (blob-size result) digest-length))  ; So no mem overflow
    184236    (move-memory! ctx result digest-length) )
    185237
     
    245297
    246298  (test-group "Procedure Source"
    247     (define just-once
    248       (let ((x #t))
    249         (lambda ()
    250           (let ((res (and x simple-src)))
    251             (set! x #f)
    252             res ) ) ) )
    253     (let ((md (initialize-message-digest mdp)))
     299    (let (
     300      (md (initialize-message-digest mdp)) )
    254301      (test-assert (message-digest-update-procedure md just-once))
    255302      (test simple-res (finalize-message-digest md)) )
     
    257304
    258305  (test-group "Port"
    259     (let ((port (open-output-digest mdp)))
     306    (let (
     307      (port (open-output-digest mdp)) )
    260308      (test-assert (output-port? port))
    261309      (display simple-src port) ;cannot be readable!
     
    269317        (use message-digest-old)
    270318
     319        (define the-ctx #f)
     320
    271321  (define (make-context)
    272322    (string->blob (make-string context-size #\nul)) )
    273323
    274   (define (init ctx) (void))
    275 
    276   (define (update ctx bytes count)
    277     (assert (>= context-size count))  ; So no mem overflow
     324  (define (init ctx)
     325    (set! the-ctx ctx) )
     326
     327  (define (update ctx bytes count)
     328    (assert (eq? ctx the-ctx))
     329    (assert (not (not bytes)))
     330    (assert (< 0 count))
     331    (assert (<= count context-size))  ; So no mem overflow
     332    (assert (blob? ctx))
    278333    (move-memory! bytes ctx count) )
    279334
    280335  (define (final ctx result)
    281     (assert (= digest-length (blob-size result)))  ; So no mem overflow
     336    (assert (eq? ctx the-ctx))
     337    (assert (not (not result)))
     338    (assert (blob? ctx))
     339    (assert (<= (blob-size result)  digest-length))  ; So no mem overflow
    282340    (move-memory! ctx result digest-length) )
    283341
    284342  (test-group "Primitive Apply (DEPRECATED)"
    285     (let ((mdp (make-message-digest-primitive context-size digest-length init update final)))
    286       (let ((res (message-digest-primitive-apply mdp simple-src)))
    287         (test-assert (string? res))
    288         (test simple-res (byte-string->hexadecimal res)) ) )
     343    (let* (
     344      (mdp
     345        (make-message-digest-primitive context-size digest-length init update final))
     346      (res (message-digest-primitive-apply mdp simple-src)) )
     347      (test-assert (string? res))
     348      (test simple-res (byte-string->hexadecimal res)) )
    289349  )
    290350
     
    298358(test-group "Chunk Read (fileno)"
    299359
     360        (define the-ctx #f)
     361
    300362  (define (init ctx)
    301363    ;(printf "  Init Ctx: ~S~%" ctx)
    302     (assert (pointer? ctx)) )
     364    (assert (pointer? ctx))
     365    (set! the-ctx ctx) )
    303366
    304367  (define (update ctx bytes count)
    305368        ;(printf "Update Ctx: ~S Count: ~S Bytes: ~S~%" ctx count bytes)
    306     (assert (pointer? ctx))
    307     (assert (not (not bytes)))
    308     (assert (<= context-size count))
    309     (move-memory! bytes ctx (min context-size count)) )
     369    (assert (eq? ctx the-ctx))
     370    (assert (not (not bytes)))
     371    (assert (< 0 count))
     372    (assert (<= count context-size))
     373    (assert (pointer? ctx))
     374    (move-memory! bytes ctx count) )
    310375
    311376  (define (raw-update ctx bytes count)
    312377        ;(printf "Raw-Update Ctx: ~S Count: ~S Bytes: ~S~%" ctx count bytes)
    313     (assert (pointer? ctx))
    314     (assert (not (not bytes)))
    315     (assert (<= context-size count))
     378    (assert (eq? ctx the-ctx))
     379    (assert (not (not bytes)))
     380    (assert (< 0 count))
     381    (assert (= SHORT-TEST-FILE-LENGTH count))
     382    (assert (pointer? ctx))
    316383    (move-memory! bytes ctx (min context-size count)) )
    317384
    318385  (define (final ctx result)
    319386    ;(printf " Final Ctx: ~S Length: ~S Result: ~S~%" ctx digest-length result)
    320     (assert (pointer? ctx))
    321     (assert (not (not result)))
    322     (assert (<= 0 digest-length))
     387    (assert (eq? ctx the-ctx))
     388    (assert (not (not result)))
     389    (assert (pointer? ctx))
     390    ;(assert (<= 0 digest-length))
    323391    (move-memory! ctx result digest-length) )
    324392
    325   (let ((mdp (make-message-digest-primitive context-size digest-length init update final raw-update)))
    326     (test "6162636465" (message-digest-file mdp short-test-filename 'hex-string)) )
     393  (let (
     394    (mdp
     395      (make-message-digest-primitive context-size digest-length init update final raw-update)) )
     396    (test "6162636465" (message-digest-file mdp SHORT-TEST-FILE-NAME 'hex-string)) )
    327397)
    328398
Note: See TracChangeset for help on using the changeset viewer.