Source file src/crypto/sha1/sha1_test.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // SHA-1 hash algorithm. See RFC 3174.
     6  
     7  package sha1
     8  
     9  import (
    10  	"bytes"
    11  	"crypto/internal/boring"
    12  	"crypto/internal/cryptotest"
    13  	"encoding"
    14  	"fmt"
    15  	"hash"
    16  	"io"
    17  	"runtime"
    18  	"sync/atomic"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  type sha1Test struct {
    24  	out       string
    25  	in        string
    26  	halfState string // marshaled hash state after first half of in written, used by TestGoldenMarshal
    27  }
    28  
    29  var golden = []sha1Test{
    30  	{"76245dbf96f661bd221046197ab8b9f063f11bad", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", "sha\x01\v\xa0)I\xdeq(8h\x9ev\xe5\x88[\xf8\x81\x17\xba4Daaaaaaaaaaaaaaaaaaaaaa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x96"},
    31  	{"da39a3ee5e6b4b0d3255bfef95601890afd80709", "", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"},
    32  	{"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8", "a", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"},
    33  	{"da23614e02469a0d7c7bd1bdab5c9c474b1904dc", "ab", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"},
    34  	{"a9993e364706816aba3e25717850c26c9cd0d89d", "abc", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"},
    35  	{"81fe8bfe87576c3ecb22426f8e57847382917acf", "abcd", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0ab\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"},
    36  	{"03de6c570bfe24bfc328ccd7ca46b76eadaf4334", "abcde", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0ab\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02"},
    37  	{"1f8ac10f23c5b5bc1167bda84b833e5c057a77d2", "abcdef", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"},
    38  	{"2fb5e13419fc89246865e7a324f476ec624e8740", "abcdefg", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abc\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x03"},
    39  	{"425af12a0743502b322e93a015bcf868e324d56a", "abcdefgh", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abcd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04"},
    40  	{"c63b19f1e4c8b5f76b25c49b8b87f57d8e4872a1", "abcdefghi", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abcd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04"},
    41  	{"d68c19a0a345b7eab78d5e11e991c026ec60db63", "abcdefghij", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0abcde\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x05"},
    42  	{"ebf81ddcbe5bf13aaabdc4d65354fdf2044f38a7", "Discard medicine more than two years old.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Discard medicine mor\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x14"},
    43  	{"e5dea09392dd886ca63531aaa00571dc07554bb6", "He who has a shady past knows that nice guys finish last.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0He who has a shady past know\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"},
    44  	{"45988f7234467b94e3e9494434c96ee3609d8f8f", "I wouldn't marry him with a ten foot pole.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0I wouldn't marry him \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15"},
    45  	{"55dee037eb7460d5a692d1ce11330b260e40c988", "Free! Free!/A trip/to Mars/for 900/empty jars/Burma Shave", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Free! Free!/A trip/to Mars/f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"},
    46  	{"b7bc5fb91080c7de6b582ea281f8a396d7c0aee8", "The days of the digital watch are numbered.  -Tom Stoppard", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0The days of the digital watch\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1d"},
    47  	{"c3aed9358f7c77f523afe86135f06b95b3999797", "Nepal premier won't resign.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Nepal premier\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\r"},
    48  	{"6e29d302bf6e3a5e4305ff318d983197d6906bb9", "For every action there is an equal and opposite government program.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0For every action there is an equa\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00!"},
    49  	{"597f6a540010f94c15d71806a99a2c8710e747bd", "His money is twice tainted: 'taint yours and 'taint mine.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0His money is twice tainted: \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"},
    50  	{"6859733b2590a8a091cecf50086febc5ceef1e80", "There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0There is no reason for any individual to hav\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,"},
    51  	{"514b2630ec089b8aee18795fc0cf1f4860cdacad", "It's a tiny change to the code and not completely disgusting. - Bob Manchek", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0It's a tiny change to the code and no\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00%"},
    52  	{"c5ca0d4a7b6676fc7aa72caa41cc3d5df567ed69", "size:  a.out:  bad magic", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0size:  a.out\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\f"},
    53  	{"74c51fa9a04eadc8c1bbeaa7fc442f834b90a00a", "The major problem is with sendmail.  -Mark Horton", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0The major problem is wit\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x18"},
    54  	{"0b4c4ce5f52c3ad2821852a8dc00217fa18b8b66", "Give me a rock, paper and scissors and I will move the world.  CCFestoon", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Give me a rock, paper and scissors a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00$"},
    55  	{"3ae7937dd790315beb0f48330e8642237c61550a", "If the enemy is within range, then so are you.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0If the enemy is within \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x17"},
    56  	{"410a2b296df92b9a47412b13281df8f830a9f44b", "It's well we cannot hear the screams/That we create in others' dreams.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0It's well we cannot hear the scream\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00#"},
    57  	{"841e7c85ca1adcddbdd0187f1289acb5c642f7f5", "You remind me of a TV show, but that's all right: I watch it anyway.", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0You remind me of a TV show, but th\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\""},
    58  	{"163173b825d03b952601376b25212df66763e1db", "C is as portable as Stonehedge!!", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0C is as portable\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x10"},
    59  	{"32b0377f2687eb88e22106f133c586ab314d5279", "Even if I could be Shakespeare, I think I should still choose to be Faraday. - A. Huxley", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0Even if I could be Shakespeare, I think I sh\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,"},
    60  	{"0885aaf99b569542fd165fa44e322718f4a984e0", "The fugacity of a constituent in a mixture of gases at a given temperature is proportional to its mole fraction.  Lewis-Randall Rule", "sha\x01x}\xf4\r\xeb\xf2\x10\x87\xe8[\xb2JA$D\xb7\u063ax8em\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00B"},
    61  	{"6627d6904d71420b0bf3886ab629623538689f45", "How can you write a big system without C++?  -Paul Glick", "sha\x01gE#\x01\xef\u036b\x89\x98\xba\xdc\xfe\x102Tv\xc3\xd2\xe1\xf0How can you write a big syst\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1c"},
    62  }
    63  
    64  func TestGolden(t *testing.T) {
    65  	cryptotest.TestAllImplementations(t, "sha1", testGolden)
    66  }
    67  func testGolden(t *testing.T) {
    68  	for i := 0; i < len(golden); i++ {
    69  		g := golden[i]
    70  		s := fmt.Sprintf("%x", Sum([]byte(g.in)))
    71  		if s != g.out {
    72  			t.Fatalf("Sum function: sha1(%s) = %s want %s", g.in, s, g.out)
    73  		}
    74  		c := New()
    75  		for j := 0; j < 4; j++ {
    76  			var sum []byte
    77  			switch j {
    78  			case 0, 1:
    79  				io.WriteString(c, g.in)
    80  				sum = c.Sum(nil)
    81  			case 2:
    82  				io.WriteString(c, g.in[:len(g.in)/2])
    83  				c.Sum(nil)
    84  				io.WriteString(c, g.in[len(g.in)/2:])
    85  				sum = c.Sum(nil)
    86  			case 3:
    87  				if boring.Enabled {
    88  					continue
    89  				}
    90  				io.WriteString(c, g.in[:len(g.in)/2])
    91  				c.(*digest).ConstantTimeSum(nil)
    92  				io.WriteString(c, g.in[len(g.in)/2:])
    93  				sum = c.(*digest).ConstantTimeSum(nil)
    94  			}
    95  			s := fmt.Sprintf("%x", sum)
    96  			if s != g.out {
    97  				t.Fatalf("sha1[%d](%s) = %s want %s", j, g.in, s, g.out)
    98  			}
    99  			c.Reset()
   100  		}
   101  	}
   102  }
   103  
   104  func TestGoldenMarshal(t *testing.T) {
   105  	cryptotest.TestAllImplementations(t, "sha1", testGoldenMarshal)
   106  }
   107  func testGoldenMarshal(t *testing.T) {
   108  	h := New()
   109  	h2 := New()
   110  	for _, g := range golden {
   111  		h.Reset()
   112  		h2.Reset()
   113  
   114  		io.WriteString(h, g.in[:len(g.in)/2])
   115  
   116  		state, err := h.(encoding.BinaryMarshaler).MarshalBinary()
   117  		if err != nil {
   118  			t.Errorf("could not marshal: %v", err)
   119  			continue
   120  		}
   121  
   122  		stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
   123  		if err != nil {
   124  			t.Errorf("could not marshal: %v", err)
   125  			continue
   126  		}
   127  		stateAppend = stateAppend[4:]
   128  
   129  		if string(state) != g.halfState {
   130  			t.Errorf("sha1(%q) state = %+q, want %+q", g.in, state, g.halfState)
   131  			continue
   132  		}
   133  
   134  		if string(stateAppend) != g.halfState {
   135  			t.Errorf("sha1(%q) stateAppend = %+q, want %+q", g.in, stateAppend, g.halfState)
   136  			continue
   137  		}
   138  
   139  		if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
   140  			t.Errorf("could not unmarshal: %v", err)
   141  			continue
   142  		}
   143  
   144  		io.WriteString(h, g.in[len(g.in)/2:])
   145  		io.WriteString(h2, g.in[len(g.in)/2:])
   146  
   147  		if actual, actual2 := h.Sum(nil), h2.Sum(nil); !bytes.Equal(actual, actual2) {
   148  			t.Errorf("sha1(%q) = 0x%x != marshaled 0x%x", g.in, actual, actual2)
   149  		}
   150  	}
   151  }
   152  
   153  func TestSize(t *testing.T) {
   154  	c := New()
   155  	if got := c.Size(); got != Size {
   156  		t.Errorf("Size = %d; want %d", got, Size)
   157  	}
   158  }
   159  
   160  func TestBlockSize(t *testing.T) {
   161  	c := New()
   162  	if got := c.BlockSize(); got != BlockSize {
   163  		t.Errorf("BlockSize = %d; want %d", got, BlockSize)
   164  	}
   165  }
   166  
   167  // Tests for unmarshaling hashes that have hashed a large amount of data
   168  // The initial hash generation is omitted from the test, because it takes a long time.
   169  // The test contains some already-generated states, and their expected sums
   170  // Tests a problem that is outlined in GitHub issue #29543
   171  // The problem is triggered when an amount of data has been hashed for which
   172  // the data length has a 1 in the 32nd bit. When casted to int, this changes
   173  // the sign of the value, and causes the modulus operation to return a
   174  // different result.
   175  type unmarshalTest struct {
   176  	state string
   177  	sum   string
   178  }
   179  
   180  var largeUnmarshalTests = []unmarshalTest{
   181  	// Data length: 7_102_415_735
   182  	{
   183  		state: "sha\x01\x13\xbc\xfe\x83\x8c\xbd\xdfP\x1f\xd8ڿ<\x9eji8t\xe1\xa5@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuv\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xa7VCw",
   184  		sum:   "bc6245c9959cc33e1c2592e5c9ea9b5d0431246c",
   185  	},
   186  	// Data length: 6_565_544_823
   187  	{
   188  		state: "sha\x01m;\x16\xa6R\xbe@\xa9nĈ\xf9S\x03\x00B\xc2\xdcv\xcf@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuv\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x87VCw",
   189  		sum:   "8f2d1c0e4271768f35feb918bfe21ea1387a2072",
   190  	},
   191  }
   192  
   193  func safeSum(h hash.Hash) (sum []byte, err error) {
   194  	defer func() {
   195  		if r := recover(); r != nil {
   196  			err = fmt.Errorf("sum panic: %v", r)
   197  		}
   198  	}()
   199  
   200  	return h.Sum(nil), nil
   201  }
   202  
   203  func TestLargeHashes(t *testing.T) {
   204  	cryptotest.TestAllImplementations(t, "sha1", testLargeHashes)
   205  }
   206  func testLargeHashes(t *testing.T) {
   207  	for i, test := range largeUnmarshalTests {
   208  		h := New()
   209  		if err := h.(encoding.BinaryUnmarshaler).UnmarshalBinary([]byte(test.state)); err != nil {
   210  			t.Errorf("test %d could not unmarshal: %v", i, err)
   211  			continue
   212  		}
   213  
   214  		sum, err := safeSum(h)
   215  		if err != nil {
   216  			t.Errorf("test %d could not sum: %v", i, err)
   217  			continue
   218  		}
   219  
   220  		if fmt.Sprintf("%x", sum) != test.sum {
   221  			t.Errorf("test %d sum mismatch: expect %s got %x", i, test.sum, sum)
   222  		}
   223  	}
   224  }
   225  
   226  func TestAllocations(t *testing.T) {
   227  	cryptotest.SkipTestAllocations(t)
   228  	in := []byte("hello, world!")
   229  	out := make([]byte, 0, Size)
   230  	h := New()
   231  	n := int(testing.AllocsPerRun(10, func() {
   232  		h.Reset()
   233  		h.Write(in)
   234  		out = h.Sum(out[:0])
   235  	}))
   236  	if n > 0 {
   237  		t.Errorf("allocs = %d, want 0", n)
   238  	}
   239  }
   240  
   241  func TestSHA1Hash(t *testing.T) {
   242  	cryptotest.TestAllImplementations(t, "sha1", func(t *testing.T) {
   243  		cryptotest.TestHash(t, New)
   244  	})
   245  }
   246  
   247  func TestExtraMethods(t *testing.T) {
   248  	h := maybeCloner(New())
   249  	cryptotest.NoExtraMethods(t, &h, "ConstantTimeSum",
   250  		"MarshalBinary", "UnmarshalBinary", "AppendBinary")
   251  }
   252  
   253  func maybeCloner(h hash.Hash) any {
   254  	if c, ok := h.(hash.Cloner); ok {
   255  		return &c
   256  	}
   257  	return &h
   258  }
   259  
   260  func TestOutOfBoundsRead(t *testing.T) {
   261  	cryptotest.TestAllImplementations(t, "sha1", func(t *testing.T) {
   262  		start, end := cryptotest.BoundarySlices(t, 1000)
   263  		for i := range len(start) + 1 {
   264  			Sum(start[:i])
   265  			Sum(start[len(start)-i:])
   266  			Sum(end[:i])
   267  			Sum(end[len(end)-i:])
   268  		}
   269  	})
   270  }
   271  
   272  var bench = New()
   273  
   274  func benchmarkSize(b *testing.B, size int) {
   275  	buf := make([]byte, size)
   276  	sum := make([]byte, bench.Size())
   277  	b.Run("New", func(b *testing.B) {
   278  		b.ReportAllocs()
   279  		b.SetBytes(int64(size))
   280  		for i := 0; i < b.N; i++ {
   281  			bench.Reset()
   282  			bench.Write(buf)
   283  			bench.Sum(sum[:0])
   284  		}
   285  	})
   286  	b.Run("Sum", func(b *testing.B) {
   287  		b.ReportAllocs()
   288  		b.SetBytes(int64(size))
   289  		for i := 0; i < b.N; i++ {
   290  			Sum(buf)
   291  		}
   292  	})
   293  }
   294  
   295  func BenchmarkHash8Bytes(b *testing.B) {
   296  	benchmarkSize(b, 8)
   297  }
   298  
   299  func BenchmarkHash320Bytes(b *testing.B) {
   300  	benchmarkSize(b, 320)
   301  }
   302  
   303  func BenchmarkHash1K(b *testing.B) {
   304  	benchmarkSize(b, 1024)
   305  }
   306  
   307  func BenchmarkHash8K(b *testing.B) {
   308  	benchmarkSize(b, 8192)
   309  }
   310  
   311  func BenchmarkHash256K(b *testing.B) {
   312  	benchmarkSize(b, 256*1024)
   313  }
   314  
   315  func BenchmarkHash1M(b *testing.B) {
   316  	benchmarkSize(b, 1024*1024)
   317  }
   318  
   319  var sinkSTW []byte
   320  
   321  // BenchmarkSTW reports how long a garbage collection had to wait while a hash
   322  // ran alongside it, as gcwait-ns/op. Assembly is not preemptible, so a call
   323  // that covers the whole input blocks every goroutine in the process for as
   324  // long as it runs; bounding the call gives the collector a way in between
   325  // chunks. Run with GOMAXPROCS>=2 so the two actually overlap.
   326  func BenchmarkSTW(b *testing.B) {
   327  	buf := make([]byte, 64<<20)
   328  	var total time.Duration
   329  	var iters int
   330  	b.SetBytes(int64(len(buf)))
   331  	for b.Loop() {
   332  		done := make(chan struct{})
   333  		var began atomic.Int64
   334  		go func() {
   335  			defer close(done)
   336  			began.Store(time.Now().UnixNano())
   337  			h := New()
   338  			h.Write(buf)
   339  			sinkSTW = h.Sum(nil)
   340  		}()
   341  		start := time.Now()
   342  		runtime.GC() // one per iteration, so the mean is well defined
   343  		end := time.Now()
   344  		<-done
   345  
   346  		// Count the iteration only if the hash had started before the
   347  		// collection finished. Otherwise there was nothing to overlap and the
   348  		// sample says nothing about preemptibility.
   349  		if t := began.Load(); t != 0 && t < end.UnixNano() {
   350  			total += end.Sub(start)
   351  			iters++
   352  		}
   353  	}
   354  	if iters == 0 {
   355  		b.Skip("no iteration overlapped a collection")
   356  	}
   357  	b.ReportMetric(float64(total.Nanoseconds())/float64(iters), "gcwait-ns/op")
   358  }
   359  

View as plain text