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