/*
 * purple
 *
 * Purple is the legal property of its developers, whose names are too numerous
 * to list here.  Please refer to the COPYRIGHT file distributed with this
 * source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 */
#include <glib.h>

#include <purple.h>

/******************************************************************************
 * email tests
 *****************************************************************************/
/*
 * Many of the valid and invalid email addresses lised below are from
 * http://fightingforalostcause.net/misc/2006/compare-email-regex.php
 */
static void
test_util_email_is_valid(void) {
	const char *valid_emails[] = {
		"purple-devel@lists.sf.net",
		"l3tt3rsAndNumb3rs@domain.com",
		"has-dash@domain.com",
		"hasApostrophe.o'leary@domain.org",
		"uncommonTLD@domain.museum",
		"uncommonTLD@domain.travel",
		"uncommonTLD@domain.mobi",
		"countryCodeTLD@domain.uk",
		"countryCodeTLD@domain.rw",
		"lettersInDomain@911.com",
		"underscore_inLocal@domain.net",
		"IPInsteadOfDomain@127.0.0.1",
		"IPAndPort@127.0.0.1:25",
		"subdomain@sub.domain.com",
		"local@dash-inDomain.com",
		"dot.inLocal@foo.com",
		"a@singleLetterLocal.org",
		"singleLetterDomain@x.org",
		"&*=?^+{}'~@validCharsInLocal.net",
		"foor@bar.newTLD", /* codespell:ignore */
		"HenryTheGreatWhiteCricket@live.ca",
		"HenryThe__WhiteCricket@hotmail.com"
	};

	for(size_t i = 0; i < G_N_ELEMENTS(valid_emails); i++) {
		g_assert_true(purple_email_is_valid(valid_emails[i]));
	}
}

static void
test_util_email_is_invalid(void) {
	const char *invalid_emails[] = {
		"purple-devel@@lists.sf.net",
		"purple@devel@lists.sf.net",
		"purple-devel",
		"purple-devel@",
		"@lists.sf.net",
		"totally bogus",
		"@missingLocal.org",
		"missingatSign.net",
		"two@@signs.com",
		"colonButNoPort@127.0.0.1:",
	};

	for(size_t i = 0; i < G_N_ELEMENTS(invalid_emails); i++) {
		g_assert_false(purple_email_is_valid(invalid_emails[i]));
	}
}

/******************************************************************************
 * MAIN
 *****************************************************************************/
int
main(int argc, char **argv) {
	g_test_init(&argc, &argv, NULL);
	g_test_set_nonfatal_assertions();

	g_test_add_func("/util/email/is valid",
	                test_util_email_is_valid);
	g_test_add_func("/util/email/is invalid",
	                test_util_email_is_invalid);

	return g_test_run();
}
