blob: f0e69227b4a5fd50fde993e0a4176dff6b0b1f67 [file] [log] [blame]
David Benjamin3ae23972024-03-18 22:43:46 +10001/* Copyright (c) 2024, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include "test_data.h"
16
17#include <stdio.h>
18#include <stdlib.h>
19
20#include "file_util.h"
21
David Benjamine2d7f2d2024-03-20 09:56:14 +100022#if defined(BORINGSSL_USE_BAZEL_RUNFILES)
23#include "tools/cpp/runfiles/runfiles.h"
24
25using bazel::tools::cpp::runfiles::Runfiles;
26#endif
27
David Benjamin3ae23972024-03-18 22:43:46 +100028#if !defined(BORINGSSL_CUSTOM_GET_TEST_DATA)
29std::string GetTestData(const char *path) {
David Benjamine2d7f2d2024-03-20 09:56:14 +100030#if defined(BORINGSSL_USE_BAZEL_RUNFILES)
31 std::string error;
32 std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest(&error));
33 if (runfiles == nullptr) {
34 fprintf(stderr, "Could not initialize runfiles: %s\n", error.c_str());
35 abort();
36 }
37
38 std::string full_path = runfiles->Rlocation(std::string("boringssl/") + path);
39 if (full_path.empty()) {
40 fprintf(stderr, "Could not find runfile '%s'.\n", path);
41 abort();
42 }
43#else
David Benjamin3ae23972024-03-18 22:43:46 +100044 const char *root = getenv("BORINGSSL_TEST_DATA_ROOT");
45 root = root != nullptr ? root : ".";
46
47 std::string full_path = root;
48 full_path.push_back('/');
49 full_path.append(path);
David Benjamine2d7f2d2024-03-20 09:56:14 +100050#endif
David Benjamin3ae23972024-03-18 22:43:46 +100051
David Benjamin03550482024-05-13 17:31:30 -040052 bssl::ScopedFILE file(fopen(full_path.c_str(), "rb"));
David Benjamin3ae23972024-03-18 22:43:46 +100053 if (file == nullptr) {
54 fprintf(stderr, "Could not open '%s'.\n", full_path.c_str());
55 abort();
56 }
57
58 std::string ret;
59 for (;;) {
60 char buf[512];
61 size_t n = fread(buf, 1, sizeof(buf), file.get());
62 if (n == 0) {
63 if (feof(file.get())) {
64 return ret;
65 }
66 fprintf(stderr, "Error reading from '%s'.\n", full_path.c_str());
67 abort();
68 }
69 ret.append(buf, n);
70 }
71}
72#endif // !BORINGSSL_CUSTOM_GET_TEST_DATA