Replace () with (void) in function prototypes
[platform/core/system/dlog.git] / src / tests / fdi_pipe_pos.c
1 /*
2  * Copyright (c) 2019-2020, Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "fdi_pipe_wrap.c"
18
19 int main(void)
20 {
21         struct fd_info info = {
22                 .ops = &ops_pipe,
23                 .id = 213,
24         };
25
26         struct log_config conf = {};
27
28         list_head *fake_list = (list_head *)54321; // It's not used anyway :)
29         log_config_set(&conf, "kmsg_ctl_sock", the_right_path);
30         malloc_fail = false;
31
32         assert(ops_pipe.create(&info, &conf, LOG_ID_KMSG, fake_list, &(log_id_t){ LOG_ID_INVALID }) == 0);
33         assert(last_sock == 1 && !closed[0]);
34
35         struct pipe_priv_data *ppd = info.priv_data;
36         assert(ppd->data_len == 0);
37         assert(ppd->offset == 0);
38         assert(ppd->sock_fd == 1);
39         assert(!ops_pipe.has_log(&info));
40
41         struct log_filter *filter = log_filter_new();
42         assert(filter);
43         log_filter_set_filterspec(filter, "filter0");
44
45         correct_sockfd = 1;
46         correct_request = DLOG_REQ_HANDLE_LOGUTIL;
47
48         log_filter_set_filterspec(filter, "filter1");
49         log_filter_set_tid(filter, 123);
50         log_filter_set_pid(filter, 456);
51
52         send_return = 0;
53
54         recv_pipe_fail = false;
55         correct_send_data = "dlogutil --pid 456 --tid 123 filter1:V filter0:V *:S";
56         correct_send_datalen = strlen(correct_send_data) + 1;
57         assert(ops_pipe.prepare_print(&info, false, false, filter) == 0);
58         assert(info.fd == 2);
59
60         log_filter_free(filter);
61         correct_sockfd = 2;
62         read_errno = 0;
63
64         read_datalen = sizeof(dlogutil_entry_s) + 4;
65         dlogutil_entry_s *ent = read_data = malloc(read_datalen);
66         assert(ent);
67         read_pos = 0;
68         ent->len = read_datalen;
69         memcpy(ent->msg, "abc", 4);
70         assert(ops_pipe.read(&info) == read_datalen);
71         assert(read_pos == read_datalen);
72
73         assert(ops_pipe.has_log(&info));
74         assert(!memcmp(ent, ops_pipe.peek_entry(&info), ent->len));
75         malloc_fail = false;
76
77         dlogutil_entry_s *ent2 = ops_pipe.extract_entry(&info);
78         assert(!memcmp(ent, ent2, ent->len));
79         __real_free(ent2);
80
81         int multiplier = RECEIVE_BUFFER_SIZE / ent->len + 1;
82         read_datalen = ent->len * multiplier;
83         ent = read_data = realloc(ent, read_datalen);
84         assert(ent);
85         read_pos = 0;
86         for (int i = 1; i < multiplier; ++i)
87                 memcpy(read_data + ent->len * i, ent, ent->len);
88
89         for (;;) {
90                 int ret = ops_pipe.read(&info);
91
92                 switch (ret) {
93                 case 0:
94                         assert(read_pos == read_datalen);
95                         goto ok;
96                 case -EAGAIN:
97                         assert(ops_pipe.has_log(&info));
98                         ent2 = ops_pipe.extract_entry(&info);
99                         assert(!memcmp(ent, ent2, ent->len));
100                         __real_free(ent2);
101                         break;
102                 default:
103                         assert(ret > 0);
104                         break;
105                 }
106         }
107 ok:
108         while (ops_pipe.has_log(&info)) {
109                 ent2 = ops_pipe.extract_entry(&info);
110                 assert(!memcmp(ent, ent2, ent->len));
111                 __real_free(ent2);
112         }
113         __real_free(ent);
114
115         read_datalen = sizeof(dlogutil_entry_s);
116         read_data = calloc(1, read_datalen);
117         read_pos = 0;
118         assert(ops_pipe.read(&info) == read_datalen);
119         assert(read_pos == read_datalen);
120         assert(ops_pipe.has_log(&info));
121         assert(!ops_pipe.extract_entry(&info));
122         __real_free(read_data);
123
124         correct_sockfd = 1;
125         correct_request = DLOG_REQ_CLEAR;
126         correct_send_data = NULL;
127         correct_send_datalen = 0;
128
129         send_return = 0;
130         assert(ops_pipe.clear(&info) == 0);
131
132         correct_request = DLOG_REQ_GET_CAPACITY;
133         unsigned int size_target = 0, usage_target = 0;
134         recv_return = 0;
135
136         recv_datalen = sizeof(unsigned int);
137         recv_data = malloc(recv_datalen);
138         assert(recv_data);
139         *(int *)recv_data = (unsigned int)17;
140         ptr_to_free = recv_data;
141         assert(ops_pipe.get_capacity(&info, &size_target) == 0);
142         assert(ptr_to_free == NULL);
143
144         correct_request = DLOG_REQ_GET_USAGE;
145         recv_data = malloc(recv_datalen);
146         assert(recv_data);
147         *(int *)recv_data = (unsigned int)235U;
148         ptr_to_free = recv_data;
149         assert(ops_pipe.get_usage(&info, &usage_target) == 0);
150         assert(ptr_to_free == NULL);
151
152         assert(size_target == 17);
153         assert(usage_target == 235U);
154
155         ptr_to_free = ppd;
156         ops_pipe.destroy(&info);
157         assert(closed[0]);
158         assert(ptr_to_free == NULL);
159 }