Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include <stdio.h>
35 : #include <string.h>
36 :
37 : #include <grpc/support/alloc.h>
38 : #include <grpc/support/log.h>
39 : #include <grpc/support/slice.h>
40 :
41 : #include "src/core/support/file.h"
42 : #include "src/core/support/string.h"
43 : #include "test/core/util/test_config.h"
44 :
45 : #define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x)
46 :
47 : static const char prefix[] = "file_test";
48 :
49 1 : static void test_load_empty_file(void) {
50 1 : FILE *tmp = NULL;
51 : gpr_slice slice;
52 : gpr_slice slice_with_null_term;
53 : int success;
54 : char *tmp_name;
55 :
56 1 : LOG_TEST_NAME("test_load_empty_file");
57 :
58 1 : tmp = gpr_tmpfile(prefix, &tmp_name);
59 1 : GPR_ASSERT(tmp_name != NULL);
60 1 : GPR_ASSERT(tmp != NULL);
61 1 : fclose(tmp);
62 :
63 1 : slice = gpr_load_file(tmp_name, 0, &success);
64 1 : GPR_ASSERT(success == 1);
65 1 : GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0);
66 :
67 1 : slice_with_null_term = gpr_load_file(tmp_name, 1, &success);
68 1 : GPR_ASSERT(success == 1);
69 1 : GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == 1);
70 1 : GPR_ASSERT(GPR_SLICE_START_PTR(slice_with_null_term)[0] == 0);
71 :
72 1 : remove(tmp_name);
73 1 : gpr_free(tmp_name);
74 1 : gpr_slice_unref(slice);
75 1 : gpr_slice_unref(slice_with_null_term);
76 1 : }
77 :
78 1 : static void test_load_failure(void) {
79 1 : FILE *tmp = NULL;
80 : gpr_slice slice;
81 : int success;
82 : char *tmp_name;
83 :
84 1 : LOG_TEST_NAME("test_load_failure");
85 :
86 1 : tmp = gpr_tmpfile(prefix, &tmp_name);
87 1 : GPR_ASSERT(tmp_name != NULL);
88 1 : GPR_ASSERT(tmp != NULL);
89 1 : fclose(tmp);
90 1 : remove(tmp_name);
91 :
92 1 : slice = gpr_load_file(tmp_name, 0, &success);
93 1 : GPR_ASSERT(success == 0);
94 1 : GPR_ASSERT(GPR_SLICE_LENGTH(slice) == 0);
95 1 : gpr_free(tmp_name);
96 1 : gpr_slice_unref(slice);
97 1 : }
98 :
99 1 : static void test_load_small_file(void) {
100 1 : FILE *tmp = NULL;
101 : gpr_slice slice;
102 : gpr_slice slice_with_null_term;
103 : int success;
104 : char *tmp_name;
105 1 : const char *blah = "blah";
106 :
107 1 : LOG_TEST_NAME("test_load_small_file");
108 :
109 1 : tmp = gpr_tmpfile(prefix, &tmp_name);
110 1 : GPR_ASSERT(tmp_name != NULL);
111 1 : GPR_ASSERT(tmp != NULL);
112 1 : GPR_ASSERT(fwrite(blah, 1, strlen(blah), tmp) == strlen(blah));
113 1 : fclose(tmp);
114 :
115 1 : slice = gpr_load_file(tmp_name, 0, &success);
116 1 : GPR_ASSERT(success == 1);
117 1 : GPR_ASSERT(GPR_SLICE_LENGTH(slice) == strlen(blah));
118 1 : GPR_ASSERT(!memcmp(GPR_SLICE_START_PTR(slice), blah, strlen(blah)));
119 :
120 1 : slice_with_null_term = gpr_load_file(tmp_name, 1, &success);
121 1 : GPR_ASSERT(success == 1);
122 1 : GPR_ASSERT(GPR_SLICE_LENGTH(slice_with_null_term) == (strlen(blah) + 1));
123 1 : GPR_ASSERT(strcmp((const char *)GPR_SLICE_START_PTR(slice_with_null_term),
124 : blah) == 0);
125 :
126 1 : remove(tmp_name);
127 1 : gpr_free(tmp_name);
128 1 : gpr_slice_unref(slice);
129 1 : gpr_slice_unref(slice_with_null_term);
130 1 : }
131 :
132 1 : static void test_load_big_file(void) {
133 1 : FILE *tmp = NULL;
134 : gpr_slice slice;
135 : int success;
136 : char *tmp_name;
137 : unsigned char buffer[124631];
138 : unsigned char *current;
139 : size_t i;
140 :
141 1 : LOG_TEST_NAME("test_load_big_file");
142 :
143 124632 : for (i = 0; i < sizeof(buffer); i++) {
144 124631 : buffer[i] = 42;
145 : }
146 :
147 1 : tmp = gpr_tmpfile(prefix, &tmp_name);
148 1 : GPR_ASSERT(tmp != NULL);
149 1 : GPR_ASSERT(tmp_name != NULL);
150 1 : GPR_ASSERT(fwrite(buffer, 1, sizeof(buffer), tmp) == sizeof(buffer));
151 1 : fclose(tmp);
152 :
153 1 : slice = gpr_load_file(tmp_name, 0, &success);
154 1 : GPR_ASSERT(success == 1);
155 1 : GPR_ASSERT(GPR_SLICE_LENGTH(slice) == sizeof(buffer));
156 1 : current = GPR_SLICE_START_PTR(slice);
157 124632 : for (i = 0; i < sizeof(buffer); i++) {
158 124631 : GPR_ASSERT(current[i] == 42);
159 : }
160 :
161 1 : remove(tmp_name);
162 1 : gpr_free(tmp_name);
163 1 : gpr_slice_unref(slice);
164 1 : }
165 :
166 1 : int main(int argc, char **argv) {
167 1 : grpc_test_init(argc, argv);
168 1 : test_load_empty_file();
169 1 : test_load_failure();
170 1 : test_load_small_file();
171 1 : test_load_big_file();
172 1 : return 0;
173 : }
|