1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/websockets/websocket_error.h"
#include <optional>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_testing.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_websocket_close_info.h"
#include "third_party/blink/renderer/bindings/modules/v8/v8_websocket_error.h"
#include "third_party/blink/renderer/modules/websockets/websocket_channel.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/testing/task_environment.h"
#include "third_party/blink/renderer/platform/wtf/text/string_builder.h"
#include "third_party/googletest/src/googletest/include/gtest/gtest.h"
namespace blink {
namespace {
class WebSocketErrorTest : public ::testing::Test {
public:
// Creates a WebSocketError API from a WebSocketCloseInfo object, optionally
// with "closeCode" and "reason" attributes set.
static WebSocketError* CreateError(
std::optional<uint16_t> close_code = std::nullopt,
String reason = String(),
ExceptionState& exception_state = ASSERT_NO_EXCEPTION) {
auto* close_info = WebSocketCloseInfo::Create();
if (close_code) {
close_info->setCloseCode(close_code.value());
}
if (!reason.IsNull()) {
close_info->setReason(reason);
}
return WebSocketError::Create("", close_info, exception_state);
}
private:
test::TaskEnvironment task_environment_;
};
TEST_F(WebSocketErrorTest, DefaultConstruct) {
auto* error = WebSocketError::Create("", WebSocketCloseInfo::Create(),
ASSERT_NO_EXCEPTION);
ASSERT_TRUE(error);
EXPECT_EQ(error->message(), "");
EXPECT_EQ(error->code(), 0);
EXPECT_EQ(error->closeCode(), std::nullopt);
EXPECT_EQ(error->reason(), "");
}
TEST_F(WebSocketErrorTest, ConstructWithMessage) {
auto* error = WebSocketError::Create("hello", WebSocketCloseInfo::Create(),
ASSERT_NO_EXCEPTION);
ASSERT_TRUE(error);
EXPECT_EQ(error->message(), "hello");
EXPECT_EQ(error->code(), 0);
EXPECT_EQ(error->closeCode(), std::nullopt);
EXPECT_EQ(error->reason(), "");
}
TEST_F(WebSocketErrorTest, ConstructWithCloseCode) {
auto* error = CreateError(4011);
ASSERT_TRUE(error);
EXPECT_EQ(error->closeCode(), 4011);
EXPECT_EQ(error->reason(), "");
}
TEST_F(WebSocketErrorTest, ConstructWithReason) {
auto* error = CreateError(std::nullopt, "wow");
ASSERT_TRUE(error);
EXPECT_EQ(error->closeCode(), WebSocketChannel::kCloseEventCodeNormalClosure);
EXPECT_EQ(error->reason(), "wow");
}
TEST_F(WebSocketErrorTest, ConstructWithEmptyReason) {
auto* error = CreateError(std::nullopt, "");
ASSERT_TRUE(error);
EXPECT_EQ(error->closeCode(), std::nullopt);
EXPECT_EQ(error->reason(), "");
}
TEST_F(WebSocketErrorTest, ConstructWithInvalidCloseCode) {
V8TestingScope scope;
DummyExceptionStateForTesting& exception_state = scope.GetExceptionState();
auto* error = CreateError(1005, String(), exception_state);
EXPECT_FALSE(error);
ASSERT_TRUE(exception_state.HadException());
EXPECT_EQ(
"The close code must be either 1000, or between 3000 and 4999. 1005 is "
"neither.",
exception_state.Message());
EXPECT_EQ(DOMExceptionCode::kInvalidAccessError,
DOMExceptionCode{exception_state.Code()});
}
TEST_F(WebSocketErrorTest, ConstructWithOverlongReason) {
V8TestingScope scope;
DummyExceptionStateForTesting& exception_state = scope.GetExceptionState();
StringBuilder builder;
for (int i = 0; i < 32; ++i) {
// Sparkling Heart emoji. Takes 4 bytes when encoded as unicode.
builder.Append(UChar32{0x1F496});
}
auto* error =
CreateError(std::nullopt, builder.ReleaseString(), exception_state);
EXPECT_FALSE(error);
ASSERT_TRUE(exception_state.HadException());
EXPECT_EQ("The close reason must not be greater than 123 UTF-8 bytes.",
exception_state.Message());
EXPECT_EQ(DOMExceptionCode::kSyntaxError,
DOMExceptionCode{exception_state.Code()});
}
TEST_F(WebSocketErrorTest, InternalCreate) {
V8TestingScope scope;
auto* isolate = scope.GetIsolate();
auto context = scope.GetContext();
auto v8value = WebSocketError::Create(isolate, "message", 1000, "reason");
ASSERT_FALSE(v8value.IsEmpty());
ASSERT_TRUE(v8value->IsObject());
v8::Local<v8::Value> stack;
ASSERT_TRUE(v8value.As<v8::Object>()
->Get(context, V8String(isolate, "stack"))
.ToLocal(&stack));
// Maybe "stack" will return some kind of structured object someday?
// Explicitly convert it to a string just in case.
v8::Local<v8::String> stack_as_v8string;
ASSERT_TRUE(stack->ToString(context).ToLocal(&stack_as_v8string));
String stack_string = ToCoreString(isolate, stack_as_v8string);
EXPECT_TRUE(stack_string.Contains("message"));
WebSocketError* error = V8WebSocketError::ToWrappable(isolate, v8value);
ASSERT_TRUE(error);
EXPECT_EQ(error->code(), 0);
EXPECT_EQ(error->closeCode(), 1000u);
EXPECT_EQ(error->message(), "message");
EXPECT_EQ(error->reason(), "reason");
}
} // namespace
} // namespace blink
|