Пример #1
0
void testParamsOk(CuTest *tc) {
    struct NameValuePair fromParam  = {"from", "1257120000", NULL};       // 2009-11-02
    struct NameValuePair toParam    = {"to",   "1257292800", &fromParam}; // 2009-11-04
    struct NameValuePair groupParam = {"group", "5", &toParam};           // Total
    struct Request req = {"GET", "/query", &groupParam, NULL};

 // The query range covers the second, third and fourth rows only
    emptyDb();
    addDbRow(makeTs("2009-11-01 12:00:00"), 3600, NULL,  1,  1, "");
    addDbRow(makeTs("2009-11-02 12:00:00"), 3600, NULL,  2,  2, ""); // Match
    addDbRow(makeTs("2009-11-03 12:00:00"), 3600, NULL,  4,  4, ""); // Match
    addDbRow(makeTs("2009-11-04 12:00:00"), 3600, NULL,  8,  8, ""); // Match
    addDbRow(makeTs("2009-11-05 12:00:00"), 3600, NULL, 16, 16, "");

    int tmpFd = makeTmpFile();
    processQueryRequest(tmpFd, &req);

    char* result = readTmpFile();

    // The 'ts' value = 2009-11-05 00:00:00, ie the end of the date range covered by the query
    // The 'dr' value = 3 * 24 * 3600, ie the number of seconds in 3 days
    CuAssertStrEquals(tc,
        "HTTP/1.0 200 OK" HTTP_EOL
        "Content-Type: application/json" HTTP_EOL
        "Server: BitMeterOS " VERSION " Web Server" HTTP_EOL
        "Date: Sun, 08 Nov 2009 10:00:00 +0000" HTTP_EOL
        "Connection: Close" HTTP_EOL HTTP_EOL
        "[{\"dl\": 14,\"ul\": 14,\"ts\": 1257379200,\"dr\": 259200}]"
    , result);
}
Пример #2
0
void testGroupByDay(CuTest *tc) {
    struct NameValuePair fromParam  = {"from", "0", NULL};
    struct NameValuePair toParam    = {"to",   "1258281927", &fromParam};
    struct NameValuePair groupParam = {"group", "2", &toParam};
    struct Request req = {"GET", "/query", &groupParam, NULL};

    emptyDb();
    addDbRow(makeTs("2009-11-01 10:00:00"), 3600, NULL,  1,  1, "");
    addDbRow(makeTs("2009-11-01 11:00:00"), 3600, NULL,  2,  2, "");
    addDbRow(makeTs("2009-11-01 12:00:00"), 3600, NULL,  4,  4, "");
    addDbRow(makeTs("2009-11-02 09:00:00"), 3600, NULL,  8,  8, "");
    addDbRow(makeTs("2009-11-02 23:00:00"), 3600, NULL, 16, 16, "");

    int tmpFd = makeTmpFile();
    processQueryRequest(tmpFd, &req);

    char* result = readTmpFile();

    CuAssertStrEquals(tc,
        "HTTP/1.0 200 OK" HTTP_EOL
        "Content-Type: application/json" HTTP_EOL
        "Server: BitMeterOS " VERSION " Web Server" HTTP_EOL
        "Date: Sun, 08 Nov 2009 10:00:00 +0000" HTTP_EOL
        "Connection: Close" HTTP_EOL HTTP_EOL
        "[{\"dl\": 7,\"ul\": 7,\"ts\": 1257120000,\"dr\": 54000},{\"dl\": 24,\"ul\": 24,\"ts\": 1257206400,\"dr\": 86400}]"
    , result);
}
Пример #3
0
void testGroupByDayCsv(CuTest *tc) {
    struct NameValuePair fromParam  = {"from", "0", NULL};
    struct NameValuePair toParam    = {"to",   "1258281927", &fromParam};
    struct NameValuePair groupParam = {"group", "2", &toParam};
    struct NameValuePair csvParam   = {"csv", "1", &groupParam};
    struct Request req = {"GET", "/query", &csvParam, NULL};

    emptyDb();
    addDbRow(makeTs("2009-11-01 10:00:00"), 3600, NULL,  1,  1, "");
    addDbRow(makeTs("2009-11-01 11:00:00"), 3600, NULL,  2,  2, "");
    addDbRow(makeTs("2009-11-01 12:00:00"), 3600, NULL,  4,  4, "");
    addDbRow(makeTs("2009-11-02 09:00:00"), 3600, NULL,  8,  8, "");
    addDbRow(makeTs("2009-11-02 23:00:00"), 3600, NULL, 16, 16, "");

    int tmpFd = makeTmpFile();
    processQueryRequest(tmpFd, &req);

    char* result = readTmpFile();

    CuAssertStrEquals(tc,
        "HTTP/1.0 200 OK" HTTP_EOL
        "Content-Type: text/csv" HTTP_EOL
        "Server: BitMeterOS " VERSION " Web Server" HTTP_EOL
        "Date: Sun, 08 Nov 2009 10:00:00 +0000" HTTP_EOL
        "Connection: Close" HTTP_EOL
        "Content-Disposition: attachment;filename=bitmeterOsQuery.csv" HTTP_EOL HTTP_EOL
        "2009-11-01 09:00:00,7,7\n"
		"2009-11-02 00:00:00,24,24\n"
    , result);
}
Пример #4
0
void testSummaryWithHostAdapterParams(CuTest *tc) {
    emptyDb();
    addDbRow(makeTs("2008-11-02 12:00:00"), 3600, "eth0",  1,  1, "host1"); // Last year
    addDbRow(makeTs("2008-11-02 12:00:00"), 3600, "eth0",  1,  1, "host2"); // Last year
    addDbRow(makeTs("2009-10-01 12:00:00"), 3600, "eth0",  2,  2, "host1"); // Earlier this year
    addDbRow(makeTs("2009-10-01 12:00:00"), 3600, "eth0",  2,  2, ""); // Earlier this year
    addDbRow(makeTs("2009-11-04 12:00:00"), 3600, "eth0",  4,  4, "host1"); // Earlier this month
    addDbRow(makeTs("2009-11-04 12:00:00"), 3600, "eth0",  4,  4, ""); // Earlier this month
    addDbRow(makeTs("2009-11-08 01:00:00"), 3600, "eth0",  8,  8, "host1"); // Today
    addDbRow(makeTs("2009-11-08 01:00:00"), 3600, "eth0",  8,  8, ""); // Today

    struct NameValuePair param = {"ha", "host1:eth0", NULL};
    struct Request req = {"GET", "/summary", &param, NULL};

    time_t now = makeTs("2009-11-08 10:00:00");
    setTime(now);

    int tmpFd = makeTmpFile();
    processSummaryRequest(tmpFd, &req);

    char* result = readTmpFile();

    CuAssertStrEquals(tc,
        "HTTP/1.0 200 OK" HTTP_EOL
        "Content-Type: application/json" HTTP_EOL
        "Server: BitMeterOS " VERSION " Web Server" HTTP_EOL
        "Date: Sun, 08 Nov 2009 10:00:00 +0000" HTTP_EOL
        "Connection: Close" HTTP_EOL HTTP_EOL
        "{\"today\": {\"dl\": 8,\"ul\": 8,\"ts\": 0,\"dr\": 0}, \"month\": {\"dl\": 12,\"ul\": 12,\"ts\": 0,\"dr\": 0}, \"year\": {\"dl\": 14,\"ul\": 14,\"ts\": 0,\"dr\": 0}, \"total\": {\"dl\": 15,\"ul\": 15,\"ts\": 0,\"dr\": 0}"
        ", \"hosts\": null, \"since\": 1225627200}"
    , result);
}
Пример #5
0
/*!
Do some clean up, and be sure that all output has been read.
*/
void MyProcess::procFinished() {

#if !USE_TEMP_FILE
	if ( bytesAvailable() > 0 ) readStdOut();
#else
	timer.stop();

	if ( temp_file.bytesAvailable() > 0 ) readTmpFile();

	temp_file.close();
#endif
}
Пример #6
0
void GOSTTests::testHash()
{
	// Get an RNG and GOST R 34.11-94 hash instance
	CPPUNIT_ASSERT((rng = CryptoFactory::i()->getRNG()) != NULL);
	CPPUNIT_ASSERT((hash = CryptoFactory::i()->getHashAlgorithm("gost")) != NULL);

	// Generate some random input data
	ByteString b;
	ByteString osslHash, gostHash;

	CPPUNIT_ASSERT(rng->generateRandom(b, 53287));

	// Write it to file
	writeTmpFile(b);

	// Use OpenSSL externally to hash it
#ifndef _WIN32
	CPPUNIT_ASSERT(system("openssl dgst -engine gost -md_gost94 -binary < gost-hashtest.tmp > gost-hashtest-out.tmp 2> /dev/null") == 0);
#else
	CPPUNIT_ASSERT(system("openssl dgst -engine gost -md_gost94 -binary < gost-hashtest.tmp > gost-hashtest-out.tmp 2> nul") == 0);
#endif

	// Read the hash from file
	readTmpFile(osslHash);

	// Now recreate the hash using our implementation in a single operation
	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b));
	CPPUNIT_ASSERT(hash->hashFinal(gostHash));

	CPPUNIT_ASSERT(osslHash == gostHash);

	// Now recreate the hash in a single part operation
	gostHash.wipe();

	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(0, 567)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567, 989)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567 + 989)));
	CPPUNIT_ASSERT(hash->hashFinal(gostHash));

	CPPUNIT_ASSERT(osslHash == gostHash);

	CryptoFactory::i()->recycleHashAlgorithm(hash);

	hash = NULL;
	rng = NULL;
}
Пример #7
0
/*!
Do some clean up, and be sure that all output has been read.
*/
void MyProcess::procFinished() {
	qDebug("MyProcess::procFinished");

#if !USE_TEMP_FILE
	qDebug("MyProcess::procFinished: Bytes available: %ld", bytesAvailable());
	if ( bytesAvailable() > 0 ) readStdOut();
#else
	timer.stop();

	qDebug("MyProcess::procFinished: Bytes available: %ld", temp_file.bytesAvailable());
	if ( temp_file.bytesAvailable() > 0 ) readTmpFile();
	qDebug("MyProcess::procFinished: Bytes available: %ld", temp_file.bytesAvailable());

	temp_file.close();
#endif
}
Пример #8
0
void HashTests::testMD5()
{
	// Get an RNG and MD5 hash instance
	CPPUNIT_ASSERT((rng = CryptoFactory::i()->getRNG()) != NULL);
	CPPUNIT_ASSERT((hash = CryptoFactory::i()->getHashAlgorithm("md5")) != NULL);

	// Generate some random input data
	ByteString b;
	ByteString osslHash, shsmHash;

	CPPUNIT_ASSERT(rng->generateRandom(b, 52237));

	// Write it to file
	writeTmpFile(b);

	// Use OpenSSL externally to hash it
	CPPUNIT_ASSERT(system("cat shsmv2-hashtest.tmp | openssl md5 -binary > shsmv2-hashtest-out.tmp") == 0);

	// Read the hash from file
	readTmpFile(osslHash);

	// Now recreate the hash using our implementation in a single operation
	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b));
	CPPUNIT_ASSERT(hash->hashFinal(shsmHash));

	CPPUNIT_ASSERT(osslHash == shsmHash);

	// Now recreate the hash in a multiple part operation
	shsmHash.wipe();

	CPPUNIT_ASSERT(hash->hashInit());
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(0, 567)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567, 989)));
	CPPUNIT_ASSERT(hash->hashUpdate(b.substr(567 + 989)));
	CPPUNIT_ASSERT(hash->hashFinal(shsmHash));

	CPPUNIT_ASSERT(osslHash == shsmHash);

	CryptoFactory::i()->recycleHashAlgorithm(hash);

	hash = NULL;
	rng = NULL;
}
Пример #9
0
void testMissingParam(CuTest *tc) {
 // The 3 parameters are required, so we should get an HTTP error if they are missing
    struct Request req = {"GET", "/query", NULL, NULL};

    time_t now = makeTs("2009-11-08 10:00:00");
    setTime(now);

    int tmpFd = makeTmpFile();
    processQueryRequest(tmpFd, &req);

    char* result = readTmpFile();

    CuAssertStrEquals(tc,
        "HTTP/1.0 500 Bad/missing parameter" HTTP_EOL
        "Server: BitMeterOS " VERSION " Web Server" HTTP_EOL
        "Date: Sun, 08 Nov 2009 10:00:00 +0000" HTTP_EOL
        "Connection: Close" HTTP_EOL HTTP_EOL
    , result);
}
Пример #10
0
MyProcess::MyProcess(QObject * parent) : QProcess(parent)
{
	clearArguments();
	setProcessChannelMode( QProcess::MergedChannels );
	
#if USE_TEMP_FILE
	temp_file.open(); // Create temporary file
	QString filename = temp_file.fileName();
	setStandardOutputFile( filename );
	temp_file.close();

	connect(&timer, SIGNAL(timeout()), this, SLOT(readTmpFile()) );
#else
	connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()) );
