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

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

Issue 2422793002: HTML MessagePort as mojo::MessagePipeHandle (Closed)
Patch Set: Add unit test for string_message_codec.cc 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;
101 for (int i = 0; i < 200; ++i)
102 message.push_back(base::char16(0x263A));
jbroman 2017/02/06 15:53:21 nit: equivalently, base::string16 message(200, 0x
103 base::string16 decoded;
104 EXPECT_TRUE(DecodeStringMessage(EncodeStringMessage(message), &decoded));
105 EXPECT_EQ(message, decoded);
106 }
107
108 TEST(StringMessageCodecTest, SelfToV8Test_ASCII) {
109 base::string16 message = base::ASCIIToUTF16("hello");
110 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
111 }
112
113 TEST(StringMessageCodecTest, SelfToV8Test_NonASCII) {
114 base::string16 message = base::WideToUTF16(L"hello \u263A");
115 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
116 }
117
118 TEST(StringMessageCodecTest, SelfToV8Test_NonASCIILongEnoughToForcePadding) {
119 base::string16 message;
jbroman 2017/02/06 15:53:21 nit: equivalently, base::string16 message(200, 0x
120 for (int i = 0; i < 200; ++i)
121 message.push_back(base::char16(0x263A));
122 EXPECT_EQ(message, DecodeWithV8(EncodeStringMessage(message)));
123 }
124
125 TEST(StringMessageCodecTest, V8ToSelfTest_ASCII) {
126 base::string16 message = base::ASCIIToUTF16("hello");
127 base::string16 decoded;
128 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
129 EXPECT_EQ(message, decoded);
130 }
131
132 TEST(StringMessageCodecTest, V8ToSelfTest_NonASCII) {
133 base::string16 message = base::WideToUTF16(L"hello \u263A");
134 base::string16 decoded;
135 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
136 EXPECT_EQ(message, decoded);
137 }
138
139 TEST(StringMessageCodecTest, V8ToSelfTest_NonASCIILongEnoughToForcePadding) {
140 base::string16 message;
141 for (int i = 0; i < 200; ++i)
142 message.push_back(base::char16(0x263A));
143 base::string16 decoded;
144 EXPECT_TRUE(DecodeStringMessage(EncodeWithV8(message), &decoded));
145 EXPECT_EQ(message, decoded);
146 }
147
148 } // namespace
149 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/android/string_message_codec.cc ('k') | content/browser/frame_host/render_frame_host_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698