35b32c2d8f6d24a982d1ca83b0ce0608c8741b86
[platform/core/system/dlog.git] / src / tests / pipe_message.c
1 #include <stdio.h>
2 #include <assert.h>
3 #include <time.h>
4
5 #include <unistd.h>
6
7 #include <logcommon.h>
8 #include <queued_entry.h>
9
10 int time_cmp(struct timespec x, int sec, int nsec)
11 {
12         if (x.tv_sec  > sec)  return +1;
13         if (x.tv_sec  < sec)  return -1;
14         if (x.tv_nsec > nsec) return +1;
15         if (x.tv_nsec < nsec) return -1;
16         return 0;
17 }
18
19 int main()
20 {
21         int prio = 5;
22         char tag[] = "TEST_TAG";
23         char msg[] = "Test message with a newline \n and some special characters \e[31m in colour";
24         struct pipe_logger_entry *ple = alloca(LOG_MAX_PAYLOAD_SIZE + sizeof *ple);
25         dlogutil_entry_s *le = alloca(LOG_MAX_PAYLOAD_SIZE + sizeof *le);
26         struct timespec ts_pre, ts_post;
27
28         clock_gettime(CLOCK_MONOTONIC, &ts_pre);
29         create_pipe_message(ple, prio, tag, msg);
30         set_pipe_message_sent_timestamp(ple, NULL, NULL);
31         parse_pipe_message(ple, le, ple->len);
32         clock_gettime(CLOCK_MONOTONIC, &ts_post);
33
34         assert(le->pid == getpid());
35
36         pid_t tid = gettid();
37         if (tid != -1 && tid != 0) {
38                 /* Some build environments have trouble with gettid.
39                  * In those cases there's little point doing this check. */
40                 assert(le->tid == tid);
41         }
42
43         assert(le->priority == prio);
44         assert(!strcmp(le->msg, tag));
45         assert(!strcmp(le->msg + sizeof(tag), msg));
46         assert(time_cmp(ts_pre,  le->sec_sent_mono, le->nsec_sent_mono) <= 0);
47         assert(time_cmp(ts_post, le->sec_sent_mono, le->nsec_sent_mono) >= 0);
48
49         static const int SAFE_SIZE = ((LOG_MAX_PAYLOAD_SIZE - 1 /* prio */) / 2 /* split over tag and msg */) - 1 /* null delimiter */;
50         char random_tag[SAFE_SIZE];
51         char random_msg[SAFE_SIZE];
52         random_tag[SAFE_SIZE - 1] = random_msg[SAFE_SIZE - 1] = '\0';
53
54         int i = 100;
55         while (i--) {
56                 int j = SAFE_SIZE - 1;
57                 while (j--) {
58                         random_msg[j] = rand() & 255;
59                         random_tag[j] = rand() & 255;
60                 }
61                 prio = (rand() % (DLOG_PRIO_MAX - DLOG_VERBOSE)) + DLOG_VERBOSE;
62
63                 clock_gettime(CLOCK_MONOTONIC, &ts_pre);
64                 create_pipe_message(ple, prio, random_tag, random_msg);
65                 set_pipe_message_sent_timestamp(ple, NULL, NULL);
66                 parse_pipe_message(ple, le, ple->len);
67                 clock_gettime(CLOCK_MONOTONIC, &ts_post);
68                 assert(le->priority == prio);
69                 assert(!strcmp(le->msg, random_tag));
70                 assert(!strcmp(le->msg + strlen(random_tag) + 1, random_msg));
71                 assert(le->pid == getpid());
72                 assert(le->tid == gettid());
73                 assert(time_cmp(ts_pre,  le->sec_sent_mono, le->nsec_sent_mono) <= 0);
74                 assert(time_cmp(ts_post, le->sec_sent_mono, le->nsec_sent_mono) >= 0);
75         }
76
77         return 0;
78 }