Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions api/all/src/test/java/io/opentelemetry/api/logs/ValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import edu.berkeley.cs.jqf.fuzz.Fuzz;
import edu.berkeley.cs.jqf.fuzz.JQF;
import edu.berkeley.cs.jqf.fuzz.junit.GuidedFuzzing;
import edu.berkeley.cs.jqf.fuzz.random.NoGuidance;
import io.opentelemetry.api.common.KeyValue;
import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.common.ValueType;
Expand All @@ -25,6 +29,8 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runner.Result;
import org.junit.runner.RunWith;

class ValueTest {

Expand Down Expand Up @@ -211,5 +217,39 @@ void valueByteAsString() {
String base64Encoded = Value.of(str.getBytes(StandardCharsets.UTF_8)).asString();
byte[] decodedBytes = Base64.getDecoder().decode(base64Encoded);
assertThat(new String(decodedBytes, StandardCharsets.UTF_8)).isEqualTo(str);

// Test empty string
String emptyStr = "";
String emptyBase64 = Value.of(emptyStr.getBytes(StandardCharsets.UTF_8)).asString();
byte[] decodedEmpty = Base64.getDecoder().decode(emptyBase64);
assertThat(new String(decodedEmpty, StandardCharsets.UTF_8)).isEqualTo(emptyStr);

// Test string with special characters
String specialStr = "hello \n \t 🌍 world!";
String specialBase64 = Value.of(specialStr.getBytes(StandardCharsets.UTF_8)).asString();
byte[] decodedSpecial = Base64.getDecoder().decode(specialBase64);
assertThat(new String(decodedSpecial, StandardCharsets.UTF_8)).isEqualTo(specialStr);
}

@RunWith(JQF.class)
public static class FuzzTestCases {
@Fuzz
public void valueByteAsStringFuzz(String randomString) {
String base64Encoded = Value.of(randomString.getBytes(StandardCharsets.UTF_8)).asString();
byte[] decodedBytes = Base64.getDecoder().decode(base64Encoded);
assertThat(new String(decodedBytes, StandardCharsets.UTF_8)).isEqualTo(randomString);
}
}

@SuppressWarnings("SystemOut")
@Test
void valueByteAsStringFuzzing() {
Result result =
GuidedFuzzing.run(
FuzzTestCases.class,
"valueByteAsStringFuzz",
new NoGuidance(10000, System.out),
System.out);
assertThat(result.wasSuccessful()).isTrue();
}
}
Loading