github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/internal/replay/replay_test.go (about) 1 // SPDX-License-Identifier: MPL-2.0 2 /* 3 * Copyright (C) 2024 The Noisy Sockets Authors. 4 * 5 * This Source Code Form is subject to the terms of the Mozilla Public 6 * License, v. 2.0. If a copy of the MPL was not distributed with this 7 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 * 9 * Portions of this file are based on code originally from wireguard-go, 10 * 11 * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved. 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a copy of 14 * this software and associated documentation files (the "Software"), to deal in 15 * the Software without restriction, including without limitation the rights to 16 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 17 * of the Software, and to permit persons to whom the Software is furnished to do 18 * so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in all 21 * copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 29 * SOFTWARE. 30 */ 31 32 package replay 33 34 import ( 35 "testing" 36 ) 37 38 /* Ported from the linux kernel implementation 39 * 40 * 41 */ 42 43 const RejectAfterMessages = 1<<64 - 1<<13 - 1 44 45 func TestReplay(t *testing.T) { 46 var filter Filter 47 48 const T_LIM = windowSize + 1 49 50 testNumber := 0 51 T := func(n uint64, expected bool) { 52 testNumber++ 53 if filter.ValidateCounter(n, RejectAfterMessages) != expected { 54 t.Fatal("Test", testNumber, "failed", n, expected) 55 } 56 } 57 58 filter.Reset() 59 60 T(0, true) /* 1 */ 61 T(1, true) /* 2 */ 62 T(1, false) /* 3 */ 63 T(9, true) /* 4 */ 64 T(8, true) /* 5 */ 65 T(7, true) /* 6 */ 66 T(7, false) /* 7 */ 67 T(T_LIM, true) /* 8 */ 68 T(T_LIM-1, true) /* 9 */ 69 T(T_LIM-1, false) /* 10 */ 70 T(T_LIM-2, true) /* 11 */ 71 T(2, true) /* 12 */ 72 T(2, false) /* 13 */ 73 T(T_LIM+16, true) /* 14 */ 74 T(3, false) /* 15 */ 75 T(T_LIM+16, false) /* 16 */ 76 T(T_LIM*4, true) /* 17 */ 77 T(T_LIM*4-(T_LIM-1), true) /* 18 */ 78 T(10, false) /* 19 */ 79 T(T_LIM*4-T_LIM, false) /* 20 */ 80 T(T_LIM*4-(T_LIM+1), false) /* 21 */ 81 T(T_LIM*4-(T_LIM-2), true) /* 22 */ 82 T(T_LIM*4+1-T_LIM, false) /* 23 */ 83 T(0, false) /* 24 */ 84 T(RejectAfterMessages, false) /* 25 */ 85 T(RejectAfterMessages-1, true) /* 26 */ 86 T(RejectAfterMessages, false) /* 27 */ 87 T(RejectAfterMessages-1, false) /* 28 */ 88 T(RejectAfterMessages-2, true) /* 29 */ 89 T(RejectAfterMessages+1, false) /* 30 */ 90 T(RejectAfterMessages+2, false) /* 31 */ 91 T(RejectAfterMessages-2, false) /* 32 */ 92 T(RejectAfterMessages-3, true) /* 33 */ 93 T(0, false) /* 34 */ 94 95 t.Log("Bulk test 1") 96 filter.Reset() 97 testNumber = 0 98 for i := uint64(1); i <= windowSize; i++ { 99 T(i, true) 100 } 101 T(0, true) 102 T(0, false) 103 104 t.Log("Bulk test 2") 105 filter.Reset() 106 testNumber = 0 107 for i := uint64(2); i <= windowSize+1; i++ { 108 T(i, true) 109 } 110 T(1, true) 111 T(0, false) 112 113 t.Log("Bulk test 3") 114 filter.Reset() 115 testNumber = 0 116 for i := uint64(windowSize + 1); i > 0; i-- { 117 T(i, true) 118 } 119 120 t.Log("Bulk test 4") 121 filter.Reset() 122 testNumber = 0 123 for i := uint64(windowSize + 2); i > 1; i-- { 124 T(i, true) 125 } 126 T(0, false) 127 128 t.Log("Bulk test 5") 129 filter.Reset() 130 testNumber = 0 131 for i := uint64(windowSize); i > 0; i-- { 132 T(i, true) 133 } 134 T(windowSize+1, true) 135 T(0, false) 136 137 t.Log("Bulk test 6") 138 filter.Reset() 139 testNumber = 0 140 for i := uint64(windowSize); i > 0; i-- { 141 T(i, true) 142 } 143 T(0, true) 144 T(windowSize+1, true) 145 }