Add plane setting to connector configuration
[src/drm-lease-manager.git] / drm-lease-manager / test / lease-config-test.c
1 /* Copyright 2022 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 "lease-config.h"
17 #include <check.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #define CONFIG_FILE_TEMPLATE "/tmp/dlmconfig_tmpXXXXXX"
23 int config_fd;
24 char config_file[] = CONFIG_FILE_TEMPLATE;
25
26 static void test_setup(void)
27 {
28         strcpy(config_file, CONFIG_FILE_TEMPLATE);
29         config_fd = mkstemp(config_file);
30         ck_assert_int_ge(config_fd, 0);
31 }
32
33 static void test_shutdown(void)
34 {
35         close(config_fd);
36         unlink(config_file);
37 }
38
39 /* parse config file test */
40 /* Test details: Parse a config file
41  * Expected results: a config with the expected results
42  */
43
44 START_TEST(parse_leases)
45 {
46         ck_assert_ptr_ne(config_file, NULL);
47
48         char test_data[] = "[[lease]]\n"
49                            "name = \"lease 1\"\n"
50                            "connectors = [\"1\", \"b\",\"gamma\" ]\n"
51                            "[[lease]]\n"
52                            "name = \"lease 2\"\n"
53                            "connectors = [\"connector 3\"]\n";
54
55         write(config_fd, test_data, sizeof(test_data));
56
57         struct lease_config *config = NULL;
58         int nconfigs = parse_config(config_file, &config);
59
60         ck_assert_int_eq(nconfigs, 2);
61         ck_assert_ptr_ne(config, NULL);
62
63         ck_assert_str_eq(config[0].lease_name, "lease 1");
64         ck_assert_int_eq(config[0].nconnectors, 3);
65         ck_assert_str_eq(config[0].connectors[0].name, "1");
66         ck_assert_str_eq(config[0].connectors[1].name, "b");
67         ck_assert_str_eq(config[0].connectors[2].name, "gamma");
68
69         ck_assert_str_eq(config[1].lease_name, "lease 2");
70         ck_assert_int_eq(config[1].nconnectors, 1);
71         ck_assert_str_eq(config[1].connectors[0].name, "connector 3");
72
73         release_config(nconfigs, config);
74 }
75 END_TEST
76
77 START_TEST(connector_config)
78 {
79         ck_assert_ptr_ne(config_file, NULL);
80
81         char test_data[] = "[[lease]]\n"
82                            "name = \"lease 1\"\n"
83                            "connectors = [\"1\", \"b\",\"gamma\" ]\n"
84                            "[b]\n"
85                            "optional = true\n"
86                            "planes = [1, 4, 3]\n";
87
88         write(config_fd, test_data, sizeof(test_data));
89
90         struct lease_config *config = NULL;
91         int nconfigs = parse_config(config_file, &config);
92
93         ck_assert_int_eq(nconfigs, 1);
94         ck_assert_ptr_ne(config, NULL);
95
96         ck_assert_str_eq(config[0].lease_name, "lease 1");
97         ck_assert_int_eq(config[0].nconnectors, 3);
98         ck_assert_str_eq(config[0].connectors[0].name, "1");
99         ck_assert_str_eq(config[0].connectors[1].name, "b");
100         ck_assert_str_eq(config[0].connectors[2].name, "gamma");
101
102         ck_assert(!config[0].connectors[0].optional);
103         ck_assert(config[0].connectors[1].optional);
104         ck_assert(!config[0].connectors[2].optional);
105
106         ck_assert_int_eq(config[0].connectors[1].nplanes, 3);
107         ck_assert_int_eq(config[0].connectors[1].planes[0], 1);
108         ck_assert_int_eq(config[0].connectors[1].planes[1], 4);
109         ck_assert_int_eq(config[0].connectors[1].planes[2], 3);
110
111         release_config(nconfigs, config);
112 }
113 END_TEST
114 static void add_parse_tests(Suite *s)
115 {
116         TCase *tc = tcase_create("Config file parsing tests");
117
118         tcase_add_checked_fixture(tc, test_setup, test_shutdown);
119
120         tcase_add_test(tc, parse_leases);
121         tcase_add_test(tc, connector_config);
122         suite_add_tcase(s, tc);
123 }
124
125 int main(void)
126 {
127         int number_failed;
128         Suite *s;
129         SRunner *sr;
130
131         s = suite_create("DLM lease manager tests");
132
133         add_parse_tests(s);
134
135         sr = srunner_create(s);
136         srunner_run_all(sr, CK_NORMAL);
137         number_failed = srunner_ntests_failed(sr);
138         srunner_free(sr);
139         return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
140 }