Skip to content

Commit 74541e1

Browse files
authored
Merge pull request #18 from RUB-NDS/delegate-modification
Delegate modification
2 parents 536f29f + 6651706 commit 74541e1

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import de.rub.nds.modifiablevariable.biginteger.BigIntegerAddModification;
1212
import de.rub.nds.modifiablevariable.biginteger.BigIntegerExplicitValueModification;
13+
import de.rub.nds.modifiablevariable.biginteger.BigIntegerInteractiveModification;
1314
import de.rub.nds.modifiablevariable.biginteger.BigIntegerShiftLeftModification;
1415
import de.rub.nds.modifiablevariable.biginteger.BigIntegerShiftRightModification;
1516
import de.rub.nds.modifiablevariable.biginteger.BigIntegerSubtractModification;
@@ -46,7 +47,7 @@
4647
*/
4748
@XmlRootElement
4849
@XmlTransient
49-
@XmlSeeAlso({ AccessModificationFilter.class, BigIntegerAddModification.class,
50+
@XmlSeeAlso({ AccessModificationFilter.class, BigIntegerAddModification.class, BigIntegerInteractiveModification.class,
5051
BigIntegerExplicitValueModification.class, BigIntegerSubtractModification.class,
5152
BigIntegerXorModification.class, BigIntegerShiftLeftModification.class, BigIntegerShiftRightModification.class,
5253
IntegerAddModification.class, IntegerExplicitValueModification.class, IntegerSubtractModification.class,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/**
2+
* ModifiableVariable - A Variable Concept for Runtime Modifications
3+
*
4+
* Copyright 2014-2017 Ruhr University Bochum / Hackmanit GmbH
5+
*
6+
* Licensed under Apache License 2.0
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*/
9+
package de.rub.nds.modifiablevariable.biginteger;
10+
11+
import de.rub.nds.modifiablevariable.VariableModification;
12+
import java.math.BigInteger;
13+
import javax.xml.bind.annotation.XmlRootElement;
14+
import javax.xml.bind.annotation.XmlType;
15+
16+
/**
17+
* @author Janis Fliegenschmidt - janis.fliegenschmidt@rub.de
18+
*/
19+
@XmlRootElement
20+
@XmlType(propOrder = { "modificationFilter", "postModification" })
21+
public class BigIntegerInteractiveModification extends VariableModification<BigInteger> {
22+
23+
private InteractiveBigIntegerModification modification;
24+
25+
protected BigIntegerInteractiveModification() {
26+
this.modification = BigIntegerModificationFactory.getStandardInteractiveModification();
27+
}
28+
29+
protected BigIntegerInteractiveModification(InteractiveBigIntegerModification modification) {
30+
this.modification = modification;
31+
}
32+
33+
@Override
34+
protected BigInteger modifyImplementationHook(final BigInteger input) {
35+
return this.modification.modify(input);
36+
}
37+
38+
public interface InteractiveBigIntegerModification {
39+
40+
BigInteger modify(BigInteger oldVal);
41+
}
42+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.LinkedList;
2121
import java.util.List;
2222
import java.util.Random;
23+
import java.util.Scanner;
2324

2425
/**
2526
* @author
@@ -88,6 +89,36 @@ public static VariableModification<BigInteger> explicitValueFromFile(int value)
8889
return modifications.get(pos);
8990
}
9091

92+
/*
93+
* Interactive modification
94+
*/
95+
private static BigIntegerInteractiveModification.InteractiveBigIntegerModification standardInteractiveModification = new BigIntegerInteractiveModification.InteractiveBigIntegerModification() {
96+
private BigInteger value;
97+
98+
@Override
99+
public BigInteger modify(BigInteger oldVal) {
100+
if (value == null) {
101+
System.out.println("Enter new value for BigInt: ");
102+
value = new Scanner(System.in).nextBigInteger();
103+
}
104+
return value;
105+
106+
}
107+
};
108+
109+
public static void setStandardInteractiveModification(
110+
BigIntegerInteractiveModification.InteractiveBigIntegerModification modification) {
111+
standardInteractiveModification = modification;
112+
}
113+
114+
protected static BigIntegerInteractiveModification.InteractiveBigIntegerModification getStandardInteractiveModification() {
115+
return standardInteractiveModification;
116+
}
117+
118+
public static VariableModification<BigInteger> interactive() {
119+
return new BigIntegerInteractiveModification();
120+
}
121+
91122
public static synchronized List<VariableModification<BigInteger>> modificationsFromFile() {
92123
try {
93124
if (modificationsFromFile == null) {

src/test/java/de/rub/nds/modifiablevariable/serialization/BigIntegerSerializationTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import de.rub.nds.modifiablevariable.VariableModification;
1212
import de.rub.nds.modifiablevariable.biginteger.BigIntegerAddModification;
13+
import de.rub.nds.modifiablevariable.biginteger.BigIntegerInteractiveModification;
1314
import de.rub.nds.modifiablevariable.biginteger.BigIntegerModificationFactory;
1415
import de.rub.nds.modifiablevariable.biginteger.ModifiableBigInteger;
1516
import de.rub.nds.modifiablevariable.bytearray.ByteArrayModificationFactory;
@@ -61,10 +62,17 @@ public void setUp() throws JAXBException {
6162

6263
writer = new StringWriter();
6364
context = JAXBContext.newInstance(ModifiableBigInteger.class, BigIntegerAddModification.class,
64-
ByteArrayModificationFactory.class);
65+
ByteArrayModificationFactory.class, BigIntegerInteractiveModification.class);
6566
m = context.createMarshaller();
6667
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
6768
um = context.createUnmarshaller();
69+
70+
BigIntegerModificationFactory
71+
.setStandardInteractiveModification(new BigIntegerInteractiveModification.InteractiveBigIntegerModification() {
72+
public BigInteger modify(BigInteger oldVal) {
73+
return new BigInteger("12");
74+
}
75+
});
6876
}
6977

7078
@Test
@@ -102,7 +110,24 @@ public void testSerializeDeserializeWithDoubleModification() throws Exception {
102110
result = mv.getValue();
103111
assertEquals(expectedResult, result);
104112
assertNotSame(expectedResult, result);
113+
}
114+
115+
@Test
116+
public void testSerializationWithInteractiveMod() throws Exception {
117+
VariableModification<BigInteger> mod = BigIntegerModificationFactory.interactive();
118+
start.setModification(mod);
119+
m.marshal(start, writer);
120+
121+
String xmlString = writer.toString();
122+
LOGGER.debug(xmlString);
105123

124+
um = context.createUnmarshaller();
125+
ModifiableBigInteger mv = (ModifiableBigInteger) um.unmarshal(new StringReader(xmlString));
126+
127+
expectedResult = new BigInteger("12");
128+
result = mv.getValue();
129+
assertEquals(expectedResult, result);
130+
assertNotSame(expectedResult, result);
106131
}
107132

108133
@Test

0 commit comments

Comments
 (0)