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
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <grpc/grpc.h>
23 #include <grpc/slice.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26
27 #include "src/core/lib/gpr/string.h"
28 #include "src/core/lib/gpr/tmpfile.h"
29 #include "src/core/lib/iomgr/load_file.h"
30 #include "test/core/util/test_config.h"
31
32 #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
33
34 static const char prefix[] = "file_test";
35
test_load_empty_file(void)36 static void test_load_empty_file(void) {
37 FILE* tmp = nullptr;
38 grpc_slice slice;
39 grpc_slice slice_with_null_term;
40 grpc_error* error;
41 char* tmp_name;
42
43 LOG_TEST_NAME("test_load_empty_file");
44
45 tmp = gpr_tmpfile(prefix, &tmp_name);
46 GPR_ASSERT(tmp_name != nullptr);
47 GPR_ASSERT(tmp != nullptr);
48 fclose(tmp);
49
50 error = grpc_load_file(tmp_name, 0, &slice);
51 GPR_ASSERT(error == GRPC_ERROR_NONE);
52 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == 0);
53
54 error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
55 GPR_ASSERT(error == GRPC_ERROR_NONE);
56 GPR_ASSERT(GRPC_SLICE_LENGTH(slice_with_null_term) == 1);
57 GPR_ASSERT(GRPC_SLICE_START_PTR(slice_with_null_term)[0] == 0);
58
59 remove(tmp_name);
60 gpr_free(tmp_name);
61 grpc_slice_unref(slice);
62 grpc_slice_unref(slice_with_null_term);
63 }
64
test_load_failure(void)65 static void test_load_failure(void) {
66 FILE* tmp = nullptr;
67 grpc_slice slice;
68 grpc_error* error;
69 char* tmp_name;
70
71 LOG_TEST_NAME("test_load_failure");
72
73 tmp = gpr_tmpfile(prefix, &tmp_name);
74 GPR_ASSERT(tmp_name != nullptr);
75 GPR_ASSERT(tmp != nullptr);
76 fclose(tmp);
77 remove(tmp_name);
78
79 error = grpc_load_file(tmp_name, 0, &slice);
80 GPR_ASSERT(error != GRPC_ERROR_NONE);
81 GRPC_ERROR_UNREF(error);
82 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == 0);
83 gpr_free(tmp_name);
84 grpc_slice_unref(slice);
85 }
86
test_load_small_file(void)87 static void test_load_small_file(void) {
88 FILE* tmp = nullptr;
89 grpc_slice slice;
90 grpc_slice slice_with_null_term;
91 grpc_error* error;
92 char* tmp_name;
93 const char* blah = "blah";
94
95 LOG_TEST_NAME("test_load_small_file");
96
97 tmp = gpr_tmpfile(prefix, &tmp_name);
98 GPR_ASSERT(tmp_name != nullptr);
99 GPR_ASSERT(tmp != nullptr);
100 GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah));
101 fclose(tmp);
102
103 error = grpc_load_file(tmp_name, 0, &slice);
104 GPR_ASSERT(error == GRPC_ERROR_NONE);
105 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == strlen(blah));
106 GPR_ASSERT(!memcmp(GRPC_SLICE_START_PTR(slice), blah, strlen(blah)));
107
108 error = grpc_load_file(tmp_name, 1, &slice_with_null_term);
109 GPR_ASSERT(error == GRPC_ERROR_NONE);
110 GPR_ASSERT(GRPC_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1));
111 GPR_ASSERT(strcmp((const char*)GRPC_SLICE_START_PTR(slice_with_null_term),
112 blah) == 0);
113
114 remove(tmp_name);
115 gpr_free(tmp_name);
116 grpc_slice_unref(slice);
117 grpc_slice_unref(slice_with_null_term);
118 }
119
test_load_big_file(void)120 static void test_load_big_file(void) {
121 FILE* tmp = nullptr;
122 grpc_slice slice;
123 grpc_error* error;
124 char* tmp_name;
125 static const size_t buffer_size = 124631;
126 unsigned char* buffer = static_cast<unsigned char*>(gpr_malloc(buffer_size));
127 unsigned char* current;
128 size_t i;
129
130 LOG_TEST_NAME("test_load_big_file");
131
132 memset(buffer, 42, buffer_size);
133
134 tmp = gpr_tmpfile(prefix, &tmp_name);
135 GPR_ASSERT(tmp != nullptr);
136 GPR_ASSERT(tmp_name != nullptr);
137 GPR_ASSERT(fwrite(buffer, 1, buffer_size, tmp) == buffer_size);
138 fclose(tmp);
139
140 error = grpc_load_file(tmp_name, 0, &slice);
141 GPR_ASSERT(error == GRPC_ERROR_NONE);
142 GPR_ASSERT(GRPC_SLICE_LENGTH(slice) == buffer_size);
143 current = GRPC_SLICE_START_PTR(slice);
144 for (i = 0; i < buffer_size; i++) {
145 GPR_ASSERT(current[i] == 42);
146 }
147
148 remove(tmp_name);
149 gpr_free(tmp_name);
150 grpc_slice_unref(slice);
151 gpr_free(buffer);
152 }
153
main(int argc,char ** argv)154 int main(int argc, char** argv) {
155 grpc_test_init(argc, argv);
156 grpc_init();
157 test_load_empty_file();
158 test_load_failure();
159 test_load_small_file();
160 test_load_big_file();
161 grpc_shutdown();
162 return 0;
163 }
164