File: test_consecutive_ack_record_container.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (304 lines) | stat: -rw-r--r-- 11,960 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (c) 2002-2019 Balabit
 * Copyright (c) 2019 Laszlo Budai
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */

#include <criterion/criterion.h>

#include "ack-tracker/consecutive_ack_record_container.h"

typedef struct _TestBookmark
{
  gsize idx;
} TestBookmark;

static TestBookmark *
_ack_record_extract_bookmark(ConsecutiveAckRecord *self)
{
  return (TestBookmark *)&(self->super.bookmark.container);
}

static void
_ack_record_write_idx_to_bookmark(ConsecutiveAckRecord *self, gsize idx)
{
  TestBookmark *bookmark = _ack_record_extract_bookmark(self);
  bookmark->idx = idx;
}

static void
_fill_container(ConsecutiveAckRecordContainer *container, gsize n, gsize idx_shift)
{
  for (gsize i = 0; i < n; i++)
    {
      ConsecutiveAckRecord *rec = consecutive_ack_record_container_request_pending(container);
      rec->acked = FALSE;
      _ack_record_write_idx_to_bookmark(rec, i + idx_shift);
      consecutive_ack_record_container_store_pending(container);
    }
}

static void
_set_ack_range(ConsecutiveAckRecordContainer *ack_records, gsize start, gsize n)
{
  for (gsize i = start; i < start + n; i++)
    {
      ConsecutiveAckRecord *rec = consecutive_ack_record_container_at(ack_records, i);
      rec->acked = TRUE;
    }
}

static void
_assert_ack_range(ConsecutiveAckRecordContainer *ack_records, gsize start, gsize n, gboolean expected_acked)
{
  for (gsize i = start; i < start + n; i++)
    {
      ConsecutiveAckRecord *rec = consecutive_ack_record_container_at(ack_records, i);
      cr_assert_eq(rec->acked, expected_acked);
    }
}

static void
_assert_indexes(ConsecutiveAckRecordContainer *ack_records, gsize n, gsize idx_shift)
{
  for (gsize i = 0; i < n; i++)
    {
      ConsecutiveAckRecord *rec = consecutive_ack_record_container_at(ack_records, i);
      TestBookmark *bookmark = _ack_record_extract_bookmark(rec);
      cr_assert_eq(bookmark->idx, i + idx_shift);
    }
}

