1a74702c6SGeorge Wang/* Copyright (c) 2017 - 2022 LiteSpeed Technologies Inc.  See LICENSE. */
250aadb33SDmitri Tikhonov/* Tests in this file have been migrated out of maintest.c */
350aadb33SDmitri Tikhonov/* TODO: fix warnings */
450aadb33SDmitri Tikhonov
550aadb33SDmitri Tikhonov#include <assert.h>
650aadb33SDmitri Tikhonov#include <stdlib.h>
750aadb33SDmitri Tikhonov#include <string.h>
850aadb33SDmitri Tikhonov#include <stdio.h>
950aadb33SDmitri Tikhonov#include <sys/queue.h>
1050aadb33SDmitri Tikhonov
1150aadb33SDmitri Tikhonov#include "lsquic.h"
1250aadb33SDmitri Tikhonov#include "lsquic_types.h"
1350aadb33SDmitri Tikhonov#include "lsquic_parse.h"
1450aadb33SDmitri Tikhonov#include "lsquic_sfcw.h"
155392f7a3SLiteSpeed Tech#include "lsquic_varint.h"
165392f7a3SLiteSpeed Tech#include "lsquic_hq.h"
175392f7a3SLiteSpeed Tech#include "lsquic_hash.h"
1850aadb33SDmitri Tikhonov#include "lsquic_stream.h"
19bfc7bfd8SDmitri Tikhonov#include "lsquic_packet_common.h"
2050aadb33SDmitri Tikhonov#include "lsquic_packet_in.h"
2150aadb33SDmitri Tikhonov
2250aadb33SDmitri Tikhonovstruct lsquic_stream_if;
2350aadb33SDmitri Tikhonov
24052a1c28SDmitri Tikhonovstatic const struct parse_funcs *const pf = select_pf_by_ver(LSQVER_035);
2550aadb33SDmitri Tikhonov
2650aadb33SDmitri Tikhonovlsquic_stream_t *
2750aadb33SDmitri Tikhonovlsquic_stream_new_ext (uint32_t id,
2850aadb33SDmitri Tikhonov                   struct lsquic_conn_public *conn_pub,
2950aadb33SDmitri Tikhonov                   const struct lsquic_stream_if *stream_if,
3050aadb33SDmitri Tikhonov                   void *stream_if_ctx, unsigned initial_sfcw,
3150aadb33SDmitri Tikhonov                   unsigned initial_send_off, enum stream_ctor_flags ctor_flags)
3250aadb33SDmitri Tikhonov{
3350aadb33SDmitri Tikhonov    lsquic_stream_t *stream = calloc(1, sizeof(*stream));
3450aadb33SDmitri Tikhonov    stream->id = id;
3550aadb33SDmitri Tikhonov    return stream;
3650aadb33SDmitri Tikhonov}
3750aadb33SDmitri Tikhonov
3850aadb33SDmitri Tikhonovuint64_t
3950aadb33SDmitri Tikhonovlsquic_stream_tosend_offset (const lsquic_stream_t *stream)
4050aadb33SDmitri Tikhonov{
4150aadb33SDmitri Tikhonov    return 1000;
4250aadb33SDmitri Tikhonov}
4350aadb33SDmitri Tikhonov
4450aadb33SDmitri Tikhonovint
4550aadb33SDmitri Tikhonovlsquic_stream_tosend_fin (const lsquic_stream_t *stream)
4650aadb33SDmitri Tikhonov{
4750aadb33SDmitri Tikhonov    return 0;
4850aadb33SDmitri Tikhonov}
4950aadb33SDmitri Tikhonov
5050aadb33SDmitri Tikhonovsize_t
5150aadb33SDmitri Tikhonovlsquic_stream_tosend_read (lsquic_stream_t *stream, void *buf, size_t len,
5250aadb33SDmitri Tikhonov                           int *reached_fin)
5350aadb33SDmitri Tikhonov{
5450aadb33SDmitri Tikhonov    memcpy(buf, "123456789012345678901234567890", 30);
5550aadb33SDmitri Tikhonov    *reached_fin = lsquic_stream_tosend_fin(stream);
5650aadb33SDmitri Tikhonov    return 30;
5750aadb33SDmitri Tikhonov}
5850aadb33SDmitri Tikhonov
5950aadb33SDmitri Tikhonovsize_t
6050aadb33SDmitri Tikhonovlsquic_stream_tosend_sz (const lsquic_stream_t *stream)
6150aadb33SDmitri Tikhonov{
6250aadb33SDmitri Tikhonov    return 30;
6350aadb33SDmitri Tikhonov}
6450aadb33SDmitri Tikhonov
6550aadb33SDmitri Tikhonovstatic int make_complex_packet(unsigned char *pkt_buf, int max_buf_len)
6650aadb33SDmitri Tikhonov{
679626cfc2SDmitri Tikhonov#if 0       /* What is this function testing?  Seems useless. */
6850aadb33SDmitri Tikhonov    unsigned char *p = pkt_buf;
6950aadb33SDmitri Tikhonov    unsigned char *const pend  = p + 1500;
7050aadb33SDmitri Tikhonov    lsquic_stream_t *stream = lsquic_stream_new(12345, NULL, NULL, NULL, 0, 0);
7150aadb33SDmitri Tikhonov    uint32_t stream_id = 13989;
7250aadb33SDmitri Tikhonov    uint64_t offset = 10000;
7350aadb33SDmitri Tikhonov    uint64_t conn_id = 123579;
7450aadb33SDmitri Tikhonov    const char nonce[] = "1234567890ABCDEF1234567890abcdef"; /*32 bytes*/
7550aadb33SDmitri Tikhonov    uint64_t packet_num = 1356789;
7650aadb33SDmitri Tikhonov
7750aadb33SDmitri Tikhonov    int buf_len = pf->pf_gen_reg_pkt_header(p, 100, &conn_id, NULL, (const unsigned char *) nonce, packet_num,
7850aadb33SDmitri Tikhonov                                                calc_packno_bits(packet_num, 0, 0));
7950aadb33SDmitri Tikhonov    assert(buf_len > 0);
8050aadb33SDmitri Tikhonov    p += buf_len;
8150aadb33SDmitri Tikhonov
8250aadb33SDmitri Tikhonov    buf_len = pf->pf_gen_stream_frame(p, pend - p,
8350aadb33SDmitri Tikhonov        stream->id, lsquic_stream_tosend_offset(stream),
84bfc7bfd8SDmitri Tikhonov        lsquic_stream_tosend_fin(stream),
85bfc7bfd8SDmitri Tikhonov        lsquic_stream_tosend_sz(stream),
8650aadb33SDmitri Tikhonov        (gsf_read_f) lsquic_stream_tosend_read,
8750aadb33SDmitri Tikhonov        stream);
8850aadb33SDmitri Tikhonov    p += buf_len;
8950aadb33SDmitri Tikhonov
9050aadb33SDmitri Tikhonov    buf_len = pf->pf_gen_window_update_frame(p, pend - p, stream_id, offset);
9150aadb33SDmitri Tikhonov    p += buf_len;
9250aadb33SDmitri Tikhonov
9350aadb33SDmitri Tikhonov    free(stream);
9450aadb33SDmitri Tikhonov
9550aadb33SDmitri Tikhonov    return p - pkt_buf;
969626cfc2SDmitri Tikhonov#endif
979626cfc2SDmitri Tikhonov    return 0;
9850aadb33SDmitri Tikhonov}
9950aadb33SDmitri Tikhonov
10050aadb33SDmitri Tikhonov
10150aadb33SDmitri Tikhonovvoid test_stream_frame()
10250aadb33SDmitri Tikhonov{
10350aadb33SDmitri Tikhonov    const uint8_t data[] = "123456789012345678901234567890";
10450aadb33SDmitri Tikhonov    lsquic_stream_t *stream = lsquic_stream_new(12345, NULL, NULL, NULL, 0, 0);
10550aadb33SDmitri Tikhonov    uint8_t buf[1500];
10650aadb33SDmitri Tikhonov    int buf_len = pf->pf_gen_stream_frame(buf, 1500,
10750aadb33SDmitri Tikhonov        stream->id, lsquic_stream_tosend_offset(stream),
108bfc7bfd8SDmitri Tikhonov        lsquic_stream_tosend_fin(stream),
109bfc7bfd8SDmitri Tikhonov        lsquic_stream_tosend_sz(stream),
11050aadb33SDmitri Tikhonov        (gsf_read_f) lsquic_stream_tosend_read,
11150aadb33SDmitri Tikhonov        stream);
11250aadb33SDmitri Tikhonov    stream_frame_t stream_frame2;
11350aadb33SDmitri Tikhonov    pf->pf_parse_stream_frame(buf, buf_len, &stream_frame2);
11450aadb33SDmitri Tikhonov    assert(0 == stream_frame2.data_frame.df_fin );
11550aadb33SDmitri Tikhonov    assert(30 == stream_frame2.data_frame.df_size );
11650aadb33SDmitri Tikhonov    assert(1000 == stream_frame2.data_frame.df_offset );
11750aadb33SDmitri Tikhonov    assert(12345 == stream_frame2.stream_id);
11850aadb33SDmitri Tikhonov    assert(memcmp(data,stream_frame2.data_frame.df_data, stream_frame2.data_frame.df_size) == 0);
11950aadb33SDmitri Tikhonov    printf("test_stream_frame passed.\n");
12050aadb33SDmitri Tikhonov    free(stream);
12150aadb33SDmitri Tikhonov}
12250aadb33SDmitri Tikhonov
12350aadb33SDmitri Tikhonov
12450aadb33SDmitri Tikhonovint
12550aadb33SDmitri Tikhonovmain (void)
12650aadb33SDmitri Tikhonov{
12750aadb33SDmitri Tikhonov    uint8_t pkt_buf[1500];
12850aadb33SDmitri Tikhonov    size_t buf_len;
12950aadb33SDmitri Tikhonov    buf_len = make_complex_packet(pkt_buf, 1500);
13050aadb33SDmitri Tikhonov    (void) buf_len;
13150aadb33SDmitri Tikhonov    test_stream_frame();
13250aadb33SDmitri Tikhonov    return 0;
13350aadb33SDmitri Tikhonov}
134