xref: /aosp_15_r20/external/bazel-skylib/tests/native_binary/assertdata.cc (revision bcb5dc7965af6ee42bf2f21341a2ec00233a8c8a)
1*bcb5dc79SHONG Yifan // Copyright 2019 The Bazel Authors. All rights reserved.
2*bcb5dc79SHONG Yifan //
3*bcb5dc79SHONG Yifan // Licensed under the Apache License, Version 2.0 (the "License");
4*bcb5dc79SHONG Yifan // you may not use this file except in compliance with the License.
5*bcb5dc79SHONG Yifan // You may obtain a copy of the License at
6*bcb5dc79SHONG Yifan //
7*bcb5dc79SHONG Yifan //    http://www.apache.org/licenses/LICENSE-2.0
8*bcb5dc79SHONG Yifan //
9*bcb5dc79SHONG Yifan // Unless required by applicable law or agreed to in writing, software
10*bcb5dc79SHONG Yifan // distributed under the License is distributed on an "AS IS" BASIS,
11*bcb5dc79SHONG Yifan // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*bcb5dc79SHONG Yifan // See the License for the specific language governing permissions and
13*bcb5dc79SHONG Yifan // limitations under the License.
14*bcb5dc79SHONG Yifan 
15*bcb5dc79SHONG Yifan #include <stdio.h>
16*bcb5dc79SHONG Yifan 
17*bcb5dc79SHONG Yifan #include <memory>
18*bcb5dc79SHONG Yifan #include <string>
19*bcb5dc79SHONG Yifan 
20*bcb5dc79SHONG Yifan #include "tools/cpp/runfiles/runfiles.h"
21*bcb5dc79SHONG Yifan 
22*bcb5dc79SHONG Yifan using bazel::tools::cpp::runfiles::Runfiles;
23*bcb5dc79SHONG Yifan 
main(int argc,char ** argv)24*bcb5dc79SHONG Yifan int main(int argc, char **argv) {
25*bcb5dc79SHONG Yifan   std::string error;
26*bcb5dc79SHONG Yifan   std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv[0], &error));
27*bcb5dc79SHONG Yifan   if (runfiles == nullptr) {
28*bcb5dc79SHONG Yifan     fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not init runfiles\n",
29*bcb5dc79SHONG Yifan             __LINE__);
30*bcb5dc79SHONG Yifan     return 1;
31*bcb5dc79SHONG Yifan   }
32*bcb5dc79SHONG Yifan 
33*bcb5dc79SHONG Yifan   char* workspace = getenv("TEST_WORKSPACE");
34*bcb5dc79SHONG Yifan   if (workspace == nullptr) {
35*bcb5dc79SHONG Yifan     fprintf(stderr, "ERROR(" __FILE__ ":%d): envvar TEST_WORKSPACE is undefined\n",
36*bcb5dc79SHONG Yifan             __LINE__);
37*bcb5dc79SHONG Yifan     return 1;
38*bcb5dc79SHONG Yifan   }
39*bcb5dc79SHONG Yifan 
40*bcb5dc79SHONG Yifan   // This should have runfiles, either from the binary itself or from the
41*bcb5dc79SHONG Yifan   // native_test.
42*bcb5dc79SHONG Yifan   std::string path =
43*bcb5dc79SHONG Yifan       runfiles->Rlocation(std::string(workspace) + "/tests/native_binary/testdata.txt");
44*bcb5dc79SHONG Yifan   FILE *f = fopen(path.c_str(), "rt");
45*bcb5dc79SHONG Yifan   if (!f) {
46*bcb5dc79SHONG Yifan     fprintf(stderr, "ERROR(" __FILE__ ":%d): Could not find runfile '%s'\n",
47*bcb5dc79SHONG Yifan             __LINE__, path.c_str());
48*bcb5dc79SHONG Yifan   }
49*bcb5dc79SHONG Yifan 
50*bcb5dc79SHONG Yifan   char buf[6];
51*bcb5dc79SHONG Yifan   size_t s = fread(buf, 1, 5, f);
52*bcb5dc79SHONG Yifan   fclose(f);
53*bcb5dc79SHONG Yifan   buf[5] = 0;
54*bcb5dc79SHONG Yifan   if (s != 5 || std::string("hello") != std::string(buf, 5)) {
55*bcb5dc79SHONG Yifan     fprintf(stderr, "ERROR(" __FILE__ ":%d): bad runfile contents (%s)\n",
56*bcb5dc79SHONG Yifan             __LINE__, buf);
57*bcb5dc79SHONG Yifan     return 1;
58*bcb5dc79SHONG Yifan   }
59*bcb5dc79SHONG Yifan 
60*bcb5dc79SHONG Yifan   return 0;
61*bcb5dc79SHONG Yifan }
62