Skip to content

Commit

Permalink
Tests for Profile and Packet + minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
irinil committed Mar 27, 2020
1 parent 3d61611 commit 1162689
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 46 deletions.
21 changes: 18 additions & 3 deletions build.gradle
Expand Up @@ -41,16 +41,31 @@ dependencies {
compile 'org.snmp4j:snmp4j:1.10.1'
compile files('libs/snmp4j-agent-2.3.3.jar')
compile files('libs/VirusTotalAPI.jar')

// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
implementation 'junit:junit:4.12'
// Optional -- Robolectric environment
testImplementation 'androidx.test:core:1.0.0'
// Optional -- Mockito framework
testImplementation 'org.mockito:mockito-core:1.10.19'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support:support-annotations:23.1.0'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'



}

android {
compileSdkVersion 19
compileSdkVersion 28
buildToolsVersion '25.0.0'
defaultConfig {
versionCode 7
versionName "1.3"
minSdkVersion 14
targetSdkVersion 19
targetSdkVersion 28
// signingConfig signingConfigs.config
}
sourceSets {
Expand All @@ -65,7 +80,7 @@ android {
}

// Move the tests to tests/java, tests/res, etc...
androidTest.setRoot('tests')
androidTest.setRoot('test')

// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Mon Feb 10 16:34:29 CET 2020
#Wed Mar 25 02:20:13 EET 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.0-rc-1-all.zip
126 changes: 126 additions & 0 deletions src/androidTest/java/hostage/profile/ProfileTest.java
@@ -0,0 +1,126 @@
package hostage.profile;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.support.test.filters.LargeTest;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.HashMap;

import de.tudarmstadt.informatik.hostage.model.Profile;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;


@RunWith(AndroidJUnit4.class)
public class ProfileTest {
private String mText;
private String mLabel;
private int mId;
private boolean mActivated;
transient private Bitmap mIcon;
transient private int mIconId;
private String mIconName;
private String mIconPath;

private boolean mEditable = false;
private boolean mIsRandom = false;

private HashMap<String, Boolean> mActiveProtocols = new HashMap<String, Boolean>();
private String mGhostPorts = "";
private boolean mGhostActive = false;
private boolean mShowTooltip = false;

Profile profileBitmap;
Profile profilePath;

@Before
public void setUp() {
mText = "androidTest";
mLabel = "testLabel";
mId = 2;
mActivated = true;
mIcon = null;
mIconId = 3;
mIconName = "testIcon";
mIconPath = "testPath";
mEditable = false;
mIsRandom = false;
mActiveProtocols = new HashMap<String, Boolean>();
mActiveProtocols.put("androidTest",true);
mGhostPorts = "";
mGhostActive = false;
mShowTooltip = false;

profileBitmap = new Profile(mId, mLabel,mText, mIcon, mEditable);
profilePath = new Profile(mId, mLabel,mText, mIconPath, mEditable);
}

@Test
public void emptyConstructorTest() {
Profile profile = new Profile();

assertTrue(profile.mEditable);
assertFalse(profile.mActivated);
assertTrue(profile.mId == -1);

}

@Test
public void bitMapConstructorTest(){

assertTrue(profileBitmap.mId == 2) ;
assertEquals("testLabel",profileBitmap.mLabel);
assertTrue(profileBitmap.mText.equals("androidTest"));
assertNull(profileBitmap.mIcon);
assertFalse(profileBitmap.mEditable);

}

@Test
public void pathConstructorTest(){

assertTrue(profilePath.mId == 2) ;
assertEquals("testLabel",profilePath.mLabel);
assertTrue(profilePath.mText.equals("androidTest"));
assertTrue(profilePath.mIconPath.equals("testPath"));
assertFalse(profilePath.mEditable);

}

@Test
public void checkProfileIsParcableTest(){

Parcel parcel = Parcel.obtain();
parcel.writeString(mText);
parcel.writeString(mLabel);
parcel.writeInt(mId);
parcel.writeByte((byte) (mActivated ? 1 : 0));
parcel.writeInt(mIconId);
parcel.writeString(mIconName);
parcel.writeString(mIconPath);
parcel.writeByte((byte) (mEditable ? 1 : 0));
parcel.writeSerializable(mActiveProtocols);
parcel.setDataPosition(0);


Profile parceled = new Profile(parcel);

assertTrue(parceled.mId == 2) ;
assertTrue(parceled.mIconId == 3) ;
assertTrue(parceled.mLabel.equals("testLabel"));
assertTrue(parceled.mText.equals("androidTest"));
assertTrue(parceled.mIconPath.equals("testPath"));
assertTrue(mActivated);
assertFalse(mEditable);
assertFalse(parceled.mEditable);

}
}
19 changes: 19 additions & 0 deletions src/androidTest/java/hostage/wrapper/PacketTest.java
@@ -0,0 +1,19 @@
package hostage.wrapper;

import org.junit.Test;
import de.tudarmstadt.informatik.hostage.wrapper.Packet;
import static org.junit.Assert.assertTrue;

public class PacketTest {
@Test
public void constructorTest(){
String test = "androidTest";
byte[] payload = test.getBytes();
String protocol = "SMB";
Packet packet = new Packet(payload,protocol);
assertTrue(packet.getBytes().equals(payload));
assertTrue(packet.getProtocol().equals(protocol));

}

}
12 changes: 3 additions & 9 deletions src/de/tudarmstadt/informatik/hostage/Hostage.java
Expand Up @@ -67,10 +67,6 @@ public class Hostage extends Service {
private HashMap<String, Boolean> mProtocolActiveAttacks;
private Boolean multistage_service;





MultiStageAlarm alarm = new MultiStageAlarm();

public class LocalBinder extends Binder {
Expand Down Expand Up @@ -139,7 +135,7 @@ public static Context getContext() {
/**
* Receiver for connectivity change broadcast.
*
* @see MainActivity#BROADCAST
* @see MainActivity #BROADCAST
*/
private BroadcastReceiver netReceiver = new BroadcastReceiver() {
@Override
Expand Down Expand Up @@ -251,7 +247,7 @@ public boolean isRunning(String protocolName, int port) {
*
* @param sender
* Source where the event took place.
* @param key
* @param values
* Detailed information about the event.
*/
public void notifyUI(String sender, String[] values) {
Expand Down Expand Up @@ -653,9 +649,7 @@ public boolean hasActiveAttacks(){
* Updates the connection info and saves them in the the SharedPreferences
* for session data.
*
* @param context
* Needs a context to get system resources.
* @see MainActivity#CONNECTION_INFO
* @see MainActivity #CONNECTION_INFO
*/
private void updateConnectionInfo() {
ConnectivityManager connManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
Expand Down
12 changes: 6 additions & 6 deletions src/de/tudarmstadt/informatik/hostage/Listener.java
Expand Up @@ -143,7 +143,7 @@ public void refreshHandlers() {
public void run() {


if (protocol.toString().equals("SMB")) return;
if (protocol.toString().equals("")) return;

//||(protocol.toString().equals("SNMP"))) return;

Expand All @@ -163,7 +163,7 @@ public void run() {
*/
public boolean start() {

if (protocol.toString().equals("SMB")) {
if (protocol.toString().equals("")) {
if (!Device.isPortRedirectionAvailable()) {
/*
We can only use SMB with iptables since we can't transfer UDP sockets using domain sockets (port binder).
Expand Down Expand Up @@ -206,10 +206,10 @@ public boolean start() {
*/
public void stop() {
try {
if (protocol.toString().equals("SMB")) {
((SMB) protocol).stop();

}
// if (protocol.toString().equals("")) {
// ((SMB) protocol).stop();
//
// }

server.close();
thread.interrupt();
Expand Down
@@ -1,7 +1,5 @@
package de.tudarmstadt.informatik.hostage.commons;

import junit.framework.Assert;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
Expand Down Expand Up @@ -58,10 +56,6 @@
*/
public final class HelperUtils {





//Getter and setters for detection of file injection

public static String filePath;
Expand Down
6 changes: 4 additions & 2 deletions src/de/tudarmstadt/informatik/hostage/model/Profile.java
Expand Up @@ -52,8 +52,8 @@ public Profile(){

public Profile(int id, String label, String text, Bitmap icon, boolean editable){
this.mId = id;
this.mLabel = text;
this.mText = label;
this.mLabel = label;
this.mText = text;
this.mActivated = false;
this.mIcon = icon;
this.mEditable = editable;
Expand Down Expand Up @@ -84,6 +84,8 @@ public Profile(Parcel in) {
mIconPath = in.readString();
mEditable = in.readInt() == 1;
mActiveProtocols = (HashMap<String,Boolean>) in.readSerializable();


}

public void setIcon(Bitmap bitmap){
Expand Down
2 changes: 0 additions & 2 deletions src/de/tudarmstadt/informatik/hostage/protocol/HTTP.java
Expand Up @@ -211,8 +211,6 @@ public List<Packet> processMessage(Packet requestPacket) {
}




private void checkProfile() {

String sharedPreferencePath = Hostage.getContext().getString(
Expand Down
11 changes: 0 additions & 11 deletions src/de/tudarmstadt/informatik/hostage/protocol/S7COMM.java
Expand Up @@ -42,10 +42,6 @@ public boolean isSecure() {
public static String UPLOAD="0x1e";
public static String END_UPLOAD="0x1f";





public static final int READ_COILS = 1;
public static final int READ_INPUT_DISCRETES = 2;
public static final int READ_HOLDING_REGISTERS=3;
Expand All @@ -54,8 +50,6 @@ public boolean isSecure() {
public static final int WRITE_SINGLE_REGISTER = 6;




@Override
public List<Packet> processMessage(Packet requestPacket) {

Expand All @@ -65,17 +59,12 @@ public List<Packet> processMessage(Packet requestPacket) {
if (requestPacket != null) {
request = requestPacket.getBytes();


//getRequestType(request);

// responsePackets.add(requestPacket); // Response packets have to be studied yet




responsePackets=processRequest(request,getRequestType(request));


}


Expand Down
1 change: 0 additions & 1 deletion src/de/tudarmstadt/informatik/hostage/protocol/SSH.java
Expand Up @@ -244,7 +244,6 @@ private Packet wrapPacket(byte[] response) {
/**
* Encrypts a request with triple DES.
*
* @param request
* that is encrypted.
* @return encrypted request.
*/
Expand Down
Expand Up @@ -42,9 +42,6 @@
*/
public class PseudoJavaFileDiskDriver extends JavaFileDiskDriver {




private static class PseudoJavaNetworkFile extends JavaNetworkFile {
protected final SMB SMB;
private final SrvSession sess;
Expand Down
Expand Up @@ -74,7 +74,7 @@ private byte[] getBytes() {

/**
* Set the NBDSType.
* @param nbdstype.
* @param nbdstype
*/
public void setNbdstype(int nbdstype) {
this.nbdstype = nbdstype;
Expand Down

0 comments on commit 1162689

Please sign in to comment.