deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 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 | |
Jonas Olsson | a4d8737 | 2019-07-05 17:08:33 | [diff] [blame] | 11 | #include "api/rtc_error.h" |
| 12 | |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 13 | #include <utility> |
| 14 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 15 | #include "test/gtest.h" |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 16 | |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 17 | namespace webrtc { |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 18 | namespace { |
| 19 | |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 20 | constexpr int kDefaultMoveOnlyIntValue = 0xbadf00d; |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 21 | |
| 22 | // Class that has no copy constructor, ensuring that RTCErrorOr can |
| 23 | struct MoveOnlyInt { |
| 24 | MoveOnlyInt() {} |
| 25 | explicit MoveOnlyInt(int value) : value(value) {} |
| 26 | MoveOnlyInt(const MoveOnlyInt& other) = delete; |
deadbeef | b5388d7 | 2017-02-24 09:17:43 | [diff] [blame] | 27 | MoveOnlyInt& operator=(const MoveOnlyInt& other) = delete; |
| 28 | MoveOnlyInt(MoveOnlyInt&& other) : value(other.value) {} |
| 29 | MoveOnlyInt& operator=(MoveOnlyInt&& other) { |
| 30 | value = other.value; |
| 31 | return *this; |
| 32 | } |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 33 | |
| 34 | int value = kDefaultMoveOnlyIntValue; |
| 35 | }; |
| 36 | |
| 37 | // Same as above. Used to test conversion from RTCErrorOr<A> to RTCErrorOr<B> |
| 38 | // when A can be converted to B. |
| 39 | struct MoveOnlyInt2 { |
| 40 | MoveOnlyInt2() {} |
| 41 | explicit MoveOnlyInt2(int value) : value(value) {} |
| 42 | MoveOnlyInt2(const MoveOnlyInt2& other) = delete; |
deadbeef | b5388d7 | 2017-02-24 09:17:43 | [diff] [blame] | 43 | MoveOnlyInt2& operator=(const MoveOnlyInt2& other) = delete; |
| 44 | MoveOnlyInt2(MoveOnlyInt2&& other) : value(other.value) {} |
| 45 | MoveOnlyInt2& operator=(MoveOnlyInt2&& other) { |
| 46 | value = other.value; |
| 47 | return *this; |
| 48 | } |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 49 | |
| 50 | explicit MoveOnlyInt2(MoveOnlyInt&& other) : value(other.value) {} |
| 51 | MoveOnlyInt2& operator=(MoveOnlyInt&& other) { |
| 52 | value = other.value; |
| 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | int value = kDefaultMoveOnlyIntValue; |
| 57 | }; |
| 58 | |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 59 | // Test that the default constructor creates a "no error" error. |
| 60 | TEST(RTCErrorTest, DefaultConstructor) { |
| 61 | RTCError e; |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 62 | EXPECT_EQ(e.type(), RTCErrorType::NONE); |
| 63 | EXPECT_STREQ(e.message(), ""); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 64 | EXPECT_TRUE(e.ok()); |
| 65 | } |
| 66 | |
| 67 | TEST(RTCErrorTest, NormalConstructors) { |
| 68 | RTCError a(RTCErrorType::INVALID_PARAMETER); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 69 | EXPECT_EQ(a.type(), RTCErrorType::INVALID_PARAMETER); |
| 70 | EXPECT_STREQ(a.message(), ""); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 71 | |
| 72 | // Constructor that takes const char* message. |
| 73 | RTCError b(RTCErrorType::UNSUPPORTED_PARAMETER, "foobar"); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 74 | EXPECT_EQ(b.type(), RTCErrorType::UNSUPPORTED_PARAMETER); |
| 75 | EXPECT_STREQ(b.message(), "foobar"); |
| 76 | |
| 77 | // Constructor that takes absl::string_view message. |
| 78 | RTCError c(RTCErrorType::SYNTAX_ERROR, absl::string_view("baz")); |
| 79 | EXPECT_EQ(c.type(), RTCErrorType::SYNTAX_ERROR); |
| 80 | EXPECT_STREQ(c.message(), "baz"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 81 | |
| 82 | // Constructor that takes std::string message. |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 83 | RTCError d(RTCErrorType::INVALID_RANGE, std::string("new")); |
| 84 | EXPECT_EQ(d.type(), RTCErrorType::INVALID_RANGE); |
| 85 | EXPECT_STREQ(d.message(), "new"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TEST(RTCErrorTest, MoveConstructor) { |
| 89 | // Static string. |
| 90 | RTCError a(RTCErrorType::INVALID_PARAMETER, "foo"); |
| 91 | RTCError b(std::move(a)); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 92 | EXPECT_EQ(b.type(), RTCErrorType::INVALID_PARAMETER); |
| 93 | EXPECT_STREQ(b.message(), "foo"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 94 | |
| 95 | // Non-static string. |
| 96 | RTCError c(RTCErrorType::UNSUPPORTED_PARAMETER, std::string("bar")); |
| 97 | RTCError d(std::move(c)); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 98 | EXPECT_EQ(d.type(), RTCErrorType::UNSUPPORTED_PARAMETER); |
| 99 | EXPECT_STREQ(d.message(), "bar"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | TEST(RTCErrorTest, MoveAssignment) { |
| 103 | // Try all combinations of "is static string"/"is non-static string" moves. |
| 104 | RTCError e(RTCErrorType::INVALID_PARAMETER, "foo"); |
| 105 | |
| 106 | e = RTCError(RTCErrorType::UNSUPPORTED_PARAMETER, "bar"); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 107 | EXPECT_EQ(e.type(), RTCErrorType::UNSUPPORTED_PARAMETER); |
| 108 | EXPECT_STREQ(e.message(), "bar"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 109 | |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 110 | e = RTCError(RTCErrorType::SYNTAX_ERROR, absl::string_view("baz")); |
| 111 | EXPECT_STREQ(e.message(), "baz"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 112 | |
| 113 | e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("another")); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 114 | EXPECT_STREQ(e.message(), "another"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // Test that the error returned by RTCError::OK() is a "no error" error. |
| 118 | TEST(RTCErrorTest, OKConstant) { |
| 119 | RTCError ok = RTCError::OK(); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 120 | EXPECT_EQ(ok.type(), RTCErrorType::NONE); |
| 121 | EXPECT_STREQ(ok.message(), ""); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 122 | EXPECT_TRUE(ok.ok()); |
| 123 | } |
| 124 | |
| 125 | // Test that "error.ok()" behaves as expected. |
| 126 | TEST(RTCErrorTest, OkMethod) { |
| 127 | RTCError success; |
| 128 | RTCError failure(RTCErrorType::INTERNAL_ERROR); |
| 129 | EXPECT_TRUE(success.ok()); |
| 130 | EXPECT_FALSE(failure.ok()); |
| 131 | } |
| 132 | |
| 133 | // Test that a message can be set using either static const strings or |
| 134 | // std::strings. |
| 135 | TEST(RTCErrorTest, SetMessage) { |
| 136 | RTCError e; |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 137 | e.set_message("foo"); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 138 | EXPECT_STREQ(e.message(), "foo"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 139 | |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 140 | e.set_message(absl::string_view("bar")); |
| 141 | EXPECT_STREQ(e.message(), "bar"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 142 | |
| 143 | e.set_message(std::string("string")); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 144 | EXPECT_STREQ(e.message(), "string"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // Test that the default constructor creates an "INTERNAL_ERROR". |
| 148 | TEST(RTCErrorOrTest, DefaultConstructor) { |
| 149 | RTCErrorOr<MoveOnlyInt> e; |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 150 | EXPECT_EQ(e.error().type(), RTCErrorType::INTERNAL_ERROR); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // Test that an RTCErrorOr can be implicitly constructed from a value. |
| 154 | TEST(RTCErrorOrTest, ImplicitValueConstructor) { |
| 155 | RTCErrorOr<MoveOnlyInt> e = [] { return MoveOnlyInt(100); }(); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 156 | EXPECT_EQ(e.value().value, 100); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Test that an RTCErrorOr can be implicitly constructed from an RTCError. |
| 160 | TEST(RTCErrorOrTest, ImplicitErrorConstructor) { |
| 161 | RTCErrorOr<MoveOnlyInt> e = [] { |
| 162 | return RTCError(RTCErrorType::SYNTAX_ERROR); |
| 163 | }(); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 164 | EXPECT_EQ(e.error().type(), RTCErrorType::SYNTAX_ERROR); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | TEST(RTCErrorOrTest, MoveConstructor) { |
| 168 | RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5)); |
| 169 | RTCErrorOr<MoveOnlyInt> b(std::move(a)); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 170 | EXPECT_EQ(b.value().value, 5); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | TEST(RTCErrorOrTest, MoveAssignment) { |
| 174 | RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5)); |
| 175 | RTCErrorOr<MoveOnlyInt> b(MoveOnlyInt(10)); |
| 176 | a = std::move(b); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 177 | EXPECT_EQ(a.value().value, 10); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | TEST(RTCErrorOrTest, ConversionConstructor) { |
| 181 | RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(1)); |
| 182 | RTCErrorOr<MoveOnlyInt2> b(std::move(a)); |
| 183 | } |
| 184 | |
| 185 | TEST(RTCErrorOrTest, ConversionAssignment) { |
| 186 | RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5)); |
| 187 | RTCErrorOr<MoveOnlyInt2> b(MoveOnlyInt2(10)); |
| 188 | b = std::move(a); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 189 | EXPECT_EQ(b.value().value, 5); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | TEST(RTCErrorOrTest, OkMethod) { |
| 193 | RTCErrorOr<int> success(1337); |
| 194 | RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR); |
| 195 | EXPECT_TRUE(success.ok()); |
| 196 | EXPECT_FALSE(error.ok()); |
| 197 | } |
| 198 | |
| 199 | TEST(RTCErrorOrTest, MoveError) { |
| 200 | RTCErrorOr<int> e({RTCErrorType::SYNTAX_ERROR, "message"}); |
| 201 | RTCError err = e.MoveError(); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 202 | EXPECT_EQ(err.type(), RTCErrorType::SYNTAX_ERROR); |
| 203 | EXPECT_STREQ(err.message(), "message"); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | TEST(RTCErrorOrTest, MoveValue) { |
| 207 | RTCErrorOr<MoveOnlyInt> e(MoveOnlyInt(88)); |
| 208 | MoveOnlyInt value = e.MoveValue(); |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 209 | EXPECT_EQ(value.value, 88); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Death tests. |
| 213 | // Disabled on Android because death tests misbehave on Android, see |
| 214 | // base/test/gtest_util.h. |
| 215 | #if RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 216 | |
| 217 | TEST(RTCErrorOrDeathTest, ConstructWithOkError) { |
Jonas Olsson | 941a07c | 2018-09-13 08:07:07 | [diff] [blame] | 218 | RTCErrorOr<int> err; |
| 219 | EXPECT_DEATH(err = RTCError::OK(), ""); |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | TEST(RTCErrorOrDeathTest, DereferenceErrorValue) { |
| 223 | RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR); |
| 224 | EXPECT_DEATH(error.value(), ""); |
| 225 | } |
| 226 | |
| 227 | TEST(RTCErrorOrDeathTest, MoveErrorValue) { |
| 228 | RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR); |
| 229 | EXPECT_DEATH(error.MoveValue(), ""); |
| 230 | } |
| 231 | |
| 232 | #endif // RTC_DCHECK_IS_ON && GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID) |
| 233 | |
Danil Chapovalov | a2d85e4 | 2023-03-20 16:37:22 | [diff] [blame] | 234 | } // namespace |
deadbeef | 6038e97 | 2017-02-17 07:31:33 | [diff] [blame] | 235 | } // namespace webrtc |