|
| 1 | +#include <string.h> |
| 2 | + |
| 3 | +#include <exception> |
| 4 | +#include <string> |
| 5 | +#include <cstdlib> |
| 6 | +#include <list> |
| 7 | + |
| 8 | +#define WANT_TEST_EXTRAS |
| 9 | +#include <tap++/tap++.h> |
| 10 | + |
| 11 | +#include "blobstamper/blobstamper.h" |
| 12 | +#include "blobstamper/helpers.h" |
| 13 | + |
| 14 | + |
| 15 | +using namespace TAP; |
| 16 | + |
| 17 | + |
| 18 | +/* Tests for very basic stamp operations */ |
| 19 | + |
| 20 | + |
| 21 | +/* This stamps chops first two bytes and treat them as string */ |
| 22 | +/* Never do this in real live, as blob is binary and may have \0 in the middle of it*/ |
| 23 | +class StampForTest: public StampGeneric |
| 24 | +{ |
| 25 | + public: |
| 26 | + StampForTest(); |
| 27 | + std::string ExtractStr(Blob &blob) override; |
| 28 | +}; |
| 29 | + |
| 30 | +StampForTest::StampForTest() : StampGeneric() |
| 31 | +{ |
| 32 | + min_size = 2; /* This stamp shifts two characters only */ |
| 33 | + max_size = 2; |
| 34 | + is_fixed_size = true; |
| 35 | +} |
| 36 | + |
| 37 | +std::string |
| 38 | +StampForTest::ExtractStr(Blob &blob) |
| 39 | +{ |
| 40 | + char * buf; |
| 41 | + size_t buf_size; |
| 42 | + |
| 43 | + Blob blob2 = blob.ShiftBytes(min_size); |
| 44 | + if (blob2.isEmpty()) |
| 45 | + return ""; |
| 46 | + |
| 47 | + /* Save shited data as string */ |
| 48 | + /* NEVER do this in prod, as in real live blob is binary and may have 0 in the middle of it */ |
| 49 | + blob2.DataDup(buf, buf_size); |
| 50 | + buf = (char *) realloc((void *)buf, buf_size + 1); |
| 51 | + buf[buf_size] = '\0'; |
| 52 | + std::string res = buf; |
| 53 | + free(buf); |
| 54 | + |
| 55 | + return res; |
| 56 | +} |
| 57 | + |
| 58 | + |
| 59 | +char my_data[]="1234567890ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyzАБВГДитд.___"; |
| 60 | + |
| 61 | +char short_sample[]="1234567"; |
| 62 | + |
| 63 | +int |
| 64 | +main() |
| 65 | +{ |
| 66 | + char *ptr; |
| 67 | + size_t size; |
| 68 | + |
| 69 | + TEST_START(7); |
| 70 | + |
| 71 | + /* Test that ShiftSingleStampStr shifts ok with StampForTest stamp */ |
| 72 | + { /* 1..3 */ |
| 73 | + std::string expected1 = "12"; |
| 74 | + char* expected2 = "34567"; |
| 75 | + |
| 76 | + |
| 77 | + Blob blob(short_sample, strlen(short_sample)); |
| 78 | + StampForTest stamp; |
| 79 | + std::string str = blob.ShiftSingleStampStr(stamp); |
| 80 | + ok(str == expected1, "ShiftSingleStampStr: shifts ok"); |
| 81 | + |
| 82 | + blob.DataDup(ptr,size); |
| 83 | + ok(size == strlen(expected2), "ShiftSingleStampStr: Remaining blob data size ok"); |
| 84 | + ok(! memcmp(expected2, ptr, size), "ShiftSingleStampStr: Remaining blob data ok"); |
| 85 | + free(ptr); |
| 86 | + } |
| 87 | + |
| 88 | + /* Test that StamList really splits the whole blob to the specified stamps */ |
| 89 | + { /* 4..7 */ |
| 90 | + std::string expected1 = "12"; |
| 91 | + std::string expected2 = "34"; |
| 92 | + std::string expected3 = "56"; |
| 93 | + |
| 94 | + StampForTest stamp; |
| 95 | + StampList stamp_list(stamp); |
| 96 | + Blob blob(short_sample, strlen(short_sample)); |
| 97 | + std::list<std::string> res = stamp_list.ExtractStrList(blob); |
| 98 | + |
| 99 | + std::string str; |
| 100 | + |
| 101 | + str = res.front(); |
| 102 | + ok(str == expected1, "ExtractStrList: First element of shifted list is ok"); |
| 103 | + res.pop_front(); |
| 104 | + |
| 105 | + str = res.front(); |
| 106 | + ok(str == expected2, "ExtractStrList: Second element of shifted list is ok"); |
| 107 | + res.pop_front(); |
| 108 | + |
| 109 | + str = res.front(); |
| 110 | + ok(str == expected3, "ExtractStrList: Third element of shifted list is ok"); |
| 111 | + res.pop_front(); |
| 112 | + |
| 113 | + ok(res.empty(), "ExtractStrList: The rest of the list is empty"); |
| 114 | + } |
| 115 | + |
| 116 | + TEST_END; |
| 117 | +} |
0 commit comments