1 /*
2  * ngtcp2
3  *
4  * Copyright (c) 2019 ngtcp2 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 "ngtcp2_pv_test.h"
26 
27 #include <CUnit/CUnit.h>
28 
29 #include "ngtcp2_pv.h"
30 #include "ngtcp2_test_helper.h"
31 
test_ngtcp2_pv_add_entry(void)32 void test_ngtcp2_pv_add_entry(void) {
33   ngtcp2_pv *pv;
34   int rv;
35   const ngtcp2_mem *mem = ngtcp2_mem_default();
36   ngtcp2_cid cid;
37   const uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xff};
38   ngtcp2_dcid dcid;
39   ngtcp2_log log;
40   uint8_t data[8];
41   size_t i;
42   ngtcp2_duration timeout = 100ULL * NGTCP2_SECONDS;
43 
44   dcid_init(&cid);
45   ngtcp2_dcid_init(&dcid, 1000000007, &cid, token);
46   ngtcp2_log_init(&log, NULL, NULL, 0, NULL);
47 
48   rv = ngtcp2_pv_new(&pv, &dcid, timeout, NGTCP2_PV_FLAG_NONE, &log, mem);
49 
50   CU_ASSERT(0 == rv);
51   CU_ASSERT(0 == ngtcp2_pv_validation_timed_out(pv, 0));
52 
53   ngtcp2_pv_handle_entry_expiry(pv, 0);
54 
55   CU_ASSERT(NGTCP2_PV_NUM_PROBE_PKT == pv->probe_pkt_left);
56   CU_ASSERT(ngtcp2_pv_should_send_probe(pv));
57 
58   for (i = 0; i < NGTCP2_PV_NUM_PROBE_PKT; ++i) {
59     ngtcp2_pv_add_entry(pv, data, 100, NGTCP2_PV_ENTRY_FLAG_NONE, 0);
60 
61     CU_ASSERT(i + 1 == ngtcp2_ringbuf_len(&pv->ents));
62   }
63 
64   CU_ASSERT(0 == pv->probe_pkt_left);
65   CU_ASSERT(!ngtcp2_pv_should_send_probe(pv));
66   CU_ASSERT(NGTCP2_PV_NUM_PROBE_PKT == ngtcp2_ringbuf_len(&pv->ents));
67   CU_ASSERT(100 == ngtcp2_pv_next_expiry(pv));
68 
69   ngtcp2_pv_handle_entry_expiry(pv, 99);
70 
71   CU_ASSERT(0 == pv->probe_pkt_left);
72   CU_ASSERT(!ngtcp2_pv_should_send_probe(pv));
73 
74   ngtcp2_pv_handle_entry_expiry(pv, 100);
75 
76   CU_ASSERT(2 == pv->probe_pkt_left);
77   CU_ASSERT(ngtcp2_pv_should_send_probe(pv));
78   CU_ASSERT(100 == ngtcp2_pv_next_expiry(pv));
79 
80   ngtcp2_pv_add_entry(pv, data, 111, NGTCP2_PV_ENTRY_FLAG_NONE, 100);
81 
82   CU_ASSERT(1 == pv->probe_pkt_left);
83   CU_ASSERT(ngtcp2_pv_should_send_probe(pv));
84   CU_ASSERT(111 == ngtcp2_pv_next_expiry(pv));
85 
86   ngtcp2_pv_del(pv);
87 }
88 
test_ngtcp2_pv_validate(void)89 void test_ngtcp2_pv_validate(void) {
90   ngtcp2_pv *pv;
91   int rv;
92   const ngtcp2_mem *mem = ngtcp2_mem_default();
93   ngtcp2_cid cid;
94   const uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xff};
95   ngtcp2_dcid dcid;
96   ngtcp2_log log;
97   uint8_t data[8];
98   ngtcp2_duration timeout = 100ULL * NGTCP2_SECONDS;
99   ngtcp2_path_storage path;
100   uint8_t flags;
101 
102   path_init(&path, 1, 0, 2, 0);
103   dcid_init(&cid);
104   ngtcp2_dcid_init(&dcid, 1000000007, &cid, token);
105   ngtcp2_path_copy(&dcid.ps.path, &path.path);
106   ngtcp2_log_init(&log, NULL, NULL, 0, NULL);
107 
108   rv = ngtcp2_pv_new(&pv, &dcid, timeout, NGTCP2_PV_FLAG_NONE, &log, mem);
109 
110   CU_ASSERT(0 == rv);
111 
112   memset(data, 0, sizeof(data));
113   ngtcp2_pv_add_entry(pv, data, 100, NGTCP2_PV_ENTRY_FLAG_NONE, 1);
114 
115   memset(data, 1, sizeof(data));
116   ngtcp2_pv_add_entry(pv, data, 100, NGTCP2_PV_ENTRY_FLAG_NONE, 1);
117 
118   memset(data, 1, sizeof(data));
119   rv = ngtcp2_pv_validate(pv, &flags, data);
120 
121   CU_ASSERT(0 == rv);
122 
123   memset(data, 3, sizeof(data));
124   rv = ngtcp2_pv_validate(pv, &flags, data);
125 
126   CU_ASSERT(NGTCP2_ERR_INVALID_ARGUMENT == rv);
127 
128   ngtcp2_pv_del(pv);
129 }
130