blob: 29dd002b141a1182967a2e031d76970a4470150d [file] [log] [blame]
deadbeef6038e972017-02-17 07:31:331/*
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 Olssona4d87372019-07-05 17:08:3311#include "api/rtc_error.h"
12
deadbeef6038e972017-02-17 07:31:3313#include <utility>
14
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "test/gtest.h"
deadbeef6038e972017-02-17 07:31:3316
Danil Chapovalova2d85e42023-03-20 16:37:2217namespace webrtc {
deadbeef6038e972017-02-17 07:31:3318namespace {
19
Danil Chapovalova2d85e42023-03-20 16:37:2220constexpr int kDefaultMoveOnlyIntValue = 0xbadf00d;
deadbeef6038e972017-02-17 07:31:3321
22// Class that has no copy constructor, ensuring that RTCErrorOr can
23struct MoveOnlyInt {
24 MoveOnlyInt() {}
25 explicit MoveOnlyInt(int value) : value(value) {}
26 MoveOnlyInt(const MoveOnlyInt& other) = delete;
deadbeefb5388d72017-02-24 09:17:4327 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 }
deadbeef6038e972017-02-17 07:31:3333
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.
39struct MoveOnlyInt2 {
40 MoveOnlyInt2() {}
41 explicit MoveOnlyInt2(int value) : value(value) {}
42 MoveOnlyInt2(const MoveOnlyInt2& other) = delete;
deadbeefb5388d72017-02-24 09:17:4343 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 }
deadbeef6038e972017-02-17 07:31:3349
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
deadbeef6038e972017-02-17 07:31:3359// Test that the default constructor creates a "no error" error.
60TEST(RTCErrorTest, DefaultConstructor) {
61 RTCError e;
Danil Chapovalova2d85e42023-03-20 16:37:2262 EXPECT_EQ(e.type(), RTCErrorType::NONE);
63 EXPECT_STREQ(e.message(), "");
deadbeef6038e972017-02-17 07:31:3364 EXPECT_TRUE(e.ok());
65}
66
67TEST(RTCErrorTest, NormalConstructors) {
68 RTCError a(RTCErrorType::INVALID_PARAMETER);
Danil Chapovalova2d85e42023-03-20 16:37:2269 EXPECT_EQ(a.type(), RTCErrorType::INVALID_PARAMETER);
70 EXPECT_STREQ(a.message(), "");
deadbeef6038e972017-02-17 07:31:3371
72 // Constructor that takes const char* message.
73 RTCError b(RTCErrorType::UNSUPPORTED_PARAMETER, "foobar");
Danil Chapovalova2d85e42023-03-20 16:37:2274 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");
deadbeef6038e972017-02-17 07:31:3381
82 // Constructor that takes std::string message.
Danil Chapovalova2d85e42023-03-20 16:37:2283 RTCError d(RTCErrorType::INVALID_RANGE, std::string("new"));
84 EXPECT_EQ(d.type(), RTCErrorType::INVALID_RANGE);
85 EXPECT_STREQ(d.message(), "new");
deadbeef6038e972017-02-17 07:31:3386}
87
88TEST(RTCErrorTest, MoveConstructor) {
89 // Static string.
90 RTCError a(RTCErrorType::INVALID_PARAMETER, "foo");
91 RTCError b(std::move(a));
Danil Chapovalova2d85e42023-03-20 16:37:2292 EXPECT_EQ(b.type(), RTCErrorType::INVALID_PARAMETER);
93 EXPECT_STREQ(b.message(), "foo");
deadbeef6038e972017-02-17 07:31:3394
95 // Non-static string.
96 RTCError c(RTCErrorType::UNSUPPORTED_PARAMETER, std::string("bar"));
97 RTCError d(std::move(c));
Danil Chapovalova2d85e42023-03-20 16:37:2298 EXPECT_EQ(d.type(), RTCErrorType::UNSUPPORTED_PARAMETER);
99 EXPECT_STREQ(d.message(), "bar");
deadbeef6038e972017-02-17 07:31:33100}
101
102TEST(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 Chapovalova2d85e42023-03-20 16:37:22107 EXPECT_EQ(e.type(), RTCErrorType::UNSUPPORTED_PARAMETER);
108 EXPECT_STREQ(e.message(), "bar");
deadbeef6038e972017-02-17 07:31:33109
Danil Chapovalova2d85e42023-03-20 16:37:22110 e = RTCError(RTCErrorType::SYNTAX_ERROR, absl::string_view("baz"));
111 EXPECT_STREQ(e.message(), "baz");
deadbeef6038e972017-02-17 07:31:33112
113 e = RTCError(RTCErrorType::SYNTAX_ERROR, std::string("another"));
Danil Chapovalova2d85e42023-03-20 16:37:22114 EXPECT_STREQ(e.message(), "another");
deadbeef6038e972017-02-17 07:31:33115}
116
117// Test that the error returned by RTCError::OK() is a "no error" error.
118TEST(RTCErrorTest, OKConstant) {
119 RTCError ok = RTCError::OK();
Danil Chapovalova2d85e42023-03-20 16:37:22120 EXPECT_EQ(ok.type(), RTCErrorType::NONE);
121 EXPECT_STREQ(ok.message(), "");
deadbeef6038e972017-02-17 07:31:33122 EXPECT_TRUE(ok.ok());
123}
124
125// Test that "error.ok()" behaves as expected.
126TEST(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.
135TEST(RTCErrorTest, SetMessage) {
136 RTCError e;
deadbeef6038e972017-02-17 07:31:33137 e.set_message("foo");
Danil Chapovalova2d85e42023-03-20 16:37:22138 EXPECT_STREQ(e.message(), "foo");
deadbeef6038e972017-02-17 07:31:33139
Danil Chapovalova2d85e42023-03-20 16:37:22140 e.set_message(absl::string_view("bar"));
141 EXPECT_STREQ(e.message(), "bar");
deadbeef6038e972017-02-17 07:31:33142
143 e.set_message(std::string("string"));
Danil Chapovalova2d85e42023-03-20 16:37:22144 EXPECT_STREQ(e.message(), "string");
deadbeef6038e972017-02-17 07:31:33145}
146
147// Test that the default constructor creates an "INTERNAL_ERROR".
148TEST(RTCErrorOrTest, DefaultConstructor) {
149 RTCErrorOr<MoveOnlyInt> e;
Danil Chapovalova2d85e42023-03-20 16:37:22150 EXPECT_EQ(e.error().type(), RTCErrorType::INTERNAL_ERROR);
deadbeef6038e972017-02-17 07:31:33151}
152
153// Test that an RTCErrorOr can be implicitly constructed from a value.
154TEST(RTCErrorOrTest, ImplicitValueConstructor) {
155 RTCErrorOr<MoveOnlyInt> e = [] { return MoveOnlyInt(100); }();
Danil Chapovalova2d85e42023-03-20 16:37:22156 EXPECT_EQ(e.value().value, 100);
deadbeef6038e972017-02-17 07:31:33157}
158
159// Test that an RTCErrorOr can be implicitly constructed from an RTCError.
160TEST(RTCErrorOrTest, ImplicitErrorConstructor) {
161 RTCErrorOr<MoveOnlyInt> e = [] {
162 return RTCError(RTCErrorType::SYNTAX_ERROR);
163 }();
Danil Chapovalova2d85e42023-03-20 16:37:22164 EXPECT_EQ(e.error().type(), RTCErrorType::SYNTAX_ERROR);
deadbeef6038e972017-02-17 07:31:33165}
166
167TEST(RTCErrorOrTest, MoveConstructor) {
168 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
169 RTCErrorOr<MoveOnlyInt> b(std::move(a));
Danil Chapovalova2d85e42023-03-20 16:37:22170 EXPECT_EQ(b.value().value, 5);
deadbeef6038e972017-02-17 07:31:33171}
172
173TEST(RTCErrorOrTest, MoveAssignment) {
174 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
175 RTCErrorOr<MoveOnlyInt> b(MoveOnlyInt(10));
176 a = std::move(b);
Danil Chapovalova2d85e42023-03-20 16:37:22177 EXPECT_EQ(a.value().value, 10);
deadbeef6038e972017-02-17 07:31:33178}
179
180TEST(RTCErrorOrTest, ConversionConstructor) {
181 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(1));
182 RTCErrorOr<MoveOnlyInt2> b(std::move(a));
183}
184
185TEST(RTCErrorOrTest, ConversionAssignment) {
186 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
187 RTCErrorOr<MoveOnlyInt2> b(MoveOnlyInt2(10));
188 b = std::move(a);
Danil Chapovalova2d85e42023-03-20 16:37:22189 EXPECT_EQ(b.value().value, 5);
deadbeef6038e972017-02-17 07:31:33190}
191
192TEST(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
199TEST(RTCErrorOrTest, MoveError) {
200 RTCErrorOr<int> e({RTCErrorType::SYNTAX_ERROR, "message"});
201 RTCError err = e.MoveError();
Danil Chapovalova2d85e42023-03-20 16:37:22202 EXPECT_EQ(err.type(), RTCErrorType::SYNTAX_ERROR);
203 EXPECT_STREQ(err.message(), "message");
deadbeef6038e972017-02-17 07:31:33204}
205
206TEST(RTCErrorOrTest, MoveValue) {
207 RTCErrorOr<MoveOnlyInt> e(MoveOnlyInt(88));
208 MoveOnlyInt value = e.MoveValue();
Danil Chapovalova2d85e42023-03-20 16:37:22209 EXPECT_EQ(value.value, 88);
deadbeef6038e972017-02-17 07:31:33210}
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
217TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
Jonas Olsson941a07c2018-09-13 08:07:07218 RTCErrorOr<int> err;
219 EXPECT_DEATH(err = RTCError::OK(), "");
deadbeef6038e972017-02-17 07:31:33220}
221
222TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {
223 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
224 EXPECT_DEATH(error.value(), "");
225}
226
227TEST(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 Chapovalova2d85e42023-03-20 16:37:22234} // namespace
deadbeef6038e972017-02-17 07:31:33235} // namespace webrtc