Skip to content

Commit

Permalink
Refactor and junitize
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkarnk committed Aug 7, 2024
1 parent fd011c8 commit 0b00947
Show file tree
Hide file tree
Showing 122 changed files with 547 additions and 2,264 deletions.
42 changes: 16 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,44 @@
#

JAVA_HOME := /usr/lib/jvm/java-21-openjdk-amd64/
BUILD := ${PWD}/build
TOPDIR := $(shell pwd)
BUILD := ${TOPDIR}/build

# Vars for compiling Java sources
JAVA_SRC := src/java/com/canonical/openssl
JAVA_SRC := src/main/java/com/canonical/openssl
JAVA_SRC_DIRS := ${JAVA_SRC} ${JAVA_SRC}/drbg ${JAVA_SRC}/keyagreement ${JAVA_SRC}/keyencapsulation ${JAVA_SRC}/mac
JAVA_SRC_DIRS += ${JAVA_SRC}/kdf ${JAVA_SRC}/md ${JAVA_SRC}/signature ${JAVA_SRC}/key ${JAVA_SRC}/cipher
JAVA_SRC_DIRS += ${JAVA_SRC}/provider ${JAVA_SRC}/util
JAVA_FILES := $(wildcard $(addsuffix /*.java, $(JAVA_SRC_DIRS)))
JAVA_FILES = $(wildcard $(addsuffix /*.java, $(JAVA_SRC_DIRS)))

# Vars for compiling the C sources
JSSL_HEADERS := -I${PWD}/src/include/jni -I${PWD}/src/include/native
JSSL_HEADERS := -I${TOPDIR}/src/main/native/include -I${TOPDIR}/src/main/native/include/jni
JNI_HEADERS := -I${JAVA_HOME}/include/linux/ -I${JAVA_HOME}/include/
OPENSSL_HEADERS := -I/usr/local/include/openssl/
INCLUDE_HEADERS := ${JSSL_HEADERS} ${JNI_HEADERS} ${OPENSSL_HEADERS}

NATDIR := ${PWD}/src/native
NATDIR := ${TOPDIR}/src/main/native/c
NATFILES := $(foreach dir,$(NATDIR),$(wildcard $(dir)/*.c))
NATOBJS := $(patsubst $(NATDIR)/%.c, $(BUILD)/bin/%.o, $(NATFILES))
OBJS := $(patsubst $(NATDIR)/%.c, $(BUILD)/bin/%.o, $(NATFILES))

JNIDIR := ${PWD}/src/jni
JNIFILES := $(foreach dir,$(JNIDIR),$(wildcard $(dir)/*.c))
JNIOBJS := $(patsubst $(JNIDIR)/%.c, $(BUILD)/bin/%.o, $(JNIFILES))
OBJS := $(NATOBJS) $(JNIOBJS)

CCFLAGS := ${INCLUDE_HEADERS} -c -fPIC
CCFLAGS := ${INCLUDE_HEADERS} -c -fPIC -g
LDFLAGS := -shared -fPIC -Wl,-soname,libjssl.so
SOLIB := $(BUILD)/bin/libjssl.so

# Vars for compiling the test sources
TEST_BIN := ${PWD}/build/test/bin
TEST_C_DIR := ${PWD}/test/native
TEST_BIN := ${TOPDIR}/build/test/bin
TEST_C_DIR := ${TOPDIR}/src/test/native
TEST_C_SRCS := $(foreach dir,$(TEST_C_DIR),$(wildcard $(dir)/*.c))
TEST_C_OBJS := $(patsubst $(TEST_C_DIR)/%.c,$(TEST_BIN)/%,$(TEST_C_SRCS))
TEST_CFLAGS := ${INCLUDE_HEADERS} -L${BUILD}/bin -L/usr/local/lib64
TEST_JAVA_DIR := ${PWD}/test/java
TEST_JAVA_DIR := ${TOPDIR}/src/test/java
TEST_JAVA_SRCS := $(wildcard $(addsuffix /*.java, $(TEST_JAVA_DIR)))
TEST_NAT_SRCS := $(wildcard $(addsuffix /*.c, $(TEST_JAVA_DIR)/native))
TESTOBJS := $(patsubst $(TEST_JAVA_DIR)/native/%.c, $(TEST_BIN)/%.o, $(TEST_NAT_SRCS))
TESTLIB := $(BUILD)/test/bin/libsigtest.so

LIBPATH := $(BUILD)/bin:${PWD}/build/test/bin
LIBPATH := $(BUILD)/bin:${TOPDIR}/build/test/bin

$(BUILD)/bin:
@mkdir -p $@
Expand Down Expand Up @@ -90,19 +87,12 @@ $(TEST_BIN)/%: $(TEST_C_DIR)/%.c
@cc $(TEST_CFLAGS) -o $@ $< -ljssl -lcrypto

gen-code:
@sh ${PWD}/gen/gen-classes.sh
@sh ${TOPDIR}/gen/gen-classes.sh

java-build: $(BUILD)/classes $(JAVA_FILES)
@${JAVA_HOME}/bin/javac -d $^
solib: $(BUILD)/bin $(SOLIB)

build: $(BUILD)/bin $(SOLIB) gen-code java-build
@cp ${SOLIB} $(BUILD)/classes/resources/native

build-test: $(TEST_JAVA_SRCS) $(BUILD)/test/bin $(TESTLIB) $(TEST_C_OBJS)
@${JAVA_HOME}/bin/javac -cp build/classes -d build/test/classes $(TEST_JAVA_SRCS)

test: build build-test
@LD_LIBRARY_PATH=$(BUILD)/bin:$(BUILD)/test JAVA_HOME=${JAVA_HOME} LIBPATH=${LIBPATH} test/runner.py
test-solib: $(BUILD)/test/bin $(TESTLIB) $(TEST_C_OBJS)
@LD_LIBRARY_PATH=$(BUILD)/bin:$(BUILD)/test LIBPATH=${LIBPATH} src/test/runner.py

clean:
@rm -rf build && rm -f ${JAVA_SRC}/cipher/AES*.java
2 changes: 1 addition & 1 deletion gen/gen-classes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ do
do
pad=$line
pad_c=$(echo $pad | tr "." _ | tr "-" _)
path=${PWD}/src/java/com/canonical/openssl/cipher/AES${ks}with${mode}padding${pad_c}.java
path=${PWD}/src/main/java/com/canonical/openssl/cipher/AES${ks}with${mode}padding${pad_c}.java
cp ${PWD}/gen/template.java ${path}
sed -i 's/__PADC__/'$pad_c'/g' ${path}
sed -i 's/__KS__/'$ks'/g' ${path}
Expand Down
107 changes: 93 additions & 14 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,68 +31,147 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<!-- Jar Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<classesDirectory>${project.basedir}/build/classes</classesDirectory>
</configuration>
<version>3.4.2</version>
</plugin>
<plugin>
<!-- Plugin to help run make commands -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<executions>
<execution>
<id>compile-c</id>
<phase>compile</phase>
<id>generate-sources</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>clean</argument>
<argument>build</argument>
<argument>gen-code</argument>
</arguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<id>generate-resources</id>
<phase>generate-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>clean</argument>
<argument>solib</argument>
</arguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
<execution>
<id>test</id>
<phase>test</phase>
<id>generate-test-resources</id>
<phase>generate-test-resources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>test-solib</argument>
</arguments>
</configuration>
</execution>
<execution>
<id>clean</id>
<phase>clean</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>make</executable>
<arguments>
<argument>clean</argument>
<argument>test</argument>
</arguments>
<workingDirectory>${project.basedir}</workingDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-files</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/classes/resources/native</outputDirectory>
<resources>
<resource>
<directory>${project.basedir}/build/bin</directory>
<includes>
<include>libjssl.so</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!-- Plugin to help run JUNIT tests -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<environmentVariables>
<LD_LIBRARY_PATH>
${env.LD_LIBRARY_PATH}:${project.basedir}/build/test/bin:${project.basedir}/build/bin
</LD_LIBRARY_PATH>
</environmentVariables>

<includes>
<include>**/*Test.java</include>
</includes>
<excludes>
<exclude>**/*SecureRandomTest.java</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
File renamed without changes.
21 changes: 17 additions & 4 deletions src/native/drbg.c → src/main/native/c/drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ DRBG* create_DRBG_with_params(const char* name, DRBG* parent, DRBGParams *drbg_p
}

EVP_RAND_CTX * context = EVP_RAND_CTX_new(rand, parent == NULL ? NULL : parent->context);
EVP_RAND_free(rand);
if (NULL == context) {
EVP_RAND_free(rand);
fprintf(stderr, "Couldn't allocate EVP_RAND_CTX\n");
return NULL;
}
Expand Down Expand Up @@ -100,13 +100,26 @@ int free_DRBGParams(DRBGParams *params) {
}

int free_DRBG(DRBG *generator) {
FREE_IF_NON_NULL(generator->seed);
EVP_RAND_CTX_free(generator->context);
if (generator->params != NULL)
if (generator == NULL) {
return 0;
}

FREE_IF_NON_NULL(generator->seed);
if (generator->context != NULL) {
EVP_RAND_CTX_free(generator->context);
generator->context = NULL;
}

if (generator->params != NULL) {
free_DRBGParams(generator->params);
generator->params = NULL;
}

if (generator->parent != NULL) {
free_DRBG(generator->parent);
generator->parent = NULL;
}

free(generator);
return 1;
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 0b00947

Please sign in to comment.