Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: content/browser/android/string_message_codec_unittest.cc

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Address feedback from Selim Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/android/string_message_codec.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "v8/include/v8.h"
10
11 namespace content {
12 namespace {
13
14 base::string16 DecodeWithV8(const base::string16& encoded) {
15 base::string16 result;
16
17 v8::Isolate::CreateParams params;
18 params.array_buffer_allocator =
19 v8::ArrayBuffer::Allocator::NewDefaultAllocator();
20 v8::Isolate* isolate = v8::Isolate::New(params);
21 {
22 v8::HandleScope scope(isolate);
23 v8::TryCatch try_catch(isolate);
24
25 v8::Local<v8::Context> context = v8::Context::New(isolate);
26
27 v8::ValueDeserializer deserializer(
28 isolate,
29 reinterpret_cast<const uint8_t*>(encoded.data()),
30 encoded.size() * sizeof(base::char16));
31 deserializer.SetSupportsLegacyWireFormat(true);
32
33 EXPECT_TRUE(deserializer.ReadHeader(context).ToChecked());
34
35 v8::Local<v8::Value> value =
36 deserializer.ReadValue(context).ToLocalChecked();
37 v8::Local<v8::String> str = value->ToString(context).ToLocalChecked();
38
39 result.resize(str->Length());
40 str->Write(&result[0], 0, result.size());
41 }
42 isolate->Dispose();
43 delete params.array_buffer_allocator;
44
45 return result;
46 }
47
48 base::string16 EncodeWithV8(const base::string16& message) {
49 base::string16 result;
50
51 v8::Isolate::CreateParams params;
52 params.array_buffer_allocator =
53 v8::ArrayBuffer::Allocator::NewDefaultAllocator();
54 v8::Isolate* isolate = v8::Isolate::New(params);
55 {
56 v8::HandleScope scope(isolate);
57 v8::TryCatch try_catch(isolate);
58
59 v8::Local<v8::Context> context = v8::Context::New(isolate);
60
61 v8::Local<v8::String> message_as_value =
62 v8::String::NewFromTwoByte(isolate,
63 message.data(),
64 v8::NewStringType::kNormal,
65 message.size()).ToLocalChecked();
66
67 v8::ValueSerializer serializer(isolate);
68 serializer.WriteHeader();
69 EXPECT_TRUE(serializer.WriteValue(context, message_as_value).ToChecked());
70
71 std::pair<uint8_t*, size_t> buffer = serializer.Release();
72
73 size_t result_num_bytes = (buffer.second + 1) & ~1;
74 result.resize(result_num_bytes / 2);
75 memcpy(reinterpret_cast<uint8_t*>(&result[0]), buffer.first, buffer.second);
76
77 free(buffer.first);
78 }
79 isolate->Dispose();
80 delete params.array_buffer_allocator;
81
82 return result;
83 }
84
85 TEST(StringMessageCodecTest, SelfTest_ASCII) {
86 base::string16 message = base::ASCIIToUTF16("hello");
87 base::string16 decoded;
88 EXPECT_TRUE(DecodeStringMessage(EncodeStringMessage(message), &decoded));
89 EXPECT_EQ(message, decoded);
90 }
91
92 TEST(StringMessageCodecTest, SelfTest_NonASCII) {
93 base::string16 message = base::WideToUTF16(L"hello \u263A");
94 base::string16 decoded;
95 EXPECT_TRUE(DecodeStringMessage(EncodeStringMessage(message), &decoded));
96 EXPECT_EQ(message, decoded);
97 }
98
99 TEST(StringMessageCodecTest, SelfTest_NonASCIILongEnoughToForcePadding) {
100 base::string16 message(200, 0x263A);
101 base::string16 decoded;
102 EXPECT_TRUE(DecodeStringMessage(EncodeStringMessage(message), &decoded));
103 EXPECT_EQ(message, decoded);
104 }
105
106 TEST(StringMessageCodecTest, SelfToV8Test_ASCII) {
107 base::string16 message = base::ASCIIToUTF16("hello");
108 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
109 }
110
111 TEST(StringMessageCodecTest, SelfToV8Test_NonASCII) {
112 base::string16 message = base::WideToUTF16(L"hello \u263A");
113 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
114 }
115
116 TEST(StringMessageCodecTest, SelfToV8Test_NonASCIILongEnoughToForcePadding) {
117 base::string16 message(200, 0x263A);
118 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
119 }
120
121 TEST(StringMessageCodecTest, V8ToSelfTest_ASCII) {
122 base::string16 message = base::ASCIIToUTF16("hello");
123 base::string16 decoded;
124 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
125 EXPECT_EQ(message, decoded);
126 }
127
128 TEST(StringMessageCodecTest, V8ToSelfTest_NonASCII) {
129 base::string16 message = base::WideToUTF16(L"hello \u263A");
130 base::string16 decoded;
131 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
132 EXPECT_EQ(message, decoded);
133 }
134
135 TEST(StringMessageCodecTest, V8ToSelfTest_NonASCIILongEnoughToForcePadding) {
136 base::string16 message(200, 0x263A);
137 base::string16 decoded;
138 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
139 EXPECT_EQ(message, decoded);
140 }
141
142 } // namespace
143 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698