Skip to content

Commit

Permalink
libc: add perror tests
Browse files Browse the repository at this point in the history
JIRA: CI-368
  • Loading branch information
KArkadiusz committed Apr 10, 2024
1 parent d8f54fa commit 91b4093
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions libc/string/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void runner(void)
RUN_TEST_GROUP(string_strlcpy);
RUN_TEST_GROUP(string_strlcat);
RUN_TEST_GROUP(string_errsign);
RUN_TEST_GROUP(string_perror);
RUN_TEST_GROUP(signal_psignal);
RUN_TEST_GROUP(string_cat);
RUN_TEST_GROUP(string_dup);
Expand Down
152 changes: 151 additions & 1 deletion libc/string/string_errsig.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@
* - strsignal()
*
* Copyright 2023 Phoenix Systems
* Author: Mateusz Bloch
* Author: Mateusz Bloch, Arkadiusz Kozlowski
*
* This file is part of Phoenix-RTOS.
*
* %LICENSE%
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <unity_fixture.h>

#include "testdata.h"

/* Typical error message does not exceed ~60 characters, that's why we expect a maximum value a little bit bigger */
#define MAX_LEN_STRING 100

Expand All @@ -48,6 +52,35 @@ static const unsigned int error_codes_len = sizeof(error_codes) / sizeof(error_c

const unsigned int signal_codes_len = sizeof(signal_codes) / sizeof(signal_codes[0]);


char *perrorToFile(const char *msg)
{
int errnoBefore = errno;

FILE *file = fopen("error.txt", "w+");

TEST_ASSERT_NOT_NULL(file);
TEST_ASSERT_NOT_EQUAL(-1, dup2(fileno(file), STDERR_FILENO));

errno = errnoBefore;
perror(msg);

fseek(file, 0, SEEK_END);
long fileSize = ftell(file);
rewind(file);

char *buffer = (char *)malloc(fileSize + 1);

fread(buffer, 1, fileSize, file);

Check warning on line 74 in libc/string/string_errsig.c

View workflow job for this annotation

GitHub Actions / call-ci / build (host-generic-pc)

ignoring return value of 'fread' declared with attribute 'warn_unused_result' [-Wunused-result]
buffer[fileSize] = '\0';

fclose(file);

remove("error.txt");
return buffer;
}


TEST_GROUP(string_errsign);


Expand Down Expand Up @@ -175,3 +208,120 @@ TEST_GROUP_RUNNER(string_errsign)
RUN_TEST_CASE(string_errsign, strsignal_basic);
RUN_TEST_CASE(string_errsign, strsignal_real_time);
}


TEST_GROUP(string_perror);


TEST_SETUP(string_perror)
{
errno = 0;
}


TEST_TEAR_DOWN(string_perror)
{
}

TEST(string_perror, perror_basic)
{
char msg[] = "Some error message";
char *res;
char exp[100];
sprintf(exp, "Some error message: %s\n", strerror(0));

res = perrorToFile(msg);

TEST_ASSERT_EQUAL_STRING(exp, res);
free(res);
}


TEST(string_perror, perror_empty_message)
{
TEST_IGNORE_MESSAGE("#929 issue");
char *res;
char exp[50];

errno = 31;
sprintf(exp, "%s\n", strerror(errno));
TEST_ASSERT_EQUAL_STRING(exp, res = perrorToFile(""));
free(res);
}


TEST(string_perror, perror_every_ascii)
{
char *msg = testdata_createCharStr(257);
char expected[356];
char *res;

errno = 8;
res = perrorToFile(msg);

sprintf(expected, "%s: %s\n", msg, strerror(errno));
TEST_ASSERT_EQUAL_STRING(expected, res);
free(res);
free(msg);
}


TEST(string_perror, perror_huge_argument)
{
const char *msg = testdata_hugeStr;
char *res;
char expected[testdata_hugeSize + 100];

errno = 42;
res = perrorToFile(msg);

sprintf(expected, "%s: %s\n", msg, strerror(errno));
TEST_ASSERT_EQUAL_STRING(expected, res);
free(res);
}


TEST(string_perror, perror_every_errno)
{
char *old_msg;
char *new_msg;

errno = 0;

for (int i = 0; i < 150; i++) {
errno = i;
old_msg = perrorToFile("Some msg");
errno++;
new_msg = perrorToFile("Some msg");
errno--;
TEST_ASSERT_NOT_EQUAL(0, strcmp(old_msg, new_msg));
free(new_msg);
free(old_msg);
}
}


TEST(string_perror, perror_null_argument)
{
TEST_IGNORE_MESSAGE("#929 issue");
char *msg;
msg = perrorToFile(NULL);
char exp[50];

strcpy(exp, strerror(errno));
strcat(exp, "\n");

TEST_ASSERT_EQUAL_STRING(exp, msg);
free(msg);
}


TEST_GROUP_RUNNER(string_perror)
{
RUN_TEST_CASE(string_perror, perror_basic);
RUN_TEST_CASE(string_perror, perror_empty_message);
RUN_TEST_CASE(string_perror, perror_every_ascii);
RUN_TEST_CASE(string_perror, perror_huge_argument);
RUN_TEST_CASE(string_perror, perror_every_errno);
RUN_TEST_CASE(string_perror, perror_null_argument);
}

0 comments on commit 91b4093

Please sign in to comment.