Skip to content

Commit

Permalink
Merge branch 'main' into feature/size_reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzcanoguz authored Nov 12, 2024
2 parents 3536076 + e626133 commit 5b8f1e8
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 43 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linting-formatting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
VALIDATE_ALL_CODEBASE: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- run: git diff
- uses: github/codeql-action/upload-sarif@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
- uses: github/codeql-action/upload-sarif@4f3212b61783c3c68e8309a0f18a699764811cda # v3.27.1
if: ${{ success() || failure() }}
with:
sarif_file: megalinter-reports/megalinter-report.sarif
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ jobs:
results_format: sarif
repo_token: ${{ secrets.SCORECARD_READ_TOKEN }}
publish_results: true
- uses: github/codeql-action/upload-sarif@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
- uses: github/codeql-action/upload-sarif@4f3212b61783c3c68e8309a0f18a699764811cda # v3.27.1
with:
sarif_file: scorecards.sarif
4 changes: 2 additions & 2 deletions .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ jobs:
- uses: hendrikmuhs/ccache-action@ed74d11c0b343532753ecead8a951bb09bb34bc9 # v1.2.14
with:
key: ${{ github.job }}
- uses: github/codeql-action/init@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
- uses: github/codeql-action/init@4f3212b61783c3c68e8309a0f18a699764811cda # v3.27.1
with:
languages: cpp
- uses: lukka/run-cmake@af1be47fd7c933593f687731bc6fdbee024d3ff4 # v10.8
with:
configurePreset: "host"
buildPreset: "host-Debug"
configurePresetAdditionalArgs: "['-DCMAKE_C_COMPILER_LAUNCHER=ccache', '-DCMAKE_CXX_COMPILER_LAUNCHER=ccache']"
- uses: github/codeql-action/analyze@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
- uses: github/codeql-action/analyze@4f3212b61783c3c68e8309a0f18a699764811cda # v3.27.1
3 changes: 3 additions & 0 deletions infra/timer/TickOnInterruptTimerService.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "infra/timer/TickOnInterruptTimerService.hpp"
#include "infra/event/EventDispatcher.hpp"
#include "infra/util/ReallyAssert.hpp"
#include <cassert>

namespace infra
Expand All @@ -8,6 +9,8 @@ namespace infra
: TimerService(id)
, resolution(resolution)
{
really_assert(infra::EventDispatcher::InstanceSet());

CalculateNextTrigger();
}

Expand Down
59 changes: 33 additions & 26 deletions services/network/HttpClientAuthentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,89 +8,89 @@ namespace services