Test(consecutive_ack_record_container_static, is_empty)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 0);

    cr_expect(consecutive_ack_record_container_is_empty(ack_records));
    _fill_container(ack_records, capacity, 0);
    cr_expect_not(consecutive_ack_record_container_is_empty(ack_records));
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 32);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_static, request_store_pending)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    for (gsize i = 0; i < capacity; i++)
      {
        ConsecutiveAckRecord *rec = consecutive_ack_record_container_request_pending(ack_records);
        _ack_record_write_idx_to_bookmark(rec, i);
        consecutive_ack_record_container_store_pending(ack_records);
        rec = consecutive_ack_record_container_at(ack_records, i);
        TestBookmark *bookmark = _ack_record_extract_bookmark(rec);
        cr_expect_eq(bookmark->idx, i);
      }
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_static, drop_more_than_current_size, .signal = SIGABRT)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    _fill_container(ack_records, capacity, 0);
    consecutive_ack_record_container_drop(ack_records, 16);
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 16);
    // when trying to drop more than current size, should assert (->SIGABRT)
    consecutive_ack_record_container_drop(ack_records, 20);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_static, drop_from_the_beginning)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    _fill_container(ack_records, capacity, 0);
    consecutive_ack_record_container_drop(ack_records, 16);
    _assert_indexes(ack_records, 16, 16);
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 16);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_static, request_pending_should_return_null_when_capacity_reached)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    _fill_container(ack_records, capacity, 0);
    ConsecutiveAckRecord *rec = consecutive_ack_record_container_request_pending(ack_records);
    cr_expect_eq(rec, NULL);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_static, store_pending_should_not_modify_container_when_capacity_reached)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    _fill_container(ack_records, capacity, 0);
    consecutive_ack_record_container_store_pending(ack_records);
    _assert_indexes(ack_records, 32, 0);
    ConsecutiveAckRecord *rec = consecutive_ack_record_container_request_pending(ack_records);
    cr_expect_eq(rec, NULL);
    consecutive_ack_record_container_store_pending(ack_records);
    _assert_indexes(ack_records, 32, 0);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_static, after_drop_from_a_full_container_can_store_new_element)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    _fill_container(ack_records, capacity, 0);
    consecutive_ack_record_container_drop(ack_records, 1);
    ConsecutiveAckRecord *rec = consecutive_ack_record_container_at(ack_records, 0);
    TestBookmark *bookmark = _ack_record_extract_bookmark(rec);
    cr_expect_eq(bookmark->idx, 1);
    _fill_container(ack_records, 1, 32); //append new element, idx = 32
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), capacity);
    // all values shifted by 1
    _assert_indexes(ack_records, capacity, 1);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_static, get_continual_range_length)
{
  static const gsize capacity = 32;
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_static_new(capacity);
  {
    _fill_container(ack_records, capacity, 0);
    _set_ack_range(ack_records, 1, 15);
    _assert_ack_range(ack_records, 0, 1, FALSE);
    _assert_ack_range(ack_records, 1, 15, TRUE);
    _assert_ack_range(ack_records, 16, 16, FALSE);
    cr_expect_eq(consecutive_ack_record_container_get_continual_range_length(ack_records), 0);
    consecutive_ack_record_container_drop(ack_records, 1); // drop unacked...
    cr_expect_eq(consecutive_ack_record_container_get_continual_range_length(ack_records), 15);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_dynamic, is_empty)
{
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_dynamic_new();
  {
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 0);

    cr_expect(consecutive_ack_record_container_is_empty(ack_records));
    _fill_container(ack_records, 32, 0);
    cr_expect_not(consecutive_ack_record_container_is_empty(ack_records));
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 32);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_dynamic, request_store_pending)
{
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_dynamic_new();
  {
    for (gsize i = 0; i < 32; i++)
      {
        ConsecutiveAckRecord *rec = consecutive_ack_record_container_request_pending(ack_records);
        _ack_record_write_idx_to_bookmark(rec, i);
        consecutive_ack_record_container_store_pending(ack_records);
        rec = consecutive_ack_record_container_at(ack_records, i);
        TestBookmark *bookmark = _ack_record_extract_bookmark(rec);
        cr_expect_eq(bookmark->idx, i);
      }
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_dynamic, drop_more_than_current_size, .signal = SIGABRT)
{
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_dynamic_new();
  {
    _fill_container(ack_records, 32, 0);
    consecutive_ack_record_container_drop(ack_records, 16);
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 16);
    // when trying to drop more than current size, remove all the elements
    consecutive_ack_record_container_drop(ack_records, 20);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_dynamic, drop_from_the_beginning)
{
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_dynamic_new();
  {
    _fill_container(ack_records, 32, 0);
    consecutive_ack_record_container_drop(ack_records, 16);
    _assert_indexes(ack_records, 16, 16);
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 16);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_dynamic, get_continual_range_length)
{
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_dynamic_new();
  {
    _fill_container(ack_records, 32, 0);
    _set_ack_range(ack_records, 1, 15);
    _assert_ack_range(ack_records, 0, 1, FALSE);
    _assert_ack_range(ack_records, 1, 15, TRUE);
    _assert_ack_range(ack_records, 16, 16, FALSE);
    cr_expect_eq(consecutive_ack_record_container_get_continual_range_length(ack_records), 0);
    consecutive_ack_record_container_drop(ack_records, 1); // drop unacked...
    cr_expect_eq(consecutive_ack_record_container_get_continual_range_length(ack_records), 15);
  }
  consecutive_ack_record_container_free(ack_records);
}

Test(consecutive_ack_record_container_dynamic, store_pending_after_drop_all)
{
  ConsecutiveAckRecordContainer *ack_records = consecutive_ack_record_container_dynamic_new();
  {
    _fill_container(ack_records, 32, 0);
    ConsecutiveAckRecord *pending = consecutive_ack_record_container_request_pending(ack_records);
    cr_assert_not_null(pending);
    TestBookmark *bookmark = _ack_record_extract_bookmark(pending);
    bookmark->idx = 111;
    consecutive_ack_record_container_drop(ack_records, 32);
    cr_expect(consecutive_ack_record_container_is_empty(ack_records));
    consecutive_ack_record_container_store_pending(ack_records);
    cr_expect_eq(consecutive_ack_record_container_size(ack_records), 1);
    ConsecutiveAckRecord *rec = consecutive_ack_record_container_at(ack_records, 0);
    bookmark = _ack_record_extract_bookmark(rec);
    cr_expect_eq(bookmark->idx, 111);
  }
  consecutive_ack_record_container_free(ack_records);
}