Exemple #1
0
TEST_F(kodFileTest, WriteFileWithMultipleEntries) {
	kod_db_file = estrdup(CreatePath("kod-output-multiple", OUTPUT_DIR).c_str());

	add_entry("example.com", "RATE");
	add_entry("192.0.2.1", "DENY");
	add_entry("192.0.2.5", "RSTR");

	/*
	 * Manipulate timestamps. This is a bit of a hack, ideally these
	 * tests should not care about the internal representation.
	 */
	kod_db[0]->timestamp = 0xabcd;
	kod_db[1]->timestamp = 0xabcd;
	kod_db[2]->timestamp = 0xabcd;

	write_kod_db();

	// Open file and compare sizes and content.
	ifstream actual(kod_db_file, ios::binary);
	ifstream expected(CreatePath("kod-expected-multiple", INPUT_DIR).c_str());
	ASSERT_TRUE(actual.good());
	ASSERT_TRUE(expected.good());
	
	ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));

	CompareFileContent(expected, actual);
}
Exemple #2
0
void
test_WriteFileWithMultipleEntries(void) {
	kod_db_file = estrdup("kod-output-multiple");
	add_entry("example.com", "RATE");
	add_entry("192.0.2.1", "DENY");
	add_entry("192.0.2.5", "RSTR");

	//
	// Manipulate timestamps. This is a bit of a hack, ideally these
	// tests should not care about the internal representation.
	//
	kod_db[0]->timestamp = 0xabcd;
	kod_db[1]->timestamp = 0xabcd;
	kod_db[2]->timestamp = 0xabcd;

	write_kod_db();

	// Open file and compare sizes and content.
	FILE * actual = fopen(kod_db_file, "rb");
	FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");

	TEST_ASSERT_NOT_NULL(actual);
	TEST_ASSERT_NOT_NULL(expected);


	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));

	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
}
Exemple #3
0
void
atexit_write_kod_db(void)
{
#ifdef WORK_FORK
	if (worker_process)
		return;
#endif
	write_kod_db();
}
Exemple #4
0
TEST_F(kodFileTest, WriteEmptyFile) {
	kod_db_file = estrdup(CreatePath("kod-output-blank", OUTPUT_DIR).c_str());

	write_kod_db();

	// Open file and ensure that the filesize is 0 bytes.
	std::ifstream is(kod_db_file, std::ios::binary);
	ASSERT_FALSE(is.fail());
	
	EXPECT_EQ(0, GetFileSize(is));

	is.close();
}
Exemple #5
0
void
test_WriteEmptyFile(void) {
	kod_db_file = estrdup("kod-output-blank");
	write_kod_db();

	// Open file and ensure that the filesize is 0 bytes.
	FILE * is = fopen(kod_db_file, "rb");
	TEST_ASSERT_NOT_NULL(is);

	TEST_ASSERT_EQUAL(0, GetFileSize(is));

	fclose(is);
}
Exemple #6
0
TEST_F(kodFileTest, WriteFileWithSingleEntry) {
	kod_db_file = estrdup(CreatePath("kod-output-single", OUTPUT_DIR).c_str());

	add_entry("host1", "DENY");

	/* Here we must manipulate the timestamps, so they match the one in
	 * the expected file.
	 */
	kod_db[0]->timestamp = 1;

	write_kod_db();

	// Open file and compare sizes.
	ifstream actual(kod_db_file, ios::binary);
	ifstream expected(CreatePath("kod-expected-single", INPUT_DIR).c_str());
	ASSERT_TRUE(actual.good());
	ASSERT_TRUE(expected.good());

	ASSERT_EQ(GetFileSize(expected), GetFileSize(actual));

	CompareFileContent(expected, actual);
}
Exemple #7
0
void
test_WriteFileWithSingleEntry(void) {
	kod_db_file = estrdup("kod-output-single"); 
	add_entry("host1", "DENY");

	// Here we must manipulate the timestamps, so they match the one in
	// the expected file.

	kod_db[0]->timestamp = 1;

	write_kod_db();

	// Open file and compare sizes.
	FILE * actual = fopen(kod_db_file, "rb");
	FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");

	TEST_ASSERT_NOT_NULL(actual);
	TEST_ASSERT_NOT_NULL(expected);

	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
	
	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
}
Exemple #8
0
void
kod_init_kod_db(
	const char *	db_file,
	int		readonly
	)
{
	/*
	 * Max. of 254 characters for hostname, 10 for timestamp, 4 for
	 * kisscode, 2 for spaces, 1 for \n, and 1 for \0
	 */
	char fbuf[254+10+4+2+1+1];
	FILE *db_s;
	int a, b, sepc, len;
	unsigned long long ull;
	char *str_ptr;
	char error = 0;

	TRACE(2, ("Initializing KOD DB...\n"));

	kod_db_file = estrdup(db_file);

	db_s = fopen(db_file, "r");

	if (NULL == db_s) {
		msyslog(LOG_WARNING, "kod_init_kod_db(): Cannot open KoD db file %s: %m",
			db_file);

		return;
	}

	if (debug)
		printf("Starting to read KoD file %s...\n", db_file);
	/* First let's see how many entries there are and check for right syntax */

	while (!feof(db_s) && NULL != fgets(fbuf, sizeof(fbuf), db_s)) {

		/* ignore blank lines */
		if ('\n' == fbuf[0])
			continue;

		sepc = 0;
		len = strlen(fbuf);
		for (a = 0; a < len; a++) {
			if (' ' == fbuf[a])
				sepc++;

			if ('\n' == fbuf[a]) {
				if (sepc != 2) {
					if (strcmp(db_file, "/dev/null"))
						msyslog(LOG_DEBUG,
							"Syntax error in KoD db file %s in line %i (missing space)",
							db_file,
							kod_db_cnt + 1);
					fclose(db_s);
					return;
				}
				sepc = 0;
				kod_db_cnt++;
			}
		}
	}

	if (0 == kod_db_cnt) {
		TRACE(2, ("KoD DB %s empty.\n", db_file));
		goto wrapup;
	}

	TRACE(2, ("KoD DB %s contains %d entries, reading...\n", db_file, kod_db_cnt));

	rewind(db_s);

	kod_db = emalloc(sizeof(kod_db[0]) * kod_db_cnt);

	/* Read contents of file */
	for (b = 0; 
	     !feof(db_s) && !ferror(db_s) && b < kod_db_cnt;
	     b++) {

		str_ptr = fgets(fbuf, sizeof(fbuf), db_s);
		if (NULL == str_ptr) {
			error = 1;
			break;
		}

		/* ignore blank lines */
		if ('\n' == fbuf[0]) {
			b--;
			continue;
		}

		kod_db[b] = emalloc(sizeof(*kod_db[b]));

		if (3 != sscanf(fbuf, "%llx %4s %254s", &ull,
		    kod_db[b]->type, kod_db[b]->hostname)) {

			free(kod_db[b]);
			kod_db[b] = NULL;
			error = 1;
			break;
		}

		kod_db[b]->timestamp = (time_t)ull;
	}

	if (ferror(db_s) || error) {
		kod_db_cnt = b;
		msyslog(LOG_WARNING, "An error occured while parsing the KoD db file %s",
			db_file);
		fclose(db_s);

		return;
	}

    wrapup:
	fclose(db_s);
	for (a = 0; a < kod_db_cnt; a++)
		TRACE(2, ("KoD entry %d: %s at %llx type %s\n", a,
			  kod_db[a]->hostname,
			  (unsigned long long)kod_db[a]->timestamp,
			  kod_db[a]->type));

	if (!readonly && write_kod_db())
		atexit(&atexit_write_kod_db);
}