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