You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

170 lines
6.2 KiB

  1. #include <config.h>
  2. #include <apt-pkg/error.h>
  3. #include <stddef.h>
  4. #include <string>
  5. #include <errno.h>
  6. #include <string.h>
  7. #include <gtest/gtest.h>
  8. TEST(GlobalErrorTest,BasicDiscard)
  9. {
  10. GlobalError e;
  11. EXPECT_TRUE(e.empty());
  12. EXPECT_FALSE(e.PendingError());
  13. EXPECT_FALSE(e.Notice("%s Notice", "A"));
  14. EXPECT_TRUE(e.empty());
  15. EXPECT_FALSE(e.empty(GlobalError::DEBUG));
  16. EXPECT_FALSE(e.PendingError());
  17. EXPECT_FALSE(e.Error("%s horrible %s %d times", "Something", "happened", 2));
  18. EXPECT_TRUE(e.PendingError());
  19. std::string text;
  20. EXPECT_FALSE(e.PopMessage(text));
  21. EXPECT_TRUE(e.PendingError());
  22. EXPECT_EQ("A Notice", text);
  23. EXPECT_TRUE(e.PopMessage(text));
  24. EXPECT_EQ("Something horrible happened 2 times", text);
  25. EXPECT_TRUE(e.empty(GlobalError::DEBUG));
  26. EXPECT_FALSE(e.PendingError());
  27. EXPECT_FALSE(e.Error("%s horrible %s %d times", "Something", "happened", 2));
  28. EXPECT_TRUE(e.PendingError());
  29. EXPECT_FALSE(e.empty(GlobalError::FATAL));
  30. e.Discard();
  31. EXPECT_TRUE(e.empty());
  32. EXPECT_FALSE(e.PendingError());
  33. }
  34. TEST(GlobalErrorTest,StackPushing)
  35. {
  36. GlobalError e;
  37. EXPECT_FALSE(e.Notice("%s Notice", "A"));
  38. EXPECT_FALSE(e.Error("%s horrible %s %d times", "Something", "happened", 2));
  39. EXPECT_TRUE(e.PendingError());
  40. EXPECT_FALSE(e.empty(GlobalError::NOTICE));
  41. e.PushToStack();
  42. EXPECT_TRUE(e.empty(GlobalError::NOTICE));
  43. EXPECT_FALSE(e.PendingError());
  44. EXPECT_FALSE(e.Warning("%s Warning", "A"));
  45. EXPECT_TRUE(e.empty(GlobalError::ERROR));
  46. EXPECT_FALSE(e.PendingError());
  47. e.RevertToStack();
  48. EXPECT_FALSE(e.empty(GlobalError::ERROR));
  49. EXPECT_TRUE(e.PendingError());
  50. std::string text;
  51. EXPECT_FALSE(e.PopMessage(text));
  52. EXPECT_TRUE(e.PendingError());
  53. EXPECT_EQ("A Notice", text);
  54. EXPECT_TRUE(e.PopMessage(text));
  55. EXPECT_EQ("Something horrible happened 2 times", text);
  56. EXPECT_FALSE(e.PendingError());
  57. EXPECT_TRUE(e.empty());
  58. EXPECT_FALSE(e.Notice("%s Notice", "A"));
  59. EXPECT_FALSE(e.Error("%s horrible %s %d times", "Something", "happened", 2));
  60. EXPECT_TRUE(e.PendingError());
  61. EXPECT_FALSE(e.empty(GlobalError::NOTICE));
  62. e.PushToStack();
  63. EXPECT_TRUE(e.empty(GlobalError::NOTICE));
  64. EXPECT_FALSE(e.PendingError());
  65. EXPECT_FALSE(e.Warning("%s Warning", "A"));
  66. EXPECT_TRUE(e.empty(GlobalError::ERROR));
  67. EXPECT_FALSE(e.PendingError());
  68. e.MergeWithStack();
  69. EXPECT_FALSE(e.empty(GlobalError::ERROR));
  70. EXPECT_TRUE(e.PendingError());
  71. EXPECT_FALSE(e.PopMessage(text));
  72. EXPECT_TRUE(e.PendingError());
  73. EXPECT_EQ("A Notice", text);
  74. EXPECT_TRUE(e.PopMessage(text));
  75. EXPECT_EQ("Something horrible happened 2 times", text);
  76. EXPECT_FALSE(e.PendingError());
  77. EXPECT_FALSE(e.empty());
  78. EXPECT_FALSE(e.PopMessage(text));
  79. EXPECT_EQ("A Warning", text);
  80. EXPECT_TRUE(e.empty());
  81. }
  82. TEST(GlobalErrorTest,Errno)
  83. {
  84. GlobalError e;
  85. std::string const textOfErrnoZero(strerror(0));
  86. errno = 0;
  87. EXPECT_FALSE(e.Errno("errno", "%s horrible %s %d times", "Something", "happened", 2));
  88. EXPECT_FALSE(e.empty());
  89. EXPECT_TRUE(e.PendingError());
  90. std::string text;
  91. EXPECT_TRUE(e.PopMessage(text));
  92. EXPECT_FALSE(e.PendingError());
  93. EXPECT_EQ(std::string("Something horrible happened 2 times - errno (0: ").append(textOfErrnoZero).append(")"), text);
  94. EXPECT_TRUE(e.empty());
  95. }
  96. TEST(GlobalErrorTest,LongMessage)
  97. {
  98. GlobalError e;
  99. std::string const textOfErrnoZero(strerror(0));
  100. errno = 0;
  101. std::string text, longText;
  102. for (size_t i = 0; i < 500; ++i)
  103. longText.append("a");
  104. EXPECT_FALSE(e.Error("%s horrible %s %d times", longText.c_str(), "happened", 2));
  105. EXPECT_TRUE(e.PopMessage(text));
  106. EXPECT_EQ(std::string(longText).append(" horrible happened 2 times"), text);
  107. EXPECT_FALSE(e.Errno("errno", "%s horrible %s %d times", longText.c_str(), "happened", 2));
  108. EXPECT_TRUE(e.PopMessage(text));
  109. EXPECT_EQ(std::string(longText).append(" horrible happened 2 times - errno (0: ").append(textOfErrnoZero).append(")"), text);
  110. EXPECT_FALSE(e.Error("%s horrible %s %d times", longText.c_str(), "happened", 2));
  111. std::ostringstream out;
  112. e.DumpErrors(out);
  113. EXPECT_EQ(std::string("E: ").append(longText).append(" horrible happened 2 times\n"), out.str());
  114. EXPECT_FALSE(e.Errno("errno", "%s horrible %s %d times", longText.c_str(), "happened", 2));
  115. std::ostringstream out2;
  116. e.DumpErrors(out2);
  117. EXPECT_EQ(std::string("E: ").append(longText).append(" horrible happened 2 times - errno (0: ").append(textOfErrnoZero).append(")\n"), out2.str());
  118. }
  119. TEST(GlobalErrorTest,UTF8Message)
  120. {
  121. GlobalError e;
  122. std::string text;
  123. EXPECT_FALSE(e.Warning("Репозиторий не обновлён и будут %d %s", 4, "test"));
  124. EXPECT_FALSE(e.PopMessage(text));
  125. EXPECT_EQ("Репозиторий не обновлён и будут 4 test", text);
  126. EXPECT_FALSE(e.Warning("Репозиторий не обновлён и будут %d %s", 4, "test"));
  127. std::ostringstream out;
  128. e.DumpErrors(out);
  129. EXPECT_EQ("W: Репозиторий не обновлён и будут 4 test\n", out.str());
  130. std::string longText;
  131. for (size_t i = 0; i < 50; ++i)
  132. longText.append("РезийбёбAZ");
  133. EXPECT_FALSE(e.Warning("%s", longText.c_str()));
  134. EXPECT_FALSE(e.PopMessage(text));
  135. EXPECT_EQ(longText, text);
  136. }
  137. TEST(GlobalErrorTest,MultiLineMessage)
  138. {
  139. GlobalError e;
  140. std::string text;
  141. EXPECT_FALSE(e.Warning("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\n%s?\rGood because I don't.", "Right"));
  142. EXPECT_FALSE(e.PopMessage(text));
  143. EXPECT_EQ("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\nRight?\rGood because I don't.", text);
  144. EXPECT_FALSE(e.Warning("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\n%s?\rGood because I don't.", "Right"));
  145. std::ostringstream out;
  146. e.DumpErrors(out);
  147. EXPECT_EQ("W: Sometimes one line isn't enough.\n You do know what I mean, right?\n Right?\n Good because I don't.\n", out.str());
  148. EXPECT_FALSE(e.Warning("Sometimes one line isn't enough.\nYou do know what I mean, right?\r\n%s?\rGood because I don't.\n", "Right"));
  149. std::ostringstream out2;
  150. e.DumpErrors(out2);
  151. EXPECT_EQ("W: Sometimes one line isn't enough.\n You do know what I mean, right?\n Right?\n Good because I don't.\n", out2.str());
  152. }