tor/src/test/test_parsecommon.c

135 lines
3.5 KiB
C
Raw Normal View History

2018-10-11 15:22:12 +03:00
/* Copyright (c) 2001-2004, Roger Dingledine.
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
#include "core/or/or.h"
#include "test/test.h"
#include "lib/memarea/memarea.h"
#include "lib/encoding/binascii.h"
#include "feature/dirparse/parsecommon.h"
#include "test/log_test_helpers.h"
static void
test_parsecommon_tokenize_string_null(void *arg)
{
memarea_t *area = memarea_new();
smartlist_t *tokens = smartlist_new();
(void)arg;
const char *str_with_null = "a\0bccccccccc";
int retval =
tokenize_string(area, str_with_null,
str_with_null + 3,
tokens, NULL, 0);
tt_int_op(retval, OP_EQ, -1);
done:
memarea_drop_all(area);
smartlist_free(tokens);
return;
}
2018-10-12 14:19:40 +03:00
static void
test_parsecommon_get_next_token_success(void *arg)
{
memarea_t *area = memarea_new();
const char *str = "uptime 1024";
const char *end = str + strlen(str);
const char **s = &str;
token_rule_t table = T01("uptime", K_UPTIME, GE(1), NO_OBJ);
(void)arg;
directory_token_t *token = get_next_token(area, s, end, &table);
tt_int_op(token->tp, OP_EQ, K_UPTIME);
tt_int_op(token->n_args, OP_EQ, 1);
tt_str_op(*(token->args), OP_EQ, "1024");
tt_assert(!token->object_type);
tt_int_op(token->object_size, OP_EQ, 0);
tt_assert(!token->object_body);
tt_ptr_op(*s, OP_EQ, end);
done:
memarea_drop_all(area);
return;
}
static void
test_parsecommon_get_next_token_concat_args(void *arg)
{
memarea_t *area = memarea_new();
const char *str = "proto A=1 B=2";
const char *end = str + strlen(str);
const char **s = &str;
token_rule_t rule = T01("proto", K_PROTO, CONCAT_ARGS, NO_OBJ);
(void)arg;
directory_token_t *token = get_next_token(area, s, end, &rule);
tt_int_op(token->tp, OP_EQ, K_PROTO);
tt_int_op(token->n_args, OP_EQ, 1);
tt_str_op(*(token->args), OP_EQ, "A=1 B=2");
done:
memarea_drop_all(area);
}
static void
test_parsecommon_get_next_token_parse_keys(void *arg)
{
(void)arg;
memarea_t *area = memarea_new();
const char *base64_key =
"MIGJAoGBAMDdIya33BfNlHOkzoTKSTT8EjD64waMfUr372syVHiFjHhObwKwGA5u\n"
"sHaMIe9r+Ij/4C1dKyuXkcz3DOl6gWNhTD7dZ89I+Okoh1jWe30jxCiAcywC22p5\n"
"XLhrDkX1A63Z7XCH9ltwU2WMqWsVM98N2GR6MTujP7wtqdLExYN1AgMBAAE=\n";
char *str;
tor_asprintf(&str, "onion-key\n"
"-----BEGIN RSA PUBLIC KEY-----\n"
"%s"
"-----END RSA PUBLIC KEY-----\n", base64_key);
const char *end = str + strlen(str);
const char **s = (const char **)&str;
const char decoded[128];
base64_decode((char *)decoded, sizeof(decoded), base64_key,
strlen(base64_key));
token_rule_t rule = T1("onion-key", R_IPO_ONION_KEY, NO_ARGS, NEED_KEY_1024);
directory_token_t *token = get_next_token(area, s, end, &rule);
tt_int_op(token->tp, OP_EQ, R_IPO_ONION_KEY);
tt_int_op(token->n_args, OP_EQ, 0);
tt_str_op(token->object_type, OP_EQ, "RSA PUBLIC KEY");
tt_int_op(token->object_size, OP_EQ, 0);
tt_assert(!token->object_body);
tt_assert(token->key);
tt_assert(!token->error);
// TODO: same with secret key
done:
memarea_drop_all(area);
}
2018-10-11 15:22:12 +03:00
#define PARSECOMMON_TEST(name) \
{ #name, test_parsecommon_ ## name, 0, NULL, NULL }
struct testcase_t parsecommon_tests[] = {
PARSECOMMON_TEST(tokenize_string_null),
2018-10-12 14:19:40 +03:00
PARSECOMMON_TEST(get_next_token_success),
PARSECOMMON_TEST(get_next_token_concat_args),
PARSECOMMON_TEST(get_next_token_parse_keys),
2018-10-11 15:22:12 +03:00
END_OF_TESTCASES
};