aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/test/java/com
diff options
context:
space:
mode:
authorGravatar janakr <janakr@google.com>2018-06-05 10:26:02 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-06-05 10:28:09 -0700
commit459b244216900506a052d56ad890fa1194882608 (patch)
treeba1f12cdf2433d7642f82f8f01c4a46033cf559f /src/test/java/com
parentfc23edc368244849c59636d1e2ae6c375bebf262 (diff)
Add functionality to SerializationContext and @AutoCodec to check that a class is allowed to be serialized in the current context. A codec can now add an explicitly allowed class that can be serialized underneath it (via SerializationContext#addExplicitlyAllowedClass), and that class's codec can check that it is explicitly allowed (via SerializationContext#checkClassExplicitlyAllowed). It is a runtime crash if a codec checks that it was explicitly allowed and finds that it wasn't. Thus, if PackageCodec is invoked without it having been explicitly allowed, we will crash, preventing Package from sneaking into a value it shouldn't be in.
This is only enabled if the codec is memoizing. PiperOrigin-RevId: 199317936
Diffstat (limited to 'src/test/java/com')
-rw-r--r--src/test/java/com/google/devtools/build/lib/skyframe/serialization/SerializationContextTest.java22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/SerializationContextTest.java b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/SerializationContextTest.java
index f151c5aa54..36f93a1265 100644
--- a/src/test/java/com/google/devtools/build/lib/skyframe/serialization/SerializationContextTest.java
+++ b/src/test/java/com/google/devtools/build/lib/skyframe/serialization/SerializationContextTest.java
@@ -140,6 +140,28 @@ public class SerializationContextTest {
assertThat(TestUtils.roundTrip(obj, registry)).isEqualTo(obj);
}
+ @Test
+ public void explicitlyAllowedClassCheck() {
+ SerializationContext underTest =
+ new SerializationContext(ObjectCodecRegistry.newBuilder().build(), ImmutableMap.of())
+ .getMemoizingContext();
+ underTest.addExplicitlyAllowedClass(String.class);
+ underTest.checkClassExplicitlyAllowed(String.class);
+ assertThrows(
+ RuntimeException.class, () -> underTest.checkClassExplicitlyAllowed(Integer.class));
+ // Explicitly registered classes do not carry over to a new context.
+ assertThrows(
+ RuntimeException.class,
+ () -> underTest.getNewMemoizingContext().checkClassExplicitlyAllowed(String.class));
+ }
+
+ @Test
+ public void explicitlyAllowedClassCheckFailsIfNotMemoizing() {
+ SerializationContext underTest =
+ new SerializationContext(ObjectCodecRegistry.newBuilder().build(), ImmutableMap.of());
+ assertThrows(RuntimeException.class, () -> underTest.addExplicitlyAllowedClass(String.class));
+ }
+
private static class CodecMemoizing implements ObjectCodec<ImmutableList<Object>> {
@SuppressWarnings("unchecked")
@Override