| 1 |
/* |
| 2 |
* |
| 3 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 4 |
* or more contributor license agreements. See the NOTICE file |
| 5 |
* distributed with this work for additional information |
| 6 |
* regarding copyright ownership. The ASF licenses this file |
| 7 |
* to you under the Apache License, Version 2.0 (the |
| 8 |
* "License"); you may not use this file except in compliance |
| 9 |
* with the License. You may obtain a copy of the License at |
| 10 |
* |
| 11 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, |
| 14 |
* software distributed under the License is distributed on an |
| 15 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 |
* KIND, either express or implied. See the License for the |
| 17 |
* specific language governing permissions and limitations |
| 18 |
* under the License. |
| 19 |
* |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "./pn_test.hpp" |
| 23 |
|
| 24 |
#include "core/data.h" |
| 25 |
|
| 26 |
#include <proton/codec.h> |
| 27 |
#include <proton/error.h> |
| 28 |
|
| 29 |
#include <cstdarg> |
| 30 |
|
| 31 |
using namespace pn_test; |
| 32 |
|
| 33 |
// Make sure we can grow the capacity of a pn_data_t all the way to the max and |
| 34 |
// we stop there. |
| 35 |
TEST_CASE("data_grow") { |
| 36 |
auto_free<pn_data_t, pn_data_free> data(pn_data(0)); |
| 37 |
int code = 0; |
| 38 |
while (pn_data_size(data) < PNI_NID_MAX && !code) { |
| 39 |
code = pn_data_put_int(data, 1); |
| 40 |
} |
| 41 |
CHECK_THAT(*pn_data_error(data), error_empty()); |
| 42 |
CHECK(pn_data_size(data) == PNI_NID_MAX); |
| 43 |
code = pn_data_put_int(data, 1); |
| 44 |
INFO(pn_code(code)); |
| 45 |
CHECK(code == PN_OUT_OF_MEMORY); |
| 46 |
CHECK(pn_data_size(data) == PNI_NID_MAX); |
| 47 |
} |
| 48 |
|
| 49 |
TEST_CASE("data_multiple") { |
| 50 |
auto_free<pn_data_t, pn_data_free> data(pn_data(1)); |
| 51 |
auto_free<pn_data_t, pn_data_free> src(pn_data(1)); |
| 52 |
|
| 53 |
/* NULL data pointer */ |
| 54 |
pn_data_fill(data, "M", NULL); |
| 55 |
CHECK("null" == inspect(data)); |
| 56 |
|
| 57 |
/* Empty data object */ |
| 58 |
pn_data_clear(data); |
| 59 |
pn_data_fill(data, "M", src.get()); |
| 60 |
CHECK("null" == inspect(data)); |
| 61 |
|
| 62 |
/* Empty array */ |
| 63 |
pn_data_clear(data); |
| 64 |
pn_data_clear(src); |
| 65 |
pn_data_put_array(src, false, PN_SYMBOL); |
| 66 |
pn_data_fill(data, "M", src.get()); |
| 67 |
CHECK("null" == inspect(data)); |
| 68 |
|
| 69 |
/* Single-element array */ |
| 70 |
pn_data_clear(data); |
| 71 |
pn_data_clear(src); |
| 72 |
pn_data_put_array(src, false, PN_SYMBOL); |
| 73 |
pn_data_enter(src); |
| 74 |
pn_data_put_symbol(src, pn_bytes("foo")); |
| 75 |
pn_data_fill(data, "M", src.get()); |
| 76 |
CHECK(":foo" == inspect(data)); |
| 77 |
|
| 78 |
/* Multi-element array */ |
| 79 |
pn_data_clear(data); |
| 80 |
pn_data_clear(src); |
| 81 |
pn_data_put_array(src, false, PN_SYMBOL); |
| 82 |
pn_data_enter(src); |
| 83 |
pn_data_put_symbol(src, pn_bytes("foo")); |
| 84 |
pn_data_put_symbol(src, pn_bytes("bar")); |
| 85 |
pn_data_fill(data, "M", src.get()); |
| 86 |
CHECK("@PN_SYMBOL[:foo, :bar]" == inspect(data)); |
| 87 |
|
| 88 |
/* Non-array */ |
| 89 |
pn_data_clear(data); |
| 90 |
pn_data_clear(src); |
| 91 |
pn_data_put_symbol(src, pn_bytes("baz")); |
| 92 |
pn_data_fill(data, "M", src.get()); |
| 93 |
CHECK(":baz" == inspect(data)); |
| 94 |
|
| 95 |
/* Described list with open frame descriptor */ |
| 96 |
pn_data_clear(data); |
| 97 |
pn_data_fill(data, "DL[]", (uint64_t)16); |
| 98 |
CHECK("@open(16) []" == inspect(data)); |
| 99 |
|
| 100 |
/* open frame with some fields */ |
| 101 |
pn_data_clear(data); |
| 102 |
pn_data_fill(data, "DL[SSnI]", (uint64_t)16, "container-1", 0, 965); |
| 103 |
CHECK("@open(16) [container-id=\"container-1\", channel-max=965]" == inspect(data)); |
| 104 |
|
| 105 |
/* Map */ |
| 106 |
pn_data_clear(data); |
| 107 |
pn_data_fill(data, "{S[iii]SI}", "foo", 1, 987, 3, "bar", 965); |
| 108 |
CHECK("{\"foo\"=[1, 987, 3], \"bar\"=965}" == inspect(data)); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
#define BUFSIZE 1024 |
| 113 |
static void check_encode_decode(auto_free<pn_data_t, pn_data_free>& src) { |
| 114 |
char buf[BUFSIZE]; |
| 115 |
auto_free<pn_data_t, pn_data_free> data(pn_data(1)); |
| 116 |
pn_data_clear(data); |
| 117 |
|
| 118 |
// Encode src array to buf |
| 119 |
int enc_size = pn_data_encode(src, buf, BUFSIZE - 1); |
| 120 |
if (enc_size < 0) { |
| 121 |
FAIL("pn_data_encode() error " << enc_size << ": " << pn_code(enc_size)); |
| 122 |
} |
| 123 |
|
| 124 |
// Decode buf to data |
| 125 |
int dec_size = pn_data_decode(data, buf, BUFSIZE - 1); |
| 126 |
pn_error_t *dec_err = pn_data_error(data); |
| 127 |
CHECK(pn_error_code(dec_err) == 0); |
| 128 |
if (dec_size < 0) { |
| 129 |
FAIL("pn_data_decode() error " << dec_size << ": " << pn_code(dec_size)); |
| 130 |
} |
| 131 |
|
| 132 |
// Checks |
| 133 |
CHECK(enc_size == dec_size); |
| 134 |
CHECK(inspect(src) == inspect(data)); |
| 135 |
} |
| 136 |
|
| 137 |
static void check_array(const char *fmt, ...) { |
| 138 |
auto_free<pn_data_t, pn_data_free> src(pn_data(1)); |
| 139 |
pn_data_clear(src); |
| 140 |
|
| 141 |
// Create src array |
| 142 |
va_list ap; |
| 143 |
va_start(ap, fmt); |
| 144 |
pn_data_vfill(src, fmt, ap); |
| 145 |
va_end(ap); |
| 146 |
|
| 147 |
check_encode_decode(src); |
| 148 |
} |
| 149 |
|
| 150 |
TEST_CASE("array_list") { |
| 151 |
check_array("@T[]", PN_LIST); |
| 152 |
// TODO: PROTON-2248: using S and s reversed |
| 153 |
// empty list as first array element |
| 154 |
check_array("@T[[][oo][][iii][Sosid]]", PN_LIST, true, false, 1, 2, 3, "hello", false, "world", 43210, 2.565e-56); |
| 155 |
// empty list not as first array element |
| 156 |
check_array("@T[[Sid][oooo][]]", PN_LIST, "aaa", 123, double(3.2415), true, true, false, true); |
| 157 |
// only empty lists |
| 158 |
check_array("@T[[][][][][]]", PN_LIST); |
| 159 |
} |