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