load_file_test.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <grpc/grpc.h>
25 #include <grpc/slice.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 
32 
33 #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
34 
35 static const char prefix[] = "file_test";
36 
37 static void test_load_empty_file(void) {
38  FILE* tmp = nullptr;
40  grpc_slice slice_with_null_term;
42  char* tmp_name;
43 
44  LOG_TEST_NAME("test_load_empty_file");
45 
46  tmp = gpr_tmpfile(prefix, &tmp_name);
47  GPR_ASSERT(tmp_name != nullptr);
48  GPR_ASSERT(tmp != nullptr);
49  fclose(tmp);
50 
51  error = grpc_load_file(tmp_name, 0, &slice);
54 
55  error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
57  GPR_ASSERT(GRPC_SLICE_LENGTH(slice_with_null_term) == 1);
58  GPR_ASSERT(GRPC_SLICE_START_PTR(slice_with_null_term)[0] == 0);
59 
60  remove(tmp_name);
61  gpr_free(tmp_name);
63  grpc_slice_unref(slice_with_null_term);
64 }
65 
66 static void test_load_failure(void) {
67  FILE* tmp = nullptr;
70  char* tmp_name;
71 
72  LOG_TEST_NAME("test_load_failure");
73 
74  tmp = gpr_tmpfile(prefix, &tmp_name);
75  GPR_ASSERT(tmp_name != nullptr);
76  GPR_ASSERT(tmp != nullptr);
77  fclose(tmp);
78  remove(tmp_name);
79 
80  error = grpc_load_file(tmp_name, 0, &slice);
84  gpr_free(tmp_name);
86 }
87 
88 static void test_load_small_file(void) {
89  FILE* tmp = nullptr;
91  grpc_slice slice_with_null_term;
93  char* tmp_name;
94  const char* blah = "blah";
95 
96  LOG_TEST_NAME("test_load_small_file");
97 
98  tmp = gpr_tmpfile(prefix, &tmp_name);
99  GPR_ASSERT(tmp_name != nullptr);
100  GPR_ASSERT(tmp != nullptr);
101  GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah));
102  fclose(tmp);
103 
104  error = grpc_load_file(tmp_name, 0, &slice);
106  GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == strlen(blah));
107  GPR_ASSERT(!memcmp(GRPC_SLICE_START_PTR(slice), blah, strlen(blah)));
108 
109  error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
111  GPR_ASSERT(GRPC_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1));
112  GPR_ASSERT(strcmp((const char*)GRPC_SLICE_START_PTR(slice_with_null_term),
113  blah) == 0);
114 
115  remove(tmp_name);
116  gpr_free(tmp_name);
118  grpc_slice_unref(slice_with_null_term);
119 }
120 
121 static void test_load_big_file(void) {
122  FILE* tmp = nullptr;
125  char* tmp_name;
126  static const size_t buffer_size = 124631;
127  unsigned char* buffer = static_cast<unsigned char*>(gpr_malloc(buffer_size));
128  unsigned char* current;
129  size_t i;
130 
131  LOG_TEST_NAME("test_load_big_file");
132 
133  memset(buffer, 42, buffer_size);
134 
135  tmp = gpr_tmpfile(prefix, &tmp_name);
136  GPR_ASSERT(tmp != nullptr);
137  GPR_ASSERT(tmp_name != nullptr);
138  GPR_ASSERT(fwrite(buffer, 1, buffer_size, tmp) == buffer_size);
139  fclose(tmp);
140 
141  error = grpc_load_file(tmp_name, 0, &slice);
144  current = GRPC_SLICE_START_PTR(slice);
145  for (i = 0; i < buffer_size; i++) {
146  GPR_ASSERT(current[i] == 42);
147  }
148 
149  remove(tmp_name);
150  gpr_free(tmp_name);
152  gpr_free(buffer);
153 }
154 
155 int main(int argc, char** argv) {
156  grpc::testing::TestEnvironment env(&argc, argv);
157  grpc_init();
162  grpc_shutdown();
163  return 0;
164 }
grpc_slice_unref
GPRAPI void grpc_slice_unref(grpc_slice s)
Definition: slice_api.cc:32
test_load_small_file
static void test_load_small_file(void)
Definition: load_file_test.cc:88
log.h
test_load_empty_file
static void test_load_empty_file(void)
Definition: load_file_test.cc:37
grpc_load_file
grpc_error_handle grpc_load_file(const char *filename, int add_null_terminator, grpc_slice *output)
Definition: load_file.cc:33
generate.env
env
Definition: generate.py:37
memset
return memset(p, 0, total)
load_file.h
slice.h
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
error
grpc_error_handle error
Definition: retry_filter.cc:499
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
gpr_tmpfile
FILE * gpr_tmpfile(const char *prefix, char **tmp_filename)
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
test_load_big_file
static void test_load_big_file(void)
Definition: load_file_test.cc:121
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc.h
tmpfile.h
test_load_failure
static void test_load_failure(void)
Definition: load_file_test.cc:66
GRPC_SLICE_START_PTR
#define GRPC_SLICE_START_PTR(slice)
Definition: include/grpc/impl/codegen/slice.h:101
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
prefix
static const char prefix[]
Definition: load_file_test.cc:35
LOG_TEST_NAME
#define LOG_TEST_NAME(x)
Definition: load_file_test.cc:33
GRPC_SLICE_LENGTH
#define GRPC_SLICE_LENGTH(slice)
Definition: include/grpc/impl/codegen/slice.h:104
test_config.h
benchmark.FILE
FILE
Definition: benchmark.py:21
grpc::fclose
fclose(creds_file)
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
main
int main(int argc, char **argv)
Definition: load_file_test.cc:155
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_error
Definition: error_internal.h:42
versiongenerate.buffer_size
int buffer_size
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/xcode/Scripts/versiongenerate.py:65
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GRPC_ERROR_IS_NONE
#define GRPC_ERROR_IS_NONE(err)
Definition: error.h:241


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:29