2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright (C) 2013-2022 Mnemosyne LLC.
|
2022-08-08 18:05:39 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2020-08-11 18:11:55 +00:00
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/tr-getopt.h>
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const Options = std::array<tr_option, 8>{
|
2020-08-11 18:11:55 +00:00
|
|
|
tr_option{ 'p', "private", "Allow this torrent to only be used with the specified tracker(s)", "p", false, nullptr },
|
|
|
|
{ 'o', "outfile", "Save the generated .torrent to this filename", "o", true, "<file>" },
|
|
|
|
{ 's', "piecesize", "Set how many KiB each piece should be, overriding the preferred default", "s", true, "<size in KiB>" },
|
|
|
|
{ 'c', "comment", "Add a comment", "c", true, "<comment>" },
|
|
|
|
{ 't', "tracker", "Add a tracker's announce URL", "t", true, "<url>" },
|
|
|
|
{ 'q', "pooka", "Pooka", "pk", false, nullptr },
|
|
|
|
{ 'V', "version", "Show version number and exit", "V", false, nullptr },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr }
|
|
|
|
};
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
} // namespace
|
2020-08-11 18:11:55 +00:00
|
|
|
|
|
|
|
class GetoptTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
protected:
|
2022-01-24 19:07:55 +00:00
|
|
|
static void runTest( //
|
2021-08-15 09:41:48 +00:00
|
|
|
int argc,
|
|
|
|
char const* const* argv,
|
|
|
|
int expected_n,
|
|
|
|
int const* expected_c,
|
2022-01-24 19:07:55 +00:00
|
|
|
char const* const* expected_args)
|
2020-08-11 18:11:55 +00:00
|
|
|
{
|
|
|
|
auto n = int{};
|
|
|
|
tr_optind = 1;
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
auto c = int{};
|
|
|
|
char const* argstr = nullptr;
|
2020-08-11 18:11:55 +00:00
|
|
|
while ((c = tr_getopt("summary", argc, argv, Options.data(), &argstr)) != TR_OPT_DONE)
|
|
|
|
{
|
|
|
|
EXPECT_LT(n, expected_n);
|
|
|
|
EXPECT_EQ(expected_c[n], c);
|
|
|
|
EXPECT_STREQ(expected_args[n], argstr);
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_EQ(expected_n, n);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, noOptions)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 1>{ "/some/path/tr-getopt-test" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 0;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{};
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{};
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, shortNoarg)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 2>{ "/some/path/tr-getopt-test", "-p" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 'p' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ nullptr };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, longNoarg)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 2>{ "/some/path/tr-getopt-test", "--private" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 'p' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ nullptr };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, shortWithArg)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 3>{ "/some/path/tr-getopt-test", "-o", "/tmp/outfile" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 'o' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ "/tmp/outfile" };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, longWithArg)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 3>{ "/some/path/tr-getopt-test", "--outfile", "/tmp/outfile" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 'o' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ "/tmp/outfile" };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, shortWithArgAfterEq)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 2>{ "/some/path/tr-getopt-test", "-o=/tmp/outfile" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 'o' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ "/tmp/outfile" };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, longWithArgAfterEq)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 2>{ "/some/path/tr-getopt-test", "--outfile=/tmp/outfile" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 'o' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ "/tmp/outfile" };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, unknownOption)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 2>{ "/some/path/tr-getopt-test", "-z" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ TR_OPT_UNK };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ "-z" };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, missingArg)
|
|
|
|
{
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 2>{ "/some/path/tr-getopt-test", "-o" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ TR_OPT_ERR };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ nullptr };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, lotsOfOptions)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto const args = std::array<char const*, 6>{
|
|
|
|
"/some/path/tr-getopt-test", "--piecesize=4", "-c", "hello world", "-p", "--tracker=foo"
|
|
|
|
};
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 4;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 's', 'c', 'p', 't' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ "4", "hello world", nullptr, "foo" };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GetoptTest, matchLongerKey)
|
|
|
|
{
|
|
|
|
// confirm that this resolves to 'q' and not 'p'
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const args = std::array<char const*, 2>{ "/some/path/tr-getopt-test", "-pk" };
|
2020-08-11 18:11:55 +00:00
|
|
|
auto constexpr ExpectedN = 1;
|
2020-09-08 00:33:36 +00:00
|
|
|
auto const expected_c = std::array<int, ExpectedN>{ 'q' };
|
|
|
|
auto const expected_opt_arg = std::array<char const*, ExpectedN>{ nullptr };
|
|
|
|
runTest(args.size(), args.data(), ExpectedN, expected_c.data(), expected_opt_arg.data());
|
2020-08-11 18:11:55 +00:00
|
|
|
}
|