Skip to content

Commit c9dcb11

Browse files
committed
Added support for ModifiableStrings
1 parent 356c285 commit c9dcb11

File tree

5 files changed

+219
-3
lines changed

5 files changed

+219
-3
lines changed

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: 3 additions & 3 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,9 +58,8 @@
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

6565
protected static final Logger LOGGER = LogManager.getLogger(VariableModification.class);
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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.VariableModification;
19+
import javax.xml.bind.annotation.XmlRootElement;
20+
import javax.xml.bind.annotation.XmlType;
21+
22+
/**
23+
*
24+
* @author Robert Merget <robert.merget@rub.de>
25+
*/
26+
@XmlRootElement
27+
@XmlType(propOrder = { "explicitValue", "modificationFilter", "postModification" })
28+
public class StringExplicitValueModification extends VariableModification<String> {
29+
30+
private String explicitValue;
31+
32+
public StringExplicitValueModification() {
33+
}
34+
35+
public StringExplicitValueModification(String explicitValue) {
36+
this.explicitValue = explicitValue;
37+
}
38+
39+
@Override
40+
protected String modifyImplementationHook(String input) {
41+
return explicitValue;
42+
}
43+
44+
public String getExplicitValue() {
45+
return explicitValue;
46+
}
47+
48+
public void setExplicitValue(String explicitValue) {
49+
this.explicitValue = explicitValue;
50+
}
51+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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.VariableModification;
19+
import de.rub.nds.modifiablevariable.mlong.LongExplicitValueModification;
20+
import de.rub.nds.modifiablevariable.util.RandomHelper;
21+
22+
/**
23+
*
24+
* @author Robert Merget <robert.merget@rub.de>
25+
*/
26+
public class StringModificationFactory {
27+
28+
private static final int MAX_BYTE_LENGTH = 1000;
29+
30+
public static VariableModification<String> explicitValue(final String value) {
31+
return new StringExplicitValueModification(value);
32+
}
33+
34+
public static VariableModification<String> createRandomModification() {
35+
int i = RandomHelper.getRandom().nextInt(1000);
36+
byte[] randomBytes = new byte[i];
37+
RandomHelper.getRandom().nextBytes(randomBytes);
38+
return explicitValue(new String(randomBytes));
39+
}
40+
}

0 commit comments

Comments
 (0)