From f13615ec140aec508501d21a21190782318c360a Mon Sep 17 00:00:00 2001 From: Martin Braun Date: Thu, 5 Mar 2020 11:35:14 -0800 Subject: uhd: cal: Add database class This class contains methods to store and retrieve data from the local calibration database. Note that in this case, the "database" is just a bunch of files on the local filesystem. --- host/tests/cal_database_test.cpp | 88 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 host/tests/cal_database_test.cpp (limited to 'host/tests/cal_database_test.cpp') diff --git a/host/tests/cal_database_test.cpp b/host/tests/cal_database_test.cpp new file mode 100644 index 000000000..cd189138b --- /dev/null +++ b/host/tests/cal_database_test.cpp @@ -0,0 +1,88 @@ +// +// Copyright 2020 Ettus Research, a National Instruments Brand +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include +#include +#include // putenv or _putenv +#include +#include +#include + +using namespace uhd::usrp::cal; +namespace fs = boost::filesystem; + +BOOST_AUTO_TEST_CASE(test_rc) +{ + BOOST_CHECK(!database::has_cal_data("does_not_exist", "1234", source::RC)); + BOOST_CHECK(database::has_cal_data("test", "1234", source::RC)); + BOOST_CHECK(database::has_cal_data("test", "1234")); + BOOST_CHECK(!database::has_cal_data("test", "1234", source::FILESYSTEM)); + + const auto test_data = database::read_cal_data("test", "", source::RC); + const std::string test_str(test_data.cbegin(), test_data.cend()); + // The expected string is also in the test.cal file. We could, in this test, + // open that file and dynamically generate the expected data, but let's not. + // First, that adds complexity here, and second, both test.cal and this test + // are hashed with the same git commit, and thus we also test the integrity + // of test.cal. + BOOST_CHECK_EQUAL(test_str, "rc::cal::test_data"); +} + +BOOST_AUTO_TEST_CASE(test_fs) +{ + BOOST_CHECK(!database::has_cal_data("does_not_exist", "1234", source::FILESYSTEM)); + + const auto tmp_dir = uhd::get_tmp_path(); + const auto tmp_cal_path = fs::path(tmp_dir) / "CAL_TEST"; + boost::system::error_code ec; + fs::create_directory(tmp_cal_path, ec); + if (ec) { + std::cout << "WARNING: Could not create temp cal path. Skipping test." + << std::endl; + return; + } + std::cout << "Using temporary cal path: " << tmp_cal_path << std::endl; + + // Now we do a non-portable hack to override the cal path during runtime: +#ifdef UHD_PLATFORM_WIN32 + const std::string putenv_str = + std::string("UHD_CAL_DATA_PATH=") + tmp_cal_path.string(); + _putenv(putenv_str.c_str()); +#else + setenv("UHD_CAL_DATA_PATH", tmp_cal_path.string().c_str(), /* overwrite */ 1); +#endif + + // Because of the hack, we won't fail if it didn't work, but instead, print + // a warning and exit this test. Running the following lines requires the + // hack to succeed. + if (uhd::get_cal_data_path() != tmp_cal_path) { + std::cout << "WARNING: Unable to update UHD_CAL_DATA_PATH! get_cal_data_path(): " + << uhd::get_cal_data_path() << std::endl; + return; + } + + std::vector mock_data{1, 2, 3, 4, 5}; + database::write_cal_data("mock_data", "1234", mock_data); + auto mock_data_rb = database::read_cal_data("mock_data", "1234"); + BOOST_CHECK_EQUAL_COLLECTIONS( + mock_data.begin(), mock_data.end(), mock_data_rb.begin(), mock_data_rb.end()); + + BOOST_CHECK(!database::has_cal_data("mock_data", "abcd")); + std::vector mock_data2{2, 3, 4, 5, 6}; + database::write_cal_data("mock_data", "abcd", mock_data); + // Write it twice to force a backup + database::write_cal_data("mock_data", "abcd", mock_data2, "BACKUP"); + mock_data_rb = database::read_cal_data("mock_data", "abcd"); + BOOST_CHECK_EQUAL_COLLECTIONS( + mock_data2.begin(), mock_data2.end(), mock_data_rb.begin(), mock_data_rb.end()); + BOOST_CHECK(database::has_cal_data("mock_data", "abcd")); + BOOST_CHECK(fs::exists(tmp_cal_path / "mock_data_abcd.cal.BACKUP")); + + fs::remove_all(tmp_cal_path, ec); + if (ec) { + std::cout << "WARNING: Could not remove temp cal path." << std::endl; + } +} -- cgit v1.2.3