void HttpClientAuthentication::Get(infra::BoundedConstString requestTarget, HttpHeaders headers)
{
Request(headers, [this, requestTarget]()
Request(headers, HttpVerb::get, requestTarget, [this]()
{
Subject().Get(requestTarget, infra::MakeRange(headersWithAuthorization));
Subject().Get(target, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Head(infra::BoundedConstString requestTarget, HttpHeaders headers)
{
Request(headers, [this, requestTarget]()
Request(headers, HttpVerb::head, requestTarget, [this]()
{
Subject().Head(requestTarget, infra::MakeRange(headersWithAuthorization));
Subject().Head(target, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Connect(infra::BoundedConstString requestTarget, HttpHeaders headers)
{
Request(headers, [this, requestTarget]()
Request(headers, HttpVerb::connect, requestTarget, [this]()
{
Subject().Connect(requestTarget, infra::MakeRange(headersWithAuthorization));
Subject().Connect(target, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Options(infra::BoundedConstString requestTarget, HttpHeaders headers)
{
Request(headers, [this, requestTarget]()
Request(headers, HttpVerb::options, requestTarget, [this]()
{
Subject().Options(requestTarget, infra::MakeRange(headersWithAuthorization));
Subject().Options(target, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Post(infra::BoundedConstString requestTarget, infra::BoundedConstString content, HttpHeaders headers)
{
Request(headers, [this, requestTarget, content]()
Request(headers, HttpVerb::post, requestTarget, [this, content]()
{
Subject().Post(requestTarget, content, infra::MakeRange(headersWithAuthorization));
Subject().Post(target, content, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Post(infra::BoundedConstString requestTarget, HttpHeaders headers)
{
Request(headers, [this, requestTarget]()
Request(headers, HttpVerb::post, requestTarget, [this]()
{
Subject().Post(requestTarget, infra::MakeRange(headersWithAuthorization));
Subject().Post(target, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Put(infra::BoundedConstString requestTarget, infra::BoundedConstString content, HttpHeaders headers)
{
Request(headers, [this, requestTarget, content]()
Request(headers, HttpVerb::put, requestTarget, [this, content]()
{
Subject().Put(requestTarget, content, infra::MakeRange(headersWithAuthorization));
Subject().Put(target, content, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Put(infra::BoundedConstString requestTarget, HttpHeaders headers)
{
Request(headers, [this, requestTarget]()
Request(headers, HttpVerb::put, requestTarget, [this]()
{
Subject().Put(requestTarget, infra::MakeRange(headersWithAuthorization));
Subject().Put(target, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Patch(infra::BoundedConstString requestTarget, infra::BoundedConstString content, HttpHeaders headers)
{
Request(headers, [this, requestTarget, content]()
Request(headers, HttpVerb::patch, requestTarget, [this, content]()
{
Subject().Patch(requestTarget, content, infra::MakeRange(headersWithAuthorization));
Subject().Patch(target, content, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Patch(infra::BoundedConstString requestTarget, HttpHeaders headers)
{
Request(headers, [this, requestTarget]()
Request(headers, HttpVerb::patch, requestTarget, [this]()
{
Subject().Patch(requestTarget, infra::MakeRange(headersWithAuthorization));
Subject().Patch(target, infra::MakeRange(headersWithAuthorization));
});
}

void HttpClientAuthentication::Delete(infra::BoundedConstString requestTarget, infra::BoundedConstString content, HttpHeaders headers)
{
Request(headers, [this, requestTarget, content]()
Request(headers, HttpVerb::delete_, requestTarget, [this, content]()
{
Subject().Delete(requestTarget, content, infra::MakeRange(headersWithAuthorization));
Subject().Delete(target, content, infra::MakeRange(headersWithAuthorization));
});
}

Expand Down Expand Up @@ -155,7 +155,8 @@ namespace services
Observer().StatusAvailable(HttpStatusCode::Unauthorized);
}

Observer().BodyComplete();
if (HttpClient::IsAttached())
Observer().BodyComplete();
}

void HttpClientAuthentication::SendStreamAvailable(infra::SharedPtr<infra::StreamWriter>&& writer)
Expand All @@ -170,15 +171,18 @@ namespace services

void HttpClientAuthentication::Detaching()
{
HttpClient::Detach();
if (HttpClient::IsAttached())
HttpClient::Detach();
HttpClientObserver::Detaching();
}

void HttpClientAuthentication::Request(HttpHeaders headers, const infra::Function<void(), sizeof(void*) + 2 * sizeof(infra::BoundedConstString)>& newRequest)
void HttpClientAuthentication::Request(HttpHeaders headers, HttpVerb verb, infra::BoundedConstString target, const infra::Function<void(), sizeof(void*) + 2 * sizeof(infra::BoundedConstString)>& newRequest)
{
MakeHeaders(headers);
unauthorized = false;
request = newRequest;
this->verb = verb;
this->target = target;
request();
}

Expand All @@ -195,7 +199,10 @@ namespace services
auto scheme = headerValue.substr(0, spaceIndex);
auto challenge = headerValue.substr(std::min(headerValue.find_first_not_of(' ', spaceIndex), headerValue.size()));

Authenticate(scheme, challenge);
Authenticate(verb, target, scheme, challenge);

headersWithAuthorization.pop_back();
headersWithAuthorization.emplace_back("Authorization", AuthenticationHeader());
}

HttpClientAuthenticationConnector::HttpClientAuthenticationConnector(HttpClientConnector& connector, HttpClientAuthentication& clientAuthentication)
Expand Down
6 changes: 4 additions & 2 deletions services/network/HttpClientAuthentication.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,20 @@ namespace services
void Detaching() override;

protected:
virtual void Authenticate(infra::BoundedConstString scheme, infra::BoundedConstString value) = 0;
virtual void Authenticate(HttpVerb verb, infra::BoundedConstString target, infra::BoundedConstString scheme, infra::BoundedConstString value) = 0;
virtual infra::BoundedConstString AuthenticationHeader() const = 0;
virtual bool Retry() const = 0;
virtual void Reset() = 0;

private:
void Request(HttpHeaders headers, const infra::Function<void(), sizeof(void*) + 2 * sizeof(infra::BoundedConstString)>& newRequest);
void Request(HttpHeaders headers, HttpVerb verb, infra::BoundedConstString target, const infra::Function<void(), sizeof(void*) + 2 * sizeof(infra::BoundedConstString)>& newRequest);
void MakeHeaders(HttpHeaders headers);
void Authenticate(infra::BoundedConstString headerValue);

private:
infra::Function<void(), sizeof(void*) + 2 * sizeof(infra::BoundedConstString)> request;
HttpVerb verb;
infra::BoundedConstString target;
bool unauthorized = false;
infra::BoundedVector<HttpHeader>& headersWithAuthorization;
};
Expand Down
76 changes: 66 additions & 10 deletions services/network/test/TestHttpClientAuthentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace

using services::HttpClientAuthentication::HttpClientAuthentication;

MOCK_METHOD2(Authenticate, void(infra::BoundedConstString scheme, infra::BoundedConstString value));
MOCK_METHOD4(Authenticate, void(services::HttpVerb verb, infra::BoundedConstString target, infra::BoundedConstString scheme, infra::BoundedConstString value));
MOCK_CONST_METHOD0(AuthenticationHeader, infra::BoundedConstString());
MOCK_CONST_METHOD0(Retry, bool());
MOCK_METHOD0(Reset, void());
Expand All @@ -37,7 +37,8 @@ class HttpClientAuthenticationTest

~HttpClientAuthenticationTest() override
{
EXPECT_CALL(*httpClientObserver, Detaching());
if (httpClientObserver->IsAttached())
EXPECT_CALL(*httpClientObserver, Detaching());
}

void CheckHeaders(services::HttpHeaders headersToCheck, infra::BoundedConstString headerValueToAdd)
Expand All @@ -47,6 +48,16 @@ class HttpClientAuthenticationTest
EXPECT_TRUE(infra::ContentsEqual(infra::MakeRange(copy), headersToCheck));
}

void Get()
{
EXPECT_CALL(httpClient, Get("target", testing::_)).WillOnce(testing::Invoke([this](infra::BoundedConstString requestTarget, services::HttpHeaders headers)
{
CheckHeaders(headers, "header contents");
}));
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
httpClientObserver->Subject().Get("target", infra::MakeRange(headers));
}

infra::SharedOptional<testing::StrictMock<services::HttpClientObserverMock>> httpClientObserver;
testing::StrictMock<HttpClientAuthentication::WithMaxHeaders<4>> clientAuthentication;
testing::StrictMock<services::HttpClientMock> httpClient;
Expand All @@ -56,12 +67,7 @@ class HttpClientAuthenticationTest

TEST_F(HttpClientAuthenticationTest, Get_request_is_forwarded)
{
EXPECT_CALL(httpClient, Get("target", testing::_)).WillOnce(testing::Invoke([this](infra::BoundedConstString requestTarget, services::HttpHeaders headers)
{
CheckHeaders(headers, "header contents");
}));
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
httpClientObserver->Subject().Get("target", infra::MakeRange(headers));
Get();
}

TEST_F(HttpClientAuthenticationTest, Head_request_is_forwarded)
Expand Down Expand Up @@ -134,6 +140,26 @@ TEST_F(HttpClientAuthenticationTest, Put_request_is_forwarded_3)
httpClientObserver->Subject().Put("target", infra::MakeRange(headers));
}

TEST_F(HttpClientAuthenticationTest, Patch_request_is_forwarded_1)
{
EXPECT_CALL(httpClient, Patch("target", "contents", testing::_)).WillOnce(testing::Invoke([this](infra::BoundedConstString requestTarget, infra::BoundedConstString contents, services::HttpHeaders headers)
{
CheckHeaders(headers, "header contents");
}));
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
httpClientObserver->Subject().Patch("target", "contents", infra::MakeRange(headers));
}

TEST_F(HttpClientAuthenticationTest, Patch_request_is_forwarded_3)
{
EXPECT_CALL(httpClient, Patch("target", testing::_)).WillOnce(testing::Invoke([this](infra::BoundedConstString requestTarget, services::HttpHeaders headers)
{
CheckHeaders(headers, "header contents");
}));
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
httpClientObserver->Subject().Patch("target", infra::MakeRange(headers));
}

TEST_F(HttpClientAuthenticationTest, Delete_request_is_forwarded)
{
EXPECT_CALL(httpClient, Delete("target", "contents", testing::_)).WillOnce(testing::Invoke([this](infra::BoundedConstString requestTarget, infra::BoundedConstString contents, services::HttpHeaders headers)
Expand Down Expand Up @@ -165,6 +191,8 @@ TEST_F(HttpClientAuthenticationTest, GetConnection_is_forwarded)

TEST_F(HttpClientAuthenticationTest, normal_flow_is_forwarded)
{
Get();

testing::StrictMock<infra::StreamWriterMock> writer;
EXPECT_CALL(*httpClientObserver, FillContent(testing::Ref(writer)));
httpClient.Observer().FillContent(writer);
Expand All @@ -190,10 +218,13 @@ TEST_F(HttpClientAuthenticationTest, normal_flow_is_forwarded)

TEST_F(HttpClientAuthenticationTest, unauthorized_is_not_retried)
{
Get();

httpClient.Observer().StatusAvailable(services::HttpStatusCode::Unauthorized);

httpClient.Observer().HeaderAvailable({ "name", "value" });
EXPECT_CALL(clientAuthentication, Authenticate("scheme", "value"));
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
EXPECT_CALL(clientAuthentication, Authenticate(services::HttpVerb::get, "target", "scheme", "value"));
httpClient.Observer().HeaderAvailable({ "WWW-Authenticate", "scheme value" });

testing::StrictMock<infra::StreamReaderMock> reader;
Expand All @@ -215,7 +246,8 @@ TEST_F(HttpClientAuthenticationTest, unauthorized_is_retried)
httpClient.Observer().StatusAvailable(services::HttpStatusCode::Unauthorized);

httpClient.Observer().HeaderAvailable({ "name", "value" });
EXPECT_CALL(clientAuthentication, Authenticate("scheme", "value"));
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
EXPECT_CALL(clientAuthentication, Authenticate(services::HttpVerb::get, "target", "scheme", "value"));
httpClient.Observer().HeaderAvailable({ "WWW-Authenticate", "scheme value" });

testing::StrictMock<infra::StreamReaderMock> reader;
Expand All @@ -230,3 +262,27 @@ TEST_F(HttpClientAuthenticationTest, unauthorized_is_retried)
}));
httpClient.Observer().BodyComplete();
}

TEST_F(HttpClientAuthenticationTest, detach_during_StatusAvailable)
{
Get();

httpClient.Observer().StatusAvailable(services::HttpStatusCode::Unauthorized);

httpClient.Observer().HeaderAvailable({ "name", "value" });
EXPECT_CALL(clientAuthentication, AuthenticationHeader()).WillOnce(testing::Return("header contents"));
EXPECT_CALL(clientAuthentication, Authenticate(services::HttpVerb::get, "target", "scheme", "value"));
httpClient.Observer().HeaderAvailable({ "WWW-Authenticate", "scheme value" });

testing::StrictMock<infra::StreamReaderMock> reader;
EXPECT_CALL(reader, Empty()).WillOnce(testing::Return(true));
httpClient.Observer().BodyAvailable(infra::UnOwnedSharedPtr(reader));

EXPECT_CALL(clientAuthentication, Retry()).WillOnce(testing::Return(false));
EXPECT_CALL(*httpClientObserver, StatusAvailable(services::HttpStatusCode::Unauthorized)).WillOnce(testing::Invoke([this](services::HttpStatusCode)
{
EXPECT_CALL(*httpClientObserver, Detaching());
httpClientObserver->Detach();
}));
httpClient.Observer().BodyComplete();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class HttpClientAuthenticationStub

protected:
// Implementation of HttpClientAuthentication
void Authenticate(infra::BoundedConstString scheme, infra::BoundedConstString value) override
void Authenticate(services::HttpVerb verb, infra::BoundedConstString target, infra::BoundedConstString scheme, infra::BoundedConstString value) override
{}

infra::BoundedConstString AuthenticationHeader() const override
Expand Down

0 comments on commit 5b8f1e8

Please sign in to comment.