webrtc_m130/net/dcsctp/socket/heartbeat_handler_test.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

185 lines
6.0 KiB
C++
Raw Normal View History

/*
* 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/socket/heartbeat_handler.h"
#include <memory>
#include <utility>
#include <vector>
Allow specifying delayed task precision of dcsctp::Timer. Context: The timer precision of PostDelayedTask() is about to be lowered to include up to 17 ms leeway. In order not to break use cases that require high precision timers, PostDelayedHighPrecisionTask() will continue to have the same precision that PostDelayedTask() has today. webrtc::TaskQueueBase has an enum (kLow, kHigh) to decide which precision to use when calling PostDelayedTaskWithPrecision(). See go/postdelayedtask-precision-in-webrtc for motivation and a table of delayed task use cases in WebRTC that are "high" or "low" precision. Most timers in DCSCTP are believed to only be needing low precision (see table), but the delayed_ack_timer_ of DataTracker[1] is an example of a use case that is likely to break if the timer precision is lowered (if ACK is sent too late, retransmissions may occur). So this is considered a high precision use case. This CL makes it possible to specify the precision of dcsctp::Timer. In a follow-up CL we will update delayed_ack_timer_ to kHigh precision. [1] https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/net/dcsctp/rx/data_tracker.cc;l=340 Bug: webrtc:13604 Change-Id: I8eec5ce37044096978b5dd1985fbb00bc0d8fb7e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/249081 Reviewed-by: Victor Boivie <boivie@webrtc.org> Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org> Commit-Queue: Henrik Boström <hbos@webrtc.org> Cr-Commit-Position: refs/heads/main@{#35809}
2022-01-26 18:38:13 +01:00
#include "api/task_queue/task_queue_base.h"
#include "net/dcsctp/packet/chunk/heartbeat_ack_chunk.h"
#include "net/dcsctp/packet/chunk/heartbeat_request_chunk.h"
#include "net/dcsctp/packet/parameter/heartbeat_info_parameter.h"
#include "net/dcsctp/public/types.h"
#include "net/dcsctp/socket/mock_context.h"
#include "net/dcsctp/testing/testing_macros.h"
#include "rtc_base/gunit.h"
#include "test/gmock.h"
namespace dcsctp {
namespace {
using ::testing::ElementsAre;
using ::testing::IsEmpty;
using ::testing::NiceMock;
using ::testing::Return;
using ::testing::SizeIs;
constexpr DurationMs kHeartbeatInterval = DurationMs(30'000);
DcSctpOptions MakeOptions(DurationMs heartbeat_interval) {
DcSctpOptions options;
options.heartbeat_interval_include_rtt = false;
options.heartbeat_interval = heartbeat_interval;
return options;
}
class HeartbeatHandlerTestBase : public testing::Test {
protected:
explicit HeartbeatHandlerTestBase(DurationMs heartbeat_interval)
: options_(MakeOptions(heartbeat_interval)),
context_(&callbacks_),
Allow specifying delayed task precision of dcsctp::Timer. Context: The timer precision of PostDelayedTask() is about to be lowered to include up to 17 ms leeway. In order not to break use cases that require high precision timers, PostDelayedHighPrecisionTask() will continue to have the same precision that PostDelayedTask() has today. webrtc::TaskQueueBase has an enum (kLow, kHigh) to decide which precision to use when calling PostDelayedTaskWithPrecision(). See go/postdelayedtask-precision-in-webrtc for motivation and a table of delayed task use cases in WebRTC that are "high" or "low" precision. Most timers in DCSCTP are believed to only be needing low precision (see table), but the delayed_ack_timer_ of DataTracker[1] is an example of a use case that is likely to break if the timer precision is lowered (if ACK is sent too late, retransmissions may occur). So this is considered a high precision use case. This CL makes it possible to specify the precision of dcsctp::Timer. In a follow-up CL we will update delayed_ack_timer_ to kHigh precision. [1] https://source.chromium.org/chromium/chromium/src/+/main:third_party/webrtc/net/dcsctp/rx/data_tracker.cc;l=340 Bug: webrtc:13604 Change-Id: I8eec5ce37044096978b5dd1985fbb00bc0d8fb7e Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/249081 Reviewed-by: Victor Boivie <boivie@webrtc.org> Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org> Commit-Queue: Henrik Boström <hbos@webrtc.org> Cr-Commit-Position: refs/heads/main@{#35809}
2022-01-26 18:38:13 +01:00
timer_manager_([this](webrtc::TaskQueueBase::DelayPrecision precision) {
return callbacks_.CreateTimeout(precision);
}),
handler_("log: ", options_, &context_, &timer_manager_) {}
void AdvanceTime(DurationMs duration) {
callbacks_.AdvanceTime(duration);
for (;;) {
absl::optional<TimeoutID> timeout_id = callbacks_.GetNextExpiredTimeout();
if (!timeout_id.has_value()) {
break;
}
timer_manager_.HandleTimeout(*timeout_id);
}
}
const DcSctpOptions options_;
NiceMock<MockDcSctpSocketCallbacks> callbacks_;
NiceMock<MockContext> context_;
TimerManager timer_manager_;
HeartbeatHandler handler_;
};
class HeartbeatHandlerTest : public HeartbeatHandlerTestBase {
protected:
HeartbeatHandlerTest() : HeartbeatHandlerTestBase(kHeartbeatInterval) {}
};
class DisabledHeartbeatHandlerTest : public HeartbeatHandlerTestBase {
protected:
DisabledHeartbeatHandlerTest() : HeartbeatHandlerTestBase(DurationMs(0)) {}
};
TEST_F(HeartbeatHandlerTest, HasRunningHeartbeatIntervalTimer) {
AdvanceTime(options_.heartbeat_interval);
// Validate that a heartbeat request was sent.
std::vector<uint8_t> payload = callbacks_.ConsumeSentPacket();
Revert "dcsctp: Support zero checksum packets" This reverts commit bd46bb7660fbc9f31799196058f7a1957f50aa31. Reason for revert: There is a slight performance degradation pointing to this CL, so revert this to be able to confirm if it is the culprit. Original change's description: > dcsctp: Support zero checksum packets > > If configured, the packet parser will allow packets with > a set checksum of zero. In that case, the correct checksum > will not even be calculated, avoiding a CPU intensive > calculation. > > Also, if specified when building a packet, the checksum can > be opted to be not calculated and written to the packet. > This is to be used when draft-tuexen-tsvwg-sctp-zero-checksum > has been negotiated, except for some packets during association > establishment. > > This is mainly a preparation CL and follow-up CL will enable > this feature. > > Low-Coverage-Reason: Affects debug logging code not run in tests > Bug: webrtc:14997 > Change-Id: I3207ac3a626df039ee2990403c2edd6429f17617 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/298481 > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > Commit-Queue: Victor Boivie <boivie@webrtc.org> > Cr-Commit-Position: refs/heads/main@{#39737} Bug: webrtc:14997 Change-Id: Ie22267abb4bcd25d5af07875eb933c51ed5be853 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/301580 Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/main@{#39878}
2023-04-17 17:46:59 +00:00
ASSERT_HAS_VALUE_AND_ASSIGN(SctpPacket packet, SctpPacket::Parse(payload));
ASSERT_THAT(packet.descriptors(), SizeIs(1));
ASSERT_HAS_VALUE_AND_ASSIGN(
HeartbeatRequestChunk request,
HeartbeatRequestChunk::Parse(packet.descriptors()[0].data));
EXPECT_TRUE(request.info().has_value());
}
TEST_F(HeartbeatHandlerTest, RepliesToHeartbeatRequests) {
uint8_t info_data[] = {1, 2, 3, 4, 5};
HeartbeatRequestChunk request(
Parameters::Builder().Add(HeartbeatInfoParameter(info_data)).Build());
handler_.HandleHeartbeatRequest(std::move(request));
std::vector<uint8_t> payload = callbacks_.ConsumeSentPacket();
Revert "dcsctp: Support zero checksum packets" This reverts commit bd46bb7660fbc9f31799196058f7a1957f50aa31. Reason for revert: There is a slight performance degradation pointing to this CL, so revert this to be able to confirm if it is the culprit. Original change's description: > dcsctp: Support zero checksum packets > > If configured, the packet parser will allow packets with > a set checksum of zero. In that case, the correct checksum > will not even be calculated, avoiding a CPU intensive > calculation. > > Also, if specified when building a packet, the checksum can > be opted to be not calculated and written to the packet. > This is to be used when draft-tuexen-tsvwg-sctp-zero-checksum > has been negotiated, except for some packets during association > establishment. > > This is mainly a preparation CL and follow-up CL will enable > this feature. > > Low-Coverage-Reason: Affects debug logging code not run in tests > Bug: webrtc:14997 > Change-Id: I3207ac3a626df039ee2990403c2edd6429f17617 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/298481 > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > Commit-Queue: Victor Boivie <boivie@webrtc.org> > Cr-Commit-Position: refs/heads/main@{#39737} Bug: webrtc:14997 Change-Id: Ie22267abb4bcd25d5af07875eb933c51ed5be853 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/301580 Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/main@{#39878}
2023-04-17 17:46:59 +00:00
ASSERT_HAS_VALUE_AND_ASSIGN(SctpPacket packet, SctpPacket::Parse(payload));
ASSERT_THAT(packet.descriptors(), SizeIs(1));
ASSERT_HAS_VALUE_AND_ASSIGN(
HeartbeatAckChunk response,
HeartbeatAckChunk::Parse(packet.descriptors()[0].data));
ASSERT_HAS_VALUE_AND_ASSIGN(
HeartbeatInfoParameter param,
response.parameters().get<HeartbeatInfoParameter>());
EXPECT_THAT(param.info(), ElementsAre(1, 2, 3, 4, 5));
}
TEST_F(HeartbeatHandlerTest, SendsHeartbeatRequestsOnIdleChannel) {
AdvanceTime(options_.heartbeat_interval);
// Grab the request, and make a response.
std::vector<uint8_t> payload = callbacks_.ConsumeSentPacket();
Revert "dcsctp: Support zero checksum packets" This reverts commit bd46bb7660fbc9f31799196058f7a1957f50aa31. Reason for revert: There is a slight performance degradation pointing to this CL, so revert this to be able to confirm if it is the culprit. Original change's description: > dcsctp: Support zero checksum packets > > If configured, the packet parser will allow packets with > a set checksum of zero. In that case, the correct checksum > will not even be calculated, avoiding a CPU intensive > calculation. > > Also, if specified when building a packet, the checksum can > be opted to be not calculated and written to the packet. > This is to be used when draft-tuexen-tsvwg-sctp-zero-checksum > has been negotiated, except for some packets during association > establishment. > > This is mainly a preparation CL and follow-up CL will enable > this feature. > > Low-Coverage-Reason: Affects debug logging code not run in tests > Bug: webrtc:14997 > Change-Id: I3207ac3a626df039ee2990403c2edd6429f17617 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/298481 > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > Commit-Queue: Victor Boivie <boivie@webrtc.org> > Cr-Commit-Position: refs/heads/main@{#39737} Bug: webrtc:14997 Change-Id: Ie22267abb4bcd25d5af07875eb933c51ed5be853 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/301580 Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/main@{#39878}
2023-04-17 17:46:59 +00:00
ASSERT_HAS_VALUE_AND_ASSIGN(SctpPacket packet, SctpPacket::Parse(payload));
ASSERT_THAT(packet.descriptors(), SizeIs(1));
ASSERT_HAS_VALUE_AND_ASSIGN(
HeartbeatRequestChunk req,
HeartbeatRequestChunk::Parse(packet.descriptors()[0].data));
HeartbeatAckChunk ack(std::move(req).extract_parameters());
// Respond a while later. This RTT will be measured by the handler
constexpr DurationMs rtt(313);
EXPECT_CALL(context_, ObserveRTT(rtt)).Times(1);
callbacks_.AdvanceTime(rtt);
handler_.HandleHeartbeatAck(std::move(ack));
}
TEST_F(HeartbeatHandlerTest, DoesntObserveInvalidHeartbeats) {
AdvanceTime(options_.heartbeat_interval);
// Grab the request, and make a response.
std::vector<uint8_t> payload = callbacks_.ConsumeSentPacket();
Revert "dcsctp: Support zero checksum packets" This reverts commit bd46bb7660fbc9f31799196058f7a1957f50aa31. Reason for revert: There is a slight performance degradation pointing to this CL, so revert this to be able to confirm if it is the culprit. Original change's description: > dcsctp: Support zero checksum packets > > If configured, the packet parser will allow packets with > a set checksum of zero. In that case, the correct checksum > will not even be calculated, avoiding a CPU intensive > calculation. > > Also, if specified when building a packet, the checksum can > be opted to be not calculated and written to the packet. > This is to be used when draft-tuexen-tsvwg-sctp-zero-checksum > has been negotiated, except for some packets during association > establishment. > > This is mainly a preparation CL and follow-up CL will enable > this feature. > > Low-Coverage-Reason: Affects debug logging code not run in tests > Bug: webrtc:14997 > Change-Id: I3207ac3a626df039ee2990403c2edd6429f17617 > Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/298481 > Reviewed-by: Harald Alvestrand <hta@webrtc.org> > Commit-Queue: Victor Boivie <boivie@webrtc.org> > Cr-Commit-Position: refs/heads/main@{#39737} Bug: webrtc:14997 Change-Id: Ie22267abb4bcd25d5af07875eb933c51ed5be853 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/301580 Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org> Commit-Queue: Victor Boivie <boivie@webrtc.org> Cr-Commit-Position: refs/heads/main@{#39878}
2023-04-17 17:46:59 +00:00
ASSERT_HAS_VALUE_AND_ASSIGN(SctpPacket packet, SctpPacket::Parse(payload));
ASSERT_THAT(packet.descriptors(), SizeIs(1));
ASSERT_HAS_VALUE_AND_ASSIGN(
HeartbeatRequestChunk req,
HeartbeatRequestChunk::Parse(packet.descriptors()[0].data));
HeartbeatAckChunk ack(std::move(req).extract_parameters());
EXPECT_CALL(context_, ObserveRTT).Times(0);
// Go backwards in time - which make the HEARTBEAT-ACK have an invalid
// timestamp in it, as it will be in the future.
callbacks_.AdvanceTime(DurationMs(-100));
handler_.HandleHeartbeatAck(std::move(ack));
}
TEST_F(HeartbeatHandlerTest, IncreasesErrorIfNotAckedInTime) {
DurationMs rto(105);
EXPECT_CALL(context_, current_rto).WillOnce(Return(rto));
AdvanceTime(options_.heartbeat_interval);
// Validate that a request was sent.
EXPECT_THAT(callbacks_.ConsumeSentPacket(), Not(IsEmpty()));
EXPECT_CALL(context_, IncrementTxErrorCounter).Times(1);
AdvanceTime(rto);
}
TEST_F(DisabledHeartbeatHandlerTest, IsReallyDisabled) {
AdvanceTime(options_.heartbeat_interval);
// Validate that a request was NOT sent.
EXPECT_THAT(callbacks_.ConsumeSentPacket(), IsEmpty());
}
} // namespace
} // namespace dcsctp