package chaPolyShrink_test

import "seekia/internal/cryptography/chaPolyShrink"

import "seekia/internal/helpers"

import "testing"

func TestChaPolyShrink(t *testing.T) {

	stringToEncrypt := "Seekia"
	
	for i := 0; i < 1000; i++{

		stringToEncrypt += "BeGeneticsAware"
	}

	bytesToEncrypt := []byte(stringToEncrypt)

	chaPolyKey, err := helpers.GetNewRandom32ByteArray()
	if (err != nil){
		t.Fatal("Failed to get new random 32 byte array: " + err.Error())
	}

	chaPolyNonce, err := helpers.GetNewRandom24ByteArray()
	if (err != nil){
		t.Fatal("Failed to get new random 24 byte array: " + err.Error())
	}

	{
		encryptedBytes, err := chaPolyShrink.EncryptChaPolyShrink(bytesToEncrypt, chaPolyKey, chaPolyNonce, true, 100, false, [32]byte{})	
		if (err != nil) {
			t.Fatal("Failed to encrypt chaPolyShrink: " + err.Error())
		}

		ableToDecrypt, decryptedBytes, err := chaPolyShrink.DecryptChaPolyShrink(encryptedBytes, chaPolyKey, chaPolyNonce, false, [32]byte{})
		if (err != nil) {
			t.Fatal("Failed to decrypt chaPolyShrink: " + err.Error())
		}
		if (ableToDecrypt == false) {
			t.Fatal("Failed to decrypt chaPolyShrink.")
		}

		if (stringToEncrypt != string(decryptedBytes)){
			t.Fatal("Decrypted chaPolyShrink string does not match.")
		}
	}

	{
		// Now we test with no padding

		encryptedBytes, err := chaPolyShrink.EncryptChaPolyShrink(bytesToEncrypt, chaPolyKey, chaPolyNonce, true, 0, false, [32]byte{})		
		if (err != nil) {
			t.Fatal("Failed to encrypt chaPolyShrink: " + err.Error())
		}

		ableToDecrypt, decryptedBytes, err := chaPolyShrink.DecryptChaPolyShrink(encryptedBytes, chaPolyKey, chaPolyNonce, false, [32]byte{})
		if (err != nil) {
			t.Fatal("Failed to decrypt chaPolyShrink: " + err.Error())
		}
		if (ableToDecrypt == false) {
			t.Fatal("Failed to decrypt chaPolyShrink.")
		}

		if (stringToEncrypt != string(decryptedBytes)){
			t.Fatal("Decrypted chaPolyShrink string does not match.")
		}
	}

	{
		// We test with no compression

		encryptedBytes, err := chaPolyShrink.EncryptChaPolyShrink(bytesToEncrypt, chaPolyKey, chaPolyNonce, false, 10000, false, [32]byte{})		
		if (err != nil) {
			t.Fatal("Failed to encrypt chaPolyShrink: " + err.Error())
		}

		ableToDecrypt, decryptedBytes, err := chaPolyShrink.DecryptChaPolyShrink(encryptedBytes, chaPolyKey, chaPolyNonce, false, [32]byte{})
		if (err != nil) {
			t.Fatal("Failed to decrypt chaPolyShrink: " + err.Error())
		}
		if (ableToDecrypt == false) {
			t.Fatal("Failed to decrypt chaPolyShrink.")
		}

		if (stringToEncrypt != string(decryptedBytes)){
			t.Fatal("Decrypted chaPolyShrink string does not match.")
		}
	}

	{
		// We test with additional data

		additionalData, err := helpers.GetNewRandom32ByteArray()
		if (err != nil){
			t.Fatal("Failed to get new random 32 byte array: " + err.Error())
		}

		encryptedBytes, err := chaPolyShrink.EncryptChaPolyShrink(bytesToEncrypt, chaPolyKey, chaPolyNonce, false, 10000, true, additionalData)		
		if (err != nil) {
			t.Fatal("Failed to encrypt chaPolyShrink: " + err.Error())
		}

		ableToDecrypt, decryptedBytes, err := chaPolyShrink.DecryptChaPolyShrink(encryptedBytes, chaPolyKey, chaPolyNonce, true, additionalData)
		if (err != nil) {
			t.Fatal("Failed to decrypt chaPolyShrink: " + err.Error())
		}
		if (ableToDecrypt == false) {
			t.Fatal("Failed to decrypt chaPolyShrink.")
		}

		if (stringToEncrypt != string(decryptedBytes)){
			t.Fatal("Decrypted chaPolyShrink string does not match.")
		}


		// We test decryption with invalid additionalData

		invalidAdditionalData, err := helpers.GetNewRandom32ByteArray()
		if (err != nil){
			t.Fatal("Failed to get new random 32 byte array: " + err.Error())
		}

		ableToDecrypt, _, err = chaPolyShrink.DecryptChaPolyShrink(encryptedBytes, chaPolyKey, chaPolyNonce, true, invalidAdditionalData)
		if (err != nil) {
			t.Fatal("Failed to decrypt chaPolyShrink: " + err.Error())
		}
		if (ableToDecrypt == true) {
			t.Fatal("Able to decrypt chaPolyShrink with invalidAdditionalData.")
		}
	}

}




