gitee.com/aurawing/surguard-go@v0.3.1-0.20240409071558-96509a61ecf3/replay/replay_test.go (about)

     1  /* SPDX-License-Identifier: MIT
     2   *
     3   * Copyright (C) 2017-2023 WireGuard LLC. All Rights Reserved.
     4   */
     5  
     6  package replay
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  /* Ported from the linux kernel implementation
    13   *
    14   *
    15   */
    16  
    17  const RejectAfterMessages = 1<<64 - 1<<13 - 1
    18  
    19  func TestReplay(t *testing.T) {
    20  	var filter Filter
    21  
    22  	const T_LIM = windowSize + 1
    23  
    24  	testNumber := 0
    25  	T := func(n uint64, expected bool) {
    26  		testNumber++
    27  		if filter.ValidateCounter(n, RejectAfterMessages) != expected {
    28  			t.Fatal("Test", testNumber, "failed", n, expected)
    29  		}
    30  	}
    31  
    32  	filter.Reset()
    33  
    34  	T(0, true)                      /*  1 */
    35  	T(1, true)                      /*  2 */
    36  	T(1, false)                     /*  3 */
    37  	T(9, true)                      /*  4 */
    38  	T(8, true)                      /*  5 */
    39  	T(7, true)                      /*  6 */
    40  	T(7, false)                     /*  7 */
    41  	T(T_LIM, true)                  /*  8 */
    42  	T(T_LIM-1, true)                /*  9 */
    43  	T(T_LIM-1, false)               /* 10 */
    44  	T(T_LIM-2, true)                /* 11 */
    45  	T(2, true)                      /* 12 */
    46  	T(2, false)                     /* 13 */
    47  	T(T_LIM+16, true)               /* 14 */
    48  	T(3, false)                     /* 15 */
    49  	T(T_LIM+16, false)              /* 16 */
    50  	T(T_LIM*4, true)                /* 17 */
    51  	T(T_LIM*4-(T_LIM-1), true)      /* 18 */
    52  	T(10, false)                    /* 19 */
    53  	T(T_LIM*4-T_LIM, false)         /* 20 */
    54  	T(T_LIM*4-(T_LIM+1), false)     /* 21 */
    55  	T(T_LIM*4-(T_LIM-2), true)      /* 22 */
    56  	T(T_LIM*4+1-T_LIM, false)       /* 23 */
    57  	T(0, false)                     /* 24 */
    58  	T(RejectAfterMessages, false)   /* 25 */
    59  	T(RejectAfterMessages-1, true)  /* 26 */
    60  	T(RejectAfterMessages, false)   /* 27 */
    61  	T(RejectAfterMessages-1, false) /* 28 */
    62  	T(RejectAfterMessages-2, true)  /* 29 */
    63  	T(RejectAfterMessages+1, false) /* 30 */
    64  	T(RejectAfterMessages+2, false) /* 31 */
    65  	T(RejectAfterMessages-2, false) /* 32 */
    66  	T(RejectAfterMessages-3, true)  /* 33 */
    67  	T(0, false)                     /* 34 */
    68  
    69  	t.Log("Bulk test 1")
    70  	filter.Reset()
    71  	testNumber = 0
    72  	for i := uint64(1); i <= windowSize; i++ {
    73  		T(i, true)
    74  	}
    75  	T(0, true)
    76  	T(0, false)
    77  
    78  	t.Log("Bulk test 2")
    79  	filter.Reset()
    80  	testNumber = 0
    81  	for i := uint64(2); i <= windowSize+1; i++ {
    82  		T(i, true)
    83  	}
    84  	T(1, true)
    85  	T(0, false)
    86  
    87  	t.Log("Bulk test 3")
    88  	filter.Reset()
    89  	testNumber = 0
    90  	for i := uint64(windowSize + 1); i > 0; i-- {
    91  		T(i, true)
    92  	}
    93  
    94  	t.Log("Bulk test 4")
    95  	filter.Reset()
    96  	testNumber = 0
    97  	for i := uint64(windowSize + 2); i > 1; i-- {
    98  		T(i, true)
    99  	}
   100  	T(0, false)
   101  
   102  	t.Log("Bulk test 5")
   103  	filter.Reset()
   104  	testNumber = 0
   105  	for i := uint64(windowSize); i > 0; i-- {
   106  		T(i, true)
   107  	}
   108  	T(windowSize+1, true)
   109  	T(0, false)
   110  
   111  	t.Log("Bulk test 6")
   112  	filter.Reset()
   113  	testNumber = 0
   114  	for i := uint64(windowSize); i > 0; i-- {
   115  		T(i, true)
   116  	}
   117  	T(0, true)
   118  	T(windowSize+1, true)
   119  }