azure-sdk-for-cpp/sdk/storage/test/test_base.cpp
JinmingHu f74011a2e0
Add Unittest for Blob Service, other fixes and enhancements found during validation (#181)
* Give AccessTier a default value to eliminate warning

* Add blob clients conversion functions

* Fix bug in shared key authentication

* Add GetUri().

* make lease-status and lease-state optional

* Check duplicates in metadata

* add tests for blob service

* AppendBlocb doesn't support AccessTier

* fix bug in "RangeFromXml"

* redefine PageRange in convenience layer

* suppress warnings from gtest

* add API AppendBlockFromUri

* connection string parsing

* Add memorystream

* Rename connection string constants

* Adapt to new bodystream, this will be rewritten when bosystream is changed to unique_ptr

* fix compiler errors

* Change MemoryStream interface

* samples framework

* Add newline at the end of file

* fix compiler error
2020-06-24 01:17:09 -07:00

143 lines
4.0 KiB
C++

// Copyright (c) Microsoft Corporation. All rights reserved.
// SPDX-License-Identifier: MIT
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "test_base.hpp"
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <limits>
#include <random>
#include <string>
namespace Azure { namespace Storage { namespace Test {
constexpr static const char* c_StandardStorageConnectionString = "";
constexpr static const char* c_PremiumStorageConnectionString = "";
constexpr static const char* c_BlobStorageConnectionString = "";
constexpr static const char* c_PremiumFileConnectionString = "";
constexpr static const char* c_ADLSGen2ConnectionString = "";
const std::string& StandardStorageConnectionString()
{
const static std::string connectionString = []() -> std::string {
if (strlen(c_StandardStorageConnectionString) != 0)
{
return c_StandardStorageConnectionString;
}
return std::getenv("STANDARD_STORAGE_CONNECTION_STRING");
}();
return connectionString;
}
const std::string& PremiumStorageConnectionString()
{
const static std::string connectionString = []() -> std::string {
if (strlen(c_PremiumStorageConnectionString) != 0)
{
return c_PremiumStorageConnectionString;
}
return std::getenv("PREMIUM_STORAGE_CONNECTION_STRING");
}();
return connectionString;
}
const std::string& BlobStorageConnectionString()
{
const static std::string connectionString = []() -> std::string {
if (strlen(c_BlobStorageConnectionString) != 0)
{
return c_BlobStorageConnectionString;
}
return std::getenv("BLOB_STORAGE_CONNECTION_STRING");
}();
return connectionString;
}
const std::string& PremiumFileConnectionString()
{
const static std::string connectionString = []() -> std::string {
if (strlen(c_PremiumFileConnectionString) != 0)
{
return c_PremiumFileConnectionString;
}
return std::getenv("PREMIUM_FILE_CONNECTION_STRING");
}();
return connectionString;
}
const std::string& ADLSGen2ConnectionString()
{
const static std::string connectionString = []() -> std::string {
if (strlen(c_ADLSGen2ConnectionString) != 0)
{
return c_ADLSGen2ConnectionString;
}
return std::getenv("ADLS_GEN2_CONNECTION_STRING");
}();
return connectionString;
}
static thread_local std::mt19937_64 random_generator(std::random_device{}());
static char random_char()
{
const char charset[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::uniform_int_distribution<std::size_t> distribution(0, sizeof(charset) - 2);
return charset[distribution(random_generator)];
}
std::string RandomString()
{
std::string str;
str.resize(10);
std::generate(str.begin(), str.end(), random_char);
return str;
}
std::string LowercaseRandomString()
{
auto str = RandomString();
std::transform(
str.begin(), str.end(), str.begin(), [](unsigned char c) { return char(std::tolower(c)); });
return str;
}
void RandomBuffer(char* buffer, std::size_t length)
{
char* start_addr = buffer;
char* end_addr = buffer + length;
const std::size_t rand_int_size = sizeof(uint64_t);
while (uintptr_t(start_addr) % rand_int_size != 0 && start_addr < end_addr)
{
*(start_addr++) = random_char();
}
std::uniform_int_distribution<uint64_t> distribution(
0ULL, std::numeric_limits<uint64_t>::max());
while (start_addr + rand_int_size <= end_addr)
{
*reinterpret_cast<uint64_t*>(start_addr) = distribution(random_generator);
start_addr += rand_int_size;
}
while (start_addr < end_addr)
{
*(start_addr++) = random_char();
}
}
std::vector<uint8_t> ReadBodyStream(Azure::Core::Http::BodyStream* stream)
{
std::vector<uint8_t> body(stream->Length(), '\x00');
stream->Read(&body[0], body.size());
return body;
}
}}} // namespace Azure::Storage::Test