The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
Data Structures | Macros | Functions | Variables
token_tests.c File Reference

Tests for the configuration file tokenizer. More...

#include "acutest_common_init.h"
#include "acutest_helpers.h"
#include <freeradius-devel/util/token.h>
#include <sys/mman.h>
+ Include dependency graph for token_tests.c:

Go to the source code of this file.

Data Structures

struct  parse_result_t
 Where a parse ran, and what the parse produced. More...
 

Macros

#define TEST_PARSE(_result, _token, _buf, _used)
 

Functions

static char * guard_alloc (char **base, char const *in)
 Copy a string so that the terminating NUL is the last readable byte.
 
static void guard_free (char *base)
 
static parse_result_t test_getop (char const *in)
 Run getop() over a guarded copy of the input.
 
static void test_getop_operators (void)
 getop() accepts an assignment or comparison operator and rejects everything else
 
static parse_result_t test_getstring (char const *in, bool unescape)
 Run getstring() over a guarded copy of the input.
 
static void test_getstring_null (void)
 getstring() rejects a NULL pointer rather than following the pointer
 
static void test_getstring_quoting (void)
 getstring() reads a quoted string as a string, and anything else as a word
 
static parse_result_t test_gettoken (char const *in, bool unescape)
 Run gettoken() over a guarded copy of the input.
 
static void test_gettoken_delimiters (void)
 gettoken() splits on the tokens in the token table
 
static void test_gettoken_no_unescape (void)
 Without unescaping, gettoken() keeps the backslash.
 
static void test_gettoken_quotes (void)
 Each quote character produces its own token type.
 
static void test_gettoken_truncation (void)
 An input longer than the output buffer stops at the end of the output buffer.
 
static void test_gettoken_unescape (void)
 gettoken() turns escape sequences into the characters the sequences name
 
static void test_gettoken_unterminated (void)
 A string which never ends is an error, and the input pointer does not move.
 
static parse_result_t test_getword (char const *in, bool unescape)
 Run getword() over a guarded copy of the input.
 
static void test_getword_no_delimiters (void)
 getword() reads to whitespace, and ignores the tokens which split gettoken()
 
static void test_token_from_quote_enum_str (void)
 fr_token_from_quote_enum_str() turns an enumeration name back into a token
 
static void test_token_name (void)
 fr_token_name() names the tokens which the tokenizer matches as tokens
 
static void test_token_to_enum_str (void)
 fr_token_to_enum_str() names a token with the name of the C enumeration value
 
static void test_triple_quote (void)
 A triple-quoted string keeps every character between the delimiters.
 

Variables

 TEST_LIST
 

Detailed Description

Tests for the configuration file tokenizer.

Definition in file token_tests.c.


Data Structure Documentation

◆ parse_result_t

struct parse_result_t

Where a parse ran, and what the parse produced.

Definition at line 33 of file token_tests.c.

Data Fields
char buf[256] what the parse function wrote.
fr_token_t token what the parse function returned.
size_t used how many bytes of the input the parse consumed.

Macro Definition Documentation

◆ TEST_PARSE

#define TEST_PARSE (   _result,
  _token,
  _buf,
  _used 
)
Value:
do { \
TEST_CHECK((_result).token == (_token)); \
TEST_MSG("token: got %u, expected %u", (_result).token, (fr_token_t)(_token)); \
TEST_CHECK(strcmp((_result).buf, (_buf)) == 0); \
TEST_MSG("buf: got \"%s\", expected \"%s\"", (_result).buf, (_buf)); \
TEST_CHECK((_result).used == (size_t) (_used)); \
TEST_MSG("used: got %zu, expected %zu", (_result).used, (size_t) (_used)); \
} while (0)
static size_t used
enum fr_token fr_token_t

Definition at line 157 of file token_tests.c.

Function Documentation

◆ guard_alloc()

static char * guard_alloc ( char **  base,
char const *  in 
)
static

Copy a string so that the terminating NUL is the last readable byte.

A read past the NUL lands on an unreadable page and faults, rather than returning whatever byte happens to follow the string. Every parse in this file runs against a guarded copy, because a tokenizer which reads one byte too far produces the right answer on almost every run and the wrong answer on the rest.

Parameters
[out]basestart of the mapping, to pass to guard_free().
[in]into copy.
Returns
the copy, positioned against the unreadable page.

Definition at line 50 of file token_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ guard_free()

static void guard_free ( char *  base)
static

Definition at line 67 of file token_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_getop()

static parse_result_t test_getop ( char const *  in)
static

Run getop() over a guarded copy of the input.

Definition at line 139 of file token_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_getop_operators()

static void test_getop_operators ( void  )
static

getop() accepts an assignment or comparison operator and rejects everything else

Definition at line 390 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_getstring()

static parse_result_t test_getstring ( char const *  in,
bool  unescape 
)
static

Run getstring() over a guarded copy of the input.

Definition at line 118 of file token_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_getstring_null()

static void test_getstring_null ( void  )
static

getstring() rejects a NULL pointer rather than following the pointer

Definition at line 433 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_getstring_quoting()

static void test_getstring_quoting ( void  )
static

