1 /*
2 * Copyright (c) 2021 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10 #include "net/dcsctp/testing/data_generator.h"
11
12 #include <cstdint>
13 #include <string>
14 #include <utility>
15 #include <vector>
16
17 #include "absl/strings/string_view.h"
18 #include "net/dcsctp/packet/data.h"
19 #include "net/dcsctp/public/types.h"
20
21 namespace dcsctp {
22 constexpr PPID kPpid = PPID(53);
23
Ordered(std::vector<uint8_t> payload,absl::string_view flags,const DataGeneratorOptions opts)24 Data DataGenerator::Ordered(std::vector<uint8_t> payload,
25 absl::string_view flags,
26 const DataGeneratorOptions opts) {
27 Data::IsBeginning is_beginning(flags.find('B') != std::string::npos);
28 Data::IsEnd is_end(flags.find('E') != std::string::npos);
29
30 if (is_beginning) {
31 fsn_ = FSN(0);
32 } else {
33 fsn_ = FSN(*fsn_ + 1);
34 }
35 MID message_id = opts.message_id.value_or(message_id_);
36 Data ret = Data(opts.stream_id, SSN(static_cast<uint16_t>(*message_id)),
37 message_id, fsn_, opts.ppid, std::move(payload), is_beginning,
38 is_end, IsUnordered(false));
39
40 if (is_end) {
41 message_id_ = MID(*message_id + 1);
42 }
43 return ret;
44 }
45
Unordered(std::vector<uint8_t> payload,absl::string_view flags,const DataGeneratorOptions opts)46 Data DataGenerator::Unordered(std::vector<uint8_t> payload,
47 absl::string_view flags,
48 const DataGeneratorOptions opts) {
49 Data::IsBeginning is_beginning(flags.find('B') != std::string::npos);
50 Data::IsEnd is_end(flags.find('E') != std::string::npos);
51
52 if (is_beginning) {
53 fsn_ = FSN(0);
54 } else {
55 fsn_ = FSN(*fsn_ + 1);
56 }
57 MID message_id = opts.message_id.value_or(message_id_);
58 Data ret = Data(opts.stream_id, SSN(0), message_id, fsn_, kPpid,
59 std::move(payload), is_beginning, is_end, IsUnordered(true));
60 if (is_end) {
61 message_id_ = MID(*message_id + 1);
62 }
63 return ret;
64 }
65 } // namespace dcsctp
66