aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_parser.rb
blob: 86b6c6dbf50c306dd56022691d0c4c4e879550bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
require_relative "helper"

class ParserTest < ASN1KitTestCase
  def test_comment
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        -- T ::= BOOLEAN
      END
    EOS
    assert_equal nil, mod["T"]

    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        /*
         * T1 ::= BOOLEAN
         * T2 ::= INTEGER
         */
      END
    EOS
    assert_equal nil, mod["T1"]
    assert_equal nil, mod["T2"]

    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        /*
         * /*
         *  * T ::= BOOLEAN
         *  */
         */
      END
    EOS
    assert_equal nil, mod["T"]
  end

  def test_module
    mod1 = ASN1Kit.parse(<<~EOS)
      M1 DEFINITIONS ::= BEGIN END
    EOS
    assert_equal "M1", mod1.name
    assert_equal nil, mod1.oid

    mod2 = ASN1Kit.parse(<<~EOS)
      M2 { 1 2 3 abc(4) } DEFINITIONS ::= BEGIN END
    EOS
    assert_equal "M2", mod2.name
    assert_instance_of ASN1Kit::ObjectIdentifier, mod2.oid
    assert_equal "1.2.3.4", mod2.oid.dot_notation
  end

  def test_boolean
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T ::= BOOLEAN
        v1 BOOLEAN ::= TRUE
        v2 T ::= FALSE
      END
    EOS

    t = mod["T"]
    assert_compare t, :<, ASN1Kit::Boolean

    v1 = mod["v1"]
    assert_instance_of ASN1Kit::Boolean, v1
    assert_equal true, v1.value

    v2 = mod["v2"]
    assert_instance_of t, v2
    assert_equal false, v2.value
  end

  def test_integer
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T1 ::= INTEGER
        T2 ::= INTEGER { a(123), b(-2) }
        v1 INTEGER ::= 123
        v2 INTEGER ::= -123
        v3 INTEGER { v1(3) } ::= v1
        v4 T2 ::= v1
      END
    EOS

    t1 = mod["T1"]
    assert_compare t1, :<, ASN1Kit::Integer
    assert_equal [], t1.named_numbers

    t2 = mod["T2"]
    assert_compare t2, :<, ASN1Kit::Integer
    assert_equal ["a", "b"], t2.named_numbers.sort
    assert_equal 123, t2.named_number("a")
    assert_equal (-2), t2.named_number("b")

    v1 = mod["v1"]
    assert_instance_of ASN1Kit::Integer, v1
    assert_equal 123, v1.value

    v2 = mod["v2"]
    assert_instance_of ASN1Kit::Integer, v2
    assert_equal (-123), v2.value

    v3 = mod["v3"]
    assert_compare v3.class, :<, ASN1Kit::Integer
    assert_equal ["v1"], v3.class.named_numbers
    assert_equal 3, v3.value
    assert_equal "v1", v3.name

    v4 = mod["v4"]
    assert_instance_of t2, v4
    assert_equal 123, v4.value
    assert_equal "a", v4.name
  end

  def test_enumerated
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T1 ::= ENUMERATED { a, b }
        T2 ::= ENUMERATED { a(2), b(1) }
        T3 ::= ENUMERATED { a, b(0) }
        T4 ::= ENUMERATED { a, b, ..., c(5) }
        T5 ::= ENUMERATED { a, b(3), ..., c }
        T6 ::= ENUMERATED { a, b(3), ..., c(1) }
        T7 ::= ENUMERATED { a, b(3), ..., c(1), d }
        v1 T1 ::= b
        v2 T1 ::= v1
      END
    EOS

    t1 = mod["T1"]
    assert_compare t1, :<, ASN1Kit::Enumerated
    assert_equal ["a", "b"], t1.enumerations.sort
    assert_equal 0, t1.value_for("a")
    assert_equal 1, t1.value_for("b")
    assert_equal false, t1.extensible?

    t2 = mod["T2"]
    assert_equal 2, t2.value_for("a")
    assert_equal 1, t2.value_for("b")

    t3 = mod["T3"]
    assert_equal 1, t3.value_for("a")
    assert_equal 0, t3.value_for("b")

    t4 = mod["T4"]
    assert_equal 0, t4.value_for("a")
    assert_equal 1, t4.value_for("b")
    assert_equal 5, t4.value_for("c")
    assert_equal true, t4.extensible?

    t5 = mod["T5"]
    assert_equal 0, t5.value_for("a")
    assert_equal 3, t5.value_for("b")
    assert_equal 2, t5.value_for("c")

    t6 = mod["T6"]
    assert_equal 0, t6.value_for("a")
    assert_equal 3, t6.value_for("b")
    assert_equal 1, t6.value_for("c")

    t7 = mod["T7"]
    assert_equal 0, t7.value_for("a")
    assert_equal 3, t7.value_for("b")
    assert_equal 1, t7.value_for("c")
    assert_equal 2, t7.value_for("d")

    v1 = mod["v1"]
    assert_instance_of t1, v1
    assert_equal 1, v1.value
    assert_equal "b", v1.name

    v2 = mod["v2"]
    assert_instance_of t1, v2
    assert_equal 1, v2.value


    mod2 = ASN1Kit.parse(<<~EOS)
      M2 DEFINITIONS EXTENSIBILITY IMPLIED ::= BEGIN
        T1 ::= ENUMERATED { a, b }
        T2 ::= ENUMERATED { a, b, ... }
      END
    EOS

    assert_equal true, mod2["T1"].extensible?
    assert_equal true, mod2["T2"].extensible?
  end

  def test_real
    # FIXME: NumericRealValue is not tested
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T ::= REAL
        v1 REAL ::= PLUS-INFINITY
        v2 REAL ::= MINUS-INFINITY
        v3 REAL ::= NOT-A-NUMBER
        v4 T ::= v1
      END
    EOS

    t = mod["T"]
    assert_compare t, :<, ASN1Kit::Real

    v1 = mod["v1"]
    assert_instance_of ASN1Kit::Real, v1
    assert_equal true, v1.plus_infinity?

    v2 = mod["v2"]
    assert_equal true, v2.minus_infinity?

    v3 = mod["v3"]
    assert_equal true, v3.not_a_number?

    v4 = mod["v4"]
    assert_instance_of t, v4
    assert_equal true, v4.plus_infinity?
  end

  def test_bitstring
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        a INTEGER ::= 1
        T1 ::= BIT STRING
        T2 ::= BIT STRING { a(3), b(a) }
        v1 BIT STRING ::= '011'B
        v2 T2 ::= '42'H
        v3 T2 ::= { }
        v4 T2 ::= { a }
      END
    EOS

    t1 = mod["T1"]
    assert_compare t1, :<, ASN1Kit::BitString
    assert_equal [], t1.named_bits

    t2 = mod["T2"]
    assert_compare t2, :<, ASN1Kit::BitString
    assert_equal ["a", "b"], t2.named_bits.sort
    assert_equal 3, t2.named_bit("a")
    assert_equal 1, t2.named_bit("b")

    v1 = mod["v1"]
    assert_instance_of ASN1Kit::BitString, v1
    assert_equal "\x60".b, v1.string
    assert_equal 3, v1.bit_length

    v2 = mod["v2"]
    assert_instance_of t2, v2
    assert_equal "\x42".b, v2.string
    assert_equal 8, v2.bit_length

    v3 = mod["v3"]
    assert_instance_of t2, v3
    assert_equal "".b, v3.string
    assert_equal 0, v3.bit_length

    v4 = mod["v4"]
    assert_instance_of t2, v4
    assert_equal "\x10".b, v4.string
    assert_equal 4, v4.bit_length
  end

  def test_octetstring
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T ::= OCTET STRING
        v1 OCTET STRING ::= '01100000 00000001'B
        v2 T ::= '4'H
      END
    EOS

    t = mod["T"]
    assert_compare t, :<, ASN1Kit::OctetString

    v1 = mod["v1"]
    assert_instance_of ASN1Kit::OctetString, v1
    assert_equal "\x60\x01".b, v1.value

    v2 = mod["v2"]
    assert_instance_of t, v2
    assert_equal "\x40".b, v2.value
  end

  def test_null
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T ::= NULL
        v1 NULL ::= NULL
        v2 T ::= NULL
      END
    EOS

    t = mod["T"]
    assert_compare t, :<, ASN1Kit::Null

    v1 = mod["v1"]
    assert_instance_of ASN1Kit::Null, v1

    v2 = mod["v2"]
    assert_instance_of t, v2
  end

  def test_sequence
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T1 ::= SEQUENCE { a NULL, b INTEGER }
        T2 ::= SEQUENCE { a NULL, b INTEGER OPTIONAL }
        v1 T1 ::= { b 1, a NULL }
      END
    EOS

    t1 = mod["T1"]
    assert_compare t1, :<, ASN1Kit::Sequence
    assert_equal ["a", "b"], t1.component_types.sort
    assert_same ASN1Kit::Null, t1.component_type("a")
    assert_same ASN1Kit::Integer, t1.component_type("b")

    t2 = mod["T2"]
    assert_compare t2, :<, ASN1Kit::Sequence
    assert_equal ["a", "b"], t2.component_types.sort
    assert_same ASN1Kit::Null, t2.component_type("a")
    assert_same ASN1Kit::Integer, t2.component_type("b")

    v1 = mod["v1"]
    assert_instance_of t1, v1
    assert_instance_of ASN1Kit::Null, v1["a"]
    assert_instance_of ASN1Kit::Integer, v1["b"]
    assert_equal 1, v1["b"].value
  end

  def test_object_identifier
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T ::= OBJECT IDENTIFIER
        v1 T ::= { 0 1 2 }
        v2 OBJECT IDENTIFIER ::= v1
        v3 OBJECT IDENTIFIER ::= { v1 3 }
        v4 OBJECT IDENTIFIER ::= { iso(1) 1 }
        v5 OBJECT IDENTIFIER ::= { iso 1 }
      END
    EOS

    t = mod["T"]
    assert_compare t, :<, ASN1Kit::ObjectIdentifier

    v1 = mod["v1"]
    assert_instance_of t, v1
    assert_equal "0.1.2", v1.dot_notation

    v2 = mod["v2"]
    assert_instance_of ASN1Kit::ObjectIdentifier, v2
    assert_equal "0.1.2", v2.dot_notation

    v3 = mod["v3"]
    assert_equal "0.1.2.3", v3.dot_notation

    v4 = mod["v4"]
    assert_equal "1.1", v4.dot_notation

    v5 = mod["v5"]
    assert_equal "1.1", v5.dot_notation
  end

  def test_relative_object_identifier
    mod = ASN1Kit.parse(<<~EOS)
      M DEFINITIONS ::= BEGIN
        T ::= RELATIVE-OID
        v1 T ::= { 1 2 3 }
        v2 RELATIVE-OID ::= v1
        v3 RELATIVE-OID ::= { 0 v1 4 }
        v4 RELATIVE-OID ::= { iso(1) }
        v5 OBJECT IDENTIFIER ::= { 0 v1 4 }
      END
    EOS

    t = mod["T"]
    assert_compare t, :<, ASN1Kit::RelativeOID

    v1 = mod["v1"]
    assert_instance_of t, v1
    assert_equal "1.2.3", v1.dot_notation

    v2 = mod["v2"]
    assert_instance_of ASN1Kit::RelativeOID, v2
    assert_equal "1.2.3", v2.dot_notation

    v3 = mod["v3"]
    assert_equal "0.1.2.3.4", v3.dot_notation

    v4 = mod["v4"]
    assert_equal "1", v4.dot_notation

    v5 = mod["v5"]
    assert_equal "0.1.2.3.4", v5.dot_notation

    assert_parse_error(<<~EOS)
      M DEFINITIONS ::= BEGIN
        v RELATIVE-OID ::= { iso }
      END
    EOS
  end

  private

  def assert_parse_error(source, pattern = nil)
    if pattern
      assert_raise_with_message(ASN1Kit::ParseError, pattern) {
        ASN1Kit.parse(source)
      }
    else
      assert_raise(ASN1Kit::ParseError) {
        ASN1Kit.parse(source)
      }
    end
  end
end