mirror of
https://github.com/status-im/react-native.git
synced 2025-02-24 07:08:27 +00:00
Fix value.cpp tests and add tests for bad utf8/16
Reviewed By: mhorowitz Differential Revision: D3699086 fbshipit-source-id: f8e4628a324602c786d6dfeb8f55736f9fc757cd
This commit is contained in:
parent
c507a05f70
commit
77e533cb9f
@ -1,31 +1,50 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#include <string>
|
||||
#include <gtest/gtest.h>
|
||||
#include <folly/json.h>
|
||||
|
||||
#undef ASSERT
|
||||
#include <JavaScriptCore/config.h>
|
||||
#include <cxxreact/Value.h>
|
||||
#include "OpaqueJSString.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace facebook;
|
||||
using namespace facebook::react;
|
||||
|
||||
// TODO(cjhopman): Fix these tests.
|
||||
/*
|
||||
#ifdef ANDROID
|
||||
#include <android/looper.h>
|
||||
void prepare() {
|
||||
ALooper_prepare(0);
|
||||
}
|
||||
#else
|
||||
void prepare() {
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(Value, Undefined) {
|
||||
JSContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
||||
prepare();
|
||||
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
||||
Value v(ctx, JSValueMakeUndefined(ctx));
|
||||
auto s = String::adopt(JSValueToStringCopy(ctx, v, nullptr));
|
||||
auto s = react::String::adopt(JSValueToStringCopy(ctx, v, nullptr));
|
||||
EXPECT_EQ("undefined", s.str());
|
||||
JSGlobalContextRelease(ctx);
|
||||
}
|
||||
|
||||
TEST(Value, FromJSON) {
|
||||
JSContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
||||
String s("{\"a\": 4}");
|
||||
prepare();
|
||||
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
||||
react::String s("{\"a\": 4}");
|
||||
Value v(Value::fromJSON(ctx, s));
|
||||
EXPECT_TRUE(JSValueIsObject(ctx, v));
|
||||
JSGlobalContextRelease(ctx);
|
||||
}
|
||||
|
||||
TEST(Value, ToJSONString) {
|
||||
JSContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
||||
String s("{\"a\": 4}");
|
||||
prepare();
|
||||
JSGlobalContextRef ctx = JSGlobalContextCreateInGroup(nullptr, nullptr);
|
||||
react::String s("{\"a\": 4}");
|
||||
Value v(Value::fromJSON(ctx, s));
|
||||
folly::dynamic dyn = folly::parseJson(v.toJSONString());
|
||||
ASSERT_NE(nullptr, dyn);
|
||||
@ -35,6 +54,50 @@ TEST(Value, ToJSONString) {
|
||||
ASSERT_TRUE(val.isInt());
|
||||
EXPECT_EQ(4, val.getInt());
|
||||
EXPECT_EQ(4.0f, val.asDouble());
|
||||
|
||||
JSGlobalContextRelease(ctx);
|
||||
}
|
||||
|
||||
*/
|
||||
// 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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user