hashmdl_test.go 2.77 KiB
Newer Older
Sandeep S. Shewalkar's avatar
Sandeep S. Shewalkar committed
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, "153120260918", hash, "hash calculated successfully")
Sandeep S. Shewalkar's avatar
Sandeep S. Shewalkar committed

}
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")
}