• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 /**************************************************************************************************
3  * IOWOW library
4  *
5  * MIT License
6  *
7  * Copyright (c) 2012-2020 Softmotions Ltd <info@softmotions.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  *  copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in all
17  * copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *************************************************************************************************/
27 
28 
29 #include "iwcfg.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <CUnit/Basic.h>
34 
35 #include "iwlog.h"
36 #include "iwutils.h"
37 
init_suite(void)38 int init_suite(void) {
39   int rc = iwlog_init();
40   return rc;
41 }
42 
clean_suite(void)43 int clean_suite(void) {
44   return 0;
45 }
46 
iwlog_test1()47 void iwlog_test1() {
48   uint32_t ec = (0xfffffffdU & 0x3fffffffU);
49   uint64_t rc = 0xfafafafaULL;
50   rc = iwrc_set_errno(rc, ec);
51   uint32_t ec2 = iwrc_strip_errno(&rc);
52   CU_ASSERT_EQUAL(ec, ec2);
53   CU_ASSERT_EQUAL(rc, 0xfafafafaULL);
54 }
55 
iwlog_test2()56 void iwlog_test2() {
57   IWLOG_DEFAULT_OPTS opts = {0};
58   int rv = 0;
59   size_t sz;
60   char fname[] = "iwlog_test1_XXXXXX";
61   int fd = mkstemp(fname);
62   CU_ASSERT_TRUE(fd != 1);
63   FILE *out = fdopen(fd, "w");
64   CU_ASSERT_PTR_NOT_NULL(out);
65 
66   fprintf(stderr, "Redirecting log to: %s" IW_LINE_SEP, fname);
67 
68   opts.out = out;
69   iwlog_set_logfn_opts(&opts);
70 
71   iwlog_info2("7fa79c75beac413d83f35ffb6bf571b9");
72   iwlog_error("7e94f7214af64513b30ab4df3f62714a%s", "C");
73   iwlog_ecode_warn(IW_ERROR_READONLY, "c94645c3b107433497ef295b1c00dcff%d", 12);
74 
75   errno = ENOENT;
76   iwrc ecode = iwrc_set_errno(IW_ERROR_ERRNO, errno);
77   rv = iwlog(IWLOG_DEBUG, ecode, NULL, 0, "ERRNO Message");
78   CU_ASSERT_EQUAL(rv, 0);
79   errno = 0;
80   fclose(out);
81 
82   out = fopen(fname, "r");
83   CU_ASSERT_PTR_NOT_NULL_FATAL(out);
84 
85   char buf[1024];
86   memset(buf, 0, 1024);
87   sz = fread(buf, 1, 1024, out);
88   CU_ASSERT_TRUE(sz);
89   fprintf(stderr, "%s\n\n" IW_LINE_SEP, buf);
90 
91   CU_ASSERT_PTR_NOT_NULL(strstr(buf, "7fa79c75beac413d83f35ffb6bf571b9"));
92   CU_ASSERT_PTR_NOT_NULL(strstr(buf, "7e94f7214af64513b30ab4df3f62714aC"));
93   CU_ASSERT_PTR_NOT_NULL(strstr(buf,
94                                 "DEBUG 70001|2|0|Error with expected errno "
95                                 "status set. (IW_ERROR_ERRNO)|"));
96   CU_ASSERT_PTR_NOT_NULL(strstr(buf, "ERRNO Message"));
97   CU_ASSERT_PTR_NOT_NULL(strstr(buf, "ERROR iwlog_test1.c:"));
98   CU_ASSERT_PTR_NOT_NULL(strstr(buf, "70004|0|0|Resource is readonly. (IW_ERROR_READONLY)|"));
99   CU_ASSERT_PTR_NOT_NULL(strstr(buf, "c94645c3b107433497ef295b1c00dcff12"));
100 
101   fclose(out);
102   unlink(fname);
103 }
104 
main()105 int main() {
106   CU_pSuite pSuite = NULL;
107 
108   /* Initialize the CUnit test registry */
109   if (CUE_SUCCESS != CU_initialize_registry())
110     return CU_get_error();
111 
112   /* Add a suite to the registry */
113   pSuite = CU_add_suite("iwlog_test1", init_suite, clean_suite);
114 
115   if (NULL == pSuite) {
116     CU_cleanup_registry();
117     return CU_get_error();
118   }
119 
120   /* Add the tests to the suite */
121   if ((NULL == CU_add_test(pSuite, "iwlog_test1", iwlog_test1)) ||
122       (NULL == CU_add_test(pSuite, "iwlog_test2", iwlog_test2))) {
123     CU_cleanup_registry();
124     return CU_get_error();
125   }
126 
127   /* Run all tests using the CUnit Basic interface */
128   CU_basic_set_mode(CU_BRM_VERBOSE);
129   CU_basic_run_tests();
130   int ret = CU_get_error() || CU_get_number_of_failures();
131   CU_cleanup_registry();
132   return ret;
133 }
134