| 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // SPDX-FileCopyrightText: 2021-2022 Bartosz Golaszewski <[email protected]> |
| 3 | |
| 4 | #include <catch2/catch_all.hpp> |
| 5 | #include <filesystem> |
| 6 | #include <gpiod.hpp> |
| 7 | #include <string> |
| 8 | #include <regex> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include "gpiosim.hpp" |
| 12 | #include "helpers.hpp" |
| 13 | |
| 14 | using ::gpiosim::make_sim; |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class symlink_guard |
| 19 | { |
| 20 | public: |
| 21 | symlink_guard(const ::std::filesystem::path& target, |
| 22 | const ::std::filesystem::path& link) |
| 23 | : _m_link(link) |
| 24 | { |
| 25 | ::std::filesystem::create_symlink(target, this->_m_link); |
| 26 | } |
| 27 | |
| 28 | ~symlink_guard() |
| 29 | { |
| 30 | ::std::filesystem::remove(this->_m_link); |
| 31 | } |
| 32 | |
| 33 | private: |
| 34 | ::std::filesystem::path _m_link; |
| 35 | }; |
| 36 | |
| 37 | TEST_CASE("is_gpiochip_device() works", "[misc][chip]") |
| 38 | { |
| 39 | SECTION("is_gpiochip_device() returns false for /dev/null") |
| 40 | { |
| 41 | REQUIRE_FALSE(::gpiod::is_gpiochip_device("/dev/null")); |
| 42 | } |
| 43 | |
| 44 | SECTION("is_gpiochip_device() returns false for nonexistent file") |
| 45 | { |
| 46 | REQUIRE_FALSE(::gpiod::is_gpiochip_device("/dev/nonexistent")); |
| 47 | } |
| 48 | |
| 49 | SECTION("is_gpiochip_device() returns true for a GPIO chip") |
| 50 | { |
| 51 | auto sim = make_sim().build(); |
| 52 | |
| 53 | REQUIRE(::gpiod::is_gpiochip_device(sim.dev_path())); |
| 54 | } |
| 55 | |
| 56 | SECTION("is_gpiochip_device() can resolve a symlink") |
| 57 | { |
| 58 | auto sim = make_sim().build(); |
| 59 | ::std::string link("/tmp/gpiod-cxx-tmp-link."); |
| 60 | |
| 61 | link += ::std::to_string(::getpid()); |
| 62 | |
| 63 | symlink_guard link_guard(sim.dev_path(), link); |
| 64 | |
| 65 | REQUIRE(::gpiod::is_gpiochip_device(link)); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | TEST_CASE("api_version() returns a valid API version", "[misc]") |
| 70 | { |
| 71 | SECTION("check api_version() format") |
| 72 | { |
| 73 | REQUIRE_THAT(::gpiod::api_version(), |
| 74 | regex_matcher("^\\d+\\.\\d+(\\.\\d+|\\-devel|\\-rc\\d+)?$")); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | } /* namespace */ |