Skip to content

Commit

Permalink
Add tests for Annex-K functions
Browse files Browse the repository at this point in the history
Implemented unit tests for the Annex-K functions to ensure their corrctness.
Covered various scenarios including normal operation, boundary conditions, and error handling.

Signed-off-by: Mostafa Salman <[email protected]>
  • Loading branch information
mostafa-salmaan committed Jul 31, 2024
1 parent a5676de commit ed68028
Show file tree
Hide file tree
Showing 11 changed files with 588 additions and 139 deletions.
70 changes: 56 additions & 14 deletions test/test-memcpy_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,62 @@
#include <string.h>
#include <errno.h>

int main(void)
void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error);

void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error)
{
char src[] = "ABCDEF";
char dst[10];

errno_t err = memcpy_s( dst, sizeof(dst), src, strlen(src)+1 );
if (err == 0)
{
printf("memcpy_s sucessful: %s\n", dst);
return 0;
}
else
{
printf("memcpy_s failed: %s\n", strerror(err));
return -1;
(void) ptr;
(void) error;
printf("Custom constraint handler called: %s\n", msg);
}

int test_id = 0;

#define TEST(cond, msg) \
if (!(cond)) { \
printf("Test %d Failed: %s\n", test_id, msg); \
return 1; \
} else { \
printf("Test %d Passed: %s\n", test_id, msg); \
}


int main(void)
{
char src[] = "Hello, world!";
char dest[50];
errno_t res;

set_constraint_handler_s( custom_constraint_handler );

// Test case 1: Normal copy
test_id++;
res = memcpy_s( dest, sizeof(dest), src, strlen(src) + 1 );
TEST(res == 0, "Normal Copy");
TEST(strcmp(dest, "Hello, world!") == 0, "Normal Copy Contents");

// Test case 2: Copy with insufficient destination size
test_id++;
res = memcpy_s( dest, 5, src, strlen(src) + 1 );
TEST(res != 0, "Copy with insufficient destination size");

// Test case 3: Copy with Null destination
test_id++;
res = memcpy_s( NULL, sizeof(dest), src, strlen(src) + 1 );
TEST(res != 0, "Copy with Null destination");

// Test case 4: Copy with Null source
test_id++;
res = memcpy_s( dest, sizeof(dest), NULL, strlen(src) + 1 );
TEST(res != 0, "Copy with Null source");

// Test case 5: Copy with zero length
test_id++;
strcpy(dest, "");
res = memcpy_s( dest, sizeof(dest), src, 0 );
TEST(res == 0, "Copy with zero length");
TEST(dest[0] == '\0', "Copy with zero length Contents")

printf("All memcpy_s tests passed!\n");
return 0;
}
68 changes: 54 additions & 14 deletions test/test-memmove_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,60 @@
#include <string.h>
#include <errno.h>

int main(void)
void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error);

void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error)
{
char src[] = "ABCDEF";
char dst[10];

errno_t err = memmove_s( dst, sizeof(dst), src, strlen(src)+1 );
if (err == 0)
{
printf("memmove_s sucessful: %s\n", dst);
return 0;
}
else
{
printf("memmove_s failed: %s\n", strerror(err));
return -1;
(void) ptr;
(void) error;
printf("Custom constraint handler called: %s\n", msg);
}

int test_id = 0;

#define TEST(cond, msg) \
if (!(cond)) { \
printf("Test %d Failed: %s\n", test_id, msg); \
return 1; \
} else { \
printf("Test %d Passed: %s\n", test_id, msg); \
}

int main(void)
{
char buf[50] = "Hello, world!";
errno_t res;

set_constraint_handler_s( custom_constraint_handler );

// Test case 1: Normal move
test_id++;
res = memmove_s( buf + 7, sizeof(buf) - 7, buf, strlen(buf) + 1 );
TEST(res == 0, "Normal move");
TEST(strcmp(buf, "Hello, Hello, world!") == 0, "Normal move Contents");

// Test case 2: Move with insufficient destination size
test_id++;
res = memmove_s( buf + 7, 5, buf, strlen(buf) + 1 );
TEST(res != 0, "Move with insufficient destination size");

// Test case 3: Move with Null destination
test_id++;
res = memmove_s( NULL, sizeof(buf), buf, strlen(buf) + 1 );
TEST(res != 0, "Move with Null destination");

// Test case 4: Move with Null source
test_id++;
res = memmove_s( buf, sizeof(buf), NULL, strlen(buf) + 1 );
TEST(res != 0, "Move with Null source");

// Test case 5: Move with zero length
test_id++;
strcpy(buf, "");
res = memmove_s( buf, sizeof(buf), buf, 0 );
TEST(res == 0, "Move with zero length");
TEST(buf[0] == '\0', "Move with zero length Contents")

printf("All memmove_s tests passed!\n");
return 0;
}
59 changes: 47 additions & 12 deletions test/test-memset_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,54 @@
#include <string.h>
#include <errno.h>

int main(void)
void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error);

void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error)
{
char dst[10];
(void) ptr;
(void) error;
printf("Custom constraint handler called: %s\n", msg);
}

