]> git.scottworley.com Git - tl-append/blob - tl-append-test.c
Encode time in time-logger's format
[tl-append] / tl-append-test.c
1 #define _POSIX_C_SOURCE 2
2 #define _XOPEN_SOURCE
3 #include <assert.h>
4 #include <ctype.h>
5 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <time.h>
10
11 #include "common.h"
12
13 typedef struct expectation {
14 time_t a, b;
15 const char *message;
16 } ex_t;
17
18 const ex_t END = {((time_t)-1), ((time_t)-1), NULL};
19 static int is_end(ex_t exp) {
20 return exp.a == END.a && exp.b == END.b && exp.message == END.message;
21 }
22 static ex_t expectation(time_t a, time_t b, const char *message) {
23 ex_t exp;
24 exp.a = a;
25 exp.b = b;
26 exp.message = message;
27 return exp;
28 }
29
30 static void remove_logfile() {
31 if (remove(FILENAME) != 0) {
32 if (errno != ENOENT) {
33 die_err("Error removing log file");
34 }
35 }
36 }
37
38 static ex_t write_to_tl_append(const char *content) {
39 FILE *p = popen("./tl-append", "w");
40 if (p == NULL)
41 die_err("Couldn't run tl-append");
42 time_t start = time(NULL);
43 if (fputs(content, p) == EOF)
44 die("Couldn't write to pipe");
45 int status = pclose(p);
46 time_t end = time(NULL);
47 if (status < 0)
48 die_err("Error closing pipe");
49 if (status != 0)
50 die("tl-append exited abnormally");
51 return expectation(start, end, content);
52 }
53
54 static void verify_log_contents(ex_t exps[]) {
55
56 FILE *f = fopen(FILENAME, "r");
57 if (f == NULL)
58 die_err("Error opening log file");
59 for (size_t i = 0; !is_end(exps[i]); i++) {
60 size_t len = strlen(exps[i].message);
61 char *buf = (char *)malloc(len + 2);
62 if (fgets(buf, len + 1, f) == NULL)
63 die("Error reading log file");
64 if (ferror(f))
65 die("Error reading log file");
66 if (strncmp(exps[i].message, buf, len + 1) != 0)
67 die("Wrong contents in log file");
68 free(buf);
69 }
70 if (fclose(f) != 0)
71 die_err("Error closing log file");
72 }
73
74 static void test_encode_time() {
75 /* localtime_r to set tm's timezone */
76 time_t now_time = time(NULL);
77 struct tm tm;
78 if (localtime_r(&now_time, &tm) == NULL)
79 die_err("Can't unpack current time?");
80
81 const char *strptime_result = strptime("2011-12-13 14:15:16", "%F %T", &tm);
82 if (strptime_result == NULL || *strptime_result != '\0')
83 die("Couldn't parse time?");
84 time_t tt = mktime(&tm);
85 if (tt == (time_t)-1)
86 die_err("Can't pack time?");
87
88 const char *encoded = encode_time(tt);
89 /* Loose check to allow for daylight savings time changes between the current
90 * time and the target time. :( */
91 assert(encoded[0] == '2');
92 assert(encoded[1] == '0');
93 assert(encoded[2] == '1');
94 assert(encoded[3] == '1');
95 assert(encoded[4] == ' ');
96 assert(encoded[5] == '1');
97 assert(encoded[6] == '2');
98 assert(encoded[7] == ' ');
99 assert(encoded[8] == '1');
100 assert(encoded[9] == '3');
101 assert(encoded[10] == ' ');
102 assert(encoded[11] == '1');
103 assert(isdigit(encoded[12]));
104 assert(encoded[13] == ' ');
105 assert(isdigit(encoded[14]));
106 assert(isdigit(encoded[15]));
107 assert(encoded[16] == ' ');
108 assert(encoded[17] == '1');
109 assert(encoded[18] == '6');
110 assert(encoded[19] == '\0');
111 }
112
113 static void write_and_read_line() {
114 remove_logfile();
115 ex_t e = write_to_tl_append("foo\n");
116 verify_log_contents((ex_t[]){e, END});
117 }
118
119 static void write_and_read_two_lines() {
120 remove_logfile();
121 ex_t e1 = write_to_tl_append("foo\n");
122 ex_t e2 = write_to_tl_append("bar\n");
123 verify_log_contents((ex_t[]){e1, e2, END});
124 }
125
126 int main() {
127 test_encode_time();
128 write_and_read_line();
129 write_and_read_two_lines();
130 }