| 1 |
// |
| 2 |
// Copyright 2020 gRPC authors. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
// |
| 16 |
|
| 17 |
#include "src/core/lib/gprpp/stat.h" |
| 18 |
|
| 19 |
#include <stdio.h> |
| 20 |
|
| 21 |
#include "gtest/gtest.h" |
| 22 |
|
| 23 |
#include <grpc/support/alloc.h> |
| 24 |
|
| 25 |
#include "src/core/lib/gpr/tmpfile.h" |
| 26 |
#include "test/core/util/test_config.h" |
| 27 |
|
| 28 |
namespace grpc_core { |
| 29 |
namespace testing { |
| 30 |
namespace { |
| 31 |
|
| 32 |
TEST(STAT, GetTimestampOnTmpFile) { |
| 33 |
// Create a temporary empty file. |
| 34 |
FILE* tmp = nullptr; |
| 35 |
char* tmp_name; |
| 36 |
tmp = gpr_tmpfile("prefix", &tmp_name); |
| 37 |
ASSERT_NE(tmp_name, nullptr); |
| 38 |
ASSERT_NE(tmp, nullptr); |
| 39 |
fclose(tmp); |
| 40 |
// Check the last modified date is correctly set. |
| 41 |
time_t timestamp = 0; |
| 42 |
absl::Status status = GetFileModificationTime(tmp_name, ×tamp); |
| 43 |
EXPECT_EQ(status.code(), absl::StatusCode::kOk); |
| 44 |
EXPECT_GT(timestamp, 0); |
| 45 |
// Clean up. |
| 46 |
remove(tmp_name); |
| 47 |
gpr_free(tmp_name); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(STAT, GetTimestampOnFailure) { |
| 51 |
time_t timestamp = 0; |
| 52 |
absl::Status status = GetFileModificationTime("/DOES_NOT_EXIST", ×tamp); |
| 53 |
EXPECT_EQ(status.code(), absl::StatusCode::kInternal); |
| 54 |
// Check the last modified date is not set. |
| 55 |
EXPECT_EQ(timestamp, 0); |
| 56 |
} |
| 57 |
|
| 58 |
} // namespace |
| 59 |
} // namespace testing |
| 60 |
} // namespace grpc_core |
| 61 |
|
| 62 |
int main(int argc, char** argv) { |
| 63 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 64 |
::testing::InitGoogleTest(&argc, argv); |
| 65 |
return RUN_ALL_TESTS(); |
| 66 |
} |