view tests/src/libirccd/command-server-connect/main.cpp @ 785:7145a3df4cb7

misc: rename host to hostname, closes #941 @2h
author David Demelier <markand@malikania.fr>
date Wed, 07 Nov 2018 12:55:00 +0100
parents 8c44bbcbbab9
children 3c090c1ff4f0
line wrap: on
line source

/*
 * main.cpp -- test server-connect remote command
 *
 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#define BOOST_TEST_MODULE "server-connect"
#include <boost/test/unit_test.hpp>

#include <irccd/test/command_fixture.hpp>

using namespace irccd::test;

namespace irccd {

namespace {

BOOST_FIXTURE_TEST_SUITE(server_connect_fixture_suite, command_fixture)

BOOST_AUTO_TEST_CASE(minimal)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "local"             },
		{ "hostname",   "irc.example.org"   }
	});

	const auto s = irccd_.servers().get("local");

	BOOST_TEST(!code);
	BOOST_TEST(s);
	BOOST_TEST(s->get_id() == "local");
	BOOST_TEST(s->get_hostname() == "irc.example.org");
	BOOST_TEST(s->get_port() == 6667U);
}

#if defined(IRCCD_HAVE_SSL)

BOOST_AUTO_TEST_CASE(full)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "local2"            },
		{ "hostname",   "irc.example2.org"  },
		{ "password",   "nonono"            },
		{ "nickname",   "francis"           },
		{ "realname",   "the_francis"       },
		{ "username",   "frc"               },
		{ "ctcpVersion", "ultra bot"        },
		{ "commandChar", "::"               },
		{ "port",       18000               },
		{ "ssl",        true                },
		{ "sslVerify",  true                },
		{ "autoRejoin", true                },
		{ "joinInvite", true                }
	});

	const auto s = irccd_.servers().get("local2");

	BOOST_TEST(!code);
	BOOST_TEST(s);
	BOOST_TEST(s->get_id() == "local2");
	BOOST_TEST(s->get_hostname() == "irc.example2.org");
	BOOST_TEST(s->get_port() == 18000U);
	BOOST_TEST(s->get_password() == "nonono");
	BOOST_TEST(s->get_nickname() == "francis");
	BOOST_TEST(s->get_realname() == "the_francis");
	BOOST_TEST(s->get_username() == "frc");
	BOOST_TEST(s->get_command_char() == "::");
	BOOST_TEST(s->get_ctcp_version() == "ultra bot");
	BOOST_TEST(static_cast<bool>(s->get_options() & server::options::ssl));
	BOOST_TEST(static_cast<bool>(s->get_options() & server::options::ssl_verify));
	BOOST_TEST(static_cast<bool>(s->get_options() & server::options::auto_rejoin));
	BOOST_TEST(static_cast<bool>(s->get_options() & server::options::join_invite));
}

#endif // !IRCCD_HAVE_SSL

BOOST_AUTO_TEST_SUITE(errors)

BOOST_AUTO_TEST_CASE(already_exists)
{
	irccd_.servers().add(std::make_unique<mock_server>(ctx_, "local"));

	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "local"             },
		{ "hostname",   "127.0.0.1"         }
	});

	BOOST_TEST(code == server_error::already_exists);
	BOOST_TEST(json["error"].get<int>() == server_error::already_exists);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_hostname_1)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "new"               },
	});

	BOOST_TEST(code == server_error::invalid_hostname);
	BOOST_TEST(json["error"].get<int>() == server_error::invalid_hostname);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_hostname_2)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "new"               },
		{ "hostname",   123456              }
	});

	BOOST_TEST(code == server_error::invalid_hostname);
	BOOST_TEST(json["error"].get<int>() == server_error::invalid_hostname);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_identifier_1)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       ""                  },
		{ "hostname",   "127.0.0.1"         }
	});

	BOOST_TEST(code == server_error::invalid_identifier);
	BOOST_TEST(json["error"].get<int>() == server_error::invalid_identifier);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_identifier_2)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       123456              },
		{ "hostname",   "127.0.0.1"         }
	});

	BOOST_TEST(code == server_error::invalid_identifier);
	BOOST_TEST(json["error"].get<int>() == server_error::invalid_identifier);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_port_1)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "new"               },
		{ "hostname",   "127.0.0.1"         },
		{ "port",       "notaint"           }
	});

	BOOST_TEST(code == server_error::invalid_port);
	BOOST_TEST(json["error"].get<int>() == server_error::invalid_port);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_port_2)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "new"               },
		{ "hostname",   "127.0.0.1"         },
		{ "port",       -123                }
	});

	BOOST_TEST(code == server_error::invalid_port);
	BOOST_TEST(json["error"].get<int>() == server_error::invalid_port);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

BOOST_AUTO_TEST_CASE(invalid_port_3)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "new"               },
		{ "hostname",   "127.0.0.1"         },
		{ "port",       1000000             }
	});

	BOOST_TEST(code == server_error::invalid_port);
	BOOST_TEST(json["error"].get<int>() == server_error::invalid_port);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

#if !defined(IRCCD_HAVE_SSL)

BOOST_AUTO_TEST_CASE(ssl_disabled)
{
	const auto [json, code] = request({
		{ "command",    "server-connect"    },
		{ "name",       "new"               },
		{ "hostname",   "127.0.0.1"         },
		{ "ssl",        true                }
	});

	BOOST_TEST(code == server_error::ssl_disabled);
	BOOST_TEST(json["error"].get<int>() == server_error::ssl_disabled);
	BOOST_TEST(json["errorCategory"].get<std::string>() == "server");
}

#endif

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE_END()

} // !namespace

} // !irccd