errno_t err = memset_s( dst, sizeof(dst), 'X', sizeof(dst) );
if (err == 0)
{
printf("memset_s sucessful: %s\n", dst);
return 0;
}
else
{
printf("memset_s failed: %s\n", strerror(err));
return -1;
int test_id = 0;

#define TEST(cond, msg) \
if (!(cond)) { \
printf("Test %d Failed: %s\n", test_id, msg); \
return 1; \
} else { \
printf("Test %d Passed: %s\n", test_id, msg); \
}

int main(void)
{
char buf[50] = "Hello, world!";
errno_t res;

set_constraint_handler_s( custom_constraint_handler );

// Test case 1: Normal Set
test_id++;
res = memset_s( buf, sizeof(buf), 'A', strlen("Hello, world!"));
TEST(res == 0, "Normal Set");
TEST(strcmp(buf, "AAAAAAAAAAAAA") == 0, "Normal Set Contents");

// Test case 2: Zero-length Set
test_id++;
res = memset_s( buf, sizeof(buf), 'B', 0);
TEST(res == 0, "Zero-length Set");
TEST(strcmp(buf, "AAAAAAAAAAAAA") == 0, "Zero-length Set Contents");

// Test case 3: Null pointers
test_id++;
res = memset_s( NULL, sizeof(buf), 'C', strlen("Hello, world!") );
TEST(res != 0, "NULL Destination Pointer");

// Test case 4: Set with zero buffer size
test_id++;
res = memset_s( buf, 0, 'D', strlen("Hello, world!"));
TEST(res != 0, "Set with zero buffer size");

printf("All memset_s tests passed!\n");
return 0;
}
68 changes: 53 additions & 15 deletions test/test-sprintf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,59 @@
#include <string.h>
#include <errno.h>

int main(void)
void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error);

void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error)
{
char buf[20];
int value = 123;
const char *text = "Hello";

errno_t err = sprintf_s(buf, sizeof(buf), "%s, value: %d", text, value);
if (err >= 0)
{
printf("Formatted string: %s\n", buf);
return 0;
}
else
{
printf("Error occurred: %d\n", err);
return -1;
(void) ptr;
(void) error;
printf("Custom constraint handler called: %s\n", msg);
}

int test_id = 0;

#define TEST(cond, msg) \
if (!(cond)) { \
printf("Test %d Failed: %s\n", test_id, msg); \
return 1; \
} else { \
printf("Test %d Passed: %s\n", test_id, msg); \
}

int main(void)
{
char buf[50];
errno_t res;

set_constraint_handler_s( custom_constraint_handler );

// Test case 1: Normal formatting
test_id++;
res = sprintf_s( buf, sizeof(buf), "Hello, %s!", "world" );
TEST(res == strlen("Hello, world!"), "Normal formatting");
TEST(strcmp(buf, "Hello, world!") == 0, "Normal formatting Contents");

// Test case 2: Formatting with buffer overflow
test_id++;
res = sprintf_s( buf, 10, "Hello, %s!", "world" );
TEST(res == 0, "Formatting with buffer overflow");

// Test case 3: Formatting with Null buffer
test_id++;
res = sprintf_s( NULL, sizeof(buf), "Hello, %s!", "world" );
TEST(res == 0, "Formatting with Null buffer");

// Test case 4: Formatting with Null format string
test_id++;
res = sprintf_s( buf, sizeof(buf), NULL, "world" );
TEST(res == 0, "Formatting with Null format string");

// Test case 5: Empty format string
test_id++;
res = sprintf_s( buf, sizeof(buf), "", "world" );
TEST(res == 0, "Empty format string");
TEST(strcmp(buf, "") == 0, "Empty format string Contents");

printf("All sprintf_s tests passed!\n");
return 0;
}
73 changes: 59 additions & 14 deletions test/test-strcat_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,65 @@
#include <string.h>
#include <errno.h>

int main(void)
void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error);

void custom_constraint_handler(const char *restrict msg, void *restrict ptr, __errno_t error)
{
const char *src = "world!";
char dst[20] = "Hello ";

errno_t err = strcat_s( dst, sizeof(dst), src );
if (err == 0)
{
printf("strcat_s sucessful: %s\n", dst);
return 0;
}
else
{
printf("strcat_s failed: %s\n", strerror(err));
return -1;
(void) ptr;
(void) error;
printf("Custom constraint handler called: %s\n", msg);
}

int test_id = 0;

#define TEST(cond, msg) \
if (!(cond)) { \
printf("Test %d Failed: %s\n", test_id, msg); \
return 1; \
} else { \
printf("Test %d Passed: %s\n", test_id, msg); \
}

int main(void)
{
char dest[50] = "Hello";
char src[] = ", world!";
errno_t res;

set_constraint_handler_s( custom_constraint_handler );

// Test case 1: Normal Concatenation
test_id++;
res = strcat_s( dest, sizeof(dest), src );
TEST(res == 0, "Normal Concatenation");
TEST(strcmp(dest, "Hello, world!") == 0, "Normal Concatenation Contents");

// Test case 2: Concatenation with insufficient buffer
test_id++;
res = strcat_s( dest, 10, src );
TEST(res != 0, "Concatenation with insufficient buffer");

// Test case 3: Null pointers
test_id++;
res = strcat_s( NULL, sizeof(dest), src );
TEST(res != 0, "NULL Destination Pointer");
res = strcat_s( dest, sizeof(dest), NULL );
TEST(res != 0, "NULL Source Pointer");

// Test case 4: Concatenation of empty source string
test_id++;
strcpy(dest, "Hello");
res = strcat_s( dest, sizeof(dest), "" );
TEST(res == 0, "Concatenation of empty source string");
TEST(strcmp(dest, "Hello") == 0, "Concatenation of empty source string Contents");

// Test case 5: Concatenation with empty destination string
test_id++;
char dest2[50];
res = strcat_s( dest2, sizeof(dest2), ", World!" );
TEST(res == 0, "Concatenation of non-empty source to empty destination");
TEST(strcmp(dest2, ", World!") == 0, "Concatenation of non-empty source to empty destination Contents");

printf("All strcat_s tests passed!\n");
return 0;
}
Loading

0 comments on commit ed68028

Please sign in to comment.