Coverage Report

Created: 2026-07-16 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/open62541_15/tests/testing-plugins/test_helpers.c
Line
Count
Source
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
#include "test_helpers.h"
6
#include <open62541/client_config_default.h>
7
#include <open62541/plugin/certificategroup_default.h>
8
#include <open62541/plugin/log.h>
9
#include <open62541/plugin/log_stdout.h>
10
#include <open62541/server_config_default.h>
11
12
20
#define TESTING_LOGLEVEL UA_LOGLEVEL_INFO
13
14
static void
15
testServerNotificationCallback(UA_Server *server,
16
                               UA_ApplicationNotificationType type,
17
0
                               const UA_KeyValueMap payload) {
18
0
}
19
20
0
UA_Server * UA_Server_newForUnitTest(void) {
21
0
    UA_ServerConfig sc;
22
0
    memset(&sc, 0, sizeof(UA_ServerConfig));
23
0
    sc.logging = UA_Log_Stdout_new(TESTING_LOGLEVEL);
24
0
    UA_ServerConfig_setMinimal(&sc, 4840, NULL);
25
0
    UA_Server *server = UA_Server_newWithConfig(&sc);
26
0
    if(!server)
27
0
        return NULL;
28
0
    UA_ServerConfig *config = UA_Server_getConfig(server);
29
    /* Manually set the eventloop clock to the fake clock */
30
0
    config->eventLoop->dateTime_now = UA_DateTime_now_fake;
31
0
    config->eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;
32
0
    config->tcpReuseAddr = true;
33
0
    return server;
34
0
}
35
36
UA_Server *
37
UA_Server_newForUnitTestWithSecurityPolicies(UA_UInt16 portNumber,
38
                                             const UA_ByteString *certificate,
39
                                             const UA_ByteString *privateKey,
40
                                             const UA_ByteString *trustList,
41
                                             size_t trustListSize,
42
                                             const UA_ByteString *issuerList,
43
                                             size_t issuerListSize,
44
                                             const UA_ByteString *revocationList,
45
0
                                             size_t revocationListSize) {
46
0
    UA_ServerConfig config;
47
0
    memset(&config, 0, sizeof(UA_ServerConfig));
48
0
    config.logging = UA_Log_Stdout_new(TESTING_LOGLEVEL);
49
0
#ifdef UA_ENABLE_ENCRYPTION
50
0
    UA_ServerConfig_setDefaultWithSecurityPolicies(&config, portNumber,
51
0
                                                   certificate, privateKey,
52
0
                                                   trustList, trustListSize,
53
0
                                                   issuerList, issuerListSize,
54
0
                                                   revocationList, revocationListSize);
55
0
#endif
56
0
    config.eventLoop->dateTime_now = UA_DateTime_now_fake;
57
0
    config.eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;
58
0
    config.tcpReuseAddr = true;
59
0
    config.globalNotificationCallback = testServerNotificationCallback;
60
0
    return UA_Server_newWithConfig(&config);
61
0
}
62
63
#if defined(__linux__) || defined(UA_ARCHITECTURE_WIN32)
64
UA_Server *
65
UA_Server_newForUnitTestWithSecurityPolicies_Filestore(UA_UInt16 portNumber,
66
                                                       const UA_ByteString *certificate,
67
                                                       const UA_ByteString *privateKey,
68
0
                                                       const UA_String storePath) {
69
0
    UA_ServerConfig config;
70
0
    memset(&config, 0, sizeof(UA_ServerConfig));
71
0
    config.logging = UA_Log_Stdout_new(TESTING_LOGLEVEL);
72
0
#ifdef UA_ENABLE_ENCRYPTION
73
0
    UA_ServerConfig_setDefaultWithFilestore(&config, portNumber,
74
0
                                            certificate, privateKey, storePath);
75
0
#endif
76
0
    config.eventLoop->dateTime_now = UA_DateTime_now_fake;
77
0
    config.eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;
78
0
    config.tcpReuseAddr = true;
79
0
    config.globalNotificationCallback = testServerNotificationCallback;
80
0
    return UA_Server_newWithConfig(&config);
81
0
}
82
#endif /* defined(__linux__) || defined(UA_ARCHITECTURE_WIN32) */
83
84
static void
85
testClientNotificationCallback(UA_Client *client,
86
                               UA_ApplicationNotificationType type,
87
20
                               const UA_KeyValueMap payload) {
88
20
}
89
90
UA_Client *
91
20
UA_Client_newForUnitTest(void) {
92
20
    UA_ClientConfig cc;
93
20
    memset(&cc, 0, sizeof(UA_ClientConfig));
94
20
    cc.logging = UA_Log_Stdout_new(TESTING_LOGLEVEL);
95
20
    UA_ClientConfig_setDefault(&cc);
96
20
    UA_Client *client = UA_Client_newWithConfig(&cc);
97
20
    if(!client)
98
0
        return NULL;
99
20
    UA_ClientConfig *config = UA_Client_getConfig(client);
100
    /* Manually set the eventloop clock to the fake clock */
101
20
    config->eventLoop->dateTime_now = UA_DateTime_now_fake;
102
20
    config->eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;
103
20
    config->tcpReuseAddr = true;
104
105
    /* Allow username/pw w/o encryption */
106
20
    config->allowNonePolicyPassword = true;
107
108
    /* Increase the timeouts (needed for valgrind CI tests) */
109
20
    config->timeout = 10 * 60 * 1000;
110
111
20
    config->globalNotificationCallback = testClientNotificationCallback;
112
113
20
    return client;
114
20
}
115
116
UA_StatusCode
117
UA_ClientConfig_newForUnitTestWithEncryption(UA_ClientConfig *config,
118
                                             const UA_ByteString certificate,
119
0
                                             const UA_ByteString privateKey) {
120
0
    memset(config, 0, sizeof(UA_ClientConfig));
121
0
    config->logging = UA_Log_Stdout_new(TESTING_LOGLEVEL);
122
0
#ifdef UA_ENABLE_ENCRYPTION
123
0
    UA_ClientConfig_setDefaultEncryption(config, certificate, privateKey,
124
0
                                         NULL, 0, NULL, 0);
125
0
#endif
126
0
    UA_CertificateGroup_AcceptAll(&config->certificateVerification);
127
0
    config->eventLoop->dateTime_now = UA_DateTime_now_fake;
128
0
    config->eventLoop->dateTime_nowMonotonic = UA_DateTime_now_fake;
129
0
    config->tcpReuseAddr = true;
130
0
    config->allowNonePolicyPassword = true;
131
0
    config->timeout = 10 * 60 * 1000; /* increased for valgrind CI */
132
0
    config->globalNotificationCallback = testClientNotificationCallback;
133
0
    return UA_STATUSCODE_GOOD;
134
0
}