2018-04-25 06:17:20 +00:00
|
|
|
package scha
|
|
|
|
|
|
|
|
import "testing"
|
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
func TestSimpleHash(t *testing.T) {
|
2018-04-25 06:17:20 +00:00
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
input := []byte("somePlainText")
|
2018-04-25 06:17:20 +00:00
|
|
|
expected := []byte{
|
|
|
|
0x4c, 0xcb, 0x0e, 0xf6, 0x81, 0x99, 0x2e, 0xd6, 0xb8, 0x17, 0x52, 0x1d, 0x09,
|
|
|
|
0x6e, 0x99, 0x19, 0xe7, 0xda, 0x50, 0xc8, 0xbf, 0x64, 0xae, 0xc1, 0x4f, 0xaa,
|
|
|
|
0x47, 0x06, 0xf3, 0x49, 0x30, 0x8a, 0x90, 0x8e, 0xd2, 0xff, 0xc2, 0x6d, 0xee,
|
|
|
|
0xaa, 0xd6, 0x45, 0xd8, 0xb3, 0x17, 0xe3, 0xb9, 0x45, 0x29, 0x26, 0xe2, 0x8e,
|
2018-09-06 14:41:03 +00:00
|
|
|
0x99, 0x50, 0x94, 0x49, 0x90, 0x02, 0xa5, 0x61, 0x4a, 0x3f, 0x5e, 0xfa}
|
|
|
|
got, err := Hash(input, 1)
|
|
|
|
digestLength := uint(len(got))
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
/* (2) Fail on errors */
|
|
|
|
if err != nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected no errors, got: %s", err)
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Fail on wrong size */
|
2018-09-06 14:41:03 +00:00
|
|
|
if uint16(digestLength) != HSIZE {
|
|
|
|
t.Errorf("Expected hash digest of %d bytes ; %d bytes received", HSIZE, digestLength)
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Check each byte */
|
2018-09-06 14:41:03 +00:00
|
|
|
for k, v := range got {
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
if v != expected[k] {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected sha[%d] of '%x' to be '%x' ; received '%x'", HSIZE, input, expected, got)
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
func TestDepth1Plus1Equals2(t *testing.T) {
|
2018-04-25 06:17:20 +00:00
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
input := []byte("someOtherPlainText")
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
/* (1) Calculate H1 */
|
|
|
|
h1, err := Hash(input, 1)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected no error")
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Calculate H1(H1) */
|
2018-09-06 14:41:03 +00:00
|
|
|
h11, err := Hash(h1, 1)
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected no error")
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Calculate H2 */
|
2018-09-06 14:41:03 +00:00
|
|
|
h2, err := Hash(input, 1+1)
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected no error")
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Manage different length */
|
|
|
|
if len(h11) != len(h2) || len(h11) != int(HSIZE) {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected digest lengths to be %d, got %d and %d", HSIZE, len(h11), len(h2))
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Compare the 2 strings */
|
|
|
|
for k, v := range h11 {
|
|
|
|
if v != h2[k] {
|
|
|
|
t.Errorf("Expected h2() to be equal to h1(h1())\n got '%x'\n expected '%x'", h2, h11)
|
2018-09-06 14:41:03 +00:00
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
func TestDepth52Plus64Equals116(t *testing.T) {
|
2018-04-25 06:17:20 +00:00
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
input := []byte("someOtherPlainText")
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
/* (1) Calculate H52 */
|
|
|
|
h52, err := Hash(input, 52)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected no error")
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (2) Calculate H52(H64) */
|
2018-09-06 14:41:03 +00:00
|
|
|
h5264, err := Hash(h52, 64)
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected no error")
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (3) Calculate H116 */
|
2018-09-06 14:41:03 +00:00
|
|
|
h116, err := Hash(input, 52+64)
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected no error")
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Manage different length */
|
|
|
|
if len(h5264) != len(h116) || len(h5264) != int(HSIZE) {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected digest lengths to be %d, got %d and %d", HSIZE, len(h5264), len(h116))
|
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* (5) Compare the 2 strings */
|
|
|
|
for k, v := range h5264 {
|
|
|
|
if v != h116[k] {
|
|
|
|
t.Errorf("Expected h116() to be equal to h52(h64())\n got '%x'\n expected '%x'", h116, h5264)
|
2018-09-06 14:41:03 +00:00
|
|
|
return
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
func TestDepthError(t *testing.T) {
|
2018-04-25 06:17:20 +00:00
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
input := []byte("somePlainText")
|
|
|
|
_, err := Hash(input, 0)
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
/* (2) Fail on errors */
|
|
|
|
if err == nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected an error for depth of 0")
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
func TestEmptyInputError(t *testing.T) {
|
2018-04-25 06:17:20 +00:00
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
_, err := Hash(nil, 1)
|
2018-04-25 06:17:20 +00:00
|
|
|
|
|
|
|
/* (2) Fail on errors */
|
|
|
|
if err == nil {
|
2018-09-06 14:41:03 +00:00
|
|
|
t.Errorf("Expected an error for empty input")
|
2018-04-25 06:17:20 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 14:41:03 +00:00
|
|
|
}
|