Line data Source code
1 : /*
2 : ___________________________________________
3 : | _ ___ _ |
4 : | | | |__ \ | | |
5 : | | |__ ) |__ _ __ _ ___ _ __ | |_ |
6 : | | '_ \ / // _` |/ _` |/ _ \ '_ \| __| | HTTP/2 AGENT FOR MOCK TESTING
7 : | | | | |/ /| (_| | (_| | __/ | | | |_ | Version 0.0.z
8 : | |_| |_|____\__,_|\__, |\___|_| |_|\__| | https://github.com/testillano/h2agent
9 : | __/ | |
10 : | |___/ |
11 : |___________________________________________|
12 :
13 : Licensed under the MIT License <http://opensource.org/licenses/MIT>.
14 : SPDX-License-Identifier: MIT
15 : Copyright (c) 2021 Eduardo Ramos
16 :
17 : Permission is hereby granted, free of charge, to any person obtaining a copy
18 : of this software and associated documentation files (the "Software"), to deal
19 : in the Software without restriction, including without limitation the rights
20 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21 : copies of the Software, and to permit persons to whom the Software is
22 : furnished to do so, subject to the following conditions:
23 :
24 : The above copyright notice and this permission notice shall be included in all
25 : copies or substantial portions of the Software.
26 :
27 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 : SOFTWARE.
34 : */
35 :
36 : #include <string>
37 : #include <algorithm>
38 :
39 : #include <ert/tracing/Logger.hpp>
40 :
41 : #include <MockClientData.hpp>
42 : #include <MockClientEvent.hpp>
43 :
44 :
45 : namespace h2agent
46 : {
47 : namespace model
48 : {
49 :
50 214 : void MockClientData::loadEvent(const DataKey &dataKey, const std::string &clientProvisionId, const std::string &previousState, const std::string &state, const std::chrono::microseconds &sendingTimestampUs, const std::chrono::microseconds &receptionTimestampUs, int responseStatusCode, const nghttp2::asio_http2::header_map &requestHeaders, const nghttp2::asio_http2::header_map &responseHeaders, const std::string &requestBody, DataPart &responseBodyDataPart, std::uint64_t sendSeq, std::int64_t sequence, unsigned int requestDelayMs, unsigned int timeoutMs, bool historyEnabled) {
51 :
52 214 : modifyOrInsert(dataKey.getKey(), [&](std::shared_ptr<MockEventsHistory> &entry) {
53 214 : if (!entry) entry = std::make_shared<MockClientEventsHistory>(dataKey);
54 214 : std::static_pointer_cast<MockClientEventsHistory>(entry)->loadEvent(clientProvisionId, previousState, state, sendingTimestampUs, receptionTimestampUs, responseStatusCode, requestHeaders, responseHeaders, requestBody, responseBodyDataPart, sendSeq, sequence, requestDelayMs, timeoutMs, historyEnabled);
55 214 : });
56 214 : }
57 :
58 0 : bool MockClientData::removeEventBySendSeq(const DataKey &dataKey, std::uint64_t sendSeq) {
59 :
60 0 : bool exists{};
61 0 : auto events = std::static_pointer_cast<MockClientEventsHistory>(get(dataKey.getKey(), exists));
62 0 : if (!exists) return false;
63 :
64 0 : bool deleted = events->removeEventBySendSeq(sendSeq);
65 :
66 : // Cleanup empty map entry:
67 0 : if (deleted && events->size() == 0) {
68 0 : bool aux{};
69 0 : remove(dataKey.getKey(), aux);
70 : }
71 :
72 0 : return deleted;
73 0 : }
74 :
75 0 : std::shared_ptr<MockEvent> MockClientData::getEventBySendSeq(const DataKey &dataKey, std::uint64_t sendSeq) {
76 :
77 0 : bool exists{};
78 0 : auto events = std::static_pointer_cast<MockClientEventsHistory>(get(dataKey.getKey(), exists));
79 0 : if (!exists) return nullptr;
80 :
81 0 : return events->getEventBySendSeq(sendSeq);
82 0 : }
83 :
84 4 : std::string MockClientData::getSequence(std::uint64_t fromTimestampUs, std::uint64_t toTimestampUs, const std::string &requestMethod, const std::regex *uriRegex) const {
85 :
86 : struct Entry {
87 : std::uint64_t timestampUs;
88 : std::string direction;
89 : std::string method;
90 : std::string uri;
91 : std::string clientProvisionId;
92 : };
93 4 : std::vector<Entry> entries;
94 :
95 4 : forEach([&](const mock_events_key_t&, const std::shared_ptr<MockEventsHistory> &history) {
96 6 : const auto &key = history->getKey();
97 6 : const std::string &method = key.getMethod();
98 6 : const std::string &uri = key.getUri();
99 :
100 7 : if (!requestMethod.empty() && method != requestMethod) return;
101 4 : if (uriRegex && !std::regex_search(uri, *uriRegex)) return;
102 :
103 3 : read_guard_t guard(history->getMutex());
104 6 : for (const auto &ev : history->getEvents()) {
105 3 : auto clientEv = std::static_pointer_cast<MockClientEvent>(ev);
106 6 : const auto &json = clientEv->getJson();
107 3 : std::uint64_t sendTs = json["sendingTimestampUs"].get<std::uint64_t>();
108 3 : std::uint64_t recvTs = json.contains("receptionTimestampUs") ? json["receptionTimestampUs"].get<std::uint64_t>() : 0;
109 3 : std::string provId = json.contains("clientProvisionId") ? json["clientProvisionId"].get<std::string>() : "";
110 :
111 : // Send entry:
112 3 : if ((!fromTimestampUs || sendTs >= fromTimestampUs) && (!toTimestampUs || sendTs <= toTimestampUs)) {
113 3 : entries.push_back({sendTs, "send", method, uri, provId});
114 : }
115 : // Recv entry:
116 3 : if (recvTs && (!fromTimestampUs || recvTs >= fromTimestampUs) && (!toTimestampUs || recvTs <= toTimestampUs)) {
117 3 : entries.push_back({recvTs, "recv", method, uri, provId});
118 : }
119 3 : }
120 21 : });
121 :
122 4 : std::sort(entries.begin(), entries.end(), [](const Entry &a, const Entry &b) {
123 9 : return a.timestampUs < b.timestampUs;
124 : });
125 :
126 4 : nlohmann::json result = nlohmann::json::array();
127 10 : for (const auto &e : entries) {
128 6 : nlohmann::json obj;
129 6 : obj["direction"] = e.direction;
130 6 : obj["method"] = e.method;
131 6 : obj["uri"] = e.uri;
132 6 : obj["timestampUs"] = e.timestampUs;
133 6 : if (!e.clientProvisionId.empty()) obj["clientProvisionId"] = e.clientProvisionId;
134 6 : result.push_back(std::move(obj));
135 6 : }
136 :
137 8 : return result.dump();
138 4 : }
139 :
140 : }
141 : }
142 :
|