Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the missing implementation for BdxTransferSession::RejectTransfer. #34484

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/protocols/bdx/BdxTransferSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,17 @@ CHIP_ERROR TransferSession::AcceptTransfer(const TransferAcceptData & acceptData
return CHIP_NO_ERROR;
}

CHIP_ERROR TransferSession::RejectTransfer(StatusCode reason)
{
VerifyOrReturnError(mState == TransferState::kNegotiateTransferParams, CHIP_ERROR_INCORRECT_STATE);
VerifyOrReturnError(mPendingOutput == OutputEventType::kNone, CHIP_ERROR_INCORRECT_STATE);

PrepareStatusReport(reason);
harimau-qirex marked this conversation as resolved.
Show resolved Hide resolved
mState = TransferState::kTransferDone;

return CHIP_NO_ERROR;
}

CHIP_ERROR TransferSession::PrepareBlockQuery()
{
const MessageType msgType = MessageType::BlockQuery;
Expand Down
61 changes: 61 additions & 0 deletions src/protocols/bdx/tests/TestBdxTransferSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ void VerifyNoMoreOutput(TransferSession & transferSession)
EXPECT_EQ(event.EventType, TransferSession::OutputEventType::kNone);
}

void VerifyInternalError(TransferSession & transferSession)
{
TransferSession::OutputEvent event;
transferSession.PollOutput(event, kNoAdvanceTime);
EXPECT_EQ(event.EventType, TransferSession::OutputEventType::kInternalError);
}

// Helper method for initializing two TransferSession objects, generating a TransferInit message, and passing it to a responding
// TransferSession.
void SendAndVerifyTransferInit(TransferSession::OutputEvent & outEvent, System::Clock::Timeout timeout, TransferSession & initiator,
Expand Down Expand Up @@ -235,6 +242,30 @@ void SendAndVerifyAcceptMsg(TransferSession::OutputEvent & outEvent, TransferSes
EXPECT_LE(acceptReceiver.GetTransferBlockSize(), initData.MaxBlockSize);
}

void SendAndVerifyRejectMsg(TransferSession::OutputEvent & outEvent, TransferSession & rejectSender, StatusCode reason,
TransferSession & rejectReceiver)
{
CHIP_ERROR err = rejectSender.RejectTransfer(reason);
EXPECT_EQ(err, CHIP_NO_ERROR);

// Verify Sender emits status message for sending
rejectSender.PollOutput(outEvent, kNoAdvanceTime);
VerifyNoMoreOutput(rejectSender);
EXPECT_EQ(outEvent.EventType, TransferSession::OutputEventType::kMsgToSend);
System::PacketBufferHandle statusReportMsg = outEvent.MsgData.Retain();
VerifyStatusReport(std::move(outEvent.MsgData), reason);

// Pass status message to rejectReceiver
err = AttachHeaderAndSend(outEvent.msgTypeData, std::move(outEvent.MsgData), rejectReceiver);
EXPECT_EQ(err, CHIP_NO_ERROR);

// Verify received status message.
rejectReceiver.PollOutput(outEvent, kNoAdvanceTime);
VerifyInternalError(rejectReceiver);
EXPECT_EQ(outEvent.EventType, TransferSession::OutputEventType::kStatusReceived);
EXPECT_EQ(outEvent.statusData.statusCode, reason);
}

// Helper method for preparing a sending a BlockQuery message between two TransferSession objects.
void SendAndVerifyQuery(TransferSession & queryReceiver, TransferSession & querySender, TransferSession::OutputEvent & outEvent)
{
Expand Down Expand Up @@ -698,3 +729,33 @@ TEST_F(TestBdxTransferSession, TestDuplicateBlockError)
EXPECT_EQ(outEvent.statusData.statusCode, StatusCode::kBadBlockCounter);
}
}

TEST_F(TestBdxTransferSession, TestRejectTransfer)
{
TransferSession::OutputEvent outEvent;
TransferSession initiatingReceiver;
TransferSession respondingSender;

// Chosen arbitrarily for this test
uint16_t proposedBlockSize = 128;
System::Clock::Timeout timeout = System::Clock::Seconds16(24);
TransferControlFlags driveMode = TransferControlFlags::kReceiverDrive;

// ReceiveInit parameters
TransferSession::TransferInitData initOptions;
initOptions.TransferCtlFlags = driveMode;
initOptions.MaxBlockSize = proposedBlockSize;
char testFileDes[9] = { "test.txt" };
initOptions.FileDesLength = static_cast<uint16_t>(strlen(testFileDes));
initOptions.FileDesignator = reinterpret_cast<uint8_t *>(testFileDes);

// Initialize respondingSender and pass ReceiveInit message
BitFlags<TransferControlFlags> senderOpts;
senderOpts.Set(driveMode);

SendAndVerifyTransferInit(outEvent, timeout, initiatingReceiver, TransferRole::kReceiver, initOptions, respondingSender,
senderOpts, proposedBlockSize);

// Reject the transfer with a status
SendAndVerifyRejectMsg(outEvent, respondingSender, StatusCode::kResponderBusy, initiatingReceiver);
}
Loading