Skip to content

Commit

Permalink
test_cliopt: Refactor by using parameterized tests
Browse files Browse the repository at this point in the history
This makes it easier to add other configuration
variants to test.

Signed-off-by: Frank Lichtenheld <[email protected]>
  • Loading branch information
flichtenheld authored and dsommers committed Mar 13, 2024
1 parent 3bd3915 commit 458e5df
Showing 1 changed file with 63 additions and 85 deletions.
148 changes: 63 additions & 85 deletions test/unittests/test_cliopt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ std::string minimalConfig = certconfig + "\n"
+ "client\n"
"remote wooden.box\n";

class ValidConfigs : public testing::TestWithParam<std::string>
{
};
typedef std::pair<std::string, std::string> config_error;
class InvalidConfigs : public testing::TestWithParam<config_error>
{
};

void load_client_config(const std::string &config_content)
{
OptionList options;
Expand All @@ -58,45 +66,38 @@ void load_client_config(const std::string &config_content)
ClientOptions cliopt(options, config);
}

TEST(config, missingRequiredOption)
TEST_P(ValidConfigs, valid_config)
{
ParseClientConfig conf = ParseClientConfig::parse("mode server");
EXPECT_EQ(conf.error(), true);
EXPECT_EQ(conf.message(), "ERR_PROFILE_OPTION: option_error: remote option not specified");
load_client_config(GetParam());
}

TEST(config, parse_missing_option)
TEST_P(InvalidConfigs, config_throws_option_error)
{
OVPN_EXPECT_THROW(
load_client_config(std::string("remote wooden.box\n") + "mode server"
+ "\n<ca>\n" + dummysecp256cert + "</ca>\n"),
load_client_config(GetParam().first),
option_error,
"option 'cert' not found");
GetParam().second);
}

TEST(config, parse_forbidden_option)
TEST(config, missingRequiredOption)
{
OVPN_EXPECT_THROW(
load_client_config(minimalConfig + "mode"),
option_error,
"Only 'mode p2p' supported");

OVPN_EXPECT_THROW(
load_client_config(minimalConfig + "mode server"),
option_error,
"Only 'mode p2p' supported");

OVPN_EXPECT_THROW(
load_client_config(minimalConfig + "key-method 1"),
option_error,
"Only 'key-method 2' is supported");

OVPN_EXPECT_THROW(
load_client_config(minimalConfig + "fragment"),
option_error,
"sorry, 'fragment' directive is not supported");
ParseClientConfig conf = ParseClientConfig::parse("mode server");
EXPECT_EQ(conf.error(), true);
EXPECT_EQ(conf.message(), "ERR_PROFILE_OPTION: option_error: remote option not specified");
}

INSTANTIATE_TEST_SUITE_P(
optionError,
InvalidConfigs,
testing::Values(
config_error{std::string("remote wooden.box\n") + "mode server"
+ "\n<ca>\n" + dummysecp256cert + "</ca>\n",
"option 'cert' not found"},
config_error{minimalConfig + "mode", "Only 'mode p2p' supported"},
config_error{minimalConfig + "mode server", "Only 'mode p2p' supported"},
config_error{minimalConfig + "key-method 1", "Only 'key-method 2' is supported"},
config_error{minimalConfig + "fragment", "sorry, 'fragment' directive is not supported"}));

TEST(config, parse_unknown_option)
{
OVPN_EXPECT_THROW(
Expand All @@ -105,27 +106,20 @@ TEST(config, parse_unknown_option)
"UNKNOWN/UNSUPPORTED OPTIONS");
}

TEST(config, duplicate_option)
{
/* A duplicate option should not cause our parser to fail */
load_client_config(minimalConfig + "cipher AES-192-CBC\ncipher AES-256-GCM\n");
}

