• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2023 nghttp2 contributors
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25 #include "nghttp2_ratelim_test.h"
26 
27 #include <stdio.h>
28 
29 #include "munit.h"
30 
31 #include "nghttp2_ratelim.h"
32 
33 static const MunitTest tests[] = {
34   munit_void_test(test_nghttp2_ratelim_update),
35   munit_void_test(test_nghttp2_ratelim_drain),
36   munit_test_end(),
37 };
38 
39 const MunitSuite ratelim_suite = {
40   "/ratelim", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE,
41 };
42 
test_nghttp2_ratelim_update(void)43 void test_nghttp2_ratelim_update(void) {
44   nghttp2_ratelim rl;
45 
46   nghttp2_ratelim_init(&rl, 1000, 21);
47 
48   assert_uint64(1000, ==, rl.val);
49   assert_uint64(1000, ==, rl.burst);
50   assert_uint64(21, ==, rl.rate);
51   assert_uint64(0, ==, rl.tstamp);
52 
53   nghttp2_ratelim_update(&rl, 999);
54 
55   assert_uint64(1000, ==, rl.val);
56   assert_uint64(999, ==, rl.tstamp);
57 
58   nghttp2_ratelim_drain(&rl, 100);
59 
60   assert_uint64(900, ==, rl.val);
61 
62   nghttp2_ratelim_update(&rl, 1000);
63 
64   assert_uint64(921, ==, rl.val);
65 
66   nghttp2_ratelim_update(&rl, 1002);
67 
68   assert_uint64(963, ==, rl.val);
69 
70   nghttp2_ratelim_update(&rl, 1004);
71 
72   assert_uint64(1000, ==, rl.val);
73   assert_uint64(1004, ==, rl.tstamp);
74 
75   /* timer skew */
76   nghttp2_ratelim_init(&rl, 1000, 21);
77   nghttp2_ratelim_update(&rl, 1);
78 
79   assert_uint64(1000, ==, rl.val);
80 
81   nghttp2_ratelim_update(&rl, 0);
82 
83   assert_uint64(1000, ==, rl.val);
84 
85   /* rate * duration overflow */
86   nghttp2_ratelim_init(&rl, 1000, 100);
87   nghttp2_ratelim_drain(&rl, 999);
88 
89   assert_uint64(1, ==, rl.val);
90 
91   nghttp2_ratelim_update(&rl, UINT64_MAX);
92 
93   assert_uint64(1000, ==, rl.val);
94 
95   /* val + rate * duration overflow */
96   nghttp2_ratelim_init(&rl, UINT64_MAX - 1, 2);
97   nghttp2_ratelim_update(&rl, 1);
98 
99   assert_uint64(UINT64_MAX - 1, ==, rl.val);
100 }
101 
test_nghttp2_ratelim_drain(void)102 void test_nghttp2_ratelim_drain(void) {
103   nghttp2_ratelim rl;
104 
105   nghttp2_ratelim_init(&rl, 100, 7);
106 
107   assert_int(-1, ==, nghttp2_ratelim_drain(&rl, 101));
108   assert_int(0, ==, nghttp2_ratelim_drain(&rl, 51));
109   assert_int(0, ==, nghttp2_ratelim_drain(&rl, 49));
110   assert_int(-1, ==, nghttp2_ratelim_drain(&rl, 1));
111 }
112