2015-09-14 14:35:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
2016-09-12 23:35:15 +00:00
|
|
|
#include <string>
|
2015-09-14 14:35:58 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <folly/json.h>
|
2016-09-12 23:35:15 +00:00
|
|
|
|
|
|
|
#undef ASSERT
|
|
|
|
#include <JavaScriptCore/config.h>
|
2016-07-25 19:19:46 +00:00
|
|
|
#include <cxxreact/Value.h>
|
2016-09-12 23:35:15 +00:00
|
|
|
#include "OpaqueJSString.h"
|
|
|
|
|
|
|
|
#include <stdexcept>
|
2015-09-14 14:35:58 +00:00
|
|
|
|
|
|
|
using namespace facebook;
|
|
|
|
using namespace facebook::react;
|
|
|
|
|
2016-09-12 23:35:15 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/looper.h>
|
|
|
|
void prepare() {
|
|
|
|
ALooper_prepare(0);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void prepare() {
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-09-14 14:35:58 +00:00
|
|
|
TEST(Value, Undefined) {
|
2016-09-12 23:35:15 +00:00
|
|
|
prepare();
|
|
|
|
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
2015-09-14 14:35:58 +00:00
|
|
|
Value v(ctx, JSValueMakeUndefined(ctx));
|
2016-09-12 23:35:15 +00:00
|
|
|
auto s = react::String::adopt(JSValueToStringCopy(ctx, v, nullptr));
|
2015-09-14 14:35:58 +00:00
|
|
|
EXPECT_EQ("undefined", s.str());
|
2016-09-12 23:35:15 +00:00
|
|
|
JSGlobalContextRelease(ctx);
|
2015-09-14 14:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Value, FromJSON) {
|
2016-09-12 23:35:15 +00:00
|
|
|
prepare();
|
|
|
|
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
|
|
|
react::String s("{\"a\": 4}");
|
2015-09-14 14:35:58 +00:00
|
|
|
Value v(Value::fromJSON(ctx, s));
|
|
|
|
EXPECT_TRUE(JSValueIsObject(ctx, v));
|
2016-09-12 23:35:15 +00:00
|
|
|
JSGlobalContextRelease(ctx);
|
2015-09-14 14:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Value, ToJSONString) {
|
2016-09-12 23:35:15 +00:00
|
|
|
prepare();
|
|
|
|
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
|
|
|
react::String s("{\"a\": 4}");
|
2015-09-14 14:35:58 +00:00
|
|
|
Value v(Value::fromJSON(ctx, s));
|
|
|
|
folly::dynamic dyn = folly::parseJson(v.toJSONString());
|
2016-07-25 19:19:46 +00:00
|
|
|
ASSERT_NE(nullptr, dyn);
|
|
|
|
EXPECT_TRUE(dyn.isObject());
|
|
|
|
auto val = dyn.at("a");
|
2015-09-14 14:35:58 +00:00
|
|
|
ASSERT_NE(nullptr, val);
|
|
|
|
ASSERT_TRUE(val.isInt());
|
2016-07-25 19:19:46 +00:00
|
|
|
EXPECT_EQ(4, val.getInt());
|
|
|
|
EXPECT_EQ(4.0f, val.asDouble());
|
2016-09-12 23:35:15 +00:00
|
|
|
|
|
|
|
JSGlobalContextRelease(ctx);
|
2015-09-14 14:35:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 23:35:15 +00:00
|
|
|
// Just test that handling invalid data doesn't crash.
|
|
|
|
TEST(Value, FromBadUtf8) {
|
|
|
|
prepare();
|
|
|
|
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
|
|
|
// 110xxxxx 10xxxxxx
|
|
|
|
auto dyn = folly::dynamic("\xC0");
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
dyn = folly::dynamic("\xC0\x00");
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
// 1110xxxx 10xxxxxx 10xxxxxx
|
|
|
|
dyn = "\xE0";
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
Value(ctx, Value::fromDynamic(ctx, dyn)).toJSONString();
|
|
|
|
dyn = "\xE0\x00";
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
Value(ctx, Value::fromDynamic(ctx, dyn)).toJSONString();
|
|
|
|
dyn = "\xE0\x00\x00";
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
Value(ctx, Value::fromDynamic(ctx, dyn)).toJSONString();
|
|
|
|
dyn = "\xE0\xA0\x00";
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
// 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
|
|
|
|
dyn = "\xF0";
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
Value(ctx, Value::fromDynamic(ctx, dyn)).toJSONString();
|
|
|
|
dyn = "\xF0\x00\x00\x00";
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
dyn = "\xF0\x80\x80\x00";
|
|
|
|
Value::fromDynamic(ctx, dyn);
|
|
|
|
Value(ctx, Value::fromDynamic(ctx, dyn)).toJSONString();
|
|
|
|
JSGlobalContextRelease(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just test that handling invalid data doesn't crash.
|
|
|
|
TEST(Value, BadUtf16) {
|
|
|
|
prepare();
|
|
|
|
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
|
|
|
UChar buf[] = { 0xDD00, 0xDD00, 0xDD00, 0x1111 };
|
|
|
|
JSStringRef ref = OpaqueJSString::create(buf, 4).leakRef();
|
|
|
|
Value v(ctx, ref);
|
|
|
|
v.toJSONString(0);
|
|
|
|
JSGlobalContextRelease(ctx);
|
|
|
|
}
|