summaryrefslogtreecommitdiffhomepage
path: root/replay_test.go
blob: 8b2e57d25f892378372279f36eeb1f3bb6288587 (plain)
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
/* SPDX-License-Identifier: GPL-2.0
 *
 * Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 * Copyright (C) 2017-2018 Mathias N. Hall-Andersen <mathias@hall-andersen.dk>.
 */

package main

import (
	"testing"
)

/* Ported from the linux kernel implementation
 *
 *
 */

func TestReplay(t *testing.T) {
	var filter ReplayFilter

	T_LIM := CounterWindowSize + 1

	testNumber := 0
	T := func(n uint64, v bool) {
		testNumber++
		if filter.ValidateCounter(n) != v {
			t.Fatal("Test", testNumber, "failed", n, v)
		}
	}

	filter.Init()

	T(0, true)                      /*  1 */
	T(1, true)                      /*  2 */
	T(1, false)                     /*  3 */
	T(9, true)                      /*  4 */
	T(8, true)                      /*  5 */
	T(7, true)                      /*  6 */
	T(7, false)                     /*  7 */
	T(T_LIM, true)                  /*  8 */
	T(T_LIM-1, true)                /*  9 */
	T(T_LIM-1, false)               /* 10 */
	T(T_LIM-2, true)                /* 11 */
	T(2, true)                      /* 12 */
	T(2, false)                     /* 13 */
	T(T_LIM+16, true)               /* 14 */
	T(3, false)                     /* 15 */
	T(T_LIM+16, false)              /* 16 */
	T(T_LIM*4, true)                /* 17 */
	T(T_LIM*4-(T_LIM-1), true)      /* 18 */
	T(10, false)                    /* 19 */
	T(T_LIM*4-T_LIM, false)         /* 20 */
	T(T_LIM*4-(T_LIM+1), false)     /* 21 */
	T(T_LIM*4-(T_LIM-2), true)      /* 22 */
	T(T_LIM*4+1-T_LIM, false)       /* 23 */
	T(0, false)                     /* 24 */
	T(RejectAfterMessages, false)   /* 25 */
	T(RejectAfterMessages-1, true)  /* 26 */
	T(RejectAfterMessages, false)   /* 27 */
	T(RejectAfterMessages-1, false) /* 28 */
	T(RejectAfterMessages-2, true)  /* 29 */
	T(RejectAfterMessages+1, false) /* 30 */
	T(RejectAfterMessages+2, false) /* 31 */
	T(RejectAfterMessages-2, false) /* 32 */
	T(RejectAfterMessages-3, true)  /* 33 */
	T(0, false)                     /* 34 */

	t.Log("Bulk test 1")
	filter.Init()
	testNumber = 0
	for i := uint64(1); i <= CounterWindowSize; i++ {
		T(i, true)
	}
	T(0, true)
	T(0, false)

	t.Log("Bulk test 2")
	filter.Init()
	testNumber = 0
	for i := uint64(2); i <= CounterWindowSize+1; i++ {
		T(i, true)
	}
	T(1, true)
	T(0, false)

	t.Log("Bulk test 3")
	filter.Init()
	testNumber = 0
	for i := CounterWindowSize + 1; i > 0; i-- {
		T(i, true)
	}

	t.Log("Bulk test 4")
	filter.Init()
	testNumber = 0
	for i := CounterWindowSize + 2; i > 1; i-- {
		T(i, true)
	}
	T(0, false)

	t.Log("Bulk test 5")
	filter.Init()
	testNumber = 0
	for i := CounterWindowSize; i > 0; i-- {
		T(i, true)
	}
	T(CounterWindowSize+1, true)
	T(0, false)

	t.Log("Bulk test 6")
	filter.Init()
	testNumber = 0
	for i := CounterWindowSize; i > 0; i-- {
		T(i, true)
	}
	T(0, true)
	T(CounterWindowSize+1, true)
}