Debian Codegraph @

c/tests/pn_test.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "./pn_test.hpp"
21
22 #include <proton/condition.h>
23 #include <proton/connection.h>
24 #include <proton/delivery.h>
25 #include <proton/event.h>
26 #include <proton/link.h>
27 #include <proton/message.h>
28 #include <proton/netaddr.h>
29 #include <proton/object.h>
30 #include <proton/transport.h>
31
32 #include <sstream>
33 #include <utility>
34
35 std::ostream &operator<<(std::ostream &o, pn_event_type_t et) {
36 return o << pn_event_type_name(et);
37 }
38
39 inline std::ostream &quote(std::ostream &o, const char *s) {
40 return s ? (o << '"' << s << '"') : (o << "null");
41 }
42
43 std::ostream &operator<<(std::ostream &o, const pn_condition_t &const_cond) {
44 pn_condition_t *cond = const_cast<pn_condition_t *>(&const_cond);
45 o << "pn_condition{";
46 if (pn_condition_is_set(cond)) {
47 quote(o, pn_condition_get_name(cond)) << ", ";
48 quote(o, pn_condition_get_description(cond));
49 }
50 o << "}";
51 return o;
52 }
53
54 std::ostream &operator<<(std::ostream &o, const pn_error_t &const_err) {
55 pn_error_t *err = const_cast<pn_error_t *>(&const_err);
56 o << "pn_error{" << pn_code(pn_error_code(err)) << ", ";
57 return quote(o, pn_error_text(err)) << "}";
58 }
59
60 namespace pn_test {
61
62 std::string inspect(void *obj) {
63 auto_free<pn_string_t, pn_string_free> s(pn_string(NULL));
64 pn_inspect(obj, s);
65 return pn_string_get(s);
66 }
67
68 etypes make_etypes_(int first, ...) {
69 etypes v;
70 va_list ap;
71 va_start(ap, first);
72 for (int i = first; i >= 0; i = va_arg(ap, int)) {
73 v.push_back(static_cast<pn_event_type_t>(i));
74 }
75 va_end(ap);
76 return v;
77 }
78
79 std::ostream &operator<<(std::ostream &o, const etypes &et) {
80 return o << Catch::Detail::stringify(static_cast<std::vector<pn_event_type_t> >(et));
81 }
82
83 pn_bytes_t pn_bytes(const std::string &s) {
84 return ::pn_bytes(s.size(), s.data());
85 }
86
87 void rwbytes_ensure(pn_rwbytes_t *buf, size_t size) {
88 if (buf->start == NULL || buf->size < size) {
89 buf->start = (char *)realloc(buf->start, size);
90 buf->size = size;
91 }
92 }
93
94 void message_decode(pn_message_t *m, pn_delivery_t *d, pn_rwbytes_t *buf) {
95 ssize_t size = pn_delivery_pending(d);
96 rwbytes_ensure(buf, size);
97 ssize_t result = pn_link_recv(pn_delivery_link(d), buf->start, size);
98 REQUIRE(size == result);
99 pn_message_clear(m);
100 if (pn_message_decode(m, buf->start, size)) FAIL(pn_message_error(m));
101 }
102
103 handler::handler()
104 : last_condition(pn_condition()), connection(), session(), link(), sender(),
105 receiver(), delivery(), message() {}
106
107 bool handler::dispatch(pn_event_t *e) {
108 log.push_back(pn_event_type(e));
109 if (pn_event_condition(e)) {
110 pn_condition_copy(last_condition, pn_event_condition(e));
111 } else {
112 pn_condition_clear(last_condition);
113 }
114 return handle(e);
115 }
116
117 etypes handler::log_clear() {
118 etypes ret;
119 std::swap(ret, log);
120 return ret;
121 }
122
123 pn_event_type_t handler::log_last() {
124 pn_event_type_t et = log.empty() ? PN_EVENT_NONE : log.back();
125 log.clear();
126 return et;
127 }
128
129 driver::driver(struct handler &h) : handler(h) {
130 pn_connection_driver_init(this, NULL, NULL);
131 }
132 driver::~driver() { pn_connection_driver_destroy(this); }
133
134 pn_event_type_t driver::run(pn_event_type_t stop) {
135 pn_event_t *e = NULL;
136 while ((e = pn_connection_driver_next_event(this))) {
137 pn_event_type_t et = pn_event_type(e);
138 if (handler.dispatch(e) || et == stop) return et;
139 }
140 return PN_EVENT_NONE;
141 }
142
143 driver_pair::driver_pair(handler &ch, handler &sh) : client(ch), server(sh) {
144 pn_transport_set_server(server.transport);
145 }
146
147 size_t driver::read(pn_connection_driver_t &src) {
148 pn_bytes_t wb = pn_connection_driver_write_buffer(&src);
149 pn_rwbytes_t rb = pn_connection_driver_read_buffer(this);
150 size_t size = rb.size < wb.size ? rb.size : wb.size;
151 if (size) {
152 std::copy(wb.start, wb.start + size, rb.start);
153 pn_connection_driver_write_done(&src, size);
154 pn_connection_driver_read_done(this, size);
155 }
156 return size;
157 }
158
159 pn_event_type_t driver_pair::run() {
160 pn_connection_open(client.connection); // Make sure it is open
161 size_t n = 0;
162 do {
163 pn_event_type_t et = PN_EVENT_NONE;
164 if ((et = client.run())) return et;
165 if ((et = server.run())) return et;
166 n = client.read(server) + server.read(client);
167 } while (n || pn_connection_driver_has_event(&client) ||
168 pn_connection_driver_has_event(&server));
169 return PN_EVENT_NONE;
170 }
171
172 std::string cond_empty::describe() const { return "is empty"; }
173
174 bool cond_empty::match(const pn_condition_t &cond) const {
175 return !pn_condition_is_set(const_cast<pn_condition_t *>(&cond));
176 }
177
178 cond_matches::cond_matches(std::string name, std::string desc)
179 : name_(std::move(name)), desc_(std::move(desc)) {}
180
181 std::string cond_matches::describe() const {
182 std::ostringstream o;
183 o << "matches " << Catch::Detail::stringify(name_);
184 if (!desc_.empty()) o << ", " + Catch::Detail::stringify(desc_);
185 return o.str();
186 }
187
188 bool cond_matches::match(const pn_condition_t &const_cond) const {
189 pn_condition_t *cond = const_cast<pn_condition_t *>(&const_cond);
190 const char *name = pn_condition_get_name(cond);
191 const char *desc = pn_condition_get_description(cond);
192 return pn_condition_is_set(cond) && name && name_ == name &&
193 (desc_.empty() || (desc && Catch::contains(desc, desc_)));
194 }
195
196 std::string error_empty::describe() const { return "is empty"; }
197
198 bool error_empty::match(const pn_error_t &err) const {
199 return !pn_error_code(const_cast<pn_error_t *>(&err));
200 }
201
202 error_matches::error_matches(int code, std::string desc)
203 : code_(code), desc_(std::move(desc)) {}
204
205 std::string error_matches::describe() const {
206 std::ostringstream o;
207 o << "matches " << pn_code(code_);
208 if (!desc_.empty()) o << ", " + Catch::Detail::stringify(desc_);
209 return o.str();
210 }
211
212 bool error_matches::match(const pn_error_t &const_err) const {
213 pn_error_t *err = const_cast<pn_error_t *>(&const_err);
214 int code = pn_error_code(err);
215 const char *desc = pn_error_text(err);
216 return code_ == code &&
217 (desc_.empty() || (desc && Catch::contains(desc, desc_)));
218 }
219
220 } // namespace pn_test