Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(789)

Unified Diff: mojo/android/javatests/src/org/chromium/mojo/bindings/SerializationTest.java

Issue 524703004: Mojo: validate nullability in Java serialization. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address Ben's comments. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/android/javatests/src/org/chromium/mojo/bindings/SerializationTest.java
diff --git a/mojo/android/javatests/src/org/chromium/mojo/bindings/SerializationTest.java b/mojo/android/javatests/src/org/chromium/mojo/bindings/SerializationTest.java
new file mode 100644
index 0000000000000000000000000000000000000000..9f7e94bdfc28bd954511743c9598f8187dd52a05
--- /dev/null
+++ b/mojo/android/javatests/src/org/chromium/mojo/bindings/SerializationTest.java
@@ -0,0 +1,132 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.mojo.bindings;
+
+import android.test.suitebuilder.annotation.SmallTest;
+
+import junit.framework.TestCase;
+
+import org.chromium.mojo.HandleMock;
+import org.chromium.mojo.bindings.test.mojom.mojo.Struct1;
+import org.chromium.mojo.bindings.test.mojom.mojo.Struct2;
+import org.chromium.mojo.bindings.test.mojom.mojo.Struct3;
+import org.chromium.mojo.bindings.test.mojom.mojo.Struct4;
+import org.chromium.mojo.bindings.test.mojom.mojo.Struct5;
+import org.chromium.mojo.bindings.test.mojom.mojo.Struct6;
+import org.chromium.mojo.bindings.test.mojom.mojo.StructOfNullables;
+
+/**
+ * Tests for the serialization logic of the generated structs, using structs defined in
+ * mojo/public/interfaces/bindings/tests/serialization_test_structs.mojom .
+ */
+public class SerializationTest extends TestCase {
+
+ private static void assertThrowsException(Struct struct) {
+ boolean exceptionThrown = false;
+ try {
+ struct.serialize(null);
+ } catch (SerializationException ex) {
+ exceptionThrown = true;
+ }
+ assertTrue(exceptionThrown);
qsr 2014/09/03 07:56:28 Instead of this, you can do: try { struct.ser
ppi 2014/09/03 08:53:46 Done.
+ }
+
+ /**
+ * Verifies that serializing a struct with an invalid handle of a non-nullable type throws an
+ * exception.
+ */
+ @SmallTest
+ public void testHandle() {
+ Struct2 struct = new Struct2();
+ assertFalse(struct.hdl.isValid());
+ assertThrowsException(struct);
+
+ // Make the struct valid and verify that it serializes without an exception.
+ struct.hdl = new HandleMock();
+ struct.serialize(null);
+ }
+
+ /**
+ * Verifies that serializing a struct with a null struct pointer throws an exception.
+ */
+ @SmallTest
+ public void testStructPointer() {
+ Struct3 struct = new Struct3();
+ assertNull(struct.struct1);
+ assertThrowsException(struct);
+
+ // Make the struct valid and verify that it serializes without an exception.
+ struct.struct1 = new Struct1();
+ struct.serialize(null);
+ }
+
+ /**
+ * Verifies that serializing a struct with an array of structs throws an exception when the
+ * struct is invalid.
+ */
+ @SmallTest
+ public void testStructArray() {
+ Struct4 struct = new Struct4();
+ assertNull(struct.array);
+ assertThrowsException(struct);
+
+ // Create the (1-element) array but have the element null.
+ struct.array = new Struct1[1];
+ assertThrowsException(struct);
+
+ // Create the array element, struct should serialize now.
+ struct.array[0] = new Struct1();
+ struct.serialize(null);
+ }
+
+ /**
+ * Verifies that serializing a struct with a fixed-size array of incorrect length throws an
+ * exception.
+ */
+ @SmallTest
+ public void testFixedSizeArray() {
+ Struct5 struct = new Struct5();
+ assertNull(struct.pair);
+ assertThrowsException(struct);
+
+ // Create the (1-element) array, 2-element array is required.
+ struct.pair = new Struct1[1];
+ struct.pair[0] = new Struct1();
+ assertThrowsException(struct);
+
+ // Create the array of a correct size, struct should serialize now.
+ struct.pair = new Struct1[2];
+ struct.pair[0] = new Struct1();
+ struct.pair[1] = new Struct1();
+ struct.serialize(null);
+ }
+
+ /**
+ * Verifies that serializing a struct with a null string throws an exception.
+ */
+ @SmallTest
+ public void testString() {
+ Struct6 struct = new Struct6();
+ assertNull(struct.str);
+ assertThrowsException(struct);
+
+ // Make the struct valid and verify that it serializes without an exception.
+ struct.str = new String();
qsr 2014/09/03 07:56:28 = "" instead?
ppi 2014/09/03 08:53:45 Done.
+ struct.serialize(null);
+ }
+
+ /**
+ * Verifies that a struct with an invalid nullable handle and a null nullable pointer serializes
qsr 2014/09/03 07:56:28 nit: There is more than one null nullable pointer
ppi 2014/09/03 08:53:46 Done.
+ * without an exception.
+ */
+ @SmallTest
+ public void testNullableFields() {
+ StructOfNullables struct = new StructOfNullables();
+ assertFalse(struct.hdl.isValid());
+ assertNull(struct.struct1);
+ assertNull(struct.str);
+ struct.serialize(null);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698