File: [local] / acopm / tests / cidr.c (download)
Revision 1.1, Sat May 8 15:42:29 2021 UTC (3 years ago) by bountyht
Branch point for: MAIN
Initial revision
|
/*
* Copyright (C) 2017 Aaron M. D. Jones <aaronmdjones@gmail.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include "acopm-common.h"
#include <inttypes.h> /* PRIu8 */
#include <string.h> /* strcmp() */
#include <netinet/in.h> /* INET6_ADDRSTRLEN */
#include "tests.h" /* EXIT_AUTOMAKE_CHECK_HARD_FAILURE */
#include "utils/log.h" /* acopm_log_*() */
#include "utils/cidr.h" /* acopm_cidr_*(), struct acopm_cidr_address */
static const struct {
const char cidr[INET6_ADDRSTRLEN + 4];
const char test[INET6_ADDRSTRLEN];
uint8_t netsize;
bool expected_result;
} vectors[] = {
{ "192.0.2.40", "192.0.2.40", 32, true },
{ "192.0.2.40", "192.0.2.41", 32, false },
{ "192.0.2.0/24", "192.0.2.40", 24, true },
{ "192.0.2.0/24", "192.0.3.40", 24, false },
{ "2001:db8:d::32", "2001:db8:d::32", 128, true },
{ "2001:db8:d::32", "2001:db8:d::33", 128, false },
{ "2001:db8:d::32/48", "2001:db8:d::ffff", 48, true },
{ "2001:db8:d::32/48", "2001:db8:e::ffff", 48, false },
};
int
main(void)
{
int result = EXIT_SUCCESS;
if (! acopm_log_init())
return EXIT_AUTOMAKE_CHECK_HARD_FAILURE;
for (size_t i = 0; i < (sizeof vectors / sizeof vectors[0]); i++)
{
struct acopm_cidr_address *const cidr = acopm_cidr_new(vectors[i].cidr);
if (! cidr)
{
(void) acopm_log_error("Test vector %zu ('%s') failed", i, vectors[i].cidr);
result = EXIT_FAILURE;
continue;
}
if (cidr->netsize != vectors[i].netsize)
{
(void) acopm_log_error("Test vector %zu ('%s') failed: netsize %" PRIu8 " != expected "
"%" PRIu8, i, vectors[i].cidr, cidr->netsize, vectors[i].netsize);
(void) acopm_cidr_free(cidr);
result = EXIT_FAILURE;
continue;
}
const bool ret = acopm_cidr_match(vectors[i].test, cidr);
if (ret != vectors[i].expected_result)
{
(void) acopm_log_error("Test vector %zu ('%s') failed: %sMATCH", i, vectors[i].test,
((ret) ? "" : "NO "));
(void) acopm_cidr_free(cidr);
result = EXIT_FAILURE;
continue;
}
(void) acopm_cidr_free(cidr);
}
return result;
}