|
1 | 1 |
|
2 | | -#include <gtest/gtest.h> |
3 | | -#include <chrono> |
4 | | -#include <thread> |
5 | | -#include "../utils/BaseTest.hpp" |
6 | | -#include <privmx/endpoint/core/Connection.hpp> |
| 2 | +#include "../utils/BaseEndpointEventTest.hpp" |
7 | 3 | #include <privmx/endpoint/core/Exception.hpp> |
8 | | -#include <Poco/Util/IniFileConfiguration.h> |
9 | 4 | #include <privmx/crypto/Crypto.hpp> |
10 | 5 | #include <privmx/endpoint/core/VarSerializer.hpp> |
11 | | -#include <privmx/endpoint/core/EventQueue.hpp> |
12 | | -#include <privmx/endpoint/core/EventQueueImpl.hpp> |
13 | 6 |
|
14 | 7 | using namespace privmx::endpoint; |
15 | 8 |
|
16 | | -class CoreEventTest : public privmx::test::BaseTest { |
17 | | -protected: |
18 | | - CoreEventTest() : BaseTest(privmx::test::BaseTestMode::online) {} |
19 | | - void customSetUp() override { |
20 | | - reader = new Poco::Util::IniFileConfiguration(INI_FILE_PATH); |
21 | | - connection = std::make_shared<core::Connection>( |
22 | | - core::Connection::connect( |
23 | | - reader->getString("Login.user_1_privKey"), |
24 | | - reader->getString("Login.solutionId"), |
25 | | - getPlatformUrl(reader->getString("Login.instanceUrl")) |
26 | | - ) |
27 | | - ); |
28 | | - } |
29 | | - void customTearDown() override { // tmp segfault fix |
30 | | - connection.reset(); |
31 | | - reader.reset(); |
32 | | - privmx::endpoint::core::EventQueueImpl::getInstance()->clear(); |
33 | | - } |
34 | | - std::shared_ptr<core::Connection> connection; |
35 | | - core::EventQueue eventQueue = core::EventQueue::getInstance(); |
36 | | - Poco::Util::IniFileConfiguration::Ptr reader; |
| 9 | +class CoreEventTest : public privmx::test::BaseEndpointEventTest { |
37 | 10 | }; |
38 | 11 |
|
39 | 12 | TEST_F(CoreEventTest, getEvent_libConnected) { |
40 | | - std::shared_ptr<privmx::endpoint::core::Event> event = nullptr; |
41 | | - EXPECT_NO_THROW({ |
42 | | - eventQueue.waitEvent(); // pop libConnected form queue |
43 | | - }); |
44 | | - |
| 13 | + eventQueue.waitEvent(); |
45 | 14 | } |
46 | 15 |
|
47 | 16 | TEST_F(CoreEventTest, getEvent_libConnected_different_instances) { |
48 | | - std::shared_ptr<privmx::endpoint::core::Event> event = nullptr; |
49 | | - EXPECT_NO_THROW({ |
50 | | - eventQueue.waitEvent(); // pop libConnected form queue |
51 | | - }); |
52 | | - std::shared_ptr<core::Connection> connection_2; |
53 | | - EXPECT_NO_THROW({ |
54 | | - connection_2 = std::make_shared<core::Connection>( |
55 | | - core::Connection::connect( |
56 | | - reader->getString("Login.user_2_privKey"), |
57 | | - reader->getString("Login.solutionId"), |
58 | | - getPlatformUrl(reader->getString("Login.instanceUrl")) |
59 | | - ) |
60 | | - ); |
61 | | - }); |
62 | | - EXPECT_NO_THROW({ |
63 | | - std::optional<core::EventHolder> eventHolder = eventQueue.getEvent(); |
64 | | - if(eventHolder.has_value()) { |
65 | | - event = eventHolder.value().get(); |
66 | | - } else { |
67 | | - event = nullptr; |
68 | | - } |
69 | | - }); |
70 | | - if(event != nullptr) { |
71 | | - EXPECT_EQ(event->connectionId, connection_2->getConnectionId()); |
72 | | - EXPECT_EQ(event->type, "libConnected"); |
73 | | - EXPECT_TRUE(core::Events::isLibConnectedEvent(event)); |
74 | | - } else { |
75 | | - FAIL(); |
76 | | - } |
| 17 | + eventQueue.waitEvent(); |
| 18 | + auto connection_2 = std::make_shared<core::Connection>( |
| 19 | + core::Connection::connect( |
| 20 | + reader->getString("Login.user_2_privKey"), |
| 21 | + reader->getString("Login.solutionId"), |
| 22 | + getPlatformUrl(reader->getString("Login.instanceUrl")) |
| 23 | + ) |
| 24 | + ); |
| 25 | + auto eventHolder = waitForEvent("libConnected", {connection_2->getConnectionId()}); |
| 26 | + ASSERT_TRUE(eventHolder.has_value()); |
| 27 | + auto event = eventHolder.value().get(); |
| 28 | + ASSERT_NE(event, nullptr); |
| 29 | + EXPECT_EQ(event->connectionId, connection_2->getConnectionId()); |
| 30 | + EXPECT_EQ(event->type, "libConnected"); |
| 31 | + EXPECT_TRUE(core::Events::isLibConnectedEvent(event)); |
77 | 32 | } |
78 | 33 |
|
79 | 34 | TEST_F(CoreEventTest, waitEvent_getEvent_libDisconnected) { |
80 | | - std::shared_ptr<privmx::endpoint::core::Event> event = nullptr; |
81 | | - EXPECT_NO_THROW({ |
82 | | - eventQueue.waitEvent(); // pop libConnected form queue |
83 | | - }); |
84 | | - EXPECT_NO_THROW({ |
85 | | - connection->disconnect(); |
86 | | - }); |
87 | | - EXPECT_NO_THROW({ |
88 | | - std::optional<core::EventHolder> eventHolder = eventQueue.getEvent(); |
89 | | - if(eventHolder.has_value()) { |
90 | | - event = eventHolder.value().get(); |
91 | | - } else { |
92 | | - event = nullptr; |
93 | | - } |
94 | | - }); |
95 | | - if(event != nullptr) { |
96 | | - EXPECT_EQ(event->connectionId, connection->getConnectionId()); |
97 | | - EXPECT_EQ(event->type, "libDisconnected"); |
98 | | - EXPECT_TRUE(core::Events::isLibDisconnectedEvent(event)); |
99 | | - } else { |
100 | | - FAIL(); |
101 | | - } |
| 35 | + eventQueue.waitEvent(); |
| 36 | + connection->disconnect(); |
| 37 | + auto eventHolder = waitForEvent("libDisconnected", {connection->getConnectionId()}); |
| 38 | + ASSERT_TRUE(eventHolder.has_value()); |
| 39 | + auto event = eventHolder.value().get(); |
| 40 | + ASSERT_NE(event, nullptr); |
| 41 | + EXPECT_EQ(event->connectionId, connection->getConnectionId()); |
| 42 | + EXPECT_EQ(event->type, "libDisconnected"); |
| 43 | + EXPECT_TRUE(core::Events::isLibDisconnectedEvent(event)); |
102 | 44 | } |
103 | 45 |
|
104 | | - |
105 | 46 | TEST_F(CoreEventTest, waitEvent_getEvent_ContextUsersStatusChangedEvent_enabled) { |
106 | | - std::shared_ptr<privmx::endpoint::core::Event> event = nullptr; |
107 | | - EXPECT_NO_THROW({ |
108 | | - eventQueue.waitEvent(); // pop libConnected form queue |
109 | | - }); |
110 | | - std::shared_ptr<core::Connection> connection_2; |
111 | | - EXPECT_NO_THROW({ |
112 | | - connection_2 = std::make_shared<core::Connection>( |
113 | | - core::Connection::connect( |
114 | | - reader->getString("Login.user_2_privKey"), |
115 | | - reader->getString("Login.solutionId"), |
116 | | - getPlatformUrl(reader->getString("Login.instanceUrl")) |
117 | | - ) |
118 | | - ); |
119 | | - }); |
120 | | - EXPECT_NO_THROW({ |
121 | | - eventQueue.waitEvent(); // pop libConnected form queue |
122 | | - }); |
| 47 | + eventQueue.waitEvent(); |
| 48 | + auto connection_2 = std::make_shared<core::Connection>( |
| 49 | + core::Connection::connect( |
| 50 | + reader->getString("Login.user_2_privKey"), |
| 51 | + reader->getString("Login.solutionId"), |
| 52 | + getPlatformUrl(reader->getString("Login.instanceUrl")) |
| 53 | + ) |
| 54 | + ); |
| 55 | + eventQueue.waitEvent(); // pop libConnected for connection_2 |
123 | 56 | std::this_thread::sleep_for(std::chrono::milliseconds(1500)); |
124 | | - EXPECT_NO_THROW({ |
125 | | - connection->subscribeFor( |
126 | | - { |
127 | | - connection->buildSubscriptionQuery( |
128 | | - core::EventType::USER_STATUS, |
129 | | - core::EventSelectorType::CONTEXT_ID, |
130 | | - reader->getString("Context_1.contextId") |
131 | | - ) |
132 | | - } |
133 | | - ); |
134 | | - connection_2->disconnect(); |
135 | | - }); |
136 | | - EXPECT_NO_THROW({ |
137 | | - eventQueue.waitEvent(); // pop libDisconnected form queue |
138 | | - }); |
139 | | - EXPECT_NO_THROW({ |
140 | | - eventQueue.waitEvent(); // pop libPlatformDisconnected form queue |
141 | | - }); |
142 | | - EXPECT_NO_THROW({ |
143 | | - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); |
144 | | - std::optional<core::EventHolder> eventHolder = eventQueue.getEvent(); |
145 | | - if(eventHolder.has_value()) { |
146 | | - event = eventHolder.value().get(); |
147 | | - } else { |
148 | | - event = nullptr; |
149 | | - } |
150 | | - }); |
151 | | - if(event != nullptr) { |
152 | | - EXPECT_EQ(event->connectionId, connection->getConnectionId()); |
153 | | - EXPECT_EQ(event->type, "contextUserStatusChanged"); |
154 | | - EXPECT_EQ(event->channel, "context/userStatus"); |
155 | | - if(core::Events::isContextUsersStatusChangedEvent(event)) { |
156 | | - EXPECT_EQ(event->subscriptions.size(), 1); |
157 | | - core::ContextUsersStatusChangedEventData usersStatusChanged = core::Events::extractContextUsersStatusChangedEvent(event).data; |
158 | | - EXPECT_EQ(usersStatusChanged.contextId, reader->getString("Context_1.contextId")); |
159 | | - EXPECT_EQ(usersStatusChanged.users.size(), 1); |
160 | | - if(usersStatusChanged.users.size() > 0) { |
161 | | - auto userWithAction = usersStatusChanged.users[0]; |
162 | | - EXPECT_EQ(userWithAction.user.pubKey, reader->getString("Login.user_2_pubKey")); |
163 | | - EXPECT_EQ(userWithAction.user.userId, reader->getString("Login.user_2_id")); |
164 | | - EXPECT_EQ(userWithAction.action, "logout"); |
165 | | - } |
166 | | - } else { |
167 | | - FAIL(); |
| 57 | + connection->subscribeFor( |
| 58 | + { |
| 59 | + connection->buildSubscriptionQuery( |
| 60 | + core::EventType::USER_STATUS, |
| 61 | + core::EventSelectorType::CONTEXT_ID, |
| 62 | + reader->getString("Context_1.contextId") |
| 63 | + ) |
168 | 64 | } |
169 | | - } else { |
170 | | - FAIL(); |
| 65 | + ); |
| 66 | + connection_2->disconnect(); |
| 67 | + eventQueue.waitEvent(); // pop libDisconnected |
| 68 | + eventQueue.waitEvent(); // pop libPlatformDisconnected |
| 69 | + auto eventHolder = waitForEvent("contextUserStatusChanged", {connection->getConnectionId()}); |
| 70 | + ASSERT_TRUE(eventHolder.has_value()); |
| 71 | + auto event = eventHolder.value().get(); |
| 72 | + ASSERT_NE(event, nullptr); |
| 73 | + EXPECT_EQ(event->connectionId, connection->getConnectionId()); |
| 74 | + EXPECT_EQ(event->type, "contextUserStatusChanged"); |
| 75 | + EXPECT_EQ(event->channel, "context/userStatus"); |
| 76 | + ASSERT_TRUE(core::Events::isContextUsersStatusChangedEvent(event)); |
| 77 | + EXPECT_EQ(event->subscriptions.size(), 1); |
| 78 | + core::ContextUsersStatusChangedEventData usersStatusChanged = core::Events::extractContextUsersStatusChangedEvent(event).data; |
| 79 | + EXPECT_EQ(usersStatusChanged.contextId, reader->getString("Context_1.contextId")); |
| 80 | + EXPECT_EQ(usersStatusChanged.users.size(), 1); |
| 81 | + if(usersStatusChanged.users.size() > 0) { |
| 82 | + auto userWithAction = usersStatusChanged.users[0]; |
| 83 | + EXPECT_EQ(userWithAction.user.pubKey, reader->getString("Login.user_2_pubKey")); |
| 84 | + EXPECT_EQ(userWithAction.user.userId, reader->getString("Login.user_2_id")); |
| 85 | + EXPECT_EQ(userWithAction.action, "logout"); |
171 | 86 | } |
172 | 87 | } |
173 | 88 |
|
174 | 89 | TEST_F(CoreEventTest, waitEvent_getEvent_ContextUsersStatusChangedEvent_disabled) { |
175 | | - std::shared_ptr<privmx::endpoint::core::Event> event = nullptr; |
176 | | - EXPECT_NO_THROW({ |
177 | | - eventQueue.waitEvent(); // pop libConnected form queue |
178 | | - }); |
179 | | - std::shared_ptr<core::Connection> connection_2; |
180 | | - EXPECT_NO_THROW({ |
181 | | - connection_2 = std::make_shared<core::Connection>( |
182 | | - core::Connection::connect( |
183 | | - reader->getString("Login.user_2_privKey"), |
184 | | - reader->getString("Login.solutionId"), |
185 | | - getPlatformUrl(reader->getString("Login.instanceUrl")) |
186 | | - ) |
187 | | - ); |
188 | | - }); |
189 | | - EXPECT_NO_THROW({ |
190 | | - eventQueue.waitEvent(); // pop libConnected form queue |
191 | | - }); |
192 | | - EXPECT_NO_THROW({ |
193 | | - connection_2->disconnect(); |
194 | | - }); |
195 | | - EXPECT_NO_THROW({ |
196 | | - eventQueue.waitEvent(); // pop libDisconnected form queue |
197 | | - }); |
198 | | - EXPECT_NO_THROW({ |
199 | | - eventQueue.waitEvent(); // pop libPlatformDisconnected form queue |
200 | | - }); |
201 | | - EXPECT_NO_THROW({ |
202 | | - std::this_thread::sleep_for(std::chrono::milliseconds(1500)); |
203 | | - std::optional<core::EventHolder> eventHolder = eventQueue.getEvent(); |
204 | | - if(eventHolder.has_value()) { |
205 | | - event = eventHolder.value().get(); |
206 | | - } else { |
207 | | - event = nullptr; |
208 | | - } |
209 | | - }); |
210 | | - if(event == nullptr) { |
211 | | - |
212 | | - } else { |
213 | | - std::cout << "Received Event with type " << event->type << std::endl; |
214 | | - std::cout << "Expected null" << std::endl; |
215 | | - FAIL(); |
216 | | - } |
| 90 | + eventQueue.waitEvent(); |
| 91 | + auto connection_2 = std::make_shared<core::Connection>( |
| 92 | + core::Connection::connect( |
| 93 | + reader->getString("Login.user_2_privKey"), |
| 94 | + reader->getString("Login.solutionId"), |
| 95 | + getPlatformUrl(reader->getString("Login.instanceUrl")) |
| 96 | + ) |
| 97 | + ); |
| 98 | + eventQueue.waitEvent(); // pop libConnected for connection_2 |
| 99 | + connection_2->disconnect(); |
| 100 | + eventQueue.waitEvent(); // pop libDisconnected |
| 101 | + eventQueue.waitEvent(); // pop libPlatformDisconnected |
| 102 | + assertNoEventReceived(); |
217 | 103 | } |
218 | 104 |
|
219 | 105 | TEST_F(CoreEventTest, subscribeFor_unsubscribeFor) { |
220 | 106 | std::vector<std::string> valid_subscriptions; |
221 | 107 | EXPECT_NO_THROW({ |
222 | 108 | valid_subscriptions = connection->subscribeFor({ |
223 | | - connection->buildSubscriptionQuery( |
224 | | - core::EventType::USER_ADD, |
225 | | - core::EventSelectorType::CONTEXT_ID, |
226 | | - reader->getString("Context_1.contextId") |
| 109 | + connection->buildSubscriptionQuery( |
| 110 | + core::EventType::USER_ADD, |
| 111 | + core::EventSelectorType::CONTEXT_ID, |
| 112 | + reader->getString("Context_1.contextId") |
227 | 113 | ) |
228 | 114 | }); |
229 | 115 | }); |
230 | 116 | std::vector<std::string> invalid_subscriptions; |
231 | 117 | EXPECT_NO_THROW({ |
232 | 118 | invalid_subscriptions = connection->subscribeFor({ |
233 | | - connection->buildSubscriptionQuery( |
234 | | - core::EventType::USER_ADD, |
235 | | - core::EventSelectorType::CONTEXT_ID, |
| 119 | + connection->buildSubscriptionQuery( |
| 120 | + core::EventType::USER_ADD, |
| 121 | + core::EventSelectorType::CONTEXT_ID, |
236 | 122 | "error" |
237 | 123 | ) |
238 | 124 | }); |
239 | 125 | }); |
240 | 126 | EXPECT_NO_THROW({ |
241 | 127 | connection->unsubscribeFrom({ |
242 | | - valid_subscriptions |
| 128 | + valid_subscriptions |
243 | 129 | }); |
244 | 130 | }); |
245 | 131 | EXPECT_NO_THROW({ |
246 | 132 | connection->unsubscribeFrom({ |
247 | | - invalid_subscriptions |
| 133 | + invalid_subscriptions |
248 | 134 | }); |
249 | 135 | }); |
250 | 136 | } |
0 commit comments