Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions IntegrationTests/CryptoTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/

'use strict';

declare var crypto: {getRandomValues: (data: Uint8Array) => Uint8Array};

const React = require('react');
const ReactNative = require('react-native');
const {View} = ReactNative;
const {TestModule} = ReactNative.NativeModules;

class CryptoTest extends React.Component<{}> {
componentDidMount() {
const data = new Uint8Array(8);
const returnValue = crypto.getRandomValues(data);

const returnsArray = data === returnValue;
const populatesData = data.find(value => value !== 0) !== undefined;

TestModule.markTestPassed(returnsArray && populatesData);
}

render(): React.Node {
return <View />;
}
}

CryptoTest.displayName = 'CryptoTest';

module.exports = CryptoTest;
1 change: 1 addition & 0 deletions IntegrationTests/IntegrationTestsApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const TESTS = [
require('./SimpleSnapshotTest'),
require('./ImageCachePolicyTest'),
require('./ImageSnapshotTest'),
require('./CryptoTest'),
require('./PromiseTest'),
require('./WebViewTest'),
require('./SyncMethodTest'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ - (void)testTheTester_ExpectError
//RCT_TEST(LayoutEventsTest) // Disabled due to flakiness: #8686784
RCT_TEST(SimpleSnapshotTest)
RCT_TEST(SyncMethodTest)
RCT_TEST(CryptoTest)
RCT_TEST(PromiseTest)
RCT_TEST_ONLY_WITH_PACKAGER(WebSocketTest)
RCT_TEST(AccessibilityManagerTest)
Expand Down
12 changes: 12 additions & 0 deletions React/React.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@
14F484561AABFCE100FDF6B9 /* RCTSliderManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 14F484551AABFCE100FDF6B9 /* RCTSliderManager.m */; };
14F7A0EC1BDA3B3C003C6C10 /* RCTPerfMonitor.m in Sources */ = {isa = PBXBuildFile; fileRef = 14F7A0EB1BDA3B3C003C6C10 /* RCTPerfMonitor.m */; };
14F7A0F01BDA714B003C6C10 /* RCTFPSGraph.m in Sources */ = {isa = PBXBuildFile; fileRef = 14F7A0EF1BDA714B003C6C10 /* RCTFPSGraph.m */; };
15698CC82125A6B300EE2C0D /* Crypto.h in Headers */ = {isa = PBXBuildFile; fileRef = 15698CC62125A6B200EE2C0D /* Crypto.h */; };
15698CC92125A6B300EE2C0D /* Crypto.h in Headers */ = {isa = PBXBuildFile; fileRef = 15698CC62125A6B200EE2C0D /* Crypto.h */; };
15698CCA2125A6B300EE2C0D /* Crypto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 15698CC72125A6B300EE2C0D /* Crypto.cpp */; };
15698CCB2125A6B300EE2C0D /* Crypto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 15698CC72125A6B300EE2C0D /* Crypto.cpp */; };
191E3EBE1C29D9AF00C180A6 /* RCTRefreshControlManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 191E3EBD1C29D9AF00C180A6 /* RCTRefreshControlManager.m */; };
191E3EC11C29DC3800C180A6 /* RCTRefreshControl.m in Sources */ = {isa = PBXBuildFile; fileRef = 191E3EC01C29DC3800C180A6 /* RCTRefreshControl.m */; };
199B8A6F1F44DB16005DEF67 /* RCTVersion.h in Headers */ = {isa = PBXBuildFile; fileRef = 199B8A6E1F44DB16005DEF67 /* RCTVersion.h */; };
Expand Down Expand Up @@ -2068,6 +2072,8 @@
14F7A0EB1BDA3B3C003C6C10 /* RCTPerfMonitor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPerfMonitor.m; sourceTree = "<group>"; };
14F7A0EE1BDA714B003C6C10 /* RCTFPSGraph.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = RCTFPSGraph.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; };
14F7A0EF1BDA714B003C6C10 /* RCTFPSGraph.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTFPSGraph.m; sourceTree = "<group>"; };
15698CC62125A6B200EE2C0D /* Crypto.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Crypto.h; sourceTree = "<group>"; };
15698CC72125A6B300EE2C0D /* Crypto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Crypto.cpp; sourceTree = "<group>"; };
191E3EBC1C29D9AF00C180A6 /* RCTRefreshControlManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTRefreshControlManager.h; sourceTree = "<group>"; };
191E3EBD1C29D9AF00C180A6 /* RCTRefreshControlManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTRefreshControlManager.m; sourceTree = "<group>"; };
191E3EBF1C29DC3800C180A6 /* RCTRefreshControl.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTRefreshControl.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2759,6 +2765,8 @@
3D4A621D1DDD3985001F41B4 /* jschelpers */ = {
isa = PBXGroup;
children = (
15698CC72125A6B300EE2C0D /* Crypto.cpp */,
15698CC62125A6B200EE2C0D /* Crypto.h */,
19DED2281E77E29200F089BB /* systemJSCWrapper.cpp */,
3D7A27DC1DE32541002E3F95 /* JavaScriptCore.h */,
3D92B1071E0369AD0018521A /* JSCHelpers.cpp */,
Expand Down Expand Up @@ -3376,6 +3384,7 @@
19F61C041E8495FF00571D81 /* JSCHelpers.h in Headers */,
19F61C051E8495FF00571D81 /* noncopyable.h in Headers */,
19F61C061E8495FF00571D81 /* Unicode.h in Headers */,
15698CC92125A6B300EE2C0D /* Crypto.h in Headers */,
19F61C071E8495FF00571D81 /* Value.h in Headers */,
3D3030251DF8295E00D6DDAE /* JavaScriptCore.h in Headers */,
3D3030261DF8295E00D6DDAE /* JSCWrapper.h in Headers */,
Expand Down Expand Up @@ -3423,6 +3432,7 @@
13EBC6821E28733C00880AC5 /* Value.h in Headers */,
13EBC6811E28733C00880AC5 /* Unicode.h in Headers */,
13EBC6801E28733C00880AC5 /* noncopyable.h in Headers */,
15698CC82125A6B300EE2C0D /* Crypto.h in Headers */,
13EBC67E1E28726000880AC5 /* JSCHelpers.h in Headers */,
3D3CD93D1DE5FC1400167DC4 /* JavaScriptCore.h in Headers */,
3D3CD93E1DE5FC1400167DC4 /* JSCWrapper.h in Headers */,
Expand Down Expand Up @@ -4387,6 +4397,7 @@
13EBC6731E2870DE00880AC5 /* Value.cpp in Sources */,
13EBC67D1E28725900880AC5 /* JSCHelpers.cpp in Sources */,
135A9C021E7B0F4800587AEB /* systemJSCWrapper.cpp in Sources */,
15698CCA2125A6B300EE2C0D /* Crypto.cpp in Sources */,
13EBC67B1E28723000880AC5 /* Unicode.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -4399,6 +4410,7 @@
13EBC6781E2870E400880AC5 /* JSCWrapper.cpp in Sources */,
13EBC6771E2870E400880AC5 /* JSCHelpers.cpp in Sources */,
135A9C011E7B0F4700587AEB /* systemJSCWrapper.cpp in Sources */,
15698CCB2125A6B300EE2C0D /* Crypto.cpp in Sources */,
13EBC67A1E2870E400880AC5 /* Value.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.tests;

import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.modules.appstate.AppStateModule;
import com.facebook.react.modules.core.ReactChoreographer;
import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
import com.facebook.react.testing.FakeWebSocketModule;
import com.facebook.react.testing.ReactIntegrationTestCase;
import com.facebook.react.testing.ReactTestHelper;
import com.facebook.react.testing.StringRecordingModule;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.views.view.ReactViewManager;
import java.util.Arrays;
import java.util.List;

/**
* Test crypto-based functionality of JS VM
*/
public class JSCryptoTest extends ReactIntegrationTestCase {

private interface TestJSCryptoModule extends JavaScriptModule {
void getRandomValues();
}

StringRecordingModule mStringRecordingModule;

private CatalystInstance mInstance;

@Override
protected void setUp() throws Exception {
super.setUp();

List<ViewManager> viewManagers = Arrays.<ViewManager>asList(
new ReactViewManager());
final UIManagerModule mUIManager = new UIManagerModule(
getContext(),
viewManagers,
0);
UiThreadUtil.runOnUiThread(
new Runnable() {
@Override
public void run() {
ReactChoreographer.initialize();
mUIManager.onHostResume();
}
});
waitForIdleSync();

mStringRecordingModule = new StringRecordingModule();
mInstance = ReactTestHelper.catalystInstanceBuilder(this)
.addNativeModule(mStringRecordingModule)
.addNativeModule(mUIManager)
.addNativeModule(new DeviceInfoModule(getContext()))
.addNativeModule(new AppStateModule(getContext()))
.addNativeModule(new FakeWebSocketModule())
.build();
}

public void testGetRandomValues() {
TestJSCryptoModule testModule = mInstance.getJSModule(TestJSCryptoModule.class);
waitForBridgeAndUIIdle();

testModule.getRandomValues();
waitForBridgeAndUIIdle();

String[] answers = mStringRecordingModule.getCalls().toArray(new String[0]);
assertEquals("true", answers[0]);
}
}
1 change: 1 addition & 0 deletions ReactAndroid/src/androidTest/js/TestBundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ console.disableYellowBox = true;
require('ProgressBarTestModule');
require('ViewRenderingTestModule');
require('TestJavaToJSArgumentsModule');
require('TestJSCryptoModule');
require('TestJSLocaleModule');
require('TestJSToJavaParametersModule');
require('TestJavaToJSReturnValuesModule');
Expand Down
31 changes: 31 additions & 0 deletions ReactAndroid/src/androidTest/js/TestJSCryptoModule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Copyright (c) 2018-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/

'use strict';

/* global crypto */

var BatchedBridge = require('BatchedBridge');
var Recording = require('NativeModules').Recording;

var TestJSCryptoModule = {
getRandomValues: function() {
const data = new Uint8Array(8);
const returnValue = crypto.getRandomValues(data);

const returnsArray = data === returnValue;
const populatesData = data.find(value => value !== 0) !== undefined;

Recording.record(returnsArray && populatesData ? 'true' : 'false');
},
};

BatchedBridge.registerCallableModule('TestJSCryptoModule', TestJSCryptoModule);

module.exports = TestJSCryptoModule;
5 changes: 5 additions & 0 deletions ReactCommon/cxxreact/JSCExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ JSCExecutor::JSCExecutor(
"nativeModuleProxy",
exceptionWrapMethod<&JSCExecutor::getNativeModule>());
}

{
SystraceSection s("nativeModuleCrypto object");
installGlobalCrypto(m_context);
}
}

JSCExecutor::~JSCExecutor() {
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ include $(CLEAR_VARS)
LOCAL_MODULE := jschelpers

LOCAL_SRC_FILES := \
Crypto.cpp \
JSCHelpers.cpp \
Unicode.cpp \
Value.cpp \
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/jschelpers/BUCK
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
load("//ReactNative:DEFS.bzl", "ANDROID", "ANDROID_JSC_INTERNAL_DEPS", "APPLE", "APPLE_JSC_INTERNAL_DEPS", "react_native_xplat_target", "rn_xplat_cxx_library")

EXPORTED_HEADERS = [
"Crypto.h",
"JavaScriptCore.h",
"JSCHelpers.h",
"JSCWrapper.h",
Expand Down
Loading