0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews */
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <config.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <atf-c.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <stdio.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <stdlib.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <string.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <unistd.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <isc/buffer.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <isc/lex.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <isc/mem.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews#include <isc/util.h>
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
f503aa345b451f94875a5bab637223bcbbd93b6dEvan HuntATF_TC(lex_0xff);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan HuntATF_TC_HEAD(lex_0xff, tc) {
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews atf_tc_set_md_var(tc, "descr", "check handling of 0xff");
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews}
f503aa345b451f94875a5bab637223bcbbd93b6dEvan HuntATF_TC_BODY(lex_0xff, tc) {
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews isc_mem_t *mctx = NULL;
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews isc_result_t result;
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews isc_lex_t *lex = NULL;
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews isc_buffer_t death_buf;
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews isc_token_t token;
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews unsigned char death[] = { EOF, 'A' };
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews UNUSED(tc);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews result = isc_mem_create(0, 0, &mctx);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews result = isc_lex_create(mctx, 1024, &lex);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews isc_buffer_init(&death_buf, &death[0], sizeof(death));
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews isc_buffer_add(&death_buf, sizeof(death));
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews result = isc_lex_openbuffer(lex, &death_buf);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews result = isc_lex_gettoken(lex, 0, &token);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews}
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews
f503aa345b451f94875a5bab637223bcbbd93b6dEvan HuntATF_TC(lex_setline);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan HuntATF_TC_HEAD(lex_setline, tc) {
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt atf_tc_set_md_var(tc, "descr", "check setting of source line");
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt}
f503aa345b451f94875a5bab637223bcbbd93b6dEvan HuntATF_TC_BODY(lex_setline, tc) {
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt isc_mem_t *mctx = NULL;
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt isc_result_t result;
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt isc_lex_t *lex = NULL;
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt unsigned char text[] = "text\nto\nbe\nprocessed\nby\nlexer";
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt isc_buffer_t buf;
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt isc_token_t token;
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt unsigned long line;
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt int i;
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt UNUSED(tc);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt result = isc_mem_create(0, 0, &mctx);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt result = isc_lex_create(mctx, 1024, &lex);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt isc_buffer_init(&buf, &text[0], sizeof(text));
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt isc_buffer_add(&buf, sizeof(text));
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt result = isc_lex_openbuffer(lex, &buf);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt result = isc_lex_setsourceline(lex, 100);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt for (i = 0; i < 6; i++) {
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt result = isc_lex_gettoken(lex, 0, &token);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt line = isc_lex_getsourceline(lex);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(line, 100U + i);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt }
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt result = isc_lex_gettoken(lex, 0, &token);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(result, ISC_R_EOF);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt line = isc_lex_getsourceline(lex);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_REQUIRE_EQ(line, 105U);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt}
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews/*
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews * Main
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews */
0a47bc90af557138a2aaff614ad73d95155ddd9eMark AndrewsATF_TP_ADD_TCS(tp) {
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_TP_ADD_TC(tp, lex_0xff);
f503aa345b451f94875a5bab637223bcbbd93b6dEvan Hunt ATF_TP_ADD_TC(tp, lex_setline);
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews return (atf_no_error());
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews}
0a47bc90af557138a2aaff614ad73d95155ddd9eMark Andrews