getstring() reads a quoted string as a string, and anything else as a word

Definition at line 414 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_gettoken()

static parse_result_t test_gettoken ( char const *  in,
bool  unescape 
)
static

Run gettoken() over a guarded copy of the input.

Definition at line 75 of file token_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_gettoken_delimiters()

static void test_gettoken_delimiters ( void  )
static

gettoken() splits on the tokens in the token table

Definition at line 198 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_gettoken_no_unescape()

static void test_gettoken_no_unescape ( void  )
static

Without unescaping, gettoken() keeps the backslash.

The one exception is a backslash in front of the quote character, which the tokenizer still has to remove so that the quote does not end the string.

Definition at line 287 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_gettoken_quotes()

static void test_gettoken_quotes ( void  )
static

Each quote character produces its own token type.

Definition at line 232 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_gettoken_truncation()

static void test_gettoken_truncation ( void  )
static

An input longer than the output buffer stops at the end of the output buffer.

Definition at line 319 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_gettoken_unescape()

static void test_gettoken_unescape ( void  )
static

gettoken() turns escape sequences into the characters the sequences name

Definition at line 252 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_gettoken_unterminated()

static void test_gettoken_unterminated ( void  )
static

A string which never ends is an error, and the input pointer does not move.

Definition at line 298 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_getword()

static parse_result_t test_getword ( char const *  in,
bool  unescape 
)
static

Run getword() over a guarded copy of the input.

getword() returns 0 at end of line and 1 otherwise, so the rcode goes in token.

Definition at line 97 of file token_tests.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ test_getword_no_delimiters()

static void test_getword_no_delimiters ( void  )
static

getword() reads to whitespace, and ignores the tokens which split gettoken()

Definition at line 353 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_token_from_quote_enum_str()

static void test_token_from_quote_enum_str ( void  )
static

fr_token_from_quote_enum_str() turns an enumeration name back into a token

Definition at line 474 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_token_name()

static void test_token_name ( void  )
static

fr_token_name() names the tokens which the tokenizer matches as tokens

The word and string tokens are not in that table, so fr_token_name() does not name the word and string tokens. Use fr_token_to_enum_str() for those.

Definition at line 450 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_token_to_enum_str()

static void test_token_to_enum_str ( void  )
static

fr_token_to_enum_str() names a token with the name of the C enumeration value

Definition at line 464 of file token_tests.c.

+ Here is the call graph for this function:

◆ test_triple_quote()

static void test_triple_quote ( void  )
static

A triple-quoted string keeps every character between the delimiters.

Skipping three characters for the opening delimiter and then one more ate the first character of the content. For an input of three quotes and nothing else, the same extra skip stepped past the terminating NUL.

Definition at line 172 of file token_tests.c.

+ Here is the call graph for this function:

Variable Documentation

◆ TEST_LIST

TEST_LIST
Initial value:
= {
{ "triple_quote", test_triple_quote },
{ "gettoken_delimiters", test_gettoken_delimiters },
{ "gettoken_quotes", test_gettoken_quotes },
{ "gettoken_unescape", test_gettoken_unescape },
{ "gettoken_no_unescape", test_gettoken_no_unescape },
{ "gettoken_unterminated", test_gettoken_unterminated },
{ "gettoken_truncation", test_gettoken_truncation },
{ "getword_no_delimiters", test_getword_no_delimiters },
{ "getop_operators", test_getop_operators },
{ "getstring_quoting", test_getstring_quoting },
{ "getstring_null", test_getstring_null },
{ "token_name", test_token_name },
{ "token_to_enum_str", test_token_to_enum_str },
{ "token_from_quote_enum_str", test_token_from_quote_enum_str },
}
#define TEST_TERMINATOR
Definition acutest.h:77
static void test_gettoken_unescape(void)
gettoken() turns escape sequences into the characters the sequences name
static void test_getstring_null(void)
getstring() rejects a NULL pointer rather than following the pointer
static void test_token_name(void)
fr_token_name() names the tokens which the tokenizer matches as tokens
static void test_gettoken_truncation(void)
An input longer than the output buffer stops at the end of the output buffer.
static void test_token_from_quote_enum_str(void)
fr_token_from_quote_enum_str() turns an enumeration name back into a token
static void test_gettoken_delimiters(void)
gettoken() splits on the tokens in the token table
static void test_getop_operators(void)
getop() accepts an assignment or comparison operator and rejects everything else
static void test_gettoken_no_unescape(void)
Without unescaping, gettoken() keeps the backslash.
static void test_getword_no_delimiters(void)
getword() reads to whitespace, and ignores the tokens which split gettoken()
static void test_triple_quote(void)
A triple-quoted string keeps every character between the delimiters.
static void test_gettoken_quotes(void)
Each quote character produces its own token type.
static void test_gettoken_unterminated(void)
A string which never ends is an error, and the input pointer does not move.
static void test_token_to_enum_str(void)
fr_token_to_enum_str() names a token with the name of the C enumeration value
static void test_getstring_quoting(void)
getstring() reads a quoted string as a string, and anything else as a word

Definition at line 495 of file token_tests.c.