• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *    Copyright (c) 2017, The OpenThread Authors.
3  *    All rights reserved.
4  *
5  *    Redistribution and use in source and binary forms, with or without
6  *    modification, are permitted provided that the following conditions are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *    3. Neither the name of the copyright holder nor the
13  *       names of its contributors may be used to endorse or promote products
14  *       derived from this software without specific prior written permission.
15  *
16  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *    POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #define OTBR_LOG_TAG "TEST"
30 
31 #include <CppUTest/TestHarness.h>
32 
33 #include <stdio.h>
34 #include <time.h>
35 #include <unistd.h>
36 
37 #include "common/logging.hpp"
38 
TEST_GROUP(Logging)39 TEST_GROUP(Logging){};
40 
TEST(Logging,TestLoggingHigherLevel)41 TEST(Logging, TestLoggingHigherLevel)
42 {
43     char ident[20];
44 
45     snprintf(ident, sizeof(ident), "otbr-test-%ld", clock());
46     otbrLogInit(ident, OTBR_LOG_INFO, true, false);
47     otbrLog(OTBR_LOG_DEBUG, OTBR_LOG_TAG, "cool-higher");
48     otbrLogDeinit();
49     sleep(0);
50 
51     char cmd[128];
52     snprintf(cmd, sizeof(cmd), "grep '%s.*cool-higher' /var/log/syslog", ident);
53     CHECK(0 != system(cmd));
54 }
55 
TEST(Logging,TestLoggingEqualLevel)56 TEST(Logging, TestLoggingEqualLevel)
57 {
58     char ident[20];
59 
60     snprintf(ident, sizeof(ident), "otbr-test-%ld", clock());
61     otbrLogInit(ident, OTBR_LOG_INFO, true, false);
62     otbrLog(OTBR_LOG_INFO, OTBR_LOG_TAG, "cool-equal");
63     otbrLogDeinit();
64     sleep(0);
65 
66     char cmd[128];
67     snprintf(cmd, sizeof(cmd), "grep '%s.*cool-equal' /var/log/syslog", ident);
68     printf("CMD = %s\n", cmd);
69     CHECK(0 == system(cmd));
70 }
71 
TEST(Logging,TestLoggingEqualLevelNoSyslog)72 TEST(Logging, TestLoggingEqualLevelNoSyslog)
73 {
74     char ident[20];
75 
76     snprintf(ident, sizeof(ident), "otbr-test-%ld", clock());
77     otbrLogInit(ident, OTBR_LOG_INFO, true, true);
78     otbrLog(OTBR_LOG_INFO, OTBR_LOG_TAG, "cool-equal");
79     otbrLogDeinit();
80     sleep(0);
81 
82     char cmd[128];
83     snprintf(cmd, sizeof(cmd), "grep '%s.*cool-equal' /var/log/syslog", ident);
84     printf("CMD = %s\n", cmd);
85     CHECK(0 != system(cmd));
86 }
87 
TEST(Logging,TestLoggingLowerLevel)88 TEST(Logging, TestLoggingLowerLevel)
89 {
90     char ident[20];
91     char cmd[128];
92 
93     snprintf(ident, sizeof(ident), "otbr-test-%ld", clock());
94     otbrLogInit(ident, OTBR_LOG_INFO, true, false);
95     otbrLog(OTBR_LOG_WARNING, OTBR_LOG_TAG, "cool-lower");
96     otbrLogDeinit();
97     sleep(0);
98 
99     snprintf(cmd, sizeof(cmd), "grep '%s.*cool-lower' /var/log/syslog", ident);
100     CHECK(0 == system(cmd));
101 }
102 
TEST(Logging,TestLoggingDump)103 TEST(Logging, TestLoggingDump)
104 {
105     char ident[32];
106     char cmd[128];
107 
108     snprintf(ident, sizeof(ident), "otbr-test-%ld", clock());
109     otbrLogInit(ident, OTBR_LOG_DEBUG, true, false);
110     const char s[] = "one super long string with lots of text";
111     otbrDump(OTBR_LOG_INFO, "Test", "foobar", s, sizeof(s));
112     otbrLogDeinit();
113     sleep(0);
114 
115     /*
116      * Above produces output like this
117      * otbr-test-5976[47088]: foobar: 0000: 6f 6e 65 20 73 75 70 65 72 20 6c 6f 6e 67 20 73
118      * otbr-test-5976[47088]: foobar: 0010: 74 72 69 6e 67 20 77 69 74 68 20 6c 6f 74 73 20
119      * otbr-test-5976[47088]: foobar: 0020: 6f 66 20 74 65 78 74 00
120      */
121 
122     snprintf(cmd, sizeof(cmd),
123              "grep '%s.*: foobar: 0000: 6f 6e 65 20 73 75 70 65 72 20 6c 6f 6e 67 20 73' /var/log/syslog", ident);
124     CHECK(0 == system(cmd));
125 
126     snprintf(cmd, sizeof(cmd),
127              "grep '%s.*: foobar: 0010: 74 72 69 6e 67 20 77 69 74 68 20 6c 6f 74 73 20' /var/log/syslog", ident);
128     CHECK(0 == system(cmd));
129 
130     snprintf(cmd, sizeof(cmd), "grep '%s.*: foobar: 0020: 6f 66 20 74 65 78 74 00' /var/log/syslog", ident);
131     CHECK(0 == system(cmd));
132 }
133