| 1 |
#ifndef TESTS_PN_TEST_HPP |
| 2 |
#define TESTS_PN_TEST_HPP |
| 3 |
/* |
| 4 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 5 |
* or more contributor license agreements. See the NOTICE file |
| 6 |
* distributed with this work for additional information |
| 7 |
* regarding copyright ownership. The ASF licenses this file |
| 8 |
* to you under the Apache License, Version 2.0 (the |
| 9 |
* "License"); you may not use this file except in compliance |
| 10 |
* with the License. You may obtain a copy of the License at |
| 11 |
* |
| 12 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
* |
| 14 |
* Unless required by applicable law or agreed to in writing, |
| 15 |
* software distributed under the License is distributed on an |
| 16 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 17 |
* KIND, either express or implied. See the License for the |
| 18 |
* specific language governing permissions and limitations |
| 19 |
* under the License. |
| 20 |
*/ |
| 21 |
|
| 22 |
/// @file |
| 23 |
/// |
| 24 |
/// Wrappers and Catch2 extensions to simplify testing the proton-C library. |
| 25 |
|
| 26 |
#include <catch_extra.hpp> |
| 27 |
|
| 28 |
#include <proton/condition.h> |
| 29 |
#include <proton/connection_driver.h> |
| 30 |
#include <proton/event.h> |
| 31 |
#include <proton/message.h> |
| 32 |
|
| 33 |
#include <iosfwd> |
| 34 |
#include <string> |
| 35 |
#include <vector> |
| 36 |
|
| 37 |
// String form of C pn_ types used in tests, must be in global C namespace |
| 38 |
// Note objects are passed by reference, not pointer. |
| 39 |
std::ostream &operator<<(std::ostream &, pn_event_type_t); |
| 40 |
std::ostream &operator<<(std::ostream &, const pn_condition_t &); |
| 41 |
std::ostream &operator<<(std::ostream &, const pn_error_t &); |
| 42 |
|
| 43 |
namespace pn_test { |
| 44 |
|
| 45 |
// Holder for T*, calls function Free() in dtor. Not copyable. |
| 46 |
template <class T, void (*Free)(T *)> class auto_free { |
| 47 |
T *ptr_; |
| 48 |
auto_free &operator=(auto_free &x); |
| 49 |
public: |
| 50 |
auto_free(auto_free &x) = delete; |
| 51 |
|
| 52 |
public: |
| 53 |
explicit auto_free(T *p = 0) : ptr_(p) {} |
| 54 |
~auto_free() { Free(ptr_); } |
| 55 |
T *get() const { return ptr_; } |
| 56 |
operator T *() const { return ptr_; } // not marking explicit for convenience |
| 57 |
}; |
| 58 |
|
| 59 |
// pn_free() works for some, but not all pn_xxx_t* types. |
| 60 |
// Add typed pn_string_free() so we can be consistent and safe. |
| 61 |
inline void pn_string_free(pn_string_t *s) { pn_free(s); } |
| 62 |
|
| 63 |
// Call pn_inspect(), return std::string |
| 64 |
std::string inspect(void *); |
| 65 |
|
| 66 |
// List of pn_event_type_t |
| 67 |
typedef std::vector<pn_event_type_t> etypes; |
| 68 |
std::ostream &operator<<(std::ostream &o, const etypes &et); |
| 69 |
|
| 70 |
// Workaround for lack of list initializers in C++03. |
| 71 |
// Use ETYPES macro, don't call make_etypes_ directly |
| 72 |
etypes make_etypes_(int first, ...); |
| 73 |
#define ETYPES(...) (make_etypes_(__VA_ARGS__, -1)) |
| 74 |
|
| 75 |
/// Make a pn_bytes_t from a std::string |
| 76 |
pn_bytes_t pn_bytes(const std::string &s); |
| 77 |
|
| 78 |
/// Ensure buf has at least size bytes, use realloc if need be |
| 79 |
void rwbytes_ensure(pn_rwbytes_t *buf, size_t size); |
| 80 |
|
| 81 |
/// Decode message from delivery into buf, expand buf as needed. |
| 82 |
void message_decode(pn_message_t *m, pn_delivery_t *d, pn_rwbytes_t *buf); |
| 83 |
|
| 84 |
// A test handler that logs the type of each event handled, and has |
| 85 |
// slots to store all of the basic proton types for ad-hoc use in |
| 86 |
// tests. Subclass and override the handle() method. |
| 87 |
struct handler { |
| 88 |
etypes log; // Log of events |
| 89 |
auto_free<pn_condition_t, pn_condition_free> |
| 90 |
last_condition; // Condition of last event |
| 91 |
|
| 92 |
// Slots to save proton objects for use outside the handler. |
| 93 |
pn_listener_t *listener; |
| 94 |
pn_connection_t *connection; |
| 95 |
pn_session_t *session; |
| 96 |
pn_link_t *link; |
| 97 |
pn_link_t *sender; |
| 98 |
pn_link_t *receiver; |
| 99 |
pn_delivery_t *delivery; |
| 100 |
pn_message_t *message; |
| 101 |
|
| 102 |
handler(); |
| 103 |
|
| 104 |
/// dispatch an event: log its type then call handle() |
| 105 |
/// Returns the value of handle() |
| 106 |
bool dispatch(pn_event_t *e); |
| 107 |
|
| 108 |
// Return the current log contents, clear the log. |
| 109 |
etypes log_clear(); |
| 110 |
|
| 111 |
// Return the last event in the log, clear the log. |
| 112 |
pn_event_type_t log_last(); |
| 113 |
|
| 114 |
protected: |
| 115 |
// Override this function to handle events. |
| 116 |
// |
| 117 |
// Return true to stop dispatching and return control to the test function, |
| 118 |
// false to continue processing. |
| 119 |
virtual bool handle(pn_event_t *e) { return false; } |
| 120 |
}; |
| 121 |
|
| 122 |
// A pn_connection_driver_t that dispatches to a pn_test::handler |
| 123 |
// |
| 124 |
// driver::run() dispatches events to the handler, but returns if the handler |
| 125 |
// returns true, or if a specific event type is handled. Test functions can |
| 126 |
// alternate between letting the handler run and checking state or calling |
| 127 |
// functions on proton objects in the test function directly. Handlers can |
| 128 |
// automate uninteresting work, the test function can make checks that are |
| 129 |
// clearly located in the flow of the test logic. |
| 130 |
struct driver : public ::pn_connection_driver_t { |
| 131 |
struct handler &handler; |
| 132 |
|
| 133 |
explicit driver(struct handler &h); |
| 134 |
~driver(); |
| 135 |
|
| 136 |
// Dispatch events till a handler returns true, the `stop` event is handled, |
| 137 |
// or there are no more events |
| 138 |
// Returns the last event handled or PN_EVENT_NONE if none were. |
| 139 |
pn_event_type_t run(pn_event_type_t stop = PN_EVENT_NONE); |
| 140 |
|
| 141 |
// Transfer available data from src write buffer to this read-buffer and |
| 142 |
// update both drivers. Return size of data transferred. |
| 143 |
size_t read(pn_connection_driver_t &src); |
| 144 |
}; |
| 145 |
|
| 146 |
// A client/server pair drivers. run() simulates a connection in memory. |
| 147 |
struct driver_pair { |
| 148 |
driver client, server; |
| 149 |
|
| 150 |
// Associate handlers with drivers. Sets server.transport to server mode |
| 151 |
driver_pair(handler &ch, handler &sh); |
| 152 |
|
| 153 |
// Run the drivers until a handle returns true or there is nothing left to |
| 154 |
// do. Opens the client.connection() if not already open. |
| 155 |
// Return the last event handled or PN_EVENT_NONE |
| 156 |
pn_event_type_t run(); |
| 157 |
}; |
| 158 |
|
| 159 |
// Matches for use with Catch macros CHECK_THAT and REQUIRE_THAT. |
| 160 |
// Check pn_condition_t and pn_error_t, failed checks report code, name, |
| 161 |
// description etc. |
| 162 |
|
| 163 |
struct cond_empty : public Catch::MatcherBase<pn_condition_t> { |
| 164 |
std::string describe() const override; |
| 165 |
bool match(const pn_condition_t &cond) const override; |
| 166 |
}; |
| 167 |
|
| 168 |
class cond_matches : public Catch::MatcherBase<pn_condition_t> { |
| 169 |
std::string name_, desc_; |
| 170 |
|
| 171 |
public: |
| 172 |
explicit cond_matches(std::string name, std::string desc_contains = ""); |
| 173 |
std::string describe() const override; |
| 174 |
bool match(const pn_condition_t &cond) const override; |
| 175 |
}; |
| 176 |
|
| 177 |
struct error_empty : public Catch::MatcherBase<pn_error_t> { |
| 178 |
std::string describe() const override; |
| 179 |
bool match(const pn_error_t &) const override; |
| 180 |
}; |
| 181 |
|
| 182 |
class error_matches : public Catch::MatcherBase<pn_error_t> { |
| 183 |
int code_; |
| 184 |
std::string desc_; |
| 185 |
|
| 186 |
public: |
| 187 |
explicit error_matches(int code, std::string desc_contains = ""); |
| 188 |
std::string describe() const override; |
| 189 |
bool match(const pn_error_t &) const override; |
| 190 |
}; |
| 191 |
|
| 192 |
} // namespace pn_test |
| 193 |
|
| 194 |
#endif // TESTS_PN_TEST_HPP |