#endif

	connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), 
            this, SLOT(procFinished()) ); 

}
Пример #11
0
MyProcess::MyProcess(QObject * parent) : QProcess(parent)
{
	clearArguments();
	setProcessChannelMode( QProcess::MergedChannels );
	
#if USE_TEMP_FILE
	temp_file.open(); // Create temporary file
	QString filename = temp_file.fileName();
	setStandardOutputFile( filename );
	qDebug("MyProcess::MyProcess: temporary file: %s", filename.toUtf8().data());
	temp_file.close();

	//connect(&temp_file, SIGNAL(readyRead()), this, SLOT(readTmpFile()) );
	connect(&timer, SIGNAL(timeout()), this, SLOT(readTmpFile()) );
#else
	connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(readStdOut()) );
#endif

	connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), 
            this, SLOT(procFinished()) ); 

	// Test splitArguments
	//QStringList l = MyProcess::splitArguments("-opt 1 hello \"56 67\" wssx -ios");
}
Пример #12
0
void DESTests::testCBC()
{
	char testKeys56[][17] =
	{
		"0000000000000000",
		"0102030405060708",
		"4041424344454647",
		"4698436794236871",
		"0940278947239572"
	};

	char testKeys112[][33] =
	{
		"00000000000000000000000000000000",
		"0102030405060708090A0B0C0D0E0F10",
		"404142434445464748494A4B4C4D4E4F",
		"64398647034486943598534703463870",
		"87406984068406984607412103517413"
	};
	
	char testKeys168[][49] =
	{
		"000000000000000000000000000000000000000000000000",
		"0102030405060708090A0B0C0D0E0F101112131415161718",
		"404142434445464748494A4B4C4D4E4F5051525354555657",
		"643906874509874309687459084769847562436043696747",
		"430135460496813044639085714376487549490586439575"
	};

	char testData[][256] =
	{
		"4938673409687134684698438657403986439058740935874395813968496846",
		"549813644389670948567490687546098245665626527788",
		"64398769586792586795867965624526",
		"468376458463264536"
	};

	char testIV[][33] =
	{
		"0000000000000000",
		"0102030405060708",
		"4041424344454647",
		"4693867334098764",
		"6209876098547207"
	};

	for (int i = 0; i < 5; i++)
	{
		char commandLine[2048];

		ByteString keyData56(testKeys56[i]);
		CPPUNIT_ASSERT(keyData56.size() == 8);
		ByteString keyData112(testKeys112[i]);
		CPPUNIT_ASSERT(keyData112.size() == 16);
		ByteString keyData168(testKeys168[i]);
		CPPUNIT_ASSERT(keyData168.size() == 24);

		DESKey desKey56(56);
		CPPUNIT_ASSERT(desKey56.setKeyBits(keyData56));
		DESKey desKey112(112);
		CPPUNIT_ASSERT(desKey112.setKeyBits(keyData112));
		DESKey desKey168(168);
		CPPUNIT_ASSERT(desKey168.setKeyBits(keyData168));

		ByteString IV(testIV[i]);

		for (int j = 0; j < 4; j++)
		{
			ByteString plainText(testData[j]), shsmPlainText;
			ByteString cipherText;
			ByteString shsmCipherText, OB;

			// Test 56-bit key

			// First, use the OpenSSL command line tool to encrypt the test data
			writeTmpFile(plainText);

			sprintf(commandLine, "openssl des-cbc -in shsmv2-destest.tmp -out shsmv2-destest-out.tmp -K %s -iv %s",
				testKeys56[i], testIV[i]);

			CPPUNIT_ASSERT(!system(commandLine));

			readTmpFile(cipherText);

			// Now, do the same thing using our DES implementation
			shsmCipherText.wipe();
			CPPUNIT_ASSERT(des->encryptInit(&desKey56, "cbc", IV));

			CPPUNIT_ASSERT(des->encryptUpdate(plainText, OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(des->encryptFinal(OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(shsmCipherText == cipherText);

			// Check that we can get the plain text
			shsmPlainText.wipe();
			CPPUNIT_ASSERT(des->decryptInit(&desKey56, "cbc", IV));

			CPPUNIT_ASSERT(des->decryptUpdate(shsmCipherText, OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(des->decryptFinal(OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(shsmPlainText == plainText);

			// Test 112-bit key

			// First, use the OpenSSL command line tool to encrypt the test data
			writeTmpFile(plainText);

			sprintf(commandLine, "openssl des-ede-cbc -in shsmv2-destest.tmp -out shsmv2-destest-out.tmp -K %s -iv %s",
				testKeys112[i], testIV[i]);

			CPPUNIT_ASSERT(!system(commandLine));

			readTmpFile(cipherText);

			// Now, do the same thing using our DES implementation
			shsmCipherText.wipe();
			CPPUNIT_ASSERT(des->encryptInit(&desKey112, "cbc", IV));

			CPPUNIT_ASSERT(des->encryptUpdate(plainText, OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(des->encryptFinal(OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(shsmCipherText == cipherText);

			// Check that we can get the plain text
			shsmPlainText.wipe();
			CPPUNIT_ASSERT(des->decryptInit(&desKey112, "cbc", IV));

			CPPUNIT_ASSERT(des->decryptUpdate(shsmCipherText, OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(des->decryptFinal(OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(shsmPlainText == plainText);

			// Test 168-bit key

			// First, use the OpenSSL command line tool to encrypt the test data
			writeTmpFile(plainText);

			sprintf(commandLine, "openssl des-ede3-cbc -in shsmv2-destest.tmp -out shsmv2-destest-out.tmp -K %s -iv %s",
				testKeys168[i], testIV[i]);

			CPPUNIT_ASSERT(!system(commandLine));

			readTmpFile(cipherText);

			// Now, do the same thing using our DES implementation
			shsmCipherText.wipe();
			CPPUNIT_ASSERT(des->encryptInit(&desKey168, "cbc", IV));

			CPPUNIT_ASSERT(des->encryptUpdate(plainText, OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(des->encryptFinal(OB));
			shsmCipherText += OB;

			CPPUNIT_ASSERT(shsmCipherText == cipherText);

			// Check that we can get the plain text
			shsmPlainText.wipe();
			CPPUNIT_ASSERT(des->decryptInit(&desKey168, "cbc", IV));

			CPPUNIT_ASSERT(des->decryptUpdate(shsmCipherText, OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(des->decryptFinal(OB));
			shsmPlainText += OB;

			CPPUNIT_ASSERT(shsmPlainText == plainText);
		}
	}
}