indutny / asn1.js

ASN.1 Decoder/Encoder/DSL
MIT License
184 stars 64 forks source link

Integers not decoded as number, doesn't match readme example. #81

Open kallerosenbaum opened 7 years ago

kallerosenbaum commented 7 years ago

This fails

it ('BigNum bug in ASN.1 implementation', () => {
        var ASNInteger = asn1.define('Integer', function () {
            this.seq().obj(
                this.key('aNumber').int());
        });
        var serialized = ASNInteger.encode({aNumber: 2}, 'der');
        var two = ASNInteger.decode(serialized, 'der');
        assert.equal(two.aNumber, 2);
        assert.equal(two+1, 3); // Fail
    });

With

AssertionError: expected '[object Object]1' to equal 3
Expected :3
Actual   :"[object Object]1"

It turns out that the example in the readme isn't quite correct:

  it ('example from github', () => {
        var Human = asn1.define('Human', function() {
            this.seq().obj(
                this.key('firstName').octstr(),
                this.key('lastName').octstr(),
                this.key('age').int(),
                this.key('gender').enum({ 0: 'male', 1: 'female' }),
                this.key('bio').seqof(Bio)
            );
        });

        var Bio = asn1.define('Bio', function() {
            this.seq().obj(
                this.key('time').gentime(),
                this.key('description').octstr()
            );
        });

        var output = Human.encode({
            firstName: 'Thomas',
            lastName: 'Anderson',
            age: 28,
            gender: 'male',
            bio: [
                {
                    time: +new Date('31 March 1999'),
                    description: 'freedom of mind'
                }
            ]
        }, 'der');

        var human = Human.decode(output, 'der');
        console.log(human);

    });

Prints

{ firstName: <Buffer 54 68 6f 6d 61 73>,
  lastName: <Buffer 41 6e 64 65 72 73 6f 6e>,
  age: <BN: 1c>,
  gender: 'male',
  bio: 
   [ { time: 922831200000,
       description: <Buffer 66 72 65 65 64 6f 6d 20 6f 66 20 6d 69 6e 64> } ] }

I expect age to be 28 but it's <BN: 1c>, which seems to be a BigNum. How is it supposed to work? According to the readme or according to this test case?

Thanks

indutny commented 7 years ago

Hello!

I think what you are looking for is a documentation for BN module. This is the bignum that this library uses for integers.

kallerosenbaum commented 7 years ago

Ok. I guess a small update to the readme should do the trick then. Thank you.