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.

242 lines
7.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2017 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "api/rtc_error.h"
  11. #include <utility>
  12. #include "test/gtest.h"
  13. namespace {
  14. const int kDefaultMoveOnlyIntValue = 0xbadf00d;
  15. // Class that has no copy constructor, ensuring that RTCErrorOr can
  16. struct MoveOnlyInt {
  17. MoveOnlyInt() {}
  18. explicit MoveOnlyInt(int value) : value(value) {}
  19. MoveOnlyInt(const MoveOnlyInt& other) = delete;
  20. MoveOnlyInt& operator=(const MoveOnlyInt& other) = delete;
  21. MoveOnlyInt(MoveOnlyInt&& other) : value(other.value) {}
  22. MoveOnlyInt& operator=(MoveOnlyInt&& other) {
  23. value = other.value;
  24. return *this;
  25. }
  26. int value = kDefaultMoveOnlyIntValue;
  27. };
  28. // Same as above. Used to test conversion from RTCErrorOr<A> to RTCErrorOr<B>
  29. // when A can be converted to B.
  30. struct MoveOnlyInt2 {
  31. MoveOnlyInt2() {}
  32. explicit MoveOnlyInt2(int value) : value(value) {}
  33. MoveOnlyInt2(const MoveOnlyInt2& other) = delete;
  34. MoveOnlyInt2& operator=(const MoveOnlyInt2& other) = delete;
  35. MoveOnlyInt2(MoveOnlyInt2&& other) : value(other.value) {}
  36. MoveOnlyInt2& operator=(MoveOnlyInt2&& other) {
  37. value = other.value;
  38. return *this;
  39. }
  40. explicit MoveOnlyInt2(MoveOnlyInt&& other) : value(other.value) {}
  41. MoveOnlyInt2& operator=(MoveOnlyInt&& other) {
  42. value = other.value;
  43. return *this;
  44. }
  45. int value = kDefaultMoveOnlyIntValue;
  46. };
  47. } // namespace
  48. namespace webrtc {
  49. // Test that the default constructor creates a "no error" error.
  50. TEST(RTCErrorTest, DefaultConstructor) {
  51. RTCError e;
  52. EXPECT_EQ(RTCErrorType::NONE, e.type());
  53. EXPECT_EQ(std::string(), e.message());
  54. EXPECT_TRUE(e.ok());
  55. }
  56. TEST(RTCErrorTest, NormalConstructors) {
  57. RTCError a(RTCErrorType::INVALID_PARAMETER);
  58. EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, a.type());
  59. EXPECT_EQ(std::string(), a.message());
  60. // Constructor that takes const char* message.
  61. RTCError b(RTCErrorType::UNSUPPORTED_PARAMETER, "foobar");
  62. EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, b.type());
  63. EXPECT_EQ(std::string("foobar"), b.message());
  64. // Constructor that takes std::string message.
  65. RTCError c(RTCErrorType::INVALID_RANGE, std::string("new"));
  66. EXPECT_EQ(RTCErrorType::INVALID_RANGE, c.type());
  67. EXPECT_EQ(std::string("new"), c.message());
  68. }
  69. TEST(RTCErrorTest, MoveConstructor) {
  70. // Static string.
  71. RTCError a(RTCErrorType::INVALID_PARAMETER, "foo");
  72. RTCError b(std::move(a));
  73. EXPECT_EQ(RTCErrorType::INVALID_PARAMETER, b.type());
  74. EXPECT_EQ(std::string("foo"), b.message());
  75. // Non-static string.
  76. RTCError c(RTCErrorType::UNSUPPORTED_PARAMETER, std::string("bar"));
  77. RTCError d(std::move(c));
  78. EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, d.type());
  79. EXPECT_EQ(std::string("bar"), d.message());
  80. }
  81. TEST(RTCErrorTest, MoveAssignment) {
  82. // Try all combinations of "is static string"/"is non-static string" moves.
  83. RTCError e(RTCErrorType::INVALID_PARAMETER, "foo");
  84. e = RTCError(RTCErrorType::UNSUPPORTED_PARAMETER, "bar");
  85. EXPECT_EQ(RTCErrorType::UNSUPPORTED_PARAMETER, e.type());
  86. EXPECT_EQ(std::string("bar"), e.message());
  87. e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("baz"));
  88. EXPECT_EQ(std::string("baz"), e.message());
  89. e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("another"));
  90. EXPECT_EQ(std::string("another"), e.message());
  91. e = RTCError(RTCErrorType::SYNTAX_ERROR, "last");
  92. EXPECT_EQ(std::string("last"), e.message());
  93. }
  94. // Test that the error returned by RTCError::OK() is a "no error" error.
  95. TEST(RTCErrorTest, OKConstant) {
  96. RTCError ok = RTCError::OK();
  97. EXPECT_EQ(RTCErrorType::NONE, ok.type());
  98. EXPECT_EQ(std::string(), ok.message());
  99. EXPECT_TRUE(ok.ok());
  100. }
  101. // Test that "error.ok()" behaves as expected.
  102. TEST(RTCErrorTest, OkMethod) {
  103. RTCError success;
  104. RTCError failure(RTCErrorType::INTERNAL_ERROR);
  105. EXPECT_TRUE(success.ok());
  106. EXPECT_FALSE(failure.ok());
  107. }
  108. // Test that a message can be set using either static const strings or
  109. // std::strings.
  110. TEST(RTCErrorTest, SetMessage) {
  111. RTCError e;
  112. // Try all combinations of "is static string"/"is non-static string" calls.
  113. e.set_message("foo");
  114. EXPECT_EQ(std::string("foo"), e.message());
  115. e.set_message("bar");
  116. EXPECT_EQ(std::string("bar"), e.message());
  117. e.set_message(std::string("string"));
  118. EXPECT_EQ(std::string("string"), e.message());
  119. e.set_message(std::string("more"));
  120. EXPECT_EQ(std::string("more"), e.message());
  121. e.set_message("love to test");
  122. EXPECT_EQ(std::string("love to test"), e.message());
  123. }
  124. // Test that the default constructor creates an "INTERNAL_ERROR".
  125. TEST(RTCErrorOrTest, DefaultConstructor) {
  126. RTCErrorOr<MoveOnlyInt> e;
  127. EXPECT_EQ(RTCErrorType::INTERNAL_ERROR, e.error().type());
  128. }
  129. // Test that an RTCErrorOr can be implicitly constructed from a value.
  130. TEST(RTCErrorOrTest, ImplicitValueConstructor) {
  131. RTCErrorOr<MoveOnlyInt> e = [] { return MoveOnlyInt(100); }();
  132. EXPECT_EQ(100, e.value().value);
  133. }
  134. // Test that an RTCErrorOr can be implicitly constructed from an RTCError.
  135. TEST(RTCErrorOrTest, ImplicitErrorConstructor) {
  136. RTCErrorOr<MoveOnlyInt> e = [] {
  137. return RTCError(RTCErrorType::SYNTAX_ERROR);
  138. }();
  139. EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, e.error().type());
  140. }
  141. TEST(RTCErrorOrTest, MoveConstructor) {
  142. RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
  143. RTCErrorOr<MoveOnlyInt> b(std::move(a));
  144. EXPECT_EQ(5, b.value().value);
  145. }
  146. TEST(RTCErrorOrTest, MoveAssignment) {
  147. RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
  148. RTCErrorOr<MoveOnlyInt> b(MoveOnlyInt(10));
  149. a = std::move(b);
  150. EXPECT_EQ(10, a.value().value);
  151. }
  152. TEST(RTCErrorOrTest, ConversionConstructor) {
  153. RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(1));
  154. RTCErrorOr<MoveOnlyInt2> b(std::move(a));
  155. }
  156. TEST(RTCErrorOrTest, ConversionAssignment) {
  157. RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
  158. RTCErrorOr<MoveOnlyInt2> b(MoveOnlyInt2(10));
  159. b = std::move(a);
  160. EXPECT_EQ(5, b.value().value);
  161. }
  162. TEST(RTCErrorOrTest, OkMethod) {
  163. RTCErrorOr<int> success(1337);
  164. RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
  165. EXPECT_TRUE(success.ok());
  166. EXPECT_FALSE(error.ok());
  167. }
  168. TEST(RTCErrorOrTest, MoveError) {
  169. RTCErrorOr<int> e({RTCErrorType::SYNTAX_ERROR, "message"});
  170. RTCError err = e.MoveError();
  171. EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, err.type());
  172. EXPECT_EQ(std::string("message"), err.message());
  173. }
  174. TEST(RTCErrorOrTest, MoveValue) {
  175. RTCErrorOr<MoveOnlyInt> e(MoveOnlyInt(88));
  176. MoveOnlyInt value = e.MoveValue();
  177. EXPECT_EQ(88, value.value);
  178. }
  179. // Death tests.
  180. // Disabled on Android because death tests misbehave on Android, see
  181. // base/test/gtest_util.h.
  182. #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
  183. TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
  184. RTCErrorOr<int> err;
  185. EXPECT_DEATH(err = RTCError::OK(), "");
  186. }
  187. TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {
  188. RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
  189. EXPECT_DEATH(error.value(), "");
  190. }
  191. TEST(RTCErrorOrDeathTest, MoveErrorValue) {
  192. RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
  193. EXPECT_DEATH(error.MoveValue(), "");
  194. }
  195. #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
  196. } // namespace webrtc