TEST(config, parse_ignore_unkown)
{
/* Bikeshed colour is ignored should throw no error */
load_client_config(minimalConfig + "ignore-unknown-option bikeshed-colour bikeshed-color\n"
"ignore-unknown-option danish axe phk\n"
"bikeshed-colour green");

load_client_config(minimalConfig + "setenv opt bikeshed-paint silver with sparkling");
}

TEST(config, ignore_warning_option)
{
load_client_config(minimalConfig + "tun-ipv6\n");
load_client_config(minimalConfig + "opt-verify\n");
}
INSTANTIATE_TEST_SUITE_P(
minimalConfigs,
ValidConfigs,
testing::Values(
/* A duplicate option should not cause our parser to fail */
minimalConfig + "cipher AES-192-CBC\ncipher AES-256-GCM\n",
/* Bikeshed colour is ignored should throw no error */
minimalConfig + "ignore-unknown-option bikeshed-colour bikeshed-color\n"
"ignore-unknown-option danish axe phk\n"
"bikeshed-colour green",
minimalConfig + "setenv opt bikeshed-paint silver with sparkling",
/* warnings, but no errors */
minimalConfig + "tun-ipv6\n",
minimalConfig + "opt-verify\n"));

TEST(config, parse_management)
{
Expand Down Expand Up @@ -238,42 +232,26 @@ TEST(config, multiple_option_errors)
os.str());
}

TEST(config, client_missing_in_config)
{
std::string configNoClient = certconfig + "\nremote 1.2.3.4\n";
OVPN_EXPECT_THROW(
load_client_config(configNoClient),
option_error,
"option_error: Neither 'client' nor both 'tls-client' and 'pull' options declared. OpenVPN3 client only supports --client mode.");
}

TEST(config, pull_and_tlsclient_in_config)
{
std::string configNoClient = certconfig + "\nremote 1.2.3.4\ntls-client\npull\n";
/* Should not trigger an error, even without --client in place */
load_client_config(configNoClient);
}

TEST(config, pull_and_client_and_tlsclient_in_config)
{
std::string configNoClient = certconfig + "\nremote 1.2.3.4\ntls-client\npull\nclient\n";
/* Should not trigger an error. Redundant options are no problem */
load_client_config(configNoClient);
}

TEST(config, onlypullortlsclient)
{
std::array<std::string, 2> options{"tls-client", "pull"};

for (const auto &opt : options)
{
std::string configNoClient = certconfig + "\nremote 1.2.3.4\n" + opt + "\n";
OVPN_EXPECT_THROW(
load_client_config(configNoClient),
option_error,
"option_error: Neither 'client' nor both 'tls-client' and 'pull' options declared. OpenVPN3 client only supports --client mode.");
}
}
INSTANTIATE_TEST_SUITE_P(
clientPull,
ValidConfigs,
testing::Values(
/* Should not trigger an error, even without --client in place */
certconfig + "\nremote 1.2.3.4\ntls-client\npull\n",
/* Should not trigger an error. Redundant options are no problem */
certconfig + "\nremote 1.2.3.4\ntls-client\npull\nclient\n",
certconfig + "\nremote 1.2.3.4\nclient\ntls-client\n"));

INSTANTIATE_TEST_SUITE_P(
clientPull,
InvalidConfigs,
testing::Values(
config_error{certconfig + "\nremote 1.2.3.4\n",
"option_error: Neither 'client' nor both 'tls-client' and 'pull' options declared. OpenVPN3 client only supports --client mode."},
config_error{certconfig + "\nremote 1.2.3.4\ntls-client\n",
"option_error: Neither 'client' nor both 'tls-client' and 'pull' options declared. OpenVPN3 client only supports --client mode."},
config_error{certconfig + "\nremote 1.2.3.4\npull\n",
"option_error: Neither 'client' nor both 'tls-client' and 'pull' options declared. OpenVPN3 client only supports --client mode."}));

TEST(config, meta_option_in_content)
{
Expand Down

0 comments on commit 458e5df

Please sign in to comment.