xref: /aosp_15_r20/external/webrtc/net/dcsctp/packet/error_cause/missing_mandatory_parameter_cause_test.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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/packet/error_cause/missing_mandatory_parameter_cause.h"
11 
12 #include <stdint.h>
13 
14 #include <type_traits>
15 #include <vector>
16 
17 #include "api/array_view.h"
18 #include "net/dcsctp/testing/testing_macros.h"
19 #include "rtc_base/gunit.h"
20 #include "test/gmock.h"
21 
22 namespace dcsctp {
23 namespace {
24 using ::testing::ElementsAre;
25 using ::testing::IsEmpty;
26 
TEST(MissingMandatoryParameterCauseTest,SerializeAndDeserialize)27 TEST(MissingMandatoryParameterCauseTest, SerializeAndDeserialize) {
28   uint16_t parameter_types[] = {1, 2, 3};
29   MissingMandatoryParameterCause parameter(parameter_types);
30 
31   std::vector<uint8_t> serialized;
32   parameter.SerializeTo(serialized);
33 
34   ASSERT_HAS_VALUE_AND_ASSIGN(
35       MissingMandatoryParameterCause deserialized,
36       MissingMandatoryParameterCause::Parse(serialized));
37 
38   EXPECT_THAT(deserialized.missing_parameter_types(), ElementsAre(1, 2, 3));
39 }
40 
TEST(MissingMandatoryParameterCauseTest,HandlesDeserializeZeroParameters)41 TEST(MissingMandatoryParameterCauseTest, HandlesDeserializeZeroParameters) {
42   uint8_t serialized[] = {0, 2, 0, 8, 0, 0, 0, 0};
43 
44   ASSERT_HAS_VALUE_AND_ASSIGN(
45       MissingMandatoryParameterCause deserialized,
46       MissingMandatoryParameterCause::Parse(serialized));
47 
48   EXPECT_THAT(deserialized.missing_parameter_types(), IsEmpty());
49 }
50 
TEST(MissingMandatoryParameterCauseTest,HandlesOverflowParameterCount)51 TEST(MissingMandatoryParameterCauseTest, HandlesOverflowParameterCount) {
52   // 0x80000004 * 2 = 2**32 + 8 -> if overflow, would validate correctly.
53   uint8_t serialized[] = {0, 2, 0, 8, 0x80, 0x00, 0x00, 0x04};
54 
55   EXPECT_FALSE(MissingMandatoryParameterCause::Parse(serialized).has_value());
56 }
57 
58 }  // namespace
59 }  // namespace dcsctp
60