Commit 96086ea6 authored by Stephen Morris's avatar Stephen Morris

[1324] Updates to get cloptions unit test nearly working

parent 7c229eba
......@@ -15,6 +15,7 @@ CLEANFILES = *.gcno *.gcda
noinst_PROGRAMS = perfdhcp
perfdhcp_SOURCES = perfdhcp.cc
perfdhcp_SOURCES += packetdisp.c
perfdhcp_SOURCES += externals.cc
perfdhcp_SOURCES += perfdhcp.h
perfdhcp_SOURCES += dhcp.h dhcp6.h
perfdhcp_SOURCES += cloptions.cc
......
......@@ -72,8 +72,8 @@ perfdhcp [-hv] [-4|-6] [-r<rate>] [-n<num-request>] [-p<test-period>]\n\
}
if (v4 && v6) {
fprintf(stderr, "Must not give -4 and -6 together.\n");
return 2;
fprintf(stderr, "Must not give -4 and -6 together.\n");
return 2;
}
switch (argc) {
case 0:
......@@ -173,3 +173,30 @@ The exit status is:\n\
not successfully completed.\n",
progName, usage);
}
// Null functions to get the tests compiling.
int parse(int, char**) {
// Argument names not given to prevent compiler warning about unused
// variables;
return (-1);
}
const char* getAddress() {
return (NULL);
}
uint16_t getPort() {
return (0);
}
uint32_t getTimeout() {
return (0);
}
const char* getQname() {
return (NULL);
}
/*
#ifdef __cplusplus
extern "C" {
#endif
*/
#include <stdint.h>
extern int v6;
extern int initialOnly;
......@@ -12,6 +16,34 @@ extern const char *diagSelector;
int procArgs(int argc, char **argv);
// Functions tested in cloptions unit test
int parse(int argc, char** argv);
const char* getAddress();
uint16_t getPort();
uint32_t getTimeout();
const char* getQname();
// Also need definitions of these values
namespace OptionInfo {
const uint32_t QR = 0x0001;
const uint32_t OP = 0x0002;
const uint32_t AA = 0x0004;
const uint32_t TC = 0x0008;
const uint32_t Z = 0x0010;
const uint32_t AD = 0x0020;
const uint32_t CD = 0x0040;
const uint32_t RC = 0x0080;
const uint32_t QC = 0x0100;
const uint32_t AC = 0x0200;
const uint32_t UC = 0x0400;
const uint32_t DC = 0x0800;
const uint32_t MS = 0x1000;
};
/*
#ifdef __cplusplus
}
#endif
*/
......@@ -58,14 +58,6 @@ void gen_solicit(struct dhcpv6_packet *dhcp_pkt, const struct duid *client_id);
void dhcp_send(int v6, const unsigned char *msg, int send_fd, const struct
sockaddr *r_addr, const struct sockaddr_storage *send_laddr);
int v6 = 0;
int initialOnly = 0;
const char *localName = NULL;
unsigned rate = 0;
unsigned numRequest = 0;
const char *server = NULL;
const char *diagSelector = "";
static const struct dkdesc diagLetters[] = {
{ 's', DK_SOCK },
{ 'm', DK_MSG },
......
......@@ -16,6 +16,7 @@ TESTS += run_unittests
run_unittests_SOURCES = run_unittests.cc
run_unittests_SOURCES += cloptions_unittest.cc
run_unittests_SOURCES += $(top_builddir)/tests/tools/perfdhcp/cloptions.cc
run_unittests_SOURCES += $(top_builddir)/tests/tools/perfdhcp/externals.cc
run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
......
......@@ -12,174 +12,167 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <cstddef>
#include <string>
#include <stdint.h>
#include <gtest/gtest.h>
#include <exceptions/exceptions.h>
#include "../cloptions.h"
#include "exceptions/exceptions.h"
using namespace std;
using namespace isc;
using namespace isc::badpacket;
/// \brief Test Fixture Class
class CommandOptionsTest : public virtual ::testing::Test,
public virtual CommandOptions
{
public:
/// \brief Default Constructor
CommandOptionsTest()
{}
/// \brief Check Non-Limit Options
///
/// Checks that the options that are NOT related to the message are set to
/// their default values.
void checkDefaultOtherValues() {
EXPECT_EQ("127.0.0.1", getAddress());
EXPECT_EQ(53, getPort());
EXPECT_EQ(500, getTimeout());
EXPECT_EQ("www.example.com", getQname());
}
/// \brief Check Non-Limit Options
///
/// Checks that the options that are NOT related to the message are set to
/// their default values.
void checkDefaultOtherValues() {
EXPECT_STREQ("127.0.0.1", getAddress());
EXPECT_EQ(53, getPort());
EXPECT_EQ(500, getTimeout());
EXPECT_STREQ("www.example.com", getQname());
}
/// \brief Checks the minimum and maximum value specified for an option
///
/// Checks the values for one of the options whose values are stored in the
/// class's options_ array.
///
/// \param index Index of the option in the limits_ array
/// \param minval Expected minimum value
/// \param maxval Expected maximum value
void checkValuePair(int index, uint32_t minval = 0, uint32_t maxval = 0) {
EXPECT_EQ(minimum(index), minval);
EXPECT_EQ(maximum(index), maxval);
}
/// \brief Checks the minimum and maximum value specified for an option
///
/// Checks the values for one of the options whose values are stored in the
/// class's options_ array.
///
/// \param index Index of the option in the limits_ array
/// \param minval Expected minimum value
/// \param maxval Expected maximum value
void checkValuePair(int, uint32_t, uint32_t) {
//void checkValuePair(int index, uint32_t minval = 0, uint32_t maxval = 0) {
// Argument names commented out as they are not used, so this avoid a
// compiler warning (which by default in BIND 10 is promoted to an
// error).
// maximum and minimum are not yet defined so following lines commented
// out.
// EXPECT_EQ(minimum(index), minval);
// EXPECT_EQ(maximum(index), maxval);
}
/// \brief Checks that all options are at default values
///
/// Checks that all options have both their maximum and minimum set to the
/// default values.
///
/// \param except Index not to check. (This allows options not being tested
/// to be checked to see that they are at the default value.) As all
/// index values are positive, a negative value means check
/// everything.
void checkDefaultLimitsValues(int except = -1) {
for (int i = 0; i < OptionInfo::SIZE; ++i) {
if (i != except) {
checkValuePair(i, OptionInfo::defval(i),
OptionInfo::defval(i));
}
/// \brief Checks that all options are at default values
///
/// Checks that all options have both their maximum and minimum set to the
/// default values.
///
/// \param except Index not to check. (This allows options not being tested
/// to be checked to see that they are at the default value.) As all
/// index values are positive, a negative value means check
/// everything.
void checkDefaultLimitsValues(int except = -1) {
// Dummy use of except to avoid an error
EXPECT_EQ(-1, except);
// OptionInfo not yet defined!
/*
for (int i = 0; i < OptionInfo::SIZE; ++i) {
if (i != except) {
checkValuePair(i, OptionInfo::defval(i), OptionInfo::defval(i));
}
}
*/
}
/// \brief Check valid command option
///
/// Checks that the command line specification of one of the options taking
/// a value correctly processes the option.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
/// \param optval Value to be passed to the option.
/// \param minval Expected minimum value
/// \param maxval Expected maximum value
void checkCommandValid(int index, const char* optflag, const char* optval,
uint32_t minval, uint32_t maxval) {
// Set up the command line and parse it.
const char* argv[] = {"badpacket", NULL, NULL};
argv[1] = optflag;
argv[2] = optval;
int argc = 3;
parse(argc, const_cast<char**>(argv));
// Check the results. Everything should be at the defaults except for
// the specified option, where the minimum and maximum should be as
// specified.
checkDefaultOtherValues();
checkDefaultLimitsValues(index);
checkValuePair(index, minval, maxval);
}
/// \brief Check valid command option
///
/// Checks that the command line specification of one of the options taking
/// a value correctly processes the option.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
/// \param optval Value to be passed to the option.
/// \param minval Expected minimum value
/// \param maxval Expected maximum value
void checkCommandValid(int index, const char* optflag, const char* optval,
uint32_t minval, uint32_t maxval) {
// Set up the command line and parse it.
const char* argv[] = {"badpacket", NULL, NULL};
argv[1] = optflag;
argv[2] = optval;
int argc = 3;
parse(argc, const_cast<char**>(argv));
/// \brief Check invalid command option
///
/// Passed a command with an invalid value, checks that the parsing throws
/// a BadValue exception.
///
/// \param optflag Option flag (in the form '--option')
/// \param optval Value to be passed to the option.
void checkCommandInvalid(const char* optflag, const char* optval) {
// Set up the command line and parse it.
const char* argv[] = {"badpacket", NULL, NULL};
argv[1] = optflag;
argv[2] = optval;
int argc = 3;
EXPECT_THROW(parse(argc, const_cast<char**>(argv)), isc::BadValue);
}
// Check the results. Everything should be at the defaults except for
// the specified option, where the minimum and maximum should be as
// specified.
checkDefaultOtherValues();
checkDefaultLimitsValues(index);
checkValuePair(index, minval, maxval);
}
/// \brief Check one-bit field
///
/// Explicitly for those fields in the flags word that are one bit wide,
/// perform a series of tests to check that they accept valid values and
/// reject invalid ones.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
void checkOneBitField(int index, const char* optflag) {
checkCommandValid(index, optflag, "0", 0, 0);
checkCommandValid(index, optflag, "1", 1, 1);
checkCommandValid(index, optflag, "0-1", 0, 1);
checkCommandValid(index, optflag, "1-0", 0, 1);
checkCommandInvalid(optflag, "0-3");
checkCommandInvalid(optflag, "4");
checkCommandInvalid(optflag, "xyz");
}
/// \brief Check invalid command option
///
/// Passed a command with an invalid value, checks that the parsing throws
/// a BadValue exception.
///
/// \param optflag Option flag (in the form '--option')
/// \param optval Value to be passed to the option.
void checkCommandInvalid(const char* optflag, const char* optval) {
// Set up the command line and parse it.
const char* argv[] = {"badpacket", NULL, NULL};
argv[1] = optflag;
argv[2] = optval;
int argc = 3;
EXPECT_THROW(parse(argc, const_cast<char**>(argv)), isc::BadValue);
}
/// \brief Check four-bit field
///
/// Explicitly for those fields in the flags word that are four bits wide,
/// perform a series of tests to check that they accept valid values and
/// reject invalid ones.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
void checkFourBitField(int index, const char* optflag) {
checkCommandValid(index, optflag, "0", 0, 0);
checkCommandValid(index, optflag, "15", 15, 15);
checkCommandValid(index, optflag, "0-15", 0, 15);
checkCommandValid(index, optflag, "15-0", 0, 15);
checkCommandInvalid(optflag, "0-17");
checkCommandInvalid(optflag, "24");
checkCommandInvalid(optflag, "xyz");
}
/// \brief Check one-bit field
///
/// Explicitly for those fields in the flags word that are one bit wide,
/// perform a series of tests to check that they accept valid values and
/// reject invalid ones.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
void checkOneBitField(int index, const char* optflag) {
checkCommandValid(index, optflag, "0", 0, 0);
checkCommandValid(index, optflag, "1", 1, 1);
checkCommandValid(index, optflag, "0-1", 0, 1);
checkCommandValid(index, optflag, "1-0", 0, 1);
checkCommandInvalid(optflag, "0-3");
checkCommandInvalid(optflag, "4");
checkCommandInvalid(optflag, "xyz");
}
/// \brief Check sixteen-bit field
///
/// Explicitly test the parsing of the fields that can take a 16-bit
/// value ranging from 0 to 65535.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
void checkSixteenBitField(int index, const char* optflag) {
checkCommandValid(index, optflag, "0", 0, 0);
checkCommandValid(index, optflag, "65535", 65535, 65535);
checkCommandValid(index, optflag, "0-65535", 0, 65535);
checkCommandValid(index, optflag, "65535-0", 0, 65535);
checkCommandInvalid(optflag, "0-65536");
checkCommandInvalid(optflag, "65537");
checkCommandInvalid(optflag, "xyz");
}
};
/// \brief Check four-bit field
///
/// Explicitly for those fields in the flags word that are four bits wide,
/// perform a series of tests to check that they accept valid values and
/// reject invalid ones.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
void checkFourBitField(int index, const char* optflag) {
checkCommandValid(index, optflag, "0", 0, 0);
checkCommandValid(index, optflag, "15", 15, 15);
checkCommandValid(index, optflag, "0-15", 0, 15);
checkCommandValid(index, optflag, "15-0", 0, 15);
checkCommandInvalid(optflag, "0-17");
checkCommandInvalid(optflag, "24");
checkCommandInvalid(optflag, "xyz");
}
/// \brief Check sixteen-bit field
///
/// Explicitly test the parsing of the fields that can take a 16-bit
/// value ranging from 0 to 65535.
///
/// \param index Option index
/// \param optflag Option flag (in the form '--option')
void checkSixteenBitField(int index, const char* optflag) {
checkCommandValid(index, optflag, "0", 0, 0);
checkCommandValid(index, optflag, "65535", 65535, 65535);
checkCommandValid(index, optflag, "0-65535", 0, 65535);
checkCommandValid(index, optflag, "65535-0", 0, 65535);
checkCommandInvalid(optflag, "0-65536");
checkCommandInvalid(optflag, "65537");
checkCommandInvalid(optflag, "xyz");
}
// Check that each of the non-message options will be recognised
TEST_F(CommandOptionsTest, address) {
TEST(CommandOptionsTest, address) {
const char* argv[] = {"badpacket", "--address", "192.0.2.1"};
int argc = sizeof(argv) / sizeof(const char*);
......@@ -188,104 +181,104 @@ TEST_F(CommandOptionsTest, address) {
// strings in order to conform to the function signature; however, the
// called functions all treat the strings as const.
parse(argc, const_cast<char**>(argv));
EXPECT_EQ("192.0.2.1", getAddress());
EXPECT_STREQ("192.0.2.1", getAddress());
EXPECT_EQ(53, getPort());
EXPECT_EQ(500, getTimeout());
EXPECT_EQ("www.example.com", getQname());
EXPECT_STREQ("www.example.com", getQname());
checkDefaultLimitsValues();
}
TEST_F(CommandOptionsTest, port) {
TEST(CommandOptionsTest, port) {
const char* argv[] = {"badpacket", "--port", "153"};
int argc = sizeof(argv) / sizeof(const char*);
parse(argc, const_cast<char**>(argv));
EXPECT_EQ("127.0.0.1", getAddress());
EXPECT_STREQ("127.0.0.1", getAddress());
EXPECT_EQ(153, getPort());
EXPECT_EQ(500, getTimeout());
EXPECT_EQ("www.example.com", getQname());
EXPECT_STREQ("www.example.com", getQname());
checkDefaultLimitsValues();
}
TEST_F(CommandOptionsTest, timeout) {
TEST(CommandOptionsTest, timeout) {
const char* argv[] = {"badpacket", "--timeout", "250"};
int argc = sizeof(argv) / sizeof(const char*);
parse(argc, const_cast<char**>(argv));
EXPECT_EQ("127.0.0.1", getAddress());
EXPECT_STREQ("127.0.0.1", getAddress());
EXPECT_EQ(53, getPort());
EXPECT_EQ(250, getTimeout());
EXPECT_EQ("www.example.com", getQname());
EXPECT_STREQ("www.example.com", getQname());
checkDefaultLimitsValues();
}
TEST_F(CommandOptionsTest, parameter) {
TEST(CommandOptionsTest, parameter) {
const char* argv[] = {"badpacket", "ftp.example.net"};
int argc = sizeof(argv) / sizeof(const char*);
parse(argc, const_cast<char**>(argv));
EXPECT_EQ("127.0.0.1", getAddress());
EXPECT_STREQ("127.0.0.1", getAddress());
EXPECT_EQ(53, getPort());
EXPECT_EQ(500, getTimeout());
EXPECT_EQ("ftp.example.net", getQname());
EXPECT_STREQ("ftp.example.net", getQname());
checkDefaultLimitsValues();
}
// Test options representing the flags fields.
TEST_F(CommandOptionsTest, qr) {
TEST(CommandOptionsTest, qr) {
checkOneBitField(OptionInfo::QR, "--qr");
}
TEST_F(CommandOptionsTest, op) {
TEST(CommandOptionsTest, op) {
checkFourBitField(OptionInfo::OP, "--op");
}
TEST_F(CommandOptionsTest, aa) {
TEST(CommandOptionsTest, aa) {
checkOneBitField(OptionInfo::AA, "--aa");
}
TEST_F(CommandOptionsTest, tc) {
TEST(CommandOptionsTest, tc) {
checkOneBitField(OptionInfo::TC, "--tc");
}
TEST_F(CommandOptionsTest, z) {
TEST(CommandOptionsTest, z) {
checkOneBitField(OptionInfo::Z, "--z");
}
TEST_F(CommandOptionsTest, ad) {
TEST(CommandOptionsTest, ad) {
checkOneBitField(OptionInfo::AD, "--ad");
}
TEST_F(CommandOptionsTest, cd) {
TEST(CommandOptionsTest, cd) {
checkOneBitField(OptionInfo::CD, "--cd");
}
TEST_F(CommandOptionsTest, rc) {
TEST(CommandOptionsTest, rc) {
checkFourBitField(OptionInfo::RC, "--rc");
}
// Section count options
TEST_F(CommandOptionsTest, qc) {
TEST(CommandOptionsTest, qc) {
checkSixteenBitField(OptionInfo::QC, "--qc");
}
TEST_F(CommandOptionsTest, ac) {
TEST(CommandOptionsTest, ac) {
checkSixteenBitField(OptionInfo::AC, "--ac");
}
TEST_F(CommandOptionsTest, uc) {
TEST(CommandOptionsTest, uc) {
checkSixteenBitField(OptionInfo::UC, "--uc");
}
TEST_F(CommandOptionsTest, dc) {
TEST(CommandOptionsTest, dc) {
checkSixteenBitField(OptionInfo::DC, "--dc");
}
// ... and the message size option
TEST_F(CommandOptionsTest, ms) {
TEST(CommandOptionsTest, ms) {
int index = OptionInfo::MS;
const char* optflag = "--ms";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment