| 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 "src/core/lib/iomgr/load_file.h" |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include <gtest/gtest.h> |
| 25 |
|
| 26 |
#include <grpc/grpc.h> |
| 27 |
#include <grpc/slice.h> |
| 28 |
#include <grpc/support/alloc.h> |
| 29 |
#include <grpc/support/log.h> |
| 30 |
|
| 31 |
#include "src/core/lib/gpr/string.h" |
| 32 |
#include "src/core/lib/gpr/tmpfile.h" |
| 33 |
#include "test/core/util/test_config.h" |
| 34 |
|
| 35 |
#define LOG_TEST_NAME(x) gpr_log(GPR_INFO, "%s", x) |
| 36 |
|
| 37 |
static const char prefix[] = "file_test"; |
| 38 |
|
| 39 |
TEST(LoadFileTest, TestLoadEmptyFile) { |
| 40 |
FILE* tmp = nullptr; |
| 41 |
grpc_slice slice; |
| 42 |
grpc_slice slice_with_null_term; |
| 43 |
grpc_error_handle error; |
| 44 |
char* tmp_name; |
| 45 |
|
| 46 |
LOG_TEST_NAME("test_load_empty_file"); |
| 47 |
|
| 48 |
tmp = gpr_tmpfile(prefix, &tmp_name); |
| 49 |
ASSERT_NE(tmp_name, nullptr); |
| 50 |
ASSERT_NE(tmp, nullptr); |
| 51 |
fclose(tmp); |
| 52 |
|
| 53 |
error = grpc_load_file(tmp_name, 0, &slice); |
| 54 |
ASSERT_TRUE(error.ok()); |
| 55 |
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), 0); |
| 56 |
|
| 57 |
error = grpc_load_file(tmp_name, 1, &slice_with_null_term); |
| 58 |
ASSERT_TRUE(error.ok()); |
| 59 |
ASSERT_EQ(GRPC_SLICE_LENGTH(slice_with_null_term), 1); |
| 60 |
ASSERT_EQ(GRPC_SLICE_START_PTR(slice_with_null_term)[0], 0); |
| 61 |
|
| 62 |
remove(tmp_name); |
| 63 |
gpr_free(tmp_name); |
| 64 |
grpc_slice_unref(slice); |
| 65 |
grpc_slice_unref(slice_with_null_term); |
| 66 |
} |
| 67 |
|
| 68 |
TEST(LoadFileTest, TestLoadFailure) { |
| 69 |
FILE* tmp = nullptr; |
| 70 |
grpc_slice slice; |
| 71 |
grpc_error_handle error; |
| 72 |
char* tmp_name; |
| 73 |
|
| 74 |
LOG_TEST_NAME("test_load_failure"); |
| 75 |
|
| 76 |
tmp = gpr_tmpfile(prefix, &tmp_name); |
| 77 |
ASSERT_NE(tmp_name, nullptr); |
| 78 |
ASSERT_NE(tmp, nullptr); |
| 79 |
fclose(tmp); |
| 80 |
remove(tmp_name); |
| 81 |
|
| 82 |
error = grpc_load_file(tmp_name, 0, &slice); |
| 83 |
ASSERT_FALSE(error.ok()); |
| 84 |
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), 0); |
| 85 |
gpr_free(tmp_name); |
| 86 |
grpc_slice_unref(slice); |
| 87 |
} |
| 88 |
|
| 89 |
TEST(LoadFileTest, TestLoadSmallFile) { |
| 90 |
FILE* tmp = nullptr; |
| 91 |
grpc_slice slice; |
| 92 |
grpc_slice slice_with_null_term; |
| 93 |
grpc_error_handle error; |
| 94 |
char* tmp_name; |
| 95 |
const char* blah = "blah"; |
| 96 |
|
| 97 |
LOG_TEST_NAME("test_load_small_file"); |
| 98 |
|
| 99 |
tmp = gpr_tmpfile(prefix, &tmp_name); |
| 100 |
ASSERT_NE(tmp_name, nullptr); |
| 101 |
ASSERT_NE(tmp, nullptr); |
| 102 |
ASSERT_EQ(fwrite(blah, 1, strlen(blah), tmp), strlen(blah)); |
| 103 |
fclose(tmp); |
| 104 |
|
| 105 |
error = grpc_load_file(tmp_name, 0, &slice); |
| 106 |
ASSERT_TRUE(error.ok()); |
| 107 |
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), strlen(blah)); |
| 108 |
ASSERT_FALSE(memcmp(GRPC_SLICE_START_PTR(slice), blah, strlen(blah))); |
| 109 |
|
| 110 |
error = grpc_load_file(tmp_name, 1, &slice_with_null_term); |
| 111 |
ASSERT_TRUE(error.ok()); |
| 112 |
ASSERT_EQ(GRPC_SLICE_LENGTH(slice_with_null_term), (strlen(blah) + 1)); |
| 113 |
ASSERT_STREQ((const char*)GRPC_SLICE_START_PTR(slice_with_null_term), blah); |
| 114 |
|
| 115 |
remove(tmp_name); |
| 116 |
gpr_free(tmp_name); |
| 117 |
grpc_slice_unref(slice); |
| 118 |
grpc_slice_unref(slice_with_null_term); |
| 119 |
} |
| 120 |
|
| 121 |
TEST(LoadFileTest, TestLoadBigFile) { |
| 122 |
FILE* tmp = nullptr; |
| 123 |
grpc_slice slice; |
| 124 |
grpc_error_handle error; |
| 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 |
ASSERT_NE(tmp, nullptr); |
| 137 |
ASSERT_NE(tmp_name, nullptr); |
| 138 |
ASSERT_EQ(fwrite(buffer, 1, buffer_size, tmp), buffer_size); |
| 139 |
fclose(tmp); |
| 140 |
|
| 141 |
error = grpc_load_file(tmp_name, 0, &slice); |
| 142 |
ASSERT_TRUE(error.ok()); |
| 143 |
ASSERT_EQ(GRPC_SLICE_LENGTH(slice), buffer_size); |
| 144 |
current = GRPC_SLICE_START_PTR(slice); |
| 145 |
for (i = 0; i < buffer_size; i++) { |
| 146 |
ASSERT_EQ(current[i], 42); |
| 147 |
} |
| 148 |
|
| 149 |
remove(tmp_name); |
| 150 |
gpr_free(tmp_name); |
| 151 |
grpc_slice_unref(slice); |
| 152 |
gpr_free(buffer); |
| 153 |
} |
| 154 |
|
| 155 |
int main(int argc, char** argv) { |
| 156 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 157 |
::testing::InitGoogleTest(&argc, argv); |
| 158 |
grpc::testing::TestGrpcScope grpc_scope; |
| 159 |
return RUN_ALL_TESTS(); |
| 160 |
} |