1 /*
2  *   Copyright (c) 2005-2012 NFG Net Facilities Group BV support@nfg.nl
3  *
4  *   This program is free software; you can redistribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation; either
7  *   version 2 of the License, or (at your option) any later
8  *   version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *   GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program; if not, write to the Free Software
17  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *
20  *
21  *
22  *
23  *
24  *
25  *   Basic unit-test framework for dbmail (www.dbmail.org)
26  *
27  *   See http://check.sf.net for details and docs.
28  *
29  *
30  *   Run 'make check' to see some action.
31  *
32  */
33 
34 #include <check.h>
35 #include "check_dbmail.h"
36 
37 extern char configFile[PATH_MAX];
38 extern int quiet;
39 extern int reallyquiet;
40 
41 /*
42  *
43  * the test fixtures
44  *
45  */
setup(void)46 void setup(void)
47 {
48 	reallyquiet = 1;
49 	config_get_file();
50 	config_read(configFile);
51 	configure_debug(NULL,255,0);
52 	GetDBParams();
53 	db_connect();
54 	auth_connect();
55 	do_add("testfailuser","testpass","md5-hash",0,0,NULL,NULL);
56 }
57 
teardown(void)58 void teardown(void)
59 {
60 	db_disconnect();
61 }
62 
63 //int do_add(const char * const user,
64 //           const char * const password,
65 //           const char * const enctype,
66 //           const uint64_t maxmail, const uint64_t clientid,
67 //	   GList * alias_add,
68 //	   GList * alias_del);
69 
START_TEST(test_do_add)70 START_TEST(test_do_add)
71 {
72 	int result;
73 	result = do_add("testfailuser","testpass","md5-hash",0,0,NULL,NULL);
74 	fail_unless(result!=0,"do_add succeeded when it should have failed");
75 	result = do_add("testadduser","testpass","md5-hash",0,0,NULL,NULL);
76 	fail_unless(result==0,"do_add failed");
77 }
78 END_TEST
79 
80 
81 //int do_show(const char * const user);
START_TEST(test_do_show)82 START_TEST(test_do_show)
83 {
84 	fail_unless(do_show("testfailuser")==0,"test_do_show failed");
85 	fail_unless(do_show("nosuchuser"),"do_show should have failed");
86 }
87 END_TEST
88 
89 //int do_empty(const uint64_t useridnr);
START_TEST(test_do_empty)90 START_TEST(test_do_empty)
91 {
92 	uint64_t user_idnr;
93 	auth_user_exists("nosuchuser",&user_idnr);
94 	//fail_unless(do_empty(user_idnr),"do_empty should have failed");
95 }
96 END_TEST
97 
98 //int do_delete(const uint64_t useridnr, const char * const user);
START_TEST(test_do_delete)99 START_TEST(test_do_delete)
100 {
101 	int result;
102 	uint64_t user_idnr;
103 	auth_user_exists("testadduser",&user_idnr);
104 	fail_unless(user_idnr > 0,"abort test_do_delete: can't find user_idnr");
105 	result = do_delete(user_idnr, "testadduser");
106 	fail_unless(result==0,"test_do_delete failed");
107 }
108 END_TEST
109 
START_TEST(test_dm_match)110 START_TEST(test_dm_match)
111 {
112 	int i;
113 	char *candidate = "MyName is SAMMY @ and I am a SLUG!";
114 	char *badpatterns[] = {
115 		"hello", "*hello", "*hello*", "hello*",
116 		"*hello", "*hello*", "hello*", "?", NULL };
117 	char *goodpatterns[] = {
118 		"*", "*and*", "*SLUG!", "My*", "????My*",
119 		"MyName ?? *", "??Name*", "*SLUG?", NULL };
120 
121 	for (i = 0; badpatterns[i] != NULL; i++) {
122 		fail_unless(match_glob(badpatterns[i], candidate)
123 			== NULL, "test_dm_match failed on a bad pattern:"
124 			" [%s]", badpatterns[i]);
125 	}
126 
127 	for (i = 0; goodpatterns[i] != NULL; i++) {
128 		fail_unless(match_glob(goodpatterns[i], candidate)
129 			== candidate, "test_dm_match failed on a good pattern:"
130 			" [%s]", goodpatterns[i]);
131 	}
132 
133 }
134 END_TEST
135 
START_TEST(test_dm_match_list)136 START_TEST(test_dm_match_list)
137 {
138 }
139 END_TEST
140 
141 /* Change operations */
142 //int do_username(const uint64_t useridnr, const char *newuser);
143 //int do_maxmail(const uint64_t useridnr, const uint64_t maxmail);
144 //int do_clientid(const uint64_t useridnr, const uint64_t clientid);
145 //int do_password(const uint64_t useridnr,
146 //                const char * const password,
147 //                const char * const enctype);
148 //int do_aliases(const uint64_t useridnr,
149 //               GList * alias_add,
150 //               GList * alias_del);
151 /* External forwards */
152 //int do_forwards(const char *alias, const uint64_t clientid,
153 //                GList * fwds_add,
154 //                GList * fwds_del);
155 
156 /* Helper functions */
157 //uint64_t strtomaxmail(const char * const str);
158 
159 
dbmail_common_suite(void)160 Suite *dbmail_common_suite(void)
161 {
162 	Suite *s = suite_create("Dbmail User");
163 	TCase *tc_user = tcase_create("User");
164 
165 	suite_add_tcase(s, tc_user);
166 
167 	tcase_add_checked_fixture(tc_user, setup, teardown);
168 	tcase_add_test(tc_user, test_do_add);
169 	tcase_add_test(tc_user, test_do_show);
170 	tcase_add_test(tc_user, test_do_empty);
171 	tcase_add_test(tc_user, test_do_delete);
172 	tcase_add_test(tc_user, test_dm_match);
173 	tcase_add_test(tc_user, test_dm_match_list);
174 
175 	return s;
176 }
177 
main(void)178 int main(void)
179 {
180 	int nf;
181 	Suite *s = dbmail_common_suite();
182 	SRunner *sr = srunner_create(s);
183 	srunner_run_all(sr, CK_NORMAL);
184 	nf = srunner_ntests_failed(sr);
185 	srunner_free(sr);
186 	return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
187 }
188 
189 
190