Skip to content

Commit

Permalink
Refactor & Fix Tests (margelo#224)
Browse files Browse the repository at this point in the history
* begin reworking test suites

* dedicated hooks dir, eslint gha fix attempt

* gha updates, fix lint issues from margelo#211

* useRunTests hook working, still fighting mocha suites/tests

* oops

* consistent naming

* fix android

* update podfile.lock

* fixed random,hash tests and upgraded chai

* bump cocoapods version

* bump GHA ruby version

* install gems

* more GHA/ios build fun

* fix basic sign/verify test

* comment out failing public cipher & sign/verify, fix generateKeyPair tests

* cleanup

* cpp lint

* more github actions updates

* finally the big re-work of tests

* js lint

* Test Results screen changes

* docs and cleanup

* better emojis for github

* add/use test fixtures from Node.js

* fix pbkdf2 deriveBits tests

* quote github actions output correctly, duh

* always more stragglers during self-review

* ugh, more coffee

---------

Co-authored-by: Szymon Kapała <[email protected]>
  • Loading branch information
boorad and Szymon20000 committed Feb 7, 2024
1 parent 2cd2f96 commit f596cbc
Show file tree
Hide file tree
Showing 56 changed files with 1,490 additions and 1,143 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ jobs:
name: Build Android Example App
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Setup JDK
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: zulu
java-version: 11
cache: gradle

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT

- name: Restore node_modules from cache
uses: actions/cache@v2
uses: actions/cache@v4
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
Expand All @@ -50,7 +50,7 @@ jobs:
run: yarn install --frozen-lockfile --cwd example

# - name: Restore Gradle cache
# uses: actions/cache@v2
# uses: actions/cache@v4
# with:
# path: |
# ~/.gradle/caches
Expand Down
17 changes: 10 additions & 7 deletions .github/workflows/build-ios.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ jobs:
run:
working-directory: example/ios
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- name: Restore node_modules from cache
uses: actions/cache@v2
uses: actions/cache@v4
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
Expand All @@ -41,18 +41,18 @@ jobs:
run: yarn install --frozen-lockfile --cwd ..

- name: Restore buildcache
uses: mikehardy/buildcache-action@v1
uses: mikehardy/buildcache-action@v2
continue-on-error: true

- name: Setup Ruby (bundle)
uses: ruby/setup-ruby@v1
with:
ruby-version: 2.6
ruby-version: 3.3
bundler-cache: true
working-directory: example/ios

- name: Restore Pods cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: |
example/ios/Pods
Expand All @@ -61,8 +61,11 @@ jobs:
key: ${{ runner.os }}-pods-${{ hashFiles('**/Podfile.lock') }}
restore-keys: |
${{ runner.os }}-pods-
- name: Install Gems
working-directory: example
run: bundle config set deployment 'true' && bundle install
- name: Install Pods
run: bundle exec pod check || bundle exec pod install
run: bundle exec pod install
- name: Install xcpretty
run: gem install xcpretty
- name: Build App
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/validate-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ jobs:
run:
working-directory: ./android
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Setup JDK
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: zulu
java-version: 11
cache: gradle

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- name: Restore node_modules from cache
uses: actions/cache@v2
uses: actions/cache@v4
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
Expand All @@ -48,7 +48,7 @@ jobs:
run: yarn install --frozen-lockfile --cwd ../example

# - name: Restore Gradle cache
# uses: actions/cache@v2
# uses: actions/cache@v4
# with:
# path: |
# ~/.gradle/caches
Expand All @@ -69,7 +69,7 @@ jobs:
# name: Kotlin Lint
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - uses: actions/checkout@v4
# - name: Run KTLint
# uses: mrousavy/[email protected]
# with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/validate-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
name: cpplint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- uses: reviewdog/action-cpplint@master
with:
github_token: ${{ secrets.github_token }}
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/validate-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ jobs:
name: Compile JS (tsc)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install reviewdog
uses: reviewdog/action-setup@v1

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- name: Restore node_modules from cache
uses: actions/cache@v2
uses: actions/cache@v4
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
Expand Down Expand Up @@ -71,13 +71,13 @@ jobs:
name: Lint JS (eslint, prettier)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
run: echo "dir=$(yarn cache dir)" >> $GITHUB_OUTPUT
- name: Restore node_modules from cache
uses: actions/cache@v2
uses: actions/cache@v4
id: yarn-cache
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
Expand Down
8 changes: 3 additions & 5 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
project(react-native-quick-crypto)
cmake_minimum_required(VERSION 3.9.0)
cmake_minimum_required(VERSION 3.10.2)

set(PACKAGE_NAME "reactnativequickcrypto")
set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_CXX_STANDARD 17)

