summaryrefslogtreecommitdiff
path: root/test/messages.c
blob: bc6daea1da093b35fbd6ca089c2e230fd7a76bbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * Copyright 2016 Vincent Sanders <vince@netsurf-browser.org>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * \file
 * Tests for message processing
 */

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <check.h>

#include "utils/errors.h"
#include "utils/log.h"
#include "utils/messages.h"

#include "test/message_data_inline.h"

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

const char *test_messages_path = "test/data/Messages";

struct message_test_vec_s {
	int test;
	char *res;
};

struct message_test_vec_s message_errorcode_test_vec[] = {
	{ NSERROR_OK, "OK" },
	{ NSERROR_NOMEM, "NetSurf is running out of memory. Please free some memory and try again." },
	{ NSERROR_NO_FETCH_HANDLER, "NoHandler" },
	{ NSERROR_NOT_FOUND, "Not found" },
	{ NSERROR_SAVE_FAILED, "SaveFailed" },
	{ NSERROR_CLONE_FAILED, "CloneFailed" },
	{ NSERROR_INIT_FAILED, "InitFailed" },
	{ NSERROR_MNG_ERROR, "Error converting MNG/PNG/JNG: %i" },
	{ NSERROR_BAD_ENCODING, "BadEncoding" },
	{ NSERROR_NEED_DATA, "NeedData" },
	{ NSERROR_ENCODING_CHANGE, "EncodingChanged" },
	{ NSERROR_BAD_PARAMETER, "BadParameter" },
	{ NSERROR_INVALID, "Invalid" },
	{ NSERROR_BOX_CONVERT, "BoxConvert" },
	{ NSERROR_STOPPED, "Stopped" },
	{ NSERROR_DOM, "Parsing the document failed." },
	{ NSERROR_CSS, "Error processing CSS" },
	{ NSERROR_CSS_BASE, "Base stylesheet failed to load" },
	{ NSERROR_BAD_URL, "BadURL" },
	{ NSERROR_UNKNOWN, "Unknown" },
};

START_TEST(messages_errorcode_test)
{
	const char *res_str;
	const struct message_test_vec_s *tst = &message_errorcode_test_vec[_i];
	nserror res;

	res = messages_add_from_inline(test_data_Messages,
				       test_data_Messages_len);
	ck_assert_int_eq(res, NSERROR_OK);

	res_str = messages_get_errorcode(tst->test);

	/* ensure result data is correct */
	ck_assert_str_eq(res_str, tst->res);
}
END_TEST

START_TEST(message_inline_load_test)
{
	nserror res;
	res = messages_add_from_inline(test_data_Messages,
				       test_data_Messages_len);
	ck_assert_int_eq(res, NSERROR_OK);
}
END_TEST

START_TEST(message_file_load_test)
{
	nserror res;
	res = messages_add_from_file(test_messages_path);
	ck_assert_int_eq(res, NSERROR_OK);
}
END_TEST

TCase *message_session_case_create(void)
{
	TCase *tc;
	tc = tcase_create("Message adding");

	tcase_add_test(tc, message_file_load_test);
	tcase_add_test(tc, message_inline_load_test);
	tcase_add_loop_test(tc, messages_errorcode_test,
			    0, NELEMS(message_errorcode_test_vec));

	return tc;
}


Suite *message_suite_create(void)
{
	Suite *s;
	s = suite_create("message");

	suite_add_tcase(s, message_session_case_create());

	return s;
}

int main(int argc, char **argv)
{
	int number_failed;
	SRunner *sr;

	sr = srunner_create(message_suite_create());

	srunner_run_all(sr, CK_ENV);

	number_failed = srunner_ntests_failed(sr);
	srunner_free(sr);

	return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}