Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3c05b85

Browse files
committed
Optional seed configuration for (partially) deterministic test runs.
1 parent bd2a4d1 commit 3c05b85

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ target
33
nb-configuration.xml
44
.idea
55
json/examples/*.json
6+
seeds/*.txt

‎seeds/dummy‎

Whitespace-only changes.

‎src/test/java/de/christofreichardt/asn1/ASN1Unit.java‎

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,42 @@
33
import de.christofreichardt.diagnosis.AbstractTracer;
44
import de.christofreichardt.diagnosis.Traceable;
55
import de.christofreichardt.diagnosis.TracerFactory;
6-
import java.util.Arrays;
7-
import java.util.HexFormat;
8-
import java.util.NoSuchElementException;
9-
import java.util.Random;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.security.SecureRandom;
10+
import java.util.*;
1011
import java.util.stream.Stream;
1112
import org.assertj.core.api.WithAssertions;
1213
import org.junit.jupiter.api.AfterAll;
1314
import org.junit.jupiter.api.BeforeAll;
15+
import org.junit.jupiter.api.Test;
1416
import org.junit.jupiter.api.TestInstance;
1517
import org.junit.jupiter.params.ParameterizedTest;
1618
import org.junit.jupiter.params.provider.MethodSource;
1719

1820
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
1921
public class ASN1Unit implements Traceable, WithAssertions {
2022

23+
Random random = new SecureRandom();
24+
2125
@BeforeAll
22-
void init() {
26+
void init() throwsIOException{
2327
AbstractTracer tracer = getCurrentTracer();
2428
tracer.entry("void", this, "init()");
2529

2630
try {
31+
Path path = Path.of("seeds", "asn1-unit-seed.txt");
32+
long seed;
33+
if (Files.exists(path)) {
34+
tracer.out().printfIndentln("Using configured seed ...");
35+
List<String> lines = Files.readAllLines(path);
36+
seed = Long.parseLong(lines.get(0));
37+
} else {
38+
seed = this.random.nextLong();
39+
}
40+
tracer.out().printfIndentln("seed = %d", seed);
41+
this.random.setSeed(seed);
2742
} finally {
2843
tracer.wayout();
2944
}
@@ -109,7 +124,7 @@ public String toString() {
109124
}
110125
}
111126

112-
staticStream<ASN1SeqTestParam> asn1IntSequenceStream(int seqCount, int maxIntByteCount, int seqMaxByteCount, int maxSeqLength) {
127+
Stream<ASN1SeqTestParam> asn1IntSequenceStream(int seqCount, int maxIntByteCount, int seqMaxByteCount, int maxSeqLength) {
113128
AbstractTracer tracer = TracerFactory.getInstance().getCurrentPoolTracer();
114129
tracer.entry("Stream<ASN1IntSequence>", ASN1Unit.class, "asn1IntSequenceStream()");
115130

@@ -118,18 +133,17 @@ static Stream<ASN1SeqTestParam> asn1IntSequenceStream(int seqCount, int maxIntBy
118133
seqCount, maxIntByteCount, seqMaxByteCount, maxSeqLength);
119134

120135
ASN1SeqTestParam[] asn1SeqTestParams = new ASN1SeqTestParam[seqCount];
121-
Random random = new Random();
122136
int index = 0;
123137
do {
124-
int intCount = random.nextInt(maxSeqLength);
138+
int intCount = this.random.nextInt(maxSeqLength);
125139
tracer.out().printfIndentln("index = %d, intCount = %d", index, intCount);
126140
int sum = 0;
127141
ASN1Integer[] asn1Integers = new ASN1Integer[intCount];
128142
for (int j=0; j<intCount; j++) {
129-
int length = random.nextInt(maxIntByteCount);
143+
int length = this.random.nextInt(maxIntByteCount);
130144
tracer.out().printfIndentln("length = %d", length);
131145
byte[] bytes = new byte[length];
132-
random.nextBytes(bytes);
146+
this.random.nextBytes(bytes);
133147
ASN1Integer asn1Integer = ASN1Integer.fromBytes(bytes);
134148
tracer.out().printfIndentln("asn1Integer = %s", asn1Integer);
135149
sum += asn1Integer.asn1Length.rawLength();
@@ -153,7 +167,7 @@ static Stream<ASN1SeqTestParam> asn1IntSequenceStream(int seqCount, int maxIntBy
153167
}
154168
}
155169

156-
staticStream<ASN1SeqTestParam> asn1IntShortSequenceStream() {
170+
Stream<ASN1SeqTestParam> asn1IntShortSequenceStream() {
157171
return asn1IntSequenceStream(25, 32, ASN1.SHORT_LENGTH, 16);
158172
}
159173

@@ -243,7 +257,7 @@ void decodeSignaturesWithLongFormLength(ASN1_SignatureTestParam asn1_signatureTe
243257
}
244258
}
245259

246-
staticStream<ASN1SeqTestParam> asn1IntNotSoShortSequenceStream() {
260+
Stream<ASN1SeqTestParam> asn1IntNotSoShortSequenceStream() {
247261
return asn1IntSequenceStream(25, 64, 255, 8);
248262
}
249263

@@ -269,6 +283,23 @@ void randomNotSoShortSequencesWithShortInts(ASN1SeqTestParam asn1SeqTestParam) {
269283
}
270284
}
271285

286+
@Test
287+
void seeding() {
288+
AbstractTracer tracer = getCurrentTracer();
289+
tracer.entry("void", this, "seeding()");
290+
291+
try {
292+
StringBuilder stringBuilder = new StringBuilder();
293+
for (int i=0; i<25; i++) {
294+
stringBuilder.append(this.random.nextInt(100));
295+
stringBuilder.append(" ");
296+
}
297+
tracer.out().printfIndentln("random ints = %s", stringBuilder.toString());
298+
} finally {
299+
tracer.wayout();
300+
}
301+
}
302+
272303
@AfterAll
273304
void exit() {
274305
AbstractTracer tracer = getCurrentTracer();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /