1 /*
2  * ngtcp2
3  *
4  * Copyright (c) 2017 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_conn_test.h"
26 
27 #include <assert.h>
28 
29 #include <CUnit/CUnit.h>
30 
31 #include "ngtcp2_conn.h"
32 #include "ngtcp2_test_helper.h"
33 #include "ngtcp2_mem.h"
34 #include "ngtcp2_pkt.h"
35 #include "ngtcp2_cid.h"
36 #include "ngtcp2_conv.h"
37 #include "ngtcp2_vec.h"
38 #include "ngtcp2_rcvry.h"
39 #include "ngtcp2_addr.h"
40 
null_encrypt(uint8_t * dest,const ngtcp2_crypto_aead * aead,const ngtcp2_crypto_aead_ctx * aead_ctx,const uint8_t * plaintext,size_t plaintextlen,const uint8_t * nonce,size_t noncelen,const uint8_t * aad,size_t aadlen)41 static int null_encrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
42                         const ngtcp2_crypto_aead_ctx *aead_ctx,
43                         const uint8_t *plaintext, size_t plaintextlen,
44                         const uint8_t *nonce, size_t noncelen,
45                         const uint8_t *aad, size_t aadlen) {
46   (void)dest;
47   (void)aead;
48   (void)aead_ctx;
49   (void)plaintext;
50   (void)plaintextlen;
51   (void)nonce;
52   (void)noncelen;
53   (void)aad;
54   (void)aadlen;
55 
56   if (plaintextlen && plaintext != dest) {
57     memcpy(dest, plaintext, plaintextlen);
58   }
59   memset(dest + plaintextlen, 0, NGTCP2_FAKE_AEAD_OVERHEAD);
60 
61   return 0;
62 }
63 
null_decrypt(uint8_t * dest,const ngtcp2_crypto_aead * aead,const ngtcp2_crypto_aead_ctx * aead_ctx,const uint8_t * ciphertext,size_t ciphertextlen,const uint8_t * nonce,size_t noncelen,const uint8_t * aad,size_t aadlen)64 static int null_decrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
65                         const ngtcp2_crypto_aead_ctx *aead_ctx,
66                         const uint8_t *ciphertext, size_t ciphertextlen,
67                         const uint8_t *nonce, size_t noncelen,
68                         const uint8_t *aad, size_t aadlen) {
69   (void)dest;
70   (void)aead;
71   (void)aead_ctx;
72   (void)ciphertext;
73   (void)nonce;
74   (void)noncelen;
75   (void)aad;
76   (void)aadlen;
77   assert(ciphertextlen >= NGTCP2_FAKE_AEAD_OVERHEAD);
78   memmove(dest, ciphertext, ciphertextlen - NGTCP2_FAKE_AEAD_OVERHEAD);
79   return 0;
80 }
81 
fail_decrypt(uint8_t * dest,const ngtcp2_crypto_aead * aead,const ngtcp2_crypto_aead_ctx * aead_ctx,const uint8_t * ciphertext,size_t ciphertextlen,const uint8_t * nonce,size_t noncelen,const uint8_t * aad,size_t aadlen)82 static int fail_decrypt(uint8_t *dest, const ngtcp2_crypto_aead *aead,
83                         const ngtcp2_crypto_aead_ctx *aead_ctx,
84                         const uint8_t *ciphertext, size_t ciphertextlen,
85                         const uint8_t *nonce, size_t noncelen,
86                         const uint8_t *aad, size_t aadlen) {
87   (void)dest;
88   (void)aead;
89   (void)aead_ctx;
90   (void)ciphertext;
91   (void)ciphertextlen;
92   (void)nonce;
93   (void)noncelen;
94   (void)aad;
95   (void)aadlen;
96   return NGTCP2_ERR_DECRYPT;
97 }
98 
null_hp_mask(uint8_t * dest,const ngtcp2_crypto_cipher * hp,const ngtcp2_crypto_cipher_ctx * hp_ctx,const uint8_t * sample)99 static int null_hp_mask(uint8_t *dest, const ngtcp2_crypto_cipher *hp,
100                         const ngtcp2_crypto_cipher_ctx *hp_ctx,
101                         const uint8_t *sample) {
102   (void)hp;
103   (void)hp_ctx;
104   (void)sample;
105   memcpy(dest, NGTCP2_FAKE_HP_MASK, sizeof(NGTCP2_FAKE_HP_MASK) - 1);
106   return 0;
107 }
108 
get_new_connection_id(ngtcp2_conn * conn,ngtcp2_cid * cid,uint8_t * token,size_t cidlen,void * user_data)109 static int get_new_connection_id(ngtcp2_conn *conn, ngtcp2_cid *cid,
110                                  uint8_t *token, size_t cidlen,
111                                  void *user_data) {
112   (void)user_data;
113   memset(cid->data, 0, cidlen);
114   cid->data[0] = (uint8_t)(conn->scid.last_seq + 1);
115   cid->datalen = cidlen;
116   memset(token, 0, NGTCP2_STATELESS_RESET_TOKENLEN);
117   return 0;
118 }
119 
120 static uint8_t null_secret[32];
121 static uint8_t null_iv[16];
122 static uint8_t null_data[4096];
123 
124 static ngtcp2_crypto_km null_ckm = {
125     {NULL, 0}, {0}, {null_iv, sizeof(null_iv)},
126     -1,        0,   NGTCP2_CRYPTO_KM_FLAG_NONE,
127 };
128 
129 static ngtcp2_path_storage null_path;
130 static ngtcp2_path_storage new_path;
131 
132 static ngtcp2_pkt_info null_pi;
133 
init_static_path(void)134 void init_static_path(void) {
135   path_init(&null_path, 0, 0, 0, 0);
136   path_init(&new_path, 1, 0, 2, 0);
137 }
138 
null_datav(ngtcp2_vec * datav,size_t len)139 static ngtcp2_vec *null_datav(ngtcp2_vec *datav, size_t len) {
140   datav->base = null_data;
141   datav->len = len;
142   return datav;
143 }
144 
init_crypto_ctx(ngtcp2_crypto_ctx * ctx)145 static void init_crypto_ctx(ngtcp2_crypto_ctx *ctx) {
146   memset(ctx, 0, sizeof(*ctx));
147   ctx->aead.max_overhead = NGTCP2_FAKE_AEAD_OVERHEAD;
148   ctx->max_encryption = 9999;
149   ctx->max_decryption_failure = 8888;
150 }
151 
init_initial_crypto_ctx(ngtcp2_crypto_ctx * ctx)152 static void init_initial_crypto_ctx(ngtcp2_crypto_ctx *ctx) {
153   memset(ctx, 0, sizeof(*ctx));
154   ctx->aead.max_overhead = NGTCP2_INITIAL_AEAD_OVERHEAD;
155   ctx->max_encryption = 9999;
156   ctx->max_decryption_failure = 8888;
157 }
158 
159 typedef struct {
160   uint64_t pkt_num;
161   /* stream_data is intended to store the arguments passed in
162      recv_stream_data callback. */
163   struct {
164     int64_t stream_id;
165     uint32_t flags;
166     size_t datalen;
167   } stream_data;
168   struct {
169     uint32_t flags;
170     const uint8_t *data;
171     size_t datalen;
172     uint64_t dgram_id;
173   } datagram;
174   struct {
175     uint32_t flags;
176     int64_t stream_id;
177     uint64_t app_error_code;
178   } stream_close;
179 } my_user_data;
180 
client_initial(ngtcp2_conn * conn,void * user_data)181 static int client_initial(ngtcp2_conn *conn, void *user_data) {
182   (void)user_data;
183 
184   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_INITIAL, null_data,
185                                  217);
186 
187   return 0;
188 }
189 
client_initial_early_data(ngtcp2_conn * conn,void * user_data)190 static int client_initial_early_data(ngtcp2_conn *conn, void *user_data) {
191   ngtcp2_crypto_aead_ctx aead_ctx = {0};
192   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
193   ngtcp2_crypto_ctx crypto_ctx;
194 
195   (void)user_data;
196 
197   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_INITIAL, null_data,
198                                  217);
199 
200   init_crypto_ctx(&crypto_ctx);
201 
202   ngtcp2_conn_set_early_crypto_ctx(conn, &crypto_ctx);
203   ngtcp2_conn_install_early_key(conn, &aead_ctx, null_iv, sizeof(null_iv),
204                                 &hp_ctx);
205 
206   return 0;
207 }
208 
client_initial_large_crypto_early_data(ngtcp2_conn * conn,void * user_data)209 static int client_initial_large_crypto_early_data(ngtcp2_conn *conn,
210                                                   void *user_data) {
211   ngtcp2_crypto_aead_ctx aead_ctx = {0};
212   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
213   ngtcp2_crypto_ctx crypto_ctx;
214 
215   (void)user_data;
216 
217   /* Initial CRYPTO data which is larger than a typical single
218      datagram. */
219   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_INITIAL, null_data,
220                                  1500);
221 
222   init_crypto_ctx(&crypto_ctx);
223 
224   ngtcp2_conn_set_early_crypto_ctx(conn, &crypto_ctx);
225   ngtcp2_conn_install_early_key(conn, &aead_ctx, null_iv, sizeof(null_iv),
226                                 &hp_ctx);
227 
228   return 0;
229 }
230 
recv_client_initial(ngtcp2_conn * conn,const ngtcp2_cid * dcid,void * user_data)231 static int recv_client_initial(ngtcp2_conn *conn, const ngtcp2_cid *dcid,
232                                void *user_data) {
233   ngtcp2_crypto_aead_ctx aead_ctx = {0};
234   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
235   ngtcp2_crypto_ctx ctx;
236 
237   (void)dcid;
238   (void)user_data;
239 
240   init_initial_crypto_ctx(&ctx);
241 
242   ngtcp2_conn_set_initial_crypto_ctx(conn, &ctx);
243   ngtcp2_conn_install_initial_key(conn, &aead_ctx, null_iv, &hp_ctx, &aead_ctx,
244                                   null_iv, &hp_ctx, sizeof(null_iv));
245 
246   init_crypto_ctx(&ctx);
247 
248   ngtcp2_conn_set_crypto_ctx(conn, &ctx);
249   ngtcp2_conn_install_rx_handshake_key(conn, &aead_ctx, null_iv,
250                                        sizeof(null_iv), &hp_ctx);
251   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
252                                        sizeof(null_iv), &hp_ctx);
253 
254   return 0;
255 }
256 
recv_client_initial_early(ngtcp2_conn * conn,const ngtcp2_cid * dcid,void * user_data)257 static int recv_client_initial_early(ngtcp2_conn *conn, const ngtcp2_cid *dcid,
258                                      void *user_data) {
259   ngtcp2_crypto_aead_ctx aead_ctx = {0};
260   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
261   ngtcp2_crypto_ctx ctx;
262 
263   recv_client_initial(conn, dcid, user_data);
264 
265   init_crypto_ctx(&ctx);
266 
267   ngtcp2_conn_set_early_crypto_ctx(conn, &ctx);
268   ngtcp2_conn_install_early_key(conn, &aead_ctx, null_iv, sizeof(null_iv),
269                                 &hp_ctx);
270 
271   return 0;
272 }
273 
recv_crypto_data(ngtcp2_conn * conn,ngtcp2_crypto_level crypto_level,uint64_t offset,const uint8_t * data,size_t datalen,void * user_data)274 static int recv_crypto_data(ngtcp2_conn *conn, ngtcp2_crypto_level crypto_level,
275                             uint64_t offset, const uint8_t *data,
276                             size_t datalen, void *user_data) {
277   (void)conn;
278   (void)crypto_level;
279   (void)offset;
280   (void)data;
281   (void)datalen;
282   (void)user_data;
283   return 0;
284 }
285 
recv_crypto_data_server_early_data(ngtcp2_conn * conn,ngtcp2_crypto_level crypto_level,uint64_t offset,const uint8_t * data,size_t datalen,void * user_data)286 static int recv_crypto_data_server_early_data(ngtcp2_conn *conn,
287                                               ngtcp2_crypto_level crypto_level,
288                                               uint64_t offset,
289                                               const uint8_t *data,
290                                               size_t datalen, void *user_data) {
291   ngtcp2_crypto_aead_ctx aead_ctx = {0};
292   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
293 
294   (void)offset;
295   (void)crypto_level;
296   (void)data;
297   (void)datalen;
298   (void)user_data;
299 
300   assert(conn->server);
301 
302   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_INITIAL, null_data,
303                                  179);
304 
305   ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret), &aead_ctx,
306                              null_iv, sizeof(null_iv), &hp_ctx);
307 
308   conn->callbacks.recv_crypto_data = recv_crypto_data;
309 
310   return 0;
311 }
312 
update_key(ngtcp2_conn * conn,uint8_t * rx_secret,uint8_t * tx_secret,ngtcp2_crypto_aead_ctx * rx_aead_ctx,uint8_t * rx_iv,ngtcp2_crypto_aead_ctx * tx_aead_ctx,uint8_t * tx_iv,const uint8_t * current_rx_secret,const uint8_t * current_tx_secret,size_t secretlen,void * user_data)313 static int update_key(ngtcp2_conn *conn, uint8_t *rx_secret, uint8_t *tx_secret,
314                       ngtcp2_crypto_aead_ctx *rx_aead_ctx, uint8_t *rx_iv,
315                       ngtcp2_crypto_aead_ctx *tx_aead_ctx, uint8_t *tx_iv,
316                       const uint8_t *current_rx_secret,
317                       const uint8_t *current_tx_secret, size_t secretlen,
318                       void *user_data) {
319   (void)conn;
320   (void)current_rx_secret;
321   (void)current_tx_secret;
322   (void)user_data;
323   (void)secretlen;
324 
325   assert(sizeof(null_secret) == secretlen);
326 
327   memset(rx_secret, 0xff, sizeof(null_secret));
328   memset(tx_secret, 0xff, sizeof(null_secret));
329   rx_aead_ctx->native_handle = NULL;
330   memset(rx_iv, 0xff, sizeof(null_iv));
331   tx_aead_ctx->native_handle = NULL;
332   memset(tx_iv, 0xff, sizeof(null_iv));
333 
334   return 0;
335 }
336 
recv_crypto_handshake_error(ngtcp2_conn * conn,ngtcp2_crypto_level crypto_level,uint64_t offset,const uint8_t * data,size_t datalen,void * user_data)337 static int recv_crypto_handshake_error(ngtcp2_conn *conn,
338                                        ngtcp2_crypto_level crypto_level,
339                                        uint64_t offset, const uint8_t *data,
340                                        size_t datalen, void *user_data) {
341   (void)conn;
342   (void)crypto_level;
343   (void)offset;
344   (void)data;
345   (void)datalen;
346   (void)user_data;
347   return NGTCP2_ERR_CRYPTO;
348 }
349 
recv_crypto_fatal_alert_generated(ngtcp2_conn * conn,ngtcp2_crypto_level crypto_level,uint64_t offset,const uint8_t * data,size_t datalen,void * user_data)350 static int recv_crypto_fatal_alert_generated(ngtcp2_conn *conn,
351                                              ngtcp2_crypto_level crypto_level,
352                                              uint64_t offset,
353                                              const uint8_t *data,
354                                              size_t datalen, void *user_data) {
355   (void)conn;
356   (void)crypto_level;
357   (void)offset;
358   (void)data;
359   (void)datalen;
360   (void)user_data;
361   return NGTCP2_ERR_CRYPTO;
362 }
363 
recv_crypto_data_server(ngtcp2_conn * conn,ngtcp2_crypto_level crypto_level,uint64_t offset,const uint8_t * data,size_t datalen,void * user_data)364 static int recv_crypto_data_server(ngtcp2_conn *conn,
365                                    ngtcp2_crypto_level crypto_level,
366                                    uint64_t offset, const uint8_t *data,
367                                    size_t datalen, void *user_data) {
368   (void)offset;
369   (void)data;
370   (void)datalen;
371   (void)user_data;
372 
373   ngtcp2_conn_submit_crypto_data(conn,
374                                  crypto_level == NGTCP2_CRYPTO_LEVEL_INITIAL
375                                      ? NGTCP2_CRYPTO_LEVEL_INITIAL
376                                      : NGTCP2_CRYPTO_LEVEL_HANDSHAKE,
377                                  null_data, 218);
378 
379   return 0;
380 }
381 
recv_stream_data(ngtcp2_conn * conn,uint32_t flags,int64_t stream_id,uint64_t offset,const uint8_t * data,size_t datalen,void * user_data,void * stream_user_data)382 static int recv_stream_data(ngtcp2_conn *conn, uint32_t flags,
383                             int64_t stream_id, uint64_t offset,
384                             const uint8_t *data, size_t datalen,
385                             void *user_data, void *stream_user_data) {
386   my_user_data *ud = user_data;
387   (void)conn;
388   (void)offset;
389   (void)data;
390   (void)stream_user_data;
391 
392   if (ud) {
393     ud->stream_data.stream_id = stream_id;
394     ud->stream_data.flags = flags;
395     ud->stream_data.datalen = datalen;
396   }
397 
398   return 0;
399 }
400 
401 static int
recv_stream_data_shutdown_stream_read(ngtcp2_conn * conn,uint32_t flags,int64_t stream_id,uint64_t offset,const uint8_t * data,size_t datalen,void * user_data,void * stream_user_data)402 recv_stream_data_shutdown_stream_read(ngtcp2_conn *conn, uint32_t flags,
403                                       int64_t stream_id, uint64_t offset,
404                                       const uint8_t *data, size_t datalen,
405                                       void *user_data, void *stream_user_data) {
406   int rv;
407 
408   recv_stream_data(conn, flags, stream_id, offset, data, datalen, user_data,
409                    stream_user_data);
410 
411   rv = ngtcp2_conn_shutdown_stream_read(conn, stream_id, NGTCP2_APP_ERR01);
412   if (rv != 0) {
413     return NGTCP2_ERR_CALLBACK_FAILURE;
414   }
415 
416   return 0;
417 }
418 
stream_close(ngtcp2_conn * conn,uint32_t flags,int64_t stream_id,uint64_t app_error_code,void * user_data,void * stream_user_data)419 static int stream_close(ngtcp2_conn *conn, uint32_t flags, int64_t stream_id,
420                         uint64_t app_error_code, void *user_data,
421                         void *stream_user_data) {
422   my_user_data *ud = user_data;
423   (void)conn;
424   (void)stream_user_data;
425 
426   if (ud) {
427     ud->stream_close.flags = flags;
428     ud->stream_close.stream_id = stream_id;
429     ud->stream_close.app_error_code = app_error_code;
430   }
431 
432   return 0;
433 }
434 
recv_retry(ngtcp2_conn * conn,const ngtcp2_pkt_hd * hd,void * user_data)435 static int recv_retry(ngtcp2_conn *conn, const ngtcp2_pkt_hd *hd,
436                       void *user_data) {
437   (void)conn;
438   (void)hd;
439   (void)user_data;
440   return 0;
441 }
442 
genrand(uint8_t * dest,size_t destlen,const ngtcp2_rand_ctx * rand_ctx)443 static void genrand(uint8_t *dest, size_t destlen,
444                     const ngtcp2_rand_ctx *rand_ctx) {
445   (void)rand_ctx;
446 
447   memset(dest, 0, destlen);
448 }
449 
recv_datagram(ngtcp2_conn * conn,uint32_t flags,const uint8_t * data,size_t datalen,void * user_data)450 static int recv_datagram(ngtcp2_conn *conn, uint32_t flags, const uint8_t *data,
451                          size_t datalen, void *user_data) {
452   my_user_data *ud = user_data;
453   (void)conn;
454   (void)flags;
455 
456   if (ud) {
457     ud->datagram.flags = flags;
458     ud->datagram.data = data;
459     ud->datagram.datalen = datalen;
460   }
461 
462   return 0;
463 }
464 
ack_datagram(ngtcp2_conn * conn,uint64_t dgram_id,void * user_data)465 static int ack_datagram(ngtcp2_conn *conn, uint64_t dgram_id, void *user_data) {
466   my_user_data *ud = user_data;
467   (void)conn;
468 
469   if (ud) {
470     ud->datagram.dgram_id = dgram_id;
471   }
472 
473   return 0;
474 }
475 
get_path_challenge_data(ngtcp2_conn * conn,uint8_t * data,void * user_data)476 static int get_path_challenge_data(ngtcp2_conn *conn, uint8_t *data,
477                                    void *user_data) {
478   (void)conn;
479   (void)user_data;
480 
481   memset(data, 0, NGTCP2_PATH_CHALLENGE_DATALEN);
482 
483   return 0;
484 }
485 
delete_crypto_aead_ctx(ngtcp2_conn * conn,ngtcp2_crypto_aead_ctx * aead_ctx,void * user_data)486 static void delete_crypto_aead_ctx(ngtcp2_conn *conn,
487                                    ngtcp2_crypto_aead_ctx *aead_ctx,
488                                    void *user_data) {
489   (void)conn;
490   (void)aead_ctx;
491   (void)user_data;
492 }
493 
delete_crypto_cipher_ctx(ngtcp2_conn * conn,ngtcp2_crypto_cipher_ctx * cipher_ctx,void * user_data)494 static void delete_crypto_cipher_ctx(ngtcp2_conn *conn,
495                                      ngtcp2_crypto_cipher_ctx *cipher_ctx,
496                                      void *user_data) {
497   (void)conn;
498   (void)cipher_ctx;
499   (void)user_data;
500 }
501 
server_default_settings(ngtcp2_settings * settings)502 static void server_default_settings(ngtcp2_settings *settings) {
503   memset(settings, 0, sizeof(*settings));
504   settings->log_printf = NULL;
505   settings->initial_ts = 0;
506   settings->initial_rtt = NGTCP2_DEFAULT_INITIAL_RTT;
507   settings->max_udp_payload_size = 2048;
508   settings->no_udp_payload_size_shaping = 1;
509 }
510 
server_default_transport_params(ngtcp2_transport_params * params)511 static void server_default_transport_params(ngtcp2_transport_params *params) {
512   size_t i;
513 
514   memset(params, 0, sizeof(*params));
515   params->initial_max_stream_data_bidi_local = 65535;
516   params->initial_max_stream_data_bidi_remote = 65535;
517   params->initial_max_stream_data_uni = 65535;
518   params->initial_max_data = 128 * 1024;
519   params->initial_max_streams_bidi = 3;
520   params->initial_max_streams_uni = 2;
521   params->max_idle_timeout = 60;
522   params->max_udp_payload_size = 65535;
523   params->stateless_reset_token_present = 1;
524   params->active_connection_id_limit = 8;
525   for (i = 0; i < NGTCP2_STATELESS_RESET_TOKENLEN; ++i) {
526     params->stateless_reset_token[i] = (uint8_t)i;
527   }
528 }
529 
server_default_callbacks(ngtcp2_callbacks * cb)530 static void server_default_callbacks(ngtcp2_callbacks *cb) {
531   memset(cb, 0, sizeof(*cb));
532   cb->recv_client_initial = recv_client_initial;
533   cb->recv_crypto_data = recv_crypto_data_server;
534   cb->decrypt = null_decrypt;
535   cb->encrypt = null_encrypt;
536   cb->hp_mask = null_hp_mask;
537   cb->rand = genrand;
538   cb->get_new_connection_id = get_new_connection_id;
539   cb->update_key = update_key;
540   cb->delete_crypto_aead_ctx = delete_crypto_aead_ctx;
541   cb->delete_crypto_cipher_ctx = delete_crypto_cipher_ctx;
542   cb->get_path_challenge_data = get_path_challenge_data;
543 }
544 
server_early_callbacks(ngtcp2_callbacks * cb)545 static void server_early_callbacks(ngtcp2_callbacks *cb) {
546   server_default_callbacks(cb);
547 
548   cb->recv_client_initial = recv_client_initial_early;
549   cb->recv_crypto_data = recv_crypto_data_server_early_data;
550 }
551 
client_default_settings(ngtcp2_settings * settings)552 static void client_default_settings(ngtcp2_settings *settings) {
553   memset(settings, 0, sizeof(*settings));
554   settings->log_printf = NULL;
555   settings->initial_ts = 0;
556   settings->initial_rtt = NGTCP2_DEFAULT_INITIAL_RTT;
557   settings->max_udp_payload_size = 2048;
558   settings->no_udp_payload_size_shaping = 1;
559 }
560 
client_default_transport_params(ngtcp2_transport_params * params)561 static void client_default_transport_params(ngtcp2_transport_params *params) {
562   memset(params, 0, sizeof(*params));
563   params->initial_max_stream_data_bidi_local = 65535;
564   params->initial_max_stream_data_bidi_remote = 65535;
565   params->initial_max_stream_data_uni = 65535;
566   params->initial_max_data = 128 * 1024;
567   params->initial_max_streams_bidi = 0;
568   params->initial_max_streams_uni = 2;
569   params->max_idle_timeout = 60;
570   params->max_udp_payload_size = 65535;
571   params->stateless_reset_token_present = 0;
572   params->active_connection_id_limit = 8;
573 }
574 
client_default_callbacks(ngtcp2_callbacks * cb)575 static void client_default_callbacks(ngtcp2_callbacks *cb) {
576   memset(cb, 0, sizeof(*cb));
577   cb->client_initial = client_initial;
578   cb->recv_crypto_data = recv_crypto_data;
579   cb->decrypt = null_decrypt;
580   cb->encrypt = null_encrypt;
581   cb->hp_mask = null_hp_mask;
582   cb->recv_retry = recv_retry;
583   cb->rand = genrand;
584   cb->get_new_connection_id = get_new_connection_id;
585   cb->update_key = update_key;
586   cb->delete_crypto_aead_ctx = delete_crypto_aead_ctx;
587   cb->delete_crypto_cipher_ctx = delete_crypto_cipher_ctx;
588   cb->get_path_challenge_data = get_path_challenge_data;
589 }
590 
client_early_callbacks(ngtcp2_callbacks * cb)591 static void client_early_callbacks(ngtcp2_callbacks *cb) {
592   client_default_callbacks(cb);
593 
594   cb->client_initial = client_initial_early_data;
595 }
596 
conn_set_scid_used(ngtcp2_conn * conn)597 static void conn_set_scid_used(ngtcp2_conn *conn) {
598   ngtcp2_scid *scid;
599   ngtcp2_ksl_it it;
600   int rv;
601   (void)rv;
602 
603   assert(1 == ngtcp2_ksl_len(&conn->scid.set));
604 
605   it = ngtcp2_ksl_begin(&conn->scid.set);
606   scid = ngtcp2_ksl_it_get(&it);
607   scid->flags |= NGTCP2_SCID_FLAG_USED;
608 
609   assert(NGTCP2_PQ_BAD_INDEX == scid->pe.index);
610 
611   rv = ngtcp2_pq_push(&conn->scid.used, &scid->pe);
612 
613   assert(0 == rv);
614 }
615 
setup_default_server(ngtcp2_conn ** pconn)616 static void setup_default_server(ngtcp2_conn **pconn) {
617   ngtcp2_callbacks cb;
618   ngtcp2_settings settings;
619   ngtcp2_transport_params params;
620   ngtcp2_cid dcid, scid;
621   ngtcp2_transport_params *remote_params;
622   ngtcp2_crypto_aead_ctx aead_ctx = {0};
623   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
624   ngtcp2_crypto_ctx crypto_ctx;
625 
626   dcid_init(&dcid);
627   scid_init(&scid);
628 
629   init_crypto_ctx(&crypto_ctx);
630 
631   server_default_callbacks(&cb);
632   server_default_settings(&settings);
633   server_default_transport_params(&params);
634 
635   ngtcp2_conn_server_new(pconn, &dcid, &scid, &null_path.path,
636                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
637                          /* mem = */ NULL, NULL);
638   ngtcp2_conn_set_crypto_ctx(*pconn, &crypto_ctx);
639   ngtcp2_conn_install_rx_handshake_key(*pconn, &aead_ctx, null_iv,
640                                        sizeof(null_iv), &hp_ctx);
641   ngtcp2_conn_install_tx_handshake_key(*pconn, &aead_ctx, null_iv,
642                                        sizeof(null_iv), &hp_ctx);
643   ngtcp2_conn_install_rx_key(*pconn, null_secret, sizeof(null_secret),
644                              &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
645   ngtcp2_conn_install_tx_key(*pconn, null_secret, sizeof(null_secret),
646                              &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
647   (*pconn)->state = NGTCP2_CS_POST_HANDSHAKE;
648   (*pconn)->flags |= NGTCP2_CONN_FLAG_CONN_ID_NEGOTIATED |
649                      NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED |
650                      NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED_HANDLED |
651                      NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED;
652   (*pconn)->dcid.current.flags |= NGTCP2_DCID_FLAG_PATH_VALIDATED;
653   conn_set_scid_used(*pconn);
654   remote_params = &(*pconn)->remote.transport_params;
655   remote_params->initial_max_stream_data_bidi_local = 64 * 1024;
656   remote_params->initial_max_stream_data_bidi_remote = 64 * 1024;
657   remote_params->initial_max_stream_data_uni = 64 * 1024;
658   remote_params->initial_max_streams_bidi = 0;
659   remote_params->initial_max_streams_uni = 1;
660   remote_params->initial_max_data = 64 * 1024;
661   remote_params->active_connection_id_limit = 8;
662   (*pconn)->local.bidi.max_streams = remote_params->initial_max_streams_bidi;
663   (*pconn)->local.uni.max_streams = remote_params->initial_max_streams_uni;
664   (*pconn)->tx.max_offset = remote_params->initial_max_data;
665 }
666 
setup_default_client(ngtcp2_conn ** pconn)667 static void setup_default_client(ngtcp2_conn **pconn) {
668   ngtcp2_callbacks cb;
669   ngtcp2_settings settings;
670   ngtcp2_transport_params params;
671   ngtcp2_cid dcid, scid;
672   ngtcp2_transport_params *remote_params;
673   ngtcp2_crypto_aead_ctx aead_ctx = {0};
674   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
675   ngtcp2_crypto_ctx crypto_ctx;
676 
677   dcid_init(&dcid);
678   scid_init(&scid);
679 
680   init_crypto_ctx(&crypto_ctx);
681 
682   client_default_callbacks(&cb);
683   client_default_settings(&settings);
684   client_default_transport_params(&params);
685 
686   ngtcp2_conn_client_new(pconn, &dcid, &scid, &null_path.path,
687                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
688                          /* mem = */ NULL, NULL);
689   ngtcp2_conn_set_crypto_ctx(*pconn, &crypto_ctx);
690   ngtcp2_conn_install_rx_handshake_key(*pconn, &aead_ctx, null_iv,
691                                        sizeof(null_iv), &hp_ctx);
692   ngtcp2_conn_install_tx_handshake_key(*pconn, &aead_ctx, null_iv,
693                                        sizeof(null_iv), &hp_ctx);
694   ngtcp2_conn_install_rx_key(*pconn, null_secret, sizeof(null_secret),
695                              &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
696   ngtcp2_conn_install_tx_key(*pconn, null_secret, sizeof(null_secret),
697                              &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
698   (*pconn)->state = NGTCP2_CS_POST_HANDSHAKE;
699   (*pconn)->flags |= NGTCP2_CONN_FLAG_CONN_ID_NEGOTIATED |
700                      NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED |
701                      NGTCP2_CONN_FLAG_HANDSHAKE_COMPLETED_HANDLED |
702                      NGTCP2_CONN_FLAG_HANDSHAKE_CONFIRMED;
703   (*pconn)->dcid.current.flags |= NGTCP2_DCID_FLAG_PATH_VALIDATED;
704   conn_set_scid_used(*pconn);
705   remote_params = &(*pconn)->remote.transport_params;
706   remote_params->initial_max_stream_data_bidi_local = 64 * 1024;
707   remote_params->initial_max_stream_data_bidi_remote = 64 * 1024;
708   remote_params->initial_max_stream_data_uni = 64 * 1024;
709   remote_params->initial_max_streams_bidi = 1;
710   remote_params->initial_max_streams_uni = 1;
711   remote_params->initial_max_data = 64 * 1024;
712   remote_params->active_connection_id_limit = 8;
713   (*pconn)->local.bidi.max_streams = remote_params->initial_max_streams_bidi;
714   (*pconn)->local.uni.max_streams = remote_params->initial_max_streams_uni;
715   (*pconn)->tx.max_offset = remote_params->initial_max_data;
716 
717   (*pconn)->dcid.current.flags |= NGTCP2_DCID_FLAG_TOKEN_PRESENT;
718   memset((*pconn)->dcid.current.token, 0xf1, NGTCP2_STATELESS_RESET_TOKENLEN);
719 }
720 
setup_handshake_server(ngtcp2_conn ** pconn)721 static void setup_handshake_server(ngtcp2_conn **pconn) {
722   ngtcp2_callbacks cb;
723   ngtcp2_settings settings;
724   ngtcp2_transport_params params;
725   ngtcp2_cid dcid, scid;
726 
727   dcid_init(&dcid);
728   scid_init(&scid);
729 
730   server_default_callbacks(&cb);
731   server_default_settings(&settings);
732   server_default_transport_params(&params);
733 
734   ngtcp2_conn_server_new(pconn, &dcid, &scid, &null_path.path,
735                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
736                          /* mem = */ NULL, NULL);
737 }
738 
setup_handshake_client(ngtcp2_conn ** pconn)739 static void setup_handshake_client(ngtcp2_conn **pconn) {
740   ngtcp2_callbacks cb;
741   ngtcp2_settings settings;
742   ngtcp2_transport_params params;
743   ngtcp2_cid rcid, scid;
744   ngtcp2_crypto_aead retry_aead = {0, NGTCP2_FAKE_AEAD_OVERHEAD};
745   ngtcp2_crypto_aead_ctx aead_ctx = {0};
746   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
747   ngtcp2_crypto_ctx crypto_ctx;
748 
749   rcid_init(&rcid);
750   scid_init(&scid);
751 
752   init_initial_crypto_ctx(&crypto_ctx);
753 
754   client_default_callbacks(&cb);
755   client_default_settings(&settings);
756   client_default_transport_params(&params);
757 
758   ngtcp2_conn_client_new(pconn, &rcid, &scid, &null_path.path,
759                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
760                          /* mem = */ NULL, NULL);
761   ngtcp2_conn_set_initial_crypto_ctx(*pconn, &crypto_ctx);
762   ngtcp2_conn_install_initial_key(*pconn, &aead_ctx, null_iv, &hp_ctx,
763                                   &aead_ctx, null_iv, &hp_ctx, sizeof(null_iv));
764   ngtcp2_conn_set_retry_aead(*pconn, &retry_aead, &aead_ctx);
765 }
766 
setup_early_server(ngtcp2_conn ** pconn)767 static void setup_early_server(ngtcp2_conn **pconn) {
768   ngtcp2_callbacks cb;
769   ngtcp2_settings settings;
770   ngtcp2_transport_params params;
771   ngtcp2_cid dcid, scid;
772 
773   dcid_init(&dcid);
774   scid_init(&scid);
775 
776   server_early_callbacks(&cb);
777   server_default_settings(&settings);
778   server_default_transport_params(&params);
779 
780   ngtcp2_conn_server_new(pconn, &dcid, &scid, &null_path.path,
781                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
782                          /* mem = */ NULL, NULL);
783 }
784 
setup_early_client(ngtcp2_conn ** pconn)785 static void setup_early_client(ngtcp2_conn **pconn) {
786   ngtcp2_callbacks cb;
787   ngtcp2_settings settings;
788   ngtcp2_transport_params params;
789   ngtcp2_cid rcid, scid;
790   ngtcp2_crypto_aead_ctx aead_ctx = {0};
791   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
792   ngtcp2_crypto_ctx crypto_ctx;
793 
794   rcid_init(&rcid);
795   scid_init(&scid);
796 
797   init_initial_crypto_ctx(&crypto_ctx);
798 
799   client_early_callbacks(&cb);
800   client_default_settings(&settings);
801   client_default_transport_params(&params);
802 
803   ngtcp2_conn_client_new(pconn, &rcid, &scid, &null_path.path,
804                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
805                          /* mem = */ NULL, NULL);
806   ngtcp2_conn_set_initial_crypto_ctx(*pconn, &crypto_ctx);
807   ngtcp2_conn_install_initial_key(*pconn, &aead_ctx, null_iv, &hp_ctx,
808                                   &aead_ctx, null_iv, &hp_ctx, sizeof(null_iv));
809 
810   memset(&params, 0, sizeof(params));
811   params.initial_max_stream_data_bidi_local = 64 * 1024;
812   params.initial_max_stream_data_bidi_remote = 64 * 1024;
813   params.initial_max_stream_data_uni = 64 * 1024;
814   params.initial_max_streams_bidi = 1;
815   params.initial_max_streams_uni = 1;
816   params.initial_max_data = 64 * 1024;
817   params.active_connection_id_limit = 8;
818 
819   ngtcp2_conn_set_early_remote_transport_params(*pconn, &params);
820 }
821 
test_ngtcp2_conn_stream_open_close(void)822 void test_ngtcp2_conn_stream_open_close(void) {
823   ngtcp2_conn *conn;
824   uint8_t buf[2048];
825   size_t pktlen;
826   ngtcp2_ssize spktlen;
827   int rv;
828   ngtcp2_frame fr;
829   ngtcp2_strm *strm;
830   int64_t stream_id;
831 
832   setup_default_server(&conn);
833 
834   fr.type = NGTCP2_FRAME_STREAM;
835   fr.stream.flags = 0;
836   fr.stream.stream_id = 4;
837   fr.stream.fin = 0;
838   fr.stream.offset = 0;
839   fr.stream.datacnt = 1;
840   fr.stream.data[0].len = 17;
841   fr.stream.data[0].base = null_data;
842 
843   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
844                                   conn->pktns.crypto.rx.ckm);
845 
846   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
847 
848   CU_ASSERT(0 == rv);
849 
850   strm = ngtcp2_conn_find_stream(conn, 4);
851 
852   CU_ASSERT(NGTCP2_STRM_FLAG_NONE == strm->flags);
853 
854   fr.stream.fin = 1;
855   fr.stream.offset = 17;
856   fr.stream.datacnt = 0;
857 
858   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
859                                   conn->pktns.crypto.rx.ckm);
860 
861   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
862 
863   CU_ASSERT(0 == rv);
864   CU_ASSERT(NGTCP2_STRM_FLAG_SHUT_RD == strm->flags);
865   CU_ASSERT(fr.stream.offset == strm->rx.last_offset);
866   CU_ASSERT(fr.stream.offset == ngtcp2_strm_rx_offset(strm));
867 
868   spktlen =
869       ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
870                                NGTCP2_WRITE_STREAM_FLAG_FIN, 4, NULL, 0, 3);
871 
872   CU_ASSERT(spktlen > 0);
873 
874   strm = ngtcp2_conn_find_stream(conn, 4);
875 
876   CU_ASSERT(NULL != strm);
877 
878   /* Open a remote unidirectional stream */
879   fr.type = NGTCP2_FRAME_STREAM;
880   fr.stream.flags = 0;
881   fr.stream.stream_id = 2;
882   fr.stream.fin = 0;
883   fr.stream.offset = 0;
884   fr.stream.datacnt = 1;
885   fr.stream.data[0].len = 19;
886   fr.stream.data[0].base = null_data;
887 
888   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 3, &fr,
889                                   conn->pktns.crypto.rx.ckm);
890 
891   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 3);
892 
893   CU_ASSERT(0 == rv);
894 
895   strm = ngtcp2_conn_find_stream(conn, 2);
896 
897   CU_ASSERT(NGTCP2_STRM_FLAG_SHUT_WR == strm->flags);
898   CU_ASSERT(fr.stream.data[0].len == strm->rx.last_offset);
899   CU_ASSERT(fr.stream.data[0].len == ngtcp2_strm_rx_offset(strm));
900 
901   /* Open a local unidirectional stream */
902   rv = ngtcp2_conn_open_uni_stream(conn, &stream_id, NULL);
903 
904   CU_ASSERT(0 == rv);
905   CU_ASSERT(3 == stream_id);
906 
907   rv = ngtcp2_conn_open_uni_stream(conn, &stream_id, NULL);
908 
909   CU_ASSERT(NGTCP2_ERR_STREAM_ID_BLOCKED == rv);
910 
911   ngtcp2_conn_del(conn);
912 }
913 
test_ngtcp2_conn_stream_rx_flow_control(void)914 void test_ngtcp2_conn_stream_rx_flow_control(void) {
915   ngtcp2_conn *conn;
916   uint8_t buf[2048];
917   size_t pktlen;
918   ngtcp2_ssize spktlen;
919   int rv;
920   ngtcp2_frame fr;
921   ngtcp2_strm *strm;
922   size_t i;
923   int64_t stream_id;
924 
925   setup_default_server(&conn);
926 
927   conn->local.transport_params.initial_max_stream_data_bidi_remote = 2047;
928 
929   for (i = 0; i < 3; ++i) {
930     stream_id = (int64_t)(i * 4);
931     fr.type = NGTCP2_FRAME_STREAM;
932     fr.stream.flags = 0;
933     fr.stream.stream_id = stream_id;
934     fr.stream.fin = 0;
935     fr.stream.offset = 0;
936     fr.stream.datacnt = 1;
937     fr.stream.data[0].len = 1024;
938     fr.stream.data[0].base = null_data;
939 
940     pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, (int64_t)i,
941                                     &fr, conn->pktns.crypto.rx.ckm);
942     rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
943 
944     CU_ASSERT(0 == rv);
945 
946     strm = ngtcp2_conn_find_stream(conn, stream_id);
947 
948     CU_ASSERT(NULL != strm);
949 
950     rv = ngtcp2_conn_extend_max_stream_offset(conn, stream_id,
951                                               fr.stream.data[0].len);
952 
953     CU_ASSERT(0 == rv);
954   }
955 
956   CU_ASSERT(3 == ngtcp2_pq_size(&conn->tx.strmq));
957 
958   strm = ngtcp2_conn_find_stream(conn, 0);
959 
960   CU_ASSERT(ngtcp2_strm_is_tx_queued(strm));
961 
962   strm = ngtcp2_conn_find_stream(conn, 4);
963 
964   CU_ASSERT(ngtcp2_strm_is_tx_queued(strm));
965 
966   strm = ngtcp2_conn_find_stream(conn, 8);
967 
968   CU_ASSERT(ngtcp2_strm_is_tx_queued(strm));
969 
970   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 2);
971 
972   CU_ASSERT(spktlen > 0);
973   CU_ASSERT(ngtcp2_pq_empty(&conn->tx.strmq));
974 
975   for (i = 0; i < 3; ++i) {
976     stream_id = (int64_t)(i * 4);
977     strm = ngtcp2_conn_find_stream(conn, stream_id);
978 
979     CU_ASSERT(2047 + 1024 == strm->rx.max_offset);
980   }
981 
982   ngtcp2_conn_del(conn);
983 }
984 
test_ngtcp2_conn_stream_rx_flow_control_error(void)985 void test_ngtcp2_conn_stream_rx_flow_control_error(void) {
986   ngtcp2_conn *conn;
987   uint8_t buf[2048];
988   size_t pktlen;
989   int rv;
990   ngtcp2_frame fr;
991 
992   setup_default_server(&conn);
993 
994   conn->local.transport_params.initial_max_stream_data_bidi_remote = 1023;
995 
996   fr.type = NGTCP2_FRAME_STREAM;
997   fr.stream.flags = 0;
998   fr.stream.stream_id = 4;
999   fr.stream.fin = 0;
1000   fr.stream.offset = 0;
1001   fr.stream.datacnt = 1;
1002   fr.stream.data[0].len = 1024;
1003   fr.stream.data[0].base = null_data;
1004 
1005   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1006                                   conn->pktns.crypto.rx.ckm);
1007   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1008 
1009   CU_ASSERT(NGTCP2_ERR_FLOW_CONTROL == rv);
1010 
1011   ngtcp2_conn_del(conn);
1012 }
1013 
test_ngtcp2_conn_stream_tx_flow_control(void)1014 void test_ngtcp2_conn_stream_tx_flow_control(void) {
1015   ngtcp2_conn *conn;
1016   uint8_t buf[2048];
1017   size_t pktlen;
1018   ngtcp2_ssize spktlen;
1019   int rv;
1020   ngtcp2_frame fr;
1021   ngtcp2_strm *strm;
1022   ngtcp2_ssize nwrite;
1023   int64_t stream_id;
1024 
1025   setup_default_client(&conn);
1026 
1027   conn->remote.transport_params.initial_max_stream_data_bidi_remote = 2047;
1028 
1029   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
1030 
1031   CU_ASSERT(0 == rv);
1032 
1033   strm = ngtcp2_conn_find_stream(conn, stream_id);
1034   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1035                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1036                                      stream_id, null_data, 1024, 1);
1037 
1038   CU_ASSERT(spktlen > 0);
1039   CU_ASSERT(1024 == nwrite);
1040   CU_ASSERT(1024 == strm->tx.offset);
1041 
1042   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1043                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1044                                      stream_id, null_data, 1024, 2);
1045 
1046   CU_ASSERT(spktlen > 0);
1047   CU_ASSERT(1023 == nwrite);
1048   CU_ASSERT(2047 == strm->tx.offset);
1049 
1050   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1051                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1052                                      stream_id, null_data, 1024, 3);
1053 
1054   CU_ASSERT(NGTCP2_ERR_STREAM_DATA_BLOCKED == spktlen);
1055 
1056   /* We can write 0 length STREAM frame */
1057   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1058                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1059                                      stream_id, null_data, 0, 3);
1060 
1061   CU_ASSERT(spktlen > 0);
1062   CU_ASSERT(0 == nwrite);
1063   CU_ASSERT(2047 == strm->tx.offset);
1064 
1065   fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
1066   fr.max_stream_data.stream_id = stream_id;
1067   fr.max_stream_data.max_stream_data = 2048;
1068 
1069   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1070                                   conn->pktns.crypto.rx.ckm);
1071 
1072   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 4);
1073 
1074   CU_ASSERT(0 == rv);
1075   CU_ASSERT(2048 == strm->tx.max_offset);
1076 
1077   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1078                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1079                                      stream_id, null_data, 1024, 5);
1080 
1081   CU_ASSERT(spktlen > 0);
1082   CU_ASSERT(1 == nwrite);
1083   CU_ASSERT(2048 == strm->tx.offset);
1084 
1085   ngtcp2_conn_del(conn);
1086 
1087   /* CWND left is round up to the maximum UDP packet size */
1088   setup_default_client(&conn);
1089 
1090   conn->cstat.cwnd = 1;
1091 
1092   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
1093 
1094   CU_ASSERT(0 == rv);
1095 
1096   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1097                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_FIN,
1098                                      stream_id, null_data, 1024, 1);
1099 
1100   CU_ASSERT(spktlen > 0);
1101   CU_ASSERT(1024 == nwrite);
1102 
1103   ngtcp2_conn_del(conn);
1104 }
1105 
test_ngtcp2_conn_rx_flow_control(void)1106 void test_ngtcp2_conn_rx_flow_control(void) {
1107   ngtcp2_conn *conn;
1108   uint8_t buf[2048];
1109   size_t pktlen;
1110   ngtcp2_ssize spktlen;
1111   int rv;
1112   ngtcp2_frame fr;
1113 
1114   setup_default_server(&conn);
1115 
1116   conn->local.transport_params.initial_max_data = 1024;
1117   conn->rx.window = 1024;
1118   conn->rx.max_offset = 1024;
1119   conn->rx.unsent_max_offset = 1024;
1120 
1121   fr.type = NGTCP2_FRAME_STREAM;
1122   fr.stream.flags = 0;
1123   fr.stream.stream_id = 4;
1124   fr.stream.fin = 0;
1125   fr.stream.offset = 0;
1126   fr.stream.datacnt = 1;
1127   fr.stream.data[0].len = 1023;
1128   fr.stream.data[0].base = null_data;
1129 
1130   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1131                                   conn->pktns.crypto.rx.ckm);
1132   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1133 
1134   CU_ASSERT(0 == rv);
1135 
1136   ngtcp2_conn_extend_max_offset(conn, 1023);
1137 
1138   CU_ASSERT(1024 + 1023 == conn->rx.unsent_max_offset);
1139   CU_ASSERT(1024 == conn->rx.max_offset);
1140 
1141   fr.type = NGTCP2_FRAME_STREAM;
1142   fr.stream.flags = 0;
1143   fr.stream.stream_id = 4;
1144   fr.stream.fin = 0;
1145   fr.stream.offset = 1023;
1146   fr.stream.datacnt = 1;
1147   fr.stream.data[0].len = 1;
1148   fr.stream.data[0].base = null_data;
1149 
1150   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1151                                   conn->pktns.crypto.rx.ckm);
1152   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1153 
1154   CU_ASSERT(0 == rv);
1155 
1156   ngtcp2_conn_extend_max_offset(conn, 1);
1157 
1158   CU_ASSERT(2048 == conn->rx.unsent_max_offset);
1159   CU_ASSERT(1024 == conn->rx.max_offset);
1160 
1161   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 3);
1162 
1163   CU_ASSERT(spktlen > 0);
1164   CU_ASSERT(2048 == conn->rx.max_offset);
1165 
1166   ngtcp2_conn_del(conn);
1167 }
1168 
test_ngtcp2_conn_rx_flow_control_error(void)1169 void test_ngtcp2_conn_rx_flow_control_error(void) {
1170   ngtcp2_conn *conn;
1171   uint8_t buf[2048];
1172   size_t pktlen;
1173   int rv;
1174   ngtcp2_frame fr;
1175 
1176   setup_default_server(&conn);
1177 
1178   conn->local.transport_params.initial_max_data = 1024;
1179   conn->rx.window = 1024;
1180   conn->rx.max_offset = 1024;
1181   conn->rx.unsent_max_offset = 1024;
1182 
1183   fr.type = NGTCP2_FRAME_STREAM;
1184   fr.stream.flags = 0;
1185   fr.stream.stream_id = 4;
1186   fr.stream.fin = 0;
1187   fr.stream.offset = 0;
1188   fr.stream.datacnt = 1;
1189   fr.stream.data[0].len = 1025;
1190   fr.stream.data[0].base = null_data;
1191 
1192   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1193                                   conn->pktns.crypto.rx.ckm);
1194   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1195 
1196   CU_ASSERT(NGTCP2_ERR_FLOW_CONTROL == rv);
1197 
1198   ngtcp2_conn_del(conn);
1199 }
1200 
test_ngtcp2_conn_tx_flow_control(void)1201 void test_ngtcp2_conn_tx_flow_control(void) {
1202   ngtcp2_conn *conn;
1203   uint8_t buf[2048];
1204   size_t pktlen;
1205   ngtcp2_ssize spktlen;
1206   int rv;
1207   ngtcp2_frame fr;
1208   ngtcp2_ssize nwrite;
1209   int64_t stream_id;
1210 
1211   setup_default_client(&conn);
1212 
1213   conn->remote.transport_params.initial_max_data = 2048;
1214   conn->tx.max_offset = 2048;
1215 
1216   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
1217 
1218   CU_ASSERT(0 == rv);
1219 
1220   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1221                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1222                                      stream_id, null_data, 1024, 1);
1223 
1224   CU_ASSERT(spktlen > 0);
1225   CU_ASSERT(1024 == nwrite);
1226   CU_ASSERT(1024 == conn->tx.offset);
1227 
1228   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1229                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1230                                      stream_id, null_data, 1023, 2);
1231 
1232   CU_ASSERT(spktlen > 0);
1233   CU_ASSERT(1023 == nwrite);
1234   CU_ASSERT(1024 + 1023 == conn->tx.offset);
1235 
1236   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1237                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1238                                      stream_id, null_data, 1024, 3);
1239 
1240   CU_ASSERT(spktlen > 0);
1241   CU_ASSERT(1 == nwrite);
1242   CU_ASSERT(2048 == conn->tx.offset);
1243 
1244   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1245                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1246                                      stream_id, null_data, 1024, 4);
1247 
1248   CU_ASSERT(spktlen == 0);
1249   CU_ASSERT(-1 == nwrite);
1250 
1251   fr.type = NGTCP2_FRAME_MAX_DATA;
1252   fr.max_data.max_data = 3072;
1253 
1254   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1255                                   conn->pktns.crypto.rx.ckm);
1256 
1257   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 5);
1258 
1259   CU_ASSERT(0 == rv);
1260   CU_ASSERT(3072 == conn->tx.max_offset);
1261 
1262   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
1263                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
1264                                      stream_id, null_data, 1024, 4);
1265 
1266   CU_ASSERT(spktlen > 0);
1267   CU_ASSERT(1024 == nwrite);
1268   CU_ASSERT(3072 == conn->tx.offset);
1269 
1270   ngtcp2_conn_del(conn);
1271 }
1272 
test_ngtcp2_conn_shutdown_stream_write(void)1273 void test_ngtcp2_conn_shutdown_stream_write(void) {
1274   ngtcp2_conn *conn;
1275   int rv;
1276   ngtcp2_frame_chain *frc;
1277   uint8_t buf[2048];
1278   ngtcp2_frame fr;
1279   size_t pktlen;
1280   ngtcp2_ssize spktlen;
1281   ngtcp2_strm *strm;
1282   int64_t stream_id;
1283 
1284   /* Stream not found */
1285   setup_default_server(&conn);
1286 
1287   rv = ngtcp2_conn_shutdown_stream_write(conn, 4, NGTCP2_APP_ERR01);
1288 
1289   CU_ASSERT(NGTCP2_ERR_STREAM_NOT_FOUND == rv);
1290 
1291   ngtcp2_conn_del(conn);
1292 
1293   /* Check final_size */
1294   setup_default_client(&conn);
1295 
1296   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
1297   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
1298                            NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id, null_data,
1299                            1239, 1);
1300   rv = ngtcp2_conn_shutdown_stream_write(conn, stream_id, NGTCP2_APP_ERR01);
1301 
1302   CU_ASSERT(0 == rv);
1303 
1304   for (frc = conn->pktns.tx.frq; frc; frc = frc->next) {
1305     if (frc->fr.type == NGTCP2_FRAME_RESET_STREAM) {
1306       break;
1307     }
1308   }
1309 
1310   CU_ASSERT(NULL != frc);
1311   CU_ASSERT(stream_id == frc->fr.reset_stream.stream_id);
1312   CU_ASSERT(NGTCP2_APP_ERR01 == frc->fr.reset_stream.app_error_code);
1313   CU_ASSERT(1239 == frc->fr.reset_stream.final_size);
1314 
1315   strm = ngtcp2_conn_find_stream(conn, stream_id);
1316 
1317   CU_ASSERT(NULL != strm);
1318   CU_ASSERT(NGTCP2_APP_ERR01 == strm->app_error_code);
1319 
1320   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 2);
1321 
1322   CU_ASSERT(spktlen > 0);
1323 
1324   fr.type = NGTCP2_FRAME_RESET_STREAM;
1325   fr.reset_stream.stream_id = stream_id;
1326   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1327   fr.reset_stream.final_size = 100;
1328 
1329   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 890, &fr,
1330                                   conn->pktns.crypto.rx.ckm);
1331 
1332   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1333 
1334   CU_ASSERT(0 == rv);
1335   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, stream_id));
1336 
1337   fr.type = NGTCP2_FRAME_ACK;
1338   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
1339   fr.ack.ack_delay = 0;
1340   fr.ack.first_ack_blklen = 0;
1341   fr.ack.num_blks = 0;
1342 
1343   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 899, &fr,
1344                                   conn->pktns.crypto.rx.ckm);
1345   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1346 
1347   CU_ASSERT(0 == rv);
1348   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, stream_id));
1349 
1350   ngtcp2_conn_del(conn);
1351 
1352   /* Check that stream is closed when RESET_STREAM is acknowledged */
1353   setup_default_client(&conn);
1354 
1355   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
1356 
1357   fr.type = NGTCP2_FRAME_STREAM;
1358   fr.stream.stream_id = stream_id;
1359   fr.stream.fin = 0;
1360   fr.stream.offset = 0;
1361   fr.stream.datacnt = 0;
1362 
1363   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 119, &fr,
1364                                   conn->pktns.crypto.rx.ckm);
1365 
1366   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1367 
1368   CU_ASSERT(0 == rv);
1369   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, stream_id));
1370 
1371   rv = ngtcp2_conn_shutdown_stream_write(conn, stream_id, NGTCP2_APP_ERR01);
1372 
1373   CU_ASSERT(0 == rv);
1374   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, stream_id));
1375 
1376   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 2);
1377 
1378   CU_ASSERT(spktlen > 0);
1379 
1380   /* Incoming FIN does not close stream */
1381   fr.type = NGTCP2_FRAME_STREAM;
1382   fr.stream.fin = 1;
1383   fr.stream.offset = 0;
1384   fr.stream.datacnt = 0;
1385 
1386   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 121, &fr,
1387                                   conn->pktns.crypto.rx.ckm);
1388   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1389 
1390   CU_ASSERT(0 == rv);
1391   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, stream_id));
1392 
1393   fr.type = NGTCP2_FRAME_ACK;
1394   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
1395   fr.ack.ack_delay = 0;
1396   fr.ack.first_ack_blklen = 0;
1397   fr.ack.num_blks = 0;
1398 
1399   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 332, &fr,
1400                                   conn->pktns.crypto.rx.ckm);
1401 
1402   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 3);
1403 
1404   CU_ASSERT(0 == rv);
1405   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, stream_id));
1406 
1407   ngtcp2_conn_del(conn);
1408 }
1409 
test_ngtcp2_conn_recv_reset_stream(void)1410 void test_ngtcp2_conn_recv_reset_stream(void) {
1411   ngtcp2_conn *conn;
1412   int rv;
1413   uint8_t buf[2048];
1414   ngtcp2_frame fr;
1415   size_t pktlen;
1416   ngtcp2_ssize spktlen;
1417   ngtcp2_strm *strm;
1418   int64_t stream_id;
1419 
1420   /* Receive RESET_STREAM */
1421   setup_default_server(&conn);
1422 
1423   fr.type = NGTCP2_FRAME_STREAM;
1424   fr.stream.flags = 0;
1425   fr.stream.stream_id = 4;
1426   fr.stream.fin = 0;
1427   fr.stream.offset = 0;
1428   fr.stream.datacnt = 1;
1429   fr.stream.data[0].len = 955;
1430   fr.stream.data[0].base = null_data;
1431 
1432   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1433                                   conn->pktns.crypto.rx.ckm);
1434   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1435 
1436   CU_ASSERT(0 == rv);
1437 
1438   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
1439                            NGTCP2_WRITE_STREAM_FLAG_NONE, 4, null_data, 354, 2);
1440 
1441   fr.type = NGTCP2_FRAME_RESET_STREAM;
1442   fr.reset_stream.stream_id = 4;
1443   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1444   fr.reset_stream.final_size = 955;
1445 
1446   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1447                                   conn->pktns.crypto.rx.ckm);
1448   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 3);
1449 
1450   CU_ASSERT(0 == rv);
1451 
1452   strm = ngtcp2_conn_find_stream(conn, 4);
1453 
1454   CU_ASSERT(strm->flags & NGTCP2_STRM_FLAG_SHUT_RD);
1455   CU_ASSERT(strm->flags & NGTCP2_STRM_FLAG_RECV_RST);
1456 
1457   ngtcp2_conn_del(conn);
1458 
1459   /* Receive RESET_STREAM after sending STOP_SENDING */
1460   setup_default_server(&conn);
1461 
1462   fr.type = NGTCP2_FRAME_STREAM;
1463   fr.stream.flags = 0;
1464   fr.stream.stream_id = 4;
1465   fr.stream.fin = 0;
1466   fr.stream.offset = 0;
1467   fr.stream.datacnt = 1;
1468   fr.stream.data[0].len = 955;
1469   fr.stream.data[0].base = null_data;
1470 
1471   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1472                                   conn->pktns.crypto.rx.ckm);
1473   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1474 
1475   CU_ASSERT(0 == rv);
1476 
1477   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
1478                            NGTCP2_WRITE_STREAM_FLAG_NONE, 4, null_data, 354, 2);
1479   ngtcp2_conn_shutdown_stream_read(conn, 4, NGTCP2_APP_ERR01);
1480   ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 3);
1481 
1482   fr.type = NGTCP2_FRAME_RESET_STREAM;
1483   fr.reset_stream.stream_id = 4;
1484   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1485   fr.reset_stream.final_size = 955;
1486 
1487   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1488                                   conn->pktns.crypto.rx.ckm);
1489   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 4);
1490 
1491   CU_ASSERT(0 == rv);
1492   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
1493 
1494   ngtcp2_conn_del(conn);
1495 
1496   /* Receive RESET_STREAM after sending RESET_STREAM */
1497   setup_default_server(&conn);
1498 
1499   fr.type = NGTCP2_FRAME_STREAM;
1500   fr.stream.flags = 0;
1501   fr.stream.stream_id = 4;
1502   fr.stream.fin = 0;
1503   fr.stream.offset = 0;
1504   fr.stream.datacnt = 1;
1505   fr.stream.data[0].len = 955;
1506   fr.stream.data[0].base = null_data;
1507 
1508   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1509                                   conn->pktns.crypto.rx.ckm);
1510   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1511 
1512   CU_ASSERT(0 == rv);
1513 
1514   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
1515                            NGTCP2_WRITE_STREAM_FLAG_NONE, 4, null_data, 354, 2);
1516   ngtcp2_conn_shutdown_stream_write(conn, 4, NGTCP2_APP_ERR01);
1517   ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 3);
1518 
1519   fr.type = NGTCP2_FRAME_RESET_STREAM;
1520   fr.reset_stream.stream_id = 4;
1521   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1522   fr.reset_stream.final_size = 955;
1523 
1524   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1525                                   conn->pktns.crypto.rx.ckm);
1526   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 4);
1527 
1528   CU_ASSERT(0 == rv);
1529   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
1530 
1531   fr.type = NGTCP2_FRAME_ACK;
1532   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
1533   fr.ack.ack_delay = 0;
1534   fr.ack.first_ack_blklen = 0;
1535   fr.ack.num_blks = 0;
1536 
1537   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 3, &fr,
1538                                   conn->pktns.crypto.rx.ckm);
1539   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 5);
1540 
1541   CU_ASSERT(0 == rv);
1542   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, 4));
1543 
1544   ngtcp2_conn_del(conn);
1545 
1546   /* Receive RESET_STREAM after receiving STOP_SENDING */
1547   setup_default_server(&conn);
1548 
1549   fr.type = NGTCP2_FRAME_STREAM;
1550   fr.stream.flags = 0;
1551   fr.stream.stream_id = 4;
1552   fr.stream.fin = 0;
1553   fr.stream.offset = 0;
1554   fr.stream.datacnt = 1;
1555   fr.stream.data[0].len = 955;
1556   fr.stream.data[0].base = null_data;
1557 
1558   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1559                                   conn->pktns.crypto.rx.ckm);
1560   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1561 
1562   CU_ASSERT(0 == rv);
1563 
1564   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
1565                            NGTCP2_WRITE_STREAM_FLAG_NONE, 4, null_data, 354, 2);
1566 
1567   fr.type = NGTCP2_FRAME_STOP_SENDING;
1568   fr.stop_sending.stream_id = 4;
1569   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
1570 
1571   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1572                                   conn->pktns.crypto.rx.ckm);
1573   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 3);
1574 
1575   CU_ASSERT(0 == rv);
1576   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
1577 
1578   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 4);
1579 
1580   CU_ASSERT(spktlen > 0);
1581 
1582   fr.type = NGTCP2_FRAME_RESET_STREAM;
1583   fr.reset_stream.stream_id = 4;
1584   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1585   fr.reset_stream.final_size = 955;
1586 
1587   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 3, &fr,
1588                                   conn->pktns.crypto.rx.ckm);
1589   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 4);
1590 
1591   CU_ASSERT(0 == rv);
1592   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
1593 
1594   fr.type = NGTCP2_FRAME_ACK;
1595   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
1596   fr.ack.ack_delay = 0;
1597   fr.ack.first_ack_blklen = 0;
1598   fr.ack.num_blks = 0;
1599 
1600   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 4, &fr,
1601                                   conn->pktns.crypto.rx.ckm);
1602   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 5);
1603 
1604   CU_ASSERT(0 == rv);
1605   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, 4));
1606 
1607   ngtcp2_conn_del(conn);
1608 
1609   /* final_size in RESET_STREAM exceeds the already received offset */
1610   setup_default_server(&conn);
1611 
1612   fr.type = NGTCP2_FRAME_STREAM;
1613   fr.stream.flags = 0;
1614   fr.stream.stream_id = 4;
1615   fr.stream.fin = 0;
1616   fr.stream.offset = 0;
1617   fr.stream.datacnt = 1;
1618   fr.stream.data[0].len = 955;
1619   fr.stream.data[0].base = null_data;
1620 
1621   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1622                                   conn->pktns.crypto.rx.ckm);
1623   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1624 
1625   CU_ASSERT(0 == rv);
1626 
1627   fr.type = NGTCP2_FRAME_RESET_STREAM;
1628   fr.reset_stream.stream_id = 4;
1629   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1630   fr.reset_stream.final_size = 954;
1631 
1632   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1633                                   conn->pktns.crypto.rx.ckm);
1634   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1635 
1636   CU_ASSERT(NGTCP2_ERR_FINAL_SIZE == rv);
1637 
1638   ngtcp2_conn_del(conn);
1639 
1640   /* final_size in RESET_STREAM differs from the final offset which
1641      STREAM frame with fin indicated. */
1642   setup_default_server(&conn);
1643 
1644   fr.type = NGTCP2_FRAME_STREAM;
1645   fr.stream.flags = 0;
1646   fr.stream.stream_id = 4;
1647   fr.stream.fin = 1;
1648   fr.stream.offset = 0;
1649   fr.stream.datacnt = 1;
1650   fr.stream.data[0].len = 955;
1651   fr.stream.data[0].base = null_data;
1652 
1653   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1654                                   conn->pktns.crypto.rx.ckm);
1655   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1656 
1657   CU_ASSERT(0 == rv);
1658 
1659   fr.type = NGTCP2_FRAME_RESET_STREAM;
1660   fr.reset_stream.stream_id = 4;
1661   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1662   fr.reset_stream.final_size = 956;
1663 
1664   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1665                                   conn->pktns.crypto.rx.ckm);
1666   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1667 
1668   CU_ASSERT(NGTCP2_ERR_FINAL_SIZE == rv);
1669 
1670   ngtcp2_conn_del(conn);
1671 
1672   /* RESET_STREAM against local stream which has not been initiated. */
1673   setup_default_server(&conn);
1674 
1675   fr.type = NGTCP2_FRAME_RESET_STREAM;
1676   fr.reset_stream.stream_id = 1;
1677   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
1678   fr.reset_stream.final_size = 0;
1679 
1680   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1681                                   conn->pktns.crypto.rx.ckm);
1682   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1683 
1684   CU_ASSERT(NGTCP2_ERR_STREAM_STATE == rv);
1685 
1686   ngtcp2_conn_del(conn);
1687 
1688   /* RESET_STREAM against remote stream which has not been initiated */
1689   setup_default_server(&conn);
1690 
1691   fr.type = NGTCP2_FRAME_RESET_STREAM;
1692   fr.reset_stream.stream_id = 0;
1693   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
1694   fr.reset_stream.final_size = 1999;
1695 
1696   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1697                                   conn->pktns.crypto.rx.ckm);
1698   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1699 
1700   CU_ASSERT(0 == rv);
1701   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, 0));
1702   CU_ASSERT(4 == conn->remote.bidi.unsent_max_streams);
1703 
1704   ngtcp2_conn_del(conn);
1705 
1706   /* RESET_STREAM against remote stream which is larger than allowed
1707      maximum */
1708   setup_default_server(&conn);
1709 
1710   fr.type = NGTCP2_FRAME_RESET_STREAM;
1711   fr.reset_stream.stream_id = 16;
1712   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
1713   fr.reset_stream.final_size = 0;
1714 
1715   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1716                                   conn->pktns.crypto.rx.ckm);
1717   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1718 
1719   CU_ASSERT(NGTCP2_ERR_STREAM_LIMIT == rv);
1720 
1721   ngtcp2_conn_del(conn);
1722 
1723   /* RESET_STREAM against remote stream which is allowed, and no
1724      ngtcp2_strm object has been created */
1725   setup_default_server(&conn);
1726 
1727   fr.type = NGTCP2_FRAME_RESET_STREAM;
1728   fr.reset_stream.stream_id = 4;
1729   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
1730   fr.reset_stream.final_size = 0;
1731 
1732   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1733                                   conn->pktns.crypto.rx.ckm);
1734   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1735 
1736   CU_ASSERT(0 == rv);
1737   CU_ASSERT(
1738       ngtcp2_idtr_is_open(&conn->remote.bidi.idtr, fr.reset_stream.stream_id));
1739 
1740   ngtcp2_conn_del(conn);
1741 
1742   /* RESET_STREAM against remote stream which is allowed, and no
1743      ngtcp2_strm object has been created, and final_size violates
1744      connection-level flow control. */
1745   setup_default_server(&conn);
1746 
1747   conn->local.transport_params.initial_max_stream_data_bidi_remote = 1 << 21;
1748 
1749   fr.type = NGTCP2_FRAME_RESET_STREAM;
1750   fr.reset_stream.stream_id = 4;
1751   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
1752   fr.reset_stream.final_size = 1 << 20;
1753 
1754   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1755                                   conn->pktns.crypto.rx.ckm);
1756   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1757 
1758   CU_ASSERT(NGTCP2_ERR_FLOW_CONTROL == rv);
1759 
1760   ngtcp2_conn_del(conn);
1761 
1762   /* RESET_STREAM against remote stream which is allowed, and no
1763       ngtcp2_strm object has been created, and final_size violates
1764       stream-level flow control. */
1765   setup_default_server(&conn);
1766 
1767   conn->rx.max_offset = 1 << 21;
1768 
1769   fr.type = NGTCP2_FRAME_RESET_STREAM;
1770   fr.reset_stream.stream_id = 4;
1771   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
1772   fr.reset_stream.final_size = 1 << 20;
1773 
1774   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1775                                   conn->pktns.crypto.rx.ckm);
1776   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1777 
1778   CU_ASSERT(NGTCP2_ERR_FLOW_CONTROL == rv);
1779 
1780   ngtcp2_conn_del(conn);
1781 
1782   /* final_size in RESET_STREAM violates connection-level flow
1783      control */
1784   setup_default_server(&conn);
1785 
1786   conn->local.transport_params.initial_max_stream_data_bidi_remote = 1 << 21;
1787 
1788   fr.type = NGTCP2_FRAME_STREAM;
1789   fr.stream.flags = 0;
1790   fr.stream.stream_id = 4;
1791   fr.stream.fin = 0;
1792   fr.stream.offset = 0;
1793   fr.stream.datacnt = 1;
1794   fr.stream.data[0].len = 955;
1795   fr.stream.data[0].base = null_data;
1796 
1797   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1798                                   conn->pktns.crypto.rx.ckm);
1799   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1800 
1801   CU_ASSERT(0 == rv);
1802 
1803   fr.type = NGTCP2_FRAME_RESET_STREAM;
1804   fr.reset_stream.stream_id = 4;
1805   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1806   fr.reset_stream.final_size = 1024 * 1024;
1807 
1808   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1809                                   conn->pktns.crypto.rx.ckm);
1810   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1811 
1812   CU_ASSERT(NGTCP2_ERR_FLOW_CONTROL == rv);
1813 
1814   ngtcp2_conn_del(conn);
1815 
1816   /* final_size in RESET_STREAM violates stream-level flow control */
1817   setup_default_server(&conn);
1818 
1819   conn->rx.max_offset = 1 << 21;
1820 
1821   fr.type = NGTCP2_FRAME_STREAM;
1822   fr.stream.flags = 0;
1823   fr.stream.stream_id = 4;
1824   fr.stream.fin = 0;
1825   fr.stream.offset = 0;
1826   fr.stream.datacnt = 1;
1827   fr.stream.data[0].len = 955;
1828   fr.stream.data[0].base = null_data;
1829 
1830   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1831                                   conn->pktns.crypto.rx.ckm);
1832   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1833 
1834   CU_ASSERT(0 == rv);
1835 
1836   fr.type = NGTCP2_FRAME_RESET_STREAM;
1837   fr.reset_stream.stream_id = 4;
1838   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1839   fr.reset_stream.final_size = 1024 * 1024;
1840 
1841   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1842                                   conn->pktns.crypto.rx.ckm);
1843   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1844 
1845   CU_ASSERT(NGTCP2_ERR_FLOW_CONTROL == rv);
1846 
1847   ngtcp2_conn_del(conn);
1848 
1849   /* Receiving RESET_STREAM for a local unidirectional stream is a
1850      protocol violation. */
1851   setup_default_server(&conn);
1852 
1853   rv = ngtcp2_conn_open_uni_stream(conn, &stream_id, NULL);
1854 
1855   CU_ASSERT(0 == rv);
1856 
1857   fr.type = NGTCP2_FRAME_RESET_STREAM;
1858   fr.reset_stream.stream_id = stream_id;
1859   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1860   fr.reset_stream.final_size = 0;
1861 
1862   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1863                                   conn->pktns.crypto.rx.ckm);
1864   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1865 
1866   CU_ASSERT(NGTCP2_ERR_PROTO == rv);
1867 
1868   ngtcp2_conn_del(conn);
1869 
1870   /* RESET_STREAM extends connection window including buffered data */
1871   setup_default_server(&conn);
1872 
1873   fr.type = NGTCP2_FRAME_STREAM;
1874   fr.stream.flags = 0;
1875   fr.stream.stream_id = 4;
1876   fr.stream.fin = 0;
1877   fr.stream.offset = 1;
1878   fr.stream.datacnt = 1;
1879   fr.stream.data[0].len = 955;
1880   fr.stream.data[0].base = null_data;
1881 
1882   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1883                                   conn->pktns.crypto.rx.ckm);
1884   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1885 
1886   CU_ASSERT(0 == rv);
1887 
1888   fr.type = NGTCP2_FRAME_RESET_STREAM;
1889   fr.reset_stream.stream_id = 4;
1890   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1891   fr.reset_stream.final_size = 1024;
1892 
1893   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1894                                   conn->pktns.crypto.rx.ckm);
1895   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
1896 
1897   CU_ASSERT(0 == rv);
1898   CU_ASSERT(1024 == conn->rx.offset);
1899   CU_ASSERT(128 * 1024 + 1024 == conn->rx.unsent_max_offset);
1900 
1901   /* Receiving same RESET_STREAM does not increase rx offsets. */
1902   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 3);
1903 
1904   CU_ASSERT(0 == rv);
1905   CU_ASSERT(1024 == conn->rx.offset);
1906   CU_ASSERT(128 * 1024 + 1024 == conn->rx.unsent_max_offset);
1907 
1908   ngtcp2_conn_del(conn);
1909 
1910   /* Verify that connection window is properly updated when
1911      RESET_STREAM is received after sending STOP_SENDING */
1912   setup_default_server(&conn);
1913 
1914   fr.type = NGTCP2_FRAME_STREAM;
1915   fr.stream.flags = 0;
1916   fr.stream.stream_id = 4;
1917   fr.stream.fin = 0;
1918   fr.stream.offset = 1;
1919   fr.stream.datacnt = 1;
1920   fr.stream.data[0].len = 955;
1921   fr.stream.data[0].base = null_data;
1922 
1923   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
1924                                   conn->pktns.crypto.rx.ckm);
1925   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
1926 
1927   CU_ASSERT(0 == rv);
1928 
1929   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
1930                            NGTCP2_WRITE_STREAM_FLAG_NONE, 4, null_data, 354, 2);
1931   ngtcp2_conn_shutdown_stream_read(conn, 4, NGTCP2_APP_ERR01);
1932   ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 3);
1933 
1934   CU_ASSERT(128 * 1024 + 956 == conn->rx.unsent_max_offset);
1935 
1936   fr.type = NGTCP2_FRAME_RESET_STREAM;
1937   fr.reset_stream.stream_id = 4;
1938   fr.reset_stream.app_error_code = NGTCP2_APP_ERR02;
1939   fr.reset_stream.final_size = 957;
1940 
1941   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
1942                                   conn->pktns.crypto.rx.ckm);
1943   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 4);
1944 
1945   CU_ASSERT(0 == rv);
1946   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
1947   CU_ASSERT(128 * 1024 + 956 + 1 == conn->rx.unsent_max_offset);
1948 
1949   ngtcp2_conn_del(conn);
1950 }
1951 
test_ngtcp2_conn_recv_stop_sending(void)1952 void test_ngtcp2_conn_recv_stop_sending(void) {
1953   ngtcp2_conn *conn;
1954   int rv;
1955   uint8_t buf[2048];
1956   ngtcp2_frame fr;
1957   size_t pktlen;
1958   ngtcp2_ssize spktlen;
1959   ngtcp2_strm *strm;
1960   ngtcp2_tstamp t = 0;
1961   int64_t pkt_num = 0;
1962   ngtcp2_frame_chain *frc;
1963   int64_t stream_id;
1964 
1965   /* Receive STOP_SENDING */
1966   setup_default_client(&conn);
1967 
1968   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
1969   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
1970                            NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id, null_data,
1971                            333, ++t);
1972 
1973   fr.type = NGTCP2_FRAME_STOP_SENDING;
1974   fr.stop_sending.stream_id = stream_id;
1975   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
1976 
1977   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
1978                                   &fr, conn->pktns.crypto.rx.ckm);
1979   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
1980 
1981   CU_ASSERT(0 == rv);
1982 
1983   strm = ngtcp2_conn_find_stream(conn, stream_id);
1984 
1985   CU_ASSERT(strm->flags & NGTCP2_STRM_FLAG_SHUT_WR);
1986   CU_ASSERT(strm->flags & NGTCP2_STRM_FLAG_SENT_RST);
1987 
1988   for (frc = conn->pktns.tx.frq; frc; frc = frc->next) {
1989     if (frc->fr.type == NGTCP2_FRAME_RESET_STREAM) {
1990       break;
1991     }
1992   }
1993 
1994   CU_ASSERT(NULL != frc);
1995   CU_ASSERT(NGTCP2_APP_ERR01 == frc->fr.reset_stream.app_error_code);
1996   CU_ASSERT(333 == frc->fr.reset_stream.final_size);
1997 
1998   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
1999 
2000   CU_ASSERT(spktlen > 0);
2001 
2002   for (frc = conn->pktns.tx.frq; frc; frc = frc->next) {
2003     if (frc->fr.type == NGTCP2_FRAME_RESET_STREAM) {
2004       break;
2005     }
2006   }
2007 
2008   CU_ASSERT(NULL == frc);
2009 
2010   /* Make sure that receiving duplicated STOP_SENDING does not trigger
2011      another RESET_STREAM. */
2012   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
2013                                   &fr, conn->pktns.crypto.rx.ckm);
2014   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2015 
2016   CU_ASSERT(0 == rv);
2017 
2018   strm = ngtcp2_conn_find_stream(conn, stream_id);
2019 
2020   CU_ASSERT(strm->flags & NGTCP2_STRM_FLAG_SHUT_WR);
2021   CU_ASSERT(strm->flags & NGTCP2_STRM_FLAG_SENT_RST);
2022 
2023   for (frc = conn->pktns.tx.frq; frc; frc = frc->next) {
2024     if (frc->fr.type == NGTCP2_FRAME_RESET_STREAM) {
2025       break;
2026     }
2027   }
2028 
2029   CU_ASSERT(NULL == frc);
2030 
2031   ngtcp2_conn_del(conn);
2032 
2033   /* Receive STOP_SENDING after receiving RESET_STREAM */
2034   setup_default_client(&conn);
2035 
2036   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2037   ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2038                            NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id, null_data,
2039                            333, ++t);
2040 
2041   fr.type = NGTCP2_FRAME_RESET_STREAM;
2042   fr.reset_stream.stream_id = stream_id;
2043   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
2044   fr.reset_stream.final_size = 0;
2045 
2046   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
2047                                   &fr, conn->pktns.crypto.rx.ckm);
2048   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2049 
2050   CU_ASSERT(0 == rv);
2051 
2052   fr.type = NGTCP2_FRAME_STOP_SENDING;
2053   fr.stop_sending.stream_id = stream_id;
2054   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
2055 
2056   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
2057                                   &fr, conn->pktns.crypto.rx.ckm);
2058   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2059 
2060   CU_ASSERT(0 == rv);
2061   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, stream_id));
2062 
2063   for (frc = conn->pktns.tx.frq; frc; frc = frc->next) {
2064     if (frc->fr.type == NGTCP2_FRAME_RESET_STREAM) {
2065       break;
2066     }
2067   }
2068 
2069   CU_ASSERT(NULL != frc);
2070   CU_ASSERT(NGTCP2_APP_ERR01 == frc->fr.reset_stream.app_error_code);
2071   CU_ASSERT(333 == frc->fr.reset_stream.final_size);
2072 
2073   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2074 
2075   CU_ASSERT(spktlen > 0);
2076 
2077   fr.type = NGTCP2_FRAME_ACK;
2078   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
2079   fr.ack.ack_delay = 0;
2080   fr.ack.first_ack_blklen = 0;
2081   fr.ack.num_blks = 0;
2082 
2083   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
2084                                   &fr, conn->pktns.crypto.rx.ckm);
2085   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2086 
2087   CU_ASSERT(0 == rv);
2088   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, stream_id));
2089 
2090   ngtcp2_conn_del(conn);
2091 
2092   /* STOP_SENDING against remote bidirectional stream which has not
2093      been initiated. */
2094   setup_default_server(&conn);
2095 
2096   fr.type = NGTCP2_FRAME_STOP_SENDING;
2097   fr.stop_sending.stream_id = 0;
2098   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
2099 
2100   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
2101                                   conn->pktns.crypto.rx.ckm);
2102   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2103 
2104   CU_ASSERT(0 == rv);
2105 
2106   strm = ngtcp2_conn_find_stream(conn, 0);
2107 
2108   CU_ASSERT(NULL != strm);
2109   CU_ASSERT(strm->flags & NGTCP2_STRM_FLAG_SHUT_WR);
2110 
2111   ngtcp2_conn_del(conn);
2112 
2113   /* STOP_SENDING against local bidirectional stream which has not
2114      been initiated. */
2115   setup_default_server(&conn);
2116 
2117   fr.type = NGTCP2_FRAME_STOP_SENDING;
2118   fr.stop_sending.stream_id = 1;
2119   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
2120 
2121   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
2122                                   conn->pktns.crypto.rx.ckm);
2123   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2124 
2125   CU_ASSERT(NGTCP2_ERR_STREAM_STATE == rv);
2126 
2127   ngtcp2_conn_del(conn);
2128 
2129   /* Receiving STOP_SENDING for a local unidirectional stream */
2130   setup_default_server(&conn);
2131 
2132   rv = ngtcp2_conn_open_uni_stream(conn, &stream_id, NULL);
2133 
2134   CU_ASSERT(0 == rv);
2135 
2136   fr.type = NGTCP2_FRAME_STOP_SENDING;
2137   fr.stop_sending.stream_id = stream_id;
2138   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
2139 
2140   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
2141                                   conn->pktns.crypto.rx.ckm);
2142   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2143 
2144   CU_ASSERT(0 == rv);
2145   CU_ASSERT(NGTCP2_FRAME_RESET_STREAM == conn->pktns.tx.frq->fr.type);
2146 
2147   ngtcp2_conn_del(conn);
2148 
2149   /* STOP_SENDING against local unidirectional stream which has not
2150      been initiated. */
2151   setup_default_server(&conn);
2152 
2153   fr.type = NGTCP2_FRAME_STOP_SENDING;
2154   fr.stop_sending.stream_id = 3;
2155   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
2156 
2157   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
2158                                   conn->pktns.crypto.rx.ckm);
2159   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2160 
2161   CU_ASSERT(NGTCP2_ERR_STREAM_STATE == rv);
2162 
2163   ngtcp2_conn_del(conn);
2164 
2165   /* STOP_SENDING against local bidirectional stream in Data Sent
2166      state.  Because all data have been acknowledged, and FIN is sent,
2167      RESET_STREAM is not necessary. */
2168   setup_default_client(&conn);
2169 
2170   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2171 
2172   CU_ASSERT(0 == rv);
2173 
2174   strm = ngtcp2_conn_find_stream(conn, stream_id);
2175   ngtcp2_strm_shutdown(strm, NGTCP2_STRM_FLAG_SHUT_WR);
2176 
2177   fr.type = NGTCP2_FRAME_STOP_SENDING;
2178   fr.stop_sending.stream_id = stream_id;
2179   fr.stop_sending.app_error_code = NGTCP2_APP_ERR01;
2180 
2181   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
2182                                   conn->pktns.crypto.rx.ckm);
2183   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2184 
2185   CU_ASSERT(0 == rv);
2186   CU_ASSERT(NULL == conn->pktns.tx.frq);
2187 
2188   ngtcp2_conn_del(conn);
2189 }
2190 
test_ngtcp2_conn_recv_conn_id_omitted(void)2191 void test_ngtcp2_conn_recv_conn_id_omitted(void) {
2192   ngtcp2_conn *conn;
2193   int rv;
2194   uint8_t buf[2048];
2195   ngtcp2_frame fr;
2196   size_t pktlen;
2197   ngtcp2_ksl_it it;
2198   ngtcp2_scid *scid;
2199 
2200   fr.type = NGTCP2_FRAME_STREAM;
2201   fr.stream.flags = 0;
2202   fr.stream.stream_id = 4;
2203   fr.stream.fin = 0;
2204   fr.stream.offset = 0;
2205   fr.stream.datacnt = 1;
2206   fr.stream.data[0].len = 100;
2207   fr.stream.data[0].base = null_data;
2208 
2209   /* Receiving packet which has no connection ID while SCID of server
2210      is not empty. */
2211   setup_default_server(&conn);
2212 
2213   pktlen = write_single_frame_pkt_without_conn_id(buf, sizeof(buf), 1, &fr,
2214                                                   conn->pktns.crypto.rx.ckm);
2215   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2216 
2217   /* packet is just ignored */
2218   CU_ASSERT(0 == rv);
2219   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, 4));
2220 
2221   ngtcp2_conn_del(conn);
2222 
2223   /* Allow omission of connection ID */
2224   setup_default_server(&conn);
2225   ngtcp2_cid_zero(&conn->oscid);
2226 
2227   it = ngtcp2_ksl_begin(&conn->scid.set);
2228   scid = ngtcp2_ksl_it_get(&it);
2229   ngtcp2_cid_zero(&scid->cid);
2230 
2231   pktlen = write_single_frame_pkt_without_conn_id(buf, sizeof(buf), 1, &fr,
2232                                                   conn->pktns.crypto.rx.ckm);
2233   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2234 
2235   CU_ASSERT(0 == rv);
2236   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
2237 
2238   ngtcp2_conn_del(conn);
2239 }
2240 
test_ngtcp2_conn_short_pkt_type(void)2241 void test_ngtcp2_conn_short_pkt_type(void) {
2242   ngtcp2_conn *conn;
2243   ngtcp2_pkt_hd hd;
2244   uint8_t buf[2048];
2245   ngtcp2_ssize spktlen;
2246   int64_t stream_id;
2247 
2248   /* 1 octet pkt num */
2249   setup_default_client(&conn);
2250 
2251   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2252   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2253                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2254                                      null_data, 19, 1);
2255 
2256   CU_ASSERT(spktlen > 0);
2257   CU_ASSERT(pkt_decode_hd_short_mask(&hd, buf, (size_t)spktlen,
2258                                      conn->oscid.datalen) > 0);
2259   CU_ASSERT(1 == hd.pkt_numlen);
2260 
2261   ngtcp2_conn_del(conn);
2262 
2263   /* 2 octets pkt num */
2264   setup_default_client(&conn);
2265   conn->pktns.rtb.largest_acked_tx_pkt_num = 0x6afa2f;
2266   conn->pktns.tx.last_pkt_num = 0x6afd78;
2267 
2268   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2269   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2270                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2271                                      null_data, 19, 1);
2272 
2273   CU_ASSERT(spktlen > 0);
2274   CU_ASSERT(pkt_decode_hd_short_mask(&hd, buf, (size_t)spktlen,
2275                                      conn->oscid.datalen) > 0);
2276   CU_ASSERT(2 == hd.pkt_numlen);
2277 
2278   ngtcp2_conn_del(conn);
2279 
2280   /* 4 octets pkt num */
2281   setup_default_client(&conn);
2282   conn->pktns.rtb.largest_acked_tx_pkt_num = 0x6afa2f;
2283   conn->pktns.tx.last_pkt_num = 0x6bc106;
2284 
2285   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2286   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2287                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2288                                      null_data, 19, 1);
2289 
2290   CU_ASSERT(spktlen > 0);
2291   CU_ASSERT(pkt_decode_hd_short_mask(&hd, buf, (size_t)spktlen,
2292                                      conn->oscid.datalen) > 0);
2293   CU_ASSERT(3 == hd.pkt_numlen);
2294 
2295   ngtcp2_conn_del(conn);
2296 
2297   /* 1 octet pkt num (largest)*/
2298   setup_default_client(&conn);
2299   conn->pktns.rtb.largest_acked_tx_pkt_num = 1;
2300   conn->pktns.tx.last_pkt_num = 128;
2301 
2302   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2303   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2304                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2305                                      null_data, 19, 1);
2306 
2307   CU_ASSERT(spktlen > 0);
2308   CU_ASSERT(pkt_decode_hd_short_mask(&hd, buf, (size_t)spktlen,
2309                                      conn->oscid.datalen) > 0);
2310   CU_ASSERT(1 == hd.pkt_numlen);
2311 
2312   ngtcp2_conn_del(conn);
2313 
2314   /* 2 octet pkt num (shortest)*/
2315   setup_default_client(&conn);
2316   conn->pktns.rtb.largest_acked_tx_pkt_num = 1;
2317   conn->pktns.tx.last_pkt_num = 129;
2318 
2319   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2320   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2321                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2322                                      null_data, 19, 1);
2323 
2324   CU_ASSERT(spktlen > 0);
2325   CU_ASSERT(pkt_decode_hd_short_mask(&hd, buf, (size_t)spktlen,
2326                                      conn->oscid.datalen) > 0);
2327   CU_ASSERT(2 == hd.pkt_numlen);
2328 
2329   ngtcp2_conn_del(conn);
2330 
2331   /* 2 octet pkt num (largest)*/
2332   setup_default_client(&conn);
2333   conn->pktns.rtb.largest_acked_tx_pkt_num = 1;
2334   conn->pktns.tx.last_pkt_num = 32768;
2335 
2336   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2337   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2338                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2339                                      null_data, 19, 1);
2340 
2341   CU_ASSERT(spktlen > 0);
2342   CU_ASSERT(
2343       pkt_decode_hd_short(&hd, buf, (size_t)spktlen, conn->oscid.datalen) > 0);
2344   CU_ASSERT(2 == hd.pkt_numlen);
2345 
2346   ngtcp2_conn_del(conn);
2347 
2348   /* 3 octet pkt num (shortest) */
2349   setup_default_client(&conn);
2350   conn->pktns.rtb.largest_acked_tx_pkt_num = 1;
2351   conn->pktns.tx.last_pkt_num = 32769;
2352 
2353   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2354   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2355                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2356                                      null_data, 19, 1);
2357 
2358   CU_ASSERT(spktlen > 0);
2359   CU_ASSERT(
2360       pkt_decode_hd_short(&hd, buf, (size_t)spktlen, conn->oscid.datalen) > 0);
2361   CU_ASSERT(3 == hd.pkt_numlen);
2362 
2363   ngtcp2_conn_del(conn);
2364 
2365   /* 3 octet pkt num (largest) */
2366   setup_default_client(&conn);
2367   conn->pktns.rtb.largest_acked_tx_pkt_num = 1;
2368   conn->pktns.tx.last_pkt_num = 8388608;
2369 
2370   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2371   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2372                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2373                                      null_data, 19, 1);
2374 
2375   CU_ASSERT(spktlen > 0);
2376   CU_ASSERT(
2377       pkt_decode_hd_short(&hd, buf, (size_t)spktlen, conn->oscid.datalen) > 0);
2378   CU_ASSERT(3 == hd.pkt_numlen);
2379 
2380   ngtcp2_conn_del(conn);
2381 
2382   /* 4 octet pkt num (shortest)*/
2383   setup_default_client(&conn);
2384   conn->pktns.rtb.largest_acked_tx_pkt_num = 1;
2385   conn->pktns.tx.last_pkt_num = 8388609;
2386 
2387   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2388   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2389                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2390                                      null_data, 19, 1);
2391 
2392   CU_ASSERT(spktlen > 0);
2393   CU_ASSERT(
2394       pkt_decode_hd_short(&hd, buf, (size_t)spktlen, conn->oscid.datalen) > 0);
2395   CU_ASSERT(4 == hd.pkt_numlen);
2396 
2397   ngtcp2_conn_del(conn);
2398 
2399   /* Overflow */
2400   setup_default_client(&conn);
2401   conn->pktns.rtb.largest_acked_tx_pkt_num = 1;
2402   conn->pktns.tx.last_pkt_num = NGTCP2_MAX_PKT_NUM - 1;
2403 
2404   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2405   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2406                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2407                                      null_data, 19, 1);
2408 
2409   CU_ASSERT(spktlen > 0);
2410   CU_ASSERT(
2411       pkt_decode_hd_short(&hd, buf, (size_t)spktlen, conn->oscid.datalen) > 0);
2412   CU_ASSERT(4 == hd.pkt_numlen);
2413 
2414   ngtcp2_conn_del(conn);
2415 }
2416 
test_ngtcp2_conn_recv_stateless_reset(void)2417 void test_ngtcp2_conn_recv_stateless_reset(void) {
2418   ngtcp2_conn *conn;
2419   uint8_t buf[256];
2420   ngtcp2_ssize spktlen;
2421   int rv;
2422   size_t i;
2423   uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN];
2424 
2425   for (i = 0; i < NGTCP2_STATELESS_RESET_TOKENLEN; ++i) {
2426     token[i] = (uint8_t)~i;
2427   }
2428 
2429   /* server */
2430   setup_default_server(&conn);
2431   conn->callbacks.decrypt = fail_decrypt;
2432   conn->pktns.rx.max_pkt_num = 24324325;
2433 
2434   ngtcp2_dcid_set_token(&conn->dcid.current, token);
2435 
2436   spktlen = ngtcp2_pkt_write_stateless_reset(
2437       buf, sizeof(buf), token, null_data, NGTCP2_MIN_STATELESS_RESET_RANDLEN);
2438 
2439   CU_ASSERT(spktlen > 0);
2440 
2441   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2442                             (size_t)spktlen, 1);
2443 
2444   CU_ASSERT(NGTCP2_ERR_DRAINING == rv);
2445   CU_ASSERT(NGTCP2_CS_DRAINING == conn->state);
2446 
2447   ngtcp2_conn_del(conn);
2448 
2449   /* client */
2450   setup_default_client(&conn);
2451   conn->callbacks.decrypt = fail_decrypt;
2452   conn->pktns.rx.max_pkt_num = 3255454;
2453 
2454   ngtcp2_dcid_set_token(&conn->dcid.current, token);
2455 
2456   spktlen =
2457       ngtcp2_pkt_write_stateless_reset(buf, sizeof(buf), token, null_data, 29);
2458 
2459   CU_ASSERT(spktlen > 0);
2460 
2461   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2462                             (size_t)spktlen, 1);
2463 
2464   CU_ASSERT(NGTCP2_ERR_DRAINING == rv);
2465   CU_ASSERT(NGTCP2_CS_DRAINING == conn->state);
2466 
2467   ngtcp2_conn_del(conn);
2468 
2469   /* stateless reset in long packet */
2470   setup_default_server(&conn);
2471   conn->callbacks.decrypt = fail_decrypt;
2472   conn->pktns.rx.max_pkt_num = 754233;
2473 
2474   ngtcp2_dcid_set_token(&conn->dcid.current, token);
2475 
2476   spktlen = ngtcp2_pkt_write_stateless_reset(
2477       buf, sizeof(buf), token, null_data, NGTCP2_MIN_STATELESS_RESET_RANDLEN);
2478 
2479   CU_ASSERT(spktlen > 0);
2480 
2481   /* long packet */
2482   buf[0] |= NGTCP2_HEADER_FORM_BIT;
2483 
2484   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2485                             (size_t)spktlen, 1);
2486 
2487   CU_ASSERT(NGTCP2_ERR_DRAINING == rv);
2488   CU_ASSERT(NGTCP2_CS_DRAINING == conn->state);
2489 
2490   ngtcp2_conn_del(conn);
2491 
2492   /* stateless reset in long packet; parsing long header fails */
2493   setup_default_server(&conn);
2494   conn->callbacks.decrypt = fail_decrypt;
2495   conn->pktns.rx.max_pkt_num = 754233;
2496 
2497   ngtcp2_dcid_set_token(&conn->dcid.current, token);
2498 
2499   spktlen = ngtcp2_pkt_write_stateless_reset(
2500       buf, 41, token, null_data, NGTCP2_MIN_STATELESS_RESET_RANDLEN);
2501 
2502   CU_ASSERT(spktlen > 0);
2503 
2504   /* long packet */
2505   buf[0] |= NGTCP2_HEADER_FORM_BIT;
2506   buf[0] |= 0x30;
2507   /* Make version nonzero so that it does not look like Version
2508      Negotiation packet */
2509   buf[1] = 0xff;
2510   /* Make largest CID so that ngtcp2_pkt_decode_hd_long fails */
2511   buf[5] = 0xff;
2512 
2513   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2514                             (size_t)spktlen, 1);
2515 
2516   CU_ASSERT(NGTCP2_ERR_DRAINING == rv);
2517   CU_ASSERT(NGTCP2_CS_DRAINING == conn->state);
2518 
2519   ngtcp2_conn_del(conn);
2520 
2521   /* token does not match */
2522   setup_default_client(&conn);
2523   conn->callbacks.decrypt = fail_decrypt;
2524   conn->pktns.rx.max_pkt_num = 24324325;
2525 
2526   spktlen =
2527       ngtcp2_pkt_write_stateless_reset(buf, sizeof(buf), token, null_data, 29);
2528 
2529   CU_ASSERT(spktlen > 0);
2530 
2531   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2532                             (size_t)spktlen, 1);
2533 
2534   CU_ASSERT(0 == rv);
2535   CU_ASSERT(NGTCP2_CS_DRAINING != conn->state);
2536 
2537   ngtcp2_conn_del(conn);
2538 }
2539 
test_ngtcp2_conn_recv_retry(void)2540 void test_ngtcp2_conn_recv_retry(void) {
2541   ngtcp2_conn *conn;
2542   uint8_t buf[2048];
2543   ngtcp2_ssize spktlen;
2544   uint64_t t = 0;
2545   ngtcp2_cid dcid;
2546   const uint8_t token[] = "address-validation-token";
2547   size_t i;
2548   int64_t stream_id;
2549   ngtcp2_ssize datalen;
2550   int rv;
2551   ngtcp2_vec datav;
2552   ngtcp2_strm *strm;
2553   ngtcp2_crypto_aead aead = {0};
2554   ngtcp2_crypto_aead_ctx aead_ctx = {0};
2555 
2556   dcid_init(&dcid);
2557   setup_handshake_client(&conn);
2558   conn->callbacks.recv_retry = recv_retry;
2559 
2560   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2561 
2562   CU_ASSERT(spktlen > 0);
2563 
2564   spktlen = ngtcp2_pkt_write_retry(
2565       buf, sizeof(buf), NGTCP2_PROTO_VER_MAX, &conn->oscid, &dcid,
2566       ngtcp2_conn_get_dcid(conn), token, strsize(token), null_encrypt, &aead,
2567       &aead_ctx);
2568 
2569   CU_ASSERT(spktlen > 0);
2570 
2571   for (i = 0; i < 2; ++i) {
2572     rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2573                               (size_t)spktlen, ++t);
2574 
2575     CU_ASSERT(0 == rv);
2576 
2577     spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2578 
2579     if (i == 1) {
2580       /* Retry packet was ignored */
2581       CU_ASSERT(spktlen == 0);
2582     } else {
2583       CU_ASSERT(spktlen > 0);
2584       CU_ASSERT(1 == conn->in_pktns->tx.last_pkt_num);
2585       CU_ASSERT(ngtcp2_cid_eq(&dcid, ngtcp2_conn_get_dcid(conn)));
2586       CU_ASSERT(conn->flags & NGTCP2_CONN_FLAG_RECV_RETRY);
2587     }
2588   }
2589 
2590   ngtcp2_conn_del(conn);
2591 
2592   /* Retry packet with non-matching tag is rejected */
2593   setup_handshake_client(&conn);
2594   conn->callbacks.recv_retry = recv_retry;
2595 
2596   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2597 
2598   CU_ASSERT(spktlen > 0);
2599 
2600   spktlen = ngtcp2_pkt_write_retry(
2601       buf, sizeof(buf), NGTCP2_PROTO_VER_MAX, &conn->oscid, &dcid,
2602       ngtcp2_conn_get_dcid(conn), token, strsize(token), null_encrypt, &aead,
2603       &aead_ctx);
2604 
2605   CU_ASSERT(spktlen > 0);
2606 
2607   /* Change tag */
2608   buf[spktlen - 1] = 1;
2609 
2610   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2611                             (size_t)spktlen, ++t);
2612 
2613   CU_ASSERT(0 == rv);
2614 
2615   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2616 
2617   CU_ASSERT(0 == spktlen);
2618 
2619   ngtcp2_conn_del(conn);
2620 
2621   /* Make sure that 0RTT packets are retransmitted */
2622   setup_early_client(&conn);
2623   conn->callbacks.recv_retry = recv_retry;
2624 
2625   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2626 
2627   CU_ASSERT(0 == rv);
2628 
2629   spktlen =
2630       ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, sizeof(buf), &datalen,
2631                                 NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2632                                 null_datav(&datav, 219), 1, ++t);
2633 
2634   CU_ASSERT(sizeof(buf) == spktlen);
2635   CU_ASSERT(219 == datalen);
2636 
2637   spktlen =
2638       ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, sizeof(buf), &datalen,
2639                                 NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2640                                 null_datav(&datav, 119), 1, ++t);
2641 
2642   CU_ASSERT(spktlen > 0);
2643   CU_ASSERT(119 == datalen);
2644 
2645   spktlen = ngtcp2_pkt_write_retry(
2646       buf, sizeof(buf), NGTCP2_PROTO_VER_MAX, &conn->oscid, &dcid,
2647       ngtcp2_conn_get_dcid(conn), token, strsize(token), null_encrypt, &aead,
2648       &aead_ctx);
2649 
2650   CU_ASSERT(spktlen > 0);
2651 
2652   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
2653                             (size_t)spktlen, ++t);
2654 
2655   CU_ASSERT(0 == rv);
2656 
2657   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2658 
2659   CU_ASSERT(spktlen > 219 + 119);
2660   CU_ASSERT(2 == conn->pktns.tx.last_pkt_num);
2661 
2662   strm = ngtcp2_conn_find_stream(conn, stream_id);
2663 
2664   CU_ASSERT(0 == ngtcp2_ksl_len(strm->tx.streamfrq));
2665 
2666   /* ngtcp2_conn_write_stream sends new 0RTT packet. */
2667   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
2668                                      &datalen, NGTCP2_WRITE_STREAM_FLAG_NONE,
2669                                      stream_id, null_data, 120, ++t);
2670 
2671   CU_ASSERT(spktlen > 0);
2672   CU_ASSERT(3 == conn->pktns.tx.last_pkt_num);
2673   CU_ASSERT(120 == datalen);
2674   CU_ASSERT(NULL == conn->pktns.tx.frq);
2675   CU_ASSERT(!ngtcp2_rtb_empty(&conn->pktns.rtb));
2676 
2677   ngtcp2_conn_del(conn);
2678 }
2679 
test_ngtcp2_conn_recv_delayed_handshake_pkt(void)2680 void test_ngtcp2_conn_recv_delayed_handshake_pkt(void) {
2681   ngtcp2_conn *conn;
2682   uint8_t buf[2048];
2683   size_t pktlen;
2684   ngtcp2_frame fr;
2685   int rv;
2686 
2687   setup_default_client(&conn);
2688 
2689   fr.type = NGTCP2_FRAME_CRYPTO;
2690   fr.crypto.offset = 0;
2691   fr.crypto.datacnt = 1;
2692   fr.crypto.data[0].len = 567;
2693   fr.crypto.data[0].base = null_data;
2694 
2695   pktlen = write_single_frame_handshake_pkt(
2696       buf, sizeof(buf), NGTCP2_PKT_HANDSHAKE, &conn->oscid,
2697       ngtcp2_conn_get_dcid(conn), 1, NGTCP2_PROTO_VER_MAX, &fr, &null_ckm);
2698   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2699 
2700   CU_ASSERT(0 == rv);
2701   CU_ASSERT(1 == ngtcp2_ksl_len(&conn->hs_pktns->acktr.ents));
2702 
2703   ngtcp2_conn_del(conn);
2704 }
2705 
test_ngtcp2_conn_recv_max_streams(void)2706 void test_ngtcp2_conn_recv_max_streams(void) {
2707   ngtcp2_conn *conn;
2708   uint8_t buf[2048];
2709   size_t pktlen;
2710   int rv;
2711   ngtcp2_frame fr;
2712 
2713   setup_default_client(&conn);
2714 
2715   fr.type = NGTCP2_FRAME_MAX_STREAMS_UNI;
2716   fr.max_streams.max_streams = 999;
2717 
2718   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
2719                                   conn->pktns.crypto.rx.ckm);
2720   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 1);
2721 
2722   CU_ASSERT(0 == rv);
2723   CU_ASSERT(999 == conn->local.uni.max_streams);
2724 
2725   fr.type = NGTCP2_FRAME_MAX_STREAMS_BIDI;
2726   fr.max_streams.max_streams = 997;
2727 
2728   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 2, &fr,
2729                                   conn->pktns.crypto.rx.ckm);
2730   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
2731 
2732   CU_ASSERT(0 == rv);
2733   CU_ASSERT(997 == conn->local.bidi.max_streams);
2734 
2735   ngtcp2_conn_del(conn);
2736 }
2737 
test_ngtcp2_conn_handshake(void)2738 void test_ngtcp2_conn_handshake(void) {
2739   ngtcp2_conn *conn;
2740   uint8_t buf[2048];
2741   size_t pktlen;
2742   ngtcp2_ssize spktlen;
2743   ngtcp2_frame fr;
2744   int64_t pkt_num = 12345689;
2745   ngtcp2_tstamp t = 0;
2746   ngtcp2_cid rcid;
2747   int rv;
2748   int64_t stream_id;
2749   ngtcp2_ssize nwrite;
2750   ngtcp2_crypto_aead_ctx aead_ctx = {0};
2751   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
2752   ngtcp2_crypto_ctx crypto_ctx;
2753 
2754   rcid_init(&rcid);
2755 
2756   /* Make sure server Initial is padded */
2757   setup_handshake_server(&conn);
2758   fr.type = NGTCP2_FRAME_CRYPTO;
2759   fr.crypto.offset = 0;
2760   fr.crypto.datacnt = 1;
2761   fr.crypto.data[0].len = 1200;
2762   fr.crypto.data[0].base = null_data;
2763 
2764   pktlen = write_single_frame_handshake_pkt(
2765       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
2766       ++pkt_num, conn->version, &fr, &null_ckm);
2767 
2768   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2769 
2770   CU_ASSERT(0 == rv);
2771 
2772   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2773 
2774   CU_ASSERT(spktlen >= 1200);
2775 
2776   ngtcp2_conn_del(conn);
2777 
2778   /* Make sure server Handshake is padded when ack-eliciting Initial
2779      is coalesced. */
2780   setup_handshake_server(&conn);
2781   fr.type = NGTCP2_FRAME_CRYPTO;
2782   fr.crypto.offset = 0;
2783   fr.crypto.datacnt = 1;
2784   fr.crypto.data[0].len = 1200;
2785   fr.crypto.data[0].base = null_data;
2786 
2787   pktlen = write_single_frame_handshake_pkt(
2788       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
2789       ++pkt_num, conn->version, &fr, &null_ckm);
2790 
2791   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2792 
2793   CU_ASSERT(0 == rv);
2794 
2795   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_HANDSHAKE, null_data,
2796                                  91);
2797 
2798   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2799 
2800   CU_ASSERT(spktlen >= 1200);
2801   CU_ASSERT(1 == ngtcp2_ksl_len(&conn->hs_pktns->rtb.ents));
2802 
2803   ngtcp2_conn_del(conn);
2804 
2805   /* Make sure that client packet is padded if it includes Initial and
2806      0RTT packets */
2807   setup_early_client(&conn);
2808 
2809   conn->callbacks.client_initial = client_initial_large_crypto_early_data;
2810 
2811   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2812 
2813   CU_ASSERT(0 == rv);
2814 
2815   /* First packet should only includes Initial.  No space for 0RTT. */
2816   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, 1280, &nwrite,
2817                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2818                                      null_data, 10, ++t);
2819 
2820   CU_ASSERT(1280 == spktlen);
2821   CU_ASSERT(-1 == nwrite);
2822 
2823   /* Second packet has a room for 0RTT. */
2824   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, 1280, &nwrite,
2825                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2826                                      null_data, 10, ++t);
2827 
2828   CU_ASSERT(1280 == spktlen);
2829   CU_ASSERT(10 == nwrite);
2830 
2831   /* We have no data to send. */
2832   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, 1280, ++t);
2833 
2834   CU_ASSERT(0 == spktlen);
2835 
2836   ngtcp2_conn_del(conn);
2837 
2838   /* Make sure that client non ack-eliciting Initial triggers
2839      padding. */
2840   setup_handshake_client(&conn);
2841 
2842   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2843 
2844   CU_ASSERT(spktlen >= 1200);
2845 
2846   fr.type = NGTCP2_FRAME_CRYPTO;
2847   fr.crypto.offset = 0;
2848   fr.crypto.datacnt = 1;
2849   fr.crypto.data[0].len = 1200;
2850   fr.crypto.data[0].base = null_data;
2851 
2852   pktlen = write_single_frame_handshake_pkt(
2853       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
2854       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
2855 
2856   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2857 
2858   CU_ASSERT(0 == rv);
2859 
2860   init_crypto_ctx(&crypto_ctx);
2861   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
2862   ngtcp2_conn_install_rx_handshake_key(conn, &aead_ctx, null_iv,
2863                                        sizeof(null_iv), &hp_ctx);
2864   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
2865                                        sizeof(null_iv), &hp_ctx);
2866 
2867   pktlen = write_single_frame_handshake_pkt(
2868       buf, sizeof(buf), NGTCP2_PKT_HANDSHAKE, &conn->oscid,
2869       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
2870 
2871   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2872 
2873   CU_ASSERT(0 == rv);
2874 
2875   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2876 
2877   CU_ASSERT(spktlen >= 1200);
2878 
2879   ngtcp2_conn_del(conn);
2880 }
2881 
test_ngtcp2_conn_handshake_error(void)2882 void test_ngtcp2_conn_handshake_error(void) {
2883   ngtcp2_conn *conn;
2884   uint8_t buf[2048];
2885   size_t pktlen;
2886   ngtcp2_ssize spktlen;
2887   ngtcp2_frame fr;
2888   int64_t pkt_num = 107;
2889   ngtcp2_tstamp t = 0;
2890   ngtcp2_cid rcid;
2891   int rv;
2892 
2893   rcid_init(&rcid);
2894 
2895   /* client side */
2896   setup_handshake_client(&conn);
2897   conn->callbacks.recv_crypto_data = recv_crypto_handshake_error;
2898   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2899 
2900   CU_ASSERT(spktlen > 0);
2901 
2902   fr.type = NGTCP2_FRAME_CRYPTO;
2903   fr.crypto.offset = 0;
2904   fr.crypto.datacnt = 1;
2905   fr.crypto.data[0].len = 333;
2906   fr.crypto.data[0].base = null_data;
2907 
2908   pktlen = write_single_frame_handshake_pkt(
2909       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
2910       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
2911 
2912   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2913 
2914   CU_ASSERT(NGTCP2_ERR_CRYPTO == rv);
2915 
2916   ngtcp2_conn_del(conn);
2917 
2918   /* server side */
2919   setup_handshake_server(&conn);
2920   conn->callbacks.recv_crypto_data = recv_crypto_handshake_error;
2921 
2922   fr.type = NGTCP2_FRAME_CRYPTO;
2923   fr.crypto.offset = 0;
2924   fr.crypto.datacnt = 1;
2925   fr.crypto.data[0].len = 551;
2926   fr.crypto.data[0].base = null_data;
2927 
2928   pktlen = write_single_frame_handshake_pkt(
2929       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
2930       ++pkt_num, conn->version, &fr, &null_ckm);
2931 
2932   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2933 
2934   CU_ASSERT(NGTCP2_ERR_CRYPTO == rv);
2935 
2936   ngtcp2_conn_del(conn);
2937 
2938   /* server side; wrong version */
2939   setup_handshake_server(&conn);
2940   conn->callbacks.recv_crypto_data = recv_crypto_handshake_error;
2941 
2942   fr.type = NGTCP2_FRAME_CRYPTO;
2943   fr.crypto.offset = 0;
2944   fr.crypto.datacnt = 1;
2945   fr.crypto.data[0].len = 551;
2946   fr.crypto.data[0].base = null_data;
2947 
2948   pktlen = write_single_frame_handshake_pkt(
2949       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
2950       ++pkt_num, 0xffff, &fr, &null_ckm);
2951 
2952   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
2953 
2954   CU_ASSERT(NGTCP2_ERR_DROP_CONN == rv);
2955 
2956   ngtcp2_conn_del(conn);
2957 }
2958 
test_ngtcp2_conn_retransmit_protected(void)2959 void test_ngtcp2_conn_retransmit_protected(void) {
2960   ngtcp2_conn *conn;
2961   uint8_t buf[2048];
2962   ngtcp2_ssize spktlen;
2963   ngtcp2_tstamp t = 0;
2964   int64_t stream_id, stream_id_a, stream_id_b;
2965   ngtcp2_ksl_it it;
2966   ngtcp2_frame fr;
2967   size_t pktlen;
2968   ngtcp2_vec datav;
2969   int accepted;
2970   int rv;
2971 
2972   /* Retransmit a packet completely */
2973   setup_default_client(&conn);
2974 
2975   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
2976   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
2977                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
2978                                      null_data, 126, ++t);
2979 
2980   CU_ASSERT(spktlen > 0);
2981 
2982   /* Kick delayed ACK timer */
2983   t += NGTCP2_SECONDS;
2984 
2985   conn->pktns.tx.last_pkt_num = 1000000009;
2986   conn->pktns.rtb.largest_acked_tx_pkt_num = 1000000007;
2987   it = ngtcp2_rtb_head(&conn->pktns.rtb);
2988   ngtcp2_conn_detect_lost_pkt(conn, &conn->pktns, &conn->cstat, ++t);
2989   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
2990 
2991   CU_ASSERT(spktlen > 0);
2992   CU_ASSERT(NULL == conn->pktns.tx.frq);
2993 
2994   it = ngtcp2_rtb_head(&conn->pktns.rtb);
2995 
2996   CU_ASSERT(!ngtcp2_ksl_it_end(&it));
2997 
2998   ngtcp2_conn_del(conn);
2999 
3000   /* Retransmission takes place per frame basis. */
3001   setup_default_client(&conn);
3002   conn->local.bidi.max_streams = 3;
3003 
3004   ngtcp2_conn_open_bidi_stream(conn, &stream_id_a, NULL);
3005   ngtcp2_conn_open_bidi_stream(conn, &stream_id_b, NULL);
3006 
3007   ngtcp2_conn_shutdown_stream_write(conn, stream_id_a, NGTCP2_APP_ERR01);
3008   ngtcp2_conn_shutdown_stream_write(conn, stream_id_b, NGTCP2_APP_ERR01);
3009 
3010   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
3011 
3012   CU_ASSERT(spktlen > 0);
3013 
3014   /* Kick delayed ACK timer */
3015   t += NGTCP2_SECONDS;
3016 
3017   conn->pktns.tx.last_pkt_num = 1000000009;
3018   conn->pktns.rtb.largest_acked_tx_pkt_num = 1000000007;
3019   it = ngtcp2_rtb_head(&conn->pktns.rtb);
3020   ngtcp2_conn_detect_lost_pkt(conn, &conn->pktns, &conn->cstat, ++t);
3021   spktlen =
3022       ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, (size_t)(spktlen - 1), ++t);
3023 
3024   CU_ASSERT(spktlen > 0);
3025 
3026   it = ngtcp2_rtb_head(&conn->pktns.rtb);
3027 
3028   CU_ASSERT(!ngtcp2_ksl_it_end(&it));
3029   CU_ASSERT(NULL != conn->pktns.tx.frq);
3030 
3031   ngtcp2_conn_del(conn);
3032 
3033   /* DATAGRAM frame must not be retransmitted */
3034   setup_default_client(&conn);
3035 
3036   conn->callbacks.ack_datagram = ack_datagram;
3037   conn->remote.transport_params.max_datagram_frame_size = 65535;
3038 
3039   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
3040   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
3041 
3042   CU_ASSERT(spktlen > 0);
3043 
3044   fr.type = NGTCP2_FRAME_ACK;
3045   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
3046   fr.ack.ack_delay = 0;
3047   fr.ack.first_ack_blklen = 0;
3048   fr.ack.num_blks = 0;
3049 
3050   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
3051                                   conn->pktns.crypto.rx.ckm);
3052   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3053 
3054   CU_ASSERT(0 == rv);
3055 
3056   datav.base = null_data;
3057   datav.len = 99;
3058 
3059   spktlen = ngtcp2_conn_writev_datagram(
3060       conn, NULL, NULL, buf, sizeof(buf), &accepted,
3061       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 1000000009, &datav, 1, ++t);
3062 
3063   CU_ASSERT(spktlen > 0);
3064 
3065   /* Kick delayed ACK timer */
3066   t += NGTCP2_SECONDS;
3067 
3068   conn->pktns.tx.last_pkt_num = 1000000009;
3069   conn->pktns.rtb.largest_acked_tx_pkt_num = 1000000007;
3070   it = ngtcp2_rtb_head(&conn->pktns.rtb);
3071   ngtcp2_conn_detect_lost_pkt(conn, &conn->pktns, &conn->cstat, ++t);
3072   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
3073 
3074   CU_ASSERT(0 == spktlen);
3075   CU_ASSERT(NULL == conn->pktns.tx.frq);
3076 
3077   it = ngtcp2_rtb_head(&conn->pktns.rtb);
3078 
3079   CU_ASSERT(!ngtcp2_ksl_it_end(&it));
3080 
3081   ngtcp2_conn_del(conn);
3082 
3083   /* Retransmit an empty STREAM frame */
3084   setup_default_client(&conn);
3085 
3086   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
3087 
3088   CU_ASSERT(spktlen > 0);
3089 
3090   fr.type = NGTCP2_FRAME_ACK;
3091   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
3092   fr.ack.ack_delay = 0;
3093   fr.ack.first_ack_blklen = 0;
3094   fr.ack.num_blks = 0;
3095 
3096   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
3097                                   conn->pktns.crypto.rx.ckm);
3098   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3099 
3100   CU_ASSERT(0 == rv);
3101 
3102   ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
3103   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf), NULL,
3104                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
3105                                      NULL, 0, ++t);
3106 
3107   CU_ASSERT(spktlen > 0);
3108 
3109   /* Kick delayed ACK timer */
3110   t += NGTCP2_SECONDS;
3111 
3112   conn->pktns.tx.last_pkt_num = 1000000009;
3113   conn->pktns.rtb.largest_acked_tx_pkt_num = 1000000007;
3114   it = ngtcp2_rtb_head(&conn->pktns.rtb);
3115   ngtcp2_conn_detect_lost_pkt(conn, &conn->pktns, &conn->cstat, ++t);
3116   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
3117 
3118   CU_ASSERT(spktlen > 0);
3119   CU_ASSERT(NULL == conn->pktns.tx.frq);
3120 
3121   it = ngtcp2_rtb_head(&conn->pktns.rtb);
3122 
3123   CU_ASSERT(!ngtcp2_ksl_it_end(&it));
3124 
3125   ngtcp2_conn_del(conn);
3126 }
3127 
test_ngtcp2_conn_send_max_stream_data(void)3128 void test_ngtcp2_conn_send_max_stream_data(void) {
3129   ngtcp2_conn *conn;
3130   uint8_t buf[2048];
3131   size_t pktlen;
3132   ngtcp2_strm *strm;
3133   int64_t pkt_num = 890;
3134   ngtcp2_tstamp t = 0;
3135   ngtcp2_frame fr;
3136   int rv;
3137   const uint32_t datalen = 1024;
3138 
3139   /* MAX_STREAM_DATA should be sent */
3140   setup_default_server(&conn);
3141   conn->local.transport_params.initial_max_stream_data_bidi_remote = datalen;
3142 
3143   fr.type = NGTCP2_FRAME_STREAM;
3144   fr.stream.stream_id = 4;
3145   fr.stream.fin = 0;
3146   fr.stream.offset = 0;
3147   fr.stream.datacnt = 1;
3148   fr.stream.data[0].len = datalen;
3149   fr.stream.data[0].base = null_data;
3150 
3151   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3152                                   &fr, conn->pktns.crypto.rx.ckm);
3153 
3154   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3155 
3156   CU_ASSERT(0 == rv);
3157 
3158   rv = ngtcp2_conn_extend_max_stream_offset(conn, 4, datalen);
3159 
3160   CU_ASSERT(0 == rv);
3161 
3162   strm = ngtcp2_conn_find_stream(conn, 4);
3163 
3164   CU_ASSERT(ngtcp2_strm_is_tx_queued(strm));
3165 
3166   ngtcp2_conn_del(conn);
3167 
3168   /* MAX_STREAM_DATA should not be sent on incoming fin */
3169   setup_default_server(&conn);
3170   conn->local.transport_params.initial_max_stream_data_bidi_remote = datalen;
3171 
3172   fr.type = NGTCP2_FRAME_STREAM;
3173   fr.stream.stream_id = 4;
3174   fr.stream.fin = 1;
3175   fr.stream.offset = 0;
3176   fr.stream.datacnt = 1;
3177   fr.stream.data[0].len = datalen;
3178   fr.stream.data[0].base = null_data;
3179 
3180   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3181                                   &fr, conn->pktns.crypto.rx.ckm);
3182 
3183   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3184 
3185   CU_ASSERT(0 == rv);
3186 
3187   rv = ngtcp2_conn_extend_max_stream_offset(conn, 4, datalen);
3188 
3189   CU_ASSERT(0 == rv);
3190 
3191   strm = ngtcp2_conn_find_stream(conn, 4);
3192 
3193   CU_ASSERT(!ngtcp2_strm_is_tx_queued(strm));
3194 
3195   ngtcp2_conn_del(conn);
3196 
3197   /* MAX_STREAM_DATA should not be sent if STOP_SENDING frame is being
3198      sent by local endpoint */
3199   setup_default_server(&conn);
3200   conn->local.transport_params.initial_max_stream_data_bidi_remote = datalen;
3201 
3202   fr.type = NGTCP2_FRAME_STREAM;
3203   fr.stream.stream_id = 4;
3204   fr.stream.fin = 0;
3205   fr.stream.offset = 0;
3206   fr.stream.datacnt = 1;
3207   fr.stream.data[0].len = datalen;
3208   fr.stream.data[0].base = null_data;
3209 
3210   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3211                                   &fr, conn->pktns.crypto.rx.ckm);
3212 
3213   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3214 
3215   CU_ASSERT(0 == rv);
3216 
3217   rv = ngtcp2_conn_shutdown_stream_read(conn, 4, NGTCP2_APP_ERR01);
3218 
3219   CU_ASSERT(0 == rv);
3220 
3221   rv = ngtcp2_conn_extend_max_stream_offset(conn, 4, datalen);
3222 
3223   CU_ASSERT(0 == rv);
3224 
3225   strm = ngtcp2_conn_find_stream(conn, 4);
3226 
3227   CU_ASSERT(!ngtcp2_strm_is_tx_queued(strm));
3228 
3229   ngtcp2_conn_del(conn);
3230 
3231   /* MAX_STREAM_DATA should not be sent if stream is being reset by
3232      remote endpoint */
3233   setup_default_server(&conn);
3234   conn->local.transport_params.initial_max_stream_data_bidi_remote = datalen;
3235 
3236   fr.type = NGTCP2_FRAME_STREAM;
3237   fr.stream.stream_id = 4;
3238   fr.stream.fin = 0;
3239   fr.stream.offset = 0;
3240   fr.stream.datacnt = 1;
3241   fr.stream.data[0].len = datalen;
3242   fr.stream.data[0].base = null_data;
3243 
3244   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3245                                   &fr, conn->pktns.crypto.rx.ckm);
3246 
3247   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3248 
3249   CU_ASSERT(0 == rv);
3250 
3251   fr.type = NGTCP2_FRAME_RESET_STREAM;
3252   fr.reset_stream.stream_id = 4;
3253   fr.reset_stream.app_error_code = NGTCP2_APP_ERR01;
3254   fr.reset_stream.final_size = datalen;
3255 
3256   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3257                                   &fr, conn->pktns.crypto.rx.ckm);
3258 
3259   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3260 
3261   CU_ASSERT(0 == rv);
3262 
3263   rv = ngtcp2_conn_extend_max_stream_offset(conn, 4, datalen);
3264 
3265   CU_ASSERT(0 == rv);
3266   CU_ASSERT(ngtcp2_pq_empty(&conn->tx.strmq));
3267 
3268   ngtcp2_conn_del(conn);
3269 }
3270 
test_ngtcp2_conn_recv_stream_data(void)3271 void test_ngtcp2_conn_recv_stream_data(void) {
3272   uint8_t buf[1024];
3273   ngtcp2_conn *conn;
3274   my_user_data ud;
3275   int64_t pkt_num = 612;
3276   ngtcp2_tstamp t = 0;
3277   ngtcp2_frame fr;
3278   size_t pktlen;
3279   int rv;
3280   int64_t stream_id;
3281   size_t i;
3282 
3283   /* 2 STREAM frames are received in the correct order. */
3284   setup_default_server(&conn);
3285   conn->callbacks.recv_stream_data = recv_stream_data;
3286   conn->user_data = &ud;
3287 
3288   fr.type = NGTCP2_FRAME_STREAM;
3289   fr.stream.stream_id = 4;
3290   fr.stream.fin = 0;
3291   fr.stream.offset = 0;
3292   fr.stream.datacnt = 1;
3293   fr.stream.data[0].len = 111;
3294   fr.stream.data[0].base = null_data;
3295 
3296   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3297                                   &fr, conn->pktns.crypto.rx.ckm);
3298 
3299   memset(&ud, 0, sizeof(ud));
3300   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3301 
3302   CU_ASSERT(0 == rv);
3303   CU_ASSERT(4 == ud.stream_data.stream_id);
3304   CU_ASSERT(!(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN));
3305   CU_ASSERT(!(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_EARLY));
3306   CU_ASSERT(111 == ud.stream_data.datalen);
3307 
3308   fr.type = NGTCP2_FRAME_STREAM;
3309   fr.stream.stream_id = 4;
3310   fr.stream.fin = 1;
3311   fr.stream.offset = 111;
3312   fr.stream.datacnt = 1;
3313   fr.stream.data[0].len = 99;
3314   fr.stream.data[0].base = null_data;
3315 
3316   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3317                                   &fr, conn->pktns.crypto.rx.ckm);
3318 
3319   memset(&ud, 0, sizeof(ud));
3320   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3321 
3322   CU_ASSERT(0 == rv);
3323   CU_ASSERT(4 == ud.stream_data.stream_id);
3324   CU_ASSERT(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN);
3325   CU_ASSERT(99 == ud.stream_data.datalen);
3326 
3327   ngtcp2_conn_del(conn);
3328 
3329   /* 2 STREAM frames are received in the correct order, and 2nd STREAM
3330      frame has 0 length, and FIN bit set. */
3331   setup_default_server(&conn);
3332   conn->callbacks.recv_stream_data = recv_stream_data;
3333   conn->user_data = &ud;
3334 
3335   fr.type = NGTCP2_FRAME_STREAM;
3336   fr.stream.stream_id = 4;
3337   fr.stream.fin = 0;
3338   fr.stream.offset = 0;
3339   fr.stream.datacnt = 1;
3340   fr.stream.data[0].len = 111;
3341   fr.stream.data[0].base = null_data;
3342 
3343   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3344                                   &fr, conn->pktns.crypto.rx.ckm);
3345 
3346   memset(&ud, 0, sizeof(ud));
3347   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3348 
3349   CU_ASSERT(0 == rv);
3350   CU_ASSERT(4 == ud.stream_data.stream_id);
3351   CU_ASSERT(!(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN));
3352   CU_ASSERT(111 == ud.stream_data.datalen);
3353 
3354   fr.type = NGTCP2_FRAME_STREAM;
3355   fr.stream.stream_id = 4;
3356   fr.stream.fin = 1;
3357   fr.stream.offset = 111;
3358   fr.stream.datacnt = 0;
3359 
3360   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3361                                   &fr, conn->pktns.crypto.rx.ckm);
3362 
3363   memset(&ud, 0, sizeof(ud));
3364   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3365 
3366   CU_ASSERT(0 == rv);
3367   CU_ASSERT(4 == ud.stream_data.stream_id);
3368   CU_ASSERT(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN);
3369   CU_ASSERT(0 == ud.stream_data.datalen);
3370 
3371   ngtcp2_conn_del(conn);
3372 
3373   /* 2 identical STREAM frames with FIN bit set are received.  The
3374      recv_stream_data callback should not be called for sencond STREAM
3375      frame. */
3376   setup_default_server(&conn);
3377   conn->callbacks.recv_stream_data = recv_stream_data;
3378   conn->user_data = &ud;
3379 
3380   fr.type = NGTCP2_FRAME_STREAM;
3381   fr.stream.stream_id = 4;
3382   fr.stream.fin = 1;
3383   fr.stream.offset = 0;
3384   fr.stream.datacnt = 1;
3385   fr.stream.data[0].len = 111;
3386   fr.stream.data[0].base = null_data;
3387 
3388   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3389                                   &fr, conn->pktns.crypto.rx.ckm);
3390 
3391   memset(&ud, 0, sizeof(ud));
3392   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3393 
3394   CU_ASSERT(0 == rv);
3395   CU_ASSERT(4 == ud.stream_data.stream_id);
3396   CU_ASSERT(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN);
3397   CU_ASSERT(111 == ud.stream_data.datalen);
3398 
3399   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3400                                   &fr, conn->pktns.crypto.rx.ckm);
3401 
3402   memset(&ud, 0, sizeof(ud));
3403   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3404 
3405   CU_ASSERT(0 == rv);
3406   CU_ASSERT(0 == ud.stream_data.stream_id);
3407   CU_ASSERT(!(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN));
3408   CU_ASSERT(0 == ud.stream_data.datalen);
3409 
3410   ngtcp2_conn_del(conn);
3411 
3412   /* Re-ordered STREAM frame; we first gets 0 length STREAM frame with
3413      FIN bit set. Then the remaining STREAM frame is received. */
3414   setup_default_server(&conn);
3415   conn->callbacks.recv_stream_data = recv_stream_data;
3416   conn->user_data = &ud;
3417 
3418   fr.type = NGTCP2_FRAME_STREAM;
3419   fr.stream.stream_id = 4;
3420   fr.stream.fin = 1;
3421   fr.stream.offset = 599;
3422   fr.stream.datacnt = 0;
3423 
3424   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3425                                   &fr, conn->pktns.crypto.rx.ckm);
3426 
3427   memset(&ud, 0, sizeof(ud));
3428   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3429 
3430   CU_ASSERT(0 == rv);
3431   CU_ASSERT(0 == ud.stream_data.stream_id);
3432 
3433   fr.type = NGTCP2_FRAME_STREAM;
3434   fr.stream.stream_id = 4;
3435   fr.stream.fin = 0;
3436   fr.stream.offset = 0;
3437   fr.stream.datacnt = 1;
3438   fr.stream.data[0].len = 599;
3439   fr.stream.data[0].base = null_data;
3440 
3441   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3442                                   &fr, conn->pktns.crypto.rx.ckm);
3443 
3444   memset(&ud, 0, sizeof(ud));
3445   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3446 
3447   CU_ASSERT(0 == rv);
3448   CU_ASSERT(4 == ud.stream_data.stream_id);
3449   CU_ASSERT(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN);
3450   CU_ASSERT(599 == ud.stream_data.datalen);
3451 
3452   ngtcp2_conn_del(conn);
3453 
3454   /* Simulate the case where packet is lost.  We first gets 0 length
3455      STREAM frame with FIN bit set.  Then the lost STREAM frame is
3456      retransmitted with FIN bit set is received. */
3457   setup_default_server(&conn);
3458   conn->callbacks.recv_stream_data = recv_stream_data;
3459   conn->user_data = &ud;
3460 
3461   fr.type = NGTCP2_FRAME_STREAM;
3462   fr.stream.stream_id = 4;
3463   fr.stream.fin = 1;
3464   fr.stream.offset = 599;
3465   fr.stream.datacnt = 0;
3466 
3467   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3468                                   &fr, conn->pktns.crypto.rx.ckm);
3469 
3470   memset(&ud, 0, sizeof(ud));
3471   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3472 
3473   CU_ASSERT(0 == rv);
3474   CU_ASSERT(0 == ud.stream_data.stream_id);
3475 
3476   fr.type = NGTCP2_FRAME_STREAM;
3477   fr.stream.stream_id = 4;
3478   fr.stream.fin = 1;
3479   fr.stream.offset = 0;
3480   fr.stream.datacnt = 1;
3481   fr.stream.data[0].len = 599;
3482   fr.stream.data[0].base = null_data;
3483 
3484   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3485                                   &fr, conn->pktns.crypto.rx.ckm);
3486 
3487   memset(&ud, 0, sizeof(ud));
3488   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3489 
3490   CU_ASSERT(0 == rv);
3491   CU_ASSERT(4 == ud.stream_data.stream_id);
3492   CU_ASSERT(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN);
3493   CU_ASSERT(599 == ud.stream_data.datalen);
3494 
3495   ngtcp2_conn_del(conn);
3496 
3497   /* Receive an unidirectional stream data */
3498   setup_default_client(&conn);
3499   conn->callbacks.recv_stream_data = recv_stream_data;
3500   conn->user_data = &ud;
3501 
3502   fr.type = NGTCP2_FRAME_STREAM;
3503   fr.stream.stream_id = 3;
3504   fr.stream.fin = 0;
3505   fr.stream.offset = 0;
3506   fr.stream.datacnt = 1;
3507   fr.stream.data[0].len = 911;
3508   fr.stream.data[0].base = null_data;
3509 
3510   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3511                                   &fr, conn->pktns.crypto.rx.ckm);
3512 
3513   memset(&ud, 0, sizeof(ud));
3514   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3515 
3516   CU_ASSERT(0 == rv);
3517   CU_ASSERT(3 == ud.stream_data.stream_id);
3518   CU_ASSERT(!(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN));
3519   CU_ASSERT(911 == ud.stream_data.datalen);
3520 
3521   ngtcp2_conn_del(conn);
3522 
3523   /* Receive an unidirectional stream which is beyond the limit. */
3524   setup_default_server(&conn);
3525   conn->callbacks.recv_stream_data = recv_stream_data;
3526   conn->remote.uni.max_streams = 0;
3527   conn->user_data = &ud;
3528 
3529   fr.type = NGTCP2_FRAME_STREAM;
3530   fr.stream.stream_id = 2;
3531   fr.stream.fin = 0;
3532   fr.stream.offset = 0;
3533   fr.stream.datacnt = 1;
3534   fr.stream.data[0].len = 911;
3535   fr.stream.data[0].base = null_data;
3536 
3537   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3538                                   &fr, conn->pktns.crypto.rx.ckm);
3539 
3540   memset(&ud, 0, sizeof(ud));
3541   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3542 
3543   CU_ASSERT(NGTCP2_ERR_STREAM_LIMIT == rv);
3544 
3545   ngtcp2_conn_del(conn);
3546 
3547   /* Receiving nonzero payload for an local unidirectional stream is a
3548      protocol violation. */
3549   setup_default_client(&conn);
3550 
3551   rv = ngtcp2_conn_open_uni_stream(conn, &stream_id, NULL);
3552 
3553   CU_ASSERT(0 == rv);
3554 
3555   fr.type = NGTCP2_FRAME_STREAM;
3556   fr.stream.stream_id = stream_id;
3557   fr.stream.fin = 0;
3558   fr.stream.offset = 0;
3559   fr.stream.datacnt = 1;
3560   fr.stream.data[0].len = 9;
3561   fr.stream.data[0].base = null_data;
3562 
3563   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3564                                   &fr, conn->pktns.crypto.rx.ckm);
3565 
3566   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3567 
3568   CU_ASSERT(NGTCP2_ERR_STREAM_STATE == rv);
3569 
3570   ngtcp2_conn_del(conn);
3571 
3572   /* DATA on crypto stream, and TLS alert is generated. */
3573   setup_default_server(&conn);
3574   conn->callbacks.recv_crypto_data = recv_crypto_fatal_alert_generated;
3575 
3576   fr.type = NGTCP2_FRAME_CRYPTO;
3577   fr.crypto.offset = 0;
3578   fr.crypto.datacnt = 1;
3579   fr.crypto.data[0].len = 139;
3580   fr.crypto.data[0].base = null_data;
3581 
3582   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3583                                   &fr, conn->pktns.crypto.rx.ckm);
3584 
3585   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3586 
3587   CU_ASSERT(NGTCP2_ERR_CRYPTO == rv);
3588 
3589   ngtcp2_conn_del(conn);
3590 
3591   /* 0 length STREAM frame is allowed */
3592   setup_default_server(&conn);
3593 
3594   fr.type = NGTCP2_FRAME_STREAM;
3595   fr.stream.stream_id = 4;
3596   fr.stream.fin = 0;
3597   fr.stream.offset = 0;
3598   fr.stream.datacnt = 0;
3599 
3600   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3601                                   &fr, conn->pktns.crypto.rx.ckm);
3602 
3603   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3604 
3605   CU_ASSERT(0 == rv);
3606   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
3607 
3608   ngtcp2_conn_del(conn);
3609 
3610   /* After sending STOP_SENDING, receiving 2 STREAM frames with fin
3611      bit set must not invoke recv_stream_data callback. */
3612   setup_default_server(&conn);
3613   conn->callbacks.recv_stream_data = recv_stream_data;
3614   conn->user_data = &ud;
3615 
3616   fr.type = NGTCP2_FRAME_STREAM;
3617   fr.stream.stream_id = 4;
3618   fr.stream.fin = 0;
3619   fr.stream.offset = 0;
3620   fr.stream.datacnt = 0;
3621 
3622   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3623                                   &fr, conn->pktns.crypto.rx.ckm);
3624 
3625   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3626 
3627   CU_ASSERT(0 == rv);
3628   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 4));
3629 
3630   rv = ngtcp2_conn_shutdown_stream_read(conn, 4, 99);
3631 
3632   CU_ASSERT(0 == rv);
3633 
3634   for (i = 0; i < 2; ++i) {
3635     fr.type = NGTCP2_FRAME_STREAM;
3636     fr.stream.stream_id = 4;
3637     fr.stream.fin = 1;
3638     fr.stream.offset = 0;
3639     fr.stream.datacnt = 1;
3640     fr.stream.data[0].base = null_data;
3641     fr.stream.data[0].len = 19;
3642 
3643     pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3644                                     &fr, conn->pktns.crypto.rx.ckm);
3645 
3646     ud.stream_data.stream_id = 0;
3647     rv =
3648         ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3649 
3650     CU_ASSERT(0 == rv);
3651     CU_ASSERT(0 == ud.stream_data.stream_id);
3652     CU_ASSERT(19 == conn->rx.offset);
3653     CU_ASSERT(19 == conn->rx.unsent_max_offset -
3654                         conn->local.transport_params.initial_max_data);
3655     CU_ASSERT(conn->local.transport_params.initial_max_data ==
3656               conn->rx.max_offset);
3657   }
3658 
3659   ngtcp2_conn_del(conn);
3660 
3661   /* After receiving RESET_STREAM, recv_stream_data callback must not
3662      be invoked */
3663   setup_default_server(&conn);
3664   conn->callbacks.recv_stream_data = recv_stream_data;
3665   conn->user_data = &ud;
3666 
3667   fr.type = NGTCP2_FRAME_STREAM;
3668   fr.stream.stream_id = 0;
3669   fr.stream.fin = 0;
3670   fr.stream.offset = 0;
3671   fr.stream.datacnt = 0;
3672 
3673   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3674                                   &fr, conn->pktns.crypto.rx.ckm);
3675   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3676 
3677   CU_ASSERT(0 == rv);
3678   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 0));
3679 
3680   fr.type = NGTCP2_FRAME_RESET_STREAM;
3681   fr.reset_stream.stream_id = 0;
3682   fr.reset_stream.app_error_code = 999;
3683   fr.reset_stream.final_size = 199;
3684 
3685   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3686                                   &fr, conn->pktns.crypto.rx.ckm);
3687   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3688 
3689   CU_ASSERT(0 == rv);
3690   CU_ASSERT(NULL != ngtcp2_conn_find_stream(conn, 0));
3691   CU_ASSERT(199 == conn->rx.unsent_max_offset -
3692                        conn->local.transport_params.initial_max_data);
3693 
3694   fr.type = NGTCP2_FRAME_STREAM;
3695   fr.stream.stream_id = 0;
3696   fr.stream.fin = 0;
3697   fr.stream.offset = 0;
3698   fr.stream.datacnt = 1;
3699   fr.stream.data[0].base = null_data;
3700   fr.stream.data[0].len = 198;
3701 
3702   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3703                                   &fr, conn->pktns.crypto.rx.ckm);
3704   ud.stream_data.stream_id = -1;
3705   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3706 
3707   CU_ASSERT(0 == rv);
3708   CU_ASSERT(-1 == ud.stream_data.stream_id);
3709   CU_ASSERT(199 == conn->rx.unsent_max_offset -
3710                        conn->local.transport_params.initial_max_data);
3711 
3712   fr.type = NGTCP2_FRAME_STREAM;
3713   fr.stream.stream_id = 0;
3714   fr.stream.fin = 1;
3715   fr.stream.offset = 198;
3716   fr.stream.datacnt = 1;
3717   fr.stream.data[0].base = null_data;
3718   fr.stream.data[0].len = 1;
3719 
3720   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3721                                   &fr, conn->pktns.crypto.rx.ckm);
3722   ud.stream_data.stream_id = -1;
3723   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3724 
3725   CU_ASSERT(0 == rv);
3726   CU_ASSERT(-1 == ud.stream_data.stream_id);
3727   CU_ASSERT(199 == conn->rx.unsent_max_offset -
3728                        conn->local.transport_params.initial_max_data);
3729 
3730   ngtcp2_conn_del(conn);
3731 
3732   /* ngtcp2_conn_shutdown_stream_read is called in recv_stream_data
3733      callback.  Further recv_stream_data callback must not be
3734      called. */
3735   setup_default_server(&conn);
3736   conn->callbacks.recv_stream_data = recv_stream_data_shutdown_stream_read;
3737   conn->user_data = &ud;
3738 
3739   fr.type = NGTCP2_FRAME_STREAM;
3740   fr.stream.stream_id = 4;
3741   fr.stream.fin = 0;
3742   fr.stream.offset = 599;
3743   fr.stream.datacnt = 1;
3744   fr.stream.data[0].len = 1;
3745   fr.stream.data[0].base = null_data;
3746 
3747   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3748                                   &fr, conn->pktns.crypto.rx.ckm);
3749 
3750   memset(&ud, 0, sizeof(ud));
3751   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3752 
3753   CU_ASSERT(0 == rv);
3754   CU_ASSERT(0 == ud.stream_data.stream_id);
3755 
3756   fr.type = NGTCP2_FRAME_STREAM;
3757   fr.stream.stream_id = 4;
3758   fr.stream.fin = 0;
3759   fr.stream.offset = 0;
3760   fr.stream.datacnt = 1;
3761   fr.stream.data[0].len = 599;
3762   fr.stream.data[0].base = null_data;
3763 
3764   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3765                                   &fr, conn->pktns.crypto.rx.ckm);
3766 
3767   memset(&ud, 0, sizeof(ud));
3768   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3769 
3770   CU_ASSERT(0 == rv);
3771   CU_ASSERT(4 == ud.stream_data.stream_id);
3772   CU_ASSERT(!(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN));
3773   CU_ASSERT(599 == ud.stream_data.datalen);
3774 
3775   ngtcp2_conn_del(conn);
3776 }
3777 
test_ngtcp2_conn_recv_ping(void)3778 void test_ngtcp2_conn_recv_ping(void) {
3779   uint8_t buf[1024];
3780   ngtcp2_conn *conn;
3781   int64_t pkt_num = 133;
3782   ngtcp2_tstamp t = 0;
3783   ngtcp2_frame fr;
3784   size_t pktlen;
3785   int rv;
3786 
3787   setup_default_client(&conn);
3788 
3789   fr.type = NGTCP2_FRAME_PING;
3790 
3791   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3792                                   &fr, conn->pktns.crypto.rx.ckm);
3793   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3794 
3795   CU_ASSERT(0 == rv);
3796   CU_ASSERT(NULL == conn->pktns.tx.frq);
3797 
3798   ngtcp2_conn_del(conn);
3799 }
3800 
test_ngtcp2_conn_recv_max_stream_data(void)3801 void test_ngtcp2_conn_recv_max_stream_data(void) {
3802   uint8_t buf[1024];
3803   ngtcp2_conn *conn;
3804   int64_t pkt_num = 1000000007;
3805   ngtcp2_tstamp t = 0;
3806   ngtcp2_frame fr;
3807   size_t pktlen;
3808   int rv;
3809   ngtcp2_strm *strm;
3810 
3811   /* Receiving MAX_STREAM_DATA to an uninitiated local bidirectional
3812      stream ID is an error */
3813   setup_default_client(&conn);
3814 
3815   fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
3816   fr.max_stream_data.stream_id = 4;
3817   fr.max_stream_data.max_stream_data = 8092;
3818 
3819   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3820                                   &fr, conn->pktns.crypto.rx.ckm);
3821 
3822   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3823 
3824   CU_ASSERT(NGTCP2_ERR_STREAM_STATE == rv);
3825 
3826   ngtcp2_conn_del(conn);
3827 
3828   /* Receiving MAX_STREAM_DATA to an uninitiated local unidirectional
3829      stream ID is an error */
3830   setup_default_client(&conn);
3831 
3832   fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
3833   fr.max_stream_data.stream_id = 2;
3834   fr.max_stream_data.max_stream_data = 8092;
3835 
3836   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3837                                   &fr, conn->pktns.crypto.rx.ckm);
3838 
3839   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3840 
3841   CU_ASSERT(NGTCP2_ERR_STREAM_STATE == rv);
3842 
3843   ngtcp2_conn_del(conn);
3844 
3845   /* Receiving MAX_STREAM_DATA to a remote bidirectional stream which
3846      exceeds limit */
3847   setup_default_client(&conn);
3848 
3849   fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
3850   fr.max_stream_data.stream_id = 1;
3851   fr.max_stream_data.max_stream_data = 1000000009;
3852 
3853   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3854                                   &fr, conn->pktns.crypto.rx.ckm);
3855 
3856   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3857 
3858   CU_ASSERT(NGTCP2_ERR_STREAM_LIMIT == rv);
3859 
3860   ngtcp2_conn_del(conn);
3861 
3862   /* Receiving MAX_STREAM_DATA to a remote bidirectional stream which
3863      the local endpoint has not received yet. */
3864   setup_default_server(&conn);
3865 
3866   fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
3867   fr.max_stream_data.stream_id = 4;
3868   fr.max_stream_data.max_stream_data = 1000000009;
3869 
3870   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3871                                   &fr, conn->pktns.crypto.rx.ckm);
3872 
3873   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3874 
3875   CU_ASSERT(0 == rv);
3876 
3877   strm = ngtcp2_conn_find_stream(conn, 4);
3878 
3879   CU_ASSERT(NULL != strm);
3880   CU_ASSERT(1000000009 == strm->tx.max_offset);
3881 
3882   ngtcp2_conn_del(conn);
3883 
3884   /* Receiving MAX_STREAM_DATA to a idle remote unidirectional stream
3885      is a protocol violation. */
3886   setup_default_server(&conn);
3887 
3888   fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
3889   fr.max_stream_data.stream_id = 2;
3890   fr.max_stream_data.max_stream_data = 1000000009;
3891 
3892   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3893                                   &fr, conn->pktns.crypto.rx.ckm);
3894 
3895   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3896 
3897   CU_ASSERT(NGTCP2_ERR_STREAM_STATE == rv);
3898 
3899   ngtcp2_conn_del(conn);
3900 
3901   /* Receiving MAX_STREAM_DATA to an existing bidirectional stream */
3902   setup_default_server(&conn);
3903 
3904   strm = open_stream(conn, 4);
3905 
3906   fr.type = NGTCP2_FRAME_MAX_STREAM_DATA;
3907   fr.max_stream_data.stream_id = 4;
3908   fr.max_stream_data.max_stream_data = 1000000009;
3909 
3910   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
3911                                   &fr, conn->pktns.crypto.rx.ckm);
3912 
3913   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
3914 
3915   CU_ASSERT(0 == rv);
3916   CU_ASSERT(1000000009 == strm->tx.max_offset);
3917 
3918   ngtcp2_conn_del(conn);
3919 }
3920 
test_ngtcp2_conn_send_early_data(void)3921 void test_ngtcp2_conn_send_early_data(void) {
3922   ngtcp2_conn *conn;
3923   ngtcp2_ssize spktlen;
3924   ngtcp2_ssize datalen;
3925   uint8_t buf[1024];
3926   int64_t stream_id;
3927   int rv;
3928   ngtcp2_tstamp t = 0;
3929   ngtcp2_vec datav;
3930 
3931   setup_early_client(&conn);
3932 
3933   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
3934 
3935   CU_ASSERT(0 == rv);
3936 
3937   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
3938                                      &datalen, NGTCP2_WRITE_STREAM_FLAG_FIN,
3939                                      stream_id, null_data, 1024, ++t);
3940 
3941   CU_ASSERT((ngtcp2_ssize)sizeof(buf) == spktlen);
3942   CU_ASSERT(674 == datalen);
3943 
3944   ngtcp2_conn_del(conn);
3945 
3946   /* Verify that Handshake packet and 0-RTT packet are coalesced into
3947      one UDP packet. */
3948   setup_early_client(&conn);
3949 
3950   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
3951 
3952   CU_ASSERT(0 == rv);
3953 
3954   spktlen =
3955       ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, sizeof(buf), &datalen,
3956                                 NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
3957                                 null_datav(&datav, 199), 1, ++t);
3958 
3959   CU_ASSERT(sizeof(buf) == spktlen);
3960   CU_ASSERT(199 == datalen);
3961 
3962   ngtcp2_conn_del(conn);
3963 
3964   /* 0 length 0-RTT packet with FIN bit set */
3965   setup_early_client(&conn);
3966 
3967   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
3968 
3969   CU_ASSERT(0 == rv);
3970 
3971   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, sizeof(buf),
3972                                       &datalen, NGTCP2_WRITE_STREAM_FLAG_FIN,
3973                                       stream_id, NULL, 0, ++t);
3974 
3975   CU_ASSERT(sizeof(buf) == spktlen);
3976   CU_ASSERT(0 == datalen);
3977 
3978   ngtcp2_conn_del(conn);
3979 
3980   /* Can write 0 length STREAM frame */
3981   setup_early_client(&conn);
3982 
3983   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
3984 
3985   CU_ASSERT(0 == rv);
3986 
3987   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, sizeof(buf),
3988                                       &datalen, NGTCP2_WRITE_STREAM_FLAG_NONE,
3989                                       -1, NULL, 0, ++t);
3990 
3991   CU_ASSERT(spktlen > 0);
3992 
3993   /* We have written Initial.  Now check that STREAM frame is
3994      written. */
3995   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, sizeof(buf),
3996                                       &datalen, NGTCP2_WRITE_STREAM_FLAG_NONE,
3997                                       stream_id, NULL, 0, ++t);
3998 
3999   CU_ASSERT(spktlen > 0);
4000 
4001   ngtcp2_conn_del(conn);
4002 
4003   /* Could not send 0-RTT data because buffer is too small. */
4004   setup_early_client(&conn);
4005 
4006   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
4007 
4008   CU_ASSERT(0 == rv);
4009 
4010   spktlen = ngtcp2_conn_writev_stream(
4011       conn, NULL, NULL, buf,
4012       NGTCP2_MIN_LONG_HEADERLEN + 1 + ngtcp2_conn_get_dcid(conn)->datalen +
4013           conn->oscid.datalen + 300,
4014       &datalen, NGTCP2_WRITE_STREAM_FLAG_FIN, stream_id, NULL, 0, ++t);
4015 
4016   CU_ASSERT(spktlen > 0);
4017   CU_ASSERT(-1 == datalen);
4018 
4019   ngtcp2_conn_del(conn);
4020 }
4021 
test_ngtcp2_conn_recv_early_data(void)4022 void test_ngtcp2_conn_recv_early_data(void) {
4023   ngtcp2_conn *conn;
4024   uint8_t buf[2048];
4025   size_t pktlen;
4026   ngtcp2_ssize spktlen;
4027   ngtcp2_frame fr;
4028   int64_t pkt_num = 1;
4029   ngtcp2_tstamp t = 0;
4030   ngtcp2_strm *strm;
4031   ngtcp2_cid rcid;
4032   int rv;
4033   my_user_data ud;
4034 
4035   rcid_init(&rcid);
4036 
4037   setup_early_server(&conn);
4038   conn->callbacks.recv_stream_data = recv_stream_data;
4039   conn->user_data = &ud;
4040 
4041   fr.type = NGTCP2_FRAME_CRYPTO;
4042   fr.crypto.offset = 0;
4043   fr.crypto.datacnt = 1;
4044   fr.crypto.data[0].len = 121;
4045   fr.crypto.data[0].base = null_data;
4046 
4047   pktlen = write_single_frame_handshake_pkt(
4048       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
4049       ++pkt_num, conn->version, &fr, &null_ckm);
4050 
4051   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4052 
4053   CU_ASSERT(0 == rv);
4054 
4055   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4056 
4057   CU_ASSERT(spktlen > 0);
4058 
4059   fr.type = NGTCP2_FRAME_STREAM;
4060   fr.stream.stream_id = 4;
4061   fr.stream.fin = 1;
4062   fr.stream.offset = 0;
4063   fr.stream.datacnt = 1;
4064   fr.stream.data[0].len = 911;
4065   fr.stream.data[0].base = null_data;
4066 
4067   pktlen = write_single_frame_0rtt_pkt(buf, sizeof(buf), &rcid,
4068                                        ngtcp2_conn_get_dcid(conn), ++pkt_num,
4069                                        conn->version, &fr, &null_ckm);
4070 
4071   memset(&ud, 0, sizeof(ud));
4072   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4073 
4074   CU_ASSERT(0 == rv);
4075   CU_ASSERT(4 == ud.stream_data.stream_id);
4076   CU_ASSERT(ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_EARLY);
4077 
4078   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4079 
4080   CU_ASSERT(spktlen > 0);
4081 
4082   strm = ngtcp2_conn_find_stream(conn, 4);
4083 
4084   CU_ASSERT(NULL != strm);
4085   CU_ASSERT(911 == strm->rx.last_offset);
4086 
4087   ngtcp2_conn_del(conn);
4088 
4089   /* Re-ordered 0-RTT packet */
4090   setup_early_server(&conn);
4091 
4092   fr.type = NGTCP2_FRAME_STREAM;
4093   fr.stream.stream_id = 4;
4094   fr.stream.fin = 1;
4095   fr.stream.offset = 0;
4096   fr.stream.datacnt = 1;
4097   fr.stream.data[0].len = 119;
4098   fr.stream.data[0].base = null_data;
4099 
4100   pktlen = write_single_frame_0rtt_pkt(buf, sizeof(buf), &rcid,
4101                                        ngtcp2_conn_get_dcid(conn), ++pkt_num,
4102                                        conn->version, &fr, &null_ckm);
4103 
4104   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4105 
4106   CU_ASSERT(NGTCP2_ERR_RETRY == rv);
4107 
4108   ngtcp2_conn_del(conn);
4109 
4110   /* Compound packet */
4111   setup_early_server(&conn);
4112 
4113   fr.type = NGTCP2_FRAME_CRYPTO;
4114   fr.crypto.offset = 0;
4115   fr.crypto.datacnt = 1;
4116   fr.crypto.data[0].len = 111;
4117   fr.crypto.data[0].base = null_data;
4118 
4119   pktlen = write_single_frame_handshake_pkt(
4120       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
4121       ++pkt_num, conn->version, &fr, &null_ckm);
4122 
4123   fr.type = NGTCP2_FRAME_STREAM;
4124   fr.stream.stream_id = 4;
4125   fr.stream.fin = 1;
4126   fr.stream.offset = 0;
4127   fr.stream.datacnt = 1;
4128   fr.stream.data[0].len = 999;
4129   fr.stream.data[0].base = null_data;
4130 
4131   pktlen += write_single_frame_0rtt_pkt(
4132       buf + pktlen, sizeof(buf) - pktlen, &rcid, ngtcp2_conn_get_dcid(conn),
4133       ++pkt_num, conn->version, &fr, &null_ckm);
4134 
4135   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4136 
4137   CU_ASSERT(0 == rv);
4138 
4139   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4140 
4141   CU_ASSERT(spktlen > 0);
4142 
4143   strm = ngtcp2_conn_find_stream(conn, 4);
4144 
4145   CU_ASSERT(NULL != strm);
4146   CU_ASSERT(999 == strm->rx.last_offset);
4147 
4148   ngtcp2_conn_del(conn);
4149 }
4150 
test_ngtcp2_conn_recv_compound_pkt(void)4151 void test_ngtcp2_conn_recv_compound_pkt(void) {
4152   ngtcp2_conn *conn;
4153   uint8_t buf[2048];
4154   size_t pktlen;
4155   ngtcp2_ssize spktlen;
4156   ngtcp2_frame fr;
4157   int64_t pkt_num = 1;
4158   ngtcp2_tstamp t = 0;
4159   ngtcp2_acktr_entry *ackent;
4160   int rv;
4161   ngtcp2_ksl_it it;
4162 
4163   /* 2 QUIC long packets in one UDP packet */
4164   setup_handshake_server(&conn);
4165 
4166   fr.type = NGTCP2_FRAME_CRYPTO;
4167   fr.crypto.offset = 0;
4168   fr.crypto.datacnt = 1;
4169   fr.crypto.data[0].len = 131;
4170   fr.crypto.data[0].base = null_data;
4171 
4172   pktlen = write_single_frame_handshake_pkt(
4173       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
4174       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
4175 
4176   pktlen += write_single_frame_handshake_pkt(
4177       buf + pktlen, sizeof(buf) - pktlen, NGTCP2_PKT_INITIAL, &conn->oscid,
4178       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
4179 
4180   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4181 
4182   CU_ASSERT(0 == rv);
4183 
4184   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4185 
4186   CU_ASSERT(spktlen > 0);
4187 
4188   it = ngtcp2_acktr_get(&conn->in_pktns->acktr);
4189   ackent = ngtcp2_ksl_it_get(&it);
4190 
4191   CU_ASSERT(pkt_num == ackent->pkt_num);
4192   CU_ASSERT(2 == ackent->len);
4193 
4194   ngtcp2_ksl_it_next(&it);
4195 
4196   CU_ASSERT(ngtcp2_ksl_it_end(&it));
4197 
4198   ngtcp2_conn_del(conn);
4199 
4200   /* 1 long packet and 1 short packet in one UDP packet */
4201   setup_default_server(&conn);
4202 
4203   fr.type = NGTCP2_FRAME_PADDING;
4204   fr.padding.len = 1;
4205 
4206   pktlen = write_single_frame_handshake_pkt(
4207       buf, sizeof(buf), NGTCP2_PKT_HANDSHAKE, &conn->oscid,
4208       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
4209 
4210   fr.type = NGTCP2_FRAME_STREAM;
4211   fr.stream.stream_id = 4;
4212   fr.stream.fin = 0;
4213   fr.stream.offset = 0;
4214   fr.stream.datacnt = 1;
4215   fr.stream.data[0].len = 426;
4216   fr.stream.data[0].base = null_data;
4217 
4218   pktlen +=
4219       write_single_frame_pkt(buf + pktlen, sizeof(buf) - pktlen, &conn->oscid,
4220                              ++pkt_num, &fr, conn->pktns.crypto.rx.ckm);
4221 
4222   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4223 
4224   CU_ASSERT(0 == rv);
4225 
4226   it = ngtcp2_acktr_get(&conn->pktns.acktr);
4227   ackent = ngtcp2_ksl_it_get(&it);
4228 
4229   CU_ASSERT(ackent->pkt_num == pkt_num);
4230 
4231   it = ngtcp2_acktr_get(&conn->hs_pktns->acktr);
4232 
4233   CU_ASSERT(!ngtcp2_ksl_it_end(&it));
4234 
4235   ngtcp2_conn_del(conn);
4236 }
4237 
test_ngtcp2_conn_pkt_payloadlen(void)4238 void test_ngtcp2_conn_pkt_payloadlen(void) {
4239   ngtcp2_conn *conn;
4240   uint8_t buf[2048];
4241   size_t pktlen;
4242   ngtcp2_ssize spktlen;
4243   ngtcp2_frame fr;
4244   int64_t pkt_num = 1;
4245   ngtcp2_tstamp t = 0;
4246   uint64_t payloadlen;
4247   int rv;
4248   const ngtcp2_cid *dcid;
4249 
4250   /* Payload length is invalid */
4251   setup_handshake_server(&conn);
4252 
4253   fr.type = NGTCP2_FRAME_STREAM;
4254   fr.stream.stream_id = 0;
4255   fr.stream.fin = 0;
4256   fr.stream.offset = 0;
4257   fr.stream.datacnt = 1;
4258   fr.stream.data[0].len = 131;
4259   fr.stream.data[0].base = null_data;
4260 
4261   dcid = ngtcp2_conn_get_dcid(conn);
4262 
4263   pktlen = write_single_frame_handshake_pkt(
4264       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid, dcid, ++pkt_num,
4265       conn->version, &fr, &null_ckm);
4266 
4267   payloadlen = read_pkt_payloadlen(buf, dcid, &conn->oscid);
4268   write_pkt_payloadlen(buf, dcid, &conn->oscid, payloadlen + 1);
4269 
4270   /* This first packet which does not increase initial packet number
4271      space CRYPTO offset or it does not get buffered as 0RTT is an
4272      error.  But it is unsecured Initial, so we just ignore it. */
4273   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4274 
4275   CU_ASSERT(NGTCP2_ERR_DROP_CONN == rv);
4276   CU_ASSERT(NGTCP2_CS_SERVER_INITIAL == conn->state);
4277 
4278   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4279 
4280   CU_ASSERT(spktlen == 0);
4281   CU_ASSERT(0 == ngtcp2_ksl_len(&conn->in_pktns->acktr.ents));
4282 
4283   ngtcp2_conn_del(conn);
4284 }
4285 
test_ngtcp2_conn_writev_stream(void)4286 void test_ngtcp2_conn_writev_stream(void) {
4287   ngtcp2_conn *conn;
4288   uint8_t buf[2048];
4289   ngtcp2_ssize spktlen;
4290   ngtcp2_tstamp t = 0;
4291   int rv;
4292   int64_t stream_id;
4293   ngtcp2_vec datav = {null_data, 10};
4294   ngtcp2_ssize datalen;
4295   size_t left;
4296 
4297   /* 0 length STREAM should not be written if we supply nonzero length
4298      data. */
4299   setup_default_client(&conn);
4300 
4301   /* This will sends NEW_CONNECTION_ID frames */
4302   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4303 
4304   CU_ASSERT(spktlen > 0);
4305 
4306   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
4307 
4308   CU_ASSERT(0 == rv);
4309 
4310   /*
4311    * Long header (1+18+1)
4312    * STREAM overhead (+3)
4313    * AEAD overhead (16)
4314    */
4315   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, 39, &datalen,
4316                                       NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
4317                                       &datav, 1, ++t);
4318 
4319   CU_ASSERT(0 == spktlen);
4320   CU_ASSERT(-1 == datalen);
4321 
4322   ngtcp2_conn_del(conn);
4323 
4324   /* +1 buffer size */
4325   setup_default_client(&conn);
4326 
4327   /* This will sends NEW_CONNECTION_ID frames */
4328   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4329 
4330   CU_ASSERT(spktlen > 0);
4331 
4332   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
4333 
4334   CU_ASSERT(0 == rv);
4335 
4336   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, 40, &datalen,
4337                                       NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
4338                                       &datav, 1, ++t);
4339 
4340   CU_ASSERT(spktlen > 0);
4341   CU_ASSERT(1 == datalen);
4342 
4343   ngtcp2_conn_del(conn);
4344 
4345   /* Coalesces multiple STREAM frames */
4346   setup_default_client(&conn);
4347   conn->local.bidi.max_streams = 100;
4348 
4349   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
4350 
4351   CU_ASSERT(0 == rv);
4352 
4353   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, 1200, &datalen,
4354                                       NGTCP2_WRITE_STREAM_FLAG_MORE, stream_id,
4355                                       &datav, 1, ++t);
4356 
4357   CU_ASSERT(NGTCP2_ERR_WRITE_MORE == spktlen);
4358   CU_ASSERT(10 == datalen);
4359 
4360   left = ngtcp2_ppe_left(&conn->pkt.ppe);
4361 
4362   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
4363 
4364   CU_ASSERT(0 == rv);
4365 
4366   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, 1200, &datalen,
4367                                       NGTCP2_WRITE_STREAM_FLAG_MORE, stream_id,
4368                                       &datav, 1, ++t);
4369 
4370   CU_ASSERT(NGTCP2_ERR_WRITE_MORE == spktlen);
4371   CU_ASSERT(10 == datalen);
4372   CU_ASSERT(ngtcp2_ppe_left(&conn->pkt.ppe) < left);
4373 
4374   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4375 
4376   CU_ASSERT(spktlen > 0);
4377 
4378   ngtcp2_conn_del(conn);
4379 
4380   /* 0RTT: Coalesces multiple STREAM frames */
4381   setup_early_client(&conn);
4382   conn->local.bidi.max_streams = 100;
4383 
4384   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
4385 
4386   CU_ASSERT(0 == rv);
4387 
4388   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, 1200, &datalen,
4389                                       NGTCP2_WRITE_STREAM_FLAG_MORE, stream_id,
4390                                       &datav, 1, ++t);
4391 
4392   CU_ASSERT(NGTCP2_ERR_WRITE_MORE == spktlen);
4393   CU_ASSERT(10 == datalen);
4394 
4395   left = ngtcp2_ppe_left(&conn->pkt.ppe);
4396 
4397   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
4398 
4399   CU_ASSERT(0 == rv);
4400 
4401   spktlen = ngtcp2_conn_writev_stream(conn, NULL, NULL, buf, 1200, &datalen,
4402                                       NGTCP2_WRITE_STREAM_FLAG_MORE, stream_id,
4403                                       &datav, 1, ++t);
4404 
4405   CU_ASSERT(NGTCP2_ERR_WRITE_MORE == spktlen);
4406   CU_ASSERT(10 == datalen);
4407   CU_ASSERT(ngtcp2_ppe_left(&conn->pkt.ppe) < left);
4408 
4409   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4410 
4411   /* Make sure that packet is padded */
4412   CU_ASSERT(1200 == spktlen);
4413 
4414   ngtcp2_conn_del(conn);
4415 }
4416 
test_ngtcp2_conn_writev_datagram(void)4417 void test_ngtcp2_conn_writev_datagram(void) {
4418   ngtcp2_conn *conn;
4419   uint8_t buf[2048];
4420   ngtcp2_ssize spktlen;
4421   ngtcp2_tstamp t = 0;
4422   ngtcp2_vec datav = {null_data, 10};
4423   ngtcp2_vec vec;
4424   int accepted;
4425   ngtcp2_transport_params params;
4426   my_user_data ud;
4427   ngtcp2_frame fr;
4428   size_t pktlen;
4429   int rv;
4430 
4431   setup_default_client(&conn);
4432   conn->callbacks.ack_datagram = ack_datagram;
4433   conn->remote.transport_params.max_datagram_frame_size = 1 + 1 + 10;
4434   conn->user_data = &ud;
4435 
4436   spktlen = ngtcp2_conn_writev_datagram(
4437       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4438       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 1000000009, &datav, 1, ++t);
4439 
4440   CU_ASSERT(spktlen > 0);
4441   CU_ASSERT(0 != accepted);
4442 
4443   fr.type = NGTCP2_FRAME_ACK;
4444   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
4445   fr.ack.ack_delay = 0;
4446   fr.ack.first_ack_blklen = 0;
4447   fr.ack.num_blks = 0;
4448 
4449   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
4450                                   conn->pktns.crypto.rx.ckm);
4451 
4452   ud.datagram.dgram_id = 0;
4453   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4454 
4455   CU_ASSERT(0 == rv);
4456   CU_ASSERT(1000000009 == ud.datagram.dgram_id);
4457 
4458   ngtcp2_conn_del(conn);
4459 
4460   /* Coalesces multiple DATAGRAM frames into a single QUIC packet */
4461   setup_default_client(&conn);
4462   conn->remote.transport_params.max_datagram_frame_size = 65535;
4463 
4464   spktlen = ngtcp2_conn_writev_datagram(
4465       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4466       NGTCP2_WRITE_DATAGRAM_FLAG_MORE, 1000000007, &datav, 1, ++t);
4467 
4468   CU_ASSERT(NGTCP2_ERR_WRITE_MORE == spktlen);
4469   CU_ASSERT(0 != accepted);
4470 
4471   spktlen = ngtcp2_conn_writev_datagram(
4472       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4473       NGTCP2_WRITE_DATAGRAM_FLAG_MORE, 1000000007, &datav, 1, ++t);
4474 
4475   CU_ASSERT(NGTCP2_ERR_WRITE_MORE == spktlen);
4476   CU_ASSERT(0 != accepted);
4477 
4478   spktlen = ngtcp2_conn_writev_datagram(
4479       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4480       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 0, &datav, 1, ++t);
4481 
4482   CU_ASSERT(spktlen > 0);
4483   CU_ASSERT(0 != accepted);
4484 
4485   ngtcp2_conn_del(conn);
4486 
4487   /* DATAGRAM cannot fit into QUIC packet because the other frames
4488      occupy the space */
4489   setup_default_client(&conn);
4490   conn->remote.transport_params.max_datagram_frame_size =
4491       1 + ngtcp2_put_varint_len(2000) + 2000;
4492 
4493   vec.base = null_data;
4494   vec.len = 2000;
4495 
4496   spktlen = ngtcp2_conn_writev_datagram(
4497       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4498       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 987, &vec, 1, ++t);
4499 
4500   CU_ASSERT(spktlen > 0);
4501   CU_ASSERT(0 == accepted);
4502 
4503   spktlen = ngtcp2_conn_writev_datagram(
4504       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4505       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 545, &vec, 1, ++t);
4506 
4507   CU_ASSERT(spktlen > 0);
4508   CU_ASSERT(0 != accepted);
4509 
4510   ngtcp2_conn_del(conn);
4511 
4512   /* Calling ngtcp2_conn_writev_datagram without receiving positive
4513      max_datagram_frame_size is an error */
4514   setup_default_client(&conn);
4515 
4516   spktlen = ngtcp2_conn_writev_datagram(
4517       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4518       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 999, &datav, 1, ++t);
4519 
4520   CU_ASSERT(NGTCP2_ERR_INVALID_STATE == spktlen);
4521 
4522   ngtcp2_conn_del(conn);
4523 
4524   /* Sending DATAGRAM which is larger than the value of received
4525      max_datagram_frame_size is an error */
4526   setup_default_client(&conn);
4527   conn->remote.transport_params.max_datagram_frame_size = 9;
4528 
4529   spktlen = ngtcp2_conn_writev_datagram(
4530       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4531       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 4433, &datav, 1, ++t);
4532 
4533   CU_ASSERT(NGTCP2_ERR_INVALID_ARGUMENT == spktlen);
4534 
4535   ngtcp2_conn_del(conn);
4536 
4537   /* Send DATAGRAM frame in a 0RTT packet */
4538   setup_early_client(&conn);
4539 
4540   params = conn->remote.transport_params;
4541   params.max_datagram_frame_size = 4311;
4542 
4543   ngtcp2_conn_set_early_remote_transport_params(conn, &params);
4544 
4545   spktlen = ngtcp2_conn_writev_datagram(
4546       conn, NULL, NULL, buf, sizeof(buf), &accepted,
4547       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 22360679, &datav, 1, ++t);
4548 
4549   CU_ASSERT(spktlen > 0);
4550   CU_ASSERT(0 != accepted);
4551 
4552   ngtcp2_conn_del(conn);
4553 }
4554 
test_ngtcp2_conn_recv_datagram(void)4555 void test_ngtcp2_conn_recv_datagram(void) {
4556   ngtcp2_conn *conn;
4557   uint8_t buf[2048];
4558   ngtcp2_frame fr;
4559   size_t pktlen;
4560   int64_t pkt_num = 0;
4561   ngtcp2_tstamp t = 0;
4562   my_user_data ud;
4563   int rv;
4564   ngtcp2_cid rcid;
4565 
4566   rcid_init(&rcid);
4567 
4568   setup_default_server(&conn);
4569   conn->user_data = &ud;
4570   conn->callbacks.recv_datagram = recv_datagram;
4571   conn->local.transport_params.max_datagram_frame_size = 1 + 1111;
4572 
4573   fr.type = NGTCP2_FRAME_DATAGRAM;
4574   fr.datagram.data = fr.datagram.rdata;
4575   fr.datagram.data->base = null_data;
4576   fr.datagram.data->len = 1111;
4577   fr.datagram.datacnt = 1;
4578 
4579   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4580                                   &fr, conn->pktns.crypto.rx.ckm);
4581 
4582   memset(&ud, 0, sizeof(ud));
4583   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4584 
4585   CU_ASSERT(0 == rv);
4586   CU_ASSERT(1111 == ud.datagram.datalen);
4587   CU_ASSERT(!(NGTCP2_DATAGRAM_FLAG_EARLY & ud.datagram.flags));
4588 
4589   ngtcp2_conn_del(conn);
4590 
4591   /* Receiving DATAGRAM frame which is strictly larger than the
4592      declared limit is an error */
4593   setup_default_server(&conn);
4594   conn->local.transport_params.max_datagram_frame_size = 1 + 1111 - 1;
4595 
4596   fr.type = NGTCP2_FRAME_DATAGRAM;
4597   fr.datagram.data = fr.datagram.rdata;
4598   fr.datagram.data->base = null_data;
4599   fr.datagram.data->len = 1111;
4600   fr.datagram.datacnt = 1;
4601 
4602   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4603                                   &fr, conn->pktns.crypto.rx.ckm);
4604 
4605   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4606 
4607   CU_ASSERT(NGTCP2_ERR_PROTO == rv);
4608 
4609   ngtcp2_conn_del(conn);
4610 
4611   /* Receiving DATAGRAM frame in a 0RTT packet */
4612   setup_early_server(&conn);
4613   conn->user_data = &ud;
4614   conn->callbacks.recv_datagram = recv_datagram;
4615   conn->local.transport_params.max_datagram_frame_size = 1 + 1111;
4616 
4617   fr.type = NGTCP2_FRAME_CRYPTO;
4618   fr.crypto.offset = 0;
4619   fr.crypto.datacnt = 1;
4620   fr.crypto.data[0].len = 121;
4621   fr.crypto.data[0].base = null_data;
4622 
4623   pktlen = write_single_frame_handshake_pkt(
4624       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
4625       ++pkt_num, conn->version, &fr, &null_ckm);
4626 
4627   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4628 
4629   CU_ASSERT(0 == rv);
4630 
4631   fr.type = NGTCP2_FRAME_DATAGRAM;
4632   fr.datagram.data = fr.datagram.rdata;
4633   fr.datagram.data->base = null_data;
4634   fr.datagram.data->len = 1111;
4635   fr.datagram.datacnt = 1;
4636 
4637   pktlen = write_single_frame_0rtt_pkt(buf, sizeof(buf), &rcid,
4638                                        ngtcp2_conn_get_dcid(conn), ++pkt_num,
4639                                        conn->version, &fr, &null_ckm);
4640 
4641   memset(&ud, 0, sizeof(ud));
4642   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4643 
4644   CU_ASSERT(0 == rv);
4645   CU_ASSERT(1111 == ud.datagram.datalen);
4646   CU_ASSERT(NGTCP2_DATAGRAM_FLAG_EARLY & ud.datagram.flags);
4647 
4648   ngtcp2_conn_del(conn);
4649 }
4650 
test_ngtcp2_conn_recv_new_connection_id(void)4651 void test_ngtcp2_conn_recv_new_connection_id(void) {
4652   ngtcp2_conn *conn;
4653   uint8_t buf[2048];
4654   size_t pktlen;
4655   ngtcp2_ssize spktlen;
4656   ngtcp2_tstamp t = 0;
4657   int64_t pkt_num = 0;
4658   ngtcp2_frame fr;
4659   ngtcp2_frame frs[4];
4660   const uint8_t cid[] = {0xf0, 0xf1, 0xf2, 0xf3};
4661   const uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xff};
4662   const uint8_t cid2[] = {0xf0, 0xf1, 0xf2, 0xf4};
4663   const uint8_t token2[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xfe};
4664   const uint8_t cid3[] = {0xf0, 0xf1, 0xf2, 0xf5};
4665   const uint8_t token3[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xfd};
4666   ngtcp2_dcid *dcid;
4667   int rv;
4668   ngtcp2_frame_chain *frc;
4669 
4670   setup_default_client(&conn);
4671 
4672   /* This will send NEW_CONNECTION_ID frames */
4673   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4674 
4675   CU_ASSERT(spktlen > 0);
4676 
4677   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4678   fr.new_connection_id.seq = 1;
4679   fr.new_connection_id.retire_prior_to = 0;
4680   ngtcp2_cid_init(&fr.new_connection_id.cid, cid, sizeof(cid));
4681   memcpy(fr.new_connection_id.stateless_reset_token, token, sizeof(token));
4682 
4683   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4684                                   &fr, conn->pktns.crypto.rx.ckm);
4685 
4686   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4687 
4688   CU_ASSERT(0 == rv);
4689   CU_ASSERT(1 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4690 
4691   assert(ngtcp2_ringbuf_len(&conn->dcid.unused));
4692   dcid = ngtcp2_ringbuf_get(&conn->dcid.unused, 0);
4693 
4694   CU_ASSERT(ngtcp2_cid_eq(&fr.new_connection_id.cid, &dcid->cid));
4695   CU_ASSERT(dcid->flags & NGTCP2_DCID_FLAG_TOKEN_PRESENT);
4696   CU_ASSERT(0 == memcmp(fr.new_connection_id.stateless_reset_token, dcid->token,
4697                         sizeof(fr.new_connection_id.stateless_reset_token)));
4698 
4699   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4700   fr.new_connection_id.seq = 2;
4701   fr.new_connection_id.retire_prior_to = 2;
4702   ngtcp2_cid_init(&fr.new_connection_id.cid, cid2, sizeof(cid2));
4703   memcpy(fr.new_connection_id.stateless_reset_token, token2, sizeof(token2));
4704 
4705   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4706                                   &fr, conn->pktns.crypto.rx.ckm);
4707 
4708   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4709 
4710   CU_ASSERT(0 == rv);
4711   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.bound));
4712   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4713   CU_ASSERT(2 == conn->dcid.current.seq);
4714   CU_ASSERT(NULL != conn->pktns.tx.frq);
4715   CU_ASSERT(2 == conn->dcid.retire_prior_to);
4716 
4717   frc = conn->pktns.tx.frq;
4718 
4719   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4720 
4721   frc = frc->next;
4722 
4723   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4724   CU_ASSERT(NULL == frc->next);
4725 
4726   /* This will send RETIRE_CONNECTION_ID frames */
4727   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4728 
4729   CU_ASSERT(spktlen > 0);
4730 
4731   ngtcp2_conn_del(conn);
4732 
4733   /* Received connection ID is immediately retired due to packet
4734      reordering */
4735   setup_default_client(&conn);
4736 
4737   /* This will send NEW_CONNECTION_ID frames */
4738   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4739 
4740   CU_ASSERT(spktlen > 0);
4741 
4742   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4743   fr.new_connection_id.seq = 2;
4744   fr.new_connection_id.retire_prior_to = 2;
4745   ngtcp2_cid_init(&fr.new_connection_id.cid, cid, sizeof(cid));
4746   memcpy(fr.new_connection_id.stateless_reset_token, token, sizeof(token));
4747 
4748   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4749                                   &fr, conn->pktns.crypto.rx.ckm);
4750 
4751   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4752 
4753   CU_ASSERT(0 == rv);
4754   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4755   CU_ASSERT(2 == conn->dcid.current.seq);
4756   CU_ASSERT(2 == conn->dcid.retire_prior_to);
4757 
4758   frc = conn->pktns.tx.frq;
4759 
4760   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4761   CU_ASSERT(NULL == frc->next);
4762 
4763   /* This will send RETIRE_CONNECTION_ID frames */
4764   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4765 
4766   CU_ASSERT(spktlen > 0);
4767 
4768   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4769   fr.new_connection_id.seq = 1;
4770   fr.new_connection_id.retire_prior_to = 0;
4771   ngtcp2_cid_init(&fr.new_connection_id.cid, cid2, sizeof(cid2));
4772   memcpy(fr.new_connection_id.stateless_reset_token, token2, sizeof(token2));
4773 
4774   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4775                                   &fr, conn->pktns.crypto.rx.ckm);
4776 
4777   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
4778 
4779   CU_ASSERT(0 == rv);
4780   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4781   CU_ASSERT(2 == conn->dcid.current.seq);
4782   CU_ASSERT(2 == conn->dcid.retire_prior_to);
4783 
4784   frc = conn->pktns.tx.frq;
4785 
4786   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4787   CU_ASSERT(NULL == frc->next);
4788 
4789   ngtcp2_conn_del(conn);
4790 
4791   /* ngtcp2_pv contains DCIDs that should be retired. */
4792   setup_default_server(&conn);
4793 
4794   /* This will send NEW_CONNECTION_ID frames */
4795   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4796 
4797   CU_ASSERT(spktlen > 0);
4798 
4799   assert(NULL == conn->pv);
4800 
4801   frs[0].type = NGTCP2_FRAME_PING;
4802   frs[1].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4803   frs[1].new_connection_id.seq = 1;
4804   frs[1].new_connection_id.retire_prior_to = 0;
4805   ngtcp2_cid_init(&frs[1].new_connection_id.cid, cid, sizeof(cid));
4806   memcpy(frs[1].new_connection_id.stateless_reset_token, token, sizeof(token));
4807   frs[2].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4808   frs[2].new_connection_id.seq = 2;
4809   frs[2].new_connection_id.retire_prior_to = 0;
4810   ngtcp2_cid_init(&frs[2].new_connection_id.cid, cid2, sizeof(cid2));
4811   memcpy(frs[2].new_connection_id.stateless_reset_token, token2,
4812          sizeof(token2));
4813   frs[3].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4814   frs[3].new_connection_id.seq = 3;
4815   frs[3].new_connection_id.retire_prior_to = 0;
4816   ngtcp2_cid_init(&frs[3].new_connection_id.cid, cid3, sizeof(cid3));
4817   memcpy(frs[3].new_connection_id.stateless_reset_token, token3,
4818          sizeof(token3));
4819 
4820   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 4,
4821                      conn->pktns.crypto.rx.ckm);
4822   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
4823 
4824   CU_ASSERT(0 == rv);
4825 
4826   assert(NULL != conn->pv);
4827 
4828   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE);
4829   CU_ASSERT(1 == conn->pv->dcid.seq);
4830   CU_ASSERT(0 == conn->pv->fallback_dcid.seq);
4831   CU_ASSERT(2 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4832 
4833   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4834   fr.new_connection_id.seq = 3;
4835   fr.new_connection_id.retire_prior_to = 2;
4836   ngtcp2_cid_init(&fr.new_connection_id.cid, cid3, sizeof(cid3));
4837   memcpy(fr.new_connection_id.stateless_reset_token, token3, sizeof(token3));
4838 
4839   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4840                                   &fr, conn->pktns.crypto.rx.ckm);
4841 
4842   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
4843 
4844   CU_ASSERT(0 == rv);
4845   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4846   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE);
4847   CU_ASSERT(2 == conn->pv->dcid.seq);
4848   CU_ASSERT(3 == conn->pv->fallback_dcid.seq);
4849 
4850   frc = conn->pktns.tx.frq;
4851 
4852   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4853   CU_ASSERT(0 == frc->fr.retire_connection_id.seq);
4854   frc = frc->next;
4855 
4856   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4857   CU_ASSERT(1 == frc->fr.retire_connection_id.seq);
4858   CU_ASSERT(NULL == frc->next);
4859 
4860   ngtcp2_conn_del(conn);
4861 
4862   /* ngtcp2_pv contains DCID in fallback that should be retired and
4863      there is not enough connection ID left.  */
4864   setup_default_server(&conn);
4865 
4866   /* This will send NEW_CONNECTION_ID frames */
4867   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4868 
4869   CU_ASSERT(spktlen > 0);
4870 
4871   assert(NULL == conn->pv);
4872 
4873   frs[0].type = NGTCP2_FRAME_PING;
4874   frs[1].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4875   frs[1].new_connection_id.seq = 1;
4876   frs[1].new_connection_id.retire_prior_to = 0;
4877   ngtcp2_cid_init(&frs[1].new_connection_id.cid, cid, sizeof(cid));
4878   memcpy(frs[1].new_connection_id.stateless_reset_token, token, sizeof(token));
4879 
4880   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 2,
4881                      conn->pktns.crypto.rx.ckm);
4882   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
4883 
4884   CU_ASSERT(0 == rv);
4885 
4886   assert(NULL != conn->pv);
4887 
4888   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE);
4889   CU_ASSERT(1 == conn->pv->dcid.seq);
4890   CU_ASSERT(0 == conn->pv->fallback_dcid.seq);
4891   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4892 
4893   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4894   fr.new_connection_id.seq = 2;
4895   fr.new_connection_id.retire_prior_to = 2;
4896   ngtcp2_cid_init(&fr.new_connection_id.cid, cid2, sizeof(cid2));
4897   memcpy(fr.new_connection_id.stateless_reset_token, token2, sizeof(token2));
4898 
4899   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4900                                   &fr, conn->pktns.crypto.rx.ckm);
4901 
4902   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
4903 
4904   CU_ASSERT(0 == rv);
4905   CU_ASSERT(2 == conn->dcid.current.seq);
4906   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4907   CU_ASSERT(NULL == conn->pv);
4908 
4909   frc = conn->pktns.tx.frq;
4910 
4911   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4912   CU_ASSERT(0 == frc->fr.retire_connection_id.seq);
4913 
4914   frc = frc->next;
4915 
4916   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4917   CU_ASSERT(1 == frc->fr.retire_connection_id.seq);
4918   CU_ASSERT(NULL == frc->next);
4919 
4920   ngtcp2_conn_del(conn);
4921 
4922   /* ngtcp2_pv contains DCIDs that should be retired and there is not
4923      enough connection ID left to continue path validation.  */
4924   setup_default_server(&conn);
4925 
4926   /* This will send NEW_CONNECTION_ID frames */
4927   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4928 
4929   CU_ASSERT(spktlen > 0);
4930 
4931   assert(NULL == conn->pv);
4932 
4933   frs[0].type = NGTCP2_FRAME_PING;
4934   frs[1].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4935   frs[1].new_connection_id.seq = 1;
4936   frs[1].new_connection_id.retire_prior_to = 0;
4937   ngtcp2_cid_init(&frs[1].new_connection_id.cid, cid, sizeof(cid));
4938   memcpy(frs[1].new_connection_id.stateless_reset_token, token, sizeof(token));
4939 
4940   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 2,
4941                      conn->pktns.crypto.rx.ckm);
4942   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
4943 
4944   CU_ASSERT(0 == rv);
4945 
4946   assert(NULL != conn->pv);
4947 
4948   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE);
4949   CU_ASSERT(1 == conn->pv->dcid.seq);
4950   CU_ASSERT(0 == conn->pv->fallback_dcid.seq);
4951   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4952 
4953   /* Overwrite seq in pv->dcid so that pv->dcid cannot be renewed. */
4954   conn->pv->dcid.seq = 2;
4955   /* Internally we assume that if primary dcid and pv->dcid differ,
4956      then no fallback dcid is present. */
4957   conn->pv->flags &= (uint8_t)~NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE;
4958 
4959   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
4960   fr.new_connection_id.seq = 3;
4961   fr.new_connection_id.retire_prior_to = 3;
4962   ngtcp2_cid_init(&fr.new_connection_id.cid, cid3, sizeof(cid3));
4963   memcpy(fr.new_connection_id.stateless_reset_token, token3, sizeof(token3));
4964 
4965   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
4966                                   &fr, conn->pktns.crypto.rx.ckm);
4967 
4968   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
4969 
4970   CU_ASSERT(0 == rv);
4971   CU_ASSERT(3 == conn->dcid.current.seq);
4972   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
4973   CU_ASSERT(NULL == conn->pv);
4974 
4975   frc = conn->pktns.tx.frq;
4976 
4977   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4978   CU_ASSERT(2 == frc->fr.retire_connection_id.seq);
4979 
4980   frc = frc->next;
4981 
4982   CU_ASSERT(NGTCP2_FRAME_RETIRE_CONNECTION_ID == frc->fr.type);
4983   CU_ASSERT(1 == frc->fr.retire_connection_id.seq);
4984   CU_ASSERT(NULL == frc->next);
4985 
4986   ngtcp2_conn_del(conn);
4987 
4988   /* Receiving more than advertised CID is treated as error */
4989   setup_default_server(&conn);
4990   conn->local.transport_params.active_connection_id_limit = 2;
4991 
4992   /* This will send NEW_CONNECTION_ID frames */
4993   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
4994 
4995   CU_ASSERT(spktlen > 0);
4996 
4997   assert(NULL == conn->pv);
4998 
4999   frs[0].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5000   frs[0].new_connection_id.seq = 1;
5001   frs[0].new_connection_id.retire_prior_to = 0;
5002   ngtcp2_cid_init(&frs[0].new_connection_id.cid, cid, sizeof(cid));
5003   memcpy(frs[0].new_connection_id.stateless_reset_token, token, sizeof(token));
5004   frs[1].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5005   frs[1].new_connection_id.seq = 2;
5006   frs[1].new_connection_id.retire_prior_to = 0;
5007   ngtcp2_cid_init(&frs[1].new_connection_id.cid, cid2, sizeof(cid2));
5008   memcpy(frs[1].new_connection_id.stateless_reset_token, token2,
5009          sizeof(token2));
5010   frs[2].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5011   frs[2].new_connection_id.seq = 3;
5012   frs[2].new_connection_id.retire_prior_to = 0;
5013   ngtcp2_cid_init(&frs[2].new_connection_id.cid, cid3, sizeof(cid3));
5014   memcpy(frs[2].new_connection_id.stateless_reset_token, token3,
5015          sizeof(token3));
5016 
5017   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 3,
5018                      conn->pktns.crypto.rx.ckm);
5019   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
5020 
5021   CU_ASSERT(NGTCP2_ERR_CONNECTION_ID_LIMIT == rv);
5022 
5023   ngtcp2_conn_del(conn);
5024 
5025   /* Receiving duplicated NEW_CONNECTION_ID frame */
5026   setup_default_server(&conn);
5027 
5028   /* This will send NEW_CONNECTION_ID frames */
5029   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5030 
5031   CU_ASSERT(spktlen > 0);
5032 
5033   frs[0].type = NGTCP2_FRAME_PING;
5034 
5035   frs[1].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5036   frs[1].new_connection_id.seq = 1;
5037   frs[1].new_connection_id.retire_prior_to = 1;
5038   ngtcp2_cid_init(&frs[1].new_connection_id.cid, cid, sizeof(cid));
5039   memcpy(frs[1].new_connection_id.stateless_reset_token, token, sizeof(token));
5040 
5041   frs[2].type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5042   frs[2].new_connection_id.seq = 2;
5043   frs[2].new_connection_id.retire_prior_to = 1;
5044   ngtcp2_cid_init(&frs[2].new_connection_id.cid, cid2, sizeof(cid2));
5045   memcpy(frs[2].new_connection_id.stateless_reset_token, token2,
5046          sizeof(token2));
5047 
5048   frs[3].type = NGTCP2_FRAME_PADDING;
5049   frs[3].padding.len = 1200;
5050 
5051   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 4,
5052                      conn->pktns.crypto.rx.ckm);
5053 
5054   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
5055 
5056   CU_ASSERT(0 == rv);
5057   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
5058   CU_ASSERT(2 == conn->dcid.current.seq);
5059   CU_ASSERT(NULL != conn->pv);
5060   CU_ASSERT(ngtcp2_cid_eq(&frs[1].new_connection_id.cid,
5061                           &conn->pv->fallback_dcid.cid));
5062 
5063   /* This will send PATH_CHALLENGE frame */
5064   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5065 
5066   CU_ASSERT(spktlen >= 1200);
5067 
5068   fr.type = NGTCP2_FRAME_PATH_RESPONSE;
5069   memset(fr.path_response.data, 0, sizeof(fr.path_response.data));
5070 
5071   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5072                                   &fr, conn->pktns.crypto.rx.ckm);
5073 
5074   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
5075 
5076   CU_ASSERT(0 == rv);
5077   /* Server starts probing old path */
5078   CU_ASSERT(NULL != conn->pv);
5079   CU_ASSERT(ngtcp2_path_eq(&null_path.path, &conn->pv->dcid.ps.path));
5080 
5081   /* Receive NEW_CONNECTION_ID seq=1 again, which should be ignored. */
5082   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 2,
5083                      conn->pktns.crypto.rx.ckm);
5084 
5085   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
5086 
5087   CU_ASSERT(0 == rv);
5088   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.unused));
5089   CU_ASSERT(2 == conn->dcid.current.seq);
5090 
5091   ngtcp2_conn_del(conn);
5092 }
5093 
test_ngtcp2_conn_recv_retire_connection_id(void)5094 void test_ngtcp2_conn_recv_retire_connection_id(void) {
5095   ngtcp2_conn *conn;
5096   uint8_t buf[2048];
5097   size_t pktlen;
5098   ngtcp2_ssize spktlen;
5099   ngtcp2_tstamp t = 1000000009;
5100   int64_t pkt_num = 0;
5101   ngtcp2_frame fr;
5102   int rv;
5103   ngtcp2_ksl_it it;
5104   ngtcp2_scid *scid;
5105   uint64_t seq;
5106 
5107   setup_default_client(&conn);
5108   conn->remote.transport_params.active_connection_id_limit = 7;
5109 
5110   /* This will send NEW_CONNECTION_ID frames */
5111   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), t);
5112 
5113   CU_ASSERT(spktlen > 0);
5114 
5115   it = ngtcp2_ksl_begin(&conn->scid.set);
5116   scid = ngtcp2_ksl_it_get(&it);
5117   seq = scid->seq;
5118 
5119   CU_ASSERT(NGTCP2_SCID_FLAG_NONE == scid->flags);
5120   CU_ASSERT(UINT64_MAX == scid->retired_ts);
5121   CU_ASSERT(1 == ngtcp2_pq_size(&conn->scid.used));
5122 
5123   fr.type = NGTCP2_FRAME_RETIRE_CONNECTION_ID;
5124   fr.retire_connection_id.seq = seq;
5125 
5126   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5127                                   &fr, conn->pktns.crypto.rx.ckm);
5128 
5129   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5130 
5131   CU_ASSERT(0 == rv);
5132   CU_ASSERT(NGTCP2_SCID_FLAG_RETIRED == scid->flags);
5133   CU_ASSERT(1000000010 == scid->retired_ts);
5134   CU_ASSERT(2 == ngtcp2_pq_size(&conn->scid.used));
5135   CU_ASSERT(7 == ngtcp2_ksl_len(&conn->scid.set));
5136   CU_ASSERT(1 == conn->scid.num_retired);
5137 
5138   /* One NEW_CONNECTION_ID frame is sent as a replacement. */
5139   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5140 
5141   CU_ASSERT(spktlen > 0);
5142   CU_ASSERT(8 == ngtcp2_ksl_len(&conn->scid.set));
5143   CU_ASSERT(1 == conn->scid.num_retired);
5144 
5145   /* No NEW_CONNECTION_ID frames should be sent. */
5146   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5147 
5148   CU_ASSERT(spktlen == 0);
5149   CU_ASSERT(8 == ngtcp2_ksl_len(&conn->scid.set));
5150   CU_ASSERT(1 == conn->scid.num_retired);
5151 
5152   /* Now time passed and retired connection ID is removed */
5153   t += 7 * NGTCP2_DEFAULT_INITIAL_RTT;
5154 
5155   ngtcp2_conn_handle_expiry(conn, t);
5156 
5157   CU_ASSERT(7 == ngtcp2_ksl_len(&conn->scid.set));
5158   CU_ASSERT(0 == conn->scid.num_retired);
5159 
5160   ngtcp2_conn_del(conn);
5161 
5162   /* Receiving RETIRE_CONNECTION_ID with seq which is greater than the
5163      sequence number previously sent must be treated as error */
5164   setup_default_server(&conn);
5165 
5166   fr.type = NGTCP2_FRAME_RETIRE_CONNECTION_ID;
5167   fr.retire_connection_id.seq = 1;
5168 
5169   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5170                                   &fr, conn->pktns.crypto.rx.ckm);
5171 
5172   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5173 
5174   CU_ASSERT(NGTCP2_ERR_PROTO == rv);
5175 
5176   ngtcp2_conn_del(conn);
5177 }
5178 
test_ngtcp2_conn_server_path_validation(void)5179 void test_ngtcp2_conn_server_path_validation(void) {
5180   ngtcp2_conn *conn;
5181   uint8_t buf[2048];
5182   size_t pktlen;
5183   ngtcp2_ssize spktlen;
5184   ngtcp2_tstamp t = 900;
5185   int64_t pkt_num = 0;
5186   ngtcp2_frame fr;
5187   int rv;
5188   const uint8_t raw_cid[] = {0x0f, 0x00, 0x00, 0x00};
5189   ngtcp2_cid cid, *new_cid;
5190   const uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xff};
5191   ngtcp2_path_storage new_path1, new_path2;
5192   ngtcp2_ksl_it it;
5193 
5194   path_init(&new_path1, 0, 0, 2, 0);
5195   path_init(&new_path2, 0, 0, 3, 0);
5196 
5197   ngtcp2_cid_init(&cid, raw_cid, sizeof(raw_cid));
5198 
5199   setup_default_server(&conn);
5200 
5201   /* This will send NEW_CONNECTION_ID frames */
5202   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5203 
5204   CU_ASSERT(spktlen > 0);
5205   CU_ASSERT(ngtcp2_ksl_len(&conn->scid.set) > 1);
5206 
5207   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5208   fr.new_connection_id.seq = 1;
5209   fr.new_connection_id.retire_prior_to = 0;
5210   fr.new_connection_id.cid = cid;
5211   memcpy(fr.new_connection_id.stateless_reset_token, token, sizeof(token));
5212 
5213   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5214                                   &fr, conn->pktns.crypto.rx.ckm);
5215 
5216   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5217 
5218   CU_ASSERT(0 == rv);
5219 
5220   fr.type = NGTCP2_FRAME_PING;
5221 
5222   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5223                                   &fr, conn->pktns.crypto.rx.ckm);
5224 
5225   rv = ngtcp2_conn_read_pkt(conn, &new_path1.path, &null_pi, buf, pktlen, ++t);
5226 
5227   CU_ASSERT(0 == rv);
5228   CU_ASSERT(NULL != conn->pv);
5229 
5230   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5231 
5232   CU_ASSERT(spktlen > 0);
5233   CU_ASSERT(ngtcp2_ringbuf_len(&conn->pv->ents) > 0);
5234 
5235   fr.type = NGTCP2_FRAME_PATH_RESPONSE;
5236   memset(fr.path_response.data, 0, sizeof(fr.path_response.data));
5237 
5238   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5239                                   &fr, conn->pktns.crypto.rx.ckm);
5240 
5241   rv = ngtcp2_conn_read_pkt(conn, &new_path1.path, &null_pi, buf, pktlen, ++t);
5242 
5243   CU_ASSERT(0 == rv);
5244   CU_ASSERT(ngtcp2_path_eq(&new_path1.path, &conn->dcid.current.ps.path));
5245   /* DCID does not change because the client does not change its
5246      DCID. */
5247   CU_ASSERT(!ngtcp2_cid_eq(&cid, &conn->dcid.current.cid));
5248 
5249   /* A remote endpoint changes DCID as well */
5250   fr.type = NGTCP2_FRAME_PING;
5251 
5252   it = ngtcp2_ksl_begin(&conn->scid.set);
5253 
5254   assert(!ngtcp2_ksl_it_end(&it));
5255 
5256   new_cid = &(((ngtcp2_scid *)ngtcp2_ksl_it_get(&it))->cid);
5257 
5258   pktlen = write_single_frame_pkt(buf, sizeof(buf), new_cid, ++pkt_num, &fr,
5259                                   conn->pktns.crypto.rx.ckm);
5260 
5261   rv = ngtcp2_conn_read_pkt(conn, &new_path2.path, &null_pi, buf, pktlen, ++t);
5262 
5263   CU_ASSERT(0 == rv);
5264   CU_ASSERT(NULL != conn->pv);
5265 
5266   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5267 
5268   CU_ASSERT(spktlen > 0);
5269   CU_ASSERT(ngtcp2_ringbuf_len(&conn->pv->ents) > 0);
5270 
5271   fr.type = NGTCP2_FRAME_PATH_RESPONSE;
5272   memset(fr.path_response.data, 0, sizeof(fr.path_response.data));
5273 
5274   pktlen = write_single_frame_pkt(buf, sizeof(buf), new_cid, ++pkt_num, &fr,
5275                                   conn->pktns.crypto.rx.ckm);
5276 
5277   rv = ngtcp2_conn_read_pkt(conn, &new_path2.path, &null_pi, buf, pktlen, ++t);
5278 
5279   CU_ASSERT(0 == rv);
5280   CU_ASSERT(ngtcp2_path_eq(&new_path2.path, &conn->dcid.current.ps.path));
5281   CU_ASSERT(ngtcp2_cid_eq(&cid, &conn->dcid.current.cid));
5282 
5283   ngtcp2_conn_del(conn);
5284 }
5285 
test_ngtcp2_conn_client_connection_migration(void)5286 void test_ngtcp2_conn_client_connection_migration(void) {
5287   ngtcp2_conn *conn;
5288   uint8_t buf[2048];
5289   size_t pktlen;
5290   ngtcp2_tstamp t = 900;
5291   int64_t pkt_num = 0;
5292   ngtcp2_frame fr;
5293   int rv;
5294   const uint8_t raw_cid[] = {0x0f, 0x00, 0x00, 0x00};
5295   ngtcp2_cid cid;
5296   const uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xff};
5297   my_user_data ud;
5298   ngtcp2_ssize spktlen;
5299   ngtcp2_path_storage to_path;
5300 
5301   ngtcp2_cid_init(&cid, raw_cid, sizeof(raw_cid));
5302 
5303   /* immediate migration */
5304   setup_default_client(&conn);
5305 
5306   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5307   fr.new_connection_id.seq = 1;
5308   fr.new_connection_id.retire_prior_to = 0;
5309   fr.new_connection_id.cid = cid;
5310   memcpy(fr.new_connection_id.stateless_reset_token, token, sizeof(token));
5311 
5312   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5313                                   &fr, conn->pktns.crypto.rx.ckm);
5314 
5315   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5316 
5317   CU_ASSERT(0 == rv);
5318 
5319   ngtcp2_path_storage_init2(&to_path, &new_path.path);
5320   to_path.path.user_data = &ud;
5321 
5322   rv = ngtcp2_conn_initiate_immediate_migration(conn, &to_path.path, ++t);
5323 
5324   CU_ASSERT(0 == rv);
5325   CU_ASSERT(NULL == conn->pv);
5326   CU_ASSERT(ngtcp2_path_eq(&to_path.path, &conn->dcid.current.ps.path));
5327   CU_ASSERT(&ud == conn->dcid.current.ps.path.user_data);
5328   CU_ASSERT(ngtcp2_cid_eq(&cid, &conn->dcid.current.cid));
5329 
5330   ngtcp2_conn_del(conn);
5331 
5332   /* migrate after successful path validation */
5333   setup_default_client(&conn);
5334 
5335   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5336   fr.new_connection_id.seq = 1;
5337   fr.new_connection_id.retire_prior_to = 0;
5338   fr.new_connection_id.cid = cid;
5339   memcpy(fr.new_connection_id.stateless_reset_token, token, sizeof(token));
5340 
5341   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5342                                   &fr, conn->pktns.crypto.rx.ckm);
5343 
5344   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5345 
5346   CU_ASSERT(0 == rv);
5347 
5348   ngtcp2_path_storage_init2(&to_path, &new_path.path);
5349   to_path.path.user_data = &ud;
5350 
5351   rv = ngtcp2_conn_initiate_migration(conn, &to_path.path, ++t);
5352 
5353   CU_ASSERT(0 == rv);
5354   CU_ASSERT(NULL != conn->pv);
5355   CU_ASSERT(ngtcp2_path_eq(&null_path.path, &conn->dcid.current.ps.path));
5356   CU_ASSERT(NULL == conn->dcid.current.ps.path.user_data);
5357   CU_ASSERT(ngtcp2_cid_eq(&conn->rcid, &conn->dcid.current.cid));
5358 
5359   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5360 
5361   CU_ASSERT(0 < spktlen);
5362 
5363   fr.type = NGTCP2_FRAME_PATH_RESPONSE;
5364   memset(fr.path_response.data, 0, sizeof(fr.path_response.data));
5365 
5366   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5367                                   &fr, conn->pktns.crypto.rx.ckm);
5368 
5369   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5370 
5371   CU_ASSERT(0 == rv);
5372   CU_ASSERT(NULL == conn->pv);
5373   CU_ASSERT(ngtcp2_path_eq(&to_path.path, &conn->dcid.current.ps.path));
5374   CU_ASSERT(&ud == conn->dcid.current.ps.path.user_data);
5375   CU_ASSERT(ngtcp2_cid_eq(&cid, &conn->dcid.current.cid));
5376 
5377   ngtcp2_conn_del(conn);
5378 }
5379 
test_ngtcp2_conn_recv_path_challenge(void)5380 void test_ngtcp2_conn_recv_path_challenge(void) {
5381   ngtcp2_conn *conn;
5382   uint8_t buf[2048];
5383   size_t pktlen;
5384   ngtcp2_ssize spktlen;
5385   ngtcp2_tstamp t = 11;
5386   int64_t pkt_num = 0;
5387   ngtcp2_frame fr;
5388   ngtcp2_frame frs[2];
5389   int rv;
5390   const uint8_t raw_cid[] = {0x0f, 0x00, 0x00, 0x00};
5391   ngtcp2_cid cid;
5392   const uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xff};
5393   const uint8_t data[] = {0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8};
5394   const uint8_t data2[] = {0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf9};
5395   ngtcp2_path_storage ps;
5396   ngtcp2_ssize shdlen;
5397   ngtcp2_pkt_hd hd;
5398 
5399   ngtcp2_cid_init(&cid, raw_cid, sizeof(raw_cid));
5400 
5401   setup_default_server(&conn);
5402 
5403   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
5404   fr.new_connection_id.seq = 1;
5405   fr.new_connection_id.retire_prior_to = 0;
5406   fr.new_connection_id.cid = cid;
5407   memcpy(fr.new_connection_id.stateless_reset_token, token, sizeof(token));
5408 
5409   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5410                                   &fr, conn->pktns.crypto.rx.ckm);
5411 
5412   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5413 
5414   CU_ASSERT(0 == rv);
5415 
5416   frs[0].type = NGTCP2_FRAME_PATH_CHALLENGE;
5417   memcpy(frs[0].path_challenge.data, data, sizeof(frs[0].path_challenge.data));
5418   frs[1].type = NGTCP2_FRAME_PADDING;
5419   frs[1].padding.len = 1200;
5420 
5421   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 2,
5422                      conn->pktns.crypto.rx.ckm);
5423 
5424   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
5425 
5426   CU_ASSERT(0 == rv);
5427   CU_ASSERT(ngtcp2_ringbuf_len(&conn->rx.path_challenge) > 0);
5428 
5429   ngtcp2_path_storage_zero(&ps);
5430 
5431   spktlen = ngtcp2_conn_write_pkt(conn, &ps.path, NULL, buf, sizeof(buf), ++t);
5432 
5433   CU_ASSERT(spktlen >= 1200);
5434   CU_ASSERT(ngtcp2_path_eq(&new_path.path, &ps.path));
5435   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->rx.path_challenge));
5436   CU_ASSERT(1 == ngtcp2_ringbuf_len(&conn->dcid.bound));
5437 
5438   shdlen = ngtcp2_pkt_decode_hd_short(&hd, buf, (size_t)spktlen, cid.datalen);
5439 
5440   CU_ASSERT(shdlen > 0);
5441   CU_ASSERT(ngtcp2_cid_eq(&cid, &hd.dcid));
5442 
5443   /* Use same bound DCID for PATH_CHALLENGE from the same path. */
5444   fr.type = NGTCP2_FRAME_PATH_CHALLENGE;
5445   memcpy(fr.path_challenge.data, data2, sizeof(fr.path_challenge.data));
5446 
5447   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5448                                   &fr, conn->pktns.crypto.rx.ckm);
5449 
5450   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
5451 
5452   CU_ASSERT(0 == rv);
5453   CU_ASSERT(ngtcp2_ringbuf_len(&conn->rx.path_challenge) > 0);
5454 
5455   ngtcp2_path_storage_zero(&ps);
5456 
5457   spktlen = ngtcp2_conn_write_pkt(conn, &ps.path, NULL, buf, sizeof(buf), ++t);
5458 
5459   CU_ASSERT(spktlen > 0);
5460   CU_ASSERT(ngtcp2_path_eq(&new_path.path, &ps.path));
5461   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->rx.path_challenge));
5462   CU_ASSERT(1 == ngtcp2_ringbuf_len(&conn->dcid.bound));
5463 
5464   shdlen = ngtcp2_pkt_decode_hd_short(&hd, buf, (size_t)spktlen, cid.datalen);
5465 
5466   CU_ASSERT(shdlen > 0);
5467   CU_ASSERT(ngtcp2_cid_eq(&cid, &hd.dcid));
5468 
5469   ngtcp2_conn_del(conn);
5470 }
5471 
test_ngtcp2_conn_key_update(void)5472 void test_ngtcp2_conn_key_update(void) {
5473   ngtcp2_conn *conn;
5474   uint8_t buf[2048];
5475   size_t pktlen;
5476   ngtcp2_ssize spktlen;
5477   ngtcp2_tstamp t = 19393;
5478   int64_t pkt_num = -1;
5479   ngtcp2_frame fr;
5480   int rv;
5481   int64_t stream_id;
5482   ngtcp2_ssize nwrite;
5483 
5484   setup_default_server(&conn);
5485 
5486   /* The remote endpoint initiates key update */
5487   fr.type = NGTCP2_FRAME_PING;
5488 
5489   pktlen = write_single_frame_pkt_flags(
5490       buf, sizeof(buf), NGTCP2_PKT_FLAG_KEY_PHASE, &conn->oscid, ++pkt_num, &fr,
5491       conn->pktns.crypto.rx.ckm);
5492 
5493   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5494 
5495   CU_ASSERT(0 == rv);
5496   CU_ASSERT(NULL != conn->crypto.key_update.old_rx_ckm);
5497   CU_ASSERT(NULL == conn->crypto.key_update.new_tx_ckm);
5498   CU_ASSERT(NULL == conn->crypto.key_update.new_rx_ckm);
5499   CU_ASSERT(UINT64_MAX == conn->crypto.key_update.confirmed_ts);
5500   CU_ASSERT(conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED);
5501 
5502   t += NGTCP2_SECONDS;
5503   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), t);
5504 
5505   CU_ASSERT(spktlen > 0);
5506 
5507   fr.type = NGTCP2_FRAME_ACK;
5508   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
5509   fr.ack.ack_delay = 0;
5510   fr.ack.first_ack_blklen = 0;
5511   fr.ack.num_blks = 0;
5512 
5513   pktlen = write_single_frame_pkt_flags(
5514       buf, sizeof(buf), NGTCP2_PKT_FLAG_KEY_PHASE, &conn->oscid, ++pkt_num, &fr,
5515       conn->pktns.crypto.rx.ckm);
5516 
5517   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5518 
5519   CU_ASSERT(0 == rv);
5520   CU_ASSERT(t == conn->crypto.key_update.confirmed_ts);
5521   CU_ASSERT(!(conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED));
5522 
5523   t += ngtcp2_conn_get_pto(conn) + 1;
5524 
5525   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), t);
5526 
5527   CU_ASSERT(0 == spktlen);
5528   CU_ASSERT(NULL == conn->crypto.key_update.old_rx_ckm);
5529   CU_ASSERT(NULL != conn->crypto.key_update.new_tx_ckm);
5530   CU_ASSERT(NULL != conn->crypto.key_update.new_rx_ckm);
5531 
5532   /* The local endpoint initiates key update */
5533   t += ngtcp2_conn_get_pto(conn) * 2;
5534 
5535   rv = ngtcp2_conn_initiate_key_update(conn, t);
5536 
5537   CU_ASSERT(0 == rv);
5538   CU_ASSERT(NULL != conn->crypto.key_update.old_rx_ckm);
5539   CU_ASSERT(NULL == conn->crypto.key_update.new_tx_ckm);
5540   CU_ASSERT(NULL == conn->crypto.key_update.new_rx_ckm);
5541   CU_ASSERT(conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED);
5542 
5543   rv = ngtcp2_conn_open_uni_stream(conn, &stream_id, NULL);
5544 
5545   CU_ASSERT(0 == rv);
5546 
5547   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
5548                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
5549                                      stream_id, null_data, 1024, ++t);
5550 
5551   CU_ASSERT(spktlen > 0);
5552   CU_ASSERT(conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED);
5553 
5554   fr.type = NGTCP2_FRAME_ACK;
5555   fr.ack.largest_ack = conn->pktns.tx.last_pkt_num;
5556   fr.ack.ack_delay = 0;
5557   fr.ack.first_ack_blklen = 0;
5558   fr.ack.num_blks = 0;
5559 
5560   pktlen = write_single_frame_pkt_flags(
5561       buf, sizeof(buf), NGTCP2_PKT_FLAG_KEY_PHASE, &conn->oscid, ++pkt_num, &fr,
5562       conn->pktns.crypto.rx.ckm);
5563 
5564   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5565 
5566   CU_ASSERT(0 == rv);
5567   CU_ASSERT(t == conn->crypto.key_update.confirmed_ts);
5568   CU_ASSERT(!(conn->flags & NGTCP2_CONN_FLAG_KEY_UPDATE_NOT_CONFIRMED));
5569 
5570   ngtcp2_conn_del(conn);
5571 }
5572 
test_ngtcp2_conn_crypto_buffer_exceeded(void)5573 void test_ngtcp2_conn_crypto_buffer_exceeded(void) {
5574   ngtcp2_conn *conn;
5575   uint8_t buf[2048];
5576   size_t pktlen;
5577   ngtcp2_tstamp t = 11111;
5578   int64_t pkt_num = -1;
5579   ngtcp2_frame fr;
5580   int rv;
5581 
5582   setup_default_client(&conn);
5583 
5584   fr.type = NGTCP2_FRAME_CRYPTO;
5585   fr.crypto.offset = 1000000;
5586   fr.crypto.datacnt = 1;
5587   fr.crypto.data[0].base = null_data;
5588   fr.crypto.data[0].len = 1;
5589 
5590   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
5591                                   &fr, conn->pktns.crypto.rx.ckm);
5592 
5593   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5594 
5595   CU_ASSERT(NGTCP2_ERR_CRYPTO_BUFFER_EXCEEDED == rv);
5596 
5597   ngtcp2_conn_del(conn);
5598 }
5599 
test_ngtcp2_conn_handshake_probe(void)5600 void test_ngtcp2_conn_handshake_probe(void) {
5601   ngtcp2_conn *conn;
5602   ngtcp2_tstamp t = 0;
5603   ngtcp2_ssize spktlen;
5604   size_t pktlen;
5605   uint8_t buf[1200];
5606   ngtcp2_frame fr;
5607   ngtcp2_rtb_entry *ent;
5608   ngtcp2_ksl_it it;
5609   int rv;
5610   ngtcp2_crypto_aead_ctx aead_ctx = {0};
5611   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
5612   ngtcp2_crypto_ctx crypto_ctx;
5613 
5614   /* Retransmit first Initial on PTO timer */
5615   setup_handshake_client(&conn);
5616 
5617   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5618 
5619   CU_ASSERT(spktlen > 0);
5620   CU_ASSERT(1 == conn->in_pktns->rtb.num_ack_eliciting);
5621 
5622   rv = ngtcp2_conn_on_loss_detection_timer(conn, ++t);
5623 
5624   CU_ASSERT(0 == rv);
5625   CU_ASSERT(1 == conn->in_pktns->rtb.probe_pkt_left);
5626 
5627   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5628 
5629   CU_ASSERT(spktlen > 0);
5630   CU_ASSERT(1 == conn->in_pktns->rtb.num_retransmittable);
5631   CU_ASSERT(2 == conn->in_pktns->rtb.num_ack_eliciting);
5632   CU_ASSERT(0 == conn->in_pktns->rtb.probe_pkt_left);
5633 
5634   fr.type = NGTCP2_FRAME_ACK;
5635   fr.ack.largest_ack = 0;
5636   fr.ack.ack_delay = 0;
5637   fr.ack.first_ack_blklen = 0;
5638   fr.ack.num_blks = 0;
5639 
5640   pktlen = write_single_frame_handshake_pkt(
5641       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
5642       ngtcp2_conn_get_dcid(conn), 0, NGTCP2_PROTO_VER_MAX, &fr, &null_ckm);
5643   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5644 
5645   CU_ASSERT(0 == rv);
5646   CU_ASSERT(1 == conn->in_pktns->rtb.num_ack_eliciting);
5647 
5648   rv = ngtcp2_conn_on_loss_detection_timer(conn, ++t);
5649 
5650   CU_ASSERT(0 == rv);
5651   CU_ASSERT(1 == conn->in_pktns->rtb.num_retransmittable);
5652   CU_ASSERT(1 == conn->in_pktns->rtb.num_ack_eliciting);
5653   CU_ASSERT(1 == conn->in_pktns->rtb.probe_pkt_left);
5654 
5655   /* This sends anti-deadlock padded Initial packet even if we have
5656      nothing to send. */
5657   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5658 
5659   CU_ASSERT(spktlen > 0);
5660   CU_ASSERT(0 == conn->in_pktns->rtb.num_retransmittable);
5661   CU_ASSERT(2 == conn->in_pktns->rtb.num_ack_eliciting);
5662   CU_ASSERT(0 == conn->in_pktns->rtb.probe_pkt_left);
5663 
5664   it = ngtcp2_rtb_head(&conn->in_pktns->rtb);
5665   ent = ngtcp2_ksl_it_get(&it);
5666 
5667   CU_ASSERT(ent->flags & NGTCP2_RTB_ENTRY_FLAG_PROBE);
5668   CU_ASSERT(sizeof(buf) == ent->pktlen);
5669 
5670   init_crypto_ctx(&crypto_ctx);
5671   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
5672   ngtcp2_conn_install_rx_handshake_key(conn, &aead_ctx, null_iv,
5673                                        sizeof(null_iv), &hp_ctx);
5674   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
5675                                        sizeof(null_iv), &hp_ctx);
5676 
5677   rv = ngtcp2_conn_on_loss_detection_timer(conn, ++t);
5678 
5679   CU_ASSERT(0 == rv);
5680   CU_ASSERT(2 == conn->in_pktns->rtb.num_ack_eliciting);
5681   CU_ASSERT(1 == conn->hs_pktns->rtb.probe_pkt_left);
5682 
5683   /* This sends anti-deadlock Handshake packet even if we have nothing
5684      to send. */
5685   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5686 
5687   CU_ASSERT(spktlen > 0);
5688   CU_ASSERT(0 == conn->hs_pktns->rtb.num_retransmittable);
5689   CU_ASSERT(1 == conn->hs_pktns->rtb.num_ack_eliciting);
5690   CU_ASSERT(0 == conn->hs_pktns->rtb.probe_pkt_left);
5691 
5692   it = ngtcp2_rtb_head(&conn->hs_pktns->rtb);
5693   ent = ngtcp2_ksl_it_get(&it);
5694 
5695   CU_ASSERT(ent->flags & NGTCP2_RTB_ENTRY_FLAG_PROBE);
5696   CU_ASSERT(sizeof(buf) > ent->pktlen);
5697 
5698   ngtcp2_conn_del(conn);
5699 }
5700 
test_ngtcp2_conn_handshake_loss(void)5701 void test_ngtcp2_conn_handshake_loss(void) {
5702   ngtcp2_conn *conn;
5703   ngtcp2_tstamp t = 0;
5704   ngtcp2_ssize spktlen;
5705   size_t i;
5706   size_t pktlen;
5707   uint8_t buf[1252];
5708   ngtcp2_frame fr;
5709   union {
5710     ngtcp2_frame fr;
5711     struct {
5712       ngtcp2_frame fr;
5713       ngtcp2_vec data[8];
5714     } crypto;
5715   } crypto;
5716   ngtcp2_frame *cfr;
5717   ngtcp2_cid rcid;
5718   int rv;
5719   int64_t pkt_num = -1;
5720   ngtcp2_ksl_it it;
5721   ngtcp2_rtb_entry *ent;
5722   ngtcp2_frame_chain *frc;
5723 
5724   rcid_init(&rcid);
5725   setup_handshake_server(&conn);
5726   conn->callbacks.recv_crypto_data = recv_crypto_data;
5727 
5728   cfr = &crypto.fr;
5729   cfr->type = NGTCP2_FRAME_CRYPTO;
5730   cfr->crypto.offset = 0;
5731   cfr->crypto.datacnt = 1;
5732   cfr->crypto.data[0].len = 123;
5733   cfr->crypto.data[0].base = null_data;
5734 
5735   pktlen = write_single_frame_handshake_pkt(
5736       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
5737       ++pkt_num, conn->version, cfr, &null_ckm);
5738 
5739   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5740 
5741   CU_ASSERT(0 == rv);
5742 
5743   /* Increase anti-amplification factor for easier testing */
5744   conn->dcid.current.bytes_recv += 10000;
5745 
5746   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_INITIAL, null_data,
5747                                  123);
5748   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_HANDSHAKE, null_data,
5749                                  163);
5750   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_HANDSHAKE, null_data,
5751                                  2369);
5752   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_HANDSHAKE, null_data,
5753                                  79);
5754   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_HANDSHAKE, null_data,
5755                                  36);
5756 
5757   /* Initial and first Handshake are coalesced into 1 packet. */
5758   for (i = 0; i < 3; ++i) {
5759     spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5760     CU_ASSERT(spktlen > 0);
5761   }
5762 
5763   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5764 
5765   CU_ASSERT(0 == spktlen);
5766 
5767   t += 1000;
5768 
5769   ngtcp2_conn_on_loss_detection_timer(conn, t);
5770 
5771   CU_ASSERT(1 == conn->in_pktns->rtb.probe_pkt_left);
5772   CU_ASSERT(1 == conn->hs_pktns->rtb.probe_pkt_left);
5773 
5774   for (i = 0; i < 2; ++i) {
5775     spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5776 
5777     CU_ASSERT(spktlen > 0);
5778   }
5779 
5780   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5781 
5782   CU_ASSERT(0 == spktlen);
5783 
5784   it = ngtcp2_ksl_begin(&conn->hs_pktns->rtb.ents);
5785   ent = ngtcp2_ksl_it_get(&it);
5786 
5787   CU_ASSERT(996 == ent->frc->fr.crypto.offset);
5788   CU_ASSERT(4 == ent->hd.pkt_num);
5789 
5790   fr.type = NGTCP2_FRAME_ACK;
5791   fr.ack.largest_ack = 2;
5792   fr.ack.ack_delay = 0;
5793   fr.ack.ack_delay_unscaled = 0;
5794   fr.ack.first_ack_blklen = 0;
5795   fr.ack.num_blks = 0;
5796 
5797   pktlen = write_single_frame_handshake_pkt(
5798       buf, sizeof(buf), NGTCP2_PKT_HANDSHAKE, &conn->oscid,
5799       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
5800 
5801   t += 8;
5802   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, t);
5803 
5804   CU_ASSERT(0 == rv);
5805 
5806   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5807 
5808   CU_ASSERT(0 == spktlen);
5809 
5810   t += 1000;
5811 
5812   ngtcp2_conn_on_loss_detection_timer(conn, t);
5813 
5814   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5815 
5816   CU_ASSERT(spktlen > 0);
5817 
5818   it = ngtcp2_ksl_begin(&conn->hs_pktns->rtb.ents);
5819   ent = ngtcp2_ksl_it_get(&it);
5820 
5821   CU_ASSERT(NGTCP2_FRAME_CRYPTO == ent->frc->fr.type);
5822   CU_ASSERT(0 == ent->frc->fr.crypto.offset);
5823   CU_ASSERT(2 == ent->frc->fr.crypto.datacnt);
5824   CU_ASSERT(1186 == ngtcp2_vec_len(ent->frc->fr.crypto.data,
5825                                    ent->frc->fr.crypto.datacnt));
5826 
5827   ngtcp2_conn_del(conn);
5828 
5829   /* Retransmission changes CRYPTO offset */
5830   setup_handshake_server(&conn);
5831   conn->callbacks.recv_crypto_data = recv_crypto_data;
5832 
5833   cfr = &crypto.fr;
5834   cfr->type = NGTCP2_FRAME_CRYPTO;
5835   cfr->crypto.offset = 0;
5836   cfr->crypto.datacnt = 1;
5837   cfr->crypto.data[0].len = 123;
5838   cfr->crypto.data[0].base = null_data;
5839 
5840   pktlen = write_single_frame_handshake_pkt(
5841       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
5842       ++pkt_num, conn->version, cfr, &null_ckm);
5843 
5844   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5845 
5846   CU_ASSERT(0 == rv);
5847 
5848   /* Increase anti-amplification factor for easier testing */
5849   conn->dcid.current.bytes_recv += 10000;
5850 
5851   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_INITIAL, null_data,
5852                                  123);
5853   ngtcp2_conn_submit_crypto_data(conn, NGTCP2_CRYPTO_LEVEL_HANDSHAKE, null_data,
5854                                  3000);
5855 
5856   /* Initial and first Handshake are coalesced into 1 packet. */
5857   for (i = 0; i < 3; ++i) {
5858     spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5859     CU_ASSERT(spktlen > 0);
5860   }
5861 
5862   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5863 
5864   CU_ASSERT(0 == spktlen);
5865 
5866   it = ngtcp2_ksl_begin(&conn->hs_pktns->rtb.ents);
5867   ent = ngtcp2_ksl_it_get(&it);
5868 
5869   CU_ASSERT(NGTCP2_FRAME_CRYPTO == ent->frc->fr.type);
5870   CU_ASSERT(2176 == ent->frc->fr.crypto.offset);
5871   CU_ASSERT(824 == ngtcp2_vec_len(ent->frc->fr.crypto.data,
5872                                   ent->frc->fr.crypto.datacnt));
5873 
5874   t += 1000;
5875 
5876   ngtcp2_conn_on_loss_detection_timer(conn, t);
5877 
5878   /* On retransmission, the first HANDSHAKE CRYPTO has additional 5
5879      bytes of data because Initial packet does not contain ACK
5880      frame. */
5881   for (i = 0; i < 2; ++i) {
5882     spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5883     CU_ASSERT(spktlen > 0);
5884   }
5885 
5886   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5887 
5888   CU_ASSERT(0 == spktlen);
5889 
5890   it = ngtcp2_ksl_begin(&conn->hs_pktns->rtb.ents);
5891   ent = ngtcp2_ksl_it_get(&it);
5892 
5893   CU_ASSERT(NGTCP2_FRAME_CRYPTO == ent->frc->fr.type);
5894   CU_ASSERT(996 == ent->frc->fr.crypto.offset);
5895   CU_ASSERT(1180 == ngtcp2_vec_len(ent->frc->fr.crypto.data,
5896                                    ent->frc->fr.crypto.datacnt));
5897 
5898   fr.type = NGTCP2_FRAME_ACK;
5899   fr.ack.largest_ack = 0;
5900   fr.ack.ack_delay = 0;
5901   fr.ack.ack_delay_unscaled = 0;
5902   fr.ack.first_ack_blklen = 0;
5903   fr.ack.num_blks = 0;
5904 
5905   pktlen = write_single_frame_handshake_pkt(
5906       buf, sizeof(buf), NGTCP2_PKT_HANDSHAKE, &conn->oscid,
5907       ngtcp2_conn_get_dcid(conn), ++pkt_num, conn->version, &fr, &null_ckm);
5908 
5909   t += 8;
5910   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, t);
5911 
5912   CU_ASSERT(0 == rv);
5913 
5914   t += 1000;
5915 
5916   ngtcp2_conn_on_loss_detection_timer(conn, t);
5917 
5918   /* Receiving Handshake ACK discards Initial packet number space.
5919      The first Handshake CRYPTO is acknowledged, but
5920      conn->hs_pktns->crypto.tx.frq still has offset without taking
5921      into account this ACK.  When calculating the available space for
5922      CRYPTO data to send, we must consider the acknowledged data and
5923      compute correct CRYPTO offset. */
5924   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5925 
5926   CU_ASSERT(spktlen > 0);
5927 
5928   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
5929   CU_ASSERT(0 == spktlen);
5930 
5931   it = ngtcp2_ksl_begin(&conn->hs_pktns->rtb.ents);
5932   ent = ngtcp2_ksl_it_get(&it);
5933 
5934   CU_ASSERT(NGTCP2_FRAME_CRYPTO == ent->frc->fr.type);
5935   CU_ASSERT(991 == ent->frc->fr.crypto.offset);
5936   CU_ASSERT(5 == ngtcp2_vec_len(ent->frc->fr.crypto.data,
5937                                 ent->frc->fr.crypto.datacnt));
5938 
5939   frc = ent->frc->next;
5940 
5941   CU_ASSERT(NGTCP2_FRAME_CRYPTO == frc->fr.type);
5942   CU_ASSERT(2176 == frc->fr.crypto.offset);
5943   CU_ASSERT(824 == ngtcp2_vec_len(frc->fr.crypto.data, frc->fr.crypto.datacnt));
5944   CU_ASSERT(NULL == frc->next);
5945 
5946   ngtcp2_conn_del(conn);
5947 }
5948 
test_ngtcp2_conn_recv_client_initial_retry(void)5949 void test_ngtcp2_conn_recv_client_initial_retry(void) {
5950   ngtcp2_conn *conn;
5951   uint8_t buf[2048];
5952   size_t pktlen;
5953   ngtcp2_frame fr;
5954   int64_t pkt_num = -1;
5955   ngtcp2_tstamp t = 0;
5956   ngtcp2_cid rcid;
5957   int rv;
5958 
5959   rcid_init(&rcid);
5960 
5961   setup_handshake_server(&conn);
5962   fr.type = NGTCP2_FRAME_CRYPTO;
5963   fr.crypto.offset = 1;
5964   fr.crypto.datacnt = 1;
5965   fr.crypto.data[0].len = 45;
5966   fr.crypto.data[0].base = null_data;
5967 
5968   pktlen = write_single_frame_handshake_pkt(
5969       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &rcid, ngtcp2_conn_get_dcid(conn),
5970       ++pkt_num, conn->version, &fr, &null_ckm);
5971 
5972   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
5973 
5974   CU_ASSERT(NGTCP2_ERR_RETRY == rv);
5975 
5976   ngtcp2_conn_del(conn);
5977 }
5978 
test_ngtcp2_conn_recv_client_initial_token(void)5979 void test_ngtcp2_conn_recv_client_initial_token(void) {
5980   ngtcp2_conn *conn;
5981   uint8_t buf[2048];
5982   size_t pktlen;
5983   ngtcp2_frame fr;
5984   int64_t pkt_num = -1;
5985   ngtcp2_tstamp t = 0;
5986   ngtcp2_cid rcid;
5987   int rv;
5988   const uint8_t raw_token[] = {0xff, 0x12, 0x31, 0x04, 0xab};
5989   ngtcp2_vec token;
5990   const ngtcp2_mem *mem;
5991 
5992   rcid_init(&rcid);
5993 
5994   setup_handshake_server(&conn);
5995   mem = conn->mem;
5996 
5997   token.base = ngtcp2_mem_malloc(mem, sizeof(raw_token));
5998   memcpy(token.base, raw_token, sizeof(raw_token));
5999   token.len = sizeof(raw_token);
6000 
6001   conn->local.settings.token = token;
6002 
6003   fr.type = NGTCP2_FRAME_CRYPTO;
6004   fr.crypto.offset = 0;
6005   fr.crypto.datacnt = 1;
6006   fr.crypto.data[0].len = 45;
6007   fr.crypto.data[0].base = null_data;
6008 
6009   pktlen = write_single_frame_initial_pkt(
6010       buf, sizeof(buf), &rcid, ngtcp2_conn_get_dcid(conn), ++pkt_num,
6011       conn->version, &fr, raw_token, sizeof(raw_token), &null_ckm);
6012 
6013   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
6014 
6015   CU_ASSERT(0 == rv);
6016   CU_ASSERT(45 == ngtcp2_strm_rx_offset(&conn->in_pktns->crypto.strm));
6017 
6018   ngtcp2_conn_del(conn);
6019 
6020   /* Specifying invalid token lets server drop the packet */
6021   setup_handshake_server(&conn);
6022   mem = conn->mem;
6023 
6024   token.base = ngtcp2_mem_malloc(mem, sizeof(raw_token));
6025   memcpy(token.base, raw_token, sizeof(raw_token));
6026   token.len = sizeof(raw_token) - 1;
6027 
6028   conn->local.settings.token = token;
6029 
6030   fr.type = NGTCP2_FRAME_CRYPTO;
6031   fr.crypto.offset = 0;
6032   fr.crypto.datacnt = 1;
6033   fr.crypto.data[0].len = 45;
6034   fr.crypto.data[0].base = null_data;
6035 
6036   pktlen = write_single_frame_initial_pkt(
6037       buf, sizeof(buf), &rcid, ngtcp2_conn_get_dcid(conn), ++pkt_num,
6038       conn->version, &fr, raw_token, sizeof(raw_token), &null_ckm);
6039 
6040   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
6041 
6042   CU_ASSERT(NGTCP2_ERR_DROP_CONN == rv);
6043   CU_ASSERT(0 == ngtcp2_strm_rx_offset(&conn->in_pktns->crypto.strm));
6044 
6045   ngtcp2_conn_del(conn);
6046 }
6047 
test_ngtcp2_conn_get_active_dcid(void)6048 void test_ngtcp2_conn_get_active_dcid(void) {
6049   ngtcp2_conn *conn;
6050   ngtcp2_cid_token cid_token[2];
6051   ngtcp2_cid dcid;
6052   static uint8_t token[] = {0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1,
6053                             0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1, 0xf1};
6054 
6055   dcid_init(&dcid);
6056   setup_default_client(&conn);
6057 
6058   CU_ASSERT(1 == ngtcp2_conn_get_num_active_dcid(conn));
6059   CU_ASSERT(1 == ngtcp2_conn_get_active_dcid(conn, cid_token));
6060   CU_ASSERT(0 == cid_token[0].seq);
6061   CU_ASSERT(ngtcp2_cid_eq(&dcid, &cid_token[0].cid));
6062   CU_ASSERT(ngtcp2_path_eq(&null_path.path, &cid_token[0].ps.path));
6063   CU_ASSERT(1 == cid_token[0].token_present);
6064   CU_ASSERT(0 ==
6065             memcmp(token, cid_token[0].token, NGTCP2_STATELESS_RESET_TOKENLEN));
6066 
6067   ngtcp2_conn_del(conn);
6068 }
6069 
test_ngtcp2_conn_recv_version_negotiation(void)6070 void test_ngtcp2_conn_recv_version_negotiation(void) {
6071   ngtcp2_conn *conn;
6072   const ngtcp2_cid *dcid;
6073   ngtcp2_ssize spktlen;
6074   uint8_t buf[1500];
6075   uint32_t nsv[3];
6076   int rv;
6077   ngtcp2_tstamp t = 0;
6078 
6079   setup_handshake_client(&conn);
6080 
6081   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
6082 
6083   CU_ASSERT(spktlen > 0);
6084 
6085   dcid = ngtcp2_conn_get_dcid(conn);
6086 
6087   nsv[0] = 0xffffffff;
6088 
6089   spktlen = ngtcp2_pkt_write_version_negotiation(
6090       buf, sizeof(buf), 0xfe, conn->oscid.data, conn->oscid.datalen, dcid->data,
6091       dcid->datalen, nsv, 1);
6092 
6093   CU_ASSERT(spktlen > 0);
6094 
6095   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
6096                             (size_t)spktlen, ++t);
6097 
6098   CU_ASSERT(NGTCP2_ERR_RECV_VERSION_NEGOTIATION == rv);
6099 
6100   ngtcp2_conn_del(conn);
6101 
6102   /* Ignore Version Negotiation if it contains version selected by
6103      client */
6104   setup_handshake_client(&conn);
6105 
6106   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
6107 
6108   CU_ASSERT(spktlen > 0);
6109 
6110   dcid = ngtcp2_conn_get_dcid(conn);
6111 
6112   nsv[0] = 0xfffffff0;
6113   nsv[1] = conn->version;
6114 
6115   spktlen = ngtcp2_pkt_write_version_negotiation(
6116       buf, sizeof(buf), 0x50, conn->oscid.data, conn->oscid.datalen, dcid->data,
6117       dcid->datalen, nsv, 2);
6118 
6119   CU_ASSERT(spktlen > 0);
6120 
6121   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
6122                             (size_t)spktlen, ++t);
6123 
6124   CU_ASSERT(0 == rv);
6125 
6126   ngtcp2_conn_del(conn);
6127 }
6128 
test_ngtcp2_conn_send_initial_token(void)6129 void test_ngtcp2_conn_send_initial_token(void) {
6130   ngtcp2_conn *conn;
6131   uint8_t buf[2048];
6132   ngtcp2_callbacks cb;
6133   ngtcp2_settings settings;
6134   ngtcp2_transport_params params;
6135   ngtcp2_cid rcid, scid;
6136   ngtcp2_crypto_aead retry_aead = {0, NGTCP2_FAKE_AEAD_OVERHEAD};
6137   uint8_t token[] = "this is token";
6138   ngtcp2_ssize spktlen, shdlen;
6139   ngtcp2_tstamp t = 0;
6140   ngtcp2_pkt_hd hd;
6141   ngtcp2_crypto_aead_ctx aead_ctx = {0};
6142   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
6143   ngtcp2_crypto_ctx crypto_ctx;
6144 
6145   rcid_init(&rcid);
6146   scid_init(&scid);
6147 
6148   init_initial_crypto_ctx(&crypto_ctx);
6149 
6150   client_default_callbacks(&cb);
6151   client_default_settings(&settings);
6152   client_default_transport_params(&params);
6153 
6154   settings.token.base = token;
6155   settings.token.len = sizeof(token);
6156 
6157   ngtcp2_conn_client_new(&conn, &rcid, &scid, &null_path.path,
6158                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
6159                          /* mem = */ NULL, NULL);
6160   ngtcp2_conn_set_initial_crypto_ctx(conn, &crypto_ctx);
6161   ngtcp2_conn_install_initial_key(conn, &aead_ctx, null_iv, &hp_ctx, &aead_ctx,
6162                                   null_iv, &hp_ctx, sizeof(null_iv));
6163   ngtcp2_conn_set_retry_aead(conn, &retry_aead, &aead_ctx);
6164 
6165   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
6166 
6167   CU_ASSERT(spktlen > 0);
6168 
6169   shdlen = ngtcp2_pkt_decode_hd_long(&hd, buf, (size_t)spktlen);
6170 
6171   CU_ASSERT(shdlen > 0);
6172   CU_ASSERT(sizeof(token) == hd.token.len);
6173   CU_ASSERT(0 == memcmp(token, hd.token.base, sizeof(token)));
6174 
6175   ngtcp2_conn_del(conn);
6176 }
6177 
test_ngtcp2_conn_set_remote_transport_params(void)6178 void test_ngtcp2_conn_set_remote_transport_params(void) {
6179   ngtcp2_conn *conn;
6180   ngtcp2_transport_params params;
6181   int rv;
6182   ngtcp2_cid dcid;
6183 
6184   dcid_init(&dcid);
6185 
6186   /* client: Successful case */
6187   setup_handshake_client(&conn);
6188 
6189   memset(&params, 0, sizeof(params));
6190   params.active_connection_id_limit = NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT;
6191   params.max_udp_payload_size = 1450;
6192   params.initial_scid = conn->dcid.current.cid;
6193   params.original_dcid = conn->rcid;
6194 
6195   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
6196 
6197   CU_ASSERT(0 == rv);
6198 
6199   ngtcp2_conn_del(conn);
6200 
6201   /* client: Wrong original_dcid */
6202   setup_handshake_client(&conn);
6203 
6204   memset(&params, 0, sizeof(params));
6205   params.active_connection_id_limit = NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT;
6206   params.max_udp_payload_size = 1450;
6207   params.initial_scid = conn->dcid.current.cid;
6208 
6209   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
6210 
6211   CU_ASSERT(NGTCP2_ERR_TRANSPORT_PARAM == rv);
6212 
6213   ngtcp2_conn_del(conn);
6214 
6215   /* client: Wrong initial_scid */
6216   setup_handshake_client(&conn);
6217 
6218   memset(&params, 0, sizeof(params));
6219   params.active_connection_id_limit = NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT;
6220   params.max_udp_payload_size = 1450;
6221   params.original_dcid = conn->rcid;
6222 
6223   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
6224 
6225   CU_ASSERT(NGTCP2_ERR_TRANSPORT_PARAM == rv);
6226 
6227   ngtcp2_conn_del(conn);
6228 
6229   /* client: Receiving retry_scid when retry is not attempted */
6230   setup_handshake_client(&conn);
6231 
6232   memset(&params, 0, sizeof(params));
6233   params.active_connection_id_limit = NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT;
6234   params.max_udp_payload_size = 1450;
6235   params.initial_scid = conn->dcid.current.cid;
6236   params.original_dcid = conn->rcid;
6237   params.retry_scid_present = 1;
6238 
6239   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
6240 
6241   CU_ASSERT(NGTCP2_ERR_TRANSPORT_PARAM == rv);
6242 
6243   ngtcp2_conn_del(conn);
6244 
6245   /* client: Receiving retry_scid */
6246   setup_handshake_client(&conn);
6247 
6248   conn->flags |= NGTCP2_CONN_FLAG_RECV_RETRY;
6249   conn->retry_scid = dcid;
6250 
6251   memset(&params, 0, sizeof(params));
6252   params.active_connection_id_limit = NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT;
6253   params.max_udp_payload_size = 1450;
6254   params.initial_scid = conn->dcid.current.cid;
6255   params.original_dcid = conn->rcid;
6256   params.retry_scid_present = 1;
6257   params.retry_scid = dcid;
6258 
6259   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
6260 
6261   CU_ASSERT(0 == rv);
6262 
6263   ngtcp2_conn_del(conn);
6264 
6265   /* client: Not receiving retry_scid when retry is attempted */
6266   setup_handshake_client(&conn);
6267 
6268   conn->flags |= NGTCP2_CONN_FLAG_RECV_RETRY;
6269   conn->retry_scid = dcid;
6270 
6271   memset(&params, 0, sizeof(params));
6272   params.active_connection_id_limit = NGTCP2_DEFAULT_ACTIVE_CONNECTION_ID_LIMIT;
6273   params.max_udp_payload_size = 1450;
6274   params.initial_scid = conn->dcid.current.cid;
6275   params.original_dcid = conn->rcid;
6276 
6277   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
6278 
6279   CU_ASSERT(NGTCP2_ERR_TRANSPORT_PARAM == rv);
6280 
6281   ngtcp2_conn_del(conn);
6282 }
6283 
test_ngtcp2_conn_write_connection_close(void)6284 void test_ngtcp2_conn_write_connection_close(void) {
6285   ngtcp2_conn *conn;
6286   uint8_t buf[1200];
6287   ngtcp2_ssize spktlen, shdlen;
6288   ngtcp2_pkt_hd hd;
6289   const uint8_t *p;
6290   ngtcp2_crypto_aead_ctx aead_ctx = {0};
6291   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
6292   ngtcp2_crypto_ctx crypto_ctx;
6293 
6294   /* Client only Initial key */
6295   setup_handshake_client(&conn);
6296 
6297   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 0);
6298 
6299   CU_ASSERT(spktlen > 0);
6300 
6301   spktlen = ngtcp2_conn_write_connection_close(conn, NULL, NULL, buf,
6302                                                sizeof(buf), NGTCP2_NO_ERROR, 0);
6303 
6304   CU_ASSERT(spktlen > 0);
6305 
6306   shdlen = ngtcp2_pkt_decode_hd_long(&hd, buf, (size_t)spktlen);
6307 
6308   CU_ASSERT(shdlen > 0);
6309   CU_ASSERT(NGTCP2_PKT_INITIAL == hd.type);
6310   CU_ASSERT(shdlen + (ngtcp2_ssize)hd.len == spktlen);
6311 
6312   ngtcp2_conn_del(conn);
6313 
6314   /* Client has Initial and Handshake keys */
6315   setup_handshake_client(&conn);
6316 
6317   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 0);
6318 
6319   CU_ASSERT(spktlen > 0);
6320 
6321   init_crypto_ctx(&crypto_ctx);
6322 
6323   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6324   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6325                                        sizeof(null_iv), &hp_ctx);
6326 
6327   spktlen = ngtcp2_conn_write_connection_close(conn, NULL, NULL, buf,
6328                                                sizeof(buf), NGTCP2_NO_ERROR, 0);
6329 
6330   CU_ASSERT(spktlen > 0);
6331 
6332   shdlen = ngtcp2_pkt_decode_hd_long(&hd, buf, (size_t)spktlen);
6333 
6334   CU_ASSERT(shdlen > 0);
6335   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6336   CU_ASSERT(shdlen + (ngtcp2_ssize)hd.len == spktlen);
6337 
6338   ngtcp2_conn_del(conn);
6339 
6340   /* Client has all keys and has not confirmed handshake */
6341   setup_handshake_client(&conn);
6342 
6343   init_crypto_ctx(&crypto_ctx);
6344 
6345   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6346   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6347                                        sizeof(null_iv), &hp_ctx);
6348   ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret), &aead_ctx,
6349                              null_iv, sizeof(null_iv), &hp_ctx);
6350 
6351   conn->state = NGTCP2_CS_POST_HANDSHAKE;
6352 
6353   spktlen = ngtcp2_conn_write_connection_close(conn, NULL, NULL, buf,
6354                                                sizeof(buf), NGTCP2_NO_ERROR, 0);
6355 
6356   CU_ASSERT(spktlen > 0);
6357 
6358   p = buf;
6359 
6360   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6361 
6362   CU_ASSERT(shdlen > 0);
6363   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6364 
6365   p += shdlen + (ngtcp2_ssize)hd.len;
6366   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6367 
6368   shdlen = ngtcp2_pkt_decode_hd_short(&hd, p, (size_t)spktlen,
6369                                       conn->dcid.current.cid.datalen);
6370   CU_ASSERT(shdlen > 0);
6371   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6372 
6373   ngtcp2_conn_del(conn);
6374 
6375   /* Client has confirmed handshake */
6376   setup_default_client(&conn);
6377 
6378   spktlen = ngtcp2_conn_write_connection_close(conn, NULL, NULL, buf,
6379                                                sizeof(buf), NGTCP2_NO_ERROR, 0);
6380 
6381   CU_ASSERT(spktlen > 0);
6382 
6383   shdlen = ngtcp2_pkt_decode_hd_short(&hd, buf, (size_t)spktlen,
6384                                       conn->dcid.current.cid.datalen);
6385 
6386   CU_ASSERT(shdlen > 0);
6387   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6388 
6389   ngtcp2_conn_del(conn);
6390 
6391   /* Server has Initial and Handshake key */
6392   setup_handshake_server(&conn);
6393 
6394   init_initial_crypto_ctx(&crypto_ctx);
6395 
6396   ngtcp2_conn_set_initial_crypto_ctx(conn, &crypto_ctx);
6397   ngtcp2_conn_install_initial_key(conn, &aead_ctx, null_iv, &hp_ctx, &aead_ctx,
6398                                   null_iv, &hp_ctx, sizeof(null_iv));
6399 
6400   init_crypto_ctx(&crypto_ctx);
6401 
6402   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6403   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6404                                        sizeof(null_iv), &hp_ctx);
6405 
6406   spktlen = ngtcp2_conn_write_connection_close(conn, NULL, NULL, buf,
6407                                                sizeof(buf), NGTCP2_NO_ERROR, 0);
6408 
6409   CU_ASSERT(spktlen > 0);
6410 
6411   p = buf;
6412 
6413   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6414 
6415   CU_ASSERT(shdlen > 0);
6416   CU_ASSERT(NGTCP2_PKT_INITIAL == hd.type);
6417 
6418   p += shdlen + (ngtcp2_ssize)hd.len;
6419   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6420 
6421   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6422 
6423   CU_ASSERT(shdlen > 0);
6424   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6425   CU_ASSERT(shdlen + (ngtcp2_ssize)hd.len == spktlen);
6426 
6427   ngtcp2_conn_del(conn);
6428 
6429   /* Server has all keys and has not confirmed handshake */
6430   setup_handshake_server(&conn);
6431 
6432   init_initial_crypto_ctx(&crypto_ctx);
6433 
6434   ngtcp2_conn_set_initial_crypto_ctx(conn, &crypto_ctx);
6435   ngtcp2_conn_install_initial_key(conn, &aead_ctx, null_iv, &hp_ctx, &aead_ctx,
6436                                   null_iv, &hp_ctx, sizeof(null_iv));
6437 
6438   init_crypto_ctx(&crypto_ctx);
6439 
6440   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6441   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6442                                        sizeof(null_iv), &hp_ctx);
6443   ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret), &aead_ctx,
6444                              null_iv, sizeof(null_iv), &hp_ctx);
6445 
6446   conn->state = NGTCP2_CS_POST_HANDSHAKE;
6447 
6448   spktlen = ngtcp2_conn_write_connection_close(conn, NULL, NULL, buf,
6449                                                sizeof(buf), NGTCP2_NO_ERROR, 0);
6450 
6451   CU_ASSERT(spktlen > 0);
6452 
6453   p = buf;
6454 
6455   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6456 
6457   CU_ASSERT(shdlen > 0);
6458   CU_ASSERT(NGTCP2_PKT_INITIAL == hd.type);
6459 
6460   p += shdlen + (ngtcp2_ssize)hd.len;
6461   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6462 
6463   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6464 
6465   CU_ASSERT(shdlen > 0);
6466   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6467 
6468   p += shdlen + (ngtcp2_ssize)hd.len;
6469   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6470 
6471   shdlen = ngtcp2_pkt_decode_hd_short(&hd, p, (size_t)spktlen,
6472                                       conn->dcid.current.cid.datalen);
6473 
6474   CU_ASSERT(shdlen > 0);
6475   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6476 
6477   ngtcp2_conn_del(conn);
6478 
6479   /* Server has confirmed handshake */
6480   setup_default_server(&conn);
6481 
6482   spktlen = ngtcp2_conn_write_connection_close(conn, NULL, NULL, buf,
6483                                                sizeof(buf), NGTCP2_NO_ERROR, 0);
6484 
6485   CU_ASSERT(spktlen > 0);
6486 
6487   shdlen = ngtcp2_pkt_decode_hd_short(&hd, buf, (size_t)spktlen,
6488                                       conn->dcid.current.cid.datalen);
6489 
6490   CU_ASSERT(shdlen > 0);
6491   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6492 
6493   ngtcp2_conn_del(conn);
6494 }
6495 
test_ngtcp2_conn_write_application_close(void)6496 void test_ngtcp2_conn_write_application_close(void) {
6497   ngtcp2_conn *conn;
6498   uint8_t buf[1200];
6499   ngtcp2_ssize spktlen, shdlen;
6500   ngtcp2_pkt_hd hd;
6501   const uint8_t *p;
6502   ngtcp2_crypto_aead_ctx aead_ctx = {0};
6503   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
6504   uint64_t app_err_code = 0;
6505   ngtcp2_crypto_ctx crypto_ctx;
6506 
6507   /* Client only Initial key */
6508   setup_handshake_client(&conn);
6509 
6510   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 0);
6511 
6512   CU_ASSERT(spktlen > 0);
6513 
6514   spktlen = ngtcp2_conn_write_application_close(conn, NULL, NULL, buf,
6515                                                 sizeof(buf), app_err_code, 0);
6516 
6517   CU_ASSERT(spktlen > 0);
6518 
6519   shdlen = ngtcp2_pkt_decode_hd_long(&hd, buf, (size_t)spktlen);
6520 
6521   CU_ASSERT(shdlen > 0);
6522   CU_ASSERT(NGTCP2_PKT_INITIAL == hd.type);
6523   CU_ASSERT(shdlen + (ngtcp2_ssize)hd.len == spktlen);
6524 
6525   ngtcp2_conn_del(conn);
6526 
6527   /* Client has Initial and Handshake keys */
6528   setup_handshake_client(&conn);
6529 
6530   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), 0);
6531 
6532   CU_ASSERT(spktlen > 0);
6533 
6534   init_crypto_ctx(&crypto_ctx);
6535 
6536   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6537   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6538                                        sizeof(null_iv), &hp_ctx);
6539 
6540   spktlen = ngtcp2_conn_write_application_close(conn, NULL, NULL, buf,
6541                                                 sizeof(buf), app_err_code, 0);
6542 
6543   CU_ASSERT(spktlen > 0);
6544 
6545   shdlen = ngtcp2_pkt_decode_hd_long(&hd, buf, (size_t)spktlen);
6546 
6547   CU_ASSERT(shdlen > 0);
6548   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6549   CU_ASSERT(shdlen + (ngtcp2_ssize)hd.len == spktlen);
6550 
6551   ngtcp2_conn_del(conn);
6552 
6553   /* Client has all keys and has not confirmed handshake */
6554   setup_handshake_client(&conn);
6555 
6556   init_crypto_ctx(&crypto_ctx);
6557 
6558   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6559   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6560                                        sizeof(null_iv), &hp_ctx);
6561   ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret), &aead_ctx,
6562                              null_iv, sizeof(null_iv), &hp_ctx);
6563 
6564   conn->state = NGTCP2_CS_POST_HANDSHAKE;
6565 
6566   spktlen = ngtcp2_conn_write_application_close(conn, NULL, NULL, buf,
6567                                                 sizeof(buf), app_err_code, 0);
6568 
6569   CU_ASSERT(spktlen > 0);
6570 
6571   p = buf;
6572 
6573   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6574 
6575   CU_ASSERT(shdlen > 0);
6576   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6577 
6578   p += shdlen + (ngtcp2_ssize)hd.len;
6579   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6580 
6581   shdlen = ngtcp2_pkt_decode_hd_short(&hd, p, (size_t)spktlen,
6582                                       conn->dcid.current.cid.datalen);
6583   CU_ASSERT(shdlen > 0);
6584   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6585 
6586   ngtcp2_conn_del(conn);
6587 
6588   /* Client has confirmed handshake */
6589   setup_default_client(&conn);
6590 
6591   spktlen = ngtcp2_conn_write_application_close(conn, NULL, NULL, buf,
6592                                                 sizeof(buf), app_err_code, 0);
6593 
6594   CU_ASSERT(spktlen > 0);
6595 
6596   shdlen = ngtcp2_pkt_decode_hd_short(&hd, buf, (size_t)spktlen,
6597                                       conn->dcid.current.cid.datalen);
6598 
6599   CU_ASSERT(shdlen > 0);
6600   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6601 
6602   ngtcp2_conn_del(conn);
6603 
6604   /* Server has Initial and Handshake key */
6605   setup_handshake_server(&conn);
6606 
6607   init_initial_crypto_ctx(&crypto_ctx);
6608 
6609   ngtcp2_conn_set_initial_crypto_ctx(conn, &crypto_ctx);
6610   ngtcp2_conn_install_initial_key(conn, &aead_ctx, null_iv, &hp_ctx, &aead_ctx,
6611                                   null_iv, &hp_ctx, sizeof(null_iv));
6612 
6613   init_crypto_ctx(&crypto_ctx);
6614 
6615   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6616   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6617                                        sizeof(null_iv), &hp_ctx);
6618 
6619   spktlen = ngtcp2_conn_write_application_close(conn, NULL, NULL, buf,
6620                                                 sizeof(buf), app_err_code, 0);
6621 
6622   CU_ASSERT(spktlen > 0);
6623 
6624   p = buf;
6625 
6626   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6627 
6628   CU_ASSERT(shdlen > 0);
6629   CU_ASSERT(NGTCP2_PKT_INITIAL == hd.type);
6630 
6631   p += shdlen + (ngtcp2_ssize)hd.len;
6632   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6633 
6634   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6635 
6636   CU_ASSERT(shdlen > 0);
6637   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6638   CU_ASSERT(shdlen + (ngtcp2_ssize)hd.len == spktlen);
6639 
6640   ngtcp2_conn_del(conn);
6641 
6642   /* Server has all keys and has not confirmed handshake */
6643   setup_handshake_server(&conn);
6644 
6645   init_initial_crypto_ctx(&crypto_ctx);
6646 
6647   ngtcp2_conn_set_initial_crypto_ctx(conn, &crypto_ctx);
6648   ngtcp2_conn_install_initial_key(conn, &aead_ctx, null_iv, &hp_ctx, &aead_ctx,
6649                                   null_iv, &hp_ctx, sizeof(null_iv));
6650 
6651   init_crypto_ctx(&crypto_ctx);
6652 
6653   ngtcp2_conn_set_crypto_ctx(conn, &crypto_ctx);
6654   ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
6655                                        sizeof(null_iv), &hp_ctx);
6656   ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret), &aead_ctx,
6657                              null_iv, sizeof(null_iv), &hp_ctx);
6658 
6659   spktlen = ngtcp2_conn_write_application_close(conn, NULL, NULL, buf,
6660                                                 sizeof(buf), app_err_code, 0);
6661 
6662   CU_ASSERT(spktlen > 0);
6663 
6664   p = buf;
6665 
6666   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6667 
6668   CU_ASSERT(shdlen > 0);
6669   CU_ASSERT(NGTCP2_PKT_INITIAL == hd.type);
6670 
6671   p += shdlen + (ngtcp2_ssize)hd.len;
6672   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6673 
6674   shdlen = ngtcp2_pkt_decode_hd_long(&hd, p, (size_t)spktlen);
6675 
6676   CU_ASSERT(shdlen > 0);
6677   CU_ASSERT(NGTCP2_PKT_HANDSHAKE == hd.type);
6678 
6679   p += shdlen + (ngtcp2_ssize)hd.len;
6680   spktlen -= shdlen + (ngtcp2_ssize)hd.len;
6681 
6682   shdlen = ngtcp2_pkt_decode_hd_short(&hd, p, (size_t)spktlen,
6683                                       conn->dcid.current.cid.datalen);
6684 
6685   CU_ASSERT(shdlen > 0);
6686   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6687 
6688   ngtcp2_conn_del(conn);
6689 
6690   /* Server has confirmed handshake */
6691   setup_default_server(&conn);
6692 
6693   spktlen = ngtcp2_conn_write_application_close(conn, NULL, NULL, buf,
6694                                                 sizeof(buf), app_err_code, 0);
6695 
6696   CU_ASSERT(spktlen > 0);
6697 
6698   shdlen = ngtcp2_pkt_decode_hd_short(&hd, buf, (size_t)spktlen,
6699                                       conn->dcid.current.cid.datalen);
6700 
6701   CU_ASSERT(shdlen > 0);
6702   CU_ASSERT(NGTCP2_PKT_SHORT == hd.type);
6703 
6704   ngtcp2_conn_del(conn);
6705 }
6706 
test_ngtcp2_conn_rtb_reclaim_on_pto(void)6707 void test_ngtcp2_conn_rtb_reclaim_on_pto(void) {
6708   ngtcp2_conn *conn;
6709   int rv;
6710   int64_t stream_id;
6711   uint8_t buf[2048];
6712   ngtcp2_ssize nwrite;
6713   ngtcp2_ssize spktlen;
6714   size_t i;
6715   size_t num_reclaim_pkt;
6716   ngtcp2_rtb_entry *ent;
6717   ngtcp2_ksl_it it;
6718 
6719   setup_default_client(&conn);
6720 
6721   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
6722 
6723   CU_ASSERT(0 == rv);
6724 
6725   for (i = 0; i < 5; ++i) {
6726     spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
6727                                        &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
6728                                        stream_id, null_data, 1024, 1);
6729 
6730     CU_ASSERT(0 < spktlen);
6731   }
6732 
6733   CU_ASSERT(5 == ngtcp2_ksl_len(&conn->pktns.rtb.ents));
6734 
6735   rv = ngtcp2_conn_on_loss_detection_timer(conn, 3 * NGTCP2_SECONDS);
6736 
6737   CU_ASSERT(0 == rv);
6738 
6739   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf),
6740                                   3 * NGTCP2_SECONDS);
6741 
6742   CU_ASSERT(spktlen > 0);
6743 
6744   it = ngtcp2_ksl_begin(&conn->pktns.rtb.ents);
6745   num_reclaim_pkt = 0;
6746   for (; !ngtcp2_ksl_it_end(&it); ngtcp2_ksl_it_next(&it)) {
6747     ent = ngtcp2_ksl_it_get(&it);
6748     if (ent->flags & NGTCP2_RTB_ENTRY_FLAG_PTO_RECLAIMED) {
6749       ++num_reclaim_pkt;
6750     }
6751   }
6752 
6753   CU_ASSERT(3 == num_reclaim_pkt);
6754 
6755   ngtcp2_conn_del(conn);
6756 }
6757 
test_ngtcp2_conn_rtb_reclaim_on_pto_datagram(void)6758 void test_ngtcp2_conn_rtb_reclaim_on_pto_datagram(void) {
6759   ngtcp2_conn *conn;
6760   int rv;
6761   int64_t stream_id;
6762   uint8_t buf[2048];
6763   ngtcp2_ssize nwrite;
6764   ngtcp2_ssize spktlen;
6765   size_t num_reclaim_pkt;
6766   ngtcp2_rtb_entry *ent;
6767   ngtcp2_ksl_it it;
6768   ngtcp2_vec datav;
6769   int accepted;
6770   ngtcp2_frame_chain *frc;
6771 
6772   /* DATAGRAM frame must not be reclaimed on PTO */
6773   setup_default_client(&conn);
6774 
6775   conn->callbacks.ack_datagram = ack_datagram;
6776   conn->remote.transport_params.max_datagram_frame_size = 65535;
6777 
6778   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
6779 
6780   CU_ASSERT(0 == rv);
6781 
6782   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
6783                                      &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
6784                                      stream_id, null_data, 1024, 1);
6785 
6786   CU_ASSERT(0 < spktlen);
6787 
6788   datav.base = null_data;
6789   datav.len = 10;
6790 
6791   spktlen = ngtcp2_conn_writev_datagram(
6792       conn, NULL, NULL, buf, sizeof(buf), &accepted,
6793       NGTCP2_WRITE_DATAGRAM_FLAG_NONE, 1000000007, &datav, 1, 1);
6794 
6795   CU_ASSERT(accepted);
6796   CU_ASSERT(0 < spktlen);
6797   CU_ASSERT(2 == ngtcp2_ksl_len(&conn->pktns.rtb.ents));
6798 
6799   rv = ngtcp2_conn_on_loss_detection_timer(conn, 3 * NGTCP2_SECONDS);
6800 
6801   CU_ASSERT(0 == rv);
6802 
6803   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf),
6804                                   3 * NGTCP2_SECONDS);
6805 
6806   CU_ASSERT(spktlen > 0);
6807 
6808   it = ngtcp2_ksl_begin(&conn->pktns.rtb.ents);
6809   num_reclaim_pkt = 0;
6810   for (; !ngtcp2_ksl_it_end(&it); ngtcp2_ksl_it_next(&it)) {
6811     ent = ngtcp2_ksl_it_get(&it);
6812     if (ent->flags & NGTCP2_RTB_ENTRY_FLAG_PTO_RECLAIMED) {
6813       ++num_reclaim_pkt;
6814       for (frc = ent->frc; frc; frc = frc->next) {
6815         CU_ASSERT(NGTCP2_FRAME_DATAGRAM != frc->fr.type);
6816       }
6817     }
6818   }
6819 
6820   CU_ASSERT(1 == num_reclaim_pkt);
6821 
6822   ngtcp2_conn_del(conn);
6823 }
6824 
test_ngtcp2_conn_validate_ecn(void)6825 void test_ngtcp2_conn_validate_ecn(void) {
6826   ngtcp2_conn *conn;
6827   uint8_t buf[2048];
6828   ngtcp2_ssize spktlen;
6829   ngtcp2_pkt_info pi;
6830   size_t pktlen;
6831   int rv;
6832   ngtcp2_frame fr;
6833   int64_t stream_id;
6834   ngtcp2_ssize nwrite;
6835   size_t i;
6836 
6837   setup_default_client(&conn);
6838 
6839   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), 1);
6840 
6841   CU_ASSERT(0 < spktlen);
6842   CU_ASSERT(NGTCP2_ECN_ECT_0 == pi.ecn);
6843   CU_ASSERT(NGTCP2_ECN_STATE_TESTING == conn->tx.ecn.state);
6844   CU_ASSERT(1 == conn->tx.ecn.validation_start_ts);
6845   CU_ASSERT(0 == conn->pktns.tx.ecn.start_pkt_num);
6846 
6847   fr.type = NGTCP2_FRAME_ACK_ECN;
6848   fr.ack.largest_ack = 0;
6849   fr.ack.ack_delay = 0;
6850   fr.ack.first_ack_blklen = 0;
6851   fr.ack.num_blks = 0;
6852   fr.ack.ecn.ect0 = 1;
6853   fr.ack.ecn.ect1 = 0;
6854   fr.ack.ecn.ce = 0;
6855 
6856   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
6857                                   conn->pktns.crypto.rx.ckm);
6858   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
6859 
6860   CU_ASSERT(0 == rv);
6861   CU_ASSERT(NGTCP2_ECN_STATE_CAPABLE == conn->tx.ecn.state);
6862 
6863   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
6864 
6865   CU_ASSERT(0 == rv);
6866 
6867   spktlen = ngtcp2_conn_write_stream(conn, NULL, &pi, buf, sizeof(buf), &nwrite,
6868                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
6869                                      null_data, 1024, 2);
6870 
6871   CU_ASSERT(0 < spktlen);
6872 
6873   /* Receiving ACK frame containing less ECN counts fails
6874      validation */
6875   fr.type = NGTCP2_FRAME_ACK_ECN;
6876   fr.ack.largest_ack = 1;
6877   fr.ack.ack_delay = 0;
6878   fr.ack.first_ack_blklen = 0;
6879   fr.ack.num_blks = 0;
6880   fr.ack.ecn.ect0 = 0;
6881   fr.ack.ecn.ect1 = 0;
6882   fr.ack.ecn.ce = 0;
6883 
6884   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 1, &fr,
6885                                   conn->pktns.crypto.rx.ckm);
6886   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 3);
6887 
6888   CU_ASSERT(0 == rv);
6889   CU_ASSERT(NGTCP2_ECN_STATE_FAILED == conn->tx.ecn.state);
6890 
6891   spktlen = ngtcp2_conn_write_stream(conn, NULL, &pi, buf, sizeof(buf), &nwrite,
6892                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
6893                                      null_data, 1024, 2);
6894 
6895   CU_ASSERT(0 < spktlen);
6896   CU_ASSERT(NGTCP2_ECN_NOT_ECT == pi.ecn);
6897 
6898   ngtcp2_conn_del(conn);
6899 
6900   /* Receiving ACK frame without ECN counts invalidates ECN
6901      capability */
6902   setup_default_server(&conn);
6903 
6904   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), 1);
6905 
6906   CU_ASSERT(0 < spktlen);
6907   CU_ASSERT(NGTCP2_ECN_ECT_0 == pi.ecn);
6908   CU_ASSERT(NGTCP2_ECN_STATE_TESTING == conn->tx.ecn.state);
6909   CU_ASSERT(1 == conn->tx.ecn.validation_start_ts);
6910   CU_ASSERT(0 == conn->pktns.tx.ecn.start_pkt_num);
6911 
6912   fr.type = NGTCP2_FRAME_ACK;
6913   fr.ack.largest_ack = 0;
6914   fr.ack.ack_delay = 0;
6915   fr.ack.first_ack_blklen = 0;
6916   fr.ack.num_blks = 0;
6917 
6918   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
6919                                   conn->pktns.crypto.rx.ckm);
6920   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
6921 
6922   CU_ASSERT(0 == rv);
6923   CU_ASSERT(NGTCP2_ECN_STATE_FAILED == conn->tx.ecn.state);
6924 
6925   ngtcp2_conn_del(conn);
6926 
6927   /* CE counts must be considered */
6928   setup_default_client(&conn);
6929 
6930   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
6931 
6932   CU_ASSERT(0 == rv);
6933 
6934   for (i = 0; i < 2; ++i) {
6935     spktlen = ngtcp2_conn_write_stream(conn, NULL, &pi, buf, sizeof(buf),
6936                                        &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
6937                                        stream_id, null_data, 1024, 2);
6938 
6939     CU_ASSERT(0 < spktlen);
6940     CU_ASSERT(NGTCP2_ECN_ECT_0 == pi.ecn);
6941   }
6942 
6943   CU_ASSERT(NGTCP2_ECN_STATE_TESTING == conn->tx.ecn.state);
6944   CU_ASSERT(2 == conn->tx.ecn.validation_start_ts);
6945   CU_ASSERT(0 == conn->pktns.tx.ecn.start_pkt_num);
6946 
6947   fr.type = NGTCP2_FRAME_ACK_ECN;
6948   fr.ack.largest_ack = 1;
6949   fr.ack.ack_delay = 0;
6950   fr.ack.first_ack_blklen = 1;
6951   fr.ack.num_blks = 0;
6952   fr.ack.ecn.ect0 = 1;
6953   fr.ack.ecn.ect1 = 0;
6954   fr.ack.ecn.ce = 1;
6955 
6956   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
6957                                   conn->pktns.crypto.rx.ckm);
6958   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
6959 
6960   CU_ASSERT(0 == rv);
6961   CU_ASSERT(NGTCP2_ECN_STATE_CAPABLE == conn->tx.ecn.state);
6962   CU_ASSERT(0 == ngtcp2_ksl_len(&conn->pktns.rtb.ents));
6963 
6964   ngtcp2_conn_del(conn);
6965 
6966   /* If increments of ECN counts is less than the number of
6967      acknowledged ECN entries, ECN validation fails. */
6968   setup_default_client(&conn);
6969 
6970   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), 1);
6971 
6972   CU_ASSERT(0 < spktlen);
6973   CU_ASSERT(NGTCP2_ECN_ECT_0 == pi.ecn);
6974   CU_ASSERT(NGTCP2_ECN_STATE_TESTING == conn->tx.ecn.state);
6975   CU_ASSERT(1 == conn->tx.ecn.validation_start_ts);
6976   CU_ASSERT(0 == conn->pktns.tx.ecn.start_pkt_num);
6977 
6978   fr.type = NGTCP2_FRAME_ACK_ECN;
6979   fr.ack.largest_ack = 0;
6980   fr.ack.ack_delay = 0;
6981   fr.ack.first_ack_blklen = 0;
6982   fr.ack.num_blks = 0;
6983   fr.ack.ecn.ect0 = 0;
6984   fr.ack.ecn.ect1 = 1;
6985   fr.ack.ecn.ce = 0;
6986 
6987   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
6988                                   conn->pktns.crypto.rx.ckm);
6989   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
6990 
6991   CU_ASSERT(0 == rv);
6992   CU_ASSERT(NGTCP2_ECN_STATE_FAILED == conn->tx.ecn.state);
6993 
6994   ngtcp2_conn_del(conn);
6995 
6996   /* If ECT count is larger than the number of ECT marked packet, ECN
6997      validation fails. */
6998   setup_default_client(&conn);
6999 
7000   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), 1);
7001 
7002   CU_ASSERT(0 < spktlen);
7003   CU_ASSERT(NGTCP2_ECN_ECT_0 == pi.ecn);
7004   CU_ASSERT(NGTCP2_ECN_STATE_TESTING == conn->tx.ecn.state);
7005   CU_ASSERT(1 == conn->tx.ecn.validation_start_ts);
7006   CU_ASSERT(0 == conn->pktns.tx.ecn.start_pkt_num);
7007 
7008   fr.type = NGTCP2_FRAME_ACK_ECN;
7009   fr.ack.largest_ack = 0;
7010   fr.ack.ack_delay = 0;
7011   fr.ack.first_ack_blklen = 0;
7012   fr.ack.num_blks = 0;
7013   fr.ack.ecn.ect0 = 2;
7014   fr.ack.ecn.ect1 = 0;
7015   fr.ack.ecn.ce = 0;
7016 
7017   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
7018                                   conn->pktns.crypto.rx.ckm);
7019   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, 2);
7020 
7021   CU_ASSERT(0 == rv);
7022   CU_ASSERT(NGTCP2_ECN_STATE_FAILED == conn->tx.ecn.state);
7023 
7024   ngtcp2_conn_del(conn);
7025 
7026   /* ECN validation fails if all ECN marked packets are lost */
7027   setup_default_client(&conn);
7028 
7029   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
7030 
7031   CU_ASSERT(0 == rv);
7032 
7033   for (i = 0; i < NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS; ++i) {
7034     spktlen = ngtcp2_conn_write_stream(conn, NULL, &pi, buf, sizeof(buf),
7035                                        &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
7036                                        stream_id, null_data, 25, 0);
7037 
7038     CU_ASSERT(0 < spktlen);
7039     CU_ASSERT(NGTCP2_ECN_ECT_0 == pi.ecn);
7040   }
7041 
7042   CU_ASSERT(NGTCP2_ECN_STATE_UNKNOWN == conn->tx.ecn.state);
7043   CU_ASSERT(NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS == conn->tx.ecn.dgram_sent);
7044 
7045   spktlen = ngtcp2_conn_write_stream(conn, NULL, &pi, buf, sizeof(buf), &nwrite,
7046                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
7047                                      null_data, 25, 0);
7048 
7049   CU_ASSERT(0 < spktlen);
7050   CU_ASSERT(NGTCP2_ECN_NOT_ECT == pi.ecn);
7051   CU_ASSERT(NGTCP2_ECN_STATE_UNKNOWN == conn->tx.ecn.state);
7052   CU_ASSERT(0 == conn->tx.ecn.validation_start_ts);
7053   CU_ASSERT(0 == conn->pktns.tx.ecn.start_pkt_num);
7054   CU_ASSERT(NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS == conn->tx.ecn.dgram_sent);
7055   CU_ASSERT(0 == conn->pktns.tx.ecn.validation_pkt_lost);
7056 
7057   fr.type = NGTCP2_FRAME_ACK;
7058   fr.ack.largest_ack = NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS;
7059   fr.ack.ack_delay = 0;
7060   fr.ack.first_ack_blklen = 0;
7061   fr.ack.num_blks = 0;
7062 
7063   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
7064                                   conn->pktns.crypto.rx.ckm);
7065   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen,
7066                             NGTCP2_SECONDS);
7067 
7068   CU_ASSERT(0 == rv);
7069   CU_ASSERT(NGTCP2_ECN_STATE_FAILED == conn->tx.ecn.state);
7070   CU_ASSERT(NGTCP2_ECN_MAX_NUM_VALIDATION_PKTS ==
7071             conn->pktns.tx.ecn.validation_pkt_lost);
7072 
7073   ngtcp2_conn_del(conn);
7074 
7075   /* ECN validation fails if all ECN marked packets sent in last 3 *
7076      RTT are lost */
7077   setup_default_client(&conn);
7078 
7079   rv = ngtcp2_conn_open_bidi_stream(conn, &stream_id, NULL);
7080 
7081   CU_ASSERT(0 == rv);
7082 
7083   for (i = 0; i < 2; ++i) {
7084     spktlen = ngtcp2_conn_write_stream(conn, NULL, &pi, buf, sizeof(buf),
7085                                        &nwrite, NGTCP2_WRITE_STREAM_FLAG_NONE,
7086                                        stream_id, null_data, 25, 0);
7087 
7088     CU_ASSERT(0 < spktlen);
7089     CU_ASSERT(NGTCP2_ECN_ECT_0 == pi.ecn);
7090   }
7091 
7092   CU_ASSERT(NGTCP2_ECN_STATE_TESTING == conn->tx.ecn.state);
7093   CU_ASSERT(2 == conn->tx.ecn.dgram_sent);
7094 
7095   spktlen = ngtcp2_conn_write_stream(conn, NULL, &pi, buf, sizeof(buf), &nwrite,
7096                                      NGTCP2_WRITE_STREAM_FLAG_NONE, stream_id,
7097                                      null_data, 25, 3 * NGTCP2_SECONDS);
7098 
7099   CU_ASSERT(0 < spktlen);
7100   CU_ASSERT(NGTCP2_ECN_NOT_ECT == pi.ecn);
7101   CU_ASSERT(NGTCP2_ECN_STATE_UNKNOWN == conn->tx.ecn.state);
7102   CU_ASSERT(0 == conn->tx.ecn.validation_start_ts);
7103   CU_ASSERT(0 == conn->pktns.tx.ecn.start_pkt_num);
7104   CU_ASSERT(2 == conn->pktns.tx.ecn.validation_pkt_sent);
7105   CU_ASSERT(0 == conn->pktns.tx.ecn.validation_pkt_lost);
7106 
7107   fr.type = NGTCP2_FRAME_ACK;
7108   fr.ack.largest_ack = 2;
7109   fr.ack.ack_delay = 0;
7110   fr.ack.first_ack_blklen = 0;
7111   fr.ack.num_blks = 0;
7112 
7113   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, 0, &fr,
7114                                   conn->pktns.crypto.rx.ckm);
7115   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen,
7116                             4 * NGTCP2_SECONDS);
7117 
7118   CU_ASSERT(0 == rv);
7119   CU_ASSERT(NGTCP2_ECN_STATE_FAILED == conn->tx.ecn.state);
7120   CU_ASSERT(2 == conn->pktns.tx.ecn.validation_pkt_lost);
7121 
7122   ngtcp2_conn_del(conn);
7123 }
7124 
test_ngtcp2_conn_path_validation(void)7125 void test_ngtcp2_conn_path_validation(void) {
7126   ngtcp2_conn *conn;
7127   uint8_t buf[2048];
7128   size_t pktlen;
7129   ngtcp2_ssize spktlen;
7130   ngtcp2_tstamp t = 0;
7131   int64_t pkt_num = 0;
7132   ngtcp2_frame frs[4];
7133   int rv;
7134   ngtcp2_path_storage rpath, wpath;
7135   ngtcp2_pv_entry *ent;
7136 
7137   /* server starts path validation in NAT rebinding scenario. */
7138   setup_default_server(&conn);
7139 
7140   /* This will send NEW_CONNECTION_ID frames */
7141   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
7142 
7143   CU_ASSERT(spktlen > 0);
7144 
7145   frs[0].type = NGTCP2_FRAME_PING;
7146 
7147   /* Just change remote port */
7148   path_init(&rpath, 0, 0, 0, 1);
7149   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 1,
7150                      conn->pktns.crypto.rx.ckm);
7151   rv = ngtcp2_conn_read_pkt(conn, &rpath.path, &null_pi, buf, pktlen, ++t);
7152 
7153   CU_ASSERT(0 == rv);
7154   CU_ASSERT(NULL != conn->pv);
7155   CU_ASSERT(0 == conn->pv->dcid.seq);
7156   CU_ASSERT(ngtcp2_path_eq(&conn->pv->dcid.ps.path, &rpath.path));
7157 
7158   ngtcp2_path_storage_zero(&wpath);
7159   spktlen =
7160       ngtcp2_conn_write_pkt(conn, &wpath.path, NULL, buf, sizeof(buf), ++t);
7161 
7162   /* Server has not received enough bytes to pad probing packet. */
7163   CU_ASSERT(1200 > spktlen);
7164   CU_ASSERT(ngtcp2_path_eq(&rpath.path, &wpath.path));
7165   CU_ASSERT(1 == ngtcp2_ringbuf_len(&conn->pv->ents));
7166 
7167   ent = ngtcp2_ringbuf_get(&conn->pv->ents, 0);
7168 
7169   CU_ASSERT(ent->flags & NGTCP2_PV_ENTRY_FLAG_UNDERSIZED);
7170   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE);
7171   CU_ASSERT(!(conn->pv->flags & NGTCP2_PV_FLAG_MTU_PROBE));
7172 
7173   frs[0].type = NGTCP2_FRAME_PATH_RESPONSE;
7174   memcpy(frs[0].path_response.data, ent->data, sizeof(ent->data));
7175 
7176   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 1,
7177                      conn->pktns.crypto.rx.ckm);
7178   rv = ngtcp2_conn_read_pkt(conn, &rpath.path, &null_pi, buf, pktlen, ++t);
7179 
7180   CU_ASSERT(0 == rv);
7181 
7182   /* Start another path validation to probe least MTU */
7183   CU_ASSERT(NULL != conn->pv);
7184   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE);
7185   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_MTU_PROBE);
7186 
7187   ngtcp2_path_storage_zero(&wpath);
7188   spktlen =
7189       ngtcp2_conn_write_pkt(conn, &wpath.path, NULL, buf, sizeof(buf), ++t);
7190 
7191   CU_ASSERT(1200 <= spktlen);
7192   CU_ASSERT(ngtcp2_path_eq(&rpath.path, &wpath.path));
7193   CU_ASSERT(1 == ngtcp2_ringbuf_len(&conn->pv->ents));
7194 
7195   ent = ngtcp2_ringbuf_get(&conn->pv->ents, 0);
7196   frs[0].type = NGTCP2_FRAME_PATH_RESPONSE;
7197   memcpy(frs[0].path_response.data, ent->data, sizeof(ent->data));
7198 
7199   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 1,
7200                      conn->pktns.crypto.rx.ckm);
7201   rv = ngtcp2_conn_read_pkt(conn, &rpath.path, &null_pi, buf, pktlen, ++t);
7202 
7203   CU_ASSERT(0 == rv);
7204 
7205   /* Now perform another validation to old path */
7206   CU_ASSERT(NULL != conn->pv);
7207   CU_ASSERT(!(conn->pv->flags & NGTCP2_PV_FLAG_FALLBACK_ON_FAILURE));
7208   CU_ASSERT(!(conn->pv->flags & NGTCP2_PV_FLAG_MTU_PROBE));
7209   CU_ASSERT(conn->pv->flags & NGTCP2_PV_FLAG_DONT_CARE);
7210 
7211   ngtcp2_path_storage_zero(&wpath);
7212   spktlen =
7213       ngtcp2_conn_write_pkt(conn, &wpath.path, NULL, buf, sizeof(buf), ++t);
7214 
7215   CU_ASSERT(1200 <= spktlen);
7216   CU_ASSERT(ngtcp2_path_eq(&null_path.path, &wpath.path));
7217   CU_ASSERT(1 == ngtcp2_ringbuf_len(&conn->pv->ents));
7218 
7219   ngtcp2_conn_del(conn);
7220 }
7221 
test_ngtcp2_conn_early_data_sync_stream_data_limit(void)7222 void test_ngtcp2_conn_early_data_sync_stream_data_limit(void) {
7223   ngtcp2_conn *conn;
7224   uint8_t buf[1024];
7225   ngtcp2_ssize spktlen;
7226   ngtcp2_ssize datalen;
7227   int64_t bidi_stream_id, uni_stream_id;
7228   int rv;
7229   ngtcp2_frame fr;
7230   size_t pktlen;
7231   ngtcp2_crypto_aead_ctx aead_ctx = {0};
7232   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
7233   ngtcp2_transport_params params;
7234   ngtcp2_strm *strm;
7235   ngtcp2_tstamp t = 0;
7236 
7237   setup_early_client(&conn);
7238 
7239   rv = ngtcp2_conn_open_bidi_stream(conn, &bidi_stream_id, NULL);
7240 
7241   CU_ASSERT(0 == rv);
7242 
7243   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
7244                                      &datalen, NGTCP2_WRITE_STREAM_FLAG_FIN,
7245                                      bidi_stream_id, null_data, 1024, ++t);
7246 
7247   CU_ASSERT((ngtcp2_ssize)sizeof(buf) == spktlen);
7248   CU_ASSERT(674 == datalen);
7249 
7250   rv = ngtcp2_conn_open_uni_stream(conn, &uni_stream_id, NULL);
7251 
7252   CU_ASSERT(0 == rv);
7253 
7254   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
7255                                      &datalen, NGTCP2_WRITE_STREAM_FLAG_FIN,
7256                                      uni_stream_id, null_data, 1024, ++t);
7257 
7258   CU_ASSERT((ngtcp2_ssize)sizeof(buf) == spktlen);
7259   CU_ASSERT(958);
7260 
7261   fr.type = NGTCP2_FRAME_CRYPTO;
7262   fr.crypto.offset = 0;
7263   fr.crypto.datacnt = 1;
7264   fr.crypto.data[0].len = 198;
7265   fr.crypto.data[0].base = null_data;
7266 
7267   pktlen = write_single_frame_handshake_pkt(
7268       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
7269       ngtcp2_conn_get_dcid(conn), 0, NGTCP2_PROTO_VER_MAX, &fr, &null_ckm);
7270 
7271   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7272 
7273   CU_ASSERT(0 == rv);
7274 
7275   rv = ngtcp2_conn_install_rx_handshake_key(conn, &aead_ctx, null_iv,
7276                                             sizeof(null_iv), &hp_ctx);
7277 
7278   CU_ASSERT(0 == rv);
7279 
7280   rv = ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
7281                                             sizeof(null_iv), &hp_ctx);
7282 
7283   CU_ASSERT(0 == rv);
7284 
7285   rv = ngtcp2_conn_install_rx_key(conn, null_secret, sizeof(null_secret),
7286                                   &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
7287 
7288   CU_ASSERT(0 == rv);
7289 
7290   memset(&params, 0, sizeof(params));
7291   ngtcp2_cid_init(&params.initial_scid, conn->dcid.current.cid.data,
7292                   conn->dcid.current.cid.datalen);
7293   ngtcp2_cid_init(&params.original_dcid, conn->rcid.data, conn->rcid.datalen);
7294   params.max_udp_payload_size = 1200;
7295   params.initial_max_stream_data_bidi_local =
7296       conn->early.transport_params.initial_max_stream_data_bidi_local;
7297   params.initial_max_stream_data_bidi_remote = 640 * 1024;
7298   params.initial_max_stream_data_uni = 320 * 1024;
7299   params.initial_max_data = conn->early.transport_params.initial_max_data;
7300   params.initial_max_streams_bidi =
7301       conn->early.transport_params.initial_max_streams_bidi;
7302   params.initial_max_streams_uni =
7303       conn->early.transport_params.initial_max_streams_uni;
7304   params.active_connection_id_limit =
7305       conn->early.transport_params.active_connection_id_limit;
7306 
7307   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
7308 
7309   CU_ASSERT(0 == rv);
7310 
7311   rv = ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret),
7312                                   &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
7313 
7314   CU_ASSERT(0 == rv);
7315 
7316   ngtcp2_conn_handshake_completed(conn);
7317   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
7318 
7319   CU_ASSERT(0 < spktlen);
7320 
7321   strm = ngtcp2_conn_find_stream(conn, bidi_stream_id);
7322 
7323   CU_ASSERT(params.initial_max_stream_data_bidi_remote == strm->tx.max_offset);
7324 
7325   strm = ngtcp2_conn_find_stream(conn, uni_stream_id);
7326 
7327   CU_ASSERT(params.initial_max_stream_data_uni == strm->tx.max_offset);
7328 
7329   ngtcp2_conn_del(conn);
7330 }
7331 
test_ngtcp2_conn_early_data_rejected(void)7332 void test_ngtcp2_conn_early_data_rejected(void) {
7333   ngtcp2_conn *conn;
7334   uint8_t buf[1024];
7335   ngtcp2_ssize spktlen;
7336   ngtcp2_ssize datalen;
7337   int64_t bidi_stream_id, uni_stream_id;
7338   int rv;
7339   ngtcp2_frame fr;
7340   size_t pktlen;
7341   ngtcp2_crypto_aead_ctx aead_ctx = {0};
7342   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
7343   ngtcp2_transport_params params;
7344   ngtcp2_tstamp t = 0;
7345 
7346   setup_early_client(&conn);
7347 
7348   rv = ngtcp2_conn_open_bidi_stream(conn, &bidi_stream_id, NULL);
7349 
7350   CU_ASSERT(0 == rv);
7351 
7352   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
7353                                      &datalen, NGTCP2_WRITE_STREAM_FLAG_FIN,
7354                                      bidi_stream_id, null_data, 1024, ++t);
7355 
7356   CU_ASSERT((ngtcp2_ssize)sizeof(buf) == spktlen);
7357   CU_ASSERT(674 == datalen);
7358 
7359   rv = ngtcp2_conn_open_uni_stream(conn, &uni_stream_id, NULL);
7360 
7361   CU_ASSERT(0 == rv);
7362 
7363   ngtcp2_conn_extend_max_offset(conn, 1000);
7364   ngtcp2_conn_extend_max_streams_bidi(conn, 7);
7365   ngtcp2_conn_extend_max_streams_uni(conn, 5);
7366 
7367   spktlen = ngtcp2_conn_write_stream(conn, NULL, NULL, buf, sizeof(buf),
7368                                      &datalen, NGTCP2_WRITE_STREAM_FLAG_FIN,
7369                                      uni_stream_id, null_data, 300, ++t);
7370 
7371   CU_ASSERT(0 < spktlen);
7372   CU_ASSERT(0 < conn->tx.offset);
7373   CU_ASSERT(conn->local.transport_params.initial_max_data + 1000 ==
7374             conn->rx.unsent_max_offset);
7375   CU_ASSERT(conn->local.transport_params.initial_max_streams_bidi + 7 ==
7376             conn->remote.bidi.unsent_max_streams);
7377   CU_ASSERT(conn->local.transport_params.initial_max_streams_bidi + 7 ==
7378             conn->remote.bidi.max_streams);
7379   CU_ASSERT(conn->local.transport_params.initial_max_streams_uni + 5 ==
7380             conn->remote.uni.unsent_max_streams);
7381   CU_ASSERT(conn->local.transport_params.initial_max_streams_uni + 5 ==
7382             conn->remote.uni.max_streams);
7383 
7384   fr.type = NGTCP2_FRAME_CRYPTO;
7385   fr.crypto.offset = 0;
7386   fr.crypto.datacnt = 1;
7387   fr.crypto.data[0].len = 198;
7388   fr.crypto.data[0].base = null_data;
7389 
7390   pktlen = write_single_frame_handshake_pkt(
7391       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
7392       ngtcp2_conn_get_dcid(conn), 0, NGTCP2_PROTO_VER_MAX, &fr, &null_ckm);
7393 
7394   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7395 
7396   CU_ASSERT(0 == rv);
7397 
7398   rv = ngtcp2_conn_install_rx_handshake_key(conn, &aead_ctx, null_iv,
7399                                             sizeof(null_iv), &hp_ctx);
7400 
7401   CU_ASSERT(0 == rv);
7402 
7403   rv = ngtcp2_conn_install_tx_handshake_key(conn, &aead_ctx, null_iv,
7404                                             sizeof(null_iv), &hp_ctx);
7405 
7406   CU_ASSERT(0 == rv);
7407 
7408   rv = ngtcp2_conn_install_rx_key(conn, null_secret, sizeof(null_secret),
7409                                   &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
7410 
7411   CU_ASSERT(0 == rv);
7412 
7413   /* Stream limits in transport parameters can be reduced if early
7414      data is rejected. */
7415   memset(&params, 0, sizeof(params));
7416   ngtcp2_cid_init(&params.initial_scid, conn->dcid.current.cid.data,
7417                   conn->dcid.current.cid.datalen);
7418   ngtcp2_cid_init(&params.original_dcid, conn->rcid.data, conn->rcid.datalen);
7419   params.max_udp_payload_size = 1200;
7420   params.initial_max_stream_data_bidi_local =
7421       conn->early.transport_params.initial_max_stream_data_bidi_local;
7422   params.initial_max_stream_data_bidi_remote =
7423       conn->early.transport_params.initial_max_stream_data_bidi_remote / 2;
7424   params.initial_max_stream_data_uni = 0;
7425   params.initial_max_data = conn->early.transport_params.initial_max_data;
7426   params.initial_max_streams_bidi =
7427       conn->early.transport_params.initial_max_streams_bidi;
7428   params.initial_max_streams_uni =
7429       conn->early.transport_params.initial_max_streams_uni;
7430   params.active_connection_id_limit =
7431       conn->early.transport_params.active_connection_id_limit;
7432 
7433   rv = ngtcp2_conn_set_remote_transport_params(conn, &params);
7434 
7435   CU_ASSERT(0 == rv);
7436 
7437   rv = ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret),
7438                                   &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
7439 
7440   CU_ASSERT(0 == rv);
7441 
7442   ngtcp2_conn_handshake_completed(conn);
7443   ngtcp2_conn_early_data_rejected(conn);
7444   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
7445 
7446   CU_ASSERT(0 < spktlen);
7447   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, bidi_stream_id));
7448   CU_ASSERT(NULL == ngtcp2_conn_find_stream(conn, uni_stream_id));
7449   CU_ASSERT(0 == conn->tx.offset);
7450   CU_ASSERT(conn->local.transport_params.initial_max_data ==
7451             conn->rx.max_offset);
7452   CU_ASSERT(conn->local.transport_params.initial_max_data ==
7453             conn->rx.unsent_max_offset);
7454   CU_ASSERT(conn->local.transport_params.initial_max_streams_bidi ==
7455             conn->remote.bidi.max_streams);
7456   CU_ASSERT(conn->local.transport_params.initial_max_streams_bidi ==
7457             conn->remote.bidi.unsent_max_streams);
7458   CU_ASSERT(conn->local.transport_params.initial_max_streams_uni ==
7459             conn->remote.uni.max_streams);
7460   CU_ASSERT(conn->local.transport_params.initial_max_streams_uni ==
7461             conn->remote.uni.unsent_max_streams);
7462 
7463   ngtcp2_conn_del(conn);
7464 }
7465 
test_ngtcp2_conn_keep_alive(void)7466 void test_ngtcp2_conn_keep_alive(void) {
7467   ngtcp2_conn *conn;
7468   uint8_t buf[2048];
7469   ngtcp2_ssize spktlen;
7470   ngtcp2_pkt_info pi;
7471   ngtcp2_tstamp t = 0;
7472 
7473   setup_default_client(&conn);
7474 
7475   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), ++t);
7476 
7477   CU_ASSERT(0 < spktlen);
7478 
7479   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), ++t);
7480 
7481   CU_ASSERT(0 == spktlen);
7482 
7483   ngtcp2_conn_set_keep_alive_timeout(conn, 10 * NGTCP2_SECONDS);
7484 
7485   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), t);
7486 
7487   CU_ASSERT(0 == spktlen);
7488 
7489   t += 10 * NGTCP2_SECONDS;
7490 
7491   ngtcp2_conn_handle_expiry(conn, t);
7492 
7493   CU_ASSERT(conn->flags & NGTCP2_CONN_FLAG_KEEP_ALIVE_CANCELLED);
7494 
7495   spktlen = ngtcp2_conn_write_pkt(conn, NULL, &pi, buf, sizeof(buf), t);
7496 
7497   CU_ASSERT(0 < spktlen);
7498   CU_ASSERT(t == conn->keep_alive.last_ts);
7499 
7500   ngtcp2_conn_del(conn);
7501 }
7502 
test_ngtcp2_conn_retire_stale_bound_dcid(void)7503 void test_ngtcp2_conn_retire_stale_bound_dcid(void) {
7504   ngtcp2_conn *conn;
7505   uint8_t buf[2048];
7506   size_t pktlen;
7507   ngtcp2_tstamp t = 0;
7508   ngtcp2_tstamp expiry;
7509   int64_t pkt_num = 0;
7510   ngtcp2_frame fr;
7511   int rv;
7512   ngtcp2_cid cid;
7513   const uint8_t raw_cid[] = {0x0f, 0x00, 0x00, 0x00};
7514   const uint8_t token[NGTCP2_STATELESS_RESET_TOKENLEN] = {0xff};
7515   const uint8_t data[] = {0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8};
7516 
7517   ngtcp2_cid_init(&cid, raw_cid, sizeof(raw_cid));
7518 
7519   setup_default_server(&conn);
7520 
7521   fr.type = NGTCP2_FRAME_NEW_CONNECTION_ID;
7522   fr.new_connection_id.seq = 1;
7523   fr.new_connection_id.retire_prior_to = 0;
7524   fr.new_connection_id.cid = cid;
7525   memcpy(fr.new_connection_id.stateless_reset_token, token, sizeof(token));
7526 
7527   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
7528                                   &fr, conn->pktns.crypto.rx.ckm);
7529 
7530   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7531 
7532   CU_ASSERT(0 == rv);
7533 
7534   fr.type = NGTCP2_FRAME_PATH_CHALLENGE;
7535   memcpy(fr.path_challenge.data, data, sizeof(fr.path_challenge.data));
7536 
7537   pktlen = write_single_frame_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num,
7538                                   &fr, conn->pktns.crypto.rx.ckm);
7539 
7540   rv = ngtcp2_conn_read_pkt(conn, &new_path.path, &null_pi, buf, pktlen, ++t);
7541 
7542   CU_ASSERT(0 == rv);
7543   CU_ASSERT(ngtcp2_ringbuf_len(&conn->rx.path_challenge) > 0);
7544   CU_ASSERT(ngtcp2_ringbuf_len(&conn->dcid.bound) > 0);
7545 
7546   expiry = ngtcp2_conn_get_expiry(conn);
7547 
7548   CU_ASSERT(UINT64_MAX != expiry);
7549 
7550   t += 3 * ngtcp2_conn_get_pto(conn);
7551 
7552   rv = ngtcp2_conn_handle_expiry(conn, t);
7553 
7554   CU_ASSERT(0 == rv);
7555   CU_ASSERT(0 == ngtcp2_ringbuf_len(&conn->dcid.bound));
7556 
7557   ngtcp2_conn_del(conn);
7558 }
7559 
test_ngtcp2_conn_get_scid(void)7560 void test_ngtcp2_conn_get_scid(void) {
7561   ngtcp2_conn *conn;
7562   ngtcp2_settings settings;
7563   ngtcp2_transport_params params;
7564   ngtcp2_cid dcid, scid;
7565   ngtcp2_callbacks cb;
7566   const uint8_t raw_cid[] = {0x0f, 0x00, 0x00, 0x00};
7567   ngtcp2_cid scids[16];
7568 
7569   dcid_init(&dcid);
7570   dcid_init(&scid);
7571 
7572   server_default_callbacks(&cb);
7573   server_default_settings(&settings);
7574 
7575   /* Without preferred address */
7576   server_default_transport_params(&params);
7577 
7578   ngtcp2_conn_server_new(&conn, &dcid, &scid, &null_path.path,
7579                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
7580                          /* mem = */ NULL, NULL);
7581 
7582   CU_ASSERT(1 == ngtcp2_conn_get_num_scid(conn));
7583 
7584   ngtcp2_conn_get_scid(conn, scids);
7585 
7586   CU_ASSERT(ngtcp2_cid_eq(&scid, &scids[0]));
7587 
7588   ngtcp2_conn_del(conn);
7589 
7590   /* With preferred address */
7591   server_default_transport_params(&params);
7592   params.preferred_address_present = 1;
7593   ngtcp2_cid_init(&params.preferred_address.cid, raw_cid, sizeof(raw_cid));
7594 
7595   ngtcp2_conn_server_new(&conn, &dcid, &scid, &null_path.path,
7596                          NGTCP2_PROTO_VER_MAX, &cb, &settings, &params,
7597                          /* mem = */ NULL, NULL);
7598 
7599   CU_ASSERT(2 == ngtcp2_conn_get_num_scid(conn));
7600 
7601   ngtcp2_conn_get_scid(conn, scids);
7602 
7603   CU_ASSERT(ngtcp2_cid_eq(&scid, &scids[0]));
7604   CU_ASSERT(ngtcp2_cid_eq(&params.preferred_address.cid, &scids[1]));
7605 
7606   ngtcp2_conn_del(conn);
7607 }
7608 
test_ngtcp2_conn_stream_close(void)7609 void test_ngtcp2_conn_stream_close(void) {
7610   ngtcp2_conn *conn;
7611   int rv;
7612   uint8_t buf[2048];
7613   ngtcp2_frame frs[2];
7614   size_t pktlen;
7615   int64_t pkt_num = 0;
7616   my_user_data ud;
7617   ngtcp2_strm *strm;
7618   ngtcp2_tstamp t = 0;
7619   ngtcp2_ssize spktlen;
7620 
7621   /* Receive RESET_STREAM and STOP_SENDING from client */
7622   setup_default_server(&conn);
7623   conn->callbacks.stream_close = stream_close;
7624   conn->user_data = &ud;
7625 
7626   open_stream(conn, 0);
7627 
7628   frs[0].type = NGTCP2_FRAME_RESET_STREAM;
7629   frs[0].reset_stream.stream_id = 0;
7630   frs[0].reset_stream.app_error_code = NGTCP2_APP_ERR01;
7631   frs[0].reset_stream.final_size = 999;
7632 
7633   frs[1].type = NGTCP2_FRAME_STOP_SENDING;
7634   frs[1].stop_sending.stream_id = 0;
7635   frs[1].stop_sending.app_error_code = NGTCP2_APP_ERR02;
7636 
7637   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 2,
7638                      conn->pktns.crypto.tx.ckm);
7639 
7640   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7641 
7642   CU_ASSERT(0 == rv);
7643 
7644   strm = ngtcp2_conn_find_stream(conn, 0);
7645 
7646   CU_ASSERT(NGTCP2_APP_ERR01 == strm->app_error_code);
7647 
7648   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
7649 
7650   CU_ASSERT(spktlen > 0);
7651   CU_ASSERT((size_t)spktlen < sizeof(buf));
7652 
7653   frs[0].type = NGTCP2_FRAME_ACK;
7654   frs[0].ack.largest_ack = 0;
7655   frs[0].ack.ack_delay = 0;
7656   frs[0].ack.first_ack_blklen = 0;
7657   frs[0].ack.num_blks = 0;
7658 
7659   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 1,
7660                      conn->pktns.crypto.tx.ckm);
7661 
7662   ud.stream_close.flags = NGTCP2_STREAM_CLOSE_FLAG_NONE;
7663   ud.stream_close.stream_id = -1;
7664   ud.stream_close.app_error_code = 0;
7665 
7666   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7667 
7668   CU_ASSERT(0 == rv);
7669 
7670   CU_ASSERT((NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET |
7671              ud.stream_close.flags) != 0);
7672   CU_ASSERT(0 == ud.stream_close.stream_id);
7673   CU_ASSERT(NGTCP2_APP_ERR01 == ud.stream_close.app_error_code);
7674 
7675   ngtcp2_conn_del(conn);
7676 
7677   /* Client sends STOP_SENDING and then STREAM and fin */
7678   pkt_num = 0;
7679 
7680   setup_default_server(&conn);
7681   conn->callbacks.stream_close = stream_close;
7682   conn->callbacks.recv_stream_data = recv_stream_data;
7683   conn->user_data = &ud;
7684 
7685   frs[0].type = NGTCP2_FRAME_STOP_SENDING;
7686   frs[0].stop_sending.stream_id = 0;
7687   frs[0].stop_sending.app_error_code = NGTCP2_APP_ERR01;
7688 
7689   frs[1].type = NGTCP2_FRAME_STREAM;
7690   frs[1].stream.flags = 0;
7691   frs[1].stream.fin = 1;
7692   frs[1].stream.stream_id = 0;
7693   frs[1].stream.offset = 0;
7694   frs[1].stream.datacnt = 0;
7695 
7696   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 2,
7697                      conn->pktns.crypto.tx.ckm);
7698 
7699   ud.stream_data.stream_id = -1;
7700   ud.stream_data.flags = NGTCP2_STREAM_DATA_FLAG_NONE;
7701   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7702 
7703   CU_ASSERT(0 == rv);
7704   CU_ASSERT(0 == ud.stream_data.stream_id);
7705   CU_ASSERT((ud.stream_data.flags & NGTCP2_STREAM_DATA_FLAG_FIN) != 0);
7706 
7707   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
7708 
7709   CU_ASSERT(spktlen > 0);
7710   CU_ASSERT((size_t)spktlen < sizeof(buf));
7711 
7712   frs[0].type = NGTCP2_FRAME_ACK;
7713   frs[0].ack.largest_ack = 0;
7714   frs[0].ack.ack_delay = 0;
7715   frs[0].ack.first_ack_blklen = 0;
7716   frs[0].ack.num_blks = 0;
7717 
7718   pktlen = write_pkt(buf, sizeof(buf), &conn->oscid, ++pkt_num, frs, 1,
7719                      conn->pktns.crypto.tx.ckm);
7720 
7721   ud.stream_close.flags = NGTCP2_STREAM_CLOSE_FLAG_NONE;
7722   ud.stream_close.stream_id = -1;
7723   ud.stream_close.app_error_code = 0;
7724 
7725   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7726 
7727   CU_ASSERT(0 == rv);
7728 
7729   CU_ASSERT((NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET |
7730              ud.stream_close.flags) != 0);
7731   CU_ASSERT(0 == ud.stream_close.stream_id);
7732   CU_ASSERT(NGTCP2_APP_ERR01 == ud.stream_close.app_error_code);
7733 
7734   ngtcp2_conn_del(conn);
7735 }
7736 
test_ngtcp2_conn_buffer_pkt(void)7737 void test_ngtcp2_conn_buffer_pkt(void) {
7738   ngtcp2_conn *conn;
7739   int rv;
7740   uint8_t buf[2048];
7741   ngtcp2_frame fr;
7742   size_t pktlen, in_pktlen;
7743   int64_t pkt_num = 0;
7744   ngtcp2_tstamp t = 0;
7745   ngtcp2_ssize spktlen;
7746   ngtcp2_crypto_aead_ctx aead_ctx = {0};
7747   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
7748   ngtcp2_ksl_it it;
7749   ngtcp2_pkt_chain *pc;
7750 
7751   /* Server should buffer Short packet if it does not complete
7752      handshake even if it has application tx key. */
7753   setup_handshake_server(&conn);
7754 
7755   fr.type = NGTCP2_FRAME_CRYPTO;
7756   fr.crypto.offset = 0;
7757   fr.crypto.datacnt = 1;
7758   fr.crypto.data[0].len = 133;
7759   fr.crypto.data[0].base = null_data;
7760 
7761   pktlen = write_single_frame_handshake_pkt(
7762       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
7763       ngtcp2_conn_get_dcid(conn), pkt_num++, NGTCP2_PROTO_VER_MAX, &fr,
7764       &null_ckm);
7765   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf, pktlen, ++t);
7766 
7767   CU_ASSERT(0 == rv);
7768 
7769   rv = ngtcp2_conn_install_tx_key(conn, null_secret, sizeof(null_secret),
7770                                   &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
7771 
7772   assert(0 == rv);
7773 
7774   rv = ngtcp2_conn_install_rx_key(conn, null_secret, sizeof(null_secret),
7775                                   &aead_ctx, null_iv, sizeof(null_iv), &hp_ctx);
7776 
7777   assert(0 == rv);
7778 
7779   spktlen = ngtcp2_conn_write_pkt(conn, NULL, NULL, buf, sizeof(buf), ++t);
7780 
7781   CU_ASSERT(spktlen > 0);
7782 
7783   fr.type = NGTCP2_FRAME_PING;
7784 
7785   in_pktlen = write_single_frame_handshake_pkt(
7786       buf, sizeof(buf), NGTCP2_PKT_INITIAL, &conn->oscid,
7787       ngtcp2_conn_get_dcid(conn), pkt_num++, NGTCP2_PROTO_VER_MAX, &fr,
7788       &null_ckm);
7789 
7790   pktlen = write_single_frame_pkt(buf + in_pktlen, sizeof(buf) - in_pktlen,
7791                                   &conn->oscid, pkt_num++, &fr, &null_ckm);
7792 
7793   CU_ASSERT(!conn->pktns.rx.buffed_pkts);
7794 
7795   rv = ngtcp2_conn_read_pkt(conn, &null_path.path, &null_pi, buf,
7796                             in_pktlen + pktlen, ++t);
7797 
7798   CU_ASSERT(0 == rv);
7799 
7800   pc = conn->pktns.rx.buffed_pkts;
7801 
7802   CU_ASSERT(pktlen == pc->pktlen);
7803   CU_ASSERT(in_pktlen + pktlen == pc->dgramlen);
7804 
7805   it = ngtcp2_acktr_get(&conn->pktns.acktr);
7806 
7807   CU_ASSERT(ngtcp2_ksl_it_end(&it));
7808 
7809   ngtcp2_conn_del(conn);
7810 }
7811 
test_ngtcp2_accept(void)7812 void test_ngtcp2_accept(void) {
7813   size_t pktlen;
7814   uint8_t buf[2048];
7815   ngtcp2_cid dcid, scid;
7816   ngtcp2_frame fr;
7817   int rv;
7818   ngtcp2_pkt_hd hd;
7819 
7820   dcid_init(&dcid);
7821   scid_init(&scid);
7822 
7823   /* Initial packet */
7824   memset(&hd, 0, sizeof(hd));
7825 
7826   fr.type = NGTCP2_FRAME_CRYPTO;
7827   fr.crypto.offset = 0;
7828   fr.crypto.datacnt = 1;
7829   fr.crypto.data[0].len = 1200;
7830   fr.crypto.data[0].base = null_data;
7831 
7832   pktlen = write_single_frame_initial_pkt(buf, sizeof(buf), &dcid, &scid, 0,
7833                                           NGTCP2_PROTO_VER_V1, &fr, NULL, 0,
7834                                           &null_ckm);
7835 
7836   CU_ASSERT(pktlen >= 1200);
7837 
7838   rv = ngtcp2_accept(&hd, buf, pktlen);
7839 
7840   CU_ASSERT(0 == rv);
7841   CU_ASSERT(ngtcp2_cid_eq(&dcid, &hd.dcid));
7842   CU_ASSERT(ngtcp2_cid_eq(&scid, &hd.scid));
7843   CU_ASSERT(0 == hd.token.len);
7844   CU_ASSERT(hd.len > 0);
7845   CU_ASSERT(NGTCP2_PROTO_VER_V1 == hd.version);
7846   CU_ASSERT(NGTCP2_PKT_INITIAL == hd.type);
7847   CU_ASSERT(hd.flags & NGTCP2_PKT_FLAG_LONG_FORM);
7848 
7849   /* 0RTT packet */
7850   memset(&hd, 0, sizeof(hd));
7851 
7852   fr.type = NGTCP2_FRAME_STREAM;
7853   fr.stream.flags = 0;
7854   fr.stream.stream_id = 0;
7855   fr.stream.fin = 0;
7856   fr.stream.offset = 0;
7857   fr.stream.datacnt = 1;
7858   fr.stream.data[0].len = 1200;
7859   fr.stream.data[0].base = null_data;
7860 
7861   pktlen = write_single_frame_0rtt_pkt(buf, sizeof(buf), &dcid, &scid, 1,
7862                                        NGTCP2_PROTO_VER_V1, &fr, &null_ckm);
7863 
7864   CU_ASSERT(pktlen >= 1200);
7865 
7866   rv = ngtcp2_accept(&hd, buf, pktlen);
7867 
7868   CU_ASSERT(NGTCP2_ERR_RETRY == rv);
7869   CU_ASSERT(ngtcp2_cid_eq(&dcid, &hd.dcid));
7870   CU_ASSERT(ngtcp2_cid_eq(&scid, &hd.scid));
7871   CU_ASSERT(0 == hd.token.len);
7872   CU_ASSERT(hd.len > 0);
7873   CU_ASSERT(NGTCP2_PROTO_VER_V1 == hd.version);
7874   CU_ASSERT(NGTCP2_PKT_0RTT == hd.type);
7875   CU_ASSERT(hd.flags & NGTCP2_PKT_FLAG_LONG_FORM);
7876 
7877   /* Unknown version */
7878   memset(&hd, 0, sizeof(hd));
7879 
7880   fr.type = NGTCP2_FRAME_CRYPTO;
7881   fr.crypto.offset = 0;
7882   fr.crypto.datacnt = 1;
7883   fr.crypto.data[0].len = 1200;
7884   fr.crypto.data[0].base = null_data;
7885 
7886   pktlen =
7887       write_single_frame_handshake_pkt(buf, sizeof(buf), NGTCP2_PKT_INITIAL,
7888                                        &dcid, &scid, 0, 0x2, &fr, &null_ckm);
7889 
7890   CU_ASSERT(pktlen >= 1200);
7891 
7892   rv = ngtcp2_accept(&hd, buf, pktlen);
7893 
7894   CU_ASSERT(NGTCP2_ERR_VERSION_NEGOTIATION == rv);
7895 
7896   /* Short packet */
7897   memset(&hd, 0, sizeof(hd));
7898 
7899   fr.type = NGTCP2_FRAME_CRYPTO;
7900   fr.crypto.offset = 0;
7901   fr.crypto.datacnt = 1;
7902   fr.crypto.data[0].len = 1200;
7903   fr.crypto.data[0].base = null_data;
7904 
7905   pktlen = write_single_frame_pkt(buf, sizeof(buf), &dcid, 0, &fr, &null_ckm);
7906 
7907   CU_ASSERT(pktlen >= 1200);
7908 
7909   rv = ngtcp2_accept(&hd, buf, pktlen);
7910 
7911   CU_ASSERT(NGTCP2_ERR_INVALID_ARGUMENT == rv);
7912 
7913   /* Unable to decode packet header */
7914   memset(&hd, 0, sizeof(hd));
7915 
7916   memset(buf, 0, 4);
7917   buf[0] = NGTCP2_HEADER_FORM_BIT;
7918 
7919   rv = ngtcp2_accept(&hd, buf, 4);
7920 
7921   CU_ASSERT(NGTCP2_ERR_INVALID_ARGUMENT == rv);
7922 }
7923 
test_ngtcp2_pkt_write_connection_close(void)7924 void test_ngtcp2_pkt_write_connection_close(void) {
7925   ngtcp2_ssize spktlen;
7926   uint8_t buf[1200];
7927   ngtcp2_cid dcid, scid;
7928   ngtcp2_crypto_aead aead = {0, NGTCP2_INITIAL_AEAD_OVERHEAD};
7929   ngtcp2_crypto_cipher hp_mask = {0};
7930   ngtcp2_crypto_aead_ctx aead_ctx = {0};
7931   ngtcp2_crypto_cipher_ctx hp_ctx = {0};
7932 
7933   dcid_init(&dcid);
7934   scid_init(&scid);
7935 
7936   spktlen = ngtcp2_pkt_write_connection_close(
7937       buf, sizeof(buf), NGTCP2_PROTO_VER_MAX, &dcid, &scid,
7938       NGTCP2_INVALID_TOKEN, null_encrypt, &aead, &aead_ctx, null_iv,
7939       null_hp_mask, &hp_mask, &hp_ctx);
7940 
7941   CU_ASSERT(spktlen > 0);
7942 
7943   spktlen = ngtcp2_pkt_write_connection_close(
7944       buf, 16, NGTCP2_PROTO_VER_MAX, &dcid, &scid, NGTCP2_INVALID_TOKEN,
7945       null_encrypt, &aead, &aead_ctx, null_iv, null_hp_mask, &hp_mask, &hp_ctx);
7946 
7947   CU_ASSERT(NGTCP2_ERR_NOBUF == spktlen);
7948 }
7949