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);
}
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);
}
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);
}
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);
}
Example #5
0
static int eventHandler(bcm_gas_t *gas, bcm_gas_event_t *event)
{
	TRACE(TRACE_DEBUG, "*** event notification ***\n");
	TRACE_MAC_ADDR(TRACE_DEBUG, "   peer", &event->peer);
	TRACE(TRACE_DEBUG, "   dialog token = %d\n", event->dialogToken);

	if (event->type == BCM_GAS_EVENT_QUERY_REQUEST) {
		TRACE(TRACE_DEBUG, "   BCM_GAS_EVENT_QUERY_REQUEST\n");
		processQueryRequest(event->gas, event->queryReq.len, event->queryReq.data);
	}
	else if (event->type == BCM_GAS_EVENT_TX) {
		TRACE(TRACE_DEBUG, "   BCM_GAS_EVENT_TX\n");
	}
	else if (event->type == BCM_GAS_EVENT_RX) {
		TRACE(TRACE_DEBUG, "   BCM_GAS_EVENT_RX\n");
	}

	else if (event->type == BCM_GAS_EVENT_STATUS) {
#ifdef BCMDBG
		char *str;

		switch (event->status.statusCode)
		{
		case DOT11_SC_SUCCESS:
			str = "SUCCESS";
			break;
		case DOT11_SC_FAILURE:
			str = "UNSPECIFIED";
			break;
		case DOT11_SC_ADV_PROTO_NOT_SUPPORTED:
			str = "ADVERTISEMENT_PROTOCOL_NOT_SUPPORTED";
			break;
		case DOT11_SC_NO_OUTSTAND_REQ:
			str = "NO_OUTSTANDING_REQUEST";
			break;
		case DOT11_SC_RSP_NOT_RX_FROM_SERVER:
			str = "RESPONSE_NOT_RECEIVED_FROM_SERVER";
			break;
		case DOT11_SC_TIMEOUT:
			str = "TIMEOUT";
			break;
		case DOT11_SC_QUERY_RSP_TOO_LARGE:
			str = "QUERY_RESPONSE_TOO_LARGE";
			break;
		case DOT11_SC_SERVER_UNREACHABLE:
			str = "SERVER_UNREACHABLE";
			break;
		case DOT11_SC_TRANSMIT_FAILURE:
			str = "TRANSMISSION_FAILURE";
			break;
		default:
			str = "UNKNOWN";
			break;
		}

		TRACE(TRACE_DEBUG, "   status code = %s\n", str);
#endif	/* BCMDBG */

		/* if status is from own instance then GAS exchange is completed */
		if (gas == event->gas)
			return FALSE;
	}
	else {
		TRACE(TRACE_DEBUG, "   UNKOWN\n");
	}

	return TRUE;
}