Newer
Older
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package hashmdl
import (
"testing"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
"github.com/stretchr/testify/assert"
)
func TestGetHashChecksumOfByteArray(t *testing.T) {
data := []byte("This is test value")
type args struct {
byteArray []byte
}
tests := []struct {
name string
args args
want uint64
wantError bool
}{
{"success scenario", args{data}, 12276080534736706571, false},
{"output mismatched", args{data}, 12276080534736706570, true},
}
for _, tt := range tests {
hash, _ := GetHashChecksumOfByteArray(data)
if tt.wantError {
assert.NotEqual(t, tt.want, hash, "Output not matching")
} else {
assert.Equal(t, tt.want, hash, "Output matching")
}
}
for _, tt := range tests {
errormdl.IsTestingNegetiveCaseOn = true
_, hashError := GetHashChecksumOfByteArray(data)
if tt.wantError {
assert.Error(t, hashError, "test error occued")
}
}
errormdl.IsTestingNegetiveCaseOn = false
}
func TestGetHashChecksumBlankByteArray(t *testing.T) {
blankData := []byte{}
_, hashError := GetHashChecksumOfByteArray(blankData)
assert.Error(t, hashError, "error occured")
}
func TestGetHashChecksumOfFileSuccess(t *testing.T) {
filePath := "../testingdata/hashcalculation.txt"
hash, _ := GetHashChecksumOfFile(filePath)
var expectedHash uint64
expectedHash = 12276080534736706571
assert.Equal(t, expectedHash, hash, "Output matching")
}
func TestGetHashChecksumOfFilePathFail(t *testing.T) {
filePath := "../testingdata/hashcalculation.tx"
_, hashError := GetHashChecksumOfFile(filePath)
assert.Error(t, hashError, "error occured")
}
func TestGetHashChecksumOfFileHashFail(t *testing.T) {
errormdl.IsTestingNegetiveCaseOn1 = true
filePath := "../testingdata/hashcalculation.txt"
_, hashError := GetHashChecksumOfFile(filePath)
assert.Error(t, hashError, "error occured")
errormdl.IsTestingNegetiveCaseOn1 = false
}
func TestGetAttributeBAsedHashSuccess(t *testing.T) {
filePath := "../testingdata/hashcalculation.txt"
hash, _ := GetAtributeBasedHash(filePath)
assert.Equal(t, "153096243718", hash, "hash calculated successfully")
}
func TestGetAttributeBasedHashFileNotFound(t *testing.T) {
filePath := "../testingdata/hashcalculation.tx"
_, hashError := GetAtributeBasedHash(filePath)
assert.Error(t, hashError, "file not found")
}
func TestGet128BitHash(t *testing.T) {
data := []byte("This is test value")
hash, _ := Get128BitHash(data)
// [153 250 83 89 165 124 176 214 93 174 227 143 162 183 105 127]
expectedHash := [16]byte{153, 250, 83, 89, 165, 124, 176, 214, 93, 174, 227, 143, 162, 183, 105, 127}
assert.Equal(t, expectedHash, hash, "matched")
}
func TestGet128BitHashError(t *testing.T) {
data := []byte("")
_, hashError := Get128BitHash(data)
assert.Error(t, hashError, "nil/empty byte array")
}