| 1 |
(use gauche.test) |
| 2 |
(use srfi.13) |
| 3 |
(use gauche.uvector) |
| 4 |
(test-start "bcrypt") |
| 5 |
|
| 6 |
(use crypt.bcrypt) |
| 7 |
(test-module 'crypt.bcrypt) |
| 8 |
|
| 9 |
;; Test data is taken from wrapper.c |
| 10 |
(define (test-hashpw hash pw) |
| 11 |
(test* "bcrypt-hashpw" hash (bcrypt-hashpw pw hash))) |
| 12 |
|
| 13 |
(test-hashpw "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW" |
| 14 |
"U*U") |
| 15 |
(test-hashpw "$2a$05$CCCCCCCCCCCCCCCCCCCCC.VGOzA784oUp/Z0DY336zx7pLYAy0lwK" |
| 16 |
"U*U*") |
| 17 |
(test-hashpw "$2a$05$XXXXXXXXXXXXXXXXXXXXXOAcXxm9kjPGEMsLznoKqmqw7tc8WCx4a" |
| 18 |
"U*U*U") |
| 19 |
(test-hashpw "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui" |
| 20 |
"0123456789abcdefghijklmnopqrstuvwxyz\ |
| 21 |
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") |
| 22 |
(test-hashpw "$2a$05$abcdefghijklmnopqrstuu5s2v8.iXieOjg/.AySBTTZIIVFJeBui" |
| 23 |
"0123456789abcdefghijklmnopqrstuvwxyz\ |
| 24 |
ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\ |
| 25 |
chars after 72 are ignored") |
| 26 |
(test-hashpw "$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e" |
| 27 |
(u8vector->string #u8(#xa3))) |
| 28 |
(test-hashpw "$2x$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e" |
| 29 |
(u8vector->string #u8(#xff #xff #xa3))) |
| 30 |
(test-hashpw "$2y$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e" |
| 31 |
(u8vector->string #u8(#xff #xff #xa3))) |
| 32 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.nqd1wy.pTMdcvrRWxyiGL2eMz.2a85." |
| 33 |
(u8vector->string #u8(#xff #xff #xa3))) |
| 34 |
(test-hashpw "$2b$05$/OK.fbVrR/bpIqNJ5ianF.CE5elHaaO4EbggVDjb8P19RukzXSM3e" |
| 35 |
(u8vector->string #u8(#xff #xff #xa3))) |
| 36 |
(test-hashpw "$2y$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq" |
| 37 |
(u8vector->string #u8(#xa3))) |
| 38 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq" |
| 39 |
(u8vector->string #u8(#xa3))) |
| 40 |
(test-hashpw "$2b$05$/OK.fbVrR/bpIqNJ5ianF.Sa7shbm4.OzKpvFnX1pQLmQW96oUlCq" |
| 41 |
(u8vector->string #u8(#xa3))) |
| 42 |
(test-hashpw "$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi" |
| 43 |
(string-append "1" (u8vector->string #u8(#xa3)) "345")) |
| 44 |
(test-hashpw "$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi" |
| 45 |
(string-append (u8vector->string #u8(#xff #xa3)) "345")) |
| 46 |
(test-hashpw "$2x$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi" |
| 47 |
(string-append (u8vector->string #u8(#xff #xa3)) "34" |
| 48 |
(u8vector->string #u8(#xff #xff #xff #xa3)) "345")) |
| 49 |
(test-hashpw "$2y$05$/OK.fbVrR/bpIqNJ5ianF.o./n25XVfn6oAPaUvHe.Csk4zRfsYPi" |
| 50 |
(string-append (u8vector->string #u8(#xff #xa3)) "34" |
| 51 |
(u8vector->string #u8(#xff #xff #xff #xa3)) "345")) |
| 52 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.ZC1JEJ8Z4gPfpe1JOr/oyPXTWl9EFd." |
| 53 |
(string-append (u8vector->string #u8(#xff #xa3)) "34" |
| 54 |
(u8vector->string #u8(#xff #xff #xff #xa3)) "345")) |
| 55 |
(test-hashpw "$2y$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e" |
| 56 |
(string-append (u8vector->string #u8(#xff #xa3)) "345")) |
| 57 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.nRht2l/HRhr6zmCp9vYUvvsqynflf9e" |
| 58 |
(string-append (u8vector->string #u8(#xff #xa3)) "345")) |
| 59 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS" |
| 60 |
(string-append (u8vector->string #u8(#xa3)) "ab")) |
| 61 |
(test-hashpw "$2x$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS" |
| 62 |
(string-append (u8vector->string #u8(#xa3)) "ab")) |
| 63 |
(test-hashpw "$2y$05$/OK.fbVrR/bpIqNJ5ianF.6IflQkJytoRVc1yuaNtHfiuq.FRlSIS" |
| 64 |
(string-append (u8vector->string #u8(#xa3)) "ab")) |
| 65 |
(test-hashpw "$2x$05$6bNw2HLQYeqHYyBfLMsv/OiwqTymGIGzFsA4hOTWebfehXHNprcAS" |
| 66 |
(u8vector->string #u8(#xd1 #x91))) |
| 67 |
(test-hashpw "$2x$05$6bNw2HLQYeqHYyBfLMsv/O9LIGgn8OMzuDoHfof8AQimSGfcSWxnS" |
| 68 |
(u8vector->string #u8(#xd0 #xc1 #xd2 #xcf #xcc #xd8))) |
| 69 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.swQOIzjOiJ9GHEPuhEkvqrUyvWhEMx6" |
| 70 |
(string-append |
| 71 |
(u8vector->string |
| 72 |
#u8(#xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa |
| 73 |
#xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa |
| 74 |
#xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa |
| 75 |
#xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa |
| 76 |
#xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa |
| 77 |
#xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa #xaa)) |
| 78 |
"chars after 72 are ignored as usual")) |
| 79 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.R9xrDjiycxMbQE2bp.vgqlYpW5wx2yy" |
| 80 |
(u8vector->string |
| 81 |
#u8(#xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 |
| 82 |
#xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 |
| 83 |
#xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 |
| 84 |
#xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 |
| 85 |
#xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 |
| 86 |
#xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55 #xaa #x55))) |
| 87 |
(test-hashpw "$2a$05$/OK.fbVrR/bpIqNJ5ianF.9tQZzcJfm3uj2NvJ/n5xkhpqLrMpWCe" |
| 88 |
(u8vector->string |
| 89 |
#u8(#x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff |
| 90 |
#x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff |
| 91 |
#x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff |
| 92 |
#x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff |
| 93 |
#x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff |
| 94 |
#x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff #x55 #xaa #xff))) |
| 95 |
(test-hashpw "$2a$05$CCCCCCCCCCCCCCCCCCCCC.7uG0VCzI2bS7j6ymqJi9CdcdxiRTWNy" |
| 96 |
"") |
| 97 |
|
| 98 |
(define (test-wrong-hash reason hash) |
| 99 |
(test* #"bcrypt-hashpw wrong hash setting: ~reason" |
| 100 |
(test-error) |
| 101 |
(bcrypt-hashpw "" hash))) |
| 102 |
|
| 103 |
(test-wrong-hash "iteration count is smaller than 4" |
| 104 |
"$2a$03$CCCCCCCCCCCCCCCCCCCCC.") |
| 105 |
(test-wrong-hash "iteration count is larger than 31" ;; can be up to 99, but currently limited to 31 |
| 106 |
"$2a$32$CCCCCCCCCCCCCCCCCCCCC.") |
| 107 |
(test-wrong-hash "method 'c' is not implemented yet" |
| 108 |
"$2c$05$CCCCCCCCCCCCCCCCCCCCC.") |
| 109 |
(test-wrong-hash "method 'z' is not implemented yet" |
| 110 |
"$2z$05$CCCCCCCCCCCCCCCCCCCCC.") |
| 111 |
(test-wrong-hash "method is smaller than 'a'" |
| 112 |
"$2`$05$CCCCCCCCCCCCCCCCCCCCC.") |
| 113 |
(test-wrong-hash "method is larger than 'z'" |
| 114 |
"$2{$05$CCCCCCCCCCCCCCCCCCCCC.") |
| 115 |
|
| 116 |
(test* "bcrypt-gensalt" "$2b$10$" |
| 117 |
(string-take (bcrypt-gensalt) 7)) |
| 118 |
(test* "bcrypt-gensalt" "$2b$12$" |
| 119 |
(string-take (bcrypt-gensalt :count 12) 7)) |
| 120 |
(test* "bcrypt-gensalt 'a'" "$2a$12$" |
| 121 |
(string-take (bcrypt-gensalt :prefix "$2a$" :count 12) 7)) |
| 122 |
(test* "bcrypt-gensalt 'b'" "$2b$12$" |
| 123 |
(string-take (bcrypt-gensalt :prefix "$2b$" :count 12) 7)) |
| 124 |
|
| 125 |
(test* "bcrypt-gensalt count smaller than 4" (test-error) |
| 126 |
(bcrypt-gensalt :count 3)) |
| 127 |
(test* "bcrypt-gensalt count larger than 31" (test-error) |
| 128 |
(bcrypt-gensalt :count 32)) |
| 129 |
|
| 130 |
(test* "bcrypt-gensalt not implemented prefix 'c'" (test-error) |
| 131 |
(bcrypt-gensalt :prefix "$2c$")) |
| 132 |
(test* "bcrypt-gensalt not implemented prefix 'z'" (test-error) |
| 133 |
(bcrypt-gensalt :prefix "$2z$")) |
| 134 |
(test* "bcrypt-gensalt prefix smaller than 'a' (`)" (test-error) |
| 135 |
(bcrypt-gensalt :prefix "$2`$")) |
| 136 |
(test* "bcrypt-gensalt prefix larger than 'z' ({)" (test-error) |
| 137 |
(bcrypt-gensalt :prefix "$2{$")) |
| 138 |
|
| 139 |
(test-end) |