2022-08-27 22:06:20 +00:00
|
|
|
// This file Copyright (C) 2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include <cstddef> // size_t
|
2022-08-27 22:06:20 +00:00
|
|
|
#include <string>
|
|
|
|
|
2023-01-02 16:23:51 +00:00
|
|
|
#include <libtransmission/error.h>
|
|
|
|
#include <libtransmission/torrent-magnet.h>
|
|
|
|
#include <libtransmission/torrent-metainfo.h>
|
2022-08-27 22:06:20 +00:00
|
|
|
|
2023-07-08 15:24:03 +00:00
|
|
|
#include "gtest/gtest.h"
|
2022-08-27 22:06:20 +00:00
|
|
|
#include "test-fixtures.h"
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
namespace libtransmission::test
|
2022-08-27 22:06:20 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
using TorrentMagnetTest = SessionTest;
|
|
|
|
|
|
|
|
TEST_F(TorrentMagnetTest, getMetadataPiece)
|
|
|
|
{
|
|
|
|
auto* tor = zeroTorrentInit(ZeroTorrentState::Complete);
|
|
|
|
EXPECT_NE(nullptr, tor);
|
|
|
|
|
|
|
|
auto benc = std::string{
|
|
|
|
"d8:announce31:http://www.example.com/announce10:created by25:Transmission/2.61 (13407)13:creation datei1358704075e8:encoding5:UTF-84:info"
|
|
|
|
};
|
|
|
|
auto piece = int{ 0 };
|
|
|
|
auto info_dict_size = size_t{ 0U };
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
auto const info_dict_data = tr_torrentGetMetadataPiece(tor, piece++);
|
|
|
|
if (!info_dict_data)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
benc.append(reinterpret_cast<char const*>(std::data(*info_dict_data)), std::size(*info_dict_data));
|
|
|
|
info_dict_size += std::size(*info_dict_data);
|
|
|
|
}
|
|
|
|
benc.append("e");
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(tor->info_dict_size(), info_dict_size);
|
2022-08-27 22:06:20 +00:00
|
|
|
|
|
|
|
auto torrent_metainfo = tr_torrent_metainfo{};
|
|
|
|
tr_error* error = nullptr;
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_TRUE(torrent_metainfo.parse_benc(benc, &error));
|
2022-08-27 22:06:20 +00:00
|
|
|
EXPECT_EQ(nullptr, error) << error->message;
|
|
|
|
tr_error_clear(&error);
|
|
|
|
|
2023-04-23 01:25:55 +00:00
|
|
|
EXPECT_EQ(tor->piece_hash(0), torrent_metainfo.piece_hash(0));
|
2022-08-27 22:06:20 +00:00
|
|
|
}
|
|
|
|
|
2022-11-14 20:16:29 +00:00
|
|
|
} // namespace libtransmission::test
|