Esempio n. 1
0
static void test_psl(void)
{
	FILE *fp;
	psl_ctx_t *psl, *psl3, *psl4, *psl5;
	const psl_ctx_t *psl2;
	int type = 0;
	char buf[256], *linep, *p;

	psl = psl_load_file(PSL_FILE); /* PSL_FILE can be set by ./configure --with-psl-file=[PATH] */
	printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));

	psl2 = psl_builtin();
	printf("builtin PSL has %d suffixes and %d exceptions\n", psl_suffix_count(psl2), psl_suffix_exception_count(psl2));

	if (!(psl3 = psl_load_file(PSL_DAFSA))) {
		fprintf(stderr, "Failed to load 'psl.dafsa'\n");
		failed++;
	}

	if (!(psl4 = psl_load_file(PSL_ASCII_DAFSA))) {
		fprintf(stderr, "Failed to load 'psl_ascii.dafsa'\n");
		failed++;
	}

	psl5 = psl_latest("psl.dafsa");

	if ((fp = fopen(PSL_FILE, "r"))) {
#ifdef HAVE_CLOCK_GETTIME
		clock_gettime(CLOCK_REALTIME, &ts1);
#endif

		while ((linep = fgets(buf, sizeof(buf), fp))) {
			while (_isspace_ascii(*linep)) linep++; /* ignore leading whitespace */
			if (!*linep) continue; /* skip empty lines */

			if (*linep == '/' && linep[1] == '/') {
				if (!type) {
					if (strstr(linep + 2, "===BEGIN ICANN DOMAINS==="))
						type = PSL_TYPE_ICANN;
					else if (!type && strstr(linep + 2, "===BEGIN PRIVATE DOMAINS==="))
						type = PSL_TYPE_PRIVATE;
				}
				else if (type == PSL_TYPE_ICANN && strstr(linep + 2, "===END ICANN DOMAINS==="))
					type = 0;
				else if (type == PSL_TYPE_PRIVATE && strstr(linep + 2, "===END PRIVATE DOMAINS==="))
					type = 0;

				continue; /* skip comments */
			}

			/* parse suffix rule */
			for (p = linep; *linep && !_isspace_ascii(*linep);) linep++;
			*linep = 0;

			test_psl_entry(psl, p, type);

			if (psl2)
				test_psl_entry(psl2, p, type);

			if (psl3)
				test_psl_entry(psl3, p, type);

			if (psl4)
				test_psl_entry(psl4, p, type);

			if (psl5)
				test_psl_entry(psl5, p, type);
		}

#ifdef HAVE_CLOCK_GETTIME
		clock_gettime(CLOCK_REALTIME, &ts2);
#endif
		fclose(fp);
	} else {
		printf("Failed to open %s\n", PSL_FILE);
		failed++;
	}

	psl_free(psl5);
	psl_free(psl4);
	psl_free(psl3);
	psl_free((psl_ctx_t *)psl2);
	psl_free(psl);
}
Esempio n. 2
0
static void test_psl(void)
{
	FILE *fp;
	psl_ctx_t *psl;
	int result;
	char buf[256], domain[64], *linep, *p;

	psl = psl_load_file(PSL_FILE); /* PSL_FILE can be set by ./configure --with-psl-file=[PATH] */

	printf("loaded %d suffixes and %d exceptions\n", psl_suffix_count(psl), psl_suffix_exception_count(psl));

	if ((fp = fopen(PSL_FILE, "r"))) {
		while ((linep = fgets(buf, sizeof(buf), fp))) {
			while (_isspace_ascii(*linep)) linep++; /* ignore leading whitespace */
			if (!*linep) continue; /* skip empty lines */

			if (*linep == '/' && linep[1] == '/')
				continue; /* skip comments */

			/* parse suffix rule */
			for (p = linep; *linep && !_isspace_ascii(*linep);) linep++;
			*linep = 0;

			if (*p == '!') { /* an exception to a wildcard, e.g. !www.ck (wildcard is *.ck) */
				if ((result = psl_is_public_suffix(psl, p + 1))) {
					failed++;
					printf("psl_is_public_suffix(%s)=%d (expected 0)\n", p, result);
				} else ok++;

				if (!(result = psl_is_public_suffix(psl, strchr(p, '.') + 1))) {
					failed++;
					printf("psl_is_public_suffix(%s)=%d (expected 1)\n", strchr(p, '.') + 1, result);
				} else ok++;
			}
			else if (*p == '*') { /* a wildcard, e.g. *.ck */
				if (!(result = psl_is_public_suffix(psl, p + 1))) {
					failed++;
					printf("psl_is_public_suffix(%s)=%d (expected 1)\n", p + 1, result);
				} else ok++;

				*p = 'x';
				if (!(result = psl_is_public_suffix(psl, p))) {
					failed++;
					printf("psl_is_public_suffix(%s)=%d (expected 1)\n", p, result);
				} else ok++;
			}
			else {
				if (!(result = psl_is_public_suffix(psl, p))) {
					failed++;
					printf("psl_is_public_suffix(%s)=%d (expected 1)\n", p, result);
				} else ok++;

				snprintf(domain, sizeof(domain), "xxxx.%s", p);
				if ((result = psl_is_public_suffix(psl, domain))) {
					failed++;
					printf("psl_is_public_suffix(%s)=%d (expected 0)\n", domain, result);
				} else ok++;
			}
		}

		fclose(fp);
	} else {
		printf("Failed to open %s\n", PSL_FILE);
		failed++;
	}

	psl_free(psl);
}