# TODO(osp) remove before release
# set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
# set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g")
set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")

# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
Expand Down
5 changes: 4 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ android {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all -DON_ANDROID -DANDROID"
abiFilters "x86", "x86_64", "armeabi-v7a", "arm64-v8a"
arguments '-DANDROID_STL=c++_shared'
arguments (
'-DANDROID_STL=c++_shared',
"-DANDROID_TOOLCHAIN=clang"
)
}
}
}
Expand Down
26 changes: 16 additions & 10 deletions cpp/Cipher/MGLGenerateKeyPairInstaller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,35 +46,41 @@ FieldDefinition getGenerateKeyPairFieldDefinition(
runtime,
jsi::Function::createFromHostFunction(
runtime, jsi::PropNameID::forAscii(runtime, "executor"), 2,
[arguments, &jsCallInvoker, config](
[&jsCallInvoker, config](
jsi::Runtime &runtime, const jsi::Value &,
const jsi::Value *promiseArgs, size_t) -> jsi::Value {
auto resolve =
std::make_shared<jsi::Value>(runtime, promiseArgs[0]);
auto reject =
std::make_shared<jsi::Value>(runtime, promiseArgs[1]);

std::thread t([&runtime, arguments, resolve, reject,
std::thread t([&runtime, resolve, reject,
jsCallInvoker, config]() {
m.lock();
try {
auto keys = generateRSAKeyPair(runtime, config);
jsCallInvoker->invokeAsync([&runtime, &keys, jsCallInvoker,
resolve]() {
jsCallInvoker->invokeAsync([&runtime, config, resolve]() {
auto keys = generateRSAKeyPair(runtime, config);
auto publicKey = toJSI(runtime, keys.first);
auto privateKey = toJSI(runtime, keys.second);
auto res = jsi::Array::createWithElements(
runtime, jsi::Value::undefined(), publicKey,
privateKey);
runtime,
jsi::Value::undefined(),
publicKey,
privateKey);
resolve->asObject(runtime).asFunction(runtime).call(
runtime, std::move(res));
});
} catch (std::exception e) {
jsCallInvoker->invokeAsync(
[&runtime, &jsCallInvoker, reject]() {
[&runtime, reject]() {
auto res = jsi::Array::createWithElements(
runtime,
jsi::String::createFromUtf8(
runtime, "Error generating key"),
jsi::Value::undefined(),
jsi::Value::undefined());
reject->asObject(runtime).asFunction(runtime).call(
runtime, jsi::String::createFromUtf8(
runtime, "Error generating key"));
runtime, std::move(res));
});
}
m.unlock();
Expand Down
2 changes: 1 addition & 1 deletion cpp/Hash/MGLHashHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void MGLHashHostObject::installMethods() {
if (!arguments[0].isObject() ||
!arguments[0].getObject(runtime).isArrayBuffer(runtime)) {
throw jsi::JSError(runtime,
"HmacHostObject::update: First argument ('message') "
"HashHostObject::update: First argument ('message') "
"has to be of type ArrayBuffer!");
}
auto messageBuffer =
Expand Down
6 changes: 2 additions & 4 deletions cpp/Hash/MGLHashInstaller.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
//
// HMAC-JSI-Installer.m
// Hash-JSI-Installer.m
// PinkPanda
//
// Created by Marc Rousavy on 31.10.21.
//

#include "MGLHashInstaller.h"

#include <openssl/hmac.h>

#include <memory>

#ifdef ANDROID
Expand All @@ -29,7 +27,7 @@ FieldDefinition getHashFieldDefinition(
// createHash(hashAlgorithm: 'sha1' | 'sha256' | 'sha512')
return HOST_LAMBDA("createHash", {
if (count != 1 && count != 2) {
throw jsi::JSError(runtime, "createHmac(..) expects 1-2 arguments!");
throw jsi::JSError(runtime, "createHash(..) expects 1-2 arguments!");
}

auto hashAlgorithm = arguments[0].asString(runtime).utf8(runtime);
Expand Down
2 changes: 1 addition & 1 deletion cpp/MGLKeys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ jsi::Value KeyObjectHandle::Init(jsi::Runtime &rt) {
break;
}
default:
throw jsi::JSError(rt, "invalid keytype for init(): " + type);
throw jsi::JSError(rt, "invalid keytype for init(): " + std::to_string(type));
}

return true;
Expand Down
6 changes: 6 additions & 0 deletions cpp/MGLQuickCryptoHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ MGLQuickCryptoHostObject::MGLQuickCryptoHostObject(
return jsi::Object::createFromHostObject(runtime, hostObject);
}));

// createSign
this->fields.push_back(getSignFieldDefinition(jsCallInvoker, workerQueue));

// createVerify
this->fields.push_back(getVerifyFieldDefinition(jsCallInvoker, workerQueue));

// subtle API created from a simple jsi::Object
// because this FieldDefinition is only good for returning
// objects and too convoluted
Expand Down
2 changes: 0 additions & 2 deletions cpp/Random/MGLRandomHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ MGLRandomHostObject::MGLRandomHostObject(
}

auto result = arguments[0].asObject(runtime).getArrayBuffer(runtime);
auto resultSize = result.size(runtime);
auto *resultData = result.data(runtime);
auto resultPreventGC =
std::make_shared<jsi::ArrayBuffer>(std::move(result));
Expand Down Expand Up @@ -81,7 +80,6 @@ MGLRandomHostObject::MGLRandomHostObject(
}

auto result = arguments[0].asObject(runtime).getArrayBuffer(runtime);
auto resultSize = result.size(runtime);
auto *resultData = result.data(runtime);
auto offset = (int)arguments[1].asNumber();
auto size = arguments[2].asNumber();
Expand Down
2 changes: 1 addition & 1 deletion cpp/Utils/MGLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jsi::Value toJSI(jsi::Runtime& rt, OptionJSVariant& value) {

jsi::Value toJSI(jsi::Runtime& rt, JSVariant& value) {
if (std::holds_alternative<bool>(value)) {
return std::get<bool>(value);
return jsi::Value(std::get<bool>(value));
} else if (std::holds_alternative<int>(value)) {
return jsi::Value(std::get<int>(value));
} else if (std::holds_alternative<long long>(value)) {
Expand Down
1 change: 1 addition & 0 deletions cpp/Utils/MGLUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <string>
#include <utility>
#include <vector>
#include <variant>

namespace margelo {

Expand Down
4 changes: 3 additions & 1 deletion cpp/webcrypto/MGLWebCrypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
#include <memory>
#include <utility>
#include "MGLKeys.h"
#include "MGLUtils.h"

#ifdef ANDROID
#include "JSIUtils/MGLJSIMacros.h"
#include "webcrypto/crypto_ec.h"
#include "Utils/MGLUtils.h"
#else
#include "MGLUtils.h"
#include "MGLJSIMacros.h"
#include "crypto_ec.h"
#endif
Expand Down
4 changes: 3 additions & 1 deletion example/.bundle/config
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
---
BUNDLE_PATH: "vendor/bundle"
BUNDLE_FORCE_RUBY_PLATFORM: 1
BUNDLE_FORCE_RUBY_PLATFORM: "1"
BUNDLE_DEPLOYMENT: "true"
2 changes: 1 addition & 1 deletion example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {
// The version of react-native is set by the React Native Gradle Plugin
implementation("com.facebook.react:react-android")

implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.0.0")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.1.0")

debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
Expand Down
1 change: 0 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:allowBackup="false"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
Expand Down
Loading

0 comments on commit f596cbc

Please sign in to comment.