temp_file_unittest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #include <gtest/gtest.h>
  18. #include <errno.h> // errno
  19. #include "butil/files/temp_file.h"
  20. namespace {
  21. class TempFileTest : public ::testing::Test{
  22. protected:
  23. TempFileTest(){};
  24. virtual ~TempFileTest(){};
  25. virtual void SetUp() {
  26. };
  27. virtual void TearDown() {
  28. };
  29. };
  30. TEST_F(TempFileTest, should_create_tmp_file)
  31. {
  32. butil::TempFile tmp;
  33. struct stat st;
  34. //check if existed
  35. ASSERT_EQ(0, stat(tmp.fname(), &st));
  36. }
  37. TEST_F(TempFileTest, should_write_string)
  38. {
  39. butil::TempFile tmp;
  40. const char *exp = "a test file";
  41. ASSERT_EQ(0, tmp.save(exp));
  42. FILE *fp = fopen(tmp.fname(), "r");
  43. ASSERT_NE((void*)0, fp);
  44. char buf[1024];
  45. char *act = fgets(buf, 1024, fp);
  46. fclose(fp);
  47. EXPECT_STREQ(exp, act);
  48. }
  49. TEST_F(TempFileTest, temp_with_specific_ext)
  50. {
  51. butil::TempFile tmp("blah");
  52. const char *exp = "a test file";
  53. ASSERT_EQ(0, tmp.save(exp));
  54. struct stat st;
  55. ASSERT_EQ(0, stat(tmp.fname(), &st));
  56. ASSERT_STREQ(".blah", strrchr(tmp.fname(), '.'));
  57. FILE *fp = fopen(tmp.fname(), "r");
  58. ASSERT_NE((void*)0, fp) << strerror(errno);
  59. char buf[1024];
  60. char *act = fgets(buf, 1024, fp);
  61. fclose(fp);
  62. EXPECT_STREQ(exp, act);
  63. }
  64. TEST_F(TempFileTest, should_delete_when_exit)
  65. {
  66. std::string fname;
  67. struct stat st;
  68. {
  69. butil::TempFile tmp;
  70. //check if existed
  71. ASSERT_EQ(0, stat(tmp.fname(), &st));
  72. fname = tmp.fname();
  73. }
  74. //check if existed
  75. ASSERT_EQ(-1, stat(fname.c_str(), &st));
  76. ASSERT_EQ(ENOENT, errno);
  77. }
  78. TEST_F(TempFileTest, should_save_with_format)
  79. {
  80. butil::TempFile tmp;
  81. tmp.save_format("%s%d%ld%s", "justmp", 1, 98L, "hello world");
  82. FILE *fp = fopen(tmp.fname(), "r");
  83. ASSERT_NE((void*)0, fp);
  84. char buf[1024];
  85. char *act = fgets(buf, 1024, fp);
  86. fclose(fp);
  87. EXPECT_STREQ("justmp198hello world", act);
  88. }
  89. TEST_F(TempFileTest, should_save_with_format_in_long_string)
  90. {
  91. char buf[2048];
  92. memset(buf, 'a', sizeof(buf));
  93. buf[2047] = '\0';
  94. butil::TempFile tmp;
  95. tmp.save_format("%s", buf);
  96. FILE *fp = fopen(tmp.fname(), "r");
  97. ASSERT_NE((void*)0, fp);
  98. char buf2[2048];
  99. char *act = fgets(buf2, 2048, fp);
  100. fclose(fp);
  101. EXPECT_STREQ(buf, act);
  102. }
  103. struct test_t {
  104. int a;
  105. int b;
  106. char c[4];
  107. };
  108. TEST_F(TempFileTest, save_binary_twice)
  109. {
  110. test_t data = {12, -34, {'B', 'E', 'E', 'F'}};
  111. butil::TempFile tmp;
  112. ASSERT_EQ(0, tmp.save_bin(&data, sizeof(data)));
  113. FILE *fp = fopen(tmp.fname(), "r");
  114. ASSERT_NE((void*)0, fp);
  115. test_t act_data;
  116. bzero(&act_data, sizeof(act_data));
  117. ASSERT_EQ((size_t)1, fread(&act_data, sizeof(act_data), 1, fp));
  118. fclose(fp);
  119. ASSERT_EQ(0, memcmp(&data, &act_data, sizeof(data)));
  120. // save twice
  121. test_t data2 = { 89, 1000, {'E', 'C', 'A', 'Z'}};
  122. ASSERT_EQ(0, tmp.save_bin(&data2, sizeof(data2)));
  123. fp = fopen(tmp.fname(), "r");
  124. ASSERT_NE((void*)0, fp);
  125. bzero(&act_data, sizeof(act_data));
  126. ASSERT_EQ((size_t)1, fread(&act_data, sizeof(act_data), 1, fp));
  127. fclose(fp);
  128. ASSERT_EQ(0, memcmp(&data2, &act_data, sizeof(data2)));
  129. }
  130. }