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/util/stat.h"
18
19 #include <grpc/support/alloc.h>
20 #include <stdio.h>
21
22 #include <memory>
23
24 #include "gtest/gtest.h"
25 #include "src/core/util/tmpfile.h"
26 #include "test/core/test_util/test_config.h"
27
28 namespace grpc_core {
29 namespace testing {
30 namespace {
31
TEST(STAT,GetTimestampOnTmpFile)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
TEST(STAT,GetTimestampOnFailure)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
main(int argc,char ** argv)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 }
67