|
1 | 1 | // Copyright The OpenTelemetry Authors |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
| 4 | +#include <curl/curl.h> |
4 | 5 | #include <curl/curlver.h> |
5 | 6 | #include "gtest/gtest.h" |
6 | 7 |
|
7 | 8 | #ifdef ENABLE_OTLP_RETRY_PREVIEW |
8 | | -# include <curl/curl.h> |
9 | 9 | # include "gmock/gmock.h" |
10 | 10 | #endif // ENABLE_OTLP_RETRY_PREVIEW |
11 | 11 |
|
12 | 12 | #ifdef ENABLE_OTLP_COMPRESSION_PREVIEW |
13 | 13 | # include <numeric> |
14 | 14 | #endif // ENABLE_OTLP_COMPRESSION_PREVIEW |
15 | 15 |
|
| 16 | +#include <algorithm> |
16 | 17 | #include <atomic> |
17 | 18 | #include <chrono> |
18 | 19 | #include <condition_variable> |
| 20 | +#include <cstdio> |
19 | 21 | #include <cstring> |
20 | 22 | #include <map> |
21 | 23 | #include <memory> |
@@ -57,6 +59,27 @@ class HttpClientTestPeer |
57 | 59 | public: |
58 | 60 | static void ResetMultiHandle(HttpClient &client) { client.resetMultiHandle(); } |
59 | 61 | }; |
| 62 | + |
| 63 | +class HttpOperationTestPeer |
| 64 | +{ |
| 65 | +public: |
| 66 | + static int Seek(HttpOperation &operation, curl_off_t offset, int origin) |
| 67 | + { |
| 68 | + return HttpOperation::SeekCallback(&operation, offset, origin); |
| 69 | + } |
| 70 | + |
| 71 | + static int SeekNullUserData(curl_off_t offset, int origin) |
| 72 | + { |
| 73 | + return HttpOperation::SeekCallback(nullptr, offset, origin); |
| 74 | + } |
| 75 | + |
| 76 | + static size_t ReadCursor(const HttpOperation &operation) { return operation.request_nwrite_; } |
| 77 | + |
| 78 | + static void SetReadCursor(HttpOperation &operation, size_t value) |
| 79 | + { |
| 80 | + operation.request_nwrite_ = value; |
| 81 | + } |
| 82 | +}; |
60 | 83 | } // namespace curl |
61 | 84 | } // namespace client |
62 | 85 | } // namespace http |
@@ -405,6 +428,89 @@ TEST_F(BasicCurlHttpTests, SendPostRequest) |
405 | 428 | session_manager->FinishAllSessions(); |
406 | 429 | } |
407 | 430 |
|
| 431 | +// Send a body large enough to span several read callbacks and check it arrives whole, so a mistake |
| 432 | +// in either CURLOPT_READFUNCTION or the seek callback registered beside it shows up as a corrupted |
| 433 | +// or short upload. |
| 434 | +TEST_F(BasicCurlHttpTests, SendPostRequestWithMultiChunkBody) |
| 435 | +{ |
| 436 | + received_requests_.clear(); |
| 437 | + auto session_manager = std::make_shared<http_client::curl::HttpCurlClientFactory>()->Create(); |
| 438 | + EXPECT_TRUE(session_manager != nullptr); |
| 439 | + |
| 440 | + auto session = session_manager->CreateSession("http://127.0.0.1:19000"); |
| 441 | + auto request = session->CreateRequest(); |
| 442 | + request->SetUri("post/"); |
| 443 | + request->SetMethod(http_client::Method::Post); |
| 444 | + |
| 445 | + // Not a round number, so an off-by-one in the read cursor cannot land on a chunk boundary. |
| 446 | + constexpr size_t kBodySize = 257u * 1024u + 7u; |
| 447 | + http_client::Body body(kBodySize); |
| 448 | + for (size_t i = 0; i < kBodySize; ++i) |
| 449 | + { |
| 450 | + body[i] = static_cast<http_client::Byte>('a' + (i % 26)); |
| 451 | + } |
| 452 | + const http_client::Body expected = body; |
| 453 | + |
| 454 | + request->SetBody(body); |
| 455 | + request->AddHeader("Content-Type", "application/octet-stream"); |
| 456 | + auto handler = std::make_shared<PostEventHandler>(); |
| 457 | + session->SendRequest(handler); |
| 458 | + ASSERT_TRUE(waitForRequests(30, 1)); |
| 459 | + session->FinishSession(); |
| 460 | + ASSERT_TRUE(handler->is_called_.load(std::memory_order_acquire)); |
| 461 | + ASSERT_TRUE(handler->got_response_.load(std::memory_order_acquire)); |
| 462 | + |
| 463 | + { |
| 464 | + std::unique_lock<std::mutex> lk(mtx_requests); |
| 465 | + ASSERT_EQ(received_requests_.size(), 1u); |
| 466 | + const auto &received = received_requests_[0].content; |
| 467 | + ASSERT_EQ(received.size(), expected.size()); |
| 468 | + EXPECT_TRUE(std::equal(expected.begin(), expected.end(), received.begin())); |
| 469 | + } |
| 470 | + |
| 471 | + session_manager->CancelAllSessions(); |
| 472 | + session_manager->FinishAllSessions(); |
| 473 | +} |
| 474 | + |
| 475 | +// Cover both halves of the callback contract, the seeks it honours and the ones it refuses. |
| 476 | +TEST_F(BasicCurlHttpTests, SeekCallbackRepositionsTheRequestBody) |
| 477 | +{ |
| 478 | + CustomEventHandler handler; |
| 479 | + http_client::HttpSslOptions no_ssl; |
| 480 | + http_client::Headers headers; |
| 481 | + const char *payload = "0123456789"; |
| 482 | + http_client::Body body = {payload, payload + std::strlen(payload)}; |
| 483 | + |
| 484 | + curl::HttpOperation operation(http_client::Method::Post, "http://127.0.0.1:19000/post/", no_ssl, |
| 485 | + &handler, headers, body, http_client::Compression::kNone, false, |
| 486 | + curl::kDefaultHttpConnTimeout); |
| 487 | + |
| 488 | + using Peer = curl::HttpOperationTestPeer; |
| 489 | + |
| 490 | + Peer::SetReadCursor(operation, 10); |
| 491 | + EXPECT_EQ(CURL_SEEKFUNC_OK, Peer::Seek(operation, 4, SEEK_SET)); |
| 492 | + EXPECT_EQ(4u, Peer::ReadCursor(operation)); |
| 493 | + |
| 494 | + // Rewinding to the start is the case libcurl actually asks for. |
| 495 | + EXPECT_EQ(CURL_SEEKFUNC_OK, Peer::Seek(operation, 0, SEEK_SET)); |
| 496 | + EXPECT_EQ(0u, Peer::ReadCursor(operation)); |
| 497 | + |
| 498 | + // Seeking to exactly the end is in range and leaves nothing left to send. |
| 499 | + EXPECT_EQ(CURL_SEEKFUNC_OK, Peer::Seek(operation, 10, SEEK_SET)); |
| 500 | + EXPECT_EQ(10u, Peer::ReadCursor(operation)); |
| 501 | + |
| 502 | + // Everything below is refused, and must leave the cursor where it was. |
| 503 | + Peer::SetReadCursor(operation, 3); |
| 504 | + |
| 505 | + EXPECT_EQ(CURL_SEEKFUNC_CANTSEEK, Peer::Seek(operation, 11, SEEK_SET)); |
| 506 | + EXPECT_EQ(CURL_SEEKFUNC_CANTSEEK, Peer::Seek(operation, -1, SEEK_SET)); |
| 507 | + EXPECT_EQ(CURL_SEEKFUNC_CANTSEEK, Peer::Seek(operation, 0, SEEK_CUR)); |
| 508 | + EXPECT_EQ(CURL_SEEKFUNC_CANTSEEK, Peer::Seek(operation, 0, SEEK_END)); |
| 509 | + EXPECT_EQ(3u, Peer::ReadCursor(operation)); |
| 510 | + |
| 511 | + EXPECT_EQ(CURL_SEEKFUNC_CANTSEEK, Peer::SeekNullUserData(0, SEEK_SET)); |
| 512 | +} |
| 513 | + |
408 | 514 | TEST_F(BasicCurlHttpTests, RequestTimeout) |
409 | 515 | { |
410 | 516 | received_requests_.clear(); |
|
0 commit comments