Skip to content

Commit

Permalink
Merge pull request #715 from bytedance/add-jvmprobe-ut
Browse files Browse the repository at this point in the history
add JVMProbe unittest
  • Loading branch information
yoloyyh authored Nov 29, 2024
2 parents 88aa15a + 0377060 commit 91ee917
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rasp/jvm/JVMProbe/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'java-library'
id 'com.github.johnrengelman.shadow' version '6.0.0'
id 'jacoco'
}

group 'com.security'
Expand All @@ -10,6 +11,29 @@ repositories {
mavenCentral()
}

jacoco {
toolVersion = "0.8.5"
}

test {
useJUnitPlatform()
finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
testImplementation 'org.mockito:mockito-core:4.11.0'
}
dependsOn test // tests are required to run before generating the report
reports {
xml.enabled true
csv.enabled false
html.destination file("${buildDir}/reports/jacoco")
}
}


dependencies {
implementation files('lib/nashorn.jar')
testImplementation group: 'junit', name: 'junit', version: '4.13.1'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import java.io.InputStream;
import java.io.Reader;
import java.util.HashSet;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;


import com.security.smith.log.AttachInfo;
import com.security.smith.log.SmithLogger;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.security.smith.client.MessageSerializer;
import com.security.smith.client.MessageDeserializer;
import com.security.smith.client.MessageDecoder;
import com.security.smith.client.MessageEncoder;
import com.security.smith.type.SmithClass;
import com.security.smith.type.SmithMethod;
import com.security.smith.ruleengine.JsRuleEngine;
import com.security.smith.SmithProbe;
import com.security.smith.client.message.Heartbeat;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.EventFactory;
import com.security.smith.client.message.Trace;
import com.security.smith.SmithProbeProxy;
import com.security.smith.client.Client;
import com.security.smith.client.Rule_Config;
import com.security.smith.client.Rule_Mgr;


public class SmithProbeTest {

@Test
void testInit() {
// Mock dependencies
AttachInfo attachInfoMock = mock(AttachInfo.class);
SmithLogger smithLoggerMock = mock(SmithLogger.class);
MessageSerializer messageSerializerMock = mock(MessageSerializer.class);
MessageEncoder messageEncoderMock = mock(MessageEncoder.class);
MessageDecoder messageDecoderMock = mock(MessageDecoder.class);
Heartbeat heartbeatMock = mock(Heartbeat.class);
Client clientMock = mock(Client.class);
Disruptor<Trace> disruptorMock = mock(Disruptor.class);
Rule_Mgr ruleMgrMock = mock(Rule_Mgr.class);
Rule_Config ruleConfigMock = mock(Rule_Config.class);
SmithProbeProxy smithProbeProxyMock = mock(SmithProbeProxy.class);
JsRuleEngine jsRuleEngineMock = mock(JsRuleEngine.class);

// Create instance of the class under test
SmithProbe yourClass = mock(SmithProbe.class);

// Set up mocks
when(yourClass.getHeartbeat()).thenReturn(heartbeatMock);
when(yourClass.getClient()).thenReturn(clientMock);
when(yourClass.getDisruptor()).thenReturn(disruptorMock);
when(yourClass.getRuleMgr()).thenReturn(ruleMgrMock);
when(yourClass.getRuleConfig()).thenReturn(ruleConfigMock);
when(yourClass.getSmithProxy()).thenReturn(smithProbeProxyMock);
when(yourClass.getJsRuleEngine()).thenReturn(jsRuleEngineMock);

doNothing().when(yourClass).init();


}
}

0 comments on commit 91ee917

Please sign in to comment.