]>
Commit | Line | Data |
---|---|---|
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 | const size_t TIMESTAMP_LEN = 19; | |
14 | ||
15 | typedef struct expectation { | |
16 | time_t a, b; | |
17 | const char *message; | |
18 | } ex_t; | |
19 | ||
20 | const ex_t END = {((time_t)-1), ((time_t)-1), NULL}; | |
21 | static int is_end(ex_t exp) { | |
22 | return exp.a == END.a && exp.b == END.b && exp.message == END.message; | |
23 | } | |
24 | static ex_t expectation(time_t a, time_t b, const char *message) { | |
25 | ex_t exp; | |
26 | exp.a = a; | |
27 | exp.b = b; | |
28 | exp.message = message; | |
29 | return exp; | |
30 | } | |
31 | ||
32 | static void remove_logfile() { | |
33 | if (remove(FILENAME) != 0) { | |
34 | if (errno != ENOENT) { | |
35 | die_err("Error removing log file"); | |
36 | } | |
37 | } | |
38 | } | |
39 | ||
40 | static ex_t write_to_tl_append(const char *content) { | |
41 | FILE *p = popen("./tl-append", "w"); | |
42 | if (p == NULL) | |
43 | die_err("Couldn't run tl-append"); | |
44 | time_t start = time(NULL); | |
45 | if (fputs(content, p) == EOF) | |
46 | die("Couldn't write to pipe"); | |
47 | int status = pclose(p); | |
48 | time_t end = time(NULL); | |
49 | if (status < 0) | |
50 | die_err("Error closing pipe"); | |
51 | if (status != 0) | |
52 | die("tl-append exited abnormally"); | |
53 | return expectation(start, end, content); | |
54 | } | |
55 | ||
56 | static void verify_timestamp(const ex_t *ex, const char *line) { | |
57 | struct tm tm; | |
58 | ||
59 | /* localtime_r to set tm's timezone */ | |
60 | time_t now_time = time(NULL); | |
61 | if (localtime_r(&now_time, &tm) == NULL) | |
62 | die_err("Can't unpack current time?"); | |
63 | ||
64 | const char *strptime_result = strptime(line, "%Y %m %d %H %M %S", &tm); | |
65 | if (strptime_result == NULL || strptime_result != &line[TIMESTAMP_LEN]) | |
66 | die("Wrong contents in log file: Couldn't parse timestamp"); | |
67 | time_t t = mktime(&tm); | |
68 | int t_in_range = ex->a <= t && t <= ex->b; | |
69 | if (!t_in_range) | |
70 | die("Wrong contents in log file: Wrong time"); | |
71 | } | |
72 | ||
73 | static void verify_line(const ex_t *ex, const char *line) { | |
74 | size_t line_len = strlen(line); | |
75 | if (line_len < TIMESTAMP_LEN + 1) | |
76 | die("Wrong contents in log file: Line too short"); | |
77 | verify_timestamp(ex, line); | |
78 | if (line[TIMESTAMP_LEN] != ' ') | |
79 | die("Wrong contents in log file: Bad format"); | |
80 | if (strncmp(ex->message, &line[TIMESTAMP_LEN + 1], strlen(ex->message) + 1) != | |
81 | 0) | |
82 | die("Wrong contents in log file"); | |
83 | } | |
84 | ||
85 | static void verify_log_contents(ex_t exps[]) { | |
86 | ||
87 | FILE *f = fopen(FILENAME, "r"); | |
88 | if (f == NULL) | |
89 | die_err("Error opening log file"); | |
90 | for (size_t i = 0; !is_end(exps[i]); i++) { | |
91 | size_t len = TIMESTAMP_LEN + 1 + strlen(exps[i].message); | |
92 | char *buf = (char *)malloc(len + 2); | |
93 | if (fgets(buf, len + 1, f) == NULL) | |
94 | die("Error reading log file"); | |
95 | if (ferror(f)) | |
96 | die("Error reading log file"); | |
97 | verify_line(&exps[i], buf); | |
98 | free(buf); | |
99 | } | |
100 | if (fclose(f) != 0) | |
101 | die_err("Error closing log file"); | |
102 | } | |
103 | ||
104 | static void test_encode_time() { | |
105 | /* localtime_r to set tm's timezone */ | |
106 | time_t now_time = time(NULL); | |
107 | struct tm tm; | |
108 | if (localtime_r(&now_time, &tm) == NULL) | |
109 | die_err("Can't unpack current time?"); | |
110 | ||
111 | const char *strptime_result = strptime("2011-12-13 14:15:16", "%F %T", &tm); | |
112 | if (strptime_result == NULL || *strptime_result != '\0') | |
113 | die("Couldn't parse time?"); | |
114 | time_t tt = mktime(&tm); | |
115 | if (tt == (time_t)-1) | |
116 | die_err("Can't pack time?"); | |
117 | ||
118 | const char *encoded = encode_time(tt); | |
119 | /* Loose check to allow for daylight savings time changes between the current | |
120 | * time and the target time. :( */ | |
121 | assert(encoded[0] == '2'); | |
122 | assert(encoded[1] == '0'); | |
123 | assert(encoded[2] == '1'); | |
124 | assert(encoded[3] == '1'); | |
125 | assert(encoded[4] == ' '); | |
126 | assert(encoded[5] == '1'); | |
127 | assert(encoded[6] == '2'); | |
128 | assert(encoded[7] == ' '); | |
129 | assert(encoded[8] == '1'); | |
130 | assert(encoded[9] == '3'); | |
131 | assert(encoded[10] == ' '); | |
132 | assert(encoded[11] == '1'); | |
133 | assert(isdigit(encoded[12])); | |
134 | assert(encoded[13] == ' '); | |
135 | assert(isdigit(encoded[14])); | |
136 | assert(isdigit(encoded[15])); | |
137 | assert(encoded[16] == ' '); | |
138 | assert(encoded[17] == '1'); | |
139 | assert(encoded[18] == '6'); | |
140 | assert(encoded[19] == '\0'); | |
141 | } | |
142 | ||
143 | static void write_and_read_line() { | |
144 | remove_logfile(); | |
145 | ex_t e = write_to_tl_append("foo\n"); | |
146 | verify_log_contents((ex_t[]){e, END}); | |
147 | } | |
148 | ||
149 | static void write_and_read_two_lines() { | |
150 | remove_logfile(); | |
151 | ex_t e1 = write_to_tl_append("foo\n"); | |
152 | ex_t e2 = write_to_tl_append("bar\n"); | |
153 | verify_log_contents((ex_t[]){e1, e2, END}); | |
154 | } | |
155 | ||
156 | int main() { | |
157 | test_encode_time(); | |
158 | write_and_read_line(); | |
159 | write_and_read_two_lines(); | |
160 | } |