| // Copyright 2019 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef ZIRCON_SYSTEM_ULIB_BLOBFS_TEST_BLOB_UTILS_H_ |
| #define ZIRCON_SYSTEM_ULIB_BLOBFS_TEST_BLOB_UTILS_H_ |
| |
| #include <lib/fdio/io.h> |
| |
| #include <memory> |
| #include <string> |
| |
| namespace blobfs { |
| |
| using BlobSrcFunction = void (*)(char* data, size_t length); |
| |
| // An in-memory representation of a blob. |
| struct BlobInfo { |
| char path[PATH_MAX]; |
| std::unique_ptr<char[]> merkle; |
| size_t size_merkle; |
| std::unique_ptr<char[]> data; |
| size_t size_data; |
| }; |
| |
| template <typename T, typename U> |
| int StreamAll(T func, int fd, U* buf, size_t max) { |
| size_t n = 0; |
| while (n != max) { |
| ssize_t d = func(fd, &buf[n], max - n); |
| if (d < 0) { |
| return -1; |
| } |
| n += d; |
| } |
| return 0; |
| } |
| |
| // Fills the provided buffer with random data. The contents of the buffer are |
| // repeatable for any iteration of the test, provided that srand() is seeded with |
| // the test framework's seed. |
| void RandomFill(char* data, size_t length); |
| |
| // Generates a blob with data generated by a user-provided function. |
| void GenerateBlob(BlobSrcFunction data_generator, const std::string& mount_path, size_t data_size, |
| std::unique_ptr<BlobInfo>* out); |
| |
| // Generates a blob with random data. |
| void GenerateRandomBlob(const std::string& mount_path, size_t data_size, std::unique_ptr<BlobInfo>* out); |
| |
| // Verifies that a file contains the data in the provided buffer. |
| void VerifyContents(int fd, const char* data, size_t data_size); |
| |
| } // namespace blobfs |
| |
| #endif // ZIRCON_SYSTEM_ULIB_BLOBFS_TEST_BLOB_UTILS_H_ |