|
|
|
@@ -12,10 +12,10 @@ func TestSecureHash(t *testing.T) {
|
|
|
|
algo := "sha256"
|
|
|
|
algo := "sha256"
|
|
|
|
h, err := New(algo)
|
|
|
|
h, err := New(algo)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.BoolT(t, h.IsSecure(), algo + " should be a secure hash")
|
|
|
|
assert.BoolT(t, h.IsSecure(), algo+" should be a secure hash")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, !h.IsHash32(), algo + " isn't actually a 32-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash32(), algo+" isn't actually a 32-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash64(), algo + " isn't actually a 64-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash64(), algo+" isn't actually a 64-bit hash")
|
|
|
|
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
var data []byte
|
|
|
|
var expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
|
|
var expected = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"
|
|
|
|
@@ -42,10 +42,10 @@ func TestInsecureHash(t *testing.T) {
|
|
|
|
algo := "md5"
|
|
|
|
algo := "md5"
|
|
|
|
h, err := New(algo)
|
|
|
|
h, err := New(algo)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.BoolT(t, !h.IsSecure(), algo + " shouldn't be a secure hash")
|
|
|
|
assert.BoolT(t, !h.IsSecure(), algo+" shouldn't be a secure hash")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, !h.IsHash32(), algo + " isn't actually a 32-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash32(), algo+" isn't actually a 32-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash64(), algo + " isn't actually a 64-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash64(), algo+" isn't actually a 64-bit hash")
|
|
|
|
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
var data []byte
|
|
|
|
var expected = "d41d8cd98f00b204e9800998ecf8427e"
|
|
|
|
var expected = "d41d8cd98f00b204e9800998ecf8427e"
|
|
|
|
@@ -72,31 +72,31 @@ func TestHash32(t *testing.T) {
|
|
|
|
algo := "crc32-ieee"
|
|
|
|
algo := "crc32-ieee"
|
|
|
|
h, err := New(algo)
|
|
|
|
h, err := New(algo)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.BoolT(t, !h.IsSecure(), algo + " shouldn't be a secure hash")
|
|
|
|
assert.BoolT(t, !h.IsSecure(), algo+" shouldn't be a secure hash")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, h.IsHash32(), algo + " is actually a 32-bit hash")
|
|
|
|
assert.BoolT(t, h.IsHash32(), algo+" is actually a 32-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash64(), algo + " isn't actually a 64-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash64(), algo+" isn't actually a 64-bit hash")
|
|
|
|
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
var data []byte
|
|
|
|
var expected uint32
|
|
|
|
var expected uint32
|
|
|
|
|
|
|
|
|
|
|
|
h.Write(data)
|
|
|
|
h.Write(data)
|
|
|
|
sum, ok := h.Sum32()
|
|
|
|
sum, ok := h.Sum32()
|
|
|
|
assert.BoolT(t, ok, algo + " should be able to return a Sum32")
|
|
|
|
assert.BoolT(t, ok, algo+" should be able to return a Sum32")
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
|
|
|
|
|
|
|
|
data = []byte("hello, world")
|
|
|
|
data = []byte("hello, world")
|
|
|
|
expected = 0xffab723a
|
|
|
|
expected = 0xffab723a
|
|
|
|
h.Write(data)
|
|
|
|
h.Write(data)
|
|
|
|
sum, ok = h.Sum32()
|
|
|
|
sum, ok = h.Sum32()
|
|
|
|
assert.BoolT(t, ok, algo + " should be able to return a Sum32")
|
|
|
|
assert.BoolT(t, ok, algo+" should be able to return a Sum32")
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
|
|
|
|
|
|
|
|
h.Reset()
|
|
|
|
h.Reset()
|
|
|
|
data = []byte("hello world")
|
|
|
|
data = []byte("hello world")
|
|
|
|
h.Write(data)
|
|
|
|
h.Write(data)
|
|
|
|
sum, ok = h.Sum32()
|
|
|
|
sum, ok = h.Sum32()
|
|
|
|
assert.BoolT(t, ok, algo + " should be able to return a Sum32")
|
|
|
|
assert.BoolT(t, ok, algo+" should be able to return a Sum32")
|
|
|
|
assert.BoolT(t, expected != sum, fmt.Sprintf("%s returned %d but shouldn't have", algo, sum, expected))
|
|
|
|
assert.BoolT(t, expected != sum, fmt.Sprintf("%s returned %d but shouldn't have", algo, sum, expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -104,31 +104,31 @@ func TestHash64(t *testing.T) {
|
|
|
|
algo := "crc64"
|
|
|
|
algo := "crc64"
|
|
|
|
h, err := New(algo)
|
|
|
|
h, err := New(algo)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.NoErrorT(t, err)
|
|
|
|
assert.BoolT(t, !h.IsSecure(), algo + " shouldn't be a secure hash")
|
|
|
|
assert.BoolT(t, !h.IsSecure(), algo+" shouldn't be a secure hash")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, h.HashAlgo() == algo, "hash returned the wrong HashAlgo")
|
|
|
|
assert.BoolT(t, h.IsHash64(), algo + " is actually a 64-bit hash")
|
|
|
|
assert.BoolT(t, h.IsHash64(), algo+" is actually a 64-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash32(), algo + " isn't actually a 32-bit hash")
|
|
|
|
assert.BoolT(t, !h.IsHash32(), algo+" isn't actually a 32-bit hash")
|
|
|
|
|
|
|
|
|
|
|
|
var data []byte
|
|
|
|
var data []byte
|
|
|
|
var expected uint64
|
|
|
|
var expected uint64
|
|
|
|
|
|
|
|
|
|
|
|
h.Write(data)
|
|
|
|
h.Write(data)
|
|
|
|
sum, ok := h.Sum64()
|
|
|
|
sum, ok := h.Sum64()
|
|
|
|
assert.BoolT(t, ok, algo + " should be able to return a Sum64")
|
|
|
|
assert.BoolT(t, ok, algo+" should be able to return a Sum64")
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
|
|
|
|
|
|
|
|
data = []byte("hello, world")
|
|
|
|
data = []byte("hello, world")
|
|
|
|
expected = 0x16c45c0eb1d9c2ec
|
|
|
|
expected = 0x16c45c0eb1d9c2ec
|
|
|
|
h.Write(data)
|
|
|
|
h.Write(data)
|
|
|
|
sum, ok = h.Sum64()
|
|
|
|
sum, ok = h.Sum64()
|
|
|
|
assert.BoolT(t, ok, algo + " should be able to return a Sum64")
|
|
|
|
assert.BoolT(t, ok, algo+" should be able to return a Sum64")
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
assert.BoolT(t, expected == sum, fmt.Sprintf("%s returned the %d but expected %d", algo, sum, expected))
|
|
|
|
|
|
|
|
|
|
|
|
h.Reset()
|
|
|
|
h.Reset()
|
|
|
|
data = []byte("hello world")
|
|
|
|
data = []byte("hello world")
|
|
|
|
h.Write(data)
|
|
|
|
h.Write(data)
|
|
|
|
sum, ok = h.Sum64()
|
|
|
|
sum, ok = h.Sum64()
|
|
|
|
assert.BoolT(t, ok, algo + " should be able to return a Sum64")
|
|
|
|
assert.BoolT(t, ok, algo+" should be able to return a Sum64")
|
|
|
|
assert.BoolT(t, expected != sum, fmt.Sprintf("%s returned %d but shouldn't have", algo, sum, expected))
|
|
|
|
assert.BoolT(t, expected != sum, fmt.Sprintf("%s returned %d but shouldn't have", algo, sum, expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -137,5 +137,5 @@ func TestListLengthSanity(t *testing.T) {
|
|
|
|
secure := SecureHashList()
|
|
|
|
secure := SecureHashList()
|
|
|
|
insecure := InsecureHashList()
|
|
|
|
insecure := InsecureHashList()
|
|
|
|
|
|
|
|
|
|
|
|
assert.BoolT(t, len(all) == len(secure) + len(insecure))
|
|
|
|
assert.BoolT(t, len(all) == len(secure)+len(insecure))
|
|
|
|
}
|
|
|
|
}
|