Skip to content

Commit 2b8ea7d

Browse files
cfriedtstephanosio
authored andcommitted
tests: lib: thrift: add tests for the thrift module
These tests include: * ThriftTest - an upstream exercies for all Thrift facilities This code was merged from the following repository at the commit specified below, with minor formatting and coding-style modifications. https://github.com/zephyrproject-rtos/gsoc-2022-thrift e12e014d295918cc5ba0b4c507d1bf595a2f539a Signed-off-by: Chris Friedt <[email protected]>
1 parent 0c00a3e commit 2b8ea7d

File tree

9 files changed

+920
-0
lines changed

9 files changed

+920
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2022 Meta
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
cmake_minimum_required(VERSION 3.20.0)
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(thrift_test)
7+
8+
set(THRIFT_UPSTREAM ${ZEPHYR_THRIFT_MODULE_DIR})
9+
10+
include(${ZEPHYR_BASE}/modules/thrift/cmake/thrift.cmake)
11+
12+
FILE(GLOB app_sources
13+
src/*.cpp
14+
)
15+
16+
set(generated_sources "")
17+
set(gen_dir ${ZEPHYR_BINARY_DIR}/misc/generated/thrift_ThriftTest)
18+
list(APPEND generated_sources ${gen_dir}/gen-cpp/SecondService.cpp)
19+
list(APPEND generated_sources ${gen_dir}/gen-cpp/SecondService.h)
20+
list(APPEND generated_sources ${gen_dir}/gen-cpp/ThriftTest_constants.cpp)
21+
list(APPEND generated_sources ${gen_dir}/gen-cpp/ThriftTest_constants.h)
22+
list(APPEND generated_sources ${gen_dir}/gen-cpp/ThriftTest.cpp)
23+
list(APPEND generated_sources ${gen_dir}/gen-cpp/ThriftTest.h)
24+
list(APPEND generated_sources ${gen_dir}/gen-cpp/ThriftTest_types.cpp)
25+
list(APPEND generated_sources ${gen_dir}/gen-cpp/ThriftTest_types.h)
26+
list(APPEND app_sources ${generated_sources})
27+
28+
thrift(
29+
app
30+
cpp
31+
:no_skeleton
32+
${gen_dir}
33+
# v0.16: ubuntu packaged thrift compiler does not support 'uuid' type
34+
"${THRIFT_UPSTREAM}/test/v0.16/ThriftTest.thrift"
35+
""
36+
${generated_sources}
37+
)
38+
39+
target_sources(app PRIVATE ${app_sources})
40+
41+
# needed because std::iterator was deprecated with -std=c++17
42+
target_compile_options(app PRIVATE -Wno-deprecated-declarations)
43+
44+
# convert .pem files to array data at build time
45+
zephyr_include_directories(${gen_dir})
46+
47+
generate_inc_file_for_target(
48+
app
49+
${ZEPHYR_BASE}/samples/modules/thrift/hello/qemu-cert.pem
50+
${gen_dir}/qemu_cert.pem.inc
51+
)
52+
53+
generate_inc_file_for_target(
54+
app
55+
${ZEPHYR_BASE}/samples/modules/thrift/hello/qemu-key.pem
56+
${gen_dir}/qemu_key.pem.inc
57+
)

tests/lib/thrift/ThriftTest/Kconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
# Copyright (c) 2022 Meta
3+
4+
source "Kconfig.zephyr"
5+
6+
config THRIFTTEST_SERVER_STACK_SIZE
7+
int "ThriftTest Server stack size"
8+
default 2048
9+
10+
config THRIFTTEST_LOG_LEVEL
11+
int "ThriftTest log level"
12+
default 4
13+
14+
config THRIFT_COMPACT_PROTOCOL
15+
bool "Use TCompactProtocol for tests"
16+
depends on THRIFT
17+
default y
18+
help
19+
Enable this option to include TCompactProtocol in tests
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CONFIG_THRIFT_SSL_SOCKET=y
2+
3+
# TLS configuration
4+
CONFIG_MBEDTLS=y
5+
CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y
6+
CONFIG_MBEDTLS_ENABLE_HEAP=y
7+
CONFIG_MBEDTLS_HEAP_SIZE=48000
8+
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
9+
CONFIG_NET_SOCKETS_TLS_MAX_CONTEXTS=6
10+
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y

tests/lib/thrift/ThriftTest/prj.conf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
CONFIG_NEWLIB_LIBC=y
2+
CONFIG_NEWLIB_LIBC_NANO=n
3+
4+
# CONFIG_THRIFT Dependencies
5+
CONFIG_CPP=y
6+
CONFIG_STD_CPP17=y
7+
CONFIG_CPP_EXCEPTIONS=y
8+
CONFIG_GLIBCXX_LIBCPP=y
9+
CONFIG_POSIX_API=y
10+
CONFIG_NETWORKING=y
11+
CONFIG_NET_TCP=y
12+
CONFIG_NET_SOCKETS=y
13+
CONFIG_NET_SOCKETPAIR=y
14+
CONFIG_HEAP_MEM_POOL_SIZE=16384
15+
CONFIG_EVENTFD=y
16+
17+
CONFIG_THRIFT=y
18+
19+
# Test dependencies
20+
CONFIG_ZTEST=y
21+
CONFIG_ZTEST_NEW_API=y
22+
CONFIG_TEST_RANDOM_GENERATOR=y
23+
CONFIG_NET_TEST=y
24+
CONFIG_NET_DRIVERS=y
25+
CONFIG_NET_LOOPBACK=y
26+
27+
# Some platforms require relatively large stack sizes.
28+
# This can be tuned per-board.
29+
CONFIG_ZTEST_STACK_SIZE=8192
30+
CONFIG_MAIN_STACK_SIZE=4096
31+
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192
32+
CONFIG_THRIFTTEST_SERVER_STACK_SIZE=8192
33+
CONFIG_NET_TCP_WORKQ_STACK_SIZE=4096
34+
CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096
35+
CONFIG_IDLE_STACK_SIZE=4096
36+
CONFIG_NET_RX_STACK_SIZE=8192
37+
38+
CONFIG_NET_BUF_TX_COUNT=20
39+
CONFIG_NET_PKT_TX_COUNT=20
40+
CONFIG_NET_BUF_RX_COUNT=20
41+
CONFIG_NET_PKT_RX_COUNT=20
42+
CONFIG_POSIX_MAX_FDS=16
43+
44+
# Network address config
45+
CONFIG_NET_IPV4=y
46+
CONFIG_NET_CONFIG_SETTINGS=y
47+
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
/*
2+
* Copyright 2022 Young Mei
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/ztest.h>
8+
9+
#include <array>
10+
#include <cfloat>
11+
#include <locale>
12+
#include <stdexcept>
13+
14+
#include <sys/time.h>
15+
16+
#include "context.hpp"
17+
18+
using namespace apache::thrift;
19+
20+
using namespace std;
21+
22+
static void init_Xtruct(Xtruct &s);
23+
24+
ZTEST(thrift, test_void)
25+
{
26+
context.client->testVoid();
27+
}
28+
29+
ZTEST(thrift, test_string)
30+
{
31+
string s;
32+
context.client->testString(s, "Test");
33+
zassert_equal(s, "Test", "");
34+
}
35+
36+
ZTEST(thrift, test_bool)
37+
{
38+
zassert_equal(false, context.client->testBool(false), "");
39+
zassert_equal(true, context.client->testBool(true), "");
40+
}
41+
42+
ZTEST(thrift, test_byte)
43+
{
44+
zassert_equal(0, context.client->testByte(0), "");
45+
zassert_equal(-1, context.client->testByte(-1), "");
46+
zassert_equal(42, context.client->testByte(42), "");
47+
zassert_equal(-42, context.client->testByte(-42), "");
48+
zassert_equal(127, context.client->testByte(127), "");
49+
zassert_equal(-128, context.client->testByte(-128), "");
50+
}
51+
52+
ZTEST(thrift, test_i32)
53+
{
54+
zassert_equal(0, context.client->testI32(0), "");
55+
zassert_equal(-1, context.client->testI32(-1), "");
56+
zassert_equal(190000013, context.client->testI32(190000013), "");
57+
zassert_equal(-190000013, context.client->testI32(-190000013), "");
58+
zassert_equal(INT32_MAX, context.client->testI32(INT32_MAX), "");
59+
zassert_equal(INT32_MIN, context.client->testI32(INT32_MIN), "");
60+
}
61+
62+
ZTEST(thrift, test_i64)
63+
{
64+
zassert_equal(0, context.client->testI64(0), "");
65+
zassert_equal(-1, context.client->testI64(-1), "");
66+
zassert_equal(7000000000000000123LL, context.client->testI64(7000000000000000123LL), "");
67+
zassert_equal(-7000000000000000123LL, context.client->testI64(-7000000000000000123LL), "");
68+
zassert_equal(INT64_MAX, context.client->testI64(INT64_MAX), "");
69+
zassert_equal(INT64_MIN, context.client->testI64(INT64_MIN), "");
70+
}
71+
72+
ZTEST(thrift, test_double)
73+
{
74+
zassert_equal(0.0, context.client->testDouble(0.0), "");
75+
zassert_equal(-1.0, context.client->testDouble(-1.0), "");
76+
zassert_equal(-5.2098523, context.client->testDouble(-5.2098523), "");
77+
zassert_equal(-0.000341012439638598279,
78+
context.client->testDouble(-0.000341012439638598279), "");
79+
zassert_equal(DBL_MAX, context.client->testDouble(DBL_MAX), "");
80+
zassert_equal(-DBL_MAX, context.client->testDouble(-DBL_MAX), "");
81+
}
82+
83+
ZTEST(thrift, test_binary)
84+
{
85+
string rsp;
86+
87+
context.client->testBinary(rsp, "");
88+
zassert_equal("", rsp, "");
89+
context.client->testBinary(rsp, "Hello");
90+
zassert_equal("Hello", rsp, "");
91+
context.client->testBinary(rsp, "H\x03\x01\x01\x00");
92+
zassert_equal("H\x03\x01\x01\x00", rsp, "");
93+
}
94+
95+
ZTEST(thrift, test_struct)
96+
{
97+
Xtruct request_struct;
98+
init_Xtruct(request_struct);
99+
Xtruct response_struct;
100+
context.client->testStruct(response_struct, request_struct);
101+
102+
zassert_equal(response_struct, request_struct, NULL);
103+
}
104+
105+
ZTEST(thrift, test_nested_struct)
106+
{
107+
Xtruct2 request_struct;
108+
request_struct.byte_thing = 1;
109+
init_Xtruct(request_struct.struct_thing);
110+
request_struct.i32_thing = 5;
111+
Xtruct2 response_struct;
112+
context.client->testNest(response_struct, request_struct);
113+
114+
zassert_equal(response_struct, request_struct, NULL);
115+
}
116+
117+
ZTEST(thrift, test_map)
118+
{
119+
static const map<int32_t, int32_t> request_map = {
120+
{0, -10}, {1, -9}, {2, -8}, {3, -7}, {4, -6}};
121+
122+
map<int32_t, int32_t> response_map;
123+
context.client->testMap(response_map, request_map);
124+
125+
zassert_equal(request_map, response_map, "");
126+
}
127+
128+
ZTEST(thrift, test_string_map)
129+
{
130+
static const map<string, string> request_smap = {
131+
{"a", "2"}, {"b", "blah"}, {"some", "thing"}
132+
};
133+
map<string, string> response_smap;
134+
135+
context.client->testStringMap(response_smap, request_smap);
136+
zassert_equal(response_smap, request_smap, "");
137+
}
138+
139+
ZTEST(thrift, test_set)
140+
{
141+
static const set<int32_t> request_set = {-2, -1, 0, 1, 2};
142+
143+
set<int32_t> response_set;
144+
context.client->testSet(response_set, request_set);
145+
146+
zassert_equal(request_set, response_set, "");
147+
}
148+
149+
ZTEST(thrift, test_list)
150+
{
151+
vector<int32_t> response_list;
152+
context.client->testList(response_list, vector<int32_t>());
153+
zassert_true(response_list.empty(), "Unexpected list size: %llu", response_list.size());
154+
155+
static const vector<int32_t> request_list = {-2, -1, 0, 1, 2};
156+
157+
response_list.clear();
158+
context.client->testList(response_list, request_list);
159+
zassert_equal(request_list, response_list, "");
160+
}
161+
162+
ZTEST(thrift, test_enum)
163+
{
164+
Numberz::type response = context.client->testEnum(Numberz::ONE);
165+
zassert_equal(response, Numberz::ONE, NULL);
166+
167+
response = context.client->testEnum(Numberz::TWO);
168+
zassert_equal(response, Numberz::TWO, NULL);
169+
170+
response = context.client->testEnum(Numberz::EIGHT);
171+
zassert_equal(response, Numberz::EIGHT, NULL);
172+
}
173+
174+
ZTEST(thrift, test_typedef)
175+
{
176+
UserId uid = context.client->testTypedef(309858235082523LL);
177+
zassert_equal(uid, 309858235082523LL, "Unexpected uid: %llu", uid);
178+
}
179+
180+
ZTEST(thrift, test_nested_map)
181+
{
182+
map<int32_t, map<int32_t, int32_t>> mm;
183+
context.client->testMapMap(mm, 1);
184+
185+
zassert_equal(mm.size(), 2, NULL);
186+
zassert_equal(mm[-4][-4], -4, NULL);
187+
zassert_equal(mm[-4][-3], -3, NULL);
188+
zassert_equal(mm[-4][-2], -2, NULL);
189+
zassert_equal(mm[-4][-1], -1, NULL);
190+
zassert_equal(mm[4][4], 4, NULL);
191+
zassert_equal(mm[4][3], 3, NULL);
192+
zassert_equal(mm[4][2], 2, NULL);
193+
zassert_equal(mm[4][1], 1, NULL);
194+
}
195+
196+
ZTEST(thrift, test_exception)
197+
{
198+
std::exception_ptr eptr = nullptr;
199+
200+
try {
201+
context.client->testException("Xception");
202+
} catch (...) {
203+
eptr = std::current_exception();
204+
}
205+
zassert_not_equal(nullptr, eptr, "an exception was not thrown");
206+
207+
eptr = nullptr;
208+
try {
209+
context.client->testException("TException");
210+
} catch (...) {
211+
eptr = std::current_exception();
212+
}
213+
zassert_not_equal(nullptr, eptr, "an exception was not thrown");
214+
215+
context.client->testException("success");
216+
}
217+
218+
ZTEST(thrift, test_multi_exception)
219+
{
220+
std::exception_ptr eptr = nullptr;
221+
222+
try {
223+
Xtruct result;
224+
context.client->testMultiException(result, "Xception", "test 1");
225+
} catch (...) {
226+
eptr = std::current_exception();
227+
}
228+
zassert_not_equal(nullptr, eptr, "an exception was not thrown");
229+
230+
eptr = nullptr;
231+
try {
232+
Xtruct result;
233+
context.client->testMultiException(result, "Xception2", "test 2");
234+
} catch (...) {
235+
eptr = std::current_exception();
236+
}
237+
zassert_not_equal(nullptr, eptr, "an exception was not thrown");
238+
}
239+
240+
static void init_Xtruct(Xtruct &s)
241+
{
242+
s.string_thing = "Zero";
243+
s.byte_thing = 1;
244+
s.i32_thing = -3;
245+
s.i64_thing = -5;
246+
}

0 commit comments

Comments
 (0)