Add initial version
[src/drm-lease-manager.git] / common / test / test-helpers.c
1 /* Copyright 2020-2021 IGEL Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include "test-helpers.h"
17 #include <check.h>
18
19 #include <errno.h>
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 int get_dummy_fd(void)
24 {
25         return dup(STDIN_FILENO);
26 }
27
28 void check_fd_equality(int fd1, int fd2)
29 {
30         struct stat s1, s2;
31         ck_assert_int_eq(fstat(fd1, &s1), 0);
32         ck_assert_int_eq(fstat(fd2, &s2), 0);
33         ck_assert_int_eq(s1.st_dev, s2.st_dev);
34         ck_assert_int_eq(s1.st_ino, s2.st_ino);
35 }
36
37 void check_fd_is_open(int fd)
38 {
39         struct stat st;
40         ck_assert_int_eq(fstat(fd, &st), 0);
41 }
42
43 void check_fd_is_closed(int fd)
44 {
45         struct stat st;
46         ck_assert_int_ne(fstat(fd, &st), 0);
47         ck_assert_int_eq(errno, EBADF);
48 }
49
50 void check_uint_array_eq(const uint32_t *a, const uint32_t *b, int cnt)
51 {
52         for (int i = 0; i < cnt; i++) {
53                 ck_assert_msg(a[i] == b[i], "Array diff at index %d (%u != %u)",
54                               i, a[i], b[i]);
55         }
56 }