stat_test.cc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #include "src/core/lib/gprpp/stat.h"
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <gmock/gmock.h>
  20. #include <gtest/gtest.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/slice.h>
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include "src/core/lib/gpr/string.h"
  26. #include "src/core/lib/gpr/tmpfile.h"
  27. #include "src/core/lib/iomgr/load_file.h"
  28. #include "test/core/util/test_config.h"
  29. namespace grpc_core {
  30. namespace testing {
  31. namespace {
  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, &timestamp);
  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. time_t timestamp = 0;
  51. absl::Status status = GetFileModificationTime("/DOES_NOT_EXIST", &timestamp);
  52. EXPECT_EQ(status.code(), absl::StatusCode::kInternal);
  53. // Check the last modified date is not set.
  54. EXPECT_EQ(timestamp, 0);
  55. }
  56. } // namespace
  57. } // namespace testing
  58. } // namespace grpc_core
  59. int main(int argc, char** argv) {
  60. grpc::testing::TestEnvironment env(argc, argv);
  61. ::testing::InitGoogleTest(&argc, argv);
  62. return RUN_ALL_TESTS();
  63. }