File: sha1_test.go

package info (click to toggle)
golang-github-mmcloughlin-avo 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 15,024 kB
  • sloc: xml: 71,029; asm: 14,862; sh: 194; makefile: 21; ansic: 11
file content (46 lines) | stat: -rw-r--r-- 1,044 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package sha1

import (
	"bytes"
	"crypto/sha1"
	"encoding/hex"
	"testing"
	"testing/quick"
)

//go:generate go run asm.go -out sha1.s -stubs stub.go

func TestVectors(t *testing.T) {
	cases := []struct {
		Data      string
		HexDigest string
	}{
		{"", "da39a3ee5e6b4b0d3255bfef95601890afd80709"},
		{"The quick brown fox jumps over the lazy dog", "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"},
		{"The quick brown fox jumps over the lazy cog", "de9f2c7fd25e1b3afad3e85a0bd17d9b100db4b3"},
	}
	for _, c := range cases {
		digest := Sum([]byte(c.Data))
		got := hex.EncodeToString(digest[:])
		if got != c.HexDigest {
			t.Errorf("Sum(%#v) = %s; expect %s", c.Data, got, c.HexDigest)
		}
	}
}

func TestCmp(t *testing.T) {
	if err := quick.CheckEqual(Sum, sha1.Sum, nil); err != nil {
		t.Fatal(err)
	}
}

func TestLengths(t *testing.T) {
	data := make([]byte, BlockSize)
	for n := 0; n <= BlockSize; n++ {
		got := Sum(data[:n])
		expect := sha1.Sum(data[:n])
		if !bytes.Equal(got[:], expect[:]) {
			t.Errorf("failed on length %d", n)
		}
	}
}