|
| 1 | +import std::sha1; |
| 2 | +import std::vec; |
| 3 | +import std::str; |
| 4 | + |
| 5 | +#[test] |
| 6 | +fn test() { |
| 7 | + type test = rec(str input, vec[u8] output); |
| 8 | + |
| 9 | + fn a_million_letter_a() -> str { |
| 10 | + auto i = 0; |
| 11 | + auto rs = ""; |
| 12 | + while (i < 100000) { rs += "aaaaaaaaaa"; i += 1; } |
| 13 | + ret rs; |
| 14 | + } |
| 15 | + // Test messages from FIPS 180-1 |
| 16 | + |
| 17 | + let vec[test] fips_180_1_tests = |
| 18 | + [rec(input="abc", |
| 19 | + output=[0xA9u8, 0x99u8, 0x3Eu8, 0x36u8, 0x47u8, 0x06u8, 0x81u8, |
| 20 | + 0x6Au8, 0xBAu8, 0x3Eu8, 0x25u8, 0x71u8, 0x78u8, 0x50u8, |
| 21 | + 0xC2u8, 0x6Cu8, 0x9Cu8, 0xD0u8, 0xD8u8, 0x9Du8]), |
| 22 | + rec(input="abcdbcdecdefdefgefghfghighij" + |
| 23 | + "hijkijkljklmklmnlmnomnopnopq", |
| 24 | + output=[0x84u8, 0x98u8, 0x3Eu8, 0x44u8, 0x1Cu8, 0x3Bu8, 0xD2u8, |
| 25 | + 0x6Eu8, 0xBAu8, 0xAEu8, 0x4Au8, 0xA1u8, 0xF9u8, 0x51u8, |
| 26 | + 0x29u8, 0xE5u8, 0xE5u8, 0x46u8, 0x70u8, 0xF1u8]), |
| 27 | + rec(input=a_million_letter_a(), |
| 28 | + output=[0x34u8, 0xAAu8, 0x97u8, 0x3Cu8, 0xD4u8, 0xC4u8, 0xDAu8, |
| 29 | + 0xA4u8, 0xF6u8, 0x1Eu8, 0xEBu8, 0x2Bu8, 0xDBu8, 0xADu8, |
| 30 | + 0x27u8, 0x31u8, 0x65u8, 0x34u8, 0x01u8, 0x6Fu8])]; |
| 31 | + // Examples from wikipedia |
| 32 | + |
| 33 | + let vec[test] wikipedia_tests = |
| 34 | + [rec(input="The quick brown fox jumps over the lazy dog", |
| 35 | + output=[0x2fu8, 0xd4u8, 0xe1u8, 0xc6u8, 0x7au8, 0x2du8, 0x28u8, |
| 36 | + 0xfcu8, 0xedu8, 0x84u8, 0x9eu8, 0xe1u8, 0xbbu8, 0x76u8, |
| 37 | + 0xe7u8, 0x39u8, 0x1bu8, 0x93u8, 0xebu8, 0x12u8]), |
| 38 | + rec(input="The quick brown fox jumps over the lazy cog", |
| 39 | + output=[0xdeu8, 0x9fu8, 0x2cu8, 0x7fu8, 0xd2u8, 0x5eu8, 0x1bu8, |
| 40 | + 0x3au8, 0xfau8, 0xd3u8, 0xe8u8, 0x5au8, 0x0bu8, 0xd1u8, |
| 41 | + 0x7du8, 0x9bu8, 0x10u8, 0x0du8, 0xb4u8, 0xb3u8])]; |
| 42 | + auto tests = fips_180_1_tests + wikipedia_tests; |
| 43 | + fn check_vec_eq(vec[u8] v0, vec[u8] v1) { |
| 44 | + assert (vec::len[u8](v0) == vec::len[u8](v1)); |
| 45 | + auto len = vec::len[u8](v0); |
| 46 | + auto i = 0u; |
| 47 | + while (i < len) { |
| 48 | + auto a = v0.(i); |
| 49 | + auto b = v1.(i); |
| 50 | + assert (a == b); |
| 51 | + i += 1u; |
| 52 | + } |
| 53 | + } |
| 54 | + // Test that it works when accepting the message all at once |
| 55 | + |
| 56 | + auto sh = sha1::mk_sha1(); |
| 57 | + for (test t in tests) { |
| 58 | + sh.input_str(t.input); |
| 59 | + auto out = sh.result(); |
| 60 | + check_vec_eq(t.output, out); |
| 61 | + sh.reset(); |
| 62 | + } |
| 63 | + |
| 64 | + // Test that it works when accepting the message in pieces |
| 65 | + for (test t in tests) { |
| 66 | + auto len = str::byte_len(t.input); |
| 67 | + auto left = len; |
| 68 | + while (left > 0u) { |
| 69 | + auto take = (left + 1u) / 2u; |
| 70 | + sh.input_str(str::substr(t.input, len - left, take)); |
| 71 | + left = left - take; |
| 72 | + } |
| 73 | + auto out = sh.result(); |
| 74 | + check_vec_eq(t.output, out); |
| 75 | + sh.reset(); |
| 76 | + } |
| 77 | +} |
| 78 | +// Local Variables: |
| 79 | +// mode: rust; |
| 80 | +// fill-column: 78; |
| 81 | +// indent-tabs-mode: nil |
| 82 | +// c-basic-offset: 4 |
| 83 | +// buffer-file-coding-system: utf-8-unix |
| 84 | +// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; |
| 85 | +// End: |
0 commit comments