+test.ha (2273B)
1 // SPDX-License-Identifier: MPL-2.0 2 // (c) Hare authors <https://harelang.org> 3 4 use encoding::hex; 5 use fmt; 6 use hash; 7 use os; 8 use strings; 9 use test; 10 11 @test fn sha1() void = { 12 let sha = sha1(); 13 const vectors = [ 14 ("", "da39a3ee5e6b4b0d3255bfef95601890afd80709"), 15 ("abc", "a9993e364706816aba3e25717850c26c9cd0d89d"), 16 ("hello world", "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"), 17 ("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", "84983e441c3bd26ebaae4aa1f95129e5e54670f1"), 18 ("abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu", "a49b2446a02c645bf419f995b67091253a04a259"), 19 // From Pro Git Chapter 10.2 20 // https://git-scm.com/book/en/v2/Git-Internals-Git-Objects#_object_storage 21 // output of: echo -n 'what is up, doc?' | git hash-object --stdin 22 ("blob 16\0what is up, doc?", "bd9dbf5aae1a3862dd1526723246b20206e5fc37"), 23 ("Hare is a cool language", "947feae3d0d65cc083c8f3e87858206e36aae908"), 24 ("'UNIX was not designed to stop its users from doing stupid things, as that would also stop them from doing clever things' - Doug Gwyn", "05c8dd2605161bdd0b5d70f1f225f4dd69a01e3b"), 25 ("'Life is too short to run proprietary software' - Bdale Garbee", "91ad4bdc2fbe2b731cbe8bf2958099391c7af3b8"), 26 ("'The central enemy of reliability is complexity.' - Geer et al", "4b6eb2aa55ef59cc59be6d181c64141e7c1e5eab"), 27 ]; 28 29 for (let i = 0z; i < len(vectors); i += 1) { 30 const vector = vectors[i]; 31 hash::reset(&sha); 32 hash::write(&sha, strings::toutf8(vector.0)); 33 34 let sum: [SZ]u8 = [0...]; 35 hash::sum(&sha, sum); 36 37 let shahex = hex::encodestr(sum); 38 defer free(shahex); 39 if (shahex != vector.1) { 40 fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; 41 abort(); 42 }; 43 }; 44 }; 45 46 @test fn sha1_1gb() void = { 47 test::require("slow"); 48 49 const input = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno"; 50 const expected = "7789f0c9ef7bfc40d93311143dfbe69e2017f592"; 51 52 let sha = sha1(); 53 54 for (let i = 0z; i < 16777216; i += 1) 55 hash::write(&sha, strings::toutf8(input)); 56 57 let sum: [SZ]u8 = [0...]; 58 hash::sum(&sha, sum); 59 60 let shahex = hex::encodestr(sum); 61 62 if (shahex != expected) { 63 fmt::errorfln("1GB vector: {} != {}", shahex, expected)!; 64 abort(); 65 }; 66 };