| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <errno.h> |
| 5 |
#include <glib.h> |
| 6 |
#include <gpiod.h> |
| 7 |
#include <gpiod-test.h> |
| 8 |
#include <gpiod-test-common.h> |
| 9 |
#include <gpiosim-glib.h> |
| 10 |
|
| 11 |
#include "helpers.h" |
| 12 |
|
| 13 |
#define GPIOD_TEST_GROUP "chip" |
| 14 |
|
| 15 |
GPIOD_TEST_CASE(open_chip_good) |
| 16 |
{ |
| 17 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL); |
| 18 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 19 |
|
| 20 |
chip = gpiod_chip_open(g_gpiosim_chip_get_dev_path(sim)); |
| 21 |
g_assert_nonnull(chip); |
| 22 |
} |
| 23 |
|
| 24 |
GPIOD_TEST_CASE(open_chip_nonexistent) |
| 25 |
{ |
| 26 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 27 |
|
| 28 |
chip = gpiod_chip_open("/dev/nonexistent"); |
| 29 |
g_assert_null(chip); |
| 30 |
gpiod_test_expect_errno(ENOENT); |
| 31 |
} |
| 32 |
|
| 33 |
GPIOD_TEST_CASE(open_chip_not_a_character_device) |
| 34 |
{ |
| 35 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 36 |
|
| 37 |
chip = gpiod_chip_open("/tmp"); |
| 38 |
g_assert_null(chip); |
| 39 |
gpiod_test_expect_errno(ENOTTY); |
| 40 |
} |
| 41 |
|
| 42 |
GPIOD_TEST_CASE(open_chip_not_a_gpio_device) |
| 43 |
{ |
| 44 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 45 |
|
| 46 |
chip = gpiod_chip_open("/dev/null"); |
| 47 |
g_assert_null(chip); |
| 48 |
gpiod_test_expect_errno(ENODEV); |
| 49 |
} |
| 50 |
|
| 51 |
GPIOD_TEST_CASE(open_chip_null_path) |
| 52 |
{ |
| 53 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 54 |
|
| 55 |
chip = gpiod_chip_open(NULL); |
| 56 |
g_assert_null(chip); |
| 57 |
gpiod_test_expect_errno(EINVAL); |
| 58 |
} |
| 59 |
|
| 60 |
GPIOD_TEST_CASE(get_chip_path) |
| 61 |
{ |
| 62 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL); |
| 63 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 64 |
const gchar *path = g_gpiosim_chip_get_dev_path(sim); |
| 65 |
|
| 66 |
chip = gpiod_test_open_chip_or_fail(path); |
| 67 |
|
| 68 |
g_assert_cmpstr(gpiod_chip_get_path(chip), ==, path); |
| 69 |
} |
| 70 |
|
| 71 |
GPIOD_TEST_CASE(get_fd) |
| 72 |
{ |
| 73 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL); |
| 74 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 75 |
|
| 76 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 77 |
|
| 78 |
g_assert_cmpint(gpiod_chip_get_fd(chip), >=, 0); |
| 79 |
} |
| 80 |
|
| 81 |
GPIOD_TEST_CASE(find_line_bad) |
| 82 |
{ |
| 83 |
static const GPIOSimLineName names[] = { |
| 84 |
{ .offset = 1, .name = "foo", }, |
| 85 |
{ .offset = 2, .name = "bar", }, |
| 86 |
{ .offset = 4, .name = "baz", }, |
| 87 |
{ .offset = 5, .name = "xyz", }, |
| 88 |
{ } |
| 89 |
}; |
| 90 |
|
| 91 |
g_autoptr(GPIOSimChip) sim = NULL; |
| 92 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 93 |
g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names); |
| 94 |
|
| 95 |
sim = g_gpiosim_chip_new( |
| 96 |
"num-lines", 8, |
| 97 |
"line-names", vnames, |
| 98 |
NULL); |
| 99 |
|
| 100 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 101 |
|
| 102 |
g_assert_cmpint( |
| 103 |
gpiod_chip_get_line_offset_from_name(chip, |
| 104 |
"nonexistent"), ==, -1); |
| 105 |
gpiod_test_expect_errno(ENOENT); |
| 106 |
} |
| 107 |
|
| 108 |
GPIOD_TEST_CASE(find_line_good) |
| 109 |
{ |
| 110 |
static const GPIOSimLineName names[] = { |
| 111 |
{ .offset = 1, .name = "foo", }, |
| 112 |
{ .offset = 2, .name = "bar", }, |
| 113 |
{ .offset = 4, .name = "baz", }, |
| 114 |
{ .offset = 5, .name = "xyz", }, |
| 115 |
{ } |
| 116 |
}; |
| 117 |
|
| 118 |
g_autoptr(GPIOSimChip) sim = NULL; |
| 119 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 120 |
g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names); |
| 121 |
|
| 122 |
sim = g_gpiosim_chip_new( |
| 123 |
"num-lines", 8, |
| 124 |
"line-names", vnames, |
| 125 |
NULL); |
| 126 |
|
| 127 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 128 |
|
| 129 |
g_assert_cmpint(gpiod_chip_get_line_offset_from_name(chip, "baz"), |
| 130 |
==, 4); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
GPIOD_TEST_CASE(find_line_duplicate) |
| 135 |
{ |
| 136 |
static const GPIOSimLineName names[] = { |
| 137 |
{ .offset = 1, .name = "foo", }, |
| 138 |
{ .offset = 2, .name = "baz", }, |
| 139 |
{ .offset = 4, .name = "baz", }, |
| 140 |
{ .offset = 5, .name = "xyz", }, |
| 141 |
{ } |
| 142 |
}; |
| 143 |
|
| 144 |
g_autoptr(GPIOSimChip) sim = NULL; |
| 145 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 146 |
g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names); |
| 147 |
|
| 148 |
sim = g_gpiosim_chip_new( |
| 149 |
"num-lines", 8, |
| 150 |
"line-names", vnames, |
| 151 |
NULL); |
| 152 |
|
| 153 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 154 |
|
| 155 |
g_assert_cmpint(gpiod_chip_get_line_offset_from_name(chip, "baz"), |
| 156 |
==, 2); |
| 157 |
} |
| 158 |
|
| 159 |
GPIOD_TEST_CASE(find_line_non_standard_names) |
| 160 |
{ |
| 161 |
static const GPIOSimLineName names[] = { |
| 162 |
{ .offset = 1, .name = "with whitespace", }, |
| 163 |
{ .offset = 2, .name = "[:-]chars", }, |
| 164 |
{ .offset = 3, .name = "l", }, |
| 165 |
{ .offset = 6, .name = "ALLCAPS", }, |
| 166 |
{ } |
| 167 |
}; |
| 168 |
|
| 169 |
g_autoptr(GVariant) vnames = g_gpiosim_package_line_names(names); |
| 170 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, |
| 171 |
"line-names", vnames, |
| 172 |
NULL); |
| 173 |
|
| 174 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 175 |
|
| 176 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 177 |
|
| 178 |
g_assert_cmpint(gpiod_chip_get_line_offset_from_name(chip, |
| 179 |
"with whitespace"), |
| 180 |
==, 1); |
| 181 |
g_assert_cmpint(gpiod_chip_get_line_offset_from_name(chip, "[:-]chars"), |
| 182 |
==, 2); |
| 183 |
g_assert_cmpint(gpiod_chip_get_line_offset_from_name(chip, "l"), |
| 184 |
==, 3); |
| 185 |
g_assert_cmpint(gpiod_chip_get_line_offset_from_name(chip, "ALLCAPS"), |
| 186 |
==, 6); |
| 187 |
} |
| 188 |
|
| 189 |
GPIOD_TEST_CASE(find_line_null_name) |
| 190 |
{ |
| 191 |
g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new(NULL); |
| 192 |
g_autoptr(struct_gpiod_chip) chip = NULL; |
| 193 |
gint ret; |
| 194 |
|
| 195 |
chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim)); |
| 196 |
|
| 197 |
ret = gpiod_chip_get_line_offset_from_name(chip, NULL); |
| 198 |
g_assert_cmpint(ret, ==, -1); |
| 199 |
gpiod_test_expect_errno(EINVAL); |
| 200 |
} |