Prevent zero length pipeline (#813)

fixes: https://github.com/azure/azure-sdk-for-cpp/issues/726
This commit is contained in:
Victor Vazquez 2020-10-20 13:01:26 -07:00 committed by GitHub
parent e1dddce239
commit eec298d16a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 1 deletions

View File

@ -14,6 +14,10 @@
- Add high-level and simplified core.hpp file for simpler include experience for customers.
### Bug Fixes
- Prevent pipeline of length zero to be created.
## 1.0.0-beta.2 (2020-10-09)
### Breaking Changes

View File

@ -37,9 +37,16 @@ namespace Azure { namespace Core { namespace Http {
*
* @param policies A sequence of #HttpPolicy representing a stack, first element corresponding
* to the top of the stack.
*
* @throw `std::invalid_argument` when policies is empty.
*/
explicit HttpPipeline(const std::vector<std::unique_ptr<HttpPolicy>>& policies)
{
if (policies.size() == 0)
{
throw std::invalid_argument("policies cannot be empty");
}
m_policies.reserve(policies.size());
for (auto&& policy : policies)
{
@ -52,13 +59,25 @@ namespace Azure { namespace Core { namespace Http {
*
* @param policies A sequence of #HttpPolicy representing a stack, first element corresponding
* to the top of the stack.
*
* @throw `std::invalid_argument` when policies is empty.
*/
explicit HttpPipeline(std::vector<std::unique_ptr<HttpPolicy>>&& policies)
: m_policies(std::move(policies))
{
if (m_policies.size() == 0)
{
throw std::invalid_argument("policies cannot be empty");
}
}
/// Copy constructor.
/**
* @brief Copy constructor.
*
* @remark \p other is expected to have at least one policy.
*
* @param other
*/
HttpPipeline(const HttpPipeline& other)
{
m_policies.reserve(other.m_policies.size());
@ -78,6 +97,8 @@ namespace Azure { namespace Core { namespace Http {
*/
std::unique_ptr<RawResponse> Send(Context const& ctx, Request& request) const
{
// Accessing position zero is fine because pipeline must be constructed with at least one
// policy.
return m_policies[0]->Send(ctx, request, NextHttpPolicy(0, &m_policies));
}
};

View File

@ -28,6 +28,7 @@ add_executable (
logging.cpp
main.cpp
nullable.cpp
pipeline.cpp
string.cpp
telemetry_policy.cpp
transport_adapter.cpp

View File

@ -0,0 +1,50 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#include "gtest/gtest.h"
#include <azure/core/http/pipeline.hpp>
#include <azure/core/http/policy.hpp>
#include <vector>
TEST(Logging, createPipeline)
{
// Construct pipeline without exception
std::vector<std::unique_ptr<Azure::Core::Http::HttpPolicy>> policies;
policies.push_back(std::make_unique<Azure::Core::Http::TelemetryPolicy>("test", "test"));
EXPECT_NO_THROW(Azure::Core::Http::HttpPipeline pipeline(policies));
}
TEST(Logging, createEmptyPipeline)
{
// throw invalid arg for empty policies
std::vector<std::unique_ptr<Azure::Core::Http::HttpPolicy>> policies;
EXPECT_THROW(Azure::Core::Http::HttpPipeline pipeline(policies), std::invalid_argument);
}
TEST(Logging, clonePipeline)
{
// Construct pipeline without exception and clone
std::vector<std::unique_ptr<Azure::Core::Http::HttpPolicy>> policies;
policies.push_back(std::make_unique<Azure::Core::Http::TelemetryPolicy>("test", "test"));
Azure::Core::Http::HttpPipeline pipeline(policies);
EXPECT_NO_THROW(Azure::Core::Http::HttpPipeline pipeline2(pipeline));
}
TEST(Logging, refrefPipeline)
{
// Construct pipeline without exception
EXPECT_NO_THROW(Azure::Core::Http::HttpPipeline pipeline(
std::vector<std::unique_ptr<Azure::Core::Http::HttpPolicy>>(1)));
}
TEST(Logging, refrefEmptyPipeline)
{
// Construct pipeline with invalid exception with move constructor
EXPECT_THROW(
Azure::Core::Http::HttpPipeline pipeline(
std::vector<std::unique_ptr<Azure::Core::Http::HttpPolicy>>(0)),
std::invalid_argument);
}