reader.test.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2011 Mark Cavage <mcavage@gmail.com> All rights reserved.
  2. var test = require('tap').test;
  3. ///--- Globals
  4. var BerReader;
  5. ///--- Tests
  6. test('load library', function(t) {
  7. BerReader = require('../../lib/index').BerReader;
  8. t.ok(BerReader);
  9. try {
  10. new BerReader();
  11. t.fail('Should have thrown');
  12. } catch (e) {
  13. t.ok(e instanceof TypeError, 'Should have been a type error');
  14. }
  15. t.end();
  16. });
  17. test('read byte', function(t) {
  18. var reader = new BerReader(new Buffer([0xde]));
  19. t.ok(reader);
  20. t.equal(reader.readByte(), 0xde, 'wrong value');
  21. t.end();
  22. });
  23. test('read 1 byte int', function(t) {
  24. var reader = new BerReader(new Buffer([0x02, 0x01, 0x03]));
  25. t.ok(reader);
  26. t.equal(reader.readInt(), 0x03, 'wrong value');
  27. t.equal(reader.length, 0x01, 'wrong length');
  28. t.end();
  29. });
  30. test('read 2 byte int', function(t) {
  31. var reader = new BerReader(new Buffer([0x02, 0x02, 0x7e, 0xde]));
  32. t.ok(reader);
  33. t.equal(reader.readInt(), 0x7ede, 'wrong value');
  34. t.equal(reader.length, 0x02, 'wrong length');
  35. t.end();
  36. });
  37. test('read 3 byte int', function(t) {
  38. var reader = new BerReader(new Buffer([0x02, 0x03, 0x7e, 0xde, 0x03]));
  39. t.ok(reader);
  40. t.equal(reader.readInt(), 0x7ede03, 'wrong value');
  41. t.equal(reader.length, 0x03, 'wrong length');
  42. t.end();
  43. });
  44. test('read 4 byte int', function(t) {
  45. var reader = new BerReader(new Buffer([0x02, 0x04, 0x7e, 0xde, 0x03, 0x01]));
  46. t.ok(reader);
  47. t.equal(reader.readInt(), 0x7ede0301, 'wrong value');
  48. t.equal(reader.length, 0x04, 'wrong length');
  49. t.end();
  50. });
  51. test('read 1 byte negative int', function(t) {
  52. var reader = new BerReader(new Buffer([0x02, 0x01, 0xdc]));
  53. t.ok(reader);
  54. t.equal(reader.readInt(), -36, 'wrong value');
  55. t.equal(reader.length, 0x01, 'wrong length');
  56. t.end();
  57. });
  58. test('read 2 byte negative int', function(t) {
  59. var reader = new BerReader(new Buffer([0x02, 0x02, 0xc0, 0x4e]));
  60. t.ok(reader);
  61. t.equal(reader.readInt(), -16306, 'wrong value');
  62. t.equal(reader.length, 0x02, 'wrong length');
  63. t.end();
  64. });
  65. test('read 3 byte negative int', function(t) {
  66. var reader = new BerReader(new Buffer([0x02, 0x03, 0xff, 0x00, 0x19]));
  67. t.ok(reader);
  68. t.equal(reader.readInt(), -65511, 'wrong value');
  69. t.equal(reader.length, 0x03, 'wrong length');
  70. t.end();
  71. });
  72. test('read 4 byte negative int', function(t) {
  73. var reader = new BerReader(new Buffer([0x02, 0x04, 0x91, 0x7c, 0x22, 0x1f]));
  74. t.ok(reader);
  75. t.equal(reader.readInt(), -1854135777, 'wrong value');
  76. t.equal(reader.length, 0x04, 'wrong length');
  77. t.end();
  78. });
  79. test('read boolean true', function(t) {
  80. var reader = new BerReader(new Buffer([0x01, 0x01, 0xff]));
  81. t.ok(reader);
  82. t.equal(reader.readBoolean(), true, 'wrong value');
  83. t.equal(reader.length, 0x01, 'wrong length');
  84. t.end();
  85. });
  86. test('read boolean false', function(t) {
  87. var reader = new BerReader(new Buffer([0x01, 0x01, 0x00]));
  88. t.ok(reader);
  89. t.equal(reader.readBoolean(), false, 'wrong value');
  90. t.equal(reader.length, 0x01, 'wrong length');
  91. t.end();
  92. });
  93. test('read enumeration', function(t) {
  94. var reader = new BerReader(new Buffer([0x0a, 0x01, 0x20]));
  95. t.ok(reader);
  96. t.equal(reader.readEnumeration(), 0x20, 'wrong value');
  97. t.equal(reader.length, 0x01, 'wrong length');
  98. t.end();
  99. });
  100. test('read string', function(t) {
  101. var dn = 'cn=foo,ou=unit,o=test';
  102. var buf = new Buffer(dn.length + 2);
  103. buf[0] = 0x04;
  104. buf[1] = Buffer.byteLength(dn);
  105. buf.write(dn, 2);
  106. var reader = new BerReader(buf);
  107. t.ok(reader);
  108. t.equal(reader.readString(), dn, 'wrong value');
  109. t.equal(reader.length, dn.length, 'wrong length');
  110. t.end();
  111. });
  112. test('read sequence', function(t) {
  113. var reader = new BerReader(new Buffer([0x30, 0x03, 0x01, 0x01, 0xff]));
  114. t.ok(reader);
  115. t.equal(reader.readSequence(), 0x30, 'wrong value');
  116. t.equal(reader.length, 0x03, 'wrong length');
  117. t.equal(reader.readBoolean(), true, 'wrong value');
  118. t.equal(reader.length, 0x01, 'wrong length');
  119. t.end();
  120. });
  121. test('anonymous LDAPv3 bind', function(t) {
  122. var BIND = new Buffer(14);
  123. BIND[0] = 0x30; // Sequence
  124. BIND[1] = 12; // len
  125. BIND[2] = 0x02; // ASN.1 Integer
  126. BIND[3] = 1; // len
  127. BIND[4] = 0x04; // msgid (make up 4)
  128. BIND[5] = 0x60; // Bind Request
  129. BIND[6] = 7; // len
  130. BIND[7] = 0x02; // ASN.1 Integer
  131. BIND[8] = 1; // len
  132. BIND[9] = 0x03; // v3
  133. BIND[10] = 0x04; // String (bind dn)
  134. BIND[11] = 0; // len
  135. BIND[12] = 0x80; // ContextSpecific (choice)
  136. BIND[13] = 0; // simple bind
  137. // Start testing ^^
  138. var ber = new BerReader(BIND);
  139. t.equal(ber.readSequence(), 48, 'Not an ASN.1 Sequence');
  140. t.equal(ber.length, 12, 'Message length should be 12');
  141. t.equal(ber.readInt(), 4, 'Message id should have been 4');
  142. t.equal(ber.readSequence(), 96, 'Bind Request should have been 96');
  143. t.equal(ber.length, 7, 'Bind length should have been 7');
  144. t.equal(ber.readInt(), 3, 'LDAP version should have been 3');
  145. t.equal(ber.readString(), '', 'Bind DN should have been empty');
  146. t.equal(ber.length, 0, 'string length should have been 0');
  147. t.equal(ber.readByte(), 0x80, 'Should have been ContextSpecific (choice)');
  148. t.equal(ber.readByte(), 0, 'Should have been simple bind');
  149. t.equal(null, ber.readByte(), 'Should be out of data');
  150. t.end();
  151. });
  152. test('long string', function(t) {
  153. var buf = new Buffer(256);
  154. var o;
  155. var s =
  156. '2;649;CN=Red Hat CS 71GA Demo,O=Red Hat CS 71GA Demo,C=US;' +
  157. 'CN=RHCS Agent - admin01,UID=admin01,O=redhat,C=US [1] This is ' +
  158. 'Teena Vradmin\'s description.';
  159. buf[0] = 0x04;
  160. buf[1] = 0x81;
  161. buf[2] = 0x94;
  162. buf.write(s, 3);
  163. var ber = new BerReader(buf.slice(0, 3 + s.length));
  164. t.equal(ber.readString(), s);
  165. t.end();
  166. });