test_some_packets.c revision a74702c6
1/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
2/* Tests in this file have been migrated out of maintest.c */
3/* TODO: fix warnings */
4
5#include <assert.h>
6#include <stdlib.h>
7#include <string.h>
8#include <stdio.h>
9#include <sys/queue.h>
10
11#include "lsquic.h"
12#include "lsquic_types.h"
13#include "lsquic_parse.h"
14#include "lsquic_sfcw.h"
15#include "lsquic_varint.h"
16#include "lsquic_hq.h"
17#include "lsquic_hash.h"
18#include "lsquic_stream.h"
19#include "lsquic_packet_common.h"
20#include "lsquic_packet_in.h"
21
22struct lsquic_stream_if;
23
24static const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_035);
25
26lsquic_stream_t *
27lsquic_stream_new_ext (uint32_t id,
28                   struct lsquic_conn_public *conn_pub,
29                   const struct lsquic_stream_if *stream_if,
30                   void *stream_if_ctx, unsigned initial_sfcw,
31                   unsigned initial_send_off, enum stream_ctor_flags ctor_flags)
32{
33    lsquic_stream_t *stream = calloc(1, sizeof(*stream));
34    stream->id = id;
35    return stream;
36}
37
38uint64_t
39lsquic_stream_tosend_offset (const lsquic_stream_t *stream)
40{
41    return 1000;
42}
43
44int
45lsquic_stream_tosend_fin (const lsquic_stream_t *stream)
46{
47    return 0;
48}
49
50size_t
51lsquic_stream_tosend_read (lsquic_stream_t *stream, void *buf, size_t len,
52                           int *reached_fin)
53{
54    memcpy(buf, "123456789012345678901234567890", 30);
55    *reached_fin = lsquic_stream_tosend_fin(stream);
56    return 30;
57}
58
59size_t
60lsquic_stream_tosend_sz (const lsquic_stream_t *stream)
61{
62    return 30;
63}
64
65static int make_complex_packet(unsigned char *pkt_buf, int max_buf_len)
66{
67#if 0       /* What is this function testing?  Seems useless. */
68    unsigned char *p = pkt_buf;
69    unsigned char *const pend  = p + 1500;
70    lsquic_stream_t *stream = lsquic_stream_new(12345, NULL, NULL, NULL, 0, 0);
71    uint32_t stream_id = 13989;
72    uint64_t offset = 10000;
73    uint64_t conn_id = 123579;
74    const char nonce[] = "1234567890ABCDEF1234567890abcdef"; /*32 bytes*/
75    uint64_t packet_num = 1356789;
76
77    int buf_len = pf->pf_gen_reg_pkt_header(p, 100, &conn_id, NULL, (const unsigned char *) nonce, packet_num,
78                                                calc_packno_bits(packet_num, 0, 0));
79    assert(buf_len > 0);
80    p += buf_len;
81
82    buf_len = pf->pf_gen_stream_frame(p, pend - p,
83        stream->id, lsquic_stream_tosend_offset(stream),
84        lsquic_stream_tosend_fin(stream),
85        lsquic_stream_tosend_sz(stream),
86        (gsf_read_f) lsquic_stream_tosend_read,
87        stream);
88    p += buf_len;
89
90    buf_len = pf->pf_gen_window_update_frame(p, pend - p, stream_id, offset);
91    p += buf_len;
92
93    free(stream);
94
95    return p - pkt_buf;
96#endif
97    return 0;
98}
99
100
101void test_stream_frame()
102{
103    const uint8_t data[] = "123456789012345678901234567890";
104    lsquic_stream_t *stream = lsquic_stream_new(12345, NULL, NULL, NULL, 0, 0);
105    uint8_t buf[1500];
106    int buf_len = pf->pf_gen_stream_frame(buf, 1500,
107        stream->id, lsquic_stream_tosend_offset(stream),
108        lsquic_stream_tosend_fin(stream),
109        lsquic_stream_tosend_sz(stream),
110        (gsf_read_f) lsquic_stream_tosend_read,
111        stream);
112    stream_frame_t stream_frame2;
113    pf->pf_parse_stream_frame(buf, buf_len, &stream_frame2);
114    assert(0 == stream_frame2.data_frame.df_fin );
115    assert(30 == stream_frame2.data_frame.df_size );
116    assert(1000 == stream_frame2.data_frame.df_offset );
117    assert(12345 == stream_frame2.stream_id);
118    assert(memcmp(data,stream_frame2.data_frame.df_data, stream_frame2.data_frame.df_size) == 0);
119    printf("test_stream_frame passed.\n");
120    free(stream);
121}
122
123
124int
125main (void)
126{
127    uint8_t pkt_buf[1500];
128    size_t buf_len;
129    buf_len = make_complex_packet(pkt_buf, 1500);
130    (void) buf_len;
131    test_stream_frame();
132    return 0;
133}
134