tldk: fix possibility of tle_event_active() from DOWN to UP
[tldk.git] / test / gtest / test_tle_udp_event.cpp
1 /*
2  * Copyright (c) 2016  Intel Corporation.
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_tle_udp_event.h"
17
18 TEST_F(udp_evq, udp_evq_create_null)
19 {
20         evq = tle_evq_create(NULL);
21         EXPECT_EQ(evq, (struct tle_evq *) NULL);
22         EXPECT_EQ(rte_errno, EINVAL);
23 }
24
25 TEST_F(udp_evq, udp_evq_create_invalid_socket)
26 {
27         evq_params.socket_id = 999;
28         evq = tle_evq_create(&evq_params);
29         ASSERT_EQ(evq, (struct tle_evq *) NULL);
30 }
31
32 TEST_F(udp_evq, udp_evq_create_destroy_positive)
33 {
34         evq = tle_evq_create(&evq_params);
35         ASSERT_NE(evq, (struct tle_evq *) NULL);
36         EXPECT_EQ(rte_errno, 0);
37         EXPECT_EQ(evq->nb_events, max_events);
38         EXPECT_EQ(evq->nb_free, max_events);
39         tle_evq_destroy(evq);
40         EXPECT_EQ(rte_errno, 0);
41 }
42
43 TEST_F(udp_event, udp_event_alloc_null)
44 {
45         event = tle_event_alloc(NULL, (void *) &fake_data);
46         EXPECT_EQ(event, (struct tle_event *) NULL);
47         EXPECT_EQ(rte_errno, EINVAL);
48 }
49
50 TEST_F(udp_event, udp_event_free_null)
51 {
52         tle_event_free(NULL);
53         EXPECT_EQ(rte_errno, EINVAL);
54 }
55
56 TEST_F(udp_event, udp_event_alloc_free_positive)
57 {
58         event = tle_event_alloc(evq, (void *) &fake_data);
59         ASSERT_NE(event, (struct tle_event *) NULL);
60         EXPECT_EQ(rte_errno, 0);
61         EXPECT_EQ(evq->nb_free, max_events - 1);
62         tle_event_free(event);
63         EXPECT_EQ(rte_errno, 0);
64         EXPECT_EQ(evq->nb_free, max_events);
65 }
66
67 TEST_F(udp_event, udp_event_alloc_free_max_reached)
68 {
69         uint32_t i;
70         struct tle_event *last_event;
71
72         for (i = 1; i <= max_events; i++) {
73                 event = tle_event_alloc(evq, (void *) &fake_data);
74                 ASSERT_NE(event, (struct tle_event *) NULL);
75                 EXPECT_EQ(rte_errno, 0);
76                 EXPECT_EQ(evq->nb_free, max_events - i);
77         }
78
79         last_event = tle_event_alloc(evq, (void *) &fake_data);
80         ASSERT_EQ(last_event, (struct tle_event *) NULL);
81         ASSERT_EQ(rte_errno, ENOMEM);
82
83         for (i = 1; i <= max_events; i++) {
84                 tle_event_free(event);
85         }
86         EXPECT_EQ(evq->nb_free, max_events);
87 }
88
89 TEST_F(udp_event_state, udp_state_default)
90 {
91         ASSERT_EQ(event->state, TLE_SEV_IDLE);
92 }
93
94 TEST_P(udp_event_state_active, udp_state_active)
95 {
96         auto states = GetParam();
97
98         tle_event_active(event, states.event_state);
99         ASSERT_EQ(event->state, states.event_state);
100         EXPECT_EQ(rte_errno, 0);
101 }
102
103 INSTANTIATE_TEST_CASE_P(Default, udp_event_state_active,
104         testing::Values(
105         event_state_active{TLE_SEV_IDLE},
106         event_state_active{TLE_SEV_UP},
107         event_state_active{TLE_SEV_DOWN}
108 ));
109
110 TEST_P(udp_event_state_active_twice, udp_state_active_twice)
111 {
112         auto states = GetParam();
113
114         tle_event_active(event, states.first_state);
115         ASSERT_EQ(event->state, states.first_state);
116         EXPECT_EQ(rte_errno, 0);
117         tle_event_active(event, states.second_state);
118         ASSERT_EQ(event->state, states.result_state);
119         EXPECT_EQ(rte_errno, 0);
120 }
121
122 INSTANTIATE_TEST_CASE_P(Default, udp_event_state_active_twice,
123         testing::Values(
124         event_state_active_twice{TLE_SEV_IDLE, TLE_SEV_IDLE, TLE_SEV_IDLE},
125         event_state_active_twice{TLE_SEV_IDLE, TLE_SEV_DOWN, TLE_SEV_DOWN},
126         event_state_active_twice{TLE_SEV_IDLE, TLE_SEV_UP, TLE_SEV_UP},
127         event_state_active_twice{TLE_SEV_DOWN, TLE_SEV_IDLE, TLE_SEV_DOWN},
128         event_state_active_twice{TLE_SEV_DOWN, TLE_SEV_UP, TLE_SEV_DOWN},
129         event_state_active_twice{TLE_SEV_DOWN, TLE_SEV_DOWN, TLE_SEV_DOWN},
130         event_state_active_twice{TLE_SEV_UP, TLE_SEV_IDLE, TLE_SEV_UP},
131         event_state_active_twice{TLE_SEV_UP, TLE_SEV_DOWN, TLE_SEV_UP},
132         event_state_active_twice{TLE_SEV_UP, TLE_SEV_UP, TLE_SEV_UP}
133 ));
134
135 TEST_F(udp_event_state, udp_state_raise)
136 {
137         tle_event_raise(event);
138         ASSERT_EQ(event->state, TLE_SEV_IDLE);
139         EXPECT_EQ(rte_errno, 0);
140         tle_event_active(event, TLE_SEV_DOWN);
141         ASSERT_EQ(event->state, TLE_SEV_DOWN);
142         EXPECT_EQ(rte_errno, 0);
143         tle_event_raise(event);
144         ASSERT_EQ(event->state, TLE_SEV_UP);
145         EXPECT_EQ(rte_errno, 0);
146 }
147
148 TEST_F(udp_event_state, udp_state_down)
149 {
150         tle_event_down(event);
151         ASSERT_EQ(event->state, TLE_SEV_IDLE);
152         EXPECT_EQ(rte_errno, 0);
153         tle_event_active(event, TLE_SEV_UP);
154         ASSERT_EQ(event->state, TLE_SEV_UP);
155         EXPECT_EQ(rte_errno, 0);
156         tle_event_down(event);
157         ASSERT_EQ(event->state, TLE_SEV_DOWN);
158         EXPECT_EQ(rte_errno, 0);
159 }
160
161 TEST_P(udp_event_state_idle, udp_state_idle)
162 {
163         auto states = GetParam();
164
165         tle_event_active(event, states.event_state);
166         ASSERT_EQ(event->state, states.event_state);
167         EXPECT_EQ(rte_errno, 0);
168         tle_event_idle(event);
169         ASSERT_EQ(event->state, TLE_SEV_IDLE);
170         EXPECT_EQ(rte_errno, 0);
171 }
172
173 INSTANTIATE_TEST_CASE_P(Default, udp_event_state_idle,
174         testing::Values(
175         event_state_active{TLE_SEV_IDLE},
176         event_state_active{TLE_SEV_UP},
177         event_state_active{TLE_SEV_DOWN}
178 ));
179
180 TEST_F(udp_event, udp_event_get)
181 {
182         uint32_t i;
183         const void **evd;
184
185         evd = (const void **) malloc(max_events * sizeof(void *));
186         for (i = 1; i <= max_events; i++) {
187                 event = tle_event_alloc(evq, (void *) &fake_data);
188                 EXPECT_NE(event, (struct tle_event *) NULL);
189                 tle_event_active(event, TLE_SEV_UP);
190                 EXPECT_EQ(event->state, TLE_SEV_UP);
191         }
192         EXPECT_EQ(evq->nb_free, 0);
193         EXPECT_EQ(tle_evq_get(evq, evd, max_events), max_events);
194         free(evd);
195 }