class_tag_map3.diff

Martin Bosslet, 01/25/2011 07:37 AM

Download (5.26 KB)

View differences:

ruby/ext/openssl/ossl_asn1.c (working copy)
489 489

  
490 490
int ossl_asn1_info_size = (sizeof(ossl_asn1_info)/sizeof(ossl_asn1_info[0]));
491 491

  
492
static VALUE class_tag_map;
493

  
492 494
static int ossl_asn1_default_tag(VALUE obj);
493 495

  
494 496
ASN1_TYPE*
......
570 572
static int
571 573
ossl_asn1_default_tag(VALUE obj)
572 574
{
573
    int i;
575
    VALUE tmp_class = CLASS_OF(obj);
576
    while (tmp_class) {
577
        VALUE tag = rb_hash_lookup(class_tag_map, tmp_class);
578
        if (tag != Qnil) {
579
            return NUM2INT(tag);
580
        }
581
        tmp_class = RCLASS_SUPER(tmp_class);
582
    }
574 583

  
575
    for(i = 0; i < ossl_asn1_info_size; i++){
576
	if(ossl_asn1_info[i].klass &&
577
	   rb_obj_is_kind_of(obj, *ossl_asn1_info[i].klass)){
578
	    return i;
579
	}
580
    }
581 584
    ossl_raise(eASN1Error, "universal tag for %s not found",
582 585
	       rb_class2name(CLASS_OF(obj)));
583 586

  
......
1260 1263
    rb_attr(cASN1BitString, rb_intern("unused_bits"), 1, 1, 0);
1261 1264

  
1262 1265
    rb_define_method(cASN1EndOfContent, "initialize", ossl_asn1eoc_initialize, 0);
1266

  
1267
    class_tag_map = rb_hash_new();
1268
    rb_hash_aset(class_tag_map, cASN1EndOfContent, INT2NUM(0));
1269
    rb_hash_aset(class_tag_map, cASN1Boolean, INT2NUM(1));
1270
    rb_hash_aset(class_tag_map, cASN1Integer, INT2NUM(2));
1271
    rb_hash_aset(class_tag_map, cASN1BitString, INT2NUM(3));
1272
    rb_hash_aset(class_tag_map, cASN1OctetString, INT2NUM(4));
1273
    rb_hash_aset(class_tag_map, cASN1Null, INT2NUM(5));
1274
    rb_hash_aset(class_tag_map, cASN1ObjectId, INT2NUM(6));
1275
    rb_hash_aset(class_tag_map, cASN1Enumerated, INT2NUM(10));
1276
    rb_hash_aset(class_tag_map, cASN1UTF8String, INT2NUM(12));
1277
    rb_hash_aset(class_tag_map, cASN1Sequence, INT2NUM(16));
1278
    rb_hash_aset(class_tag_map, cASN1Set, INT2NUM(17));
1279
    rb_hash_aset(class_tag_map, cASN1NumericString, INT2NUM(18));
1280
    rb_hash_aset(class_tag_map, cASN1PrintableString, INT2NUM(19));
1281
    rb_hash_aset(class_tag_map, cASN1T61String, INT2NUM(20));
1282
    rb_hash_aset(class_tag_map, cASN1VideotexString, INT2NUM(21));
1283
    rb_hash_aset(class_tag_map, cASN1IA5String, INT2NUM(22));
1284
    rb_hash_aset(class_tag_map, cASN1UTCTime, INT2NUM(23));
1285
    rb_hash_aset(class_tag_map, cASN1GeneralizedTime, INT2NUM(24));
1286
    rb_hash_aset(class_tag_map, cASN1GraphicString, INT2NUM(25));
1287
    rb_hash_aset(class_tag_map, cASN1ISO64String, INT2NUM(26));
1288
    rb_hash_aset(class_tag_map, cASN1GeneralString, INT2NUM(27));
1289
    rb_hash_aset(class_tag_map, cASN1UniversalString, INT2NUM(28));
1290
    rb_hash_aset(class_tag_map, cASN1BMPString, INT2NUM(30));
1291

  
1292
    class_tag_map = rb_obj_freeze(class_tag_map);
1293
    rb_define_const(mASN1, "CLASS_TAG_MAP", class_tag_map);
1263 1294
}
ruby/test/openssl/test_asn1.rb (working copy)
430 430
    end
431 431
  end
432 432
  
433
  def test_default_tag_of_class
434
    assert_equal(0, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::EndOfContent])    
435
    assert_equal(1, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::Boolean])
436
    assert_equal(2, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::Integer])
437
    assert_equal(3, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::BitString])
438
    assert_equal(4, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::OctetString])    
439
    assert_equal(5, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::Null])    
440
    assert_equal(6, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::ObjectId])    
441
    assert_equal(10, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::Enumerated])    
442
    assert_equal(12, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::UTF8String]) 
443
    assert_equal(16, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::Sequence])    
444
    assert_equal(17, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::Set])    
445
    assert_equal(18, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::NumericString])
446
    assert_equal(19, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::PrintableString])
447
    assert_equal(20, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::T61String])
448
    assert_equal(21, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::VideotexString])
449
    assert_equal(22, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::IA5String])
450
    assert_equal(23, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::UTCTime])
451
    assert_equal(24, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::GeneralizedTime])
452
    assert_equal(25, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::GraphicString])
453
    assert_equal(26, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::ISO64String])
454
    assert_equal(27, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::GeneralString])
455
    assert_equal(28, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::UniversalString])
456
    assert_equal(30, OpenSSL::ASN1::CLASS_TAG_MAP[OpenSSL::ASN1::BMPString])
457
  end
458
  
459
  def test_class_tag_map_is_frozen
460
    assert_raises(RuntimeError) do
461
      OpenSSL::ASN1::CLASS_TAG_MAP[self.class] = 99
462
    end
463
  end
464
  
433 465
end if defined?(OpenSSL)