Skip to content

Commit 77bb5f7

Browse files
authored
test: Add debugging application ReadCertClient (#1382)
1 parent b244d2a commit 77bb5f7

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

tests/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ set(CMAKE_CXX_STANDARD 20)
77
add_library(test_util_client OBJECT util/UAClient.cpp)
88
target_link_libraries(test_util_client PUBLIC open62541::open62541 Open62541Cpp::Open62541Cpp)
99

10+
add_executable(ReadCertClient readCertClient/ReadCertClient.cpp)
11+
target_link_libraries(ReadCertClient PUBLIC open62541::open62541 Open62541Cpp::Open62541Cpp)
12+
1013
# NO_CMAKE_SYSTEM_PATH prevents the usage of 'FindGTest.cmake' from the cmake installation. As this does not include
1114
# GMock.
1215
find_package(GTest REQUIRED NO_CMAKE_SYSTEM_PACKAGE_REGISTRY)
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
2+
#include <open62541/client_config_default.h>
3+
#include <open62541/client_highlevel.h>
4+
#include <open62541/plugin/log_stdout.h>
5+
6+
#include <iostream>
7+
#include <memory>
8+
#include <string>
9+
10+
std::ostream& operator<<(std::ostream& ostr, const UA_String& s) {
11+
auto str = std::string((char*)s.data, s.length);
12+
ostr << str;
13+
return ostr;
14+
}
15+
16+
std::ostream& operator<<(std::ostream& ostr, UA_MessageSecurityMode secMode) {
17+
switch (secMode) {
18+
case UA_MESSAGESECURITYMODE_INVALID: {
19+
ostr << "UA_MESSAGESECURITYMODE_INVALID";
20+
break;
21+
}
22+
case UA_MESSAGESECURITYMODE_NONE: {
23+
ostr << "UA_MESSAGESECURITYMODE_NONE";
24+
break;
25+
}
26+
case UA_MESSAGESECURITYMODE_SIGN: {
27+
ostr << "UA_MESSAGESECURITYMODE_SIGN";
28+
break;
29+
}
30+
case UA_MESSAGESECURITYMODE_SIGNANDENCRYPT: {
31+
ostr << "UA_MESSAGESECURITYMODE_SIGNANDENCRYPT";
32+
break;
33+
}
34+
default: {
35+
ostr << "UA_MESSAGESECURITYMODE_UNKNOWN " << (int)secMode;
36+
break;
37+
}
38+
}
39+
40+
return ostr;
41+
}
42+
43+
void printEndpoint(const UA_EndpointDescription& endpointDesc) {
44+
std::cout << "EndpointURL: " << endpointDesc.endpointUrl << std::endl;
45+
std::cout << " SecurityPolicyUri:" << endpointDesc.securityPolicyUri << std::endl;
46+
std::cout << " securityMode:" << endpointDesc.securityMode << std::endl;
47+
std::cout << " ServerCert:" << endpointDesc.serverCertificate << std::endl;
48+
}
49+
50+
int main(int argc, char* argv[]) {
51+
std::cout << "Begin ReadCertClient" << std::endl;
52+
std::string serverUri = "opc.tcp://localhost:4840";
53+
if (argc >= 2) {
54+
serverUri = argv[1];
55+
}
56+
57+
std::shared_ptr<UA_Client> pClientShared(UA_Client_new(), UA_Client_delete);
58+
auto pClient = pClientShared.get();
59+
UA_ClientConfig_setDefault(UA_Client_getConfig(pClient));
60+
size_t numEndpoints = 0;
61+
UA_EndpointDescription* pEndpointDescriptions;
62+
UA_StatusCode retval = UA_Client_getEndpoints(pClient, serverUri.c_str(), &numEndpoints, &pEndpointDescriptions);
63+
if (retval != UA_STATUSCODE_GOOD) {
64+
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "UA_Client_getEndpoints failed with status code %s", UA_StatusCode_name(retval));
65+
return EXIT_FAILURE;
66+
}
67+
68+
for (size_t i = 0; i < numEndpoints; ++i) {
69+
printEndpoint(pEndpointDescriptions[i]);
70+
}
71+
UA_Array_delete(pEndpointDescriptions, numEndpoints, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
72+
73+
std::cout << "End ReadCertClient" << std::endl;
74+
return EXIT_SUCCESS;
75+
}

0 commit comments

Comments
 (0)