| 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 <gpiod.hpp> |
| 6 | #include <sstream> |
| 7 | #include <system_error> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "gpiosim.hpp" |
| 11 | #include "helpers.hpp" |
| 12 | |
| 13 | using ::gpiosim::make_sim; |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | TEST_CASE("chip constructor works", "[chip]") |
| 18 | { |
| 19 | SECTION("open an existing GPIO chip") |
| 20 | { |
| 21 | auto sim = make_sim().build(); |
| 22 | |
| 23 | REQUIRE_NOTHROW(::gpiod::chip(sim.dev_path())); |
| 24 | } |
| 25 | |
| 26 | SECTION("opening a nonexistent file fails with ENOENT") |
| 27 | { |
| 28 | REQUIRE_THROWS_MATCHES(::gpiod::chip("/dev/nonexistent"), |
| 29 | ::std::system_error, system_error_matcher(ENOENT)); |
| 30 | } |
| 31 | |
| 32 | SECTION("opening a file that is not a device fails with ENOTTY") |
| 33 | { |
| 34 | REQUIRE_THROWS_MATCHES(::gpiod::chip("/tmp"), |
| 35 | ::std::system_error, system_error_matcher(ENOTTY)); |
| 36 | } |
| 37 | |
| 38 | SECTION("opening a non-GPIO character device fails with ENODEV") |
| 39 | { |
| 40 | REQUIRE_THROWS_MATCHES(::gpiod::chip("/dev/null"), |
| 41 | ::std::system_error, system_error_matcher(ENODEV)); |
| 42 | } |
| 43 | |
| 44 | SECTION("move constructor") |
| 45 | { |
| 46 | auto sim = make_sim() |
| 47 | .set_label("foobar") |
| 48 | .build(); |
| 49 | |
| 50 | ::gpiod::chip first(sim.dev_path()); |
| 51 | REQUIRE_THAT(first.get_info().label(), Catch::Matchers::Equals("foobar")); |
| 52 | ::gpiod::chip second(::std::move(first)); |
| 53 | REQUIRE_THAT(second.get_info().label(), Catch::Matchers::Equals("foobar")); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | TEST_CASE("chip operators work", "[chip]") |
| 58 | { |
| 59 | auto sim = make_sim() |
| 60 | .set_label("foobar") |
| 61 | .build(); |
| 62 | |
| 63 | ::gpiod::chip chip(sim.dev_path()); |
| 64 | |
| 65 | SECTION("assignment operator") |
| 66 | { |
| 67 | auto moved_sim = make_sim() |
| 68 | .set_label("moved") |
| 69 | .build(); |
| 70 | |
| 71 | ::gpiod::chip moved_chip(moved_sim.dev_path()); |
| 72 | |
| 73 | REQUIRE_THAT(chip.get_info().label(), Catch::Matchers::Equals("foobar")); |
| 74 | chip = ::std::move(moved_chip); |
| 75 | REQUIRE_THAT(chip.get_info().label(), Catch::Matchers::Equals("moved")); |
| 76 | } |
| 77 | |
| 78 | SECTION("boolean operator") |
| 79 | { |
| 80 | REQUIRE(chip); |
| 81 | chip.close(); |
| 82 | REQUIRE_FALSE(chip); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | TEST_CASE("chip properties can be read", "[chip]") |
| 87 | { |
| 88 | auto sim = make_sim() |
| 89 | .set_num_lines(8) |
| 90 | .set_label("foobar") |
| 91 | .build(); |
| 92 | |
| 93 | ::gpiod::chip chip(sim.dev_path()); |
| 94 | |
| 95 | SECTION("get device path") |
| 96 | { |
| 97 | REQUIRE_THAT(chip.path(), Catch::Matchers::Equals(sim.dev_path())); |
| 98 | } |
| 99 | |
| 100 | SECTION("get file descriptor") |
| 101 | { |
| 102 | REQUIRE(chip.fd() >= 0); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | TEST_CASE("line lookup by name works", "[chip]") |
| 107 | { |
| 108 | auto sim = make_sim() |
| 109 | .set_num_lines(8) |
| 110 | .set_line_name(0, "foo") |
| 111 | .set_line_name(2, "bar") |
| 112 | .set_line_name(3, "baz") |
| 113 | .set_line_name(5, "xyz") |
| 114 | .build(); |
| 115 | |
| 116 | ::gpiod::chip chip(sim.dev_path()); |
| 117 | |
| 118 | SECTION("lookup successful") |
| 119 | { |
| 120 | REQUIRE(chip.get_line_offset_from_name("baz") == 3); |
| 121 | } |
| 122 | |
| 123 | SECTION("lookup failed") |
| 124 | { |
| 125 | REQUIRE(chip.get_line_offset_from_name("nonexistent") < 0); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | TEST_CASE("line lookup: behavior for duplicate names", "[chip]") |
| 130 | { |
| 131 | auto sim = make_sim() |
| 132 | .set_num_lines(8) |
| 133 | .set_line_name(0, "foo") |
| 134 | .set_line_name(2, "bar") |
| 135 | .set_line_name(3, "baz") |
| 136 | .set_line_name(5, "bar") |
| 137 | .build(); |
| 138 | |
| 139 | ::gpiod::chip chip(sim.dev_path()); |
| 140 | |
| 141 | REQUIRE(chip.get_line_offset_from_name("bar") == 2); |
| 142 | } |
| 143 | |
| 144 | TEST_CASE("closed chip can no longer be used", "[chip]") |
| 145 | { |
| 146 | auto sim = make_sim().build(); |
| 147 | |
| 148 | ::gpiod::chip chip(sim.dev_path()); |
| 149 | chip.close(); |
| 150 | REQUIRE_THROWS_AS(chip.path(), ::gpiod::chip_closed); |
| 151 | } |
| 152 | |
| 153 | TEST_CASE("stream insertion operator works for chip", "[chip]") |
| 154 | { |
| 155 | auto sim = make_sim() |
| 156 | .set_num_lines(4) |
| 157 | .set_label("foobar") |
| 158 | .build(); |
| 159 | |
| 160 | ::gpiod::chip chip(sim.dev_path()); |
| 161 | ::std::stringstream buf; |
| 162 | |
| 163 | SECTION("open chip") |
| 164 | { |
| 165 | ::std::stringstream expected; |
| 166 | |
| 167 | expected << "gpiod::chip(path=" << sim.dev_path() << |
| 168 | ", info=gpiod::chip_info(name=\"" << sim.name() << |
| 169 | "\", label=\"foobar\", num_lines=4))"; |
| 170 | |
| 171 | buf << chip; |
| 172 | REQUIRE_THAT(buf.str(), Catch::Matchers::Equals(expected.str())); |
| 173 | } |
| 174 | |
| 175 | SECTION("closed chip") |
| 176 | { |
| 177 | chip.close(); |
| 178 | REQUIRE_THAT(chip, stringify_matcher<::gpiod::chip>("gpiod::chip(closed)")); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | } /* namespace */ |