From 9b7f943969e17273ac9bd78bb238ffbea3865993 Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Tue, 20 Aug 2024 23:21:06 +0200 Subject: Support building libsamba.so for unittests Unittests still fail as there is no samba server to talk to (step 2). --- libs/samba/CMakeLists.txt | 9 +++-- libs/samba/build.gradle.kts | 39 ++++++++++++++++++++++ libs/samba/src/.gitignore | 2 ++ libs/samba/src/main/cpp/jni.cpp | 26 +++++++++++++++ libs/samba/src/main/cpp/samba.cpp | 1 + .../org/the_jk/cleversync/samba/SambaTreeTest.kt | 39 ++++++++++++++++++++++ 6 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 libs/samba/src/.gitignore create mode 100644 libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt (limited to 'libs') diff --git a/libs/samba/CMakeLists.txt b/libs/samba/CMakeLists.txt index 66f6cd7..d47113a 100644 --- a/libs/samba/CMakeLists.txt +++ b/libs/samba/CMakeLists.txt @@ -20,6 +20,13 @@ include(cmake/ConfigureChecks.cmake) add_subdirectory(libsmb2/lib) +if (ANDROID) + find_library(log-lib log) +else() + find_package(JNI) + include_directories(${JNI_INCLUDE_DIRS}) +endif() + add_library( samba SHARED @@ -28,6 +35,4 @@ add_library( src/main/cpp/samba.cpp ) -find_library(log-lib log) - target_link_libraries(samba smb2 ${log-lib}) diff --git a/libs/samba/build.gradle.kts b/libs/samba/build.gradle.kts index 79827ee..2fc5007 100644 --- a/libs/samba/build.gradle.kts +++ b/libs/samba/build.gradle.kts @@ -24,3 +24,42 @@ android { dependencies { implementation(project(":libs:io")) } + +listOf("Debug", "Release").forEach { buildType -> + val buildDir = project.layout.buildDirectory.dir("test/debug/build") + + val configure by tasks.register( + "configureLibsFor${buildType}UnitTest", + Exec::class + ) { + args( + "-S", + project.layout.projectDirectory.dir("."), + "-B", + buildDir.get() + ) + executable = "cmake" + } + + val compile by tasks.register( + "compileLibsFor${buildType}UnitTest", + Exec::class + ) { + dependsOn(configure) + args("--build", buildDir.get()) + executable = "cmake" + } + + val copy by tasks.register( + "copyLibsFor${buildType}UnitTest", + Copy::class + ) { + dependsOn(compile) + from(buildDir.map { it.file("libsamba.so") }) + into(project.layout.projectDirectory.dir("src/test${buildType}/jniLibs")) + } + + tasks.matching { it.name == "test${buildType}UnitTest" }.all { + dependsOn(copy) + } +} diff --git a/libs/samba/src/.gitignore b/libs/samba/src/.gitignore new file mode 100644 index 0000000..2b22da4 --- /dev/null +++ b/libs/samba/src/.gitignore @@ -0,0 +1,2 @@ +testDebug/ +testRelease/ diff --git a/libs/samba/src/main/cpp/jni.cpp b/libs/samba/src/main/cpp/jni.cpp index 30f03a9..aac1d28 100644 --- a/libs/samba/src/main/cpp/jni.cpp +++ b/libs/samba/src/main/cpp/jni.cpp @@ -1,6 +1,10 @@ #include "jni.hpp" +#ifdef ANDROID #include +#else +#include +#endif namespace { @@ -35,7 +39,12 @@ namespace internal { void _abort_if_not_ok(const char *file, int line, jint ret) { if (ret == JNI_OK) [[likely]] return; +#ifdef ANDROID __android_log_assert(nullptr, "jni", "JNI error: %s", _jni_error(ret)); +#else + std::cerr << "JNI error: " << _jni_error(ret) << std::endl; + abort(); +#endif } void _abort_with_exception(const char* file, int line, JNIEnv* env) { @@ -51,22 +60,39 @@ void _abort_with_exception(const char* file, int line, JNIEnv* env) { auto description = jni::CallObjectMethod(env, throwable, throwable_toString); auto str = jni::StringToUTF8(env, description); +#ifdef ANDROID __android_log_assert(nullptr, "jni", "JNI error: %s", str.c_str()); +#else + std::cerr << "JNI error: " << str << std::endl; + abort(); +#endif } } env->ExceptionClear(); +#ifdef ANDROID __android_log_assert(nullptr, "jni", "Unexpected NULL but no exception"); +#else + std::cerr << "Unexpected NULL but no exception" << std::endl; + abort(); +#endif } } } // namespace internal JNIEnv* AttachCurrentThread() { +#ifdef ANDROID JNIEnv* env; auto ret = g_vm->AttachCurrentThread(&env, nullptr); ABORT_IF_NOT_OK(ret); return env; +#else + void* v_env; + auto ret = g_vm->AttachCurrentThread(&v_env, nullptr); + ABORT_IF_NOT_OK(ret); + return reinterpret_cast(v_env); +#endif } JNIEnv* OnLoad(JavaVM* vm) { diff --git a/libs/samba/src/main/cpp/samba.cpp b/libs/samba/src/main/cpp/samba.cpp index 7b83eb4..7266a7f 100644 --- a/libs/samba/src/main/cpp/samba.cpp +++ b/libs/samba/src/main/cpp/samba.cpp @@ -1,3 +1,4 @@ +#include #include #include #include diff --git a/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt b/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt new file mode 100644 index 0000000..3f1fa04 --- /dev/null +++ b/libs/samba/src/test/java/org/the_jk/cleversync/samba/SambaTreeTest.kt @@ -0,0 +1,39 @@ +package org.the_jk.cleversync.samba + +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import org.robolectric.annotation.Config +import org.the_jk.cleversync.io.samba.SambaCredentials + +@Config(manifest=Config.NONE) +@RunWith(RobolectricTestRunner::class) +class SambaTreeTest { + private lateinit var uri: String + private lateinit var credentials: SambaCredentials + + @Before + fun setUp() { + uri = "smb://127.0.0.1:10445/" + credentials = SambaCredentials("test", "notverysecret") + } + + @After + fun tearDown() { + + } + + @Test + fun listRoot() { + val result = SambaTreeFactory.tree(uri, credentials) + assertThat(result.isSuccess).isTrue() + val root = result.getOrThrow() + val content = root.list() + assertThat(content.directories).hasSize(1) + assertThat(content.files).isEmpty() + assertThat(content.links).isEmpty() + } +} -- cgit v1.2.3-70-g09d2