replaced malloc & strcpy with strdup in core-test

This commit is contained in:
Václav Šmejkal 2023-01-26 13:28:00 +01:00
parent c75c4fd84b
commit 5fd017c6fb
Signed by: ENGO150
GPG Key ID: F6D6DF86242C5A59

View File

@ -30,7 +30,7 @@ int encryptionOperationTest(int text, int encryptedText)
int main(void) int main(void)
{ {
//VARIABLES //VARIABLES
char *textBuffer = malloc(256); char *textBuffer;
char *keyBuffer; char *keyBuffer;
char *statusBuffer; char *statusBuffer;
char *outputBuffer = malloc(1); //THIS IS TEMP ALLOCATION char *outputBuffer = malloc(1); //THIS IS TEMP ALLOCATION
@ -51,7 +51,6 @@ int main(void)
//SET KEY_LENGTH TO 100 //SET KEY_LENGTH TO 100
setKeyLength(100); setKeyLength(100);
keyBuffer = malloc(getKeyLength() + 1);
//SET ENCRYPTION_SEPARATOR TO '|' //SET ENCRYPTION_SEPARATOR TO '|'
setEncryptionSeparator('|'); setEncryptionSeparator('|');
@ -65,8 +64,8 @@ int main(void)
//ENCRYPT //ENCRYPT
outputFlags encrypted = encryptText(TEST_TEXT, NULL); outputFlags encrypted = encryptText(TEST_TEXT, NULL);
strcpy(textBuffer, encrypted.outputText); //GET ENCRYPTED TEXT textBuffer = strdup(encrypted.outputText); //GET ENCRYPTED TEXT
strcpy(keyBuffer, encrypted.usedKey); //GET KEY keyBuffer = strdup(encrypted.usedKey); //GET KEY
timeBuffer = encrypted.elapsedTime; //GET TIME 1 timeBuffer = encrypted.elapsedTime; //GET TIME 1
//DEALLOCATE BUFFER //DEALLOCATE BUFFER
@ -80,13 +79,11 @@ int main(void)
//COMPARE DIFFERENCE //COMPARE DIFFERENCE
if (strcmp(encrypted.outputText, TEST_TEXT) == 0 && encrypted.exitCode == 0) //SUCCESS if (strcmp(encrypted.outputText, TEST_TEXT) == 0 && encrypted.exitCode == 0) //SUCCESS
{ {
statusBuffer = malloc(11); statusBuffer = strdup("successful");
strcpy(statusBuffer, "successful");
} }
else //FAILURE else //FAILURE
{ {
statusBuffer = malloc(7); statusBuffer = strdup("failed");
strcpy(statusBuffer, "failed");
outputBuffer = realloc(outputBuffer, strlen(encrypted.outputText) + 6); outputBuffer = realloc(outputBuffer, strlen(encrypted.outputText) + 6);
sprintf(outputBuffer, "\t\t\"%s\"\n", encrypted.outputText); sprintf(outputBuffer, "\t\t\"%s\"\n", encrypted.outputText);