Unit test

This commit is contained in:
Nullptr 2022-02-18 11:01:39 +08:00
parent 1c83feb1a5
commit cb6e930e3f
11 changed files with 188 additions and 1 deletions

View File

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@ -34,6 +34,13 @@ android {
buildFeatures {
prefab = true
prefabPublishing = true
}
prefab {
create("lsplant") {
headers = "jni/include"
}
}
defaultConfig {

View File

@ -9,4 +9,5 @@ dependencyResolutionManagement {
rootProject.name = "LSPlant"
include(
":library",
":test"
)

1
test/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

57
test/build.gradle.kts Normal file
View File

@ -0,0 +1,57 @@
plugins {
id("com.android.application")
}
val androidTargetSdkVersion: Int by rootProject.extra
val androidMinSdkVersion: Int by rootProject.extra
val androidBuildToolsVersion: String by rootProject.extra
val androidCompileSdkVersion: Int by rootProject.extra
val androidCompileNdkVersion: String by rootProject.extra
android {
namespace = "org.lsposed.lsplant.test"
compileSdk = androidCompileSdkVersion
ndkVersion = androidCompileNdkVersion
buildToolsVersion = androidBuildToolsVersion
buildFeatures {
prefab = true
}
defaultConfig {
applicationId = "org.lsposed.lsplant.test"
minSdk = androidMinSdkVersion
targetSdk = androidTargetSdkVersion
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
}
externalNativeBuild {
cmake {
path("src/main/jni/CMakeLists.txt")
}
}
buildTypes {
release {
isMinifyEnabled = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
}
dependencies {
implementation(project(":library"))
implementation("io.github.vvb2060.ndk:dobby:1.2")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("com.android.support.test:runner:1.0.2")
androidTestImplementation("com.android.support.test.espresso:espresso-core:3.0.2")
}

21
test/proguard-rules.pro vendored Normal file
View File

@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@ -0,0 +1,17 @@
package org.lsposed.lsplant;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
public class UnitTest {
@Test
public void initTest() {
boolean result = LSPTest.initHooker();
Assert.assertTrue(result);
}
}

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:label="LSPlant"
tools:ignore="MissingApplicationIcon" />
</manifest>

View File

@ -0,0 +1,10 @@
package org.lsposed.lsplant;
public class LSPTest {
static {
System.loadLibrary("test");
}
native static boolean initHooker();
}

View File

@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.18.1)
project("lsplant_test")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_library(test SHARED test.cpp)
find_package(dobby REQUIRED CONFIG)
find_package(library REQUIRED CONFIG)
target_link_libraries(test log dobby::dobby library::lsplant)

View File

@ -0,0 +1,55 @@
#include <dobby.h>
#include <lsplant.hpp>
#include <sys/mman.h>
#define _uintval(p) reinterpret_cast<uintptr_t>(p)
#define _ptr(p) reinterpret_cast<void *>(p)
#define _align_up(x, n) (((x) + ((n) - 1)) & ~((n) - 1))
#define _align_down(x, n) ((x) & -(n))
#define _page_size 4096
#define _page_align(n) _align_up(static_cast<uintptr_t>(n), _page_size)
#define _ptr_align(x) _ptr(_align_down(reinterpret_cast<uintptr_t>(x), _page_size))
#define _make_rwx(p, n) ::mprotect(_ptr_align(p), \
_page_align(_uintval(p) + n) != _page_align(_uintval(p)) ? _page_align(n) + _page_size : _page_align(n), \
PROT_READ | PROT_WRITE | PROT_EXEC)
bool init_result;
void* InlineHooker(void* target, void* hooker) {
_make_rwx(target, _page_size);
void* origin_call;
if (DobbyHook(target, hooker, &origin_call) == RS_SUCCESS) {
return origin_call;
} else {
return nullptr;
}
}
bool InlineUnhooker(void* func) {
return DobbyDestroy(func) == RT_SUCCESS;
}
void* ArtSymbolResolver(std::string_view symbol_name) {
return DobbySymbolResolver("libart.so", symbol_name.data());
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_org_lsposed_lsplant_LSPTest_initHooker(JNIEnv*, jclass) {
return init_result;
}
JNIEXPORT jint JNICALL
JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}
lsplant::InitInfo initInfo{
.inline_hooker = InlineHooker,
.inline_unhooker = InlineUnhooker,
.art_symbol_resolver = ArtSymbolResolver
};
init_result = lsplant::Init(env, initInfo);
return JNI_VERSION_1_6;
}