mock_http_fetcher.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // Copyright (C) 2009 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #ifndef UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_
  17. #define UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_
  18. #include <map>
  19. #include <string>
  20. #include <vector>
  21. #include <base/logging.h>
  22. #include <brillo/message_loops/message_loop.h>
  23. #include "update_engine/common/http_fetcher.h"
  24. // This is a mock implementation of HttpFetcher which is useful for testing.
  25. // All data must be passed into the ctor. When started, MockHttpFetcher will
  26. // deliver the data in chunks of size kMockHttpFetcherChunkSize. To simulate
  27. // a network failure, you can call FailTransfer().
  28. namespace chromeos_update_engine {
  29. // MockHttpFetcher will send a chunk of data down in each call to BeginTransfer
  30. // and Unpause. For the other chunks of data, a callback is put on the run
  31. // loop and when that's called, another chunk is sent down.
  32. const size_t kMockHttpFetcherChunkSize(65536);
  33. class MockHttpFetcher : public HttpFetcher {
  34. public:
  35. // The data passed in here is copied and then passed to the delegate after
  36. // the transfer begins.
  37. MockHttpFetcher(const uint8_t* data,
  38. size_t size,
  39. ProxyResolver* proxy_resolver)
  40. : HttpFetcher(proxy_resolver),
  41. sent_size_(0),
  42. timeout_id_(brillo::MessageLoop::kTaskIdNull),
  43. paused_(false),
  44. fail_transfer_(false),
  45. never_use_(false) {
  46. data_.insert(data_.end(), data, data + size);
  47. }
  48. // Constructor overload for string data.
  49. MockHttpFetcher(const char* data, size_t size, ProxyResolver* proxy_resolver)
  50. : MockHttpFetcher(
  51. reinterpret_cast<const uint8_t*>(data), size, proxy_resolver) {}
  52. // Cleans up all internal state. Does not notify delegate
  53. ~MockHttpFetcher() override;
  54. // Ignores this.
  55. void SetOffset(off_t offset) override {
  56. sent_size_ = offset;
  57. if (delegate_)
  58. delegate_->SeekToOffset(offset);
  59. }
  60. // Do nothing.
  61. void SetLength(size_t length) override {}
  62. void UnsetLength() override {}
  63. void set_low_speed_limit(int low_speed_bps, int low_speed_sec) override {}
  64. void set_connect_timeout(int connect_timeout_seconds) override {}
  65. void set_max_retry_count(int max_retry_count) override {}
  66. // Dummy: no bytes were downloaded.
  67. size_t GetBytesDownloaded() override { return sent_size_; }
  68. // Begins the transfer if it hasn't already begun.
  69. void BeginTransfer(const std::string& url) override;
  70. // If the transfer is in progress, aborts the transfer early.
  71. // The transfer cannot be resumed.
  72. void TerminateTransfer() override;
  73. void SetHeader(const std::string& header_name,
  74. const std::string& header_value) override;
  75. // Return the value of the header |header_name| or the empty string if not
  76. // set.
  77. std::string GetHeader(const std::string& header_name) const;
  78. // Suspend the mock transfer.
  79. void Pause() override;
  80. // Resume the mock transfer.
  81. void Unpause() override;
  82. // Fail the transfer. This simulates a network failure.
  83. void FailTransfer(int http_response_code);
  84. // If set to true, this will EXPECT fail on BeginTransfer
  85. void set_never_use(bool never_use) { never_use_ = never_use; }
  86. const brillo::Blob& post_data() const { return post_data_; }
  87. private:
  88. // Sends data to the delegate and sets up a timeout callback if needed. There
  89. // must be a delegate. If |skip_delivery| is true, no bytes will be delivered,
  90. // but the callbacks still be set if needed.
  91. void SendData(bool skip_delivery);
  92. // Callback for when our message loop timeout expires.
  93. void TimeoutCallback();
  94. // Sets the HTTP response code and signals to the delegate that the transfer
  95. // is complete.
  96. void SignalTransferComplete();
  97. // A full copy of the data we'll return to the delegate
  98. brillo::Blob data_;
  99. // The number of bytes we've sent so far
  100. size_t sent_size_;
  101. // The extra headers set.
  102. std::map<std::string, std::string> extra_headers_;
  103. // The TaskId of the timeout callback. After each chunk of data sent, we
  104. // time out for 0s just to make sure that run loop services other clients.
  105. brillo::MessageLoop::TaskId timeout_id_;
  106. // True iff the fetcher is paused.
  107. bool paused_;
  108. // Set to true if the transfer should fail.
  109. bool fail_transfer_;
  110. // Set to true if BeginTransfer should EXPECT fail.
  111. bool never_use_;
  112. DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
  113. };
  114. } // namespace chromeos_update_engine
  115. #endif // UPDATE_ENGINE_COMMON_MOCK_HTTP_FETCHER_H_