Skip to content

Commit 4913606

Browse files
authored
Merge pull request #25 from RUB-NDS/ModifiableStrings
Modifiable strings
2 parents 360af1e + 4feb068 commit 4913606

File tree

14 files changed

+286
-134
lines changed

14 files changed

+286
-134
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ sudo: false
44

55
jdk:
66
- oraclejdk8
7-
- oraclejdk7
87
- openjdk7
8+
- openjdk8
99

1010
#branches:
1111
# only:
@@ -15,4 +15,4 @@ notifications:
1515
email:
1616
recipients:
1717
- juraj.somorovsky@hackmanit.de
18-
- robert.merget@rub.de
18+
- robert.merget@rub.de

src/main/java/de/rub/nds/modifiablevariable/ModifiableVariableFactory.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import de.rub.nds.modifiablevariable.length.ModifiableLengthField;
1616
import de.rub.nds.modifiablevariable.mlong.ModifiableLong;
1717
import de.rub.nds.modifiablevariable.singlebyte.ModifiableByte;
18+
import de.rub.nds.modifiablevariable.string.ModifiableString;
1819
import java.math.BigInteger;
1920

2021
/**
@@ -50,6 +51,14 @@ public static ModifiableBigInteger safelySetValue(ModifiableBigInteger mv, BigIn
5051
return mv;
5152
}
5253

54+
public static ModifiableString safelySetValue(ModifiableString mv, String value) {
55+
if (mv == null) {
56+
mv = new ModifiableString();
57+
}
58+
mv.setOriginalValue(value);
59+
return mv;
60+
}
61+
5362
public static ModifiableInteger safelySetValue(ModifiableInteger mv, Integer value) {
5463
if (mv == null) {
5564
mv = new ModifiableInteger();

src/main/java/de/rub/nds/modifiablevariable/VariableModification.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import de.rub.nds.modifiablevariable.singlebyte.ByteExplicitValueModification;
3535
import de.rub.nds.modifiablevariable.singlebyte.ByteSubtractModification;
3636
import de.rub.nds.modifiablevariable.singlebyte.ByteXorModification;
37+
import de.rub.nds.modifiablevariable.string.StringExplicitValueModification;
3738
import de.rub.nds.modifiablevariable.util.ArrayConverter;
3839
import javax.xml.bind.annotation.XmlAnyElement;
3940
import javax.xml.bind.annotation.XmlRootElement;
@@ -57,12 +58,11 @@
5758
IntegerShiftLeftModification.class, IntegerShiftRightModification.class, ByteArrayDeleteModification.class,
5859
ByteArrayExplicitValueModification.class, ByteArrayInsertModification.class, ByteArrayXorModification.class,
5960
ByteArrayDuplicateModification.class, ByteArrayShuffleModification.class, ByteAddModification.class,
60-
ByteExplicitValueModification.class, ByteSubtractModification.class, ByteXorModification.class
61-
62-
})
61+
ByteExplicitValueModification.class, ByteSubtractModification.class, ByteXorModification.class,
62+
StringExplicitValueModification.class })
6363
public abstract class VariableModification<E> {
6464

65-
private static final Logger LOGGER = LogManager.getLogger(VariableModification.class);
65+
protected static final Logger LOGGER = LogManager.getLogger(VariableModification.class);
6666

6767
/**
6868
* post modification for next modification executed on the given variable

src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerExplicitValueModification.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public class BigIntegerExplicitValueModification extends VariableModification<Bi
2323
private BigInteger explicitValue;
2424

2525
public BigIntegerExplicitValueModification() {
26-
2726
}
2827

2928
public BigIntegerExplicitValueModification(BigInteger bi) {
@@ -42,4 +41,4 @@ public BigInteger getExplicitValue() {
4241
public void setExplicitValue(BigInteger explicitValue) {
4342
this.explicitValue = explicitValue;
4443
}
45-
}
44+
}

src/main/java/de/rub/nds/modifiablevariable/biginteger/BigIntegerModificationFactory.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010

1111
import de.rub.nds.modifiablevariable.FileConfigurationException;
1212
import de.rub.nds.modifiablevariable.VariableModification;
13+
import de.rub.nds.modifiablevariable.bytearray.ByteArrayModificationFactory;
1314
import de.rub.nds.modifiablevariable.integer.IntegerModificationFactory;
1415
import de.rub.nds.modifiablevariable.util.RandomHelper;
1516
import java.io.BufferedReader;
16-
import java.io.File;
17-
import java.io.FileReader;
1817
import java.io.IOException;
18+
import java.io.InputStream;
19+
import java.io.InputStreamReader;
1920
import java.math.BigInteger;
2021
import java.util.LinkedList;
2122
import java.util.List;
@@ -123,13 +124,13 @@ public static synchronized List<VariableModification<BigInteger>> modificationsF
123124
try {
124125
if (modificationsFromFile == null) {
125126
modificationsFromFile = new LinkedList<>();
126-
ClassLoader classLoader = IntegerModificationFactory.class.getClassLoader();
127-
128-
File file = new File(classLoader.getResource(IntegerModificationFactory.FILE_NAME).getFile());
129-
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
130-
String line;
131-
while ((line = br.readLine()) != null) {
132-
String value = line.trim().split(" ")[0];
127+
ClassLoader classLoader = ByteArrayModificationFactory.class.getClassLoader();
128+
InputStream is = classLoader.getResourceAsStream(IntegerModificationFactory.FILE_NAME);
129+
BufferedReader br = new BufferedReader(new InputStreamReader(is));
130+
String line;
131+
while ((line = br.readLine()) != null) {
132+
String value = line.trim().split(" ")[0];
133+
if (!value.equals("")) {
133134
modificationsFromFile.add(explicitValue(value));
134135
}
135136
}

src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayDeleteModification.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,19 @@ protected byte[] modifyImplementationHook(byte[] input) {
4444
if (start < 0) {
4545
start += input.length;
4646
if (start < 0) {
47-
throw new IllegalArgumentException("Trying to delete from too negative Startposition. start = "
48-
+ (start - input.length));
47+
LOGGER.debug("Trying to delete from too negative Startposition. start = " + (start - input.length));
48+
return input;
4949
}
5050
}
5151
final int endPosition = start + count;
5252
if ((endPosition) > input.length) {
53-
throw new ArrayIndexOutOfBoundsException(String.format(
54-
"Bytes %d..%d cannot be deleted from {%s} of length %d", start, endPosition,
53+
LOGGER.debug(String.format("Bytes %d..%d cannot be deleted from {%s} of length %d", start, endPosition,
5554
bytesToHexString(input), input.length));
55+
return input;
5656
}
5757
if (count <= 0) {
58-
throw new IllegalArgumentException("You must delete at least one byte. count = " + count);
58+
LOGGER.debug("You must delete at least one byte. count = " + count);
59+
return input;
5960
}
6061
byte[] ret1 = Arrays.copyOf(input, start);
6162
byte[] ret2 = null;

src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayInsertModification.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ protected byte[] modifyImplementationHook(byte[] input) {
4646
if (start < 0) {
4747
start += input.length;
4848
if (start < 0) {
49-
throw new IllegalArgumentException("Trying to insert from too negative Startposition. start = "
50-
+ startPosition);
49+
LOGGER.debug("Trying to insert from too negative Startposition. start = " + startPosition);
50+
return input;
5151
}
5252
}
5353
if (startPosition > input.length) {
54-
throw new ArrayIndexOutOfBoundsException("Trying to insert behind the Array. ArraySize:" + input.length
55-
+ " Insert Position:" + startPosition);
54+
LOGGER.debug("Trying to insert behind the Array. ArraySize:" + input.length + " Insert Position:"
55+
+ startPosition);
56+
return input;
5657
}
5758
byte[] ret1 = Arrays.copyOf(input, start);
5859
byte[] ret3 = null;

src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayShuffleModification.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Shuffles the byte array, using a pre-defined array of array pointers
1919
* (#shuffle). Array pointers are currently defined as bytes, since we are
2020
* modifying rather smaller arrays.
21-
*
21+
*
2222
* @author Juraj Somorovsky - juraj.somorovsky@rub.de
2323
*/
2424
@XmlRootElement
@@ -37,6 +37,9 @@ public ByteArrayShuffleModification(byte[] shuffle) {
3737

3838
@Override
3939
protected byte[] modifyImplementationHook(final byte[] input) {
40+
if (input == null) {
41+
return input;
42+
}
4043
byte[] result = input.clone();
4144
int size = input.length;
4245
for (int i = 0; i < shuffle.length - 1; i += 2) {

src/main/java/de/rub/nds/modifiablevariable/bytearray/ByteArrayXorModification.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ protected byte[] modifyImplementationHook(byte[] input) {
4949
if (end > result.length) {
5050
// result = new byte[end];
5151
// System.arraycopy(input, 0, result, 0, input.length);
52-
throw new ArrayIndexOutOfBoundsException(String.format(
52+
LOGGER.debug(String.format(
5353
"Input {%s} of length %d cannot be xored with {%s} of length %d with start position %d",
5454
ArrayConverter.bytesToHexString(input), input.length, ArrayConverter.bytesToHexString(xor),
5555
xor.length, startPosition));
56+
return input;
5657
}
5758
for (int i = 0; i < xor.length; ++i) {
5859
result[start + i] = (byte) (input[start + i] ^ xor[i]);
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2017 Robert Merget <robert.merget@rub.de>.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package de.rub.nds.modifiablevariable.string;
17+
18+
import de.rub.nds.modifiablevariable.ModifiableVariable;
19+
import de.rub.nds.modifiablevariable.VariableModification;
20+
import de.rub.nds.modifiablevariable.mlong.LongAddModification;
21+
import de.rub.nds.modifiablevariable.mlong.LongExplicitValueModification;
22+
import de.rub.nds.modifiablevariable.mlong.LongModificationFactory;
23+
import de.rub.nds.modifiablevariable.mlong.LongSubtractModification;
24+
import de.rub.nds.modifiablevariable.mlong.LongXorModification;
25+
import de.rub.nds.modifiablevariable.mlong.ModifiableLong;
26+
import de.rub.nds.modifiablevariable.util.ArrayConverter;
27+
import java.io.Serializable;
28+
import javax.xml.bind.annotation.XmlRootElement;
29+
import javax.xml.bind.annotation.XmlSeeAlso;
30+
import javax.xml.bind.annotation.XmlType;
31+
32+
/**
33+
*
34+
* @author Robert Merget <robert.merget@rub.de>
35+
*/
36+
@XmlRootElement
37+
@XmlSeeAlso({ StringExplicitValueModification.class })
38+
@XmlType(propOrder = { "originalValue", "modification", "assertEquals" })
39+
public class ModifiableString extends ModifiableVariable<String> implements Serializable {
40+
41+
private String originalValue;
42+
43+
public ModifiableString() {
44+
}
45+
46+
@Override
47+
protected void createRandomModification() {
48+
VariableModification<String> vm = StringModificationFactory.createRandomModification();
49+
setModification(vm);
50+
}
51+
52+
public String getAssertEquals() {
53+
return assertEquals;
54+
}
55+
56+
public void setAssertEquals(String assertEquals) {
57+
this.assertEquals = assertEquals;
58+
}
59+
60+
@Override
61+
public boolean isOriginalValueModified() {
62+
return originalValue != null && originalValue.compareTo(getValue()) != 0;
63+
}
64+
65+
public byte[] getByteArray(int size) {
66+
return getValue().getBytes();
67+
}
68+
69+
@Override
70+
public boolean validateAssertions() {
71+
boolean valid = true;
72+
if (assertEquals != null) {
73+
if (assertEquals.compareTo(getValue()) != 0) {
74+
valid = false;
75+
}
76+
}
77+
return valid;
78+
}
79+
80+
@Override
81+
public String getOriginalValue() {
82+
return originalValue;
83+
}
84+
85+
@Override
86+
public void setOriginalValue(String originalValue) {
87+
this.originalValue = originalValue;
88+
}
89+
90+
@Override
91+
public String toString() {
92+
return "ModifiableString{" + "originalValue=" + originalValue + '}';
93+
}
94+
95+
@Override
96+
public boolean equals(Object o) {
97+
if (this == o) {
98+
return true;
99+
}
100+
if (!(o instanceof ModifiableString)) {
101+
return false;
102+
}
103+
104+
ModifiableString that = (ModifiableString) o;
105+
106+
return getValue() != null ? getValue().equals(that.getValue()) : that.getValue() == null;
107+
}
108+
109+
@Override
110+
public int hashCode() {
111+
int result = super.hashCode();
112+
result = 31 * result + (getValue() != null ? getValue().hashCode() : 0);
113+
return result;
114+
}
115+
116+
}

0 commit comments

Comments
 (0)