|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.sdk.extension.incubator.trace.samplers; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Assertions.assertThat; |
| 9 | + |
| 10 | +import com.pholser.junit.quickcheck.generator.InRange; |
| 11 | +import edu.berkeley.cs.jqf.fuzz.Fuzz; |
| 12 | +import edu.berkeley.cs.jqf.fuzz.JQF; |
| 13 | +import edu.berkeley.cs.jqf.fuzz.junit.GuidedFuzzing; |
| 14 | +import edu.berkeley.cs.jqf.fuzz.random.NoGuidance; |
| 15 | +import io.opentelemetry.api.trace.TraceState; |
| 16 | +import java.util.Collections; |
| 17 | +import org.junit.jupiter.api.Test; |
| 18 | +import org.junit.runner.Result; |
| 19 | +import org.junit.runner.RunWith; |
| 20 | + |
| 21 | +@SuppressWarnings("SystemOut") |
| 22 | +class OtelTraceStateFuzzTest { |
| 23 | + |
| 24 | + @RunWith(JQF.class) |
| 25 | + public static class TestCases { |
| 26 | + @Fuzz |
| 27 | + public void roundTripRandomValues(long rv, long th) { |
| 28 | + OtelTraceState input = new OtelTraceState(rv, th, Collections.emptyList()); |
| 29 | + OtelTraceState output = |
| 30 | + OtelTraceState.parse(TraceState.builder().put("ot", input.serialize()).build()); |
| 31 | + assertState(output, rv, th); |
| 32 | + } |
| 33 | + |
| 34 | + @Fuzz |
| 35 | + public void roundTripValidValues( |
| 36 | + @InRange(minLong = 0, maxLong = ImmutableSamplingIntent.MAX_RANDOM_VALUE) long rv, |
| 37 | + @InRange(minLong = 0, maxLong = ImmutableSamplingIntent.MAX_THRESHOLD) long th) { |
| 38 | + OtelTraceState input = new OtelTraceState(rv, th, Collections.emptyList()); |
| 39 | + OtelTraceState output = |
| 40 | + OtelTraceState.parse(TraceState.builder().put("ot", input.serialize()).build()); |
| 41 | + assertState(output, rv, th); |
| 42 | + } |
| 43 | + |
| 44 | + private static void assertState(OtelTraceState state, long rv, long th) { |
| 45 | + boolean hasRv = false; |
| 46 | + boolean hasTh = false; |
| 47 | + if (rv >= 0 && rv <= ImmutableSamplingIntent.MAX_RANDOM_VALUE) { |
| 48 | + assertThat(state.getRandomValue()).isEqualTo(rv); |
| 49 | + hasRv = true; |
| 50 | + } else { |
| 51 | + assertThat(state.getRandomValue()).isEqualTo(ImmutableSamplingIntent.INVALID_RANDOM_VALUE); |
| 52 | + } |
| 53 | + if (th >= 0 && th <= ImmutableSamplingIntent.MAX_THRESHOLD) { |
| 54 | + assertThat(state.getThreshold()).isEqualTo(th); |
| 55 | + hasTh = state.getThreshold() != ImmutableSamplingIntent.MAX_THRESHOLD; |
| 56 | + } else { |
| 57 | + assertThat(state.getThreshold()).isEqualTo(ImmutableSamplingIntent.INVALID_THRESHOLD); |
| 58 | + } |
| 59 | + String[] parts = state.serialize().split(";"); |
| 60 | + String thStr = null; |
| 61 | + String rvStr = null; |
| 62 | + if (hasRv && hasTh) { |
| 63 | + assertThat(parts).hasSize(2); |
| 64 | + thStr = parts[0]; |
| 65 | + rvStr = parts[1]; |
| 66 | + } else if (hasRv) { |
| 67 | + assertThat(parts).hasSize(1); |
| 68 | + rvStr = parts[0]; |
| 69 | + } else if (hasTh) { |
| 70 | + assertThat(parts).hasSize(1); |
| 71 | + thStr = parts[0]; |
| 72 | + } |
| 73 | + |
| 74 | + if (hasRv) { |
| 75 | + assertThat(rvStr).startsWith("rv:"); |
| 76 | + rvStr = rvStr.substring("rv:".length()); |
| 77 | + assertThat(rvStr).hasSize(OtelTraceState.MAX_VALUE_LENGTH); |
| 78 | + assertThat(rvStr).isHexadecimal(); |
| 79 | + } |
| 80 | + |
| 81 | + if (hasTh) { |
| 82 | + assertThat(thStr).startsWith("th:"); |
| 83 | + thStr = thStr.substring("th:".length()); |
| 84 | + assertThat(thStr).hasSizeBetween(1, OtelTraceState.MAX_VALUE_LENGTH); |
| 85 | + assertThat(thStr).isHexadecimal(); |
| 86 | + if (th == 0) { |
| 87 | + assertThat(thStr).isEqualTo("0"); |
| 88 | + } else { |
| 89 | + assertThat(thStr).doesNotMatch("[^0]+0"); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // driver methods to avoid having to use the vintage junit engine, and to enable increasing the |
| 96 | + // number of iterations: |
| 97 | + |
| 98 | + @Test |
| 99 | + void roundTripFuzzing() { |
| 100 | + Result result = runTestCase("roundTripRandomValues"); |
| 101 | + assertThat(result.wasSuccessful()).isTrue(); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void roundTripValidValues() { |
| 106 | + Result result = runTestCase("roundTripValidValues"); |
| 107 | + assertThat(result.wasSuccessful()).isTrue(); |
| 108 | + } |
| 109 | + |
| 110 | + private static Result runTestCase(String testCaseName) { |
| 111 | + return GuidedFuzzing.run( |
| 112 | + TestCases.class, testCaseName, new NoGuidance(10000, System.out), System.out); |
| 113 | + } |
| 114 | +} |
0 commit comments