1 // Copyright 2023 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 /////////////////////////////////////////////////////////////////////////////// 16 #ifndef TINK_INTERNAL_TEST_RANDOM_ACCESS_STREAM_H_ 17 #define TINK_INTERNAL_TEST_RANDOM_ACCESS_STREAM_H_ 18 19 #include <stdint.h> 20 21 #include <memory> 22 #include <string> 23 #include <utility> 24 25 #include "absl/strings/string_view.h" 26 #include "tink/random_access_stream.h" 27 #include "tink/util/buffer.h" 28 #include "tink/util/status.h" 29 #include "tink/util/statusor.h" 30 31 namespace crypto { 32 namespace tink { 33 namespace internal { 34 35 // A simple test-only RandomAccessStream implementation that reads from a 36 // std::string. 37 class TestRandomAccessStream : public RandomAccessStream { 38 public: TestRandomAccessStream(std::string content)39 explicit TestRandomAccessStream(std::string content) 40 : content_(std::move(content)) {} 41 // Move only. 42 TestRandomAccessStream(TestRandomAccessStream&& other) = default; 43 TestRandomAccessStream& operator=(TestRandomAccessStream&& other) = default; 44 TestRandomAccessStream(const TestRandomAccessStream&) = delete; 45 TestRandomAccessStream& operator=(const TestRandomAccessStream&) = delete; 46 47 util::Status PRead(int64_t position, int count, 48 util::Buffer* dest_buffer) override; 49 size()50 util::StatusOr<int64_t> size() override { return content_.size(); } 51 52 private: 53 std::string content_; 54 }; 55 56 // Reads the entire `random_access_stream` using a buffer of size `chunk_size` 57 // until no more bytes can be read, and puts the read bytes into `contents`. 58 // Returns the status of the last call to random_access_stream->PRead(). 59 util::Status ReadAllFromRandomAccessStream( 60 RandomAccessStream* random_access_stream, std::string& contents, 61 int chunk_size = 42); 62 63 } // namespace internal 64 } // namespace tink 65 } // namespace crypto 66 67 #endif // TINK_INTERNAL_TEST_RANDOM_ACCESS_STREAM_H_ 68