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