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
11 #include "api/rtc_error.h"
12
13 #include <utility>
14
15 #include "test/gtest.h"
16
17 namespace {
18
19 const int kDefaultMoveOnlyIntValue = 0xbadf00d;
20
21 // Class that has no copy constructor, ensuring that RTCErrorOr can
22 struct MoveOnlyInt {
MoveOnlyInt__anoncb5b3ecb0111::MoveOnlyInt23 MoveOnlyInt() {}
MoveOnlyInt__anoncb5b3ecb0111::MoveOnlyInt24 explicit MoveOnlyInt(int value) : value(value) {}
25 MoveOnlyInt(const MoveOnlyInt& other) = delete;
26 MoveOnlyInt& operator=(const MoveOnlyInt& other) = delete;
MoveOnlyInt__anoncb5b3ecb0111::MoveOnlyInt27 MoveOnlyInt(MoveOnlyInt&& other) : value(other.value) {}
operator =__anoncb5b3ecb0111::MoveOnlyInt28 MoveOnlyInt& operator=(MoveOnlyInt&& other) {
29 value = other.value;
30 return *this;
31 }
32
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.
38 struct MoveOnlyInt2 {
MoveOnlyInt2__anoncb5b3ecb0111::MoveOnlyInt239 MoveOnlyInt2() {}
MoveOnlyInt2__anoncb5b3ecb0111::MoveOnlyInt240 explicit MoveOnlyInt2(int value) : value(value) {}
41 MoveOnlyInt2(const MoveOnlyInt2& other) = delete;
42 MoveOnlyInt2& operator=(const MoveOnlyInt2& other) = delete;
MoveOnlyInt2__anoncb5b3ecb0111::MoveOnlyInt243 MoveOnlyInt2(MoveOnlyInt2&& other) : value(other.value) {}
operator =__anoncb5b3ecb0111::MoveOnlyInt244 MoveOnlyInt2& operator=(MoveOnlyInt2&& other) {
45 value = other.value;
46 return *this;
47 }
48
MoveOnlyInt2__anoncb5b3ecb0111::MoveOnlyInt249 explicit MoveOnlyInt2(MoveOnlyInt&& other) : value(other.value) {}
operator =__anoncb5b3ecb0111::MoveOnlyInt250 MoveOnlyInt2& operator=(MoveOnlyInt&& other) {
51 value = other.value;
52 return *this;
53 }
54
55 int value = kDefaultMoveOnlyIntValue;
56 };
57
58 } // namespace
59
60 namespace webrtc {
61
62 // Test that the default constructor creates a "no error" error.
TEST(RTCErrorTest,DefaultConstructor)63 TEST(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
TEST(RTCErrorTest,NormalConstructors)70 TEST(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
TEST(RTCErrorTest,MoveConstructor)86 TEST(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
TEST(RTCErrorTest,MoveAssignment)100 TEST(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.
TEST(RTCErrorTest,OKConstant)119 TEST(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.
TEST(RTCErrorTest,OkMethod)127 TEST(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.
TEST(RTCErrorTest,SetMessage)136 TEST(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".
TEST(RTCErrorOrTest,DefaultConstructor)156 TEST(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.
TEST(RTCErrorOrTest,ImplicitValueConstructor)162 TEST(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.
TEST(RTCErrorOrTest,ImplicitErrorConstructor)168 TEST(RTCErrorOrTest, ImplicitErrorConstructor) {
169 RTCErrorOr<MoveOnlyInt> e = [] {
170 return RTCError(RTCErrorType::SYNTAX_ERROR);
171 }();
172 EXPECT_EQ(RTCErrorType::SYNTAX_ERROR, e.error().type());
173 }
174
TEST(RTCErrorOrTest,MoveConstructor)175 TEST(RTCErrorOrTest, MoveConstructor) {
176 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(5));
177 RTCErrorOr<MoveOnlyInt> b(std::move(a));
178 EXPECT_EQ(5, b.value().value);
179 }
180
TEST(RTCErrorOrTest,MoveAssignment)181 TEST(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
TEST(RTCErrorOrTest,ConversionConstructor)188 TEST(RTCErrorOrTest, ConversionConstructor) {
189 RTCErrorOr<MoveOnlyInt> a(MoveOnlyInt(1));
190 RTCErrorOr<MoveOnlyInt2> b(std::move(a));
191 }
192
TEST(RTCErrorOrTest,ConversionAssignment)193 TEST(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
TEST(RTCErrorOrTest,OkMethod)200 TEST(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
TEST(RTCErrorOrTest,MoveError)207 TEST(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
TEST(RTCErrorOrTest,MoveValue)214 TEST(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
TEST(RTCErrorOrDeathTest,ConstructWithOkError)225 TEST(RTCErrorOrDeathTest, ConstructWithOkError) {
226 RTCErrorOr<int> err;
227 EXPECT_DEATH(err = RTCError::OK(), "");
228 }
229
TEST(RTCErrorOrDeathTest,DereferenceErrorValue)230 TEST(RTCErrorOrDeathTest, DereferenceErrorValue) {
231 RTCErrorOr<int> error = RTCError(RTCErrorType::INTERNAL_ERROR);
232 EXPECT_DEATH(error.value(), "");
233 }
234
TEST(RTCErrorOrDeathTest,MoveErrorValue)235 TEST(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
243