]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/testcases/org/simantics/databoard/tests/TestMaliciousData.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / testcases / org / simantics / databoard / tests / TestMaliciousData.java
1 package org.simantics.databoard.tests;
2
3 import java.io.IOException;
4 import java.util.List;
5 import java.util.Map;
6
7 import junit.framework.Assert;
8
9 import org.junit.Test;
10 import org.simantics.databoard.Bindings;
11 import org.simantics.databoard.binding.Binding;
12 import org.simantics.databoard.serialization.Serializer;
13
14 /**
15  *
16  * @author Toni Kalajainen <toni.kalajainen@iki.fi>
17  */
18 public class TestMaliciousData {
19
20         public @Test void testArray() throws Exception {
21                 
22                 
23                 Binding b = Bindings.getBinding(List.class, Integer.class);
24                 Serializer s = Bindings.getSerializer(b);
25
26                 // negative array length
27                 try {
28                         byte[] data = new byte[] {-1, -1, -1, -1, 0, 1, 2, 3, 4};
29                         s.deserialize(data);
30                         Assert.fail("Deserialization did not fail expectedly on negative length array");
31                 } catch (IOException e) {
32                         // Expected exception
33                 }
34
35                 // too big array
36                 try {
37                         byte[] data = new byte[] {10, 10, 10, 10, 0, 1, 2, 3, 4};
38                         s.deserialize(data);
39                         Assert.fail("Deserialization did not fail expectedly on too long array");
40                 } catch (IOException e) {
41                         // Expected exception
42                 }
43
44                 // OK Data
45                 try {
46                         byte[] data = new byte[] {0, 0, 0, 1, 0, 1, 2, 3};
47                         s.deserialize(data);
48                 } catch (IOException e) {
49                         Assert.fail("Deserialization did not work");
50                 }
51                 
52         }
53         
54         public @Test void testBooleanArray() throws Exception {
55                 
56                 
57                 Binding b = Bindings.BOOLEAN_ARRAY;
58                 Serializer s = Bindings.getSerializer(b);
59
60                 // negative array length
61                 try {
62                         byte[] data = new byte[] {-1, -1, -1, -1, 0, 1, 2, 3, 4};
63                         s.deserialize(data);
64                         Assert.fail("Deserialization did not fail expectedly on negative length array");
65                 } catch (IOException e) {
66                         // Expected exception
67                 }
68
69                 // too big array
70                 try {
71                         byte[] data = new byte[] {10, 10, 10, 10, 0, 1, 2, 3, 4};
72                         s.deserialize(data);
73                         Assert.fail("Deserialization did not fail expectedly on too long array");
74                 } catch (IOException e) {
75                         // Expected exception
76                 }
77
78                 // OK Data
79                 try {
80                         byte[] data = new byte[] {0, 0, 0, 1, 1};
81                         s.deserialize(data);
82                 } catch (IOException e) {
83                         Assert.fail("Deserialization did not work");
84                 }
85                 
86         }
87
88         public @Test void testMap() throws Exception {
89                 
90                 
91                 Binding b = Bindings.getBinding(Map.class, Integer.class, Integer.class);
92                 Serializer s = Bindings.getSerializer(b);
93
94                 // negative array length
95                 try {
96                         byte[] data = new byte[] {-1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7};
97                         s.deserialize(data);
98                         Assert.fail("Deserialization did not fail expectedly on negative length array");
99                 } catch (IOException e) {
100                         // Expected exception
101                 }
102
103                 // too big array
104                 try {
105                         byte[] data = new byte[] {10, 10, 10, 10, 0, 1, 2, 3, 4};
106                         s.deserialize(data);
107                         Assert.fail("Deserialization did not fail expectedly on too long array");
108                 } catch (IOException e) {
109                         // Expected exception
110                 }
111
112                 // OK Data
113                 try {
114                         byte[] data = new byte[] {0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0};
115                         s.deserialize(data);
116                 } catch (IOException e) {
117                         Assert.fail("Deserialization did not work");
118                 }
119                 
120         }
121         
122         
123 }