SaMaEL
unknown
c_cpp
2 years ago
7.7 kB
5
Indexable
/** * @file ut_syslog.c * @copyright Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved. * @brief testing system log functions * @details test syslog functions and sub-functions * @author f84231664 * @date 2022-02-08 * @version v0.0.1 * ******************************************************************************************* * @par 修改日志: * <table> * <tr><th>Date <th>Version <th>Author <th>Description * <tr><td>2022-02-08 <td>0.0.1 <td>f84231664 <td>Initial Version * </table> * ******************************************************************************************* */ #include <unistd.h> #include "dtest.h" #include "syslog.h" #include <resolv.h> #define STR_TERMINATOR '\0' void CallVsyslogChk(int priority, int flag, const char *format, ...) { va_list ap; va_start(ap, format); __vsyslog_chk(priority, flag, format, ap); va_end(ap); } /** * @brief Testing if flags and name of the file works * @details * 1. Open a log socket with a given name and flags. * 2. Call syslog function with the given message. * 3. Close log socket. * @attention */ void UT_SysLog_Test001(void) { openlog("exampleprog-1", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog(LOG_NOTICE, "Test Notice Output-%d", getuid()); syslog(LOG_INFO, "Test Info Output"); syslog(LOG_ERR, "Test Err Output"); closelog(); } /** * @brief Testing if perror flag works * @details * 1. Open a log socket with a name and flags. * 2. Call syslog function with a message, it should output the given message to stderr. * 3. Close log socket. * @attention */ void UT_SysLog_Test002(void) { openlog("exampleprog-2", LOG_CONS | LOG_PID | LOG_PERROR, LOG_LOCAL1); syslog(LOG_NOTICE, "Test Notice Console-%d", getuid()); syslog(LOG_INFO, "Test Info Console"); syslog(LOG_ERR, "Test Err Console"); closelog(); } /** * @brief Checking if __syslog_chk function outputs given msg or not. * @details * 1. Prepare environment by redirecting STDERR to a file and opening log. * 2. Calling __syslog_chk function. * 3. Checking if the file includes given message. * 4. Closing the files and reverting redirection process. * @attention */ void UT_SysLog_Test003(void) { int fd; int savedFd = dup(STDERR_FILENO); /* store stderr so that we can revert the process */ char buf[128]; FILE* file = fopen("syslog_test.txt", "w+"); const char *msg = "__syslog_chk function is called"; const char *header = "syslog_chk"; const char *verifyMsg = "syslog_chk: __syslog_chk function is called"; const int strSize = strlen(verifyMsg); buf[strSize] = STR_TERMINATOR; /* open log socket */ openlog(header, LOG_CONS | LOG_PERROR, LOG_LOCAL1); /* get the file no and redirect stderr to this file */ fd = fileno(file); dup2(fd, STDERR_FILENO); /* call syslog_chk function, read the file and check if it is written correctly */ __syslog_chk(LOG_NOTICE, NULL, msg); fseek(file, 0L, SEEK_SET); fread(buf, sizeof(char), strSize, file); printf("buf: %s\n", buf); DT_ASSERT_STRING_EQUAL(buf, verifyMsg); /* restore stderr and close the file */ dup2(savedFd, STDERR_FILENO); remove("syslog_test.txt"); fclose(file); closelog(); } /** * @brief Checking if __vsyslog_chk function outputs given msg or not. * @details * 1. Prepare environment by redirecting STDERR to a file and opening log. * 2. Calling __vsyslog_chk function. * 3. Checking if the file includes given message. * 4. Closing the files and reverting redirection process. * @attention */ void UT_SysLog_Test004(void) { int fd; int savedFd = dup(STDERR_FILENO); /* store stderr so that we can revert the process */ char buf[128]; FILE* file = fopen("syslog_test.txt", "w+"); const char *msg = "__vsyslog_chk function is called"; const char *header = "vsyslog_chk"; const char *verifyMsg = "vsyslog_chk: __vsyslog_chk function is called"; const int strSize = strlen(verifyMsg); buf[strSize] = STR_TERMINATOR; /* open log socket */ openlog(header, LOG_CONS | LOG_PERROR, LOG_LOCAL1); /* get the file no and redirect stderr to this file */ fd = fileno(file); dup2(fd, STDERR_FILENO); /* call syslog_chk function, read the file and check if it is written correctly */ CallVsyslogChk(LOG_NOTICE, NULL, msg); fseek(file, 0L, SEEK_SET); fread(buf, sizeof(char), strSize, file); printf("buf: %s\n", buf); DT_ASSERT_STRING_EQUAL(buf, verifyMsg); /* restore stderr and close the file */ dup2(savedFd, STDERR_FILENO); remove("syslog_test.txt"); fclose(file); closelog(); } void getanswer(unsigned char *pos, unsigned char *end_res) { // Answer printf("answer name: %s\n", pos); pos+= dn_skipname(pos, end_res); short type; GETSHORT(type, pos); printf("answer type: %d\n", type); short class; GETSHORT(class, pos); printf("answer class: %d\n", class); int ttl; GETLONG(ttl, pos); printf("answer ttl: %d\n", class); short rdlen; GETSHORT(rdlen, pos); printf("answer rdlen: %d\n", rdlen); printf("answer rdata: %.*s\n", rdlen, pos); } typedef struct RR_header{ short type; short class; int ttl; short rdlen; } RR_header; void UT_SysLog_Test005(void) { union { HEADER header; unsigned char buf[512]; } res; char dest[1024]; int len = res_query("google.com", C_IN, T_A, res.buf, sizeof(dest)); printf("return: %d\n", len); printf("----------\nquestion\n"); printf("header id: %d\n", res.header.id); printf("qr: %d, opcode: %d, rcode: %d\n", res.header.qr, res.header.opcode, res.header.rcode); printf("header qdcount: %d\n", res.header.qdcount); printf("header ancount: %d\n", res.header.ancount); printf("header nscount: %d\n", res.header.nscount); printf("header arcount: %d\n", res.header.arcount); unsigned char *pos = res.buf + sizeof(res.header); // header skip unsigned char *end_res = res.buf + len; // Question printf("----------\nquestion\n"); printf("question data: %s\n", pos); pos += dn_skipname(pos, end_res); // + QFIXEDSZ; // Question skip short temp; GETSHORT(temp, pos); printf("question type: %d\n", temp); GETSHORT(temp, pos); printf("question class: %d\n", temp); printf("----------\nanswer\n"); printf("rr name: %s\n", pos); pos += dn_skipname(pos, end_res); //getanswer(pos, end_res); //getanswer(pos, end_res); RR_header *head = (RR_header*)pos; printf("rr type: %d\n", ntohs(head->type)); printf("rr class: %d\n", ntohs(head->class)); printf("rr ttl: %d\n", ntohs(head->ttl)); printf("rr rdlen: %d\n", ntohs(head->rdlen)); printf("rr rdata: %*s\n", 10, pos + 10); //getanswer(pos, end_res); //pos += 28; printf("----------\n"); //getanswer(pos, end_res); // Authority //pos+= dn_skipname(pos, end_res); printf("data: %s\n", pos); //char dest[1024]; //printf("return: %d\n", res_query("google.com", C_IN, T_A, dest, sizeof(dest))); //printf("dest: %s\n", dest); } void AddTest_SysLog(void) { DT_pSuite suite = DT_AddSuite(__func__, NULL, NULL); DT_ADD_TEST(suite, UT_SysLog_Test001, DT_TEST_PRIO_1); DT_ADD_TEST(suite, UT_SysLog_Test002, DT_TEST_PRIO_1); DT_ADD_TEST(suite, UT_SysLog_Test003, DT_TEST_PRIO_1); DT_ADD_TEST(suite, UT_SysLog_Test004, DT_TEST_PRIO_1); DT_ADD_TEST(suite, UT_SysLog_Test005, DT_TEST_PRIO_1); }
Editor is loading...