1 /*
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 #include "proxygen/lib/http/session/test/HQSessionMocks.h"
10 #include <proxygen/lib/http/session/test/HTTPSessionMocks.h>
11 #include <proxygen/lib/http/session/test/HTTPTransactionMocks.h>
12 
13 using namespace proxygen;
14 using namespace testing;
15 /**
16  * A test case to validate that mocks exported by HQsesion
17  * work correctly
18  */
19 class MocksTest : public Test {
20  public:
SetUp()21   void SetUp() override {
22     handler_ = std::make_unique<StrictMock<MockHTTPHandler>>();
23     hqSession_ = std::make_unique<StrictMock<MockHQSession>>();
24 
25     hqSession_->quicStreamProtocolInfo_->ptoCount = 1;
26     hqSession_->quicStreamProtocolInfo_->totalPTOCount = 2;
27     hqSession_->quicStreamProtocolInfo_->totalTransportBytesSent = 3;
28     hqSession_->quicStreamProtocolInfo_->streamTransportInfo.holbCount = 4;
29   }
30 
TearDown()31   void TearDown() override {
32   }
33 
34  protected:
35   std::unique_ptr<StrictMock<MockHTTPHandler>> handler_;
36   folly::HHWheelTimer::UniquePtr timeoutSet_;
37   wangle::TransportInfo tInfo_;
38   std::unique_ptr<StrictMock<MockHQSession>> hqSession_;
39 };
40 
41 // Test that the mocked `newTransaction` does the right thing
TEST_F(MocksTest,MockHQSessionNewTransaction)42 TEST_F(MocksTest, MockHQSessionNewTransaction) {
43   // The default implementation of `newTransaction` is provided in Mocks.h
44   // But it has to be enabled via `EXPECT_CALL` without an action.
45   EXPECT_CALL(*hqSession_, newTransaction(_));
46 
47   // Capture the transaction object that HQSession will pass to the
48   // handler
49   HTTPTransaction* txn;
50   EXPECT_CALL(*handler_, setTransaction(_)).WillOnce(SaveArg<0>(&txn));
51 
52   hqSession_->newTransaction(handler_.get());
53 
54   // After the `setTransaction` has been invoked, the handler and transaction
55   // must be tied.
56   EXPECT_EQ(txn, hqSession_->txn_.get())
57       << "`newTransaction` should call `handler::setTransaction`";
58   EXPECT_EQ(handler_.get(), hqSession_->handler_)
59       << "`newTransaction` should set the session handler";
60 }
61 
62 // Test that the mocked `newTransaction` does the right thing
TEST_F(MocksTest,MockHQSessionPropagatesQuickProtocolInfo)63 TEST_F(MocksTest, MockHQSessionPropagatesQuickProtocolInfo) {
64   // The default implementation of `newTransaction` is provided in Mocks.h
65   // But it has to be enabled via `EXPECT_CALL` without an action.
66   EXPECT_CALL(*hqSession_, newTransaction(_));
67 
68   // Capture the transaction object that HQSession will pass to the
69   // handler
70   HTTPTransaction* txn;
71   EXPECT_CALL(*handler_, setTransaction(_)).WillOnce(SaveArg<0>(&txn));
72 
73   hqSession_->newTransaction(handler_.get());
74 
75   wangle::TransportInfo tinfo;
76 
77   hqSession_->txn_->getCurrentTransportInfo(&tinfo);
78 
79   auto receivedProtocolInfo =
80       dynamic_cast<QuicStreamProtocolInfo*>(tinfo.protocolInfo.get());
81 
82   EXPECT_NE(receivedProtocolInfo, nullptr)
83       << "`getCurrentTransportInfo` should return QuicStreamProtocolInfo";
84 
85   EXPECT_EQ(receivedProtocolInfo->totalPTOCount, 2)
86       << "received protocol info should reflect the quic protocol info";
87 }
88