Ejemplo n.º 1
0
gint
main (gint argc,
      gchar **argv)
{
	gint i, j;
	struct _camel_search_words *words, *tmp;

	camel_test_init (argc, argv);

	camel_test_start ("Search splitting");

	for (i = 0; i < G_N_ELEMENTS (split_tests); i++) {
		camel_test_push ("split %d '%s'", i, split_tests[i].word);

		words = camel_search_words_split (split_tests[i].word);
		check (words != NULL);
		check_msg (words->len == split_tests[i].count, "words->len = %d, count = %d", words->len, split_tests[i].count);

		for (j = 0; j < words->len; j++) {
			check_msg (
				strcmp (split_tests[i].splits[j].word, words->words[j]->word) == 0,
				"'%s' != '%s'", split_tests[i].splits[j].word, words->words[j]->word);
			check (split_tests[i].splits[j].type == words->words[j]->type);
		}

		camel_search_words_free (words);
		camel_test_pull ();
	}

	camel_test_end ();

	camel_test_start ("Search splitting - simple");

	for (i = 0; i < G_N_ELEMENTS (simple_tests); i++) {
		camel_test_push ("simple split %d '%s'", i, simple_tests[i].word);

		tmp = camel_search_words_split (simple_tests[i].word);
		check (tmp != NULL);

		words = camel_search_words_simple (tmp);
		check (words != NULL);
		check_msg (words->len == simple_tests[i].count, "words->len = %d, count = %d", words->len, simple_tests[i].count);

		for (j = 0; j < words->len; j++) {
			check_msg (
				strcmp (simple_tests[i].splits[j].word, words->words[j]->word) == 0,
				"'%s' != '%s'", simple_tests[i].splits[j].word, words->words[j]->word);
			check (simple_tests[i].splits[j].type == words->words[j]->type);
		}

		camel_search_words_free (words);
		camel_search_words_free (tmp);
		camel_test_pull ();
	}

	camel_test_end ();

	return 0;
}
Ejemplo n.º 2
0
gint
main (gint argc,
      gchar **argv)
{
	CamelURL *base_url, *url;
	gchar *url_string;
	gint i;
	GError *error = NULL;

	camel_test_init (argc, argv);

	camel_test_start ("URL parsing");

	camel_test_push ("base URL parsing");
	base_url = camel_url_new (base, &error);
	if (!base_url) {
		camel_test_fail (
			"Could not parse %s: %s\n",
			base, error->message);
	}
	camel_test_pull ();

	camel_test_push ("base URL unparsing");
	url_string = camel_url_to_string (base_url, 0);
	if (strcmp (url_string, base) != 0) {
		camel_test_fail ("URL <%s> unparses to <%s>\n",
				 base, url_string);
	}
	camel_test_pull ();
	g_free (url_string);

	for (i = 0; i < G_N_ELEMENTS (tests); i++) {
		camel_test_push ("<%s> + <%s> = <%s>?", base, tests[i].url_string, tests[i].result);
		url = camel_url_new_with_base (base_url, tests[i].url_string);
		if (!url) {
			camel_test_fail ("could not parse");
			camel_test_pull ();
			continue;
		}

		url_string = camel_url_to_string (url, 0);
		if (strcmp (url_string, tests[i].result) != 0)
			camel_test_fail ("got <%s>!", url_string);
		g_free (url_string);
		camel_test_pull ();
	}

	camel_test_end ();

	return 0;
}
int
main (int argc, char **argv)
{
	int i, j;

	camel_test_init(argc, argv);

	camel_test_start("references decoding");

	for (i=0;i<sizeof(test1)/sizeof(test1[0]);i++) {
		struct _camel_header_references *head, *node;

		camel_test_push("references decoding[%d] '%s'", i, test1[i].header);
		head = camel_header_references_decode(test1[i].header);
		node = head;
		for (j=0;test1[i].values[j];j++) {
			check_msg(node != NULL, "didn't find all references");
			check(strcmp(test1[i].values[j], node->id) == 0);
			node = node->next;
		}
		check_msg(node == NULL, "found more references than should have");
		camel_header_references_list_clear(&head);
		camel_test_pull();
	}

	camel_test_end();

	return 0;
}
Ejemplo n.º 4
0
static void
check_fi (CamelFolderInfo *fi,
          CamelFolderInfo *list,
          gint len)
{
	GPtrArray *folders = g_ptr_array_new ();
	gint i;

	add_fi (folders, fi);
	check_msg (folders->len == len, "unexpected number of folders returned from folderinfo");
	qsort (folders->pdata, folders->len, sizeof (folders->pdata[0]), cmp_fi);
	for (i = 0; i < len; i++) {
		CamelFolderInfo *f = folders->pdata[i];

		camel_test_push ("checking folder '%s'", list[i].display_name);

		check (!strcmp (f->full_name, list[i].full_name));

		/* this might be translated, but we can't know */
		camel_test_nonfatal ("Inbox not english");
		check (!strcmp (f->display_name, list[i].display_name));
		camel_test_fatal ();

		camel_test_nonfatal ("Flags mismatch");
		check (f->flags == list[i].flags);
		camel_test_fatal ();

		camel_test_pull ();
	}

	g_ptr_array_free (folders, TRUE);
}
Ejemplo n.º 5
0
gint
main (gint argc,
      gchar **argv)
{
	gint i, j;

	camel_test_init (argc, argv);

	camel_test_start ("references decoding");

	for (i = 0; i < G_N_ELEMENTS (test1); i++) {
		GSList *list;

		camel_test_push ("references decoding[%d] '%s'", i, test1[i].header);
		list = camel_header_references_decode (test1[i].header);
		for (j = 0; test1[i].values[j]; j++) {
			check_msg (list != NULL, "didn't find all references");
			check (strcmp (test1[i].values[j], list->data) == 0);
			list = g_slist_next (list);
		}
		check_msg (list == NULL, "found more references than should have");
		g_slist_free_full (list, g_free);
		camel_test_pull ();
	}

	camel_test_end ();

	return 0;
}
Ejemplo n.º 6
0
gint
main (gint argc,
      gchar **argv)
{
	gint i, j;
	GThread *threads[MAX_THREADS];

	camel_test_init (argc, argv);
	camel_test_provider_init (1, local_drivers);

	/* clear out any camel-test data */
	system ("/bin/rm -rf /tmp/camel-test");

	session = camel_test_session_new ("/tmp/camel-test");

	for (testid = 0; testid < 2; testid++) {
		if (testid == 0)
			camel_test_start ("store and folder bag torture test, stacked references");
		else
			camel_test_start ("store and folder bag torture test, unstacked references");

		for (j = 0; j < G_N_ELEMENTS (local_providers); j++) {

			camel_test_push ("provider %s", local_providers[j]);
			path = g_strdup_printf ("%s:///tmp/camel-test/%s", local_providers[j], local_providers[j]);

			for (i = 0; i < MAX_THREADS; i++) {
				GError *error = NULL;

				threads[i] = g_thread_create (worker, NULL, TRUE, &error);
				if (error) {
					fprintf (stderr, "%s: Failed to create a thread: %s\n", G_STRFUNC, error->message);
					g_error_free (error);
				}
			}

			for (i = 0; i < MAX_THREADS; i++) {
				if (threads[i])
					g_thread_join (threads[i]);
			}

			test_free (path);

			camel_test_pull ();
		}

		camel_test_end ();
	}

	g_object_unref (session);

	return 0;
}
int main(int argc, char **argv)
{
	CamelException *ex;
	int i, j;
	pthread_t threads[MAX_THREADS];

	camel_test_init(argc, argv);
	camel_test_provider_init(1, local_drivers);

	ex = camel_exception_new();

	/* clear out any camel-test data */
	system("/bin/rm -rf /tmp/camel-test");

	session = camel_test_session_new ("/tmp/camel-test");

	for (testid=0;testid<2;testid++) {
		if (testid == 0)
			camel_test_start("store and folder bag torture test, stacked references");
		else
			camel_test_start("store and folder bag torture test, unstacked references");

		for (j=0;j<ARRAY_LEN(local_providers);j++) {

			camel_test_push("provider %s", local_providers[j]);
			path = g_strdup_printf("%s:///tmp/camel-test/%s", local_providers[j], local_providers[j]);

			for (i=0;i<MAX_THREADS;i++)
				pthread_create(&threads[i], 0, worker, NULL);

			for (i=0;i<MAX_THREADS;i++)
				pthread_join(threads[i], NULL);

			test_free(path);

			camel_test_pull();
		}

		camel_test_end();
	}

	camel_object_unref((CamelObject *)session);
	camel_exception_free(ex);

	return 0;
}
Ejemplo n.º 8
0
gint
main (gint argc,
      gchar **argv)
{
	CamelStream *source;
	CamelStream *correct;
	CamelStream *stream;
	CamelMimeFilter *sh;
	gchar *work;
	gint i;
	gssize comp_progress, comp_correct_chunk, comp_filter_chunk;
	gint comp_i;
	gchar comp_correct[CHUNK_SIZE], comp_filter[CHUNK_SIZE];

	camel_test_init (argc, argv);

	for (i = 0; i < NUM_CASES; i++) {
		gint j;

		work = g_strdup_printf ("CRLF/DOT filter, test case %d", i);
		camel_test_start (work);
		g_free (work);

		for (j = CRLF_ENCODE; j < CRLF_DONE; j++) {
			CamelMimeFilterCRLFDirection direction;
			gchar *infile = NULL, *outfile = NULL;

			switch (j) {
			case CRLF_ENCODE:
				camel_test_push ("Test of the encoder");
				direction = CAMEL_MIME_FILTER_CRLF_ENCODE;
				infile = g_strdup_printf ("%s/crlf-%d.in", SOURCEDIR, i + 1);
				outfile = g_strdup_printf ("%s/crlf-%d.out", SOURCEDIR, i + 1);
				break;
			case CRLF_DECODE:
				camel_test_push ("Test of the decoder");
				direction = CAMEL_MIME_FILTER_CRLF_DECODE;
				infile = g_strdup_printf ("%s/crlf-%d.out", SOURCEDIR, i + 1);
				outfile = g_strdup_printf ("%s/crlf-%d.in", SOURCEDIR, i + 1);
				break;
			default:
				break;
			}

			camel_test_push ("Initializing objects");
			source = camel_stream_fs_new_with_name (infile, 0, O_RDONLY, NULL);
			if (!source) {
				camel_test_fail ("Failed to open input case in \"%s\"", infile);
				g_free (infile);
				continue;
			}
			g_free (infile);

			correct = camel_stream_fs_new_with_name (outfile, 0, O_RDONLY, NULL);
			if (!correct) {
				camel_test_fail ("Failed to open correct output in \"%s\"", outfile);
				g_free (outfile);
				continue;
			}
			g_free (outfile);

			stream = camel_stream_filter_new (source);
			if (!stream) {
				camel_test_fail ("Couldn't create CamelStreamFilter??");
				continue;
			}

			sh = camel_mime_filter_crlf_new (direction, CAMEL_MIME_FILTER_CRLF_MODE_CRLF_DOTS);
			if (!sh) {
				camel_test_fail ("Couldn't create CamelMimeFilterCrlf??");
				continue;
			}

			camel_stream_filter_add (
				CAMEL_STREAM_FILTER (stream), sh);
			camel_test_pull ();

			camel_test_push ("Running filter and comparing to correct result");

			comp_progress = 0;

			while (1) {
				comp_correct_chunk = camel_stream_read (
					correct, comp_correct,
					CHUNK_SIZE, NULL, NULL);
				comp_filter_chunk = 0;

				if (comp_correct_chunk == 0)
					break;

				while (comp_filter_chunk < comp_correct_chunk) {
					gssize delta;

					delta = camel_stream_read (
						stream,
						comp_filter + comp_filter_chunk,
						CHUNK_SIZE - comp_filter_chunk,
						NULL, NULL);

					if (delta == 0) {
						camel_test_fail ("Chunks are different sizes: correct is %d, "
								 "filter is %d, %d bytes into stream",
								 comp_correct_chunk, comp_filter_chunk, comp_progress);
					}

					comp_filter_chunk += delta;
				}

				for (comp_i = 0; comp_i < comp_filter_chunk; comp_i++) {
					if (comp_correct[comp_i] != comp_filter[comp_i]) {
						camel_test_fail ("Difference: correct is %c, filter is %c, "
								 "%d bytes into stream",
								 comp_correct[comp_i],
								 comp_filter[comp_i],
								 comp_progress + comp_i);
					}
				}

				comp_progress += comp_filter_chunk;
			}

			camel_test_pull ();

			/* inefficient */
			camel_test_push ("Cleaning up");
			g_object_unref (stream);
			g_object_unref (correct);
			g_object_unref (source);
			g_object_unref (sh);
			camel_test_pull ();

			camel_test_pull ();
		}

		camel_test_end ();
	}

	return 0;
}
Ejemplo n.º 9
0
void camel_test_fatal (void)
{
	camel_test_pull ();
}
Ejemplo n.º 10
0
gint
main (gint argc,
      gchar **argv)
{
	CamelFolder *f1, *f2;
	CamelStore *store;
	CamelService *service;
	CamelFolderInfo *fi;
	GError *error = NULL;

	camel_test_init (argc, argv);
	camel_test_provider_init (1, local_drivers);

	/* clear out any camel-test data */
	system ("/bin/rm -rf /tmp/camel-test");

	session = camel_test_session_new ("/tmp/camel-test");
	service = camel_session_add_service (
		session, "test-uid",
		"maildir:///tmp/camel-test/maildir",
		CAMEL_PROVIDER_STORE, NULL);
	store = CAMEL_STORE (service);

	camel_test_start ("Maildir backward compatability tests");

	camel_test_push ("./ prefix path, one level");
	f1 = camel_store_get_folder_sync (
		store, "testbox",
		CAMEL_STORE_FOLDER_CREATE, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	f2 = camel_store_get_folder_sync (
		store, "./testbox",
		CAMEL_STORE_FOLDER_CREATE, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	check (f1 == f2);
	check_unref (f2, 2);
	check_unref (f1, 1);
	camel_test_pull ();

	camel_test_push ("./ prefix path, one level, no create");
	f1 = camel_store_get_folder_sync (
		store, "testbox2",
		CAMEL_STORE_FOLDER_CREATE, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	f2 = camel_store_get_folder_sync (
		store, "./testbox2", 0, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	check (f1 == f2);
	check_unref (f2, 2);
	check_unref (f1, 1);
	camel_test_pull ();

	camel_test_push ("./ prefix path, two levels");
	f1 = camel_store_get_folder_sync (
		store, "testbox/foo",
		CAMEL_STORE_FOLDER_CREATE, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	f2 = camel_store_get_folder_sync (
		store, "./testbox/foo",
		CAMEL_STORE_FOLDER_CREATE, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	check (f1 == f2);
	check_unref (f2, 2);
	check_unref (f1, 1);
	camel_test_pull ();

	camel_test_push ("'.' == Inbox");
	f2 = camel_store_get_inbox_folder_sync (store, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	f1 = camel_store_get_folder_sync (store, ".", 0, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	check (f1 == f2);
	check_unref (f2, 2);
	check_unref (f1, 1);
	camel_test_pull ();

	camel_test_push ("folder info, recursive");
	fi = camel_store_get_folder_info_sync (
		store, "",
		CAMEL_STORE_FOLDER_INFO_RECURSIVE,
		NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	check (fi != NULL);
	check_fi (fi, fi_list_1, G_N_ELEMENTS (fi_list_1));
	camel_test_pull ();

	camel_test_push ("folder info, flat");
	fi = camel_store_get_folder_info_sync (store, "", 0, NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	check (fi != NULL);
	check_fi (fi, fi_list_2, G_N_ELEMENTS (fi_list_2));
	camel_test_pull ();

	camel_test_push ("folder info, recursive, non root");
	fi = camel_store_get_folder_info_sync (
		store, "testbox",
		CAMEL_STORE_FOLDER_INFO_RECURSIVE,
		NULL, &error);
	check_msg (error == NULL, "%s", error->message);
	g_clear_error (&error);
	check (fi != NULL);
	check_fi (fi, fi_list_3, G_N_ELEMENTS (fi_list_3));
	camel_test_pull ();

	check_unref (store, 1);
	check_unref (session, 1);

	camel_test_end ();

	return 0;
}
Ejemplo n.º 11
0
gint main (gint argc, gchar **argv)
{
	CamelSession *session;
	CamelSMimeContext *ctx;
	GError **error;
	CamelCipherValidity *valid;
	CamelStream *stream1, *stream2, *stream3;
	GPtrArray *recipients;
	GByteArray *buf;
	gchar *before, *after;

	camel_test_init (argc, argv);

	ex = camel_exception_new ();

	/* clear out any camel-test data */
	system ("/bin/rm -rf /tmp/camel-test");

	session = camel_test_session_new ("/tmp/camel-test");

	ctx = camel_smime_context_new (session);

	camel_test_start ("Test of S/MIME PKCS7 functions");

	stream1 = camel_stream_mem_new ();
	camel_stream_write (stream1, "Hello, I am a test stream.", 25);
	g_seekable_seek (G_SEEKABLE (stream1), 0, G_SEEK_SET, NULL, NULL);

	stream2 = camel_stream_mem_new ();

	camel_test_push ("PKCS7 signing");
	camel_smime_sign (ctx, "*****@*****.**", CAMEL_CIPHER_HASH_SHA1,
			  stream1, stream2, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	camel_test_pull ();

	camel_exception_clear (ex);

	camel_test_push ("PKCS7 verify");
	g_seekable_seek (G_SEEKABLE (stream1), 0, G_SEEK_SET, NULL, NULL);
	g_seekable_seek (G_SEEKABLE (stream2), 0, G_SEEK_SET, NULL, NULL);
	valid = camel_smime_verify (ctx, CAMEL_CIPHER_HASH_SHA1, stream1, stream2, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	check_msg (camel_cipher_validity_get_valid (valid), "%s", camel_cipher_validity_get_description (valid));
	camel_cipher_validity_free (valid);
	camel_test_pull ();

	g_object_unref (stream1);
	g_object_unref (stream2);

	stream1 = camel_stream_mem_new ();
	stream2 = camel_stream_mem_new ();
	stream3 = camel_stream_mem_new ();

	camel_stream_write (stream1, "Hello, I am a test of encryption/decryption.", 44);
	g_seekable_seek (G_SEEKABLE (stream1), 0, G_SEEK_SET, NULL, NULL);

	camel_exception_clear (ex);

	camel_test_push ("PKCS7 encrypt");
	recipients = g_ptr_array_new ();
	g_ptr_array_add (recipients, "*****@*****.**");
	camel_smime_encrypt (ctx, FALSE, "*****@*****.**", recipients,
			     stream1, stream2, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	g_ptr_array_free (recipients, TRUE);
	camel_test_pull ();

	g_seekable_seek (G_SEEKABLE (stream2), 0, G_SEEK_SET, NULL, NULL);
	camel_exception_clear (ex);

	camel_test_push ("PKCS7 decrypt");
	camel_smime_decrypt (ctx, stream2, stream3, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	buf = CAMEL_STREAM_MEM (stream1)->buffer;
	before = g_strndup (buf->data, buf->len);
	buf = CAMEL_STREAM_MEM (stream3)->buffer;
	after = g_strndup (buf->data, buf->len);
	check_msg (string_equal (before, after), "before = '%s', after = '%s'", before, after);
	g_free (before);
	g_free (after);
	camel_test_pull ();

	g_object_unref (ctx);
	g_object_unref (session);

	camel_test_end ();

	return 0;
}
Ejemplo n.º 12
0
gint main (gint argc, gchar **argv)
{
	CamelSession *session;
	CamelCipherContext *ctx;
	GError **error;
	CamelCipherValidity *valid;
	CamelMimePart *mime_part;
	CamelMultipartSigned *mps;
	CamelMultipartEncrypted *mpe;
	GPtrArray *recipients;
	gint ret;

	camel_test_init (argc, argv);

	/* clear out any camel-test data */
	system ("/bin/rm -rf /tmp/camel-test");
	system ("/bin/mkdir /tmp/camel-test");
	setenv ("GNUPGHOME", "/tmp/camel-test/.gnupg", 1);

	/* import the gpg keys */
	if ((ret = system ("gpg < /dev/null > /dev/null 2>&1")) == -1)
		return 77;
	else if (WEXITSTATUS (ret) == 127)
		return 77;

	system ("gpg --import ../data/camel-test.gpg.pub > /dev/null 2>&1");
	system ("gpg --import ../data/camel-test.gpg.sec > /dev/null 2>&1");

	session = camel_pgp_session_new ("/tmp/camel-test");

	ex = camel_exception_new ();

	ctx = camel_gpg_context_new (session);
	camel_gpg_context_set_always_trust (CAMEL_GPG_CONTEXT (ctx), TRUE);

	camel_test_start ("Test of PGP/MIME functions");

	mime_part = camel_mime_part_new ();
	camel_mime_part_set_content (mime_part, test_msg, strlen (test_msg), "text/plain");
	camel_mime_part_set_description (mime_part, "Test of PGP/MIME multipart/signed stuff");

	camel_test_push ("PGP/MIME signing");
	mps = camel_multipart_signed_new ();
	camel_multipart_signed_sign (mps, ctx, mime_part, "*****@*****.**", CAMEL_CIPHER_HASH_SHA1, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	camel_test_pull ();

	g_object_unref (mime_part);
	camel_exception_clear (ex);

	camel_test_push ("PGP/MIME verify");
	valid = camel_multipart_signed_verify (mps, ctx, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	check_msg (camel_cipher_validity_get_valid (valid), "%s", camel_cipher_validity_get_description (valid));
	camel_cipher_validity_free (valid);
	camel_test_pull ();

	g_object_unref (mps);
	camel_exception_clear (ex);

	mime_part = camel_mime_part_new ();
	camel_mime_part_set_content (mime_part, test_msg, strlen (test_msg), "text/plain");
	camel_mime_part_set_description (mime_part, "Test of PGP/MIME multipart/encrypted stuff");

	camel_test_push ("PGP/MIME encrypt");
	recipients = g_ptr_array_new ();
	g_ptr_array_add (recipients, "*****@*****.**");

	mpe = camel_multipart_encrypted_new ();
	camel_multipart_encrypted_encrypt (mpe, mime_part, ctx, "*****@*****.**", recipients, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	g_ptr_array_free (recipients, TRUE);
	camel_test_pull ();

	camel_exception_clear (ex);
	g_object_unref (mime_part);

	camel_test_push ("PGP/MIME decrypt");
	mime_part = camel_multipart_encrypted_decrypt (mpe, ctx, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	g_object_unref (mime_part);
	g_object_unref (mpe);
	camel_test_pull ();

	g_object_unref (ctx);
	g_object_unref (session);

	camel_test_end ();

	return 0;
}
static void
test_filter(CamelMimeFilter *f, const char *inname, const char *outname)
{
	CamelStreamMem *in, *out;
	CamelStream *indisk, *outdisk, *filter;
	int id;

	camel_test_push("Data file `%s'", inname);

	camel_test_push("setup");

	indisk = camel_stream_fs_new_with_name(inname, O_RDONLY, 0);
	check(indisk);
	outdisk = camel_stream_fs_new_with_name(outname, O_RDONLY, 0);
	check(outdisk);

	out = (CamelStreamMem *)camel_stream_mem_new();
	check(camel_stream_write_to_stream(outdisk, (CamelStream *)out) > 0);

	camel_test_pull();

	camel_test_push("reading through filter stream");

	in = (CamelStreamMem *)camel_stream_mem_new();

	filter = (CamelStream *)camel_stream_filter_new_with_stream(indisk);
	check_count(indisk, 2);
	id = camel_stream_filter_add((CamelStreamFilter *)filter, f);
	check_count(f, 2);

	check(camel_stream_write_to_stream(filter, (CamelStream *)in) > 0);
	check_msg(in->buffer->len == out->buffer->len
		  && memcmp(in->buffer->data, out->buffer->data, in->buffer->len) == 0,
		  "Buffer content mismatch, %d != %d, in = '%.*s' != out = '%.*s'", in->buffer->len, out->buffer->len,
		  in->buffer->len, in->buffer->data, out->buffer->len, out->buffer->data);

	camel_test_pull();

	camel_stream_filter_remove((CamelStreamFilter *)filter, id);
	check_count(f, 1);
	camel_mime_filter_reset(f);

	check_unref(filter, 1);
	check_count(indisk, 1);
	check_count(f, 1);
	check_unref(in, 1);

	check(camel_stream_reset(indisk) == 0);

	camel_test_push("writing through filter stream");

	in = (CamelStreamMem *)camel_stream_mem_new();
	filter = (CamelStream *)camel_stream_filter_new_with_stream((CamelStream *)in);
	check_count(in, 2);
	id = camel_stream_filter_add((CamelStreamFilter *)filter, f);
	check_count(f, 2);

	check(camel_stream_write_to_stream(indisk, filter) > 0);
	check(camel_stream_flush(filter) == 0);
	check_msg(in->buffer->len == out->buffer->len
		  && memcmp(in->buffer->data, out->buffer->data, in->buffer->len) == 0,
		  "Buffer content mismatch, %d != %d, in = '%.*s' != out = '%.*s'", in->buffer->len, out->buffer->len,
		  in->buffer->len, in->buffer->data, out->buffer->len, out->buffer->data);

	camel_stream_filter_remove((CamelStreamFilter *)filter, id);
	check_unref(filter, 1);
	check_unref(in, 1);
	check_unref(indisk, 1);
	check_unref(outdisk, 1);
	check_unref(out, 1);

	camel_test_pull();

	camel_test_pull();
}
Ejemplo n.º 14
0
int main (int argc, char **argv)
{
	CamelSession *session;
	CamelCipherContext *ctx;
	CamelException *ex;
	CamelCipherValidity *valid;
	CamelStream *stream1, *stream2;
	struct _CamelMimePart *sigpart, *conpart, *encpart, *outpart;
	CamelDataWrapper *dw;
	GPtrArray *recipients;
	GByteArray *buf;
	char *before, *after;
	int ret;

	if (getenv("CAMEL_TEST_GPG") == NULL)
		return 77;

	camel_test_init (argc, argv);

	/* clear out any camel-test data */
	system ("/bin/rm -rf /tmp/camel-test");
	system ("/bin/mkdir /tmp/camel-test");
	setenv ("GNUPGHOME", "/tmp/camel-test/.gnupg", 1);

	/* import the gpg keys */
	if ((ret = system ("gpg < /dev/null > /dev/null 2>&1")) == -1)
		return 77;
	else if (WEXITSTATUS (ret) == 127)
		return 77;

	g_message ("gpg --import " TEST_DATA_DIR "/camel-test.gpg.pub > /dev/null 2>&1");
	system ("gpg --import " TEST_DATA_DIR "/camel-test.gpg.pub > /dev/null 2>&1");
	g_message ("gpg --import " TEST_DATA_DIR "/camel-test.gpg.sec > /dev/null 2>&1");
	system ("gpg --import " TEST_DATA_DIR "/camel-test.gpg.sec > /dev/null 2>&1");

	session = camel_pgp_session_new ("/tmp/camel-test");

	ex = camel_exception_new ();

	ctx = camel_gpg_context_new (session);
	camel_gpg_context_set_always_trust (CAMEL_GPG_CONTEXT (ctx), TRUE);

	camel_test_start ("Test of PGP functions");

	stream1 = camel_stream_mem_new ();
	camel_stream_write (stream1, "Hello, I am a test stream.\n", 27);
	camel_stream_reset (stream1);

	conpart = camel_mime_part_new();
	dw = camel_data_wrapper_new();
	camel_data_wrapper_construct_from_stream(dw, stream1);
	camel_medium_set_content_object((CamelMedium *)conpart, dw);
	camel_object_unref(stream1);
	camel_object_unref(dw);

	sigpart = camel_mime_part_new();

	camel_test_push ("PGP signing");
	camel_cipher_sign (ctx, "*****@*****.**", CAMEL_CIPHER_HASH_SHA1, conpart, sigpart, ex);
	if (camel_exception_is_set(ex)) {
		printf("PGP signing failed assuming non-functional environment\n%s", camel_exception_get_description (ex));
		camel_test_pull();
		return 77;
	}
	camel_test_pull ();

	camel_exception_clear (ex);

	camel_test_push ("PGP verify");
	valid = camel_cipher_verify (ctx, sigpart, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	check_msg (camel_cipher_validity_get_valid (valid), "%s", camel_cipher_validity_get_description (valid));
	camel_cipher_validity_free (valid);
	camel_test_pull ();

	camel_object_unref(conpart);
	camel_object_unref(sigpart);

	stream1 = camel_stream_mem_new ();
	camel_stream_write (stream1, "Hello, I am a test of encryption/decryption.", 44);
	camel_stream_reset (stream1);

	conpart = camel_mime_part_new();
	dw = camel_data_wrapper_new();
	camel_stream_reset(stream1);
	camel_data_wrapper_construct_from_stream(dw, stream1);
	camel_medium_set_content_object((CamelMedium *)conpart, dw);
	camel_object_unref(stream1);
	camel_object_unref(dw);

	encpart = camel_mime_part_new();

	camel_exception_clear (ex);

	camel_test_push ("PGP encrypt");
	recipients = g_ptr_array_new ();
	g_ptr_array_add (recipients, "*****@*****.**");
	camel_cipher_encrypt (ctx, "*****@*****.**", recipients, conpart, encpart, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	g_ptr_array_free (recipients, TRUE);
	camel_test_pull ();

	camel_exception_clear (ex);

	camel_test_push ("PGP decrypt");
	outpart = camel_mime_part_new();
	valid = camel_cipher_decrypt (ctx, encpart, outpart, ex);
	check_msg (!camel_exception_is_set (ex), "%s", camel_exception_get_description (ex));
	check_msg (valid->encrypt.status == CAMEL_CIPHER_VALIDITY_ENCRYPT_ENCRYPTED, "%s", valid->encrypt.description);

	stream1 = camel_stream_mem_new();
	stream2 = camel_stream_mem_new();

	camel_data_wrapper_write_to_stream((CamelDataWrapper *)conpart, stream1);
	camel_data_wrapper_write_to_stream((CamelDataWrapper *)outpart, stream2);

	buf = CAMEL_STREAM_MEM (stream1)->buffer;
	before = g_strndup (buf->data, buf->len);
	buf = CAMEL_STREAM_MEM (stream2)->buffer;
	after = g_strndup (buf->data, buf->len);
	check_msg (string_equal (before, after), "before = '%s', after = '%s'", before, after);
	g_free (before);
	g_free (after);

	camel_object_unref(stream1);
	camel_object_unref(stream2);
	camel_object_unref(conpart);
	camel_object_unref(encpart);
	camel_object_unref(outpart);

	camel_test_pull ();

	camel_object_unref (CAMEL_OBJECT (ctx));
	camel_object_unref (CAMEL_OBJECT (session));

	camel_test_end ();

	return 0;
}