2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 18:19:42 +00:00
bind/lib/isc/tests/lex_test.c

143 lines
2.9 KiB
C
Raw Normal View History

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* 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 https://mozilla.org/MPL/2.0/.
*
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
*/
2018-10-23 22:33:45 -07:00
#if HAVE_CMOCKA
#include <sched.h> /* IWYU pragma: keep */
#include <setjmp.h>
2018-10-23 22:33:45 -07:00
#include <stdarg.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
2018-10-23 22:33:45 -07:00
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/buffer.h>
#include <isc/lex.h>
#include <isc/mem.h>
#include <isc/util.h>
#include "isctest.h"
static int
2020-02-13 14:44:37 -08:00
_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);
}
static int
2020-02-13 14:44:37 -08:00
_teardown(void **state) {
UNUSED(state);
isc_test_end();
return (0);
}
2018-10-23 22:33:45 -07:00
/* check handling of 0xff */
static void
2020-02-13 14:44:37 -08:00
lex_0xff(void **state) {
isc_result_t result;
2020-02-13 14:44:37 -08:00
isc_lex_t *lex = NULL;
isc_buffer_t death_buf;
2020-02-13 14:44:37 -08:00
isc_token_t token;
unsigned char death[] = { EOF, 'A' };
2018-10-23 22:33:45 -07:00
UNUSED(state);
result = isc_lex_create(test_mctx, 1024, &lex);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_SUCCESS);
isc_buffer_init(&death_buf, &death[0], sizeof(death));
isc_buffer_add(&death_buf, sizeof(death));
result = isc_lex_openbuffer(lex, &death_buf);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_lex_gettoken(lex, 0, &token);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_SUCCESS);
2018-11-19 07:47:05 +01:00
isc_lex_destroy(&lex);
}
2018-10-23 22:33:45 -07:00
/* check setting of source line */
static void
2020-02-13 14:44:37 -08:00
lex_setline(void **state) {
isc_result_t result;
isc_lex_t *lex = NULL;
unsigned char text[] = "text\nto\nbe\nprocessed\nby\nlexer";
2020-02-13 14:44:37 -08:00
isc_buffer_t buf;
isc_token_t token;
unsigned long line;
2020-02-13 14:44:37 -08:00
int i;
2018-10-23 22:33:45 -07:00
UNUSED(state);
result = isc_lex_create(test_mctx, 1024, &lex);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_SUCCESS);
isc_buffer_init(&buf, &text[0], sizeof(text));
isc_buffer_add(&buf, sizeof(text));
result = isc_lex_openbuffer(lex, &buf);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_lex_setsourceline(lex, 100);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_SUCCESS);
for (i = 0; i < 6; i++) {
result = isc_lex_gettoken(lex, 0, &token);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_SUCCESS);
line = isc_lex_getsourceline(lex);
2018-10-23 22:33:45 -07:00
assert_int_equal(line, 100U + i);
}
result = isc_lex_gettoken(lex, 0, &token);
2018-10-23 22:33:45 -07:00
assert_int_equal(result, ISC_R_EOF);
line = isc_lex_getsourceline(lex);
2018-10-23 22:33:45 -07:00
assert_int_equal(line, 105U);
2018-11-19 07:47:05 +01:00
isc_lex_destroy(&lex);
}
2018-10-23 22:33:45 -07:00
int
2020-02-13 14:44:37 -08:00
main(void) {
2018-10-23 22:33:45 -07:00
const struct CMUnitTest tests[] = {
cmocka_unit_test(lex_0xff),
cmocka_unit_test(lex_setline),
};
return (cmocka_run_group_tests(tests, _setup, _teardown));
2018-10-23 22:33:45 -07:00
}
#else /* HAVE_CMOCKA */
#include <stdio.h>
int
2020-02-13 14:44:37 -08:00
main(void) {
2018-10-23 22:33:45 -07:00
printf("1..0 # Skipped: cmocka not available\n");
return (SKIPPED_TEST_EXIT_CODE);
}
#endif /* if HAVE_CMOCKA */