/* * Copyright (c) 2005-2006 NFG Net Facilities Group BV support@nfg.nl * * This program 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; either * version 2 of the License, or (at your option) any later * version. * * This program 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, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * * * * * * * Basic unit-test framework for dbmail (www.dbmail.org) * * See http://check.sf.net for details and docs. * * * Run 'make check' to see some action. * */ #include #include "check_dbmail.h" extern char *configFile; extern db_param_t _db_params; #define DBPFX _db_params.pfx /* we need this one because we can't directly link imapd.o */ int imap_before_smtp = 0; extern u64_t msgbuf_idx; extern u64_t msgbuf_buflen; extern char *multipart_message; extern char *multipart_message_part; extern char *raw_lmtp_data; /* * * the test fixtures * */ void init_testuser1(void) { u64_t user_idnr; if (! (auth_user_exists("testuser1",&user_idnr))) auth_adduser("testuser1","test", "md5", 101, 1024000, &user_idnr); } void setup(void) { configure_debug(5,0); config_read(configFile); GetDBParams(&_db_params); db_connect(); auth_connect(); init_testuser1(); } void teardown(void) { auth_disconnect(); db_disconnect(); config_free(); } START_TEST(test_dbmail_imap_plist_as_string) { char *result; GList *l; l = NULL; l = g_list_append(l, "NIL"); l = g_list_append(l, "NIL"); result = dbmail_imap_plist_as_string(l); fail_unless(strcmp(result,"(NIL NIL)")==0,"plist construction failed"); //g_list_foreach(l,(GFunc)g_free,NULL); g_free(result); l = NULL; l = g_list_append(l, "(NIL NIL)"); result = dbmail_imap_plist_as_string(l); fail_unless(strcmp(result,"(NIL NIL)")==0,"plist construction failed"); //g_list_foreach(l,(GFunc)g_free,NULL); g_free(result); l = g_list_append(NULL, "NIL"); l = g_list_append(l, "NIL"); l = g_list_append(l, "(NIL NIL)"); l = g_list_append(l, "(NIL NIL)"); result = dbmail_imap_plist_as_string(l); fail_unless(strcmp(result,"(NIL NIL (NIL NIL) (NIL NIL))")==0,"plist construction failed"); //g_list_foreach(l,(GFunc)g_free,NULL); g_free(result); } END_TEST START_TEST(test_dbmail_imap_plist_collapse) { char *result; char *in = "(NIL) (NIL) (NIL)"; result = dbmail_imap_plist_collapse(in); fail_unless(strcmp(result,"(NIL)(NIL)(NIL)")==0,"plist collapse failed"); g_free(result); } END_TEST #define A(x,y) s=dbmail_imap_astring_as_string(x); \ fail_unless(strcmp(y,s)==0,"dbmail_imap_astring_as_string failed [%s] != [%s]", s, y); \ g_free(s) START_TEST(test_dbmail_imap_astring_as_string) { char *s; A("test","\"test\""); A("\"test\"","\"test\""); A("\"test\" \"test\"","{13}\r\n\"test\" \"test\""); A("\"test","{5}\r\n\"test"); A("testÃ","{5}\r\ntestÃ"); A("test\"","{5}\r\ntest\""); A("test\"","{5}\r\ntest\""); A("test\\","{5}\r\ntest\\"); A("test\225","{5}\r\ntest\225"); } END_TEST static clientinfo_t * ci_new(void) { clientinfo_t *ci = g_new0(clientinfo_t,1); FILE *fd = fopen("/dev/null","w"); ci->userData = dbmail_imap_userdata_new(); ci->rx = stdin; ci->tx = fd; //ci->tx = stderr; return ci; } static char *tempfile; static clientinfo_t * ci_new_writable(void) { clientinfo_t *ci = ci_new(); tempfile = tmpnam(NULL); mkfifo(tempfile, 0600); // Open r+ because we're controlling both sides. ci->rx = fopen(tempfile, "r+"); ci->tx = fopen(tempfile, "r+"); return ci; } static void ci_free_writable(clientinfo_t *ci) { fclose(ci->tx); fclose(ci->rx); unlink(tempfile); } //struct ImapSession * dbmail_imap_session_new(void); START_TEST(test_imap_session_new) { struct ImapSession *s; s = dbmail_imap_session_new(); fail_unless(s!=NULL, "Failed to initialize imapsession"); dbmail_imap_session_delete(s); } END_TEST //struct ImapSession * dbmail_imap_session_setClientinfo(struct ImapSession * self, clientinfo_t *ci); //struct ImapSession * dbmail_imap_session_setTag(struct ImapSession * self, char * tag); //struct ImapSession * dbmail_imap_session_setCommand(struct ImapSession * self, char * command); //struct ImapSession * dbmail_imap_session_setArgs(struct ImapSession * self, char ** args); //struct ImapSession * dbmail_imap_session_setMsginfo(struct ImapSession * self, msginfo_t * msginfo); //struct ImapSession * dbmail_imap_session_resetFi(struct ImapSession * self); //void dbmail_imap_session_delete(struct ImapSession * self); //int dbmail_imap_session_readln(struct ImapSession * self, char * buffer); //int dbmail_imap_session_discard_to_eol(struct ImapSession *self); //int dbmail_imap_session_printf(struct ImapSession * self, char * message, ...); //int dbmail_imap_session_set_state(struct ImapSession *self, int state); //int check_state_and_args(struct ImapSession * self, const char * command, int minargs, int maxargs, int state); //int dbmail_imap_session_handle_auth(struct ImapSession * self, char * username, char * password); //int dbmail_imap_session_prompt(struct ImapSession * self, char * prompt, char * value); //u64_t dbmail_imap_session_mailbox_get_idnr(struct ImapSession * self, char * mailbox); //int dbmail_imap_session_mailbox_check_acl(struct ImapSession * self, u64_t idnr, ACLRight_t right); //int dbmail_imap_session_mailbox_get_selectable(struct ImapSession * self, u64_t idnr); //int dbmail_imap_session_mailbox_show_info(struct ImapSession * self); //int dbmail_imap_session_mailbox_open(struct ImapSession * self, char * mailbox); //dbmail_imap_session_handle_auth(self, const char *username, const char *password); START_TEST(test_imap_session_prompt) { int result; unsigned char tmpline[MAX_LINESIZE]; clientinfo_t *ci = ci_new_writable(); struct ImapSession *s = dbmail_imap_session_new(); s = dbmail_imap_session_setClientinfo(s,ci); unsigned char *testuser = (unsigned char *)"testuser1"; base64_encode(tmpline, testuser, strlen("testuser1")); fprintf(ci->tx, "%s\r\n", tmpline); fflush(ci->tx); result = dbmail_imap_session_prompt(s, "username", (char *)tmpline); fail_unless(result==0,"dbmail_imap_session_prompt failed"); fail_unless(strcmp((char *)tmpline,(char *)testuser)==0,"failed at the username prompt"); // Read back whatever the prompt was. fgets((char *)tmpline, MAX_LINESIZE, ci->rx); base64_encode(tmpline, (unsigned char *)"test", strlen("test")); fprintf(ci->tx, "%s\r\n", tmpline); fflush(ci->tx); result = dbmail_imap_session_prompt(s, "password", (char *)tmpline); fail_unless(result==0,"dbmail_imap_session_prompt failed"); fail_unless(strcmp((char *)tmpline,"test")==0,"failed at the password prompt"); // Read back whatever the prompt was. fgets((char *)tmpline, MAX_LINESIZE, ci->rx); dbmail_imap_session_delete(s); ci_free_writable(ci); } END_TEST START_TEST(test_imap_handle_auth) { int result; clientinfo_t *ci = ci_new(); struct ImapSession *s = dbmail_imap_session_new(); s = dbmail_imap_session_setClientinfo(s,ci); result = dbmail_imap_session_handle_auth(s,"testuser1","test"); fail_unless(result==0,"dbmail_imap_session_handle_auth failed"); dbmail_imap_session_delete(s); g_free(ci); } END_TEST START_TEST(test_imap_mailbox_open) { int result; clientinfo_t *ci = ci_new(); imap_userdata_t *ud; u64_t mailbox_idnr = 0; const char *message; struct ImapSession *s = dbmail_imap_session_new(); s = dbmail_imap_session_setClientinfo(s,ci); dbmail_imap_session_handle_auth(s,"testuser1","test"); result = dbmail_imap_session_mailbox_open(s,"INBOX"); fail_unless(result==0,"dbmail_imap_session_mailbox_open failed"); // create and open a new and empty mailbox ud = (imap_userdata_t *) ci->userData; result = db_mailbox_create_with_parents("INBOX/Foo/Bar/Baz", BOX_COMMANDLINE, ud->userid, &mailbox_idnr, &message); fail_unless(result == 0 && mailbox_idnr != 0, "Failed at db_mailbox_create_with_parents: [%s]", message); result = dbmail_imap_session_mailbox_open(s,"INBOX/Foo/Bar/Baz"); fail_unless(result==0,"dbmail_imap_session_mailbox_open failed"); db_delete_mailbox(mailbox_idnr,0,0); dbmail_imap_session_delete(s); g_free(ci); } END_TEST //int dbmail_imap_session_fetch_get_items(struct ImapSession *self, u64_t row) START_TEST(test_dbmail_imap_session_fetch_get_items) { int result; char * mailbox = g_strdup("INBOX"); struct ImapSession *s = dbmail_imap_session_new(); clientinfo_t *ci = ci_new(); s = dbmail_imap_session_setClientinfo(s,ci); g_free(ci); result = dbmail_imap_session_handle_auth(s,"testuser1","test"); fail_unless(result==0, "handle_auth failed"); result = dbmail_imap_session_mailbox_open(s,mailbox); fail_unless(result==0, "mailbox_open failed"); dbmail_mailbox_set_uid(s->mailbox, TRUE); s->ids = dbmail_mailbox_get_set(s->mailbox, "1:*", TRUE); fail_unless(s->ids!=NULL, "get_set failed"); result = dbmail_imap_session_fetch_get_items(s); fail_unless(result==0, "fetch_get_items failed"); dbmail_imap_session_delete(s); g_free(mailbox); } END_TEST START_TEST(test_imap_bodyfetch) { int result; guint64 octet; struct ImapSession *s = dbmail_imap_session_new(); dbmail_imap_session_bodyfetch_new(s); fail_unless(0 == dbmail_imap_session_bodyfetch_get_last_octetstart(s), "octetstart init value incorrect"); fail_unless(0 == dbmail_imap_session_bodyfetch_get_last_octetcnt(s), "octetcnt init value incorrect"); fail_unless(0 == dbmail_imap_session_bodyfetch_get_last_argstart(s), "argstart init value incorrect"); s->args_idx = 23; dbmail_imap_session_bodyfetch_set_argstart(s); result = dbmail_imap_session_bodyfetch_get_last_argstart(s); fail_unless(result==23, "argstart incorrect"); dbmail_imap_session_bodyfetch_set_octetstart(s,0); octet = dbmail_imap_session_bodyfetch_get_last_octetstart(s); fail_unless(octet==0, "octetstart incorrect"); dbmail_imap_session_bodyfetch_set_octetcnt(s,12288); octet = dbmail_imap_session_bodyfetch_get_last_octetcnt(s); fail_unless(octet==12288, "octetcnt incorrect"); dbmail_imap_session_delete(s); } END_TEST START_TEST(test_g_mime_object_get_body) { char * result; struct DbmailMessage *m; m = dbmail_message_new(); m = dbmail_message_init_with_string(m,g_string_new(multipart_message)); result = g_mime_object_get_body(GMIME_OBJECT(m->content)); fail_unless(strlen(result)==1045,"g_mime_object_get_body failed"); g_free(result); dbmail_message_free(m); } END_TEST START_TEST(test_imap_get_structure) { struct DbmailMessage *message; char *result; char *expect = g_new0(char,1024); /* bare bones */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(simple)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); dbmail_message_free(message); g_free(result); /* multipart */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(multipart_message)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); strncpy(expect,"((\"text\" \"html\" NIL NIL NIL \"7BIT\" 16 1 NIL (\"inline\" NIL) NIL NIL)" "(\"text\" \"plain\" (\"charset\" \"us-ascii\" \"name\" \"testfile\") NIL NIL \"base64\" 432 7 NIL NIL NIL NIL)" " \"mixed\" (\"boundary\" \"boundary\") NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_structure failed"); g_free(result); dbmail_message_free(message); /* multipart alternative */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(multipart_alternative)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); strncpy(expect,"(((\"TEXT\" \"PLAIN\" (\"CHARSET\" \"ISO-8859-1\") NIL NIL \"7BIT\" 281 10 NIL NIL NIL NIL)(\"TEXT\" \"HTML\" (\"CHARSET\" \"ISO-8859-1\") NIL NIL \"7BIT\" 759 17 NIL NIL NIL NIL) \"ALTERNATIVE\" (\"BOUNDARY\" \"------------040302030903000400040101\") NIL NIL NIL)(\"IMAGE\" \"JPEG\" (\"NAME\" \"jesse_2.jpg\") NIL NIL \"BASE64\" 262 NIL (\"INLINE\" (\"FILENAME\" \"jesse_2.jpg\")) NIL NIL) \"MIXED\" (\"BOUNDARY\" \"------------050000030206040804030909\") NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_structure failed"); g_free(result); dbmail_message_free(message); /* text/plain */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(rfc822)); result = imap_get_structure(GMIME_MESSAGE(message->content), 1); strncpy(expect,"(\"text\" \"plain\" (\"charset\" \"us-ascii\") NIL NIL \"7bit\" 32 4 NIL NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_structure failed"); g_free(result); g_free(expect); dbmail_message_free(message); } END_TEST START_TEST(test_internet_address_parse_string) { char * trythese [] = { "undisclosed-recipients", "undisclosed-recipients;", "undisclosed-recipients:", "undisclosed-recipients:;", "undisclosed-recipients: ;", NULL }; int i; for (i = 0; trythese[i] != NULL; i++) { char *result = trythese[i]; char *t; InternetAddressList *alist; char *expect = "((NIL NIL \"undisclosed-recipients\" NIL))"; GList *list = NULL; // Copied from dm_imaputil.c, envelope_address_part t = imap_cleanup_address(result); alist = internet_address_parse_string(t); g_free(t); list = dbmail_imap_append_alist_as_plist(list, (const InternetAddressList *)alist); internet_address_list_destroy(alist); alist = NULL; result = dbmail_imap_plist_as_string(list); fail_unless(strcmp(result,expect)==0, "internet_address_parse_string failed to generate correct undisclosed-recipients plist, expected [%s] got [%s]", expect, result); g_list_destroy(list); g_free(result); } char * testlist[][2] = { { "", "((NIL NIL \"i_am_not\" \"broken.org\"))" }, { "Break me: ", "((NIL NIL \"Break me\" NIL)(NIL NIL \"foo\" \"bar.org\"))" }, { "Joe's Friends: mary@joe.com, joe@joe.com, jane@joe.com;", "((NIL NIL \"Joe's Friends\" NIL)(NIL NIL \"mary\" \"joe.com\")" "(NIL NIL \"joe\" \"joe.com\")(NIL NIL \"jane\" \"joe.com\"))" }, // These have the wrong separator; ms lookout style. { "one@my.dom;two@my.dom", "((NIL NIL \"one\" \"my.dom\")(NIL NIL \"two\" \"my.dom\"))" }, { "one@my.dom; two@my.dom", "((NIL NIL \"one\" \"my.dom\")(NIL NIL \"two\" \"my.dom\"))" }, { "Group: one@my.dom;, two@my.dom", "((NIL NIL \"Group\" NIL)(NIL NIL \"one\" \"my.dom\")(NIL NIL \"two\" \"my.dom\"))" }, { NULL, NULL } }; char *input, *expect; for (i = 0; testlist[i][0] != NULL; i++) { input = testlist[i][0]; expect = testlist[i][1]; InternetAddressList *alist; GList *list = NULL; char *result; int res; char *t; t = imap_cleanup_address(input); alist = internet_address_parse_string(t); list = dbmail_imap_append_alist_as_plist(list, (const InternetAddressList *)alist); result = dbmail_imap_plist_as_string(list); res = strcmp(result, expect); fail_unless(res == 0, "dbmail_imap_append_alist_as_plist failed, expected:\n[%s]\ngot:\n[%s]\n", expect, result); internet_address_list_destroy(alist); alist = NULL; g_list_destroy(list); g_free(result); g_free(t); } } END_TEST START_TEST(test_imap_get_envelope) { struct DbmailMessage *message; char *result, *expect; GString *s; expect = g_new0(char, 1024); /* text/plain */ message = dbmail_message_new(); s = g_string_new(rfc822); message = dbmail_message_init_with_string(message, s); g_string_free(s,TRUE); result = imap_get_envelope(GMIME_MESSAGE(message->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"dbmail test message\" ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"testuser\" \"foo.org\")) NIL NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_envelope failed"); dbmail_message_free(message); g_free(result); result = NULL; /* bare bones message */ message = dbmail_message_new(); s = g_string_new(simple); message = dbmail_message_init_with_string(message, s); g_string_free(s,TRUE); result = imap_get_envelope(GMIME_MESSAGE(message->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"dbmail test message\" NIL NIL NIL NIL NIL NIL NIL NIL)", 1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_envelope failed"); dbmail_message_free(message); g_free(result); result = NULL; g_free(expect); expect = NULL; } END_TEST START_TEST(test_imap_get_envelope_8bit_id) { struct DbmailMessage *message; char *result, *expect; const char *msgid = "<000001c1f64e$c4a34180$0100007f@z=F0=B5=D241>"; expect = g_new0(char, 1024); /* text/plain */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(rfc822)); dbmail_message_set_header(message,"Message-ID",msgid); result = imap_get_envelope(GMIME_MESSAGE(message->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"dbmail test message\" ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"testuser\" \"foo.org\")) NIL NIL NIL NIL)",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_envelope failed"); g_free(result); dbmail_message_set_header(message,"Message-ID","<123123123@foo.bar>"); result = imap_get_envelope(GMIME_MESSAGE(message->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"dbmail test message\" ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"somewher\" \"foo.org\")) ((NIL NIL \"testuser\" \"foo.org\")) NIL NIL NIL \"<123123123@foo.bar>\")",1024); fail_unless(strncasecmp(result,expect,1024)==0, "imap_get_envelope failed"); dbmail_message_free(message); g_free(result); g_free(expect); } END_TEST START_TEST(test_imap_get_envelope_koi) { char *t; const char *exp = "(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"test\" ((\"=?koi8-r?Q?=E1=CE=D4=CF=CE=20=EE=C5=C8=CF=D2=CF=DB=C9=C8=20?=\" NIL \"bad\" \"foo.ru\")) ((\"=?koi8-r?Q?=E1=CE=D4=CF=CE=20=EE=C5=C8=CF=D2=CF=DB=C9=C8=20?=\" NIL \"bad\" \"foo.ru\")) ((\"=?koi8-r?Q?=E1=CE=D4=CF=CE=20=EE=C5=C8=CF=D2=CF=DB=C9=C8=20?=\" NIL \"bad\" \"foo.ru\")) ((NIL NIL \"nobody\" \"foo.ru\")) NIL NIL NIL NIL)"; struct DbmailMessage *m = dbmail_message_new(); GString *s = g_string_new(encoded_message_koi); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); t = imap_get_envelope(GMIME_MESSAGE(m->content)); fail_unless(strcmp(t,exp)==0,"encode/decode/encode loop failed\n[%s] !=\n[%s]", t,exp); g_free(t); dbmail_message_free(m); } END_TEST #define F(a,b) fail_unless(strcmp(c = imap_cleanup_address(a), b)==0, "\n[%s] should have yielded \n[%s] but got \n[%s]", a,b,c) #define Fnull(a,b) fail_unless(strcmp(c = imap_cleanup_address(a), b)==0, "\n[] should have yielded \n[" b "] but got \n[%s]", c) START_TEST(test_imap_cleanup_address) { char *c; F("=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?= ","\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" "); F("=?iso-8859-1?Q?\"B=BA_V._F._Z=EAzere\"?=","\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" "); F("=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=","\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" "); F("\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" ","\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" "); F("", ""); Fnull(NULL, ""); F("Some One ", "Some One "); F(" ", ""); F("=?ISO-8859-2?Q? \"Verlag=20Dash=F6fer=20-=20DU.cz?= =?ISO-8859-2?Q?\" ?= ", "\"=?ISO-8859-2?Q?Verlag=20Dash=F6fer=20-=20DU.cz?= =?ISO-8859-2?Q?" /* Stringify here to kill the '??=' trigraph. */ "?=\" "); F("=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\n" " \n", "\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" "); F("=?iso-8859-1?Q?::_=5B_Arrty_=5D_::_=5B_Roy_=28L=29_St=E8phanie_=5D?= ", "\"=?iso-8859-1?Q?::_=5B_Arrty_=5D_::_=5B_Roy_=28L=29_St=E8phanie_=5D?=\" "); F("\"First Address\" , =?iso-8859-1?Q?::_=5B_Arrty_=5D_::_=5B_Roy_=28L=29_St=E8phanie_=5D?= ", "\"First Address\" , \"=?iso-8859-1?Q?::_=5B_Arrty_=5D_::_=5B_Roy_=28L=29_St=E8phanie_=5D?=\" "); } END_TEST START_TEST(test_imap_get_envelope_latin) { char *t; char *expect = g_new0(char,1024); struct DbmailMessage *m; GString *s; /* */ m = dbmail_message_new(); s = g_string_new(encoded_message_latin_1); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); t = imap_get_envelope(GMIME_MESSAGE(m->content)); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"=?iso-8859-1?Q?Re:_M=F3dulo_Extintores?=\" ((\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" NIL \"nobody\" \"nowhere.org\")) ((\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" NIL \"nobody\" \"nowhere.org\")) ((\"=?iso-8859-1?Q?B=BA_V._F._Z=EAzere?=\" NIL \"nobody\" \"nowhere.org\")) ((NIL NIL \"nobody\" \"foo.org\")) NIL NIL NIL NIL)",1024); fail_unless(strcmp(t,expect)==0,"imap_get_envelope failed\n%s\n%s\n ", expect, t); g_free(t); dbmail_message_free(m); /* */ m = dbmail_message_new(); s = g_string_new(encoded_message_latin_2); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"=?ISO-8859-2?Q?Re=3A_=5Bgentoo-dev=5D_New_developer=3A__?= =?ISO-8859-2?Q?Miroslav_=A9ulc_=28fordfrog=29?=\" ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((NIL NIL \"gentoo-dev\" \"lists.gentoo.org\")) NIL NIL NIL NIL)",1024); t = imap_get_envelope(GMIME_MESSAGE(m->content)); fail_unless(strcmp(t,expect)==0,"imap_get_envelope failed\n%s\n%s\n ", expect, t); g_free(t); g_free(expect); dbmail_message_free(m); /* */ m = dbmail_message_new(); s = g_string_new(encoded_message_utf8); m = dbmail_message_init_with_string(m, s); g_string_free(s,TRUE); //strncpy(expect,"(\"Thu, 01 Jan 1970 00:00:00 +0000\" \"=?ISO-8859-2?Q?Re=3A_=5Bgentoo-dev=5D_New_developer=3A__?= =?ISO-8859-2?Q?Miroslav_=A9ulc_=28fordfrog=29?=\" ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((\"=?ISO-8859-2?Q?=22Miroslav_=A9ulc_=28fordfrog=29=22?=\" NIL \"fordfrog\" \"gentoo.org\")) ((NIL NIL \"gentoo-dev\" \"lists.gentoo.org\")) NIL NIL NIL NIL)",1024); t = imap_get_envelope(GMIME_MESSAGE(m->content)); //fail_unless(strcmp(t,expect)==0,"imap_get_envelope failed\n%s\n%s\n ", expect, t); g_free(t); //g_free(expect); dbmail_message_free(m); } END_TEST START_TEST(test_imap_get_partspec) { struct DbmailMessage *message; GMimeObject *object; char *result, *expect; /* text/plain */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(rfc822)); object = imap_get_partspec(GMIME_OBJECT(message->content),"HEADER"); result = imap_get_logical_part(object,"HEADER"); fail_unless(strlen(result)==169,"imap_get_partspec failed"); g_free(result); object = imap_get_partspec(GMIME_OBJECT(message->content),"TEXT"); result = imap_get_logical_part(object,"TEXT"); fail_unless(strlen(result)==29,"imap_get_partspec failed"); g_free(result); dbmail_message_free(message); /* multipart */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(multipart_message)); object = imap_get_partspec(GMIME_OBJECT(message->content),"1"); result = g_mime_object_to_string(object); expect = g_strdup("Content-type: text/html\n" "Content-disposition: inline\n\n" "Test message one"); fail_unless(MATCH(expect,result),"imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); object = imap_get_partspec(GMIME_OBJECT(message->content),"1.TEXT"); result = imap_get_logical_part(object,"TEXT"); fail_unless(strlen(result)==16,"imap_get_partspec failed"); g_free(result); object = imap_get_partspec(GMIME_OBJECT(message->content),"1.HEADER"); result = imap_get_logical_part(object,"HEADER"); fail_unless(strlen(result)==53,"imap_get_partspec failed"); g_free(result); object = imap_get_partspec(GMIME_OBJECT(message->content),"2.MIME"); result = imap_get_logical_part(object,"MIME"); fail_unless(strlen(result)==93,"imap_get_partspec failed"); g_free(result); dbmail_message_free(message); /* multipart mixed */ message = dbmail_message_new(); message = dbmail_message_init_with_string(message, g_string_new(multipart_mixed)); object = imap_get_partspec(GMIME_OBJECT(message->content),"2.HEADER"); result = imap_get_logical_part(object,"HEADER"); fail_unless(strncmp(result,"From: \"try\" ",29)==0,"imap_get_partspec failed"); g_free(result); object = imap_get_partspec(GMIME_OBJECT(message->content),"2.1.1"); result = g_mime_object_to_string(object); expect = g_strdup("Content-Type: text/plain;\n" " charset=\"us-ascii\"\n" "Content-Transfer-Encoding: 7bit\n\n" "Body of doc2\n\n"); fail_unless(MATCH(expect,result),"imap_get_partspec failed:\n[%s] != \n[%s]\n", expect, result); g_free(result); g_free(expect); dbmail_message_free(message); } END_TEST #ifdef OLD static u64_t get_physid(void) { u64_t id = 0; GString *q = g_string_new(""); g_string_printf(q,"select id from %sphysmessage order by id desc limit 1", DBPFX); db_query(q->str); g_string_free(q,TRUE); id = db_get_result_u64(0,0); db_free_result(); return id; } static u64_t get_msgsid(void) { u64_t id = 0; GString *q = g_string_new(""); g_string_printf(q,"select message_idnr from %smessages order by message_idnr desc limit 1", DBPFX); db_query(q->str); g_string_free(q,TRUE); id = db_get_result_u64(0,0); db_free_result(); return id; } #endif START_TEST(test_g_list_slices) { unsigned i=0; unsigned j=98; unsigned s=11; GList *list = NULL; GList *sub = NULL; for (i=0; i< j; i++) list = g_list_append_printf(list, "ELEM_%d", i); list = g_list_slices(list, s); list = g_list_first(list); fail_unless(g_list_length(list)==9, "number of slices incorrect"); sub = g_string_split(g_string_new((gchar *)list->data), ","); fail_unless(g_list_length(sub)==s,"Slice length incorrect"); g_list_foreach(list,(GFunc)g_free,NULL); g_list_foreach(sub,(GFunc)g_free,NULL); i=0; j=17; s=100; list = NULL; sub = NULL; for (i=0; i< j; i++) list = g_list_append_printf(list, "ELEM_%d", i); list = g_list_slices(list, s); list = g_list_first(list); fail_unless(g_list_length(list)==1, "number of slices incorrect [%d]", g_list_length(list)); sub = g_string_split(g_string_new((gchar *)list->data), ","); fail_unless(g_list_length(sub)==j,"Slice length incorrect"); } END_TEST START_TEST(test_g_list_slices_u64) { unsigned i=0; unsigned j=98; unsigned s=11; u64_t *l; GList *list = NULL; GList *sub = NULL; for (i=0; i< j; i++) { l = g_new0(u64_t,1); *l = i; list = g_list_append(list, l); } list = g_list_slices_u64(list, s); list = g_list_first(list); fail_unless(g_list_length(list)==9, "number of slices incorrect"); sub = g_string_split(g_string_new((gchar *)list->data), ","); fail_unless(g_list_length(sub)==s,"Slice length incorrect"); g_list_foreach(list,(GFunc)g_free,NULL); g_list_foreach(sub,(GFunc)g_free,NULL); i=0; j=17; s=100; list = NULL; sub = NULL; for (i=0; i< j; i++) list = g_list_append_printf(list, "ELEM_%d", i); list = g_list_slices(list, s); list = g_list_first(list); fail_unless(g_list_length(list)==1, "number of slices incorrect [%d]", g_list_length(list)); sub = g_string_split(g_string_new((gchar *)list->data), ","); fail_unless(g_list_length(sub)==j,"Slice length incorrect"); } END_TEST unsigned int get_bound_lo(unsigned int * set, unsigned int setlen) { unsigned int index = 0; while (set[index]==0 && index < setlen) index++; return index; } unsigned int get_bound_hi(unsigned int * set, unsigned int setlen) { int index = setlen; while (set[index]==0 && index >= 0) index--; return (unsigned)index; } unsigned int get_count_on(unsigned int * set, unsigned int setlen) { unsigned int i, count = 0; for (i=0; i