2014-11-19 18:21:02 -08:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2014-11-19 18:21:02 -08:00
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2014-11-19 18:21:02 -08:00
|
|
|
*/
|
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
2014-11-19 18:21:02 -08:00
|
|
|
#include <stdlib.h>
|
2018-10-23 21:16:16 -07:00
|
|
|
#include <string.h>
|
2014-11-19 18:21:02 -08:00
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
2014-11-19 18:21:02 -08:00
|
|
|
|
|
|
|
#include <isc/counter.h>
|
|
|
|
#include <isc/result.h>
|
2018-10-23 21:16:16 -07:00
|
|
|
#include <isc/util.h>
|
2014-11-19 18:21:02 -08:00
|
|
|
|
|
|
|
#include "isctest.h"
|
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
static int
|
|
|
|
_setup(void **state) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
2014-11-19 18:21:02 -08:00
|
|
|
}
|
2018-10-23 21:16:16 -07:00
|
|
|
|
|
|
|
static int
|
|
|
|
_teardown(void **state) {
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test isc_counter object */
|
|
|
|
static void
|
|
|
|
isc_counter_test(void **state) {
|
2014-11-19 18:21:02 -08:00
|
|
|
isc_result_t result;
|
|
|
|
isc_counter_t *counter = NULL;
|
|
|
|
int i;
|
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
UNUSED(state);
|
2014-11-19 18:21:02 -08:00
|
|
|
|
|
|
|
result = isc_counter_create(mctx, 0, &counter);
|
2018-10-23 21:16:16 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2014-11-19 18:21:02 -08:00
|
|
|
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
result = isc_counter_increment(counter);
|
2018-10-23 21:16:16 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2014-11-19 18:21:02 -08:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
assert_int_equal(isc_counter_used(counter), 10);
|
2014-11-19 18:21:02 -08:00
|
|
|
|
|
|
|
isc_counter_setlimit(counter, 15);
|
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
result = isc_counter_increment(counter);
|
2018-10-23 21:16:16 -07:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2014-11-19 18:21:02 -08:00
|
|
|
break;
|
2018-10-23 21:16:16 -07:00
|
|
|
}
|
2014-11-19 18:21:02 -08:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
assert_int_equal(isc_counter_used(counter), 15);
|
2014-11-19 18:21:02 -08:00
|
|
|
|
|
|
|
isc_counter_detach(&counter);
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
int
|
|
|
|
main(void) {
|
|
|
|
const struct CMUnitTest tests[] = {
|
|
|
|
cmocka_unit_test_setup_teardown(isc_counter_test,
|
|
|
|
_setup, _teardown),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int
|
|
|
|
main(void) {
|
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
|
|
return (0);
|
2014-11-19 18:21:02 -08:00
|
|
|
}
|
|
|
|
|
2018-10-23 21:16:16 -07:00
|
|
|
#endif
|