#define _POSIX_C_SOURCE 2
+#define _XOPEN_SOURCE
+#include <assert.h>
+#include <ctype.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <time.h>
-static void die(const char *message) {
- fputs(message, stderr);
- fputc('\n', stderr);
- exit(1);
-}
+#include "common.h"
+
+typedef struct expectation {
+ time_t a, b;
+ const char *message;
+} ex_t;
-static void die_err(const char *message) {
- perror(message);
- exit(1);
+const ex_t END = {((time_t)-1), ((time_t)-1), NULL};
+static int is_end(ex_t exp) {
+ return exp.a == END.a && exp.b == END.b && exp.message == END.message;
+}
+static ex_t expectation(time_t a, time_t b, const char *message) {
+ ex_t exp;
+ exp.a = a;
+ exp.b = b;
+ exp.message = message;
+ return exp;
}
-static void write_and_read_line() {
- char buf[10];
+static void remove_logfile() {
+ if (remove(FILENAME) != 0) {
+ if (errno != ENOENT) {
+ die_err("Error removing log file");
+ }
+ }
+}
+static ex_t write_to_tl_append(const char *content) {
FILE *p = popen("./tl-append", "w");
if (p == NULL)
die_err("Couldn't run tl-append");
- if (fputs("foo\n", p) == EOF)
+ time_t start = time(NULL);
+ if (fputs(content, p) == EOF)
die("Couldn't write to pipe");
int status = pclose(p);
+ time_t end = time(NULL);
if (status < 0)
die_err("Error closing pipe");
if (status != 0)
die("tl-append exited abnormally");
+ return expectation(start, end, content);
+}
- FILE *f = fopen("tl.log", "r");
+static void verify_line(const ex_t *ex, const char *line) {
+ if (strncmp(ex->message, line, strlen(ex->message) + 1) != 0)
+ die("Wrong contents in log file");
+}
+
+static void verify_log_contents(ex_t exps[]) {
+
+ FILE *f = fopen(FILENAME, "r");
if (f == NULL)
die_err("Error opening log file");
- if (fgets(buf, sizeof(buf), f) == NULL)
- die("Error reading log file");
- if (strncmp("foo\n", buf, sizeof(buf)) != 0)
- die("Wrong contents in log file");
+ for (size_t i = 0; !is_end(exps[i]); i++) {
+ size_t len = strlen(exps[i].message);
+ char *buf = (char *)malloc(len + 2);
+ if (fgets(buf, len + 1, f) == NULL)
+ die("Error reading log file");
+ if (ferror(f))
+ die("Error reading log file");
+ verify_line(&exps[i], buf);
+ free(buf);
+ }
if (fclose(f) != 0)
die_err("Error closing log file");
}
-int main() { write_and_read_line(); }
+static void test_encode_time() {
+ /* localtime_r to set tm's timezone */
+ time_t now_time = time(NULL);
+ struct tm tm;
+ if (localtime_r(&now_time, &tm) == NULL)
+ die_err("Can't unpack current time?");
+
+ const char *strptime_result = strptime("2011-12-13 14:15:16", "%F %T", &tm);
+ if (strptime_result == NULL || *strptime_result != '\0')
+ die("Couldn't parse time?");
+ time_t tt = mktime(&tm);
+ if (tt == (time_t)-1)
+ die_err("Can't pack time?");
+
+ const char *encoded = encode_time(tt);
+ /* Loose check to allow for daylight savings time changes between the current
+ * time and the target time. :( */
+ assert(encoded[0] == '2');
+ assert(encoded[1] == '0');
+ assert(encoded[2] == '1');
+ assert(encoded[3] == '1');
+ assert(encoded[4] == ' ');
+ assert(encoded[5] == '1');
+ assert(encoded[6] == '2');
+ assert(encoded[7] == ' ');
+ assert(encoded[8] == '1');
+ assert(encoded[9] == '3');
+ assert(encoded[10] == ' ');
+ assert(encoded[11] == '1');
+ assert(isdigit(encoded[12]));
+ assert(encoded[13] == ' ');
+ assert(isdigit(encoded[14]));
+ assert(isdigit(encoded[15]));
+ assert(encoded[16] == ' ');
+ assert(encoded[17] == '1');
+ assert(encoded[18] == '6');
+ assert(encoded[19] == '\0');
+}
+
+static void write_and_read_line() {
+ remove_logfile();
+ ex_t e = write_to_tl_append("foo\n");
+ verify_log_contents((ex_t[]){e, END});
+}
+
+static void write_and_read_two_lines() {
+ remove_logfile();
+ ex_t e1 = write_to_tl_append("foo\n");
+ ex_t e2 = write_to_tl_append("bar\n");
+ verify_log_contents((ex_t[]){e1, e2, END});
+}
+
+int main() {
+ test_encode_time();
+ write_and_read_line();
+ write_and_read_two_lines();
+}