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