item: checking against overflows in title.
[agg.git] / tests_dev / tests.c
bloba003304590439b7f0e5947a56d09a497a9042bd2
1 /* agg - the news aggregator
2 * Copyright (C) 2011 Andreas Waidler <arandes@programmers.at>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 #include <stdlib.h>
17 #include <string.h>
18 #include <stdio.h>
20 #include "../src/config.h"
21 #include "../src/text.h"
22 #include "../src/layer.h"
23 #include "../src/fs.h"
24 #include "../src/stack.h"
26 #define TEST(fun) test(#fun, fun())
27 void test(const char* expr, int res);
29 int test_text_is_empty();
30 int test_text_enable();
31 int test_text_disable();
32 int test_text_write();
33 int test_text_clear();
34 int test_text_append();
35 int test_text_fill();
37 int test_stack_set_top();
38 int test_stack_next_prev();
39 int test_stack_set_next();
41 int test_set_item_title();
42 int test_get_item_title();
43 int test_item_sanitize_title();
44 int test_item_title_fill_minus_one();
45 int test_item_title_fill();
46 int test_item_title_fill_plus_one();
48 unsigned int failures = 0;
50 int main()
52 TEST(test_text_is_empty);
53 TEST(test_text_enable);
54 TEST(test_text_disable);
55 TEST(test_text_write);
56 TEST(test_text_clear);
57 TEST(test_text_append);
58 TEST(test_text_fill);
60 TEST(test_stack_set_top);
61 TEST(test_stack_next_prev);
62 TEST(test_stack_set_next);
64 TEST(test_set_item_title);
65 TEST(test_get_item_title);
66 TEST(test_item_sanitize_title);
67 TEST(test_item_title_fill_minus_one);
68 TEST(test_item_title_fill);
69 TEST(test_item_title_fill_plus_one);
71 return failures;
74 void test(const char* expr, int res)
76 printf(" %-32s: %s\n", expr, res ? "OK" : "FAIL");
77 if (!res) {
78 ++failures;
82 int test_text_is_empty()
84 return strcmp("", text_get()) == 0;
87 int test_text_enable()
89 text_enable();
90 return 1;
93 int test_text_disable()
95 text_disable();
96 return 1;
99 int test_text_write()
101 text_buffer("foobar", 3);
102 return strcmp("foo", text_get()) == 0;
105 int test_text_clear()
107 text_enable();
108 if (!test_text_is_empty()) return 0;
110 test_text_write();
111 text_disable();
112 return test_text_is_empty();
115 int test_text_append()
117 text_buffer("foobar", 3);
118 text_buffer("foobar", 3);
119 return strcmp("foofoo", text_get()) == 0;
122 int test_text_fill()
124 /* buffer must be >= text buffer. */
125 #define BUFSIZE (1024 * 1024)
126 char buf[BUFSIZE];
127 memset(buf, ' ', BUFSIZE);
128 text_buffer(buf, BUFSIZE);
130 return strncmp(buf, text_get(), BUFSIZE) != 0;
133 int test_stack_set_top()
135 struct Layer mock;
136 mock.enter = (void (*)(const char*, const char**)) 0x7E57AB1E;
137 mock.leave = (void (*)(const char*)) 0xCA11AB1E;
139 stack_set(0, mock);
140 return stack_top()->enter == mock.enter
141 && stack_top()->leave == mock.leave;
144 int test_stack_next_prev()
146 struct Layer mock;
147 mock.enter = (void (*)(const char*, const char**)) 0x7E57AB1E;
148 mock.leave = (void (*)(const char*)) 0xCA11AB1E;
150 /* Mock still set from method before. */
152 stack_next();
153 stack_prev();
155 return stack_top()->enter == mock.enter
156 && stack_top()->leave == mock.leave;
159 int test_stack_set_next()
161 struct Layer mock;
162 mock.enter = (void (*)(const char*, const char**)) 0x7E57AB1E;
163 mock.leave = (void (*)(const char*)) 0xCA11AB1E;
165 stack_set(1, mock);
166 stack_next();
167 return stack_top()->enter == mock.enter
168 && stack_top()->leave == mock.leave;
171 int test_set_item_title()
173 set_item_title("foo");
174 return 1;
177 int test_get_item_title()
179 return strcmp("foo", get_item_title()) == 0;
182 int test_item_sanitize_title()
184 set_item_title("../../foo");
185 return strcmp("..\\..\\foo", get_item_title()) == 0;
188 int test_item_title_fill_minus_one()
190 char exp[TEXT_BUFFER_SIZE] = { 0 };
191 char buf[TEXT_BUFFER_SIZE] = { 0 };
193 memset(exp, ' ', TEXT_BUFFER_SIZE - 2);
194 memset(buf, ' ', TEXT_BUFFER_SIZE - 2);
196 set_item_title(buf);
198 return strcmp(exp, get_item_title()) == 0;
201 int test_item_title_fill()
203 char exp[TEXT_BUFFER_SIZE] = { 0 };
204 char buf[TEXT_BUFFER_SIZE] = { 0 };
206 memset(exp, ' ', TEXT_BUFFER_SIZE - 1);
207 memset(buf, ' ', TEXT_BUFFER_SIZE - 1);
209 set_item_title(buf);
211 return strcmp(exp, get_item_title()) == 0;
214 int test_item_title_fill_plus_one()
216 char exp[TEXT_BUFFER_SIZE] = { 0 };
217 char buf[TEXT_BUFFER_SIZE + 1] = { 0 };
219 memset(exp, ' ', TEXT_BUFFER_SIZE - 1);
220 memset(buf, ' ', TEXT_BUFFER_SIZE);
222 exp[TEXT_BUFFER_SIZE - 4] = '.';
223 exp[TEXT_BUFFER_SIZE - 3] = '.';
224 exp[TEXT_BUFFER_SIZE - 2] = '.';
226 set_item_title(buf);
228 return strcmp(exp, get_item_title()) == 0;