test/lease-manager: Create dummy fds for fake lease grants
[src/drm-lease-manager.git] / drm-lease-manager / test / test-drm-device.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 <check.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <xf86drmMode.h>
22
23 #include "test-drm-device.h"
24 #define UNUSED(x) (void)(x)
25
26 /* Set the base value for IDs of each resource type.
27  * These can be adjusted if test cases need more IDs. */
28 #define IDS_PER_RES_TYPE 32
29
30 #define CRTC_BASE (IDS_PER_RES_TYPE)
31 #define CONNECTOR_BASE (CRTC_BASE + IDS_PER_RES_TYPE)
32 #define ENCODER_BASE (CONNECTOR_BASE + IDS_PER_RES_TYPE)
33 #define PLANE_BASE (ENCODER_BASE + IDS_PER_RES_TYPE)
34 #define LESSEE_ID_BASE (PLANE_BASE + IDS_PER_RES_TYPE)
35
36 struct drm_device test_device;
37
38 #define ALLOC_RESOURCE(res, container)                           \
39         do {                                                     \
40                 if (res != 0) {                                  \
41                         test_device.container.res =              \
42                             malloc(sizeof(uint32_t) * res);      \
43                         if (!test_device.container.res)          \
44                                 return false;                    \
45                         test_device.container.count_##res = res; \
46                 }                                                \
47         } while (0)
48
49 #define FILL_RESOURCE(res, RES, container)                     \
50         for (int i = 0; i < res; i++) {                        \
51                 test_device.container.res[i] = RES##_BASE + i; \
52         }
53
54 bool setup_drm_test_device(int crtcs, int connectors, int encoders, int planes)
55 {
56         int lessee_ids = crtcs;
57         ALLOC_RESOURCE(crtcs, resources);
58         ALLOC_RESOURCE(connectors, resources);
59         ALLOC_RESOURCE(encoders, resources);
60         ALLOC_RESOURCE(planes, plane_resources);
61         ALLOC_RESOURCE(lessee_ids, leases);
62
63         FILL_RESOURCE(crtcs, CRTC, resources);
64         FILL_RESOURCE(connectors, CONNECTOR, resources);
65         FILL_RESOURCE(encoders, ENCODER, resources);
66         FILL_RESOURCE(planes, PLANE, plane_resources);
67         FILL_RESOURCE(lessee_ids, LESSEE_ID, leases);
68
69         return true;
70 }
71
72 void reset_drm_test_device(void)
73 {
74         free(test_device.resources.crtcs);
75         free(test_device.resources.connectors);
76         free(test_device.resources.encoders);
77         free(test_device.plane_resources.planes);
78         free(test_device.leases.lessee_ids);
79         memset(&test_device, 0, sizeof(test_device));
80 }
81
82 void setup_test_device_layout(drmModeConnector *connectors,
83                               drmModeEncoder *encoders, drmModePlane *planes)
84 {
85         test_device.layout.connectors = connectors;
86         test_device.layout.encoders = encoders;
87         test_device.layout.planes = planes;
88 }
89
90 #define GET_DRM_RESOURCE_FN(Res, res, RES, container)                       \
91         drmMode##Res##Ptr get_##res(int fd, uint32_t id)                    \
92         {                                                                   \
93                 UNUSED(fd);                                                 \
94                 if (id == 0)                                                \
95                         return NULL;                                        \
96                 ck_assert_int_ge(id, RES##_BASE);                           \
97                 ck_assert_int_lt(                                           \
98                     id, RES##_BASE + test_device.container.count_##res##s); \
99                 return &test_device.layout.res##s[id - RES##_BASE];         \
100         }
101
102 GET_DRM_RESOURCE_FN(Connector, connector, CONNECTOR, resources)
103 GET_DRM_RESOURCE_FN(Encoder, encoder, ENCODER, resources)
104 GET_DRM_RESOURCE_FN(Plane, plane, PLANE, plane_resources)
105
106 int create_lease(int fd, const uint32_t *objects, int num_objects, int flags,
107                  uint32_t *lessee_id)
108 {
109         UNUSED(objects);
110         UNUSED(num_objects);
111         UNUSED(flags);
112
113         int lease_count = test_device.leases.count;
114         if (lease_count < test_device.leases.count_lessee_ids)
115                 *lessee_id = test_device.leases.lessee_ids[lease_count];
116         else
117                 *lessee_id = 0;
118
119         test_device.leases.count++;
120
121         return dup(fd);
122 }