Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SWIG bindings: Support DownloadCallbacks user_data, unit tests #1939

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 47 additions & 10 deletions bindings/libdnf5/repo.i
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,29 @@

#define CV __perl_CV

%{
static inline void * integer_to_void_ptr(int value) noexcept {
return reinterpret_cast<void *>(static_cast<intptr_t>(value));
}

static inline int void_ptr_to_int(void * value) noexcept {
return static_cast<int>(reinterpret_cast<intptr_t>(value));
}
%}

%feature("valuewrapper") Package;

%include "libdnf5/repo/config_repo.hpp"

%feature("director") DownloadCallbacks;

%typemap(directorin, noblock=1) void * user_cb_data {
$input = SWIG_From_int(static_cast<int>(reinterpret_cast<intptr_t>($1)));
%typemap(directorin, noblock=1) void * {
$input = SWIG_From_int(void_ptr_to_int($1));
}

%typemap(directorout, noblock=1) void * {
int swig_val;
int swig_res = SWIG_AsVal_int($1, &swig_val);
if (SWIG_IsOK(swig_res)) {
$result = reinterpret_cast<void *>(swig_val);
$result = integer_to_void_ptr(swig_val);
#if defined(SWIGPYTHON)
} else if (Py_IsNone($1)) {
$result = 0;
Expand All @@ -72,37 +80,66 @@
}
}

%typemap(in, noblock=1) void * user_cb_data {
%typemap(in, noblock=1) void * {
{
int swig_val;
int swig_res = SWIG_AsVal_int($input, &swig_val);
if (!SWIG_IsOK(swig_res)) {
Swig::DirectorTypeMismatchException::raise(SWIG_ErrorType(SWIG_ArgError(swig_res)), "in input value of type '""int""'");
}
$1 = reinterpret_cast<void *>(swig_val);
$1 = integer_to_void_ptr(swig_val);
}
}

%typemap(out, noblock=1) void * {
$result = SWIG_From_int(static_cast<int>(reinterpret_cast<intptr_t>($1)));
$result = SWIG_From_int(void_ptr_to_int($1));
}

%feature("director") DownloadCallbacks;
%include "libdnf5/repo/download_callbacks.hpp"
%typemap(directorin) void *;
%typemap(directorout) void * user_cb_data;
%typemap(in) void * user_cb_data;
%typemap(directorout) void *;
%typemap(in) void *;
%typemap(out) void *;
wrap_unique_ptr(DownloadCallbacksUniquePtr, libdnf5::repo::DownloadCallbacks);

%extend libdnf5::repo::FileDownloader {
void add(libdnf5::repo::RepoWeakPtr & repo, const std::string & url, const std::string & destination, int user_data = 0) {
$self->add(repo, url, destination, integer_to_void_ptr(user_data));
}
void add(const std::string & url, const std::string & destination, int user_data = 0) {
$self->add(url, destination, integer_to_void_ptr(user_data));
}
};
%ignore libdnf5::repo::FileDownloader::add;
%ignore FileDownloadError;
%include "libdnf5/repo/file_downloader.hpp"

%extend libdnf5::repo::PackageDownloader {
void add(const libdnf5::rpm::Package & package, int user_data = 0) {
$self->add(package, integer_to_void_ptr(user_data));
}
void add(const libdnf5::rpm::Package & package, const std::string & destination, int user_data = 0) {
$self->add(package, destination, integer_to_void_ptr(user_data));
}
};
%ignore libdnf5::repo::PackageDownloader::add;
%ignore PackageDownloadError;
%include "libdnf5/repo/package_downloader.hpp"

%ignore RepoCacheError;
%include "libdnf5/repo/repo_cache.hpp"

%extend libdnf5::repo::Repo {
void set_user_data(int user_data) noexcept {
$self->set_user_data(integer_to_void_ptr(user_data));
}
int get_user_data() const noexcept {
return void_ptr_to_int($self->get_user_data());
}
};
%ignore libdnf5::repo::Repo::set_user_data;
%ignore libdnf5::repo::Repo::get_user_data;
%include "libdnf5/repo/repo.hpp"

%include "libdnf5/repo/repo_weak.hpp"
Expand Down
2 changes: 1 addition & 1 deletion test/dnf5-plugins/copr_plugin/CoprRepoTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ std::string repo_to_string(const dnf5::CoprRepo & repo) {
}

void CoprRepoTest::prepare_env() {
installroot = temp->get_path() / "installroot";
installroot = temp_dir->get_path() / "installroot";
std::filesystem::create_directory(installroot);
confdir = installroot / "etc";
std::filesystem::create_directory(confdir);
Expand Down
2 changes: 1 addition & 1 deletion test/libdnf5/comps/test_group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ void CompsGroupTest::test_serialize() {
q_standard.filter_groupid("standard");
auto standard = q_standard.get();

auto serialize_path = temp->get_path() / "serialized-standard.xml";
auto serialize_path = temp_dir->get_path() / "serialized-standard.xml";
standard.serialize(serialize_path);

std::string actual = libdnf5::utils::fs::File(serialize_path, "r").read();
Expand Down
117 changes: 117 additions & 0 deletions test/libdnf5/repo/test_file_downloader.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
Copyright Contributors to the libdnf project.

This file is part of libdnf: https://github.com/rpm-software-management/libdnf/

Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/

#include "test_file_downloader.hpp"

#include "utils/string.hpp"

#include <libdnf5/repo/file_downloader.hpp>

CPPUNIT_TEST_SUITE_REGISTRATION(FileDownloaderTest);

namespace {

class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
public:
void * add_new_download(
[[maybe_unused]] void * user_data,
[[maybe_unused]] const char * description,
[[maybe_unused]] double total_to_download) override {
++add_new_download_cnt;
this->user_data = user_data;
user_cb_data_holder = "User cb data";
return &user_cb_data_holder;
}

int end([[maybe_unused]] void * user_cb_data, TransferStatus status, const char * msg) override {
++end_cnt;
end_user_cb_data = static_cast<std::string *>(user_cb_data);
end_status = status;
end_msg = libdnf5::utils::string::c_to_str(msg);
return 0;
}

int progress(
[[maybe_unused]] void * user_cb_data,
[[maybe_unused]] double total_to_download,
[[maybe_unused]] double downloaded) override {
++progress_cnt;
return 0;
}

int mirror_failure(
[[maybe_unused]] void * user_cb_data,
[[maybe_unused]] const char * msg,
[[maybe_unused]] const char * url,
[[maybe_unused]] const char * metadata) override {
++mirror_failure_cnt;
return 0;
}

int add_new_download_cnt = 0;
int progress_cnt = 0;
int mirror_failure_cnt = 0;
int end_cnt = 0;

void * user_data = nullptr;
std::string user_cb_data_holder;
std::string * end_user_cb_data = nullptr;

TransferStatus end_status;
std::string end_msg;
};

} // namespace


void FileDownloaderTest::test_file_downloader() {
std::string user_data = "User data";

std::string source_file_path = std::string(PROJECT_SOURCE_DIR) + "/test/data/keys/key.pub";
std::string source_url = "file://" + source_file_path;
auto dest_file_path = temp_dir->get_path() / "file_downloader.pub";

auto cbs_unique_ptr = std::make_unique<DownloadCallbacks>();
auto cbs = cbs_unique_ptr.get();
base.set_download_callbacks(std::move(cbs_unique_ptr));

libdnf5::repo::FileDownloader file_downloader(base);
file_downloader.add(source_url, dest_file_path, &user_data);
file_downloader.download();

CPPUNIT_ASSERT_EQUAL(&user_data, static_cast<std::string *>(cbs->user_data));
CPPUNIT_ASSERT_EQUAL(std::string("User cb data"), cbs->end_user_cb_data ? *cbs->end_user_cb_data : "");

CPPUNIT_ASSERT_EQUAL(1, cbs->add_new_download_cnt);
CPPUNIT_ASSERT_EQUAL(1, cbs->end_cnt);
CPPUNIT_ASSERT_GREATEREQUAL(1, cbs->progress_cnt);
CPPUNIT_ASSERT_EQUAL(0, cbs->mirror_failure_cnt);

CPPUNIT_ASSERT_EQUAL(DownloadCallbacks::TransferStatus::SUCCESSFUL, cbs->end_status);
CPPUNIT_ASSERT_EQUAL(std::string(), cbs->end_msg);

// Check contents of downloaded file
libdnf5::utils::fs::File source_file;
source_file.open(source_file_path, "r");
std::string source_file_content = source_file.read();
libdnf5::utils::fs::File dest_file;
dest_file.open(dest_file_path, "r");
std::string dest_file_content = dest_file.read();
CPPUNIT_ASSERT_EQUAL(source_file_content, dest_file_content);
}
37 changes: 37 additions & 0 deletions test/libdnf5/repo/test_file_downloader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright Contributors to the libdnf project.

This file is part of libdnf: https://github.com/rpm-software-management/libdnf/

Libdnf is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

Libdnf is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with libdnf. If not, see <https://www.gnu.org/licenses/>.
*/

#ifndef LIBDNF5_TEST_REPO_FILE_DOWNLOADER_HPP
#define LIBDNF5_TEST_REPO_FILE_DOWNLOADER_HPP

#include "../shared/base_test_case.hpp"

#include <cppunit/extensions/HelperMacros.h>


class FileDownloaderTest : public BaseTestCase {
CPPUNIT_TEST_SUITE(FileDownloaderTest);
CPPUNIT_TEST(test_file_downloader);
CPPUNIT_TEST_SUITE_END();

public:
void test_file_downloader();
};

#endif
13 changes: 12 additions & 1 deletion test/libdnf5/repo/test_package_downloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

CPPUNIT_TEST_SUITE_REGISTRATION(PackageDownloaderTest);

namespace {

class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
public:
Expand All @@ -42,6 +43,7 @@ class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
[[maybe_unused]] const char * description,
[[maybe_unused]] double total_to_download) override {
++add_new_download_cnt;
user_data_array.emplace_back(user_data);
std::string user_cb_data = std::string("Package: ") + description;
return user_cb_data_container.emplace_back(std::move(user_cb_data)).data();
}
Expand Down Expand Up @@ -78,11 +80,15 @@ class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
int mirror_failure_cnt = 0;
int end_cnt = 0;

std::vector<void *> user_data_array;
std::vector<const char *> user_cb_data_array;
std::vector<TransferStatus> end_status;
std::vector<std::string> end_msg;
};

} // namespace


void PackageDownloaderTest::test_package_downloader() {
auto repo = add_repo_rpm("rpm-repo1");

Expand All @@ -97,8 +103,9 @@ void PackageDownloaderTest::test_package_downloader() {
auto cbs = cbs_unique_ptr.get();
base.set_download_callbacks(std::move(cbs_unique_ptr));

std::string user_data = "User data";
for (const auto & package : query) {
downloader.add(package);
downloader.add(package, &user_data);
}

downloader.download();
Expand All @@ -113,6 +120,10 @@ void PackageDownloaderTest::test_package_downloader() {
CPPUNIT_ASSERT_GREATEREQUAL(2, cbs->progress_cnt);
CPPUNIT_ASSERT_EQUAL(0, cbs->mirror_failure_cnt);

for (auto * item : cbs->user_data_array) {
CPPUNIT_ASSERT_EQUAL(&user_data, static_cast<std::string *>(item));
}

std::sort(cbs->user_cb_data_array.begin(), cbs->user_cb_data_array.end(), [](const char * a, const char * b) {
return std::string_view(a).compare(b) < 0;
});
Expand Down
22 changes: 15 additions & 7 deletions test/libdnf5/repo/test_repo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,20 @@ along with libdnf. If not, see <https://www.gnu.org/licenses/>.

CPPUNIT_TEST_SUITE_REGISTRATION(RepoTest);

namespace {

// Accessor of private Base::p_impl, see private_accessor.hpp
create_private_getter_template;
create_getter(load, &libdnf5::repo::Repo::load);

void RepoTest::test_load_system_repo() {
// TODO(lukash) there's no rpmdb in the installroot, create data for the test
(*(repo_sack->get_system_repo()).*get(load{}))();
}

namespace {

class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
public:
void * add_new_download(
[[maybe_unused]] void * user_data,
const char * description,
[[maybe_unused]] double total_to_download) override {
++start_cnt;
last_user_data = user_data;
start_what = description;
return nullptr;
}
Expand Down Expand Up @@ -81,6 +77,8 @@ class DownloadCallbacks : public libdnf5::repo::DownloadCallbacks {
return 0;
}

void * last_user_data = nullptr;

int start_cnt = 0;
std::string start_what;

Expand All @@ -104,10 +102,18 @@ class RepoCallbacks : public libdnf5::repo::RepoCallbacks {

} // namespace

void RepoTest::test_load_system_repo() {
// TODO(lukash) there's no rpmdb in the installroot, create data for the test
(*(repo_sack->get_system_repo()).*get(load{}))();
}

void RepoTest::test_load_repo() {
std::string repoid("repomd-repo1");
auto repo = add_repo_repomd(repoid, false);

std::string user_data = "User data";
repo->set_user_data(&user_data);

auto dl_callbacks = std::make_unique<DownloadCallbacks>();
auto dl_callbacks_ptr = dl_callbacks.get();
base.set_download_callbacks(std::move(dl_callbacks));
Expand All @@ -118,6 +124,8 @@ void RepoTest::test_load_repo() {

repo_sack->load_repos(libdnf5::repo::Repo::Type::AVAILABLE);

CPPUNIT_ASSERT_EQUAL(&user_data, static_cast<std::string *>(dl_callbacks_ptr->last_user_data));

CPPUNIT_ASSERT_EQUAL(1, dl_callbacks_ptr->start_cnt);
CPPUNIT_ASSERT_EQUAL(repoid, dl_callbacks_ptr->start_what);

Expand Down
Loading
Loading