1 /*
2  * Copyright (c) 2023, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <CppUTest/TestHarness.h>
8 
9 #include "protocols/rpc/common/packed-c/status.h"
10 #include "protocols/service/discovery/packed-c/opcodes.h"
11 #include "psa/error.h"
12 #include "rpc/http/caller/http_caller.h"
13 #include "service/locator/remote/restapi/restapi_location.h"
14 
15 /*
16  * http_caller tests rely on a fw test api server running on the local host
17  */
TEST_GROUP(RpcCallerTests)18 TEST_GROUP(RpcCallerTests)
19 {
20 	void setup()
21 	{
22 		rpc_caller = http_caller_init(&http_caller_under_test);
23 		CHECK_TRUE(rpc_caller);
24 	}
25 
26 	void teardown()
27 	{
28 		http_caller_deinit(&http_caller_under_test);
29 	}
30 
31 	http_caller http_caller_under_test;
32 	struct rpc_caller *rpc_caller;
33 };
34 
TEST(RpcCallerTests,probeAvailableHttpEndpoint)35 TEST(RpcCallerTests, probeAvailableHttpEndpoint)
36 {
37 	long http_code = 0;
38 
39 	CHECK_TRUE(http_caller_probe(RESTAPI_LOCATOR_API_URL, &http_code));
40 	LONGS_EQUAL(200, http_code);
41 }
42 
TEST(RpcCallerTests,probeUnavailableHost)43 TEST(RpcCallerTests, probeUnavailableHost)
44 {
45 	long http_code = 0;
46 
47 	CHECK_FALSE(http_caller_probe("http://127.0.0.1:5001/", &http_code));
48 	LONGS_EQUAL(0, http_code);
49 }
50 
TEST(RpcCallerTests,callUnavailableApiEndpoint)51 TEST(RpcCallerTests, callUnavailableApiEndpoint)
52 {
53 	int status;
54 
55 	status = http_caller_open(&http_caller_under_test, RESTAPI_LOCATOR_API_URL "foo/call/");
56 	LONGS_EQUAL(0, status);
57 
58 	rpc_call_handle call_handle;
59 	uint8_t *req_buf = NULL;
60 
61 	call_handle = rpc_caller_begin(rpc_caller, &req_buf, 48);
62 	CHECK_TRUE(call_handle);
63 	CHECK_TRUE(req_buf);
64 
65 	rpc_status_t rpc_status;
66 	rpc_opstatus_t op_status;
67 	uint8_t *resp_buf = NULL;
68 	size_t resp_len = 0;
69 
70 	rpc_status =
71 		rpc_caller_invoke(rpc_caller, call_handle, 251, &op_status, &resp_buf, &resp_len);
72 	LONGS_EQUAL(TS_RPC_ERROR_EP_DOES_NOT_EXIT, rpc_status);
73 
74 	rpc_caller_end(rpc_caller, call_handle);
75 }
76 
TEST(RpcCallerTests,callAvailableApiEndpoint)77 TEST(RpcCallerTests, callAvailableApiEndpoint)
78 {
79 	int status;
80 
81 	status = http_caller_open(&http_caller_under_test, RESTAPI_LOCATOR_API_URL "fwu/call/");
82 	LONGS_EQUAL(0, status);
83 
84 	rpc_call_handle call_handle;
85 	uint8_t *req_buf = NULL;
86 
87 	call_handle = rpc_caller_begin(rpc_caller, &req_buf, 48);
88 	CHECK_TRUE(call_handle);
89 	CHECK_TRUE(req_buf);
90 
91 	rpc_status_t rpc_status;
92 	rpc_opstatus_t op_status;
93 	uint8_t *resp_buf = NULL;
94 	size_t resp_len = 0;
95 
96 	rpc_status = rpc_caller_invoke(rpc_caller, call_handle,
97 				       TS_DISCOVERY_OPCODE_GET_SERVICE_INFO, &op_status, &resp_buf,
98 				       &resp_len);
99 	LONGS_EQUAL(TS_RPC_CALL_ACCEPTED, rpc_status);
100 	LONGS_EQUAL(PSA_SUCCESS, op_status);
101 	CHECK_TRUE(resp_len > 0);
102 
103 	rpc_caller_end(rpc_caller, call_handle);
104 }
105