• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 gRPC authors.
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 #include "src/core/util/load_file.h"
16 
17 #include <grpc/support/alloc.h>
18 #include <stdio.h>
19 #include <string.h>
20 
21 #include <cstdint>
22 
23 #include "gtest/gtest.h"
24 #include "src/core/util/tmpfile.h"
25 #include "test/core/test_util/test_config.h"
26 
27 static const char prefix[] = "file_test";
28 
TEST(LoadFileTest,TestLoadEmptyFile)29 TEST(LoadFileTest, TestLoadEmptyFile) {
30   FILE* tmp = nullptr;
31   absl::StatusOr<grpc_core::Slice> result;
32   char* tmp_name;
33 
34   tmp = gpr_tmpfile(prefix, &tmp_name);
35   ASSERT_NE(tmp_name, nullptr);
36   ASSERT_NE(tmp, nullptr);
37   fclose(tmp);
38 
39   result = grpc_core::LoadFile(tmp_name, false);
40   ASSERT_TRUE(result.ok());
41   ASSERT_EQ(result->length(), 0);
42 
43   result = grpc_core::LoadFile(tmp_name, true);
44   ASSERT_TRUE(result.ok());
45   ASSERT_EQ(result->length(), 1);
46   ASSERT_EQ(result->begin()[0], 0);
47 
48   remove(tmp_name);
49   gpr_free(tmp_name);
50 }
51 
TEST(LoadFileTest,TestLoadFailure)52 TEST(LoadFileTest, TestLoadFailure) {
53   FILE* tmp = nullptr;
54   absl::StatusOr<grpc_core::Slice> result;
55   char* tmp_name;
56 
57   tmp = gpr_tmpfile(prefix, &tmp_name);
58   ASSERT_NE(tmp_name, nullptr);
59   ASSERT_NE(tmp, nullptr);
60   fclose(tmp);
61   remove(tmp_name);
62 
63   result = grpc_core::LoadFile(tmp_name, false);
64   ASSERT_FALSE(result.ok());
65 
66   gpr_free(tmp_name);
67 }
68 
TEST(LoadFileTest,TestLoadSmallFile)69 TEST(LoadFileTest, TestLoadSmallFile) {
70   FILE* tmp = nullptr;
71   absl::StatusOr<grpc_core::Slice> result;
72   char* tmp_name;
73   const char* blah = "blah";
74 
75   tmp = gpr_tmpfile(prefix, &tmp_name);
76   ASSERT_NE(tmp_name, nullptr);
77   ASSERT_NE(tmp, nullptr);
78   ASSERT_EQ(fwrite(blah, 1, strlen(blah), tmp), strlen(blah));
79   fclose(tmp);
80 
81   result = grpc_core::LoadFile(tmp_name, false);
82   ASSERT_TRUE(result.ok());
83   ASSERT_EQ(result->length(), strlen(blah));
84   ASSERT_FALSE(memcmp(result->begin(), blah, strlen(blah)));
85 
86   result = grpc_core::LoadFile(tmp_name, true);
87   ASSERT_TRUE(result.ok());
88   ASSERT_EQ(result->length(), strlen(blah) + 1);
89   ASSERT_STREQ(reinterpret_cast<const char*>(result->begin()), blah);
90 
91   remove(tmp_name);
92   gpr_free(tmp_name);
93 }
94 
TEST(LoadFileTest,TestLoadBigFile)95 TEST(LoadFileTest, TestLoadBigFile) {
96   FILE* tmp = nullptr;
97   absl::StatusOr<grpc_core::Slice> result;
98   char* tmp_name;
99   static const size_t buffer_size = 124631;
100   unsigned char* buffer = static_cast<unsigned char*>(gpr_malloc(buffer_size));
101   const uint8_t* current;
102   size_t i;
103 
104   memset(buffer, 42, buffer_size);
105 
106   tmp = gpr_tmpfile(prefix, &tmp_name);
107   ASSERT_NE(tmp, nullptr);
108   ASSERT_NE(tmp_name, nullptr);
109   ASSERT_EQ(fwrite(buffer, 1, buffer_size, tmp), buffer_size);
110   fclose(tmp);
111 
112   result = grpc_core::LoadFile(tmp_name, false);
113   ASSERT_TRUE(result.ok());
114   ASSERT_EQ(result->length(), buffer_size);
115   current = result->begin();
116   for (i = 0; i < buffer_size; i++) {
117     ASSERT_EQ(current[i], 42);
118   }
119 
120   remove(tmp_name);
121   gpr_free(tmp_name);
122   gpr_free(buffer);
123 }
124 
main(int argc,char ** argv)125 int main(int argc, char** argv) {
126   grpc::testing::TestEnvironment env(&argc, argv);
127   ::testing::InitGoogleTest(&argc, argv);
128   grpc::testing::TestGrpcScope grpc_scope;
129   return RUN_ALL_TESTS();
130 }
131