xref: /aosp_15_r20/external/grpc-grpc/test/core/gprpp/stat_test.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 <memory>
22 
23 #include "gtest/gtest.h"
24 
25 #include <grpc/support/alloc.h>
26 
27 #include "src/core/lib/gpr/tmpfile.h"
28 #include "test/core/util/test_config.h"
29 
30 namespace grpc_core {
31 namespace testing {
32 namespace {
33 
TEST(STAT,GetTimestampOnTmpFile)34 TEST(STAT, GetTimestampOnTmpFile) {
35   // Create a temporary empty file.
36   FILE* tmp = nullptr;
37   char* tmp_name;
38   tmp = gpr_tmpfile("prefix", &tmp_name);
39   ASSERT_NE(tmp_name, nullptr);
40   ASSERT_NE(tmp, nullptr);
41   fclose(tmp);
42   // Check the last modified date is correctly set.
43   time_t timestamp = 0;
44   absl::Status status = GetFileModificationTime(tmp_name, &timestamp);
45   EXPECT_EQ(status.code(), absl::StatusCode::kOk);
46   EXPECT_GT(timestamp, 0);
47   // Clean up.
48   remove(tmp_name);
49   gpr_free(tmp_name);
50 }
51 
TEST(STAT,GetTimestampOnFailure)52 TEST(STAT, GetTimestampOnFailure) {
53   time_t timestamp = 0;
54   absl::Status status = GetFileModificationTime("/DOES_NOT_EXIST", &timestamp);
55   EXPECT_EQ(status.code(), absl::StatusCode::kInternal);
56   // Check the last modified date is not set.
57   EXPECT_EQ(timestamp, 0);
58 }
59 
60 }  // namespace
61 }  // namespace testing
62 }  // namespace grpc_core
63 
main(int argc,char ** argv)64 int main(int argc, char** argv) {
65   grpc::testing::TestEnvironment env(&argc, argv);
66   ::testing::InitGoogleTest(&argc, argv);
67   return RUN_ALL_TESTS();
68 }
69