File: string_message_codec_unittest.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (338 lines) | stat: -rw-r--r-- 12,972 bytes parent folder | download | duplicates (2)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "third_party/blink/public/common/messaging/string_message_codec.h"

#include <string>
#include <variant>

#include "base/containers/span.h"
#include "base/functional/overloaded.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/task_environment.h"
#include "mojo/public/cpp/base/big_buffer.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8.h"

namespace blink {
namespace {

WebMessagePayload DecodeWithV8(const TransferableMessage& message) {
  base::test::TaskEnvironment task_environment;
  WebMessagePayload result;

  v8::Isolate::CreateParams params;
  params.array_buffer_allocator =
      v8::ArrayBuffer::Allocator::NewDefaultAllocator();
  v8::Isolate* isolate = v8::Isolate::New(params);
  {
    v8::Isolate::Scope isolate_scope(isolate);
    v8::HandleScope scope(isolate);
    v8::TryCatch try_catch(isolate);

    v8::Local<v8::Context> context = v8::Context::New(isolate);
    v8::Context::Scope lock(context);

    v8::ValueDeserializer deserializer(isolate, message.encoded_message.data(),
                                       message.encoded_message.size());
    deserializer.SetSupportsLegacyWireFormat(true);
    if (message.array_buffer_contents_array.size() == 1) {
      // Prepare to transfer ArrayBuffer first. This does not necessary mean the
      // result type is ArrayBuffer.
      mojo_base::BigBuffer& big_buffer =
          message.array_buffer_contents_array[0]->contents;
      v8::Local<v8::ArrayBuffer> message_as_array_buffer =
          v8::ArrayBuffer::New(isolate, big_buffer.size());
      memcpy(message_as_array_buffer->GetBackingStore()->Data(),
             big_buffer.data(), big_buffer.size());
      deserializer.TransferArrayBuffer(0, message_as_array_buffer);
    }
    EXPECT_TRUE(deserializer.ReadHeader(context).ToChecked());

    v8::Local<v8::Value> value =
        deserializer.ReadValue(context).ToLocalChecked();
    if (value->IsString()) {
      v8::Local<v8::String> js_str = value->ToString(context).ToLocalChecked();
      std::u16string str;
      str.resize(js_str->Length());
      js_str->WriteV2(isolate, 0, str.size(),
                      reinterpret_cast<uint16_t*>(&str[0]));
      result = str;
    }
    if (value->IsArrayBuffer()) {
      auto js_array_buffer = value.As<v8::ArrayBuffer>()->GetBackingStore();
      std::vector<uint8_t> array_buffer_contents;
      array_buffer_contents.resize(js_array_buffer->ByteLength());
      memcpy(array_buffer_contents.data(), js_array_buffer->Data(),
             js_array_buffer->ByteLength());
      result = WebMessageArrayBufferPayload::CreateForTesting(
          std::move(array_buffer_contents));
    }
  }
  isolate->Dispose();
  delete params.array_buffer_allocator;

  return result;
}

TransferableMessage EncodeWithV8(const WebMessagePayload& message,
                                 const bool transferable = false) {
  TransferableMessage transferable_message;
  base::test::TaskEnvironment task_environment;
  std::vector<uint8_t> result;

  v8::Isolate::CreateParams params;
  params.array_buffer_allocator =
      v8::ArrayBuffer::Allocator::NewDefaultAllocator();
  v8::Isolate* isolate = v8::Isolate::New(params);
  {
    v8::Isolate::Scope isolate_scope(isolate);
    v8::HandleScope scope(isolate);
    v8::TryCatch try_catch(isolate);

    v8::Local<v8::Context> context = v8::Context::New(isolate);
    v8::Context::Scope lock(context);
    v8::ValueSerializer serializer(isolate);
    serializer.WriteHeader();

    std::visit(
        base::Overloaded{
            [&](const std::u16string& str) {
              v8::Local<v8::String> message_as_value =
                  v8::String::NewFromTwoByte(
                      isolate, reinterpret_cast<const uint16_t*>(str.data()),
                      v8::NewStringType::kNormal, str.size())
                      .ToLocalChecked();
              EXPECT_TRUE(
                  serializer.WriteValue(context, message_as_value).ToChecked());
            },
            [&](const std::unique_ptr<WebMessageArrayBufferPayload>&
                    array_buffer) {
              // Create a new JS ArrayBuffer, then transfer into serializer.
              v8::Local<v8::ArrayBuffer> message_as_array_buffer =
                  v8::ArrayBuffer::New(isolate, array_buffer->GetLength());
              array_buffer->CopyInto(base::span(
                  reinterpret_cast<uint8_t*>(message_as_array_buffer->Data()),
                  message_as_array_buffer->ByteLength()));
              if (transferable) {
                serializer.TransferArrayBuffer(0, message_as_array_buffer);
                // Copy data into a new array_buffer_contents_array slot.
                mojo_base::BigBuffer big_buffer(array_buffer->GetLength());
                array_buffer->CopyInto(big_buffer);
                constexpr bool is_resizable_by_user_js = false;
                constexpr size_t max_byte_length = 0;
                transferable_message.array_buffer_contents_array.push_back(
                    mojom::SerializedArrayBufferContents::New(
                        std::move(big_buffer), is_resizable_by_user_js,
                        max_byte_length));
              }
              EXPECT_TRUE(
                  serializer.WriteValue(context, message_as_array_buffer)
                      .ToChecked());
            }},
        message);

    std::pair<uint8_t*, size_t> buffer = serializer.Release();
    result = std::vector<uint8_t>(buffer.first, buffer.first + buffer.second);
    free(buffer.first);
  }
  isolate->Dispose();
  delete params.array_buffer_allocator;

  transferable_message.owned_encoded_message = std::move(result);
  transferable_message.encoded_message =
      transferable_message.owned_encoded_message;
  return transferable_message;
}

void CheckStringEQ(const std::optional<WebMessagePayload>& optional_payload,
                   const std::u16string& str) {
  EXPECT_TRUE(optional_payload);
  auto& payload = optional_payload.value();
  EXPECT_TRUE(std::holds_alternative<std::u16string>(payload));
  EXPECT_EQ(str, std::get<std::u16string>(payload));
}

void CheckVectorEQ(const std::optional<WebMessagePayload>& optional_payload,
                   const std::vector<uint8_t>& buffer) {
  EXPECT_TRUE(optional_payload);
  auto& payload = optional_payload.value();
  EXPECT_TRUE(
      std::holds_alternative<std::unique_ptr<WebMessageArrayBufferPayload>>(
          payload));
  auto& array_buffer =
      std::get<std::unique_ptr<WebMessageArrayBufferPayload>>(payload);
  EXPECT_EQ(buffer.size(), array_buffer->GetLength());

  auto span = array_buffer->GetAsSpanIfPossible();
  if (span) {
    // GetAsSpan is supported, check it is the same as the original buffer.
    EXPECT_EQ(std::vector<uint8_t>(span->begin(), span->end()), buffer);
  }

  std::vector<uint8_t> temp(array_buffer->GetLength());
  array_buffer->CopyInto(base::span(temp));
  EXPECT_EQ(temp, buffer);
}

TEST(StringMessageCodecTest, SelfTest_ASCII) {
  std::u16string message = u"hello";
  CheckStringEQ(DecodeToWebMessagePayload(
                    EncodeWebMessagePayload(WebMessagePayload(message))),
                message);
}

TEST(StringMessageCodecTest, SelfTest_Latin1) {
  std::u16string message = u"hello \u00E7";
  CheckStringEQ(DecodeToWebMessagePayload(
                    EncodeWebMessagePayload(WebMessagePayload(message))),
                message);
}

TEST(StringMessageCodecTest, SelfTest_TwoByte) {
  std::u16string message = u"hello \u263A";
  CheckStringEQ(DecodeToWebMessagePayload(
                    EncodeWebMessagePayload(WebMessagePayload(message))),
                message);
}

TEST(StringMessageCodecTest, SelfTest_TwoByteLongEnoughToForcePadding) {
  std::u16string message(200, 0x263A);
  CheckStringEQ(DecodeToWebMessagePayload(
                    EncodeWebMessagePayload(WebMessagePayload(message))),
                message);
}

TEST(StringMessageCodecTest, SelfTest_ArrayBuffer) {
  std::vector<uint8_t> message(200, 0xFF);
  CheckVectorEQ(DecodeToWebMessagePayload(EncodeWebMessagePayload(
                    WebMessageArrayBufferPayload::CreateForTesting(message))),
                message);
}

TEST(StringMessageCodecTest, SelfToV8Test_ASCII) {
  std::u16string message = u"hello";
  CheckStringEQ(
      DecodeWithV8(EncodeWebMessagePayload(WebMessagePayload(message))),
      message);
}

TEST(StringMessageCodecTest, SelfToV8Test_Latin1) {
  std::u16string message = u"hello \u00E7";
  CheckStringEQ(
      DecodeWithV8(EncodeWebMessagePayload(WebMessagePayload(message))),
      message);
}

TEST(StringMessageCodecTest, SelfToV8Test_TwoByte) {
  std::u16string message = u"hello \u263A";
  CheckStringEQ(
      DecodeWithV8(EncodeWebMessagePayload(WebMessagePayload(message))),
      message);
}

TEST(StringMessageCodecTest, SelfToV8Test_TwoByteLongEnoughToForcePadding) {
  std::u16string message(200, 0x263A);
  CheckStringEQ(
      DecodeWithV8(EncodeWebMessagePayload(WebMessagePayload(message))),
      message);
}

TEST(StringMessageCodecTest, SelfToV8Test_ArrayBuffer) {
  std::vector<uint8_t> message(200, 0xFF);
  CheckVectorEQ(DecodeWithV8(EncodeWebMessagePayload(
                    WebMessageArrayBufferPayload::CreateForTesting(message))),
                message);
}

TEST(StringMessageCodecTest, V8ToSelfTest_ASCII) {
  std::u16string message = u"hello";
  CheckStringEQ(DecodeToWebMessagePayload(EncodeWithV8(message)), message);
}

TEST(StringMessageCodecTest, V8ToSelfTest_Latin1) {
  std::u16string message = u"hello \u00E7";
  CheckStringEQ(DecodeToWebMessagePayload(EncodeWithV8(message)), message);
}

TEST(StringMessageCodecTest, V8ToSelfTest_TwoByte) {
  std::u16string message = u"hello \u263A";
  CheckStringEQ(DecodeToWebMessagePayload(EncodeWithV8(message)), message);
}

TEST(StringMessageCodecTest, V8ToSelfTest_TwoByteLongEnoughToForcePadding) {
  std::u16string message(200, 0x263A);
  CheckStringEQ(DecodeToWebMessagePayload(EncodeWithV8(message)), message);
}

TEST(StringMessageCodecTest, V8ToSelfTest_ArrayBuffer) {
  std::vector<uint8_t> message(200, 0xFF);
  CheckVectorEQ(DecodeToWebMessagePayload(EncodeWithV8(
                    WebMessageArrayBufferPayload::CreateForTesting(message))),
                message);
}

TEST(StringMessageCodecTest, V8ToSelfTest_ArrayBuffer_transferrable) {
  std::vector<uint8_t> message(200, 0xFF);
  CheckVectorEQ(
      DecodeToWebMessagePayload(EncodeWithV8(
          WebMessageArrayBufferPayload::CreateForTesting(message), true)),
      message);
}

TransferableMessage TransferableMessageFromRawData(std::vector<uint8_t> data) {
  TransferableMessage message;
  message.owned_encoded_message = std::move(data);
  message.encoded_message = message.owned_encoded_message;
  return message;
}

TEST(StringMessageCodecTest, Overflow) {
  const std::vector<uint8_t> kOverflowOneByteData{'"', 0xff, 0xff, 0xff, 0x7f};
  EXPECT_FALSE(DecodeToWebMessagePayload(
      TransferableMessageFromRawData(kOverflowOneByteData)));

  const std::vector<uint8_t> kOverflowTwoByteData{'c', 0xff, 0xff, 0xff, 0x7f};
  EXPECT_FALSE(DecodeToWebMessagePayload(
      TransferableMessageFromRawData(kOverflowTwoByteData)));
}

TEST(StringMessageCodecTest, InvalidDecode) {
  auto decode_from_raw = [](std::vector<uint8_t> data) {
    return DecodeToWebMessagePayload(
        TransferableMessageFromRawData(std::move(data)));
  };

  EXPECT_FALSE(decode_from_raw({})) << "no data";
  EXPECT_FALSE(decode_from_raw({0xff, 0x01})) << "only one version";
  EXPECT_FALSE(decode_from_raw({0xff, 0x80}))
      << "end of buffer during first version";
  EXPECT_FALSE(decode_from_raw({0xff, 0x01, 0xff, 0x01}))
      << "only two versions";
  EXPECT_FALSE(decode_from_raw({0xff, 0x10, 0xff, 0x80}))
      << "end of buffer during second version";
  EXPECT_FALSE(decode_from_raw({0xff, 0x15, 0xfe, 0xff, 0x01, '"', 0x01, 'a'}))
      << "end of buffer during trailer offset";
  EXPECT_FALSE(decode_from_raw({0xff, 0x15, 0x7f, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                0x00, 0xff, 0x10, '"',  0x01, 'a'}))
      << "unrecognized trailer offset tag";

  // Confirm that aside from the specific errors above, this encoding is
  // generally correct.
  auto valid_payload = decode_from_raw(
      {0xff, 0x15, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
       0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x10, '"',  0x01, 'a'});
  ASSERT_TRUE(valid_payload.has_value());
  ASSERT_TRUE(std::holds_alternative<std::u16string>(*valid_payload));
  EXPECT_EQ(std::get<std::u16string>(*valid_payload), u"a");
}

}  // namespace
}  // namespace blink