Add cmake tests, fix a bug with parser expecting at least 1 arg, shorten code, add boolean for user assembly

This commit is contained in:
Alex 2020-07-09 22:37:29 +02:00
parent eb8263e0f5
commit 5869197ce7
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
5 changed files with 93 additions and 5 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ CMakeFiles
cmake_install.cmake
install_manifest.txt
Makefile
Testing
CTestTestfile.cmake

View File

@ -9,6 +9,8 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/build/archive)
enable_testing()
add_subdirectory(tests)
set(build_FILES src/uirc.c)
if ( USE_HELPERS )
message("Helper functions are going to be built.")

View File

@ -110,14 +110,13 @@ signed int Tok_mesg(char* str, IRC_Message* out)
}
int i;
for (i = 0; *progr != ':' && i < 14; i++) {
for (i = 0; *progr != ':' && i < 14 && *progr != '\0'; i++) {
if ((out->args[i] = strtok_r(NULL, " ", &progr)) == NULL)
return ERR_UIRC_INVALID_FORMAT;
}
out->args[i] = NULL;
if (*progr == ':')
++progr;
out->trailing = progr;
out->trailing = ++progr;
return 1;
}
signed int Assm_mesg(char* buf, IRC_Message* in)
@ -261,7 +260,7 @@ signed int Assm_user(char* buf, IRC_User* in, bool useorig)
else
return ERR_UIRC_BUFFER_ERR;
}
if (in->orig != NULL) {
if (useorig && in->orig != NULL) {
if ((cnt = sprintf(pos, "%%%s", in->orig)) > 0)
pos += cnt;
else
@ -282,7 +281,7 @@ signed int Tok_user(char* str, IRC_User* out, bool useorig)
char* pos = (*str == ':') ? str + 1 : str;
if ((out->host = strchr(pos, '@')) != NULL)
*((char*)out->host++) = '\0';
if ((out->orig = strchr(pos, '%')) != NULL)
if (useorig && (out->orig = strchr(pos, '%')) != NULL)
*((char*)out->orig++) = '\0';
if ((out->user = strchr(pos, '!')) != NULL) /* RFC2812 says this cannot be here without the host but RFC1459 says it can, we accept both options */
*((char*)out->user++) = '\0';

6
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.16)
add_executable(parsing parsing.c)
#add_executable(assembly assembly.c)
add_test(Parsing ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/parsing)
#add_test(Assembly ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assembly)
target_link_libraries(parsing uirc)

79
tests/parsing.c Normal file
View File

@ -0,0 +1,79 @@
#include "../include/uirc.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
int main(void)
{
IRC_Message results[] = {
{.name.nick = "user", .cmd = NOTICE, .trailing = "ok"},
{.cmd = QUIT},
{.name = {.nick = "nick", .user = "user", .host = "host"}, .cmd = PRIVMSG, .args[0] = "friend", .trailing = "hello there"}};
char mesg[][513] = {
":user NOTICE :ok",
"QUIT",
":nick!user@host PRIVMSG friend :hello there"};
IRC_Message parseout;
int res = 0;
for (unsigned long i = 0; i < sizeof(results) / sizeof(IRC_Message); i++) {
memset((void*)&parseout, '\0', sizeof(parseout));
printf("Testing [%lu] --> %s\n", i, mesg[i]);
printf("Tokenize: ");
if ((res = Tok_mesg(mesg[i], &parseout)) <= 0) {
printf("String could not be tokenized. %i\n", res);
return EXIT_FAILURE;
}
printf("OK\n");
const char* const strings[][2] = {
{parseout.trailing, results[i].trailing}, // 0
{parseout.name.nick, results[i].name.nick}, // 1
{parseout.name.host, results[i].name.host}, // 2
{parseout.name.user, results[i].name.user}, // 3
{parseout.tags.account.value, results[i].tags.account.value}, // 4
{parseout.tags.batch.value, results[i].tags.batch.value}, // 5
{parseout.tags.label.value, results[i].tags.label.value}, // 6
{parseout.tags.msgid.value, results[i].tags.msgid.value}, // 7
{parseout.tags.multiline_concat.value, results[i].tags.multiline_concat.value}, // 8
{parseout.tags.time.value, results[i].tags.time.value}, // 9
{parseout.tags.typing.value, results[i].tags.typing.value}, // 10
{parseout.tags.react.value, results[i].tags.react.value}, // 11
{parseout.tags.reply.value, results[i].tags.reply.value} // 12
};
printf("Validation:\n");
printf("\tStrings:\n");
for (unsigned long j = 0; j < sizeof(strings) / sizeof(char* [2]); j++) {
if (strings[j][0] != NULL && *strings[j][0] != '\0') {
printf("\t\t[%lu]: ", j);
if (strings[j][1] == NULL || *strings[j][1] == '\0') {
printf("Matching argument is a NULL pointer or zero-lenght: --> %lu\n", i);
return EXIT_FAILURE;
}
if (strcmp(strings[j][0], strings[j][1])) {
printf("Arguments didn't match at iteration %lu: expected %s but got %s", i, strings[j][1], strings[j][0]);
return EXIT_FAILURE;
}
printf("OK\n");
}
}
printf("\tCommand: ");
if (parseout.cmd == 0 || parseout.cmd != results[i].cmd) {
printf("Command is not parsed or non-matching. Got %s (%i)\n", uirc_ircmd[parseout.cmd], parseout.cmd);
return EXIT_FAILURE;
}
printf("OK\n");
printf("\tArguments:\n");
for (int k = 0; i < 14 && results[i].args[k] != NULL; k++) {
printf("\t\t[%i]: ", k);
if (parseout.args[k] == NULL) {
printf("Argument %i is NULL\n", k);
return EXIT_FAILURE;
}
if (strcmp(parseout.args[k], results[i].args[k])) {
printf("Argument %i does not match expected value. Expected %s, got %s instead\n", k, results[i].args[k], parseout.args[k]);
return EXIT_FAILURE;
}
printf("OK\n");
}
}
return EXIT_SUCCESS;
}