18 #include "../suricata-common.h"
19 #include "../util-unittest.h"
21 static uint32_t Upper32(uint64_t value)
26 static uint32_t Lower32(uint64_t value)
38 static int UpdatePcapStatsValue64NoChange01(
void)
45 {.last = 12345, .current = 12345},
46 {.last = (uint64_t)UINT32_MAX, .current = UINT_MAX}};
48 for (
size_t i = 0; i <
ARRAY_SIZE(data); ++i) {
51 UpdatePcapStatsValue64(&data[i].last, data[i].current);
58 static int UpdatePcapStatsValue64NoChange02(
void)
64 TestData data[] = {{.
last = (2ull << 32) + 0, .current = 0},
65 {.last = (3ull << 32) + 12345, .current = 12345},
66 {.last = (3ull << 32) + (uint64_t)UINT32_MAX, .current = UINT_MAX},
67 {.last = UINT64_MAX, .current = UINT_MAX}};
69 for (
size_t i = 0; i <
ARRAY_SIZE(data); ++i) {
70 uint32_t upper = Upper32(data[i].last);
71 FAIL_IF_NOT(Lower32(data[i].last) == data[i].current);
73 UpdatePcapStatsValue64(&data[i].last, data[i].current);
74 FAIL_IF_NOT(Lower32(data[i].last) == data[i].current);
81 static int UpdatePcapStatsValue64NoOverflow01(
void)
89 {.last = 12345, .current = 34567},
90 {.last = (uint64_t)UINT32_MAX - 1, .current = UINT_MAX}};
92 for (
size_t i = 0; i <
ARRAY_SIZE(data); ++i) {
95 UpdatePcapStatsValue64(&data[i].last, data[i].current);
102 static int UpdatePcapStatsValue64NoOverflow02(
void)
108 TestData data[] = {{.
last = (2ull << 32) + 0, .current = 1},
109 {.last = (3ull << 32) + 12345, .current = 34567},
110 {.last = UINT64_MAX - 1, .current = UINT_MAX}};
112 for (
size_t i = 0; i <
ARRAY_SIZE(data); ++i) {
113 uint32_t upper = Upper32(data[i].last);
114 FAIL_IF_NOT(Lower32(data[i].last) < data[i].current);
116 UpdatePcapStatsValue64(&data[i].last, data[i].current);
117 FAIL_IF_NOT(Lower32(data[i].last) == data[i].current);
124 static int UpdatePcapStatsValue64Overflow01(
void)
131 {.last = 12345, .current = 22}, {.last = 12345, .current = 12344},
132 {.last = (uint64_t)UINT32_MAX, .current = UINT_MAX - 1}};
134 for (
size_t i = 0; i <
ARRAY_SIZE(data); ++i) {
137 UpdatePcapStatsValue64(&data[i].last, data[i].current);
138 FAIL_IF_NOT(Lower32(data[i].last) == data[i].current);
145 static int UpdatePcapStatsValue64Overflow02(
void)
151 TestData data[] = {{.
last = (2ull << 32) + 1, .current = 0},
152 {.last = (3ull << 32) + 12345, .current = 22},
153 {.last = (3ull << 32) + 12345, .current = 12344},
154 {.last = UINT64_MAX, .current = UINT_MAX - 1}};
156 for (
size_t i = 0; i <
ARRAY_SIZE(data); ++i) {
157 uint32_t upper = Upper32(data[i].last);
158 FAIL_IF_NOT(Lower32(data[i].last) > data[i].current);
160 UpdatePcapStatsValue64(&data[i].last, data[i].current);
161 FAIL_IF_NOT(Lower32(data[i].last) == data[i].current);
168 static int UpdatePcapStatsValue64Overflow03(
void)
175 {.last = UINT64_MAX, .current = 3333}};
177 for (
size_t i = 0; i <
ARRAY_SIZE(data); ++i) {
178 FAIL_IF_NOT(Lower32(data[i].last) > data[i].current);
180 UpdatePcapStatsValue64(&data[i].last, data[i].current);
181 FAIL_IF_NOT(Lower32(data[i].last) == data[i].current);
188 static int UpdatePcapStats64Assorted01(
void)
197 struct pcap_stat current = {
198 .ps_recv = 12, .ps_drop = 2345, .ps_ifdrop = 9876};
205 UpdatePcapStats64(&last, ¤t);
214 static void SourcePcapRegisterStatsTests(
void)
217 UpdatePcapStatsValue64NoChange01);
219 UpdatePcapStatsValue64NoChange02);
221 UpdatePcapStatsValue64NoOverflow01);
223 UpdatePcapStatsValue64NoOverflow02);
225 UpdatePcapStatsValue64Overflow01);
227 UpdatePcapStatsValue64Overflow02);
229 UpdatePcapStatsValue64Overflow03);
231 UtRegisterTest(
"UpdatePcapStats64Assorted01", UpdatePcapStats64Assorted01);