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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
|
// Copyright 2012 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/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "content/renderer/v8_value_converter_impl.h"
#include <stddef.h>
#include <stdint.h>
#include <cmath>
#include <memory>
#include "base/containers/span.h"
#include "base/memory/raw_ptr.h"
#include "base/test/task_environment.h"
#include "base/test/values_test_util.h"
#include "base/values.h"
#include "gin/public/isolate_holder.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "v8/include/v8-array-buffer.h"
#include "v8/include/v8-container.h"
#include "v8/include/v8-context.h"
#include "v8/include/v8-date.h"
#include "v8/include/v8-isolate.h"
#include "v8/include/v8-local-handle.h"
#include "v8/include/v8-microtask-queue.h"
#include "v8/include/v8-object.h"
#include "v8/include/v8-persistent-handle.h"
#include "v8/include/v8-primitive.h"
#include "v8/include/v8-regexp.h"
#include "v8/include/v8-script.h"
#include "v8/include/v8-template.h"
#include "v8/include/v8-typed-array.h"
namespace content {
// To improve the performance of
// V8ValueConverterImpl::UpdateAndCheckUniqueness, identity hashes of objects
// are used during checking for duplicates. For testing purposes we need to
// ignore the hash sometimes. Create this helper object to avoid using identity
// hashes for the lifetime of the helper.
class ScopedAvoidIdentityHashForTesting {
public:
// The hashes will be ignored in |converter|, which must not be NULL and it
// must outlive the created instance of this helper.
explicit ScopedAvoidIdentityHashForTesting(
content::V8ValueConverterImpl* converter);
ScopedAvoidIdentityHashForTesting(const ScopedAvoidIdentityHashForTesting&) =
delete;
ScopedAvoidIdentityHashForTesting& operator=(
const ScopedAvoidIdentityHashForTesting&) = delete;
~ScopedAvoidIdentityHashForTesting();
private:
raw_ptr<content::V8ValueConverterImpl> converter_;
};
ScopedAvoidIdentityHashForTesting::ScopedAvoidIdentityHashForTesting(
content::V8ValueConverterImpl* converter)
: converter_(converter) {
CHECK(converter_);
converter_->avoid_identity_hash_for_testing_ = true;
}
ScopedAvoidIdentityHashForTesting::~ScopedAvoidIdentityHashForTesting() {
converter_->avoid_identity_hash_for_testing_ = false;
}
class V8ValueConverterImplTest : public testing::Test {
public:
V8ValueConverterImplTest()
: isolate_holder_(task_environment_.GetMainThreadTaskRunner(),
gin::IsolateHolder::IsolateType::kTest),
isolate_scope_(isolate_holder_.isolate()),
isolate_(isolate_holder_.isolate()) {}
protected:
void SetUp() override {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::ObjectTemplate> global = v8::ObjectTemplate::New(isolate_);
context_.Reset(isolate_, v8::Context::New(isolate_, nullptr, global));
}
void TearDown() override { context_.Reset(); }
std::string GetString(base::Value::Dict* value, const std::string& key) {
std::string* temp = value->FindString(key);
if (!temp) {
ADD_FAILURE();
return std::string();
}
return *temp;
}
std::string GetString(v8::Local<v8::Object> value, const std::string& key) {
v8::Local<v8::Value> temp;
if (!value
->Get(isolate_->GetCurrentContext(),
v8::String::NewFromUtf8(isolate_, key.c_str(),
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocal(&temp)) {
ADD_FAILURE();
return std::string();
}
v8::String::Utf8Value utf8(isolate_, temp.As<v8::String>());
return std::string(*utf8, utf8.length());
}
std::string GetString(const base::Value::List& value_list, uint32_t index) {
if (index >= value_list.size() || !value_list[index].is_string()) {
ADD_FAILURE();
return std::string();
}
return value_list[index].GetString();
}
std::string GetString(v8::Local<v8::Array> value, uint32_t index) {
v8::Local<v8::Value> temp;
if (!value->Get(isolate_->GetCurrentContext(), index).ToLocal(&temp)) {
ADD_FAILURE();
return std::string();
}
v8::String::Utf8Value utf8(isolate_, temp.As<v8::String>());
return std::string(*utf8, utf8.length());
}
int32_t GetInt(v8::Local<v8::Object> value, const std::string& key) {
v8::Local<v8::Value> temp;
if (!value
->Get(isolate_->GetCurrentContext(),
v8::String::NewFromUtf8(isolate_, key.c_str(),
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocal(&temp)) {
ADD_FAILURE();
return -1;
}
return temp.As<v8::Int32>()->Value();
}
int32_t GetInt(v8::Local<v8::Object> value, uint32_t index) {
v8::Local<v8::Value> temp;
if (!value->Get(isolate_->GetCurrentContext(), index).ToLocal(&temp)) {
ADD_FAILURE();
return -1;
}
return temp.As<v8::Int32>()->Value();
}
bool IsNull(base::Value::Dict* value, const std::string& key) {
base::Value* child = value->Find(key);
if (!child) {
ADD_FAILURE();
return false;
}
return child->type() == base::Value::Type::NONE;
}
bool IsNull(v8::Local<v8::Object> value, const std::string& key) {
v8::Local<v8::Value> child;
if (!value
->Get(isolate_->GetCurrentContext(),
v8::String::NewFromUtf8(isolate_, key.c_str(),
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocal(&child)) {
ADD_FAILURE();
return false;
}
return child->IsNull();
}
bool IsNull(const base::Value::List& list, uint32_t index) {
if (list.size() <= index) {
ADD_FAILURE();
return false;
}
return list[index].type() == base::Value::Type::NONE;
}
bool IsNull(v8::Local<v8::Array> value, uint32_t index) {
v8::Local<v8::Value> child;
if (!value->Get(isolate_->GetCurrentContext(), index).ToLocal(&child)) {
ADD_FAILURE();
return false;
}
return child->IsNull();
}
void TestWeirdType(V8ValueConverterImpl& converter,
v8::Local<v8::Value> val,
base::Value::Type expected_type,
std::unique_ptr<base::Value> expected_value) {
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
std::unique_ptr<base::Value> raw(converter.FromV8Value(val, context));
if (expected_value) {
ASSERT_TRUE(raw.get());
EXPECT_TRUE(*expected_value == *raw);
EXPECT_EQ(expected_type, raw->type());
} else {
EXPECT_FALSE(raw.get());
}
v8::Local<v8::Object> object(v8::Object::New(isolate_));
object
->Set(context,
v8::String::NewFromUtf8(isolate_, "test",
v8::NewStringType::kInternalized)
.ToLocalChecked(),
val)
.Check();
std::unique_ptr<base::Value> dictionary_val(
converter.FromV8Value(object, context));
ASSERT_TRUE(dictionary_val.get());
ASSERT_TRUE(dictionary_val->is_dict());
const base::Value::Dict& dictionary = dictionary_val->GetDict();
if (expected_value) {
const base::Value* temp = dictionary.Find("test");
ASSERT_TRUE(temp);
EXPECT_EQ(expected_type, temp->type());
EXPECT_EQ(*expected_value, *temp);
} else {
EXPECT_FALSE(dictionary.contains("test"));
}
v8::Local<v8::Array> array(v8::Array::New(isolate_));
array->Set(context, 0, val).Check();
std::unique_ptr<base::Value> list(converter.FromV8Value(array, context));
ASSERT_TRUE(list.get());
ASSERT_TRUE(list->is_list());
if (expected_value) {
ASSERT_FALSE(list->GetList().empty());
const base::Value& temp = list->GetList()[0];
EXPECT_EQ(expected_type, temp.type());
EXPECT_EQ(*expected_value, temp);
} else {
// Arrays should preserve their length, and convert unconvertible
// types into null.
ASSERT_FALSE(list->GetList().empty());
const base::Value& temp = list->GetList()[0];
EXPECT_EQ(base::Value::Type::NONE, temp.type());
}
}
template <typename T>
v8::Local<T> CompileRun(v8::Local<v8::Context> context, const char* source) {
return v8::Script::Compile(
context,
v8::String::NewFromUtf8(isolate_, source).ToLocalChecked())
.ToLocalChecked()
->Run(context)
.ToLocalChecked()
.As<T>();
}
base::test::TaskEnvironment task_environment_;
gin::IsolateHolder isolate_holder_;
v8::Isolate::Scope isolate_scope_;
raw_ptr<v8::Isolate> isolate_ = nullptr;
// Context for the JavaScript in the test.
v8::Persistent<v8::Context> context_;
};
TEST_F(V8ValueConverterImplTest, BasicRoundTrip) {
const base::Value original_root = base::test::ParseJson(
"{ \n"
" \"null\": null, \n"
" \"true\": true, \n"
" \"false\": false, \n"
" \"positive-int\": 42, \n"
" \"negative-int\": -42, \n"
" \"zero\": 0, \n"
" \"double\": 88.8, \n"
" \"big-integral-double\": 9007199254740992.0, \n" // 2.0^53
" \"string\": \"foobar\", \n"
" \"empty-string\": \"\", \n"
" \"dictionary\": { \n"
" \"foo\": \"bar\",\n"
" \"hot\": \"dog\",\n"
" }, \n"
" \"empty-dictionary\": {}, \n"
" \"list\": [ \"bar\", \"foo\" ], \n"
" \"empty-list\": [], \n"
"}");
ASSERT_TRUE(original_root.is_dict());
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
V8ValueConverterImpl converter;
v8::Local<v8::Object> v8_object =
converter.ToV8Value(original_root, context).As<v8::Object>();
ASSERT_FALSE(v8_object.IsEmpty());
EXPECT_EQ(original_root.GetDict().size(),
v8_object->GetPropertyNames(context).ToLocalChecked()->Length());
EXPECT_TRUE(
v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "null", v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsNull());
EXPECT_TRUE(
v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "true", v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsTrue());
EXPECT_TRUE(v8_object
->Get(context,
v8::String::NewFromUtf8(
isolate_, "false", v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsFalse());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "positive-int",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsInt32());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "negative-int",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsInt32());
EXPECT_TRUE(
v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "zero", v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsInt32());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "double",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsNumber());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "big-integral-double",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsNumber());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "string",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsString());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "empty-string",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsString());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "dictionary",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsObject());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "empty-dictionary",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsObject());
EXPECT_TRUE(
v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "list", v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsArray());
EXPECT_TRUE(v8_object
->Get(context, v8::String::NewFromUtf8(
isolate_, "empty-list",
v8::NewStringType::kInternalized)
.ToLocalChecked())
.ToLocalChecked()
->IsArray());
std::unique_ptr<base::Value> new_root(
converter.FromV8Value(v8_object, context));
EXPECT_EQ(original_root, *new_root);
}
TEST_F(V8ValueConverterImplTest, KeysWithDots) {
const base::Value original =
base::test::ParseJson("{ \"foo.bar\": \"baz\" }");
ASSERT_TRUE(original.is_dict());
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> copy(
converter.FromV8Value(converter.ToV8Value(original, context), context));
EXPECT_EQ(original, *copy);
}
TEST_F(V8ValueConverterImplTest, ObjectExceptions) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
// Set up objects to throw when reading or writing 'foo'.
const char source[] =
"Object.prototype.__defineSetter__('foo', "
" function() { throw new Error('muah!'); });"
"Object.prototype.__defineGetter__('foo', "
" function() { throw new Error('muah!'); });";
CompileRun<v8::Value>(context, source);
v8::Local<v8::Object> object(v8::Object::New(isolate_));
v8::Local<v8::String> bar =
v8::String::NewFromUtf8(isolate_, "bar", v8::NewStringType::kInternalized)
.ToLocalChecked();
object->Set(context, bar, bar).Check();
// Converting from v8 value should replace the foo property with null.
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> base_value =
converter.FromV8Value(object, context);
base::Value::Dict* converted = base_value->GetIfDict();
ASSERT_TRUE(converted);
// http://code.google.com/p/v8/issues/detail?id=1342
// EXPECT_EQ(2u, converted);
// EXPECT_TRUE(IsNull(*converted, "foo"));
EXPECT_EQ(1u, converted->size());
EXPECT_EQ("bar", GetString(converted, "bar"));
// Converting to v8 value should not trigger the setter.
converted->Set("foo", "foo");
v8::Local<v8::Object> copy =
converter.ToV8Value(*converted, context).As<v8::Object>();
EXPECT_FALSE(copy.IsEmpty());
EXPECT_EQ(2u, copy->GetPropertyNames(context).ToLocalChecked()->Length());
EXPECT_EQ("foo", GetString(copy, "foo"));
EXPECT_EQ("bar", GetString(copy, "bar"));
}
TEST_F(V8ValueConverterImplTest, ArrayExceptions) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
const char source[] =
"(function() {"
"var arr = [];"
"arr.__defineSetter__(0, "
" function() { throw new Error('muah!'); });"
"arr.__defineGetter__(0, "
" function() { throw new Error('muah!'); });"
"arr[1] = 'bar';"
"return arr;"
"})();";
v8::Local<v8::Array> array = CompileRun<v8::Array>(context, source);
// Converting from v8 value should replace the first item with null.
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> converted =
converter.FromV8Value(array, context);
ASSERT_TRUE(converted);
ASSERT_TRUE(converted->is_list());
// http://code.google.com/p/v8/issues/detail?id=1342
EXPECT_EQ(2u, converted->GetList().size());
EXPECT_TRUE(IsNull(converted->GetList(), 0));
// Converting to v8 value should not be affected by the getter/setter
// because the setters/getters are defined on the array instance, not
// on the Array's prototype.
*converted = base::test::ParseJson("[ \"foo\", \"bar\" ]");
v8::Local<v8::Array> copy =
converter.ToV8Value(*converted, context).As<v8::Array>();
ASSERT_FALSE(copy.IsEmpty());
EXPECT_EQ(2u, copy->Length());
EXPECT_EQ("foo", GetString(copy, 0));
EXPECT_EQ("bar", GetString(copy, 1));
}
TEST_F(V8ValueConverterImplTest, WeirdTypes) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::RegExp> regex(
v8::RegExp::New(context, v8::String::NewFromUtf8Literal(isolate_, "."),
v8::RegExp::kNone)
.ToLocalChecked());
V8ValueConverterImpl converter;
TestWeirdType(converter, v8::Undefined(isolate_),
base::Value::Type::NONE, // Arbitrary type, result is NULL.
std::unique_ptr<base::Value>());
TestWeirdType(converter, v8::Date::New(context, 1000).ToLocalChecked(),
base::Value::Type::DICT,
std::make_unique<base::Value>(base::Value::Type::DICT));
TestWeirdType(converter, regex, base::Value::Type::DICT,
std::make_unique<base::Value>(base::Value::Type::DICT));
converter.SetDateAllowed(true);
TestWeirdType(converter, v8::Date::New(context, 1000).ToLocalChecked(),
base::Value::Type::DOUBLE, std::make_unique<base::Value>(1.0));
converter.SetRegExpAllowed(true);
TestWeirdType(converter, regex, base::Value::Type::STRING,
std::make_unique<base::Value>("/./"));
}
TEST_F(V8ValueConverterImplTest, Prototype) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
const char source[] =
"(function() {"
"Object.prototype.foo = 'foo';"
"return {};"
"})();";
v8::Local<v8::Object> object = CompileRun<v8::Object>(context, source);
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> base_value =
converter.FromV8Value(object, context);
base::Value::Dict* result = base_value->GetIfDict();
ASSERT_TRUE(result);
EXPECT_TRUE(result->empty());
}
TEST_F(V8ValueConverterImplTest, ObjectPrototypeSetter) {
const base::Value original =
base::test::ParseJson("{ \"foo\": \"good value\" }");
ASSERT_TRUE(original.is_dict());
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
const char source[] =
"var result = { getters: 0, setters: 0 };"
"Object.defineProperty(Object.prototype, 'foo', {"
" get() { ++result.getters; return 'bogus'; },"
" set() { ++result.setters; },"
"});"
"result;";
const char source_sanity[] =
"({}).foo = 'Trigger setter';"
"({}).foo;";
v8::Local<v8::Object> result = CompileRun<v8::Object>(context, source);
// Sanity checks: the getters/setters are normally triggered.
CompileRun<v8::Value>(context, source_sanity);
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
V8ValueConverterImpl converter;
v8::Local<v8::Object> converted =
converter.ToV8Value(original, context).As<v8::Object>();
EXPECT_FALSE(converted.IsEmpty());
// Getters/setters shouldn't be triggered.
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
EXPECT_EQ(1u,
converted->GetPropertyNames(context).ToLocalChecked()->Length());
EXPECT_EQ("good value", GetString(converted, "foo"));
// Getters/setters shouldn't be triggered while accessing existing values.
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
// Repeat the same exercise with a dictionary without the key.
base::Value::Dict missing_key_dict;
missing_key_dict.Set("otherkey", "hello");
v8::Local<v8::Object> converted2 =
converter.ToV8Value(missing_key_dict, context).As<v8::Object>();
EXPECT_FALSE(converted2.IsEmpty());
// Getters/setters shouldn't be triggered.
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
EXPECT_EQ(1u,
converted2->GetPropertyNames(context).ToLocalChecked()->Length());
EXPECT_EQ("hello", GetString(converted2, "otherkey"));
// Missing key = should trigger getter upon access.
EXPECT_EQ("bogus", GetString(converted2, "foo"));
EXPECT_EQ(2, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
}
TEST_F(V8ValueConverterImplTest, ArrayPrototypeSetter) {
const base::Value original = base::test::ParseJson("[100, 200, 300]");
ASSERT_TRUE(original.is_list());
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
const char source[] =
"var result = { getters: 0, setters: 0 };"
"Object.defineProperty(Array.prototype, '1', {"
" get() { ++result.getters; return 1337; },"
" set() { ++result.setters; },"
"});"
"result;";
const char source_sanity[] =
"[][1] = 'Trigger setter';"
"[][1];";
v8::Local<v8::Object> result = CompileRun<v8::Object>(context, source);
// Sanity checks: the getters/setters are normally triggered.
CompileRun<v8::Value>(context, source_sanity);
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
V8ValueConverterImpl converter;
v8::Local<v8::Array> converted =
converter.ToV8Value(original, context).As<v8::Array>();
EXPECT_FALSE(converted.IsEmpty());
// Getters/setters shouldn't be triggered during the conversion.
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
EXPECT_EQ(3u, converted->Length());
EXPECT_EQ(100, GetInt(converted, 0));
EXPECT_EQ(200, GetInt(converted, 1));
EXPECT_EQ(300, GetInt(converted, 2));
// Getters/setters shouldn't be triggered while accessing existing values.
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
// Try again, using an array without the index.
base::Value::List one_item_list;
one_item_list.Append(123456);
v8::Local<v8::Array> converted2 =
converter.ToV8Value(one_item_list, context).As<v8::Array>();
EXPECT_FALSE(converted2.IsEmpty());
// Getters/setters shouldn't be triggered during the conversion.
EXPECT_EQ(1, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
EXPECT_EQ(1u, converted2->Length());
EXPECT_EQ(123456, GetInt(converted2, 0));
// Accessing missing index 1 triggers the getter.
EXPECT_EQ(1337, GetInt(converted2, 1));
EXPECT_EQ(2, GetInt(result, "getters"));
EXPECT_EQ(1, GetInt(result, "setters"));
}
TEST_F(V8ValueConverterImplTest, StripNullFromObjects) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
const char source[] =
"(function() {"
"return { foo: undefined, bar: null };"
"})();";
v8::Local<v8::Object> object = CompileRun<v8::Object>(context, source);
V8ValueConverterImpl converter;
converter.SetStripNullFromObjects(true);
std::unique_ptr<base::Value> base_value =
converter.FromV8Value(object, context);
base::Value::Dict* result = base_value->GetIfDict();
ASSERT_TRUE(result);
EXPECT_TRUE(result->empty());
}
TEST_F(V8ValueConverterImplTest, RecursiveObjects) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
V8ValueConverterImpl converter;
v8::Local<v8::Object> object = v8::Object::New(isolate_).As<v8::Object>();
ASSERT_FALSE(object.IsEmpty());
object
->Set(context,
v8::String::NewFromUtf8(isolate_, "foo",
v8::NewStringType::kInternalized)
.ToLocalChecked(),
v8::String::NewFromUtf8Literal(isolate_, "bar"))
.Check();
object
->Set(context,
v8::String::NewFromUtf8(isolate_, "obj",
v8::NewStringType::kInternalized)
.ToLocalChecked(),
object)
.Check();
std::unique_ptr<base::Value> base_value =
converter.FromV8Value(object, context);
base::Value::Dict* object_result = base_value->GetIfDict();
ASSERT_TRUE(object_result);
EXPECT_EQ(2u, object_result->size());
EXPECT_TRUE(IsNull(object_result, "obj"));
v8::Local<v8::Array> array = v8::Array::New(isolate_).As<v8::Array>();
ASSERT_FALSE(array.IsEmpty());
array->Set(context, 0, v8::String::NewFromUtf8Literal(isolate_, "1")).Check();
array->Set(context, 1, array).Check();
base::Value::List list_result(
std::move(*converter.FromV8Value(array, context)).TakeList());
EXPECT_EQ(2u, list_result.size());
EXPECT_TRUE(IsNull(list_result, 1));
}
TEST_F(V8ValueConverterImplTest, WeirdProperties) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
const char source[] =
"(function() {"
"return {"
" 1: 'foo',"
" '2': 'bar',"
" true: 'baz',"
" false: 'qux',"
" null: 'quux',"
" undefined: 'oops'"
"};"
"})();";
v8::Local<v8::Object> object = CompileRun<v8::Object>(context, source);
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> actual(converter.FromV8Value(object, context));
const base::Value expected = base::test::ParseJson(
"{ \n"
" \"1\": \"foo\", \n"
" \"2\": \"bar\", \n"
" \"true\": \"baz\", \n"
" \"false\": \"qux\", \n"
" \"null\": \"quux\", \n"
" \"undefined\": \"oops\", \n"
"}");
EXPECT_EQ(expected, *actual);
}
TEST_F(V8ValueConverterImplTest, ArrayGetters) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
const char source[] =
"(function() {"
"var a = [0];"
"a.__defineGetter__(1, function() { return 'bar'; });"
"return a;"
"})();";
v8::Local<v8::Array> array = CompileRun<v8::Array>(context, source);
V8ValueConverterImpl converter;
base::Value::List result(
std::move(*converter.FromV8Value(array, context)).TakeList());
EXPECT_EQ(2u, result.size());
}
TEST_F(V8ValueConverterImplTest, UndefinedValueBehavior) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::Object> object;
{
const char source[] =
"(function() {"
"return { foo: undefined, bar: null, baz: function(){} };"
"})();";
object = CompileRun<v8::Object>(context, source);
}
v8::Local<v8::Array> array;
{
const char source[] =
"(function() {"
"return [ undefined, null, function(){} ];"
"})();";
array = CompileRun<v8::Array>(context, source);
}
v8::Local<v8::Array> sparse_array;
{
const char source[] =
"(function() {"
"return new Array(3);"
"})();";
sparse_array = CompileRun<v8::Array>(context, source);
}
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> actual_object(
converter.FromV8Value(object, context));
EXPECT_EQ(base::test::ParseJson("{ \"bar\": null }"), *actual_object);
// Everything is null because JSON stringification preserves array length.
std::unique_ptr<base::Value> actual_array(
converter.FromV8Value(array, context));
EXPECT_EQ(base::test::ParseJson("[ null, null, null ]"), *actual_array);
std::unique_ptr<base::Value> actual_sparse_array(
converter.FromV8Value(sparse_array, context));
EXPECT_EQ(base::test::ParseJson("[ null, null, null ]"),
*actual_sparse_array);
}
TEST_F(V8ValueConverterImplTest, ObjectsWithClashingIdentityHash) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
V8ValueConverterImpl converter;
// We check that the converter checks identity correctly by disabling the
// optimization of using identity hashes.
ScopedAvoidIdentityHashForTesting scoped_hash_avoider(&converter);
// Create the v8::Object to be converted.
v8::Local<v8::Array> root(v8::Array::New(isolate_, 4));
root->Set(context, 0, v8::Local<v8::Object>(v8::Object::New(isolate_)))
.Check();
root->Set(context, 1, v8::Local<v8::Object>(v8::Object::New(isolate_)))
.Check();
root->Set(context, 2, v8::Local<v8::Object>(v8::Array::New(isolate_, 0)))
.Check();
root->Set(context, 3, v8::Local<v8::Object>(v8::Array::New(isolate_, 0)))
.Check();
// The expected base::Value result.
const base::Value expected = base::test::ParseJson("[{},{},[],[]]");
ASSERT_TRUE(expected.is_list());
// The actual result.
std::unique_ptr<base::Value> value(converter.FromV8Value(root, context));
ASSERT_TRUE(value);
EXPECT_EQ(expected, *value);
}
TEST_F(V8ValueConverterImplTest, DetectCycles) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
V8ValueConverterImpl converter;
// Create a recursive array.
v8::Local<v8::Array> recursive_array(v8::Array::New(isolate_, 1));
recursive_array->Set(context, 0, recursive_array).Check();
// The first repetition should be trimmed and replaced by a null value.
base::Value::List expected_list;
expected_list.Append(base::Value());
// The actual result.
std::unique_ptr<base::Value> actual_list(
converter.FromV8Value(recursive_array, context));
ASSERT_TRUE(actual_list.get());
EXPECT_TRUE(expected_list == actual_list->GetList());
// Now create a recursive object
const std::string key("key");
v8::Local<v8::Object> recursive_object(v8::Object::New(isolate_));
recursive_object
->Set(context,
v8::String::NewFromUtf8(isolate_, key.c_str(),
v8::NewStringType::kInternalized,
key.length())
.ToLocalChecked(),
recursive_object)
.Check();
// The first repetition should be trimmed and replaced by a null value.
base::Value::Dict expected_dictionary;
expected_dictionary.Set(key, base::Value());
// The actual result.
std::unique_ptr<base::Value> base_value =
converter.FromV8Value(recursive_object, context);
base::Value::Dict* actual_dictionary = base_value->GetIfDict();
ASSERT_TRUE(actual_dictionary);
EXPECT_EQ(expected_dictionary, *actual_dictionary);
}
// Tests that reused object values with no cycles do not get nullified.
TEST_F(V8ValueConverterImplTest, ReuseObjects) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
V8ValueConverterImpl converter;
// Object with reused values in different keys.
{
const char source[] =
"(function() {"
"var objA = {key: 'another same value'};"
"var obj = {one: objA, two: objA};"
"return obj;"
"})();";
v8::Local<v8::Object> object = CompileRun<v8::Object>(context, source);
// The actual result.
std::unique_ptr<base::Value> base_value =
converter.FromV8Value(object, context);
base::Value::Dict* result = base_value->GetIfDict();
ASSERT_TRUE(result);
EXPECT_EQ(2u, result->size());
{
const char key1[] = "one";
base::Value::Dict* one_dict = result->FindDict(key1);
ASSERT_TRUE(one_dict);
EXPECT_EQ("another same value", GetString(one_dict, "key"));
}
{
const char key2[] = "two";
base::Value::Dict* two_dict = result->FindDict(key2);
ASSERT_TRUE(two_dict);
EXPECT_EQ("another same value", GetString(two_dict, "key"));
}
}
// Array with reused values.
{
const char source[] =
"(function() {"
"var objA = {key: 'same value'};"
"var arr = [objA, objA];"
"return arr;"
"})();";
v8::Local<v8::Array> array = CompileRun<v8::Array>(context, source);
// The actual result.
base::Value::List list_result(
std::move(*converter.FromV8Value(array, context)).TakeList());
ASSERT_EQ(2u, list_result.size());
for (size_t i = 0; i < list_result.size(); ++i) {
ASSERT_FALSE(IsNull(list_result, i));
base::Value::Dict* dict_value = list_result[0].GetIfDict();
ASSERT_TRUE(dict_value);
EXPECT_STREQ("same value", dict_value->FindString("key")->c_str());
}
}
}
TEST_F(V8ValueConverterImplTest, MaxRecursionDepth) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
// Must larger than kMaxRecursionDepth in v8_value_converter_impl.cc.
int kDepth = 1000;
const char kKey[] = "key";
v8::Local<v8::Object> deep_object = v8::Object::New(isolate_);
v8::Local<v8::Object> leaf = deep_object;
for (int i = 0; i < kDepth; ++i) {
v8::Local<v8::Object> new_object = v8::Object::New(isolate_);
leaf->Set(context,
v8::String::NewFromUtf8(isolate_, kKey,
v8::NewStringType::kInternalized)
.ToLocalChecked(),
new_object)
.Check();
leaf = new_object;
}
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> value(
converter.FromV8Value(deep_object, context));
ASSERT_TRUE(value);
// Expected depth is kMaxRecursionDepth in v8_value_converter_impl.cc.
int kExpectedDepth = 100;
const base::Value* current = value.get();
for (int i = 1; i < kExpectedDepth; ++i) {
ASSERT_TRUE(current->is_dict()) << i;
const base::Value::Dict& current_as_object = current->GetDict();
current = current_as_object.Find(kKey);
ASSERT_TRUE(current) << i;
}
// The leaf node shouldn't have any properties.
EXPECT_EQ(base::Value(base::Value::Dict()), *current) << *current;
}
TEST_F(V8ValueConverterImplTest, NegativeZero) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::MicrotasksScope microtasks(context,
v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Context::Scope context_scope(context);
const char source[] = "(function() { return -0; })();";
v8::Local<v8::Value> value = CompileRun<v8::Value>(context, source);
{
V8ValueConverterImpl converter;
std::unique_ptr<base::Value> result = converter.FromV8Value(value, context);
ASSERT_TRUE(result->is_double())
<< base::Value::GetTypeName(result->type());
EXPECT_EQ(0, result->GetDouble());
}
{
V8ValueConverterImpl converter;
converter.SetConvertNegativeZeroToInt(true);
std::unique_ptr<base::Value> result = converter.FromV8Value(value, context);
ASSERT_TRUE(result->is_int()) << base::Value::GetTypeName(result->type());
EXPECT_EQ(0, result->GetInt());
}
}
class V8ValueConverterOverridingStrategyForTesting
: public V8ValueConverter::Strategy {
public:
V8ValueConverterOverridingStrategyForTesting()
: reference_value_(NewReferenceValue()) {}
bool FromV8Object(v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) override {
*out = NewReferenceValue();
return true;
}
bool FromV8Array(v8::Local<v8::Array> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) override {
*out = NewReferenceValue();
return true;
}
bool FromV8ArrayBuffer(v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) override {
*out = NewReferenceValue();
return true;
}
bool FromV8Number(v8::Local<v8::Number> value,
std::unique_ptr<base::Value>* out) override {
*out = NewReferenceValue();
return true;
}
bool FromV8Undefined(std::unique_ptr<base::Value>* out) override {
*out = NewReferenceValue();
return true;
}
base::Value* reference_value() const { return reference_value_.get(); }
private:
static std::unique_ptr<base::Value> NewReferenceValue() {
return std::make_unique<base::Value>("strategy");
}
std::unique_ptr<base::Value> reference_value_;
};
TEST_F(V8ValueConverterImplTest, StrategyOverrides) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
V8ValueConverterImpl converter;
V8ValueConverterOverridingStrategyForTesting strategy;
converter.SetStrategy(&strategy);
v8::Local<v8::Object> object(v8::Object::New(isolate_));
std::unique_ptr<base::Value> object_value(
converter.FromV8Value(object, context));
ASSERT_TRUE(object_value);
EXPECT_EQ(*strategy.reference_value(), *object_value);
v8::Local<v8::Array> array(v8::Array::New(isolate_));
std::unique_ptr<base::Value> array_value(
converter.FromV8Value(array, context));
ASSERT_TRUE(array_value);
EXPECT_EQ(*strategy.reference_value(), *array_value);
v8::Local<v8::ArrayBuffer> array_buffer(v8::ArrayBuffer::New(isolate_, 0));
std::unique_ptr<base::Value> array_buffer_value(
converter.FromV8Value(array_buffer, context));
ASSERT_TRUE(array_buffer_value);
EXPECT_EQ(*strategy.reference_value(), *array_buffer_value);
v8::Local<v8::ArrayBufferView> array_buffer_view(
v8::Uint8Array::New(array_buffer, 0, 0));
std::unique_ptr<base::Value> array_buffer_view_value(
converter.FromV8Value(array_buffer_view, context));
ASSERT_TRUE(array_buffer_view_value);
EXPECT_EQ(*strategy.reference_value(), *array_buffer_view_value);
v8::Local<v8::Number> number(v8::Number::New(isolate_, 0.0));
std::unique_ptr<base::Value> number_value(
converter.FromV8Value(number, context));
ASSERT_TRUE(number_value);
EXPECT_EQ(*strategy.reference_value(), *number_value);
v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_));
std::unique_ptr<base::Value> undefined_value(
converter.FromV8Value(undefined, context));
ASSERT_TRUE(undefined_value);
EXPECT_EQ(*strategy.reference_value(), *undefined_value);
}
class V8ValueConverterBypassStrategyForTesting
: public V8ValueConverter::Strategy {
public:
bool FromV8Object(v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) override {
return false;
}
bool FromV8Array(v8::Local<v8::Array> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) override {
return false;
}
bool FromV8ArrayBuffer(v8::Local<v8::Object> value,
std::unique_ptr<base::Value>* out,
v8::Isolate* isolate) override {
return false;
}
bool FromV8Number(v8::Local<v8::Number> value,
std::unique_ptr<base::Value>* out) override {
return false;
}
bool FromV8Undefined(std::unique_ptr<base::Value>* out) override {
return false;
}
};
// Verify that having a strategy that fallbacks to default behaviour
// actually preserves it.
TEST_F(V8ValueConverterImplTest, StrategyBypass) {
v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context> context =
v8::Local<v8::Context>::New(isolate_, context_);
v8::Context::Scope context_scope(context);
V8ValueConverterImpl converter;
V8ValueConverterBypassStrategyForTesting strategy;
converter.SetStrategy(&strategy);
v8::Local<v8::Object> object(v8::Object::New(isolate_));
std::unique_ptr<base::Value> object_value(
converter.FromV8Value(object, context));
ASSERT_TRUE(object_value);
const base::Value reference_object_value = base::test::ParseJson("{}");
EXPECT_EQ(reference_object_value, *object_value);
v8::Local<v8::Array> array(v8::Array::New(isolate_));
std::unique_ptr<base::Value> array_value(
converter.FromV8Value(array, context));
ASSERT_TRUE(array_value);
const base::Value reference_array_value = base::test::ParseJson("[]");
EXPECT_EQ(reference_array_value, *array_value);
const char kExampleData[] = {1, 2, 3, 4, 5};
v8::Local<v8::ArrayBuffer> array_buffer(
v8::ArrayBuffer::New(isolate_, sizeof(kExampleData)));
memcpy(array_buffer->GetBackingStore()->Data(), kExampleData,
sizeof(kExampleData));
std::unique_ptr<base::Value> binary_value(
converter.FromV8Value(array_buffer, context));
ASSERT_TRUE(binary_value);
base::Value reference_binary_value(base::as_byte_span(kExampleData));
EXPECT_EQ(reference_binary_value, *binary_value);
v8::Local<v8::ArrayBufferView> array_buffer_view(
v8::Uint8Array::New(array_buffer, 1, 3));
std::unique_ptr<base::Value> binary_view_value(
converter.FromV8Value(array_buffer_view, context));
ASSERT_TRUE(binary_view_value);
base::Value reference_binary_view_value(
base::as_byte_span(kExampleData).subspan<1, 3>());
EXPECT_EQ(reference_binary_view_value, *binary_view_value);
v8::Local<v8::Number> number(v8::Number::New(isolate_, 0.0));
std::unique_ptr<base::Value> number_value(
converter.FromV8Value(number, context));
ASSERT_TRUE(number_value);
const base::Value reference_number_value = base::test::ParseJson("0");
EXPECT_EQ(reference_number_value, *number_value);
v8::Local<v8::Primitive> undefined(v8::Undefined(isolate_));
std::unique_ptr<base::Value> undefined_value(
converter.FromV8Value(undefined, context));
EXPECT_FALSE(undefined_value);
}
} // namespace content
|