Don't use implicitly namespace td in HTTP tests.
This commit is contained in:
parent
1769b7b61c
commit
687193c8e9
249
test/http.cpp
249
test/http.cpp
@ -41,18 +41,15 @@
|
|||||||
#include "td/utils/UInt.h"
|
#include "td/utils/UInt.h"
|
||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <limits>
|
|
||||||
|
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
#include <limits>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
using namespace td;
|
static td::string make_chunked(const td::string &str) {
|
||||||
|
auto v = td::rand_split(str);
|
||||||
static string make_chunked(const string &str) {
|
td::string res;
|
||||||
auto v = rand_split(str);
|
|
||||||
string res;
|
|
||||||
for (auto &s : v) {
|
for (auto &s : v) {
|
||||||
res += PSTRING() << format::as_hex_dump(static_cast<int32>(s.size()));
|
res += PSTRING() << td::format::as_hex_dump(static_cast<td::int32>(s.size()));
|
||||||
res += "\r\n";
|
res += "\r\n";
|
||||||
res += s;
|
res += s;
|
||||||
res += "\r\n";
|
res += "\r\n";
|
||||||
@ -61,33 +58,33 @@ static string make_chunked(const string &str) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static string gen_http_content() {
|
static td::string gen_http_content() {
|
||||||
int t = Random::fast(0, 2);
|
int t = td::Random::fast(0, 2);
|
||||||
int len;
|
int len;
|
||||||
if (t == 0) {
|
if (t == 0) {
|
||||||
len = Random::fast(1, 10);
|
len = td::Random::fast(1, 10);
|
||||||
} else if (t == 1) {
|
} else if (t == 1) {
|
||||||
len = Random::fast(100, 200);
|
len = td::Random::fast(100, 200);
|
||||||
} else {
|
} else {
|
||||||
len = Random::fast(1000, 20000);
|
len = td::Random::fast(1000, 20000);
|
||||||
}
|
}
|
||||||
return rand_string(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), len);
|
return td::rand_string(std::numeric_limits<char>::min(), std::numeric_limits<char>::max(), len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static string make_http_query(string content, bool is_json, bool is_chunked, bool is_gzip, double gzip_k = 5,
|
static td::string make_http_query(td::string content, bool is_json, bool is_chunked, bool is_gzip, double gzip_k = 5,
|
||||||
string zip_override = string()) {
|
td::string zip_override = td::string()) {
|
||||||
HttpHeaderCreator hc;
|
td::HttpHeaderCreator hc;
|
||||||
hc.init_post("/");
|
hc.init_post("/");
|
||||||
hc.add_header("jfkdlsahhjk", rand_string('a', 'z', Random::fast(1, 2000)));
|
hc.add_header("jfkdlsahhjk", td::rand_string('a', 'z', td::Random::fast(1, 2000)));
|
||||||
if (is_json) {
|
if (is_json) {
|
||||||
hc.add_header("content-type", "application/json");
|
hc.add_header("content-type", "application/json");
|
||||||
}
|
}
|
||||||
if (is_gzip) {
|
if (is_gzip) {
|
||||||
BufferSlice zip;
|
td::BufferSlice zip;
|
||||||
if (zip_override.empty()) {
|
if (zip_override.empty()) {
|
||||||
zip = gzencode(content, gzip_k);
|
zip = td::gzencode(content, gzip_k);
|
||||||
} else {
|
} else {
|
||||||
zip = BufferSlice(zip_override);
|
zip = td::BufferSlice(zip_override);
|
||||||
}
|
}
|
||||||
if (!zip.empty()) {
|
if (!zip.empty()) {
|
||||||
hc.add_header("content-encoding", "gzip");
|
hc.add_header("content-encoding", "gzip");
|
||||||
@ -100,22 +97,19 @@ static string make_http_query(string content, bool is_json, bool is_chunked, boo
|
|||||||
} else {
|
} else {
|
||||||
hc.set_content_size(content.size());
|
hc.set_content_size(content.size());
|
||||||
}
|
}
|
||||||
string res;
|
|
||||||
auto r_header = hc.finish();
|
auto r_header = hc.finish();
|
||||||
CHECK(r_header.is_ok());
|
CHECK(r_header.is_ok());
|
||||||
res += r_header.ok().str();
|
return PSTRING() << r_header.ok() << content;
|
||||||
res += content;
|
|
||||||
return res;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static string rand_http_query(string content) {
|
static td::string rand_http_query(td::string content) {
|
||||||
bool is_chunked = Random::fast_bool();
|
bool is_chunked = td::Random::fast_bool();
|
||||||
bool is_gzip = Random::fast_bool();
|
bool is_gzip = td::Random::fast_bool();
|
||||||
return make_http_query(std::move(content), false, is_chunked, is_gzip);
|
return make_http_query(std::move(content), false, is_chunked, is_gzip);
|
||||||
}
|
}
|
||||||
|
|
||||||
static string join(const std::vector<string> &v) {
|
static td::string join(const td::vector<td::string> &v) {
|
||||||
string res;
|
td::string res;
|
||||||
for (auto &s : v) {
|
for (auto &s : v) {
|
||||||
res += s;
|
res += s;
|
||||||
}
|
}
|
||||||
@ -123,10 +117,10 @@ static string join(const std::vector<string> &v) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, stack_overflow) {
|
TEST(Http, stack_overflow) {
|
||||||
ChainBufferWriter writer;
|
td::ChainBufferWriter writer;
|
||||||
BufferSlice slice(string(256, 'A'));
|
td::BufferSlice slice(td::string(256, 'A'));
|
||||||
for (int i = 0; i < 1000000; i++) {
|
for (int i = 0; i < 1000000; i++) {
|
||||||
ChainBufferWriter tmp_writer;
|
td::ChainBufferWriter tmp_writer;
|
||||||
writer.append(slice.clone());
|
writer.append(slice.clone());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
@ -139,12 +133,12 @@ TEST(Http, reader) {
|
|||||||
#if TD_ANDROID || TD_TIZEN
|
#if TD_ANDROID || TD_TIZEN
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
clear_thread_locals();
|
td::clear_thread_locals();
|
||||||
auto start_mem = BufferAllocator::get_buffer_mem();
|
auto start_mem = td::BufferAllocator::get_buffer_mem();
|
||||||
auto start_size = BufferAllocator::get_buffer_slice_size();
|
auto start_size = td::BufferAllocator::get_buffer_slice_size();
|
||||||
{
|
{
|
||||||
BufferSlice a("test test");
|
td::BufferSlice a("test test");
|
||||||
BufferSlice b = std::move(a);
|
td::BufferSlice b = std::move(a);
|
||||||
#if TD_CLANG
|
#if TD_CLANG
|
||||||
#pragma clang diagnostic push
|
#pragma clang diagnostic push
|
||||||
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
#pragma clang diagnostic ignored "-Wunknown-pragmas"
|
||||||
@ -157,32 +151,32 @@ TEST(Http, reader) {
|
|||||||
#pragma clang diagnostic pop
|
#pragma clang diagnostic pop
|
||||||
#endif
|
#endif
|
||||||
a = std::move(b);
|
a = std::move(b);
|
||||||
BufferSlice c = a.from_slice(a);
|
td::BufferSlice c = a.from_slice(a);
|
||||||
CHECK(c.size() == a.size());
|
CHECK(c.size() == a.size());
|
||||||
}
|
}
|
||||||
clear_thread_locals();
|
td::clear_thread_locals();
|
||||||
ASSERT_EQ(start_mem, BufferAllocator::get_buffer_mem());
|
ASSERT_EQ(start_mem, td::BufferAllocator::get_buffer_mem());
|
||||||
ASSERT_EQ(start_size, BufferAllocator::get_buffer_slice_size());
|
ASSERT_EQ(start_size, td::BufferAllocator::get_buffer_slice_size());
|
||||||
for (int i = 0; i < 20; i++) {
|
for (int i = 0; i < 20; i++) {
|
||||||
td::ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
HttpReader reader;
|
td::HttpReader reader;
|
||||||
int max_post_size = 10000;
|
int max_post_size = 10000;
|
||||||
reader.init(&input, max_post_size, 0);
|
reader.init(&input, max_post_size, 0);
|
||||||
|
|
||||||
std::vector<string> contents(100);
|
td::vector<td::string> contents(100);
|
||||||
std::generate(contents.begin(), contents.end(), gen_http_content);
|
std::generate(contents.begin(), contents.end(), gen_http_content);
|
||||||
auto v = td::transform(contents, rand_http_query);
|
auto v = td::transform(contents, rand_http_query);
|
||||||
auto vec_str = rand_split(join(v));
|
auto vec_str = td::rand_split(join(v));
|
||||||
|
|
||||||
HttpQuery q;
|
td::HttpQuery q;
|
||||||
std::vector<string> res;
|
td::vector<td::string> res;
|
||||||
for (auto &str : vec_str) {
|
for (auto &str : vec_str) {
|
||||||
input_writer.append(str);
|
input_writer.append(str);
|
||||||
input.sync_with_writer();
|
input.sync_with_writer();
|
||||||
while (true) {
|
while (true) {
|
||||||
auto r_state = reader.read_next(&q);
|
auto r_state = reader.read_next(&q);
|
||||||
LOG_IF(ERROR, r_state.is_error()) << r_state.error() << tag("ok", res.size());
|
LOG_IF(ERROR, r_state.is_error()) << r_state.error() << td::tag("ok", res.size());
|
||||||
ASSERT_TRUE(r_state.is_ok());
|
ASSERT_TRUE(r_state.is_ok());
|
||||||
auto state = r_state.ok();
|
auto state = r_state.ok();
|
||||||
if (state == 0) {
|
if (state == 0) {
|
||||||
@ -192,11 +186,11 @@ TEST(Http, reader) {
|
|||||||
ASSERT_EQ(expected, q.content_.str());
|
ASSERT_EQ(expected, q.content_.str());
|
||||||
res.push_back(q.content_.str());
|
res.push_back(q.content_.str());
|
||||||
} else {
|
} else {
|
||||||
auto r_fd = FileFd::open(q.files_[0].temp_file_name, FileFd::Read);
|
auto r_fd = td::FileFd::open(q.files_[0].temp_file_name, td::FileFd::Read);
|
||||||
ASSERT_TRUE(r_fd.is_ok());
|
ASSERT_TRUE(r_fd.is_ok());
|
||||||
auto fd = r_fd.move_as_ok();
|
auto fd = r_fd.move_as_ok();
|
||||||
string content(td::narrow_cast<size_t>(q.files_[0].size), '\0');
|
td::string content(td::narrow_cast<std::size_t>(q.files_[0].size), '\0');
|
||||||
auto r_size = fd.read(MutableSlice(content));
|
auto r_size = fd.read(td::MutableSlice(content));
|
||||||
ASSERT_TRUE(r_size.is_ok());
|
ASSERT_TRUE(r_size.is_ok());
|
||||||
ASSERT_TRUE(r_size.ok() == content.size());
|
ASSERT_TRUE(r_size.ok() == content.size());
|
||||||
ASSERT_TRUE(td::narrow_cast<int>(content.size()) > max_post_size);
|
ASSERT_TRUE(td::narrow_cast<int>(content.size()) > max_post_size);
|
||||||
@ -212,24 +206,26 @@ TEST(Http, reader) {
|
|||||||
ASSERT_EQ(contents.size(), res.size());
|
ASSERT_EQ(contents.size(), res.size());
|
||||||
ASSERT_EQ(contents, res);
|
ASSERT_EQ(contents, res);
|
||||||
}
|
}
|
||||||
clear_thread_locals();
|
td::clear_thread_locals();
|
||||||
ASSERT_EQ(start_mem, BufferAllocator::get_buffer_mem());
|
ASSERT_EQ(start_mem, td::BufferAllocator::get_buffer_mem());
|
||||||
ASSERT_EQ(start_size, BufferAllocator::get_buffer_slice_size());
|
ASSERT_EQ(start_size, td::BufferAllocator::get_buffer_slice_size());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, gzip_bomb) {
|
TEST(Http, gzip_bomb) {
|
||||||
#if TD_ANDROID || TD_TIZEN || TD_EMSCRIPTEN // the test should be disabled on low-memory systems
|
#if TD_ANDROID || TD_TIZEN || TD_EMSCRIPTEN // the test must be disabled on low-memory systems
|
||||||
return;
|
return;
|
||||||
#endif
|
#endif
|
||||||
auto gzip_bomb_str =
|
auto gzip_bomb_str =
|
||||||
gzdecode(gzdecode(base64url_decode(Slice(gzip_bomb, gzip_bomb_size)).ok()).as_slice()).as_slice().str();
|
td::gzdecode(td::gzdecode(td::base64url_decode(td::Slice(gzip_bomb, gzip_bomb_size)).ok()).as_slice())
|
||||||
|
.as_slice()
|
||||||
|
.str();
|
||||||
|
|
||||||
auto query = make_http_query("", false, false, true, 0.01, gzip_bomb_str);
|
auto query = make_http_query("", false, false, true, 0.01, gzip_bomb_str);
|
||||||
auto parts = rand_split(query);
|
auto parts = td::rand_split(query);
|
||||||
td::ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
HttpReader reader;
|
td::HttpReader reader;
|
||||||
HttpQuery q;
|
td::HttpQuery q;
|
||||||
reader.init(&input, 100000000, 0);
|
reader.init(&input, 100000000, 0);
|
||||||
for (auto &part : parts) {
|
for (auto &part : parts) {
|
||||||
input_writer.append(part);
|
input_writer.append(part);
|
||||||
@ -244,39 +240,39 @@ TEST(Http, gzip_bomb) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, gzip) {
|
TEST(Http, gzip) {
|
||||||
auto gzip_str = gzdecode(base64url_decode(Slice(gzip, gzip_size)).ok()).as_slice().str();
|
auto gzip_str = td::gzdecode(td::base64url_decode(td::Slice(gzip, gzip_size)).ok()).as_slice().str();
|
||||||
|
|
||||||
td::ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
|
|
||||||
HttpReader reader;
|
td::HttpReader reader;
|
||||||
reader.init(&input, 0, 0);
|
reader.init(&input, 0, 0);
|
||||||
|
|
||||||
auto query = make_http_query("", true, false, true, 0.01, gzip_str);
|
auto query = make_http_query("", true, false, true, 0.01, gzip_str);
|
||||||
input_writer.append(query);
|
input_writer.append(query);
|
||||||
input.sync_with_writer();
|
input.sync_with_writer();
|
||||||
|
|
||||||
HttpQuery q;
|
td::HttpQuery q;
|
||||||
auto r_state = reader.read_next(&q);
|
auto r_state = reader.read_next(&q);
|
||||||
ASSERT_TRUE(r_state.is_error());
|
ASSERT_TRUE(r_state.is_error());
|
||||||
ASSERT_EQ(413, r_state.error().code());
|
ASSERT_EQ(413, r_state.error().code());
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, aes_ctr_encode_decode_flow) {
|
TEST(Http, aes_ctr_encode_decode_flow) {
|
||||||
auto str = rand_string('a', 'z', 1000000);
|
auto str = td::rand_string('a', 'z', 1000000);
|
||||||
auto parts = rand_split(str);
|
auto parts = td::rand_split(str);
|
||||||
td::ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
ByteFlowSource source(&input);
|
td::ByteFlowSource source(&input);
|
||||||
UInt256 key;
|
td::UInt256 key;
|
||||||
UInt128 iv;
|
td::UInt128 iv;
|
||||||
Random::secure_bytes(key.raw, sizeof(key));
|
td::Random::secure_bytes(key.raw, sizeof(key));
|
||||||
Random::secure_bytes(iv.raw, sizeof(iv));
|
td::Random::secure_bytes(iv.raw, sizeof(iv));
|
||||||
AesCtrByteFlow aes_encode;
|
td::AesCtrByteFlow aes_encode;
|
||||||
aes_encode.init(key, iv);
|
aes_encode.init(key, iv);
|
||||||
AesCtrByteFlow aes_decode;
|
td::AesCtrByteFlow aes_decode;
|
||||||
aes_decode.init(key, iv);
|
aes_decode.init(key, iv);
|
||||||
ByteFlowSink sink;
|
td::ByteFlowSink sink;
|
||||||
source >> aes_encode >> aes_decode >> sink;
|
source >> aes_encode >> aes_decode >> sink;
|
||||||
|
|
||||||
ASSERT_TRUE(!sink.is_ready());
|
ASSERT_TRUE(!sink.is_ready());
|
||||||
@ -285,7 +281,7 @@ TEST(Http, aes_ctr_encode_decode_flow) {
|
|||||||
source.wakeup();
|
source.wakeup();
|
||||||
}
|
}
|
||||||
ASSERT_TRUE(!sink.is_ready());
|
ASSERT_TRUE(!sink.is_ready());
|
||||||
source.close_input(Status::OK());
|
source.close_input(td::Status::OK());
|
||||||
ASSERT_TRUE(sink.is_ready());
|
ASSERT_TRUE(sink.is_ready());
|
||||||
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
||||||
ASSERT_TRUE(sink.status().is_ok());
|
ASSERT_TRUE(sink.status().is_ok());
|
||||||
@ -293,25 +289,25 @@ TEST(Http, aes_ctr_encode_decode_flow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, aes_file_encryption) {
|
TEST(Http, aes_file_encryption) {
|
||||||
auto str = rand_string('a', 'z', 1000000);
|
auto str = td::rand_string('a', 'z', 1000000);
|
||||||
CSlice name = "test_encryption";
|
td::CSlice name = "test_encryption";
|
||||||
unlink(name).ignore();
|
td::unlink(name).ignore();
|
||||||
UInt256 key;
|
td::UInt256 key;
|
||||||
UInt128 iv;
|
td::UInt128 iv;
|
||||||
Random::secure_bytes(key.raw, sizeof(key));
|
td::Random::secure_bytes(key.raw, sizeof(key));
|
||||||
Random::secure_bytes(iv.raw, sizeof(iv));
|
td::Random::secure_bytes(iv.raw, sizeof(iv));
|
||||||
|
|
||||||
{
|
{
|
||||||
BufferedFdBase<FileFd> fd(FileFd::open(name, FileFd::Write | FileFd::Create).move_as_ok());
|
td::BufferedFdBase<td::FileFd> fd(td::FileFd::open(name, td::FileFd::Write | td::FileFd::Create).move_as_ok());
|
||||||
|
|
||||||
auto parts = rand_split(str);
|
auto parts = td::rand_split(str);
|
||||||
|
|
||||||
ChainBufferWriter output_writer;
|
td::ChainBufferWriter output_writer;
|
||||||
auto output_reader = output_writer.extract_reader();
|
auto output_reader = output_writer.extract_reader();
|
||||||
ByteFlowSource source(&output_reader);
|
td::ByteFlowSource source(&output_reader);
|
||||||
AesCtrByteFlow aes_encode;
|
td::AesCtrByteFlow aes_encode;
|
||||||
aes_encode.init(key, iv);
|
aes_encode.init(key, iv);
|
||||||
ByteFlowSink sink;
|
td::ByteFlowSink sink;
|
||||||
|
|
||||||
source >> aes_encode >> sink;
|
source >> aes_encode >> sink;
|
||||||
fd.set_output_reader(sink.get_output());
|
fd.set_output_reader(sink.get_output());
|
||||||
@ -325,18 +321,18 @@ TEST(Http, aes_file_encryption) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
BufferedFdBase<FileFd> fd(FileFd::open(name, FileFd::Read).move_as_ok());
|
td::BufferedFdBase<td::FileFd> fd(td::FileFd::open(name, td::FileFd::Read).move_as_ok());
|
||||||
|
|
||||||
ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input_reader = input_writer.extract_reader();
|
auto input_reader = input_writer.extract_reader();
|
||||||
ByteFlowSource source(&input_reader);
|
td::ByteFlowSource source(&input_reader);
|
||||||
AesCtrByteFlow aes_encode;
|
td::AesCtrByteFlow aes_encode;
|
||||||
aes_encode.init(key, iv);
|
aes_encode.init(key, iv);
|
||||||
ByteFlowSink sink;
|
td::ByteFlowSink sink;
|
||||||
source >> aes_encode >> sink;
|
source >> aes_encode >> sink;
|
||||||
fd.set_input_writer(&input_writer);
|
fd.set_input_writer(&input_writer);
|
||||||
|
|
||||||
fd.get_poll_info().add_flags(PollFlags::Read());
|
fd.get_poll_info().add_flags(td::PollFlags::Read());
|
||||||
while (can_read_local(fd)) {
|
while (can_read_local(fd)) {
|
||||||
fd.flush_read(4096).ensure();
|
fd.flush_read(4096).ensure();
|
||||||
source.wakeup();
|
source.wakeup();
|
||||||
@ -344,7 +340,7 @@ TEST(Http, aes_file_encryption) {
|
|||||||
|
|
||||||
fd.close();
|
fd.close();
|
||||||
|
|
||||||
source.close_input(Status::OK());
|
source.close_input(td::Status::OK());
|
||||||
ASSERT_TRUE(sink.is_ready());
|
ASSERT_TRUE(sink.is_ready());
|
||||||
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
||||||
ASSERT_TRUE(sink.status().is_ok());
|
ASSERT_TRUE(sink.status().is_ok());
|
||||||
@ -354,20 +350,20 @@ TEST(Http, aes_file_encryption) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, chunked_flow) {
|
TEST(Http, chunked_flow) {
|
||||||
auto str = rand_string('a', 'z', 100);
|
auto str = td::rand_string('a', 'z', 100);
|
||||||
auto parts = rand_split(make_chunked(str));
|
auto parts = td::rand_split(make_chunked(str));
|
||||||
td::ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
ByteFlowSource source(&input);
|
td::ByteFlowSource source(&input);
|
||||||
HttpChunkedByteFlow chunked_flow;
|
td::HttpChunkedByteFlow chunked_flow;
|
||||||
ByteFlowSink sink;
|
td::ByteFlowSink sink;
|
||||||
source >> chunked_flow >> sink;
|
source >> chunked_flow >> sink;
|
||||||
|
|
||||||
for (auto &part : parts) {
|
for (auto &part : parts) {
|
||||||
input_writer.append(part);
|
input_writer.append(part);
|
||||||
source.wakeup();
|
source.wakeup();
|
||||||
}
|
}
|
||||||
source.close_input(Status::OK());
|
source.close_input(td::Status::OK());
|
||||||
ASSERT_TRUE(sink.is_ready());
|
ASSERT_TRUE(sink.is_ready());
|
||||||
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
||||||
ASSERT_TRUE(sink.status().is_ok());
|
ASSERT_TRUE(sink.status().is_ok());
|
||||||
@ -377,16 +373,16 @@ TEST(Http, chunked_flow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, chunked_flow_error) {
|
TEST(Http, chunked_flow_error) {
|
||||||
auto str = rand_string('a', 'z', 100000);
|
auto str = td::rand_string('a', 'z', 100000);
|
||||||
for (int d = 1; d < 100; d += 10) {
|
for (int d = 1; d < 100; d += 10) {
|
||||||
auto new_str = make_chunked(str);
|
auto new_str = make_chunked(str);
|
||||||
new_str.resize(str.size() - d);
|
new_str.resize(str.size() - d);
|
||||||
auto parts = rand_split(new_str);
|
auto parts = td::rand_split(new_str);
|
||||||
td::ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
ByteFlowSource source(&input);
|
td::ByteFlowSource source(&input);
|
||||||
HttpChunkedByteFlow chunked_flow;
|
td::HttpChunkedByteFlow chunked_flow;
|
||||||
ByteFlowSink sink;
|
td::ByteFlowSink sink;
|
||||||
source >> chunked_flow >> sink;
|
source >> chunked_flow >> sink;
|
||||||
|
|
||||||
for (auto &part : parts) {
|
for (auto &part : parts) {
|
||||||
@ -394,29 +390,29 @@ TEST(Http, chunked_flow_error) {
|
|||||||
source.wakeup();
|
source.wakeup();
|
||||||
}
|
}
|
||||||
ASSERT_TRUE(!sink.is_ready());
|
ASSERT_TRUE(!sink.is_ready());
|
||||||
source.close_input(Status::OK());
|
source.close_input(td::Status::OK());
|
||||||
ASSERT_TRUE(sink.is_ready());
|
ASSERT_TRUE(sink.is_ready());
|
||||||
ASSERT_TRUE(!sink.status().is_ok());
|
ASSERT_TRUE(!sink.status().is_ok());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, gzip_chunked_flow) {
|
TEST(Http, gzip_chunked_flow) {
|
||||||
auto str = rand_string('a', 'z', 1000000);
|
auto str = td::rand_string('a', 'z', 1000000);
|
||||||
auto parts = rand_split(make_chunked(gzencode(str, 2.0).as_slice().str()));
|
auto parts = td::rand_split(make_chunked(td::gzencode(str, 2.0).as_slice().str()));
|
||||||
|
|
||||||
ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
ByteFlowSource source(&input);
|
td::ByteFlowSource source(&input);
|
||||||
HttpChunkedByteFlow chunked_flow;
|
td::HttpChunkedByteFlow chunked_flow;
|
||||||
GzipByteFlow gzip_flow(Gzip::Mode::Decode);
|
td::GzipByteFlow gzip_flow(td::Gzip::Mode::Decode);
|
||||||
ByteFlowSink sink;
|
td::ByteFlowSink sink;
|
||||||
source >> chunked_flow >> gzip_flow >> sink;
|
source >> chunked_flow >> gzip_flow >> sink;
|
||||||
|
|
||||||
for (auto &part : parts) {
|
for (auto &part : parts) {
|
||||||
input_writer.append(part);
|
input_writer.append(part);
|
||||||
source.wakeup();
|
source.wakeup();
|
||||||
}
|
}
|
||||||
source.close_input(Status::OK());
|
source.close_input(td::Status::OK());
|
||||||
ASSERT_TRUE(sink.is_ready());
|
ASSERT_TRUE(sink.is_ready());
|
||||||
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
||||||
ASSERT_TRUE(sink.status().is_ok());
|
ASSERT_TRUE(sink.status().is_ok());
|
||||||
@ -424,21 +420,21 @@ TEST(Http, gzip_chunked_flow) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
TEST(Http, gzip_bomb_with_limit) {
|
TEST(Http, gzip_bomb_with_limit) {
|
||||||
std::string gzip_bomb_str;
|
td::string gzip_bomb_str;
|
||||||
{
|
{
|
||||||
ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
GzipByteFlow gzip_flow(Gzip::Mode::Encode);
|
td::GzipByteFlow gzip_flow(td::Gzip::Mode::Encode);
|
||||||
ByteFlowSource source(&input);
|
td::ByteFlowSource source(&input);
|
||||||
ByteFlowSink sink;
|
td::ByteFlowSink sink;
|
||||||
source >> gzip_flow >> sink;
|
source >> gzip_flow >> sink;
|
||||||
|
|
||||||
std::string s(1 << 16, 'a');
|
td::string s(1 << 16, 'a');
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
input_writer.append(s);
|
input_writer.append(s);
|
||||||
source.wakeup();
|
source.wakeup();
|
||||||
}
|
}
|
||||||
source.close_input(Status::OK());
|
source.close_input(td::Status::OK());
|
||||||
ASSERT_TRUE(sink.is_ready());
|
ASSERT_TRUE(sink.is_ready());
|
||||||
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
LOG_IF(ERROR, sink.status().is_error()) << sink.status();
|
||||||
ASSERT_TRUE(sink.status().is_ok());
|
ASSERT_TRUE(sink.status().is_ok());
|
||||||
@ -446,11 +442,11 @@ TEST(Http, gzip_bomb_with_limit) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto query = make_http_query("", false, false, true, 0.01, gzip_bomb_str);
|
auto query = make_http_query("", false, false, true, 0.01, gzip_bomb_str);
|
||||||
auto parts = rand_split(query);
|
auto parts = td::rand_split(query);
|
||||||
td::ChainBufferWriter input_writer;
|
td::ChainBufferWriter input_writer;
|
||||||
auto input = input_writer.extract_reader();
|
auto input = input_writer.extract_reader();
|
||||||
HttpReader reader;
|
td::HttpReader reader;
|
||||||
HttpQuery q;
|
td::HttpQuery q;
|
||||||
reader.init(&input, 1000000);
|
reader.init(&input, 1000000);
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
for (auto &part : parts) {
|
for (auto &part : parts) {
|
||||||
@ -493,12 +489,7 @@ struct Baton {
|
|||||||
|
|
||||||
TEST(Http, Darwin) {
|
TEST(Http, Darwin) {
|
||||||
Baton baton;
|
Baton baton;
|
||||||
//LOG(ERROR) << "???";
|
td::DarwinHttp::get("http://example.com", [&](td::BufferSlice data) { baton.post(); });
|
||||||
td::DarwinHttp::get("http://example.com", [&](td::BufferSlice data) {
|
|
||||||
//LOG(ERROR) << data.as_slice();
|
|
||||||
baton.post();
|
|
||||||
});
|
|
||||||
//LOG(ERROR) << "!!!";
|
|
||||||
baton.wait();
|
baton.wait();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user