Issues (26)

fdh_test.go (22 issues)

Severity
1
package fdh
2
3
import (
4
	"bytes"
5
	"crypto"
6
	"crypto/sha256"
7
	"encoding/hex"
8
	"testing"
9
)
10
11
var message = []byte("ATTACK AT DAWN")
12
13
func TestKnownResults(t *testing.T) {
14
	h := New(crypto.SHA256, 256)
15
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
16
	// echo -n -e 'ATTACK AT DAWN\x00' | shasum -a 256
17
	if hex.EncodeToString(h.Sum(nil)) != "015d53c7925b4434f00286fe2f0eb28378a49300b159b896eb2356a7c4de95f1" {
18
		t.Error("Bad result on known outout")
19
	}
20
21
	h = New(crypto.SHA256, 128)
22
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
23
	// echo -n -e 'ATTACK AT DAWN\x00' | shasum -a 256
24
	if hex.EncodeToString(h.Sum(nil)) != "015d53c7925b4434f00286fe2f0eb283" {
25
		t.Error("Bad result on known outout")
26
	}
27
28
	h = New(crypto.SHA256, 264)
29
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
30
	h.Sum(nil)
31
	// echo -n -e 'ATTACK AT DAWN\x00' | shasum -a 256 && echo -n -e 'ATTACK AT DAWN\x01' | shasum -a 256
32
	if hex.EncodeToString(h.Sum(nil)) != "015d53c7925b4434f00286fe2f0eb28378a49300b159b896eb2356a7c4de95f158" {
33
		t.Error("Bad result on known outout")
34
	}
35
36
	h = New(crypto.SHA256, 1024)
37
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
38
	// See READNE.md for bash command under "Bash Equivalent" sectiin.
39
	if hex.EncodeToString(h.Sum(nil)) != "015d53c7925b4434f00286fe2f0eb28378a49300b159b896eb2356a7c4de95f158617fec3b813f834cd86ab0dd26b971c46b7ede451b490279628a265edf0a10691095675808b47c0add4300b3181a31109cbc31a945d05562ceb6cca0fea834d9c456fe1abf34a5a775ed572ce571b1dcca03b984102e666e9ab876876fb3af" {
40
		t.Error("Bad result on known outout")
41
	}
42
43
}
44
45
func TestSHA256(t *testing.T) {
46
	h := New(crypto.SHA256, 1024)
47
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
48
	result := h.Sum(nil)
49
50
	if len(result) != 1024/8 {
51
		t.Error("Hash result not the same length as bit length")
52
	}
53
	if h.Size() != len(result) {
54
		t.Error("Hash result not the same length Size()")
55
	}
56
	if h.BlockSize() != sha256.BlockSize {
57
		t.Error("Incorrect block size")
58
	}
59
60
	// Now let's do it manually and confirm they are the same
61
	var manual []byte
62
	h0 := sha256.New()
63
	h0.Write(message)
0 ignored issues
show
error return value not checked (h0.Write(message))
Loading history...
64
	h0.Write([]byte{byte(0)})
0 ignored issues
show
error return value not checked (h0.Write([]bytebyte(0)))
Loading history...
65
	manual = h0.Sum(manual)
66
67
	h1 := sha256.New()
68
	h1.Write(message)
0 ignored issues
show
error return value not checked (h1.Write(message))
Loading history...
69
	h1.Write([]byte{byte(1)})
0 ignored issues
show
error return value not checked (h1.Write([]bytebyte(1)))
Loading history...
70
	manual = h1.Sum(manual)
71
72
	h2 := sha256.New()
73
	h2.Write(message)
0 ignored issues
show
error return value not checked (h2.Write(message))
Loading history...
74
	h2.Write([]byte{byte(2)})
0 ignored issues
show
error return value not checked (h2.Write([]bytebyte(2)))
Loading history...
75
	manual = h2.Sum(manual)
76
77
	h3 := sha256.New()
78
	h3.Write(message)
0 ignored issues
show
error return value not checked (h3.Write(message))
Loading history...
79
	h3.Write([]byte{byte(3)})
0 ignored issues
show
error return value not checked (h3.Write([]bytebyte(3)))
Loading history...
80
	manual = h3.Sum(manual)
81
82
	if !bytes.Equal(result, manual) {
83
		t.Error("Hash result not the same as manually constructed result")
84
	}
85
86
	// Test calling the utility Sum function
87
	if !bytes.Equal(result, Sum(crypto.SHA256, 1024, message)) {
88
		t.Error("Hash result not the same when called via Sum")
89
	}
90
}
91
92
// Writing after calling Sum() should panic
93
func TestPanicFinalized(t *testing.T) {
94
	defer func() {
95
		if r := recover(); r != nil {
96
			return
97
		} else {
0 ignored issues
show
if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
Loading history...
98
			t.Error("Failed to panic for writing after being finalized")
99
		}
100
	}()
101
102
	h := New(crypto.SHA256, 1024)
103
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
104
	h.Sum(nil)
105
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
106
}
107
108
// Using an unimported hash should panic
109
func TestPanicNoImport(t *testing.T) {
110
	defer func() {
111
		if r := recover(); r != nil {
112
			return
113
		} else {
0 ignored issues
show
if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
Loading history...
114
			t.Error("Failed to panic for using unimported hash")
115
		}
116
	}()
117
118
	h := New(crypto.MD5, 1024)
119
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
120
	h.Sum(nil)
121
}
122
123
// Using an unimported hash should panic
124
func TestPanicOddBitlen(t *testing.T) {
125
	defer func() {
126
		if r := recover(); r != nil {
127
			return
128
		} else {
0 ignored issues
show
if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
Loading history...
129
			t.Error("Failed to panic when using a bitlen that does not fit hash")
130
		}
131
	}()
132
133
	h := New(crypto.SHA256, 2379)
134
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
135
	h.Sum(nil)
136
}
137
138
// Using an unimported hash should panic
139
func TestPanicZeroBitlen(t *testing.T) {
140
	defer func() {
141
		if r := recover(); r != nil {
142
			return
143
		} else {
0 ignored issues
show
if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
Loading history...
144
			t.Error("Failed to panic when using a small bitlen")
145
		}
146
	}()
147
148
	h := New(crypto.SHA256, 0)
149
	h.Write(message)
0 ignored issues
show
error return value not checked (h.Write(message))
Loading history...
150
	h.Sum(nil)
151
}
152