|
20 | 20 |
|
21 | 21 | #include <fluent-bit.h> |
22 | 22 | #include <fluent-bit/flb_time.h> |
| 23 | +#include <fluent-bit/flb_pack.h> |
23 | 24 | #include <mpack/mpack.h> |
| 25 | +#include <msgpack.h> |
| 26 | +#include <msgpack/timestamp.h> |
24 | 27 | #include "flb_tests_internal.h" |
25 | 28 |
|
| 29 | +#define SEC_32BIT 1647061992 /* 0x622c2be8 */ |
| 30 | +#define NSEC_32BIT 123000000 /* 123ms 0x0754d4c0 */ |
| 31 | +#define D_SEC 1647061992.123; |
| 32 | +const char eventtime[8] = {0x62, 0x2c, 0x2b, 0xe8, 0x07, 0x54, 0xd4, 0xc0 }; |
26 | 33 |
|
27 | 34 | void test_to_nanosec() |
28 | 35 | { |
@@ -88,8 +95,241 @@ void test_append_to_mpack_v1() { |
88 | 95 | flb_free(data); |
89 | 96 | } |
90 | 97 |
|
| 98 | +void test_msgpack_to_time_int() |
| 99 | +{ |
| 100 | + struct flb_time tm; |
| 101 | + int64_t expect = SEC_32BIT; |
| 102 | + int ret; |
| 103 | + |
| 104 | + msgpack_packer mp_pck; |
| 105 | + msgpack_sbuffer mp_sbuf; |
| 106 | + msgpack_unpacked result; |
| 107 | + |
| 108 | + msgpack_object tm_obj; |
| 109 | + |
| 110 | + /* create int object*/ |
| 111 | + msgpack_sbuffer_init(&mp_sbuf); |
| 112 | + msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write); |
| 113 | + msgpack_pack_int(&mp_pck, expect); |
| 114 | + |
| 115 | + msgpack_unpacked_init(&result); |
| 116 | + msgpack_unpack_next(&result, mp_sbuf.data, mp_sbuf.size, NULL); |
| 117 | + |
| 118 | + tm_obj = result.data; |
| 119 | + ret = flb_time_msgpack_to_time(&tm, &tm_obj); |
| 120 | + if(!TEST_CHECK(ret == 0)) { |
| 121 | + TEST_MSG("flb_time_msgpack_to_time failed"); |
| 122 | + exit(EXIT_FAILURE); |
| 123 | + } |
| 124 | + |
| 125 | + if (!TEST_CHECK(tm.tm.tv_sec == expect && tm.tm.tv_nsec == 0)) { |
| 126 | + TEST_MSG("got %ld.%ld, expect %ld.%d", tm.tm.tv_sec, tm.tm.tv_nsec, expect, 0); |
| 127 | + } |
| 128 | + |
| 129 | + msgpack_sbuffer_destroy(&mp_sbuf); |
| 130 | + msgpack_unpacked_destroy(&result); |
| 131 | +} |
| 132 | + |
| 133 | +void test_msgpack_to_time_double() |
| 134 | +{ |
| 135 | + struct flb_time tm; |
| 136 | + double d_time = D_SEC; |
| 137 | + int64_t expect_sec = SEC_32BIT; |
| 138 | + int64_t expect_nsec = NSEC_32BIT; |
| 139 | + |
| 140 | + int ret; |
| 141 | + |
| 142 | + msgpack_packer mp_pck; |
| 143 | + msgpack_sbuffer mp_sbuf; |
| 144 | + msgpack_unpacked result; |
| 145 | + |
| 146 | + msgpack_object tm_obj; |
| 147 | + |
| 148 | + /* create int object*/ |
| 149 | + msgpack_sbuffer_init(&mp_sbuf); |
| 150 | + msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write); |
| 151 | + msgpack_pack_double(&mp_pck, d_time); |
| 152 | + |
| 153 | + msgpack_unpacked_init(&result); |
| 154 | + msgpack_unpack_next(&result, mp_sbuf.data, mp_sbuf.size, NULL); |
| 155 | + |
| 156 | + tm_obj = result.data; |
| 157 | + ret = flb_time_msgpack_to_time(&tm, &tm_obj); |
| 158 | + if(!TEST_CHECK(ret == 0)) { |
| 159 | + TEST_MSG("flb_time_msgpack_to_time failed"); |
| 160 | + exit(EXIT_FAILURE); |
| 161 | + } |
| 162 | + |
| 163 | + if (!TEST_CHECK(tm.tm.tv_sec == expect_sec && |
| 164 | + llabs(tm.tm.tv_nsec - expect_nsec ) < 10000 /* 10us*/)) { |
| 165 | + TEST_MSG("got %ld.%ld, expect %ld.%ld", tm.tm.tv_sec, tm.tm.tv_nsec, expect_sec, expect_nsec); |
| 166 | + } |
| 167 | + |
| 168 | + msgpack_sbuffer_destroy(&mp_sbuf); |
| 169 | + msgpack_unpacked_destroy(&result); |
| 170 | +} |
| 171 | + |
| 172 | +void test_msgpack_to_time_eventtime() |
| 173 | +{ |
| 174 | + struct flb_time tm; |
| 175 | + int64_t expect_sec = SEC_32BIT; |
| 176 | + int64_t expect_nsec = NSEC_32BIT; |
| 177 | + char ext_data[8] = {0}; |
| 178 | + int ret; |
| 179 | + |
| 180 | + msgpack_packer mp_pck; |
| 181 | + msgpack_sbuffer mp_sbuf; |
| 182 | + msgpack_unpacked result; |
| 183 | + |
| 184 | + msgpack_object tm_obj; |
| 185 | + |
| 186 | + memcpy(&ext_data[0], &eventtime[0], 8); |
| 187 | + |
| 188 | + /* create int object*/ |
| 189 | + msgpack_sbuffer_init(&mp_sbuf); |
| 190 | + msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write); |
| 191 | + |
| 192 | + /* https://github.com/fluent/fluentd/wiki/Forward-Protocol-Specification-v1#eventtime-ext-format */ |
| 193 | + msgpack_pack_ext(&mp_pck, 8/*fixext8*/, 0); |
| 194 | + msgpack_pack_ext_body(&mp_pck, ext_data, sizeof(ext_data)); |
| 195 | + |
| 196 | + msgpack_unpacked_init(&result); |
| 197 | + msgpack_unpack_next(&result, mp_sbuf.data, mp_sbuf.size, NULL); |
| 198 | + |
| 199 | + tm_obj = result.data; |
| 200 | + ret = flb_time_msgpack_to_time(&tm, &tm_obj); |
| 201 | + if(!TEST_CHECK(ret == 0)) { |
| 202 | + TEST_MSG("flb_time_msgpack_to_time failed"); |
| 203 | + exit(EXIT_FAILURE); |
| 204 | + } |
| 205 | + |
| 206 | + if (!TEST_CHECK(tm.tm.tv_sec == expect_sec && |
| 207 | + llabs(tm.tm.tv_nsec - expect_nsec ) < 10000 /* 10us*/)) { |
| 208 | + TEST_MSG("got %ld.%ld, expect %ld.%ld", tm.tm.tv_sec, tm.tm.tv_nsec, expect_sec, expect_nsec); |
| 209 | + } |
| 210 | + |
| 211 | + msgpack_sbuffer_destroy(&mp_sbuf); |
| 212 | + msgpack_unpacked_destroy(&result); |
| 213 | +} |
| 214 | + |
| 215 | +void test_msgpack_to_time_invalid() |
| 216 | +{ |
| 217 | + struct flb_time tm; |
| 218 | + char ext_data[8] = {0x00, 0x11, 0x22, 0xaa, 0xbb, 0xcc, 0xdd, 0xee}; |
| 219 | + int ret; |
| 220 | + |
| 221 | + msgpack_packer mp_pck; |
| 222 | + msgpack_sbuffer mp_sbuf; |
| 223 | + msgpack_unpacked result; |
| 224 | + |
| 225 | + |
| 226 | + msgpack_object tm_obj; |
| 227 | + |
| 228 | + /* create int object*/ |
| 229 | + msgpack_sbuffer_init(&mp_sbuf); |
| 230 | + msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write); |
| 231 | + |
| 232 | + msgpack_pack_ext(&mp_pck, 5 /* invalid size */, 0); |
| 233 | + msgpack_pack_ext_body(&mp_pck, ext_data, 5); |
| 234 | + |
| 235 | + msgpack_unpacked_init(&result); |
| 236 | + msgpack_unpack_next(&result, mp_sbuf.data, mp_sbuf.size, NULL); |
| 237 | + |
| 238 | + tm_obj = result.data; |
| 239 | + |
| 240 | + /* Check if ext */ |
| 241 | + TEST_CHECK(tm_obj.type == MSGPACK_OBJECT_EXT); |
| 242 | + TEST_CHECK(tm_obj.via.ext.type == 0); |
| 243 | + TEST_CHECK(tm_obj.via.ext.size == 5); |
| 244 | + |
| 245 | + ret = flb_time_msgpack_to_time(&tm, &tm_obj); |
| 246 | + if(!TEST_CHECK(ret != 0)) { |
| 247 | + TEST_MSG("flb_time_msgpack_to_time should fail"); |
| 248 | + exit(EXIT_FAILURE); |
| 249 | + } |
| 250 | + |
| 251 | + msgpack_sbuffer_destroy(&mp_sbuf); |
| 252 | + msgpack_unpacked_destroy(&result); |
| 253 | + |
| 254 | + |
| 255 | + /* create int object*/ |
| 256 | + msgpack_sbuffer_init(&mp_sbuf); |
| 257 | + msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write); |
| 258 | + |
| 259 | + msgpack_pack_ext(&mp_pck, 8, 10 /* invalid type */); |
| 260 | + msgpack_pack_ext_body(&mp_pck, ext_data, 8); |
| 261 | + |
| 262 | + msgpack_unpacked_init(&result); |
| 263 | + msgpack_unpack_next(&result, mp_sbuf.data, mp_sbuf.size, NULL); |
| 264 | + |
| 265 | + tm_obj = result.data; |
| 266 | + |
| 267 | + /* Check if ext */ |
| 268 | + TEST_CHECK(tm_obj.type == MSGPACK_OBJECT_EXT); |
| 269 | + TEST_CHECK(tm_obj.via.ext.type == 10); |
| 270 | + TEST_CHECK(tm_obj.via.ext.size == 8); |
| 271 | + |
| 272 | + ret = flb_time_msgpack_to_time(&tm, &tm_obj); |
| 273 | + if(!TEST_CHECK(ret != 0)) { |
| 274 | + TEST_MSG("flb_time_msgpack_to_time should fail"); |
| 275 | + exit(EXIT_FAILURE); |
| 276 | + } |
| 277 | + |
| 278 | + msgpack_sbuffer_destroy(&mp_sbuf); |
| 279 | + msgpack_unpacked_destroy(&result); |
| 280 | +} |
| 281 | + |
| 282 | +void test_append_to_msgpack_eventtime() |
| 283 | +{ |
| 284 | + struct flb_time tm; |
| 285 | + int ret; |
| 286 | + char expect_data[8] = {0}; |
| 287 | + |
| 288 | + msgpack_packer mp_pck; |
| 289 | + msgpack_sbuffer mp_sbuf; |
| 290 | + msgpack_unpacked result; |
| 291 | + |
| 292 | + msgpack_object tm_obj; |
| 293 | + |
| 294 | + memcpy(&expect_data[0], &eventtime[0], 8); |
| 295 | + |
| 296 | + tm.tm.tv_sec = SEC_32BIT; |
| 297 | + tm.tm.tv_nsec = NSEC_32BIT; |
| 298 | + |
| 299 | + /* create int object*/ |
| 300 | + msgpack_sbuffer_init(&mp_sbuf); |
| 301 | + msgpack_packer_init(&mp_pck, &mp_sbuf, msgpack_sbuffer_write); |
| 302 | + |
| 303 | + ret = flb_time_append_to_msgpack(&tm, &mp_pck, FLB_TIME_ETFMT_V1_FIXEXT); |
| 304 | + if(!TEST_CHECK(ret == 0)) { |
| 305 | + TEST_MSG("flb_time_append_to_msgpack failed"); |
| 306 | + exit(EXIT_FAILURE); |
| 307 | + } |
| 308 | + msgpack_unpacked_init(&result); |
| 309 | + msgpack_unpack_next(&result, mp_sbuf.data, mp_sbuf.size, NULL); |
| 310 | + |
| 311 | + tm_obj = result.data; |
| 312 | + |
| 313 | + /* Check if Eventtime */ |
| 314 | + TEST_CHECK(tm_obj.type == MSGPACK_OBJECT_EXT); |
| 315 | + TEST_CHECK(tm_obj.via.ext.type == 0); |
| 316 | + TEST_CHECK(tm_obj.via.ext.size == 8); |
| 317 | + |
| 318 | + if (!TEST_CHECK(memcmp(&expect_data[0], tm_obj.via.ext.ptr, 8) == 0) ) { |
| 319 | + TEST_MSG("got 0x%x, expect 0x%x", *(uint32_t*)tm_obj.via.ext.ptr, *((uint32_t*)&expect_data[0])); |
| 320 | + } |
| 321 | + |
| 322 | + msgpack_sbuffer_destroy(&mp_sbuf); |
| 323 | + msgpack_unpacked_destroy(&result); |
| 324 | +} |
| 325 | + |
91 | 326 | TEST_LIST = { |
92 | 327 | { "flb_time_to_nanosec" , test_to_nanosec}, |
93 | 328 | { "flb_time_append_to_mpack_v1" , test_append_to_mpack_v1}, |
| 329 | + { "msgpack_to_time_int" , test_msgpack_to_time_int}, |
| 330 | + { "msgpack_to_time_double" , test_msgpack_to_time_double}, |
| 331 | + { "msgpack_to_time_eventtime" , test_msgpack_to_time_eventtime}, |
| 332 | + { "msgpack_to_time_invalid" , test_msgpack_to_time_invalid}, |
| 333 | + { "append_to_msgpack_eventtime" , test_append_to_msgpack_eventtime}, |
94 | 334 | { NULL, NULL } |
95 | 335 | }; |
0 commit comments