marshal-structure 1.1.1 → 2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,163 @@
1
+ require 'marshal/structure/test_case'
2
+
3
+ class TestMarshalStructureAllocationCounter < Marshal::Structure::TestCase
4
+
5
+ def test_count
6
+ count = count_allocations EVERYTHING
7
+
8
+ assert_equal 21, count
9
+ end
10
+
11
+ def test_count_array
12
+ count = count_allocations "\x04\x08[\x07TF"
13
+
14
+ assert_equal 1, count
15
+ end
16
+
17
+ def test_count_bignum
18
+ count = count_allocations "\x04\x08l-\x07\x00\x00\x00@"
19
+
20
+ assert_equal 1, count
21
+ end
22
+
23
+ def test_count_class
24
+ count = count_allocations "\x04\x08c\x06C"
25
+
26
+ assert_equal 0, count
27
+ end
28
+
29
+ def test_count_data
30
+ count = count_allocations "\x04\bd:\x18OpenSSL::X509::Name[\x00"
31
+
32
+ assert_equal 2, count
33
+ end
34
+
35
+ def test_count_extended
36
+ count = count_allocations "\x04\be:\x0FEnumerableo:\vObject\x00"
37
+
38
+ assert_equal 1, count
39
+ end
40
+
41
+ def test_count_false
42
+ count = count_allocations "\x04\x080"
43
+
44
+ assert_equal 0, count
45
+ end
46
+
47
+ def test_count_fixnum
48
+ count = count_allocations "\x04\x08i/"
49
+
50
+ assert_equal 0, count
51
+ end
52
+
53
+ def test_count_float
54
+ count = count_allocations "\x04\bf\b4.2"
55
+
56
+ assert_equal 1, count
57
+ end
58
+
59
+ def test_count_hash
60
+ count = count_allocations "\x04\b{\ai\x06i\aTF"
61
+
62
+ assert_equal 1, count
63
+ end
64
+
65
+ def test_count_hash_default
66
+ count = count_allocations "\x04\x08}\x00i\x06"
67
+
68
+ assert_equal 1, count
69
+ end
70
+
71
+ def test_count_instance_variables
72
+ count = count_allocations "\x04\bI\"\x00\a:\x06ET:\a@xi\a"
73
+
74
+ assert_equal 3, count
75
+ end
76
+
77
+ def test_count_link
78
+ count = count_allocations "\x04\x08[\x07I\"\x00\x06:\x06ET@\x06"
79
+
80
+ assert_equal 3, count
81
+ end
82
+
83
+ def test_count_module
84
+ count = count_allocations "\x04\bm\x0FEnumerable"
85
+
86
+ assert_equal 0, count
87
+ end
88
+
89
+ def test_count_module_old
90
+ count = count_allocations "\x04\bM\x0FEnumerable"
91
+
92
+ assert_equal 0, count
93
+ end
94
+
95
+ def test_count_allocation
96
+ count = count_allocations "\x04\bo:\vObject\x00"
97
+
98
+ assert_equal 1, count
99
+ end
100
+
101
+ def test_count_regexp
102
+ count = count_allocations "\x04\bI/\x06x\x01\x06:\x06EF"
103
+
104
+ assert_equal 2, count
105
+ end
106
+
107
+ def test_count_string
108
+ count = count_allocations "\x04\b\"\x06x"
109
+
110
+ assert_equal 1, count
111
+ end
112
+
113
+ def test_count_struct
114
+ count = count_allocations "\x04\x08S:\x06S\x06:\x06ai\x08"
115
+
116
+ assert_equal 2, count
117
+ end
118
+
119
+ def test_count_symbol
120
+ count = count_allocations "\x04\x08:\x06S"
121
+
122
+ assert_equal 1, count
123
+ end
124
+
125
+ def test_count_symbol_link
126
+ count = count_allocations "\x04\b[\a:\x06s;\x00"
127
+
128
+ assert_equal 2, count
129
+ end
130
+
131
+ def test_count_true
132
+ count = count_allocations "\x04\x08T"
133
+
134
+ assert_equal 0, count
135
+ end
136
+
137
+ def test_count_user_defined
138
+ count = count_allocations "\x04\bIu:\tTime\r\xE7Q\x1C\x80\xA8\xC3\x83\xE5\a" \
139
+ ":\voffseti\xFE\x90\x9D:\tzoneI\"\bPDT\x06:\x06ET"
140
+
141
+ timeval = "\xE7Q\x1C\x80\xA8\xC3\x83\xE5"
142
+ timeval.force_encoding Encoding::BINARY
143
+
144
+ assert_equal 6, count
145
+ end
146
+
147
+ def test_count_user_marshal
148
+ count =
149
+ count_allocations "\x04\bU:\tDate[\vi\x00i\x03l{%i\x00i\x00i\x00f\f2299161"
150
+
151
+ assert_equal 3, count
152
+ end
153
+
154
+ def count_allocations marshal
155
+ tokenizer = @MS::Tokenizer.new marshal
156
+
157
+ allocation_counter = @MS::AllocationCounter.new tokenizer.tokens
158
+
159
+ allocation_counter.count
160
+ end
161
+
162
+ end
163
+
@@ -0,0 +1,97 @@
1
+ require 'marshal/structure/test_case'
2
+
3
+ class TestMarshalStructureParser < Marshal::Structure::TestCase
4
+
5
+ def test_parse
6
+ structure = parse EVERYTHING
7
+
8
+ float_data = "2.2999999999999998\x00ff"
9
+ float_data.force_encoding Encoding::BINARY
10
+
11
+ expected = [
12
+ :hash,
13
+ 0,
14
+ 1,
15
+ [:symbol, 0, "a"],
16
+ [:array,
17
+ 1,
18
+ 20,
19
+ [:class, 2, "B"],
20
+ [:module, 3, "C"],
21
+ [:string, 4, "d"],
22
+ [:regexp, 5, "e", 0],
23
+ [:fixnum, 1],
24
+ [:float, 6, float_data],
25
+ [:bignum, 7, 18446744073709551616],
26
+ :nil,
27
+ :true,
28
+ :false,
29
+ [:hash_default, 8, 0, [:fixnum, 0]],
30
+ [:struct, 9, [:symbol, 1, "S"], 1, [:symbol, 2, "f"], [:fixnum, 0]],
31
+ [:object, 10, [:symbol, 3, "Object"], [0]],
32
+ [:link, 10],
33
+ [:user_marshal, 11, [:symbol, 4, "M"], [:string, 12, "marshal_dump"]],
34
+ [:instance_variables,
35
+ [:user_defined, 13, [:symbol, 5, "U"], "_dump"],
36
+ 1,
37
+ [:symbol, 6, "@ivar_on_dump_str"],
38
+ [:string, 14, "value on ivar on dump str"]],
39
+ [:symbol_link, 0],
40
+ [:extended, [:symbol, 7, "E"], [:object, 15, [:symbol_link, 3], [0]]],
41
+ [:instance_variables,
42
+ [:string, 16, "string with ivar"],
43
+ 1,
44
+ [:symbol, 8, "@value"],
45
+ [:string, 17, "some value"]],
46
+ [:user_class, [:symbol, 9, "BenString"], [:string, 18, ""]]]]
47
+
48
+ assert_equal expected, structure
49
+ end
50
+
51
+ def test_parse_too_short
52
+ str = "\x04\x08{"
53
+
54
+ e = assert_raises ArgumentError do
55
+ parse str
56
+ end
57
+
58
+ assert_equal 'marshal data too short', e.message
59
+ end
60
+
61
+ def test_parse_data
62
+ name = OpenSSL::X509::Name.parse 'CN=nobody/DC=example'
63
+ str = Marshal.dump name
64
+
65
+ expected = [
66
+ :data,
67
+ 0,
68
+ [:symbol, 0, "OpenSSL::X509::Name"],
69
+ [:array,
70
+ 1,
71
+ 2,
72
+ [:array, 2, 3,
73
+ [:string, 3, "CN"],
74
+ [:string, 4, "nobody"],
75
+ [:fixnum, 12]],
76
+ [:array, 5, 3,
77
+ [:string, 6, "DC"],
78
+ [:string, 7, "example"],
79
+ [:fixnum, 22]]]]
80
+
81
+ assert_equal expected, parse(str)
82
+ end
83
+
84
+ def test_parse_module_old
85
+ assert_equal [:module, 0, "M"], parse("\x04\x08M\x06M")
86
+ end
87
+
88
+ def parse marshal
89
+ tokenizer = @MS::Tokenizer.new marshal
90
+
91
+ parser = @MS::Parser.new tokenizer.tokens
92
+
93
+ parser.parse
94
+ end
95
+
96
+ end
97
+
@@ -0,0 +1,344 @@
1
+ require 'marshal/structure/test_case'
2
+
3
+ class TestMarshalStructureTokenizer < Marshal::Structure::TestCase
4
+
5
+ def test_bytes
6
+ ms = @MST.new "\x04\x08\x06M"
7
+
8
+ assert_equal "\x06M", ms.bytes(2)
9
+ end
10
+
11
+ def test_byte_array
12
+ ms = @MST.new "\x04\x08\x06M"
13
+
14
+ assert_equal [6, 77], ms.byte_array(2)
15
+
16
+ e = assert_raises Marshal::Structure::EndOfMarshal do
17
+ ms.byte_array 3
18
+ end
19
+
20
+ assert_equal 4, e.consumed
21
+ assert_equal 3, e.requested
22
+ end
23
+
24
+ def test_byte
25
+ ms = @MST.new "\x04\x08M"
26
+
27
+ assert_equal 77, ms.byte
28
+
29
+ e = assert_raises Marshal::Structure::EndOfMarshal do
30
+ ms.byte
31
+ end
32
+
33
+ assert_equal 3, e.consumed
34
+ assert_equal 1, e.requested
35
+ end
36
+
37
+ def test_character
38
+ ms = @MST.new "\x04\x08M"
39
+
40
+ assert_equal 'M', ms.character
41
+
42
+ e = assert_raises Marshal::Structure::EndOfMarshal do
43
+ ms.character
44
+ end
45
+
46
+ assert_equal 3, e.consumed
47
+ assert_equal 1, e.requested
48
+ end
49
+
50
+ def test_check_version
51
+ assert_raises TypeError do
52
+ @MST.new("\x03\x00").check_version
53
+ end
54
+
55
+ @MST.new("\x04\x07").check_version
56
+ @MST.new("\x04\x08").check_version
57
+
58
+ assert_raises TypeError do
59
+ @MST.new("\x04\x09").check_version
60
+ end
61
+ end
62
+
63
+ def test_long
64
+ assert_equal 0, @MST.new("\x04\x08\x00").long
65
+
66
+ assert_equal 0, @MST.new("\x04\x08\x01\x00").long
67
+ assert_equal 1, @MST.new("\x04\x08\x01\x01").long
68
+ assert_equal 0, @MST.new("\x04\x08\x02\x00\x00").long
69
+ assert_equal 2<<7, @MST.new("\x04\x08\x02\x00\x01").long
70
+ assert_equal 0, @MST.new("\x04\x08\x03\x00\x00\x00").long
71
+ assert_equal 2<<15, @MST.new("\x04\x08\x03\x00\x00\x01").long
72
+ assert_equal 0, @MST.new("\x04\x08\x04\x00\x00\x00\x00").long
73
+ assert_equal 2<<23, @MST.new("\x04\x08\x04\x00\x00\x00\x01").long
74
+ assert_equal (2<<31) - 1, @MST.new("\x04\x08\x04\xff\xff\xff\xff").long
75
+
76
+ assert_equal 0, @MST.new("\x04\x08\x05").long
77
+ assert_equal 1, @MST.new("\x04\x08\x06").long
78
+ assert_equal 122, @MST.new("\x04\x08\x7f").long
79
+ assert_equal( -123, @MST.new("\x04\x08\x80").long)
80
+ assert_equal( -1, @MST.new("\x04\x08\xfa").long)
81
+ assert_equal 0, @MST.new("\x04\x08\xfb").long
82
+
83
+ assert_equal( -(1<<32), @MST.new("\x04\x08\xfc\x00\x00\x00\x00").long)
84
+ assert_equal( -1, @MST.new("\x04\x08\xfc\xff\xff\xff\xff").long)
85
+ assert_equal( -(1<<24), @MST.new("\x04\x08\xfd\x00\x00\x00").long)
86
+ assert_equal( -1, @MST.new("\x04\x08\xfd\xff\xff\xff").long)
87
+ assert_equal( -(1<<16), @MST.new("\x04\x08\xfe\x00\x00").long)
88
+ assert_equal( -1, @MST.new("\x04\x08\xfe\xff\xff").long)
89
+ assert_equal( -(1<<8), @MST.new("\x04\x08\xff\x00").long)
90
+ assert_equal( -1, @MST.new("\x04\x08\xff\xff").long)
91
+ end
92
+
93
+ def test_byte_sequence
94
+ ms = @MST.new "\x04\x08\x06M"
95
+
96
+ assert_equal "M", ms.byte_sequence
97
+ end
98
+
99
+ def test_tokens_array
100
+ ms = @MST.new "\x04\x08[\x07TF"
101
+
102
+ assert_equal [:array, 2, :true, :false], ms.tokens.to_a
103
+ end
104
+
105
+ def test_tokens_bignum
106
+ ms = @MST.new "\x04\x08l-\x07\x00\x00\x00@"
107
+
108
+ assert_equal [:bignum, -1073741824], ms.tokens.to_a
109
+ end
110
+
111
+ def test_tokens_check_version
112
+ assert_raises TypeError do
113
+ @MST.new("\x04\x09").tokens
114
+ end
115
+ end
116
+
117
+ def test_tokens_class
118
+ ms = @MST.new "\x04\x08c\x06C"
119
+
120
+ assert_equal [:class, 'C'], ms.tokens.to_a
121
+ end
122
+
123
+ def test_tokens_data
124
+ ms = @MST.new "\x04\bd:\x18OpenSSL::X509::Name[\x00"
125
+
126
+ assert_equal [:data, :symbol, 'OpenSSL::X509::Name', :array, 0],
127
+ ms.tokens.to_a
128
+ end
129
+
130
+ def test_tokens_extended
131
+ ms = @MST.new "\x04\be:\x0FEnumerableo:\vObject\x00"
132
+
133
+ expected = [
134
+ :extended, :symbol, 'Enumerable',
135
+ :object, :symbol, 'Object', 0
136
+ ]
137
+
138
+ assert_equal expected, ms.tokens.to_a
139
+ end
140
+
141
+ def test_tokens_false
142
+ ms = @MST.new "\x04\x080"
143
+
144
+ assert_equal [:nil], ms.tokens.to_a
145
+ end
146
+
147
+ def test_tokens_fixnum
148
+ ms = @MST.new "\x04\x08i/"
149
+
150
+ assert_equal [:fixnum, 42], ms.tokens.to_a
151
+ end
152
+
153
+ def test_tokens_float
154
+ ms = @MST.new "\x04\bf\b4.2"
155
+
156
+ assert_equal [:float, '4.2'], ms.tokens.to_a
157
+ end
158
+
159
+ def test_tokens_hash
160
+ ms = @MST.new "\x04\b{\ai\x06i\aTF"
161
+
162
+ assert_equal [:hash, 2, :fixnum, 1, :fixnum, 2, :true, :false],
163
+ ms.tokens.to_a
164
+ end
165
+
166
+ def test_tokens_hash_default
167
+ ms = @MST.new "\x04\x08}\x00i\x06"
168
+
169
+ assert_equal [:hash_default, 0, :fixnum, 1], ms.tokens.to_a
170
+ end
171
+
172
+ def test_tokens_instance_variables
173
+ ms = @MST.new "\x04\bI\"\x00\a:\x06ET:\a@xi\a"
174
+
175
+ expected = [
176
+ :instance_variables,
177
+ :string, '',
178
+ 2, :symbol, 'E', :true, :symbol, '@x', :fixnum, 2,
179
+ ]
180
+
181
+ assert_equal expected, ms.tokens.to_a
182
+ end
183
+
184
+ def test_tokens_link
185
+ ms = @MST.new "\x04\x08[\x07I\"\x00\x06:\x06ET@\x06"
186
+
187
+ expected = [
188
+ :array, 2,
189
+ :instance_variables,
190
+ :string, '',
191
+ 1,
192
+ :symbol, 'E', :true,
193
+ :link, 1,
194
+ ]
195
+
196
+ assert_equal expected, ms.tokens.to_a
197
+ end
198
+
199
+ def test_tokens_module
200
+ ms = @MST.new "\x04\bm\x0FEnumerable"
201
+
202
+ assert_equal [:module, 'Enumerable'], ms.tokens.to_a
203
+ end
204
+
205
+ def test_tokens_module_old
206
+ ms = @MST.new "\x04\bM\x0FEnumerable"
207
+
208
+ assert_equal [:module_old, 'Enumerable'], ms.tokens.to_a
209
+ end
210
+
211
+ def test_tokens_object
212
+ mst = @MST.new \
213
+ "\x04\bo:\x16DRb::DRbConnError\a" +
214
+ ":\tmesgI\"\x16connection closed\x06:\x06ET" +
215
+ ":\abt[\x06" +
216
+ "\"\x21whatever.rb:in `some_method'"
217
+
218
+ expected = [
219
+ :object,
220
+ :symbol,
221
+ 'DRb::DRbConnError',
222
+ 2,
223
+ :symbol,
224
+ 'mesg',
225
+ :instance_variables,
226
+ :string,
227
+ 'connection closed',
228
+ 1,
229
+ :symbol,
230
+ 'E',
231
+ :true,
232
+ :symbol,
233
+ 'bt',
234
+ :array,
235
+ 1,
236
+ :string,
237
+ 'whatever.rb:in `some_method\'',
238
+ ]
239
+
240
+ assert_equal expected, mst.tokens.to_a
241
+ end
242
+
243
+ def test_tokens_regexp
244
+ ms = @MST.new "\x04\bI/\x06x\x01\x06:\x06EF"
245
+
246
+ expected = [
247
+ :instance_variables,
248
+ :regexp, 'x', 1,
249
+ 1, :symbol, 'E', :false,
250
+ ]
251
+
252
+ assert_equal expected, ms.tokens.to_a
253
+ end
254
+
255
+ def test_tokens_string
256
+ ms = @MST.new "\x04\b\"\x06x"
257
+
258
+ assert_equal [:string, 'x'], ms.tokens.to_a
259
+ end
260
+
261
+ def test_tokens_struct
262
+ ms = @MST.new "\x04\x08S:\x06S\x06:\x06ai\x08"
263
+
264
+ expected = [
265
+ :struct, :symbol, 'S', 1, :symbol, 'a', :fixnum, 3
266
+ ]
267
+
268
+ assert_equal expected, ms.tokens.to_a
269
+ end
270
+
271
+ def test_tokens_symbol
272
+ ms = @MST.new "\x04\x08:\x06S"
273
+
274
+ expected = [
275
+ :symbol, 'S'
276
+ ]
277
+
278
+ assert_equal expected, ms.tokens.to_a
279
+ end
280
+
281
+ def test_tokens_symbol_link
282
+ ms = @MST.new "\x04\b[\a:\x06s;\x00"
283
+
284
+ expected = [
285
+ :array, 2, :symbol, 's', :symbol_link, 0,
286
+ ]
287
+
288
+ assert_equal expected, ms.tokens.to_a
289
+ end
290
+
291
+ def test_tokens_too_short
292
+ ms = @MST.new "\x04\x08"
293
+
294
+ assert_raises Marshal::Structure::EndOfMarshal do
295
+ ms.tokens.to_a
296
+ end
297
+ end
298
+
299
+ def test_tokens_true
300
+ ms = @MST.new "\x04\x08T"
301
+
302
+ assert_equal [:true], ms.tokens.to_a
303
+ end
304
+
305
+ def test_tokens_user_defined
306
+ ms = @MST.new "\x04\bIu:\tTime\r\xE7Q\x1C\x80\xA8\xC3\x83\xE5\a" \
307
+ ":\voffseti\xFE\x90\x9D:\tzoneI\"\bPDT\x06:\x06ET"
308
+
309
+ timeval = "\xE7Q\x1C\x80\xA8\xC3\x83\xE5"
310
+ timeval.force_encoding Encoding::BINARY
311
+
312
+ expected = [
313
+ :instance_variables,
314
+ :user_defined, :symbol, 'Time', timeval,
315
+ 2,
316
+ :symbol, 'offset', :fixnum, -25200,
317
+ :symbol, 'zone',
318
+ :instance_variables,
319
+ :string, 'PDT',
320
+ 1, :symbol, 'E', :true,
321
+ ]
322
+
323
+ assert_equal expected, ms.tokens.to_a
324
+ end
325
+
326
+ def test_tokens_user_marshal
327
+ ms = @MST.new "\x04\bU:\tDate[\vi\x00i\x03l{%i\x00i\x00i\x00f\f2299161"
328
+
329
+ expected = [
330
+ :user_marshal, :symbol, 'Date',
331
+ :array, 6,
332
+ :fixnum, 0,
333
+ :fixnum, 2456428,
334
+ :fixnum, 0,
335
+ :fixnum, 0,
336
+ :fixnum, 0,
337
+ :float, '2299161',
338
+ ]
339
+
340
+ assert_equal expected, ms.tokens.to_a
341
+ end
342
+
343
+ end
344
+