aboutsummaryrefslogtreecommitdiff
path: root/net/dcsctp/packet/chunk/idata_chunk_test.cc
blob: d1cc0d8f9032a106b89e6717e603d6f9e37403b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 *  Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */
#include "net/dcsctp/packet/chunk/idata_chunk.h"

#include <cstdint>
#include <type_traits>
#include <vector>

#include "api/array_view.h"
#include "net/dcsctp/testing/testing_macros.h"
#include "rtc_base/gunit.h"
#include "test/gmock.h"

namespace dcsctp {
namespace {
using ::testing::ElementsAre;

TEST(IDataChunkTest, AtBeginningFromCapture) {
  /*
  I_DATA chunk(ordered, first segment, TSN: 2487901653, SID: 1, MID: 0,
    payload length: 1180 bytes)
      Chunk type: I_DATA (64)
      Chunk flags: 0x02
      Chunk length: 1200
      Transmission sequence number: 2487901653
      Stream identifier: 0x0001
      Reserved: 0
      Message identifier: 0
      Payload protocol identifier: WebRTC Binary (53)
      Reassembled Message in frame: 39
  */

  uint8_t data[] = {0x40, 0x02, 0x00, 0x15, 0x94, 0x4a, 0x5d, 0xd5,
                    0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, 0x35, 0x01, 0x00, 0x00, 0x00};

  ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk chunk, IDataChunk::Parse(data));
  EXPECT_EQ(*chunk.tsn(), 2487901653);
  EXPECT_EQ(*chunk.stream_id(), 1);
  EXPECT_EQ(*chunk.mid(), 0u);
  EXPECT_EQ(*chunk.ppid(), 53u);
  EXPECT_EQ(*chunk.fsn(), 0u);  // Not provided (so set to zero)
}

TEST(IDataChunkTest, AtBeginningSerializeAndDeserialize) {
  IDataChunk::Options options;
  options.is_beginning = Data::IsBeginning(true);
  IDataChunk chunk(TSN(123), StreamID(456), MID(789), PPID(53), FSN(0), {1},
                   options);

  std::vector<uint8_t> serialized;
  chunk.SerializeTo(serialized);

  ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk deserialized,
                              IDataChunk::Parse(serialized));
  EXPECT_EQ(*deserialized.tsn(), 123u);
  EXPECT_EQ(*deserialized.stream_id(), 456u);
  EXPECT_EQ(*deserialized.mid(), 789u);
  EXPECT_EQ(*deserialized.ppid(), 53u);
  EXPECT_EQ(*deserialized.fsn(), 0u);

  EXPECT_EQ(deserialized.ToString(),
            "I-DATA, type=ordered::first, tsn=123, stream_id=456, "
            "mid=789, ppid=53, length=1");
}

TEST(IDataChunkTest, InMiddleFromCapture) {
  /*
  I_DATA chunk(ordered, last segment, TSN: 2487901706, SID: 3, MID: 1,
    FSN: 8, payload length: 560 bytes)
      Chunk type: I_DATA (64)
      Chunk flags: 0x01
      Chunk length: 580
      Transmission sequence number: 2487901706
      Stream identifier: 0x0003
      Reserved: 0
      Message identifier: 1
      Fragment sequence number: 8
      Reassembled SCTP Fragments (10000 bytes, 9 fragments):
  */

  uint8_t data[] = {0x40, 0x01, 0x00, 0x15, 0x94, 0x4a, 0x5e, 0x0a,
                    0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
                    0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00};

  ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk chunk, IDataChunk::Parse(data));
  EXPECT_EQ(*chunk.tsn(), 2487901706);
  EXPECT_EQ(*chunk.stream_id(), 3u);
  EXPECT_EQ(*chunk.mid(), 1u);
  EXPECT_EQ(*chunk.ppid(), 0u);  // Not provided (so set to zero)
  EXPECT_EQ(*chunk.fsn(), 8u);
}

TEST(IDataChunkTest, InMiddleSerializeAndDeserialize) {
  IDataChunk chunk(TSN(123), StreamID(456), MID(789), PPID(0), FSN(101112),
                   {1, 2, 3}, /*options=*/{});

  std::vector<uint8_t> serialized;
  chunk.SerializeTo(serialized);

  ASSERT_HAS_VALUE_AND_ASSIGN(IDataChunk deserialized,
                              IDataChunk::Parse(serialized));
  EXPECT_EQ(*deserialized.tsn(), 123u);
  EXPECT_EQ(*deserialized.stream_id(), 456u);
  EXPECT_EQ(*deserialized.mid(), 789u);
  EXPECT_EQ(*deserialized.ppid(), 0u);
  EXPECT_EQ(*deserialized.fsn(), 101112u);
  EXPECT_THAT(deserialized.payload(), ElementsAre(1, 2, 3));

  EXPECT_EQ(deserialized.ToString(),
            "I-DATA, type=ordered::middle, tsn=123, stream_id=456, "
            "mid=789, fsn=101112, length=3");
}

}  // namespace
}  // namespace dcsctp