void
setUp(void)
{
if (counter == 0) {
counter++;
init_auth(); // causes segfault if called more than once
}
/*
* init_auth() is called by tests_main.cpp earlier. It
* does not initialize global variables like
* authnumkeys, so let's reset them to zero here.
*/
authnumkeys = 0;
static const int KEYTYPE = KEY_TYPE_MD5;
static char msgbuf[128];
void
AddTrustedKey(keyid_t keyno)
{
/*
* We need to add a MD5-key in addition to setting the
* trust, because authhavekey() requires type != 0.
*/
MD5auth_setkey(keyno, KEYTYPE, NULL, 0, NULL);
/* test the implementation of 'auth_log2' -- use a local copy of the code */
static u_short
auth_log2(
size_t x)
{
int s;
int r = 0;
size_t m = ~(size_t)0;
for (s = sizeof(size_t) / 2 * CHAR_BIT; s != 0; s >>= 1) {
m <<= s;
if (x & m)
r += s;
else
x <<= s;
}
return (u_short)r;
}
void test_auth_log2(void);
void test_auth_log2(void)
{
int l2;
size_t tv;
TEST_ASSERT_EQUAL_INT(0, auth_log2(0));
TEST_ASSERT_EQUAL_INT(0, auth_log2(1));
for (l2 = 1; l2 < sizeof(size_t)*CHAR_BIT; ++l2) {
tv = (size_t)1 << l2;
TEST_ASSERT_EQUAL_INT(l2, auth_log2( tv ));
TEST_ASSERT_EQUAL_INT(l2, auth_log2( tv + 1 ));
TEST_ASSERT_EQUAL_INT(l2, auth_log2(2*tv - 1));
}
}
/* Converting a string to a host address. Here we use 'getaddrinfo()' in
* an independent implementation to avoid cross-reactions with the
* object under test. 'inet_pton' is too dangerous to handle it
* properly, and ultimate performance is *not* the goal here.
*/
static int/*BOOL*/
getaddr(
int af,
const char *astr,
sockaddr_u * addr)
{
struct addrinfo hint;
struct addrinfo *ares;
void test_AddrMatch_anull(void);
void test_AddrMatch_anull(void)
{
/* Check the not-an-address logic with a prefix/check length of
* zero bits. Any compare with a NULL or AF_UNSPEC address
* returns inequality (aka FALSE).
*/
sockaddr_u ip4, ip6, ipn;
/* the first 23 bits are equal, so any prefix <= 23 should match */
for (bits = 0; bits < 40; ++bits) {
snprintf(msgbuf, sizeof(msgbuf),
"keyacc_amatch(*,*,%u) wrong", bits);
want = (bits <= 23);
TEST_ASSERT_EQUAL_MESSAGE(want, keyacc_amatch(&a1, &a2, bits), msgbuf);
}
/* the first 111 bits are equal, so any prefix <= 111 should match */
for (bits = 0; bits < 136; ++bits) {
snprintf(msgbuf, sizeof(msgbuf),
"keyacc_amatch(*,*,%u) wrong", bits);
want = (bits <= 111);
TEST_ASSERT_EQUAL_MESSAGE(want, keyacc_amatch(&a1, &a2, bits), msgbuf);
}