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_stream.h"
17#include <arpa/inet.h>
18
19TEST_F(test_tle_udp_stream, stream_test_open)
20{
21	stream = tle_udp_stream_open(ctx,
22			(const struct tle_udp_stream_param *)&stream_prm);
23	EXPECT_NE(stream, nullptr);
24	streams.push_back(stream);
25}
26
27TEST_F(test_tle_udp_stream, stream_test_open_nullctx)
28{
29	stream = tle_udp_stream_open(nullptr,
30			(const struct tle_udp_stream_param *)&stream_prm);
31	EXPECT_EQ(stream, nullptr);
32	EXPECT_EQ(rte_errno, EINVAL);
33
34	ret = tle_udp_stream_close(stream);
35	EXPECT_EQ(ret, -EINVAL);
36}
37
38TEST_F(test_tle_udp_stream, stream_test_open_null_stream_prm)
39{
40	stream = tle_udp_stream_open(ctx, nullptr);
41	EXPECT_EQ(stream, nullptr);
42	EXPECT_EQ(rte_errno, EINVAL);
43
44	ret = tle_udp_stream_close(stream);
45	EXPECT_EQ(ret, -EINVAL);
46}
47
48
49TEST_F(test_tle_udp_stream, stream_test_open_close_open_close)
50{
51	stream = tle_udp_stream_open(ctx,
52			(const struct tle_udp_stream_param *)&stream_prm);
53	EXPECT_NE(stream, nullptr);
54
55	ret = tle_udp_stream_close(stream);
56	EXPECT_EQ(ret, 0);
57
58	stream = tle_udp_stream_open(ctx,
59			(const struct tle_udp_stream_param*)&stream_prm);
60	EXPECT_NE(stream, nullptr);
61
62	streams.push_back(stream);
63}
64
65TEST_F(test_tle_udp_stream, stream_test_open_duplicate)
66{
67	stream = tle_udp_stream_open(ctx,
68			(const struct tle_udp_stream_param *)&stream_prm);
69	EXPECT_NE(stream, nullptr);
70	streams.push_back(stream);
71
72	stream = tle_udp_stream_open(ctx,
73			(const struct tle_udp_stream_param *)&stream_prm);
74	EXPECT_EQ(stream, nullptr);
75	EXPECT_EQ(rte_errno, EEXIST);
76}
77
78TEST_F(test_tle_udp_stream, stream_test_close)
79{
80	stream = tle_udp_stream_open(ctx,
81			(const struct tle_udp_stream_param *)&stream_prm);
82	EXPECT_NE(stream, nullptr);
83
84	ret = tle_udp_stream_close(stream);
85	EXPECT_EQ(ret, 0);
86}
87
88TEST_F(test_tle_udp_stream, stream_test_close_null)
89{
90	ret = tle_udp_stream_close(nullptr);
91	EXPECT_EQ(ret, -EINVAL);
92}
93
94TEST_F(test_tle_udp_stream, stream_test_close_already)
95{
96	stream = tle_udp_stream_open(ctx,
97			(const struct tle_udp_stream_param *)&stream_prm);
98	EXPECT_NE(stream, nullptr);
99
100	ret = tle_udp_stream_close(stream);
101	EXPECT_EQ(ret, 0);
102
103	ret = tle_udp_stream_close(stream);
104	EXPECT_NE(ret, 0);
105	EXPECT_EQ(ret, -EINVAL);
106}
107
108TEST_F(test_tle_udp_stream, stream_get_param)
109{
110	struct tle_udp_stream_param prm;
111
112	stream = tle_udp_stream_open(ctx,
113			(const struct tle_udp_stream_param *)&stream_prm);
114	EXPECT_NE(stream, nullptr);
115	streams.push_back(stream);
116
117	ret = tle_udp_stream_get_param(stream,&prm);
118	EXPECT_EQ(ret, 0);
119}
120
121TEST_F(test_tle_udp_stream, stream_get_param_streamnull)
122{
123	struct tle_udp_stream_param prm;
124
125	stream = tle_udp_stream_open(ctx,
126			(const struct tle_udp_stream_param *)&stream_prm);
127	EXPECT_NE(stream, nullptr);
128	streams.push_back(stream);
129
130	ret = tle_udp_stream_get_param(nullptr, &prm);
131	EXPECT_EQ(ret, -EINVAL);
132}
133
134TEST_F(test_tle_udp_stream, stream_get_param_prmnull)
135{
136	struct tle_udp_stream_param prm;
137
138	stream = tle_udp_stream_open(ctx,
139			(const struct tle_udp_stream_param *)&stream_prm);
140	EXPECT_NE(stream, nullptr);
141	streams.push_back(stream);
142
143	ret = tle_udp_stream_get_param(stream, nullptr);
144	EXPECT_EQ(ret, -EINVAL);
145}
146
147TEST_F(test_tle_udp_stream_max, stream_test_open_max)
148{
149	int i, j, cnt;
150	struct in_addr src_s;
151	struct in_addr dst_s;
152	int dst_port = 32678;
153	struct sockaddr_in *l_ipv4;
154	struct sockaddr_in *r_ipv4;
155
156	/* Set fields that will not change in sockaddr structures */
157	inet_pton(AF_INET, base_l_ipv4, &src_s);
158	l_ipv4 = (struct sockaddr_in *) &stream_prm.local_addr;
159	l_ipv4->sin_family = AF_INET;
160	l_ipv4->sin_port = htons(0);
161
162	inet_pton(AF_INET, base_r_ipv4, &dst_s);
163	r_ipv4 = (struct sockaddr_in *) &stream_prm.remote_addr;
164	r_ipv4->sin_family = AF_INET;
165
166	for(i = 0, cnt = 0; i < devs.size(); i++) {
167		/* Get base IPv4 address and increment it if needed for
168		 * stream source address;
169		 * Incrementing only highest octet
170		 */
171
172		l_ipv4->sin_addr.s_addr = src_s.s_addr + i;
173
174		for(j = 0; j < nb_streams; j++, cnt++) {
175			/* Get base IPv4 address and increment it if needed for
176			 * stream destination  address
177			 */
178			r_ipv4->sin_port = htons(dst_port + j);
179			r_ipv4->sin_addr.s_addr =
180				htonl(ntohl(dst_s.s_addr) + j);
181
182			stream = tle_udp_stream_open(ctx,
183				(const struct tle_udp_stream_param *)
184				&stream_prm);
185
186			if (cnt < MAX_STREAMS) {
187				EXPECT_EQ(rte_errno, 0);
188				ASSERT_NE(stream, nullptr);
189				streams.push_back(stream);
190			} else if (cnt >= MAX_STREAMS) {
191				EXPECT_EQ(stream, nullptr);
192				EXPECT_EQ(rte_errno, ENFILE);
193			}
194		}
195	}
196}
197