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