1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 
5 #include <pthread.h>
6 
7 #include <alarm.h>
8 #include "test_common.h"
9 
10 static alarm_queue_t *alarmqueue = NULL;
11 
handle_setup(void)12 static void handle_setup(void) {
13     alarmqueue = init_alarmqueue();
14     fail_if(alarmqueue == NULL, "Failed to set up alarm queue");
15 }
16 
handle_teardown(void)17 static void handle_teardown(void) {
18     destroy_alarmqueue(alarmqueue);
19     alarmqueue = NULL;
20 }
21 
test_simple_alarm(void)22 static void test_simple_alarm(void)
23 {
24     alarm_t in_alarm;
25 
26     fail_unless(add_alarm(alarmqueue, "test", "This is a test message 1"),
27                 "Failed to alarm.");
28     in_alarm = get_alarm(alarmqueue);
29     fail_unless(in_alarm.open == 1, "Didn't receive alarm message 1.");
30     fail_unless(strcmp(in_alarm.name, "test") == 0, "Alarm name didn't match");
31     fail_unless(strcmp(in_alarm.msg, "This is a test message 1") == 0,
32                 "Didn't get the right message for message 1.");
33 
34     fail_unless(add_alarm(alarmqueue, "test2", "This is a test message 2"),
35                 "Failed to alarm.");
36     fail_unless(add_alarm(alarmqueue, "test3", "This is a test message 3"),
37                 "Failed to alarm.");
38     in_alarm = get_alarm(alarmqueue);
39     fail_unless(in_alarm.open == 1, "Didn't receive alarm message 2.");
40     fail_unless(strcmp(in_alarm.name, "test2") == 0, "test2 didn't match");
41     fail_unless(strcmp(in_alarm.msg, "This is a test message 2") == 0,
42                 "Didn't get the right message for message 2.");
43 
44     in_alarm = get_alarm(alarmqueue);
45     fail_unless(in_alarm.open == 1, "Didn't receive alarm message 1.");
46     fail_unless(strcmp(in_alarm.name, "test3") == 0, "test3 didn't match");
47     fail_unless(strcmp(in_alarm.msg, "This is a test message 3") == 0,
48                 "Didn't get the right message for message 3.");
49     in_alarm = get_alarm(alarmqueue);
50     fail_unless(in_alarm.open == 0, "Shouldn't have recieved open alarm.");
51 }
52 
test_giant_alarm(void)53 static void test_giant_alarm(void)
54 {
55     char *msg="This is a really large message that exceeds the 256 or "
56         "so bytes allocated for messages to see what happens when a "
57         "message is too big.  Hopefully it's fine.  "
58         "It turns out that 256 bytes or whatever the current value is ends "
59         "up being quite a bit of bytes.  I suppose that makes sense if "
60         "I do a bit of math, but I'm too lazy to do anything other than assert.";
61 
62     fail_unless(strlen(msg) > ALARM_MSG_MAXLEN,
63                 "Message string was too short to blow up.");
64     fail_unless(add_alarm(alarmqueue, "bigass", msg), "Failed to alarm.");
65 
66     alarm_t in_alarm = get_alarm(alarmqueue);
67     fail_unless(in_alarm.open, "Didn't receive a large alarm.");
68 
69     fail_unless(strlen(in_alarm.msg) == ALARM_MSG_MAXLEN,
70             "Alarm message is too long.");
71 
72     fail_unless(strcmp("bigass", in_alarm.name) == 0,
73                 "Alarm name didn't match.");
74     fail_unless(strncmp(msg, in_alarm.msg, ALARM_MSG_MAXLEN) == 0,
75                 "Alarm message didn't match.");
76 }
77 
test_giant_name(void)78 static void test_giant_name(void)
79 {
80     const char *name = "this name should exceed the max length";
81     fail_unless(strlen(name) > ALARM_NAME_MAXLEN,
82                 "Name wasn't too big enough.");
83 
84     fail_unless(add_alarm(alarmqueue, name, "some message"),
85                 "Failed to alarm.");
86 
87     alarm_t in_alarm = get_alarm(alarmqueue);
88     fail_unless(in_alarm.open, "Didn't receive an alarm.");
89 
90     fail_unless(strlen(in_alarm.name) == ALARM_NAME_MAXLEN,
91                 "Alarm name is too long.");
92     fail_unless(strncmp(name, in_alarm.name, ALARM_NAME_MAXLEN) == 0,
93                 "Alarm name didn't match.");
94     fail_unless(strcmp("some message", in_alarm.msg) == 0,
95                 "Alarm message didn't match.");
96 }
97 
test_full_queue(void)98 static void test_full_queue(void)
99 {
100     for (int i = 0; i < ALARM_QUEUE_SIZE; i++) {
101         fail_unless(add_alarm(alarmqueue, "add", "Test alarm message."),
102                     "Failed to add alarm.");
103     }
104     fail_if(add_alarm(alarmqueue, "fail", "Test failing alarm."),
105             "Should have failed to add another alarm.");
106 }
107 
main(void)108 int main(void)
109 {
110     typedef void (*testcase)(void);
111     testcase tc[] = {
112         test_simple_alarm,
113         test_giant_alarm,
114         test_giant_name,
115         test_full_queue,
116         NULL
117     };
118 
119     int ii = 0;
120 
121     while (tc[ii] != 0) {
122         handle_setup();
123         tc[ii++]();
124         handle_teardown();
125     }
126 
127     return EXIT_SUCCESS;
128 }
129