Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unittests_lib-ber-reader.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. var asn1 = require("../")
  2. var assert = require("assert")
  3. var BerReader = asn1.BerReader
  4. describe("lib/ber/reader.js", function() {
  5. describe("readByte()", function() {
  6. it("can read a value", function() {
  7. var reader = new BerReader(Buffer.from([0xde]))
  8. assert.equal(reader.readByte(), 0xde)
  9. })
  10. })
  11. describe("readInt()", function() {
  12. it("can read zero", function() {
  13. var reader = new BerReader(Buffer.from([0x02, 0x01, 0x00]))
  14. assert.equal(reader.readInt(), 0)
  15. assert.equal(reader.length, 1)
  16. })
  17. it("can read a 1 byte positive integer - lowest", function() {
  18. var reader = new BerReader(Buffer.from([0x02, 0x01, 0x01]))
  19. assert.equal(reader.readInt(), 1)
  20. assert.equal(reader.length, 1)
  21. })
  22. it("can read a 1 byte positive integer - middle", function() {
  23. var reader = new BerReader(Buffer.from([0x02, 0x01, 0x34]))
  24. assert.equal(reader.readInt(), 52)
  25. assert.equal(reader.length, 1)
  26. })
  27. it("can read a 1 byte positive integer - highest", function() {
  28. var reader = new BerReader(Buffer.from([0x02, 0x01, 0x7f]))
  29. assert.equal(reader.readInt(), 127)
  30. assert.equal(reader.length, 1)
  31. })
  32. it("can read a 2 byte positive integer - lowest", function() {
  33. var reader = new BerReader(Buffer.from([0x02, 0x02, 0x00, 0x80]))
  34. assert.equal(reader.readInt(), 128)
  35. assert.equal(reader.length, 2)
  36. })
  37. it("can read a 2 byte positive integer - middle", function() {
  38. var reader = new BerReader(Buffer.from([0x02, 0x02, 0x7e, 0xde]))
  39. assert.equal(reader.readInt(), 0x7ede)
  40. assert.equal(reader.length, 2)
  41. })
  42. it("can read a 2 byte positive integer - highest", function() {
  43. var reader = new BerReader(Buffer.from([0x02, 0x02, 0x7f, 0xff]))
  44. assert.equal(reader.readInt(), 32767)
  45. assert.equal(reader.length, 2)
  46. })
  47. it("can read a 3 byte positive integer - lowest", function() {
  48. var reader = new BerReader(Buffer.from([0x02, 0x03, 0x00, 0x80, 0x00]))
  49. assert.equal(reader.readInt(), 32768)
  50. assert.equal(reader.length, 3)
  51. })
  52. it("can read a 3 byte positive integer - middle", function() {
  53. var reader = new BerReader(Buffer.from([0x02, 0x03, 0x7e, 0xde, 0x03]))
  54. assert.equal(reader.readInt(), 8314371)
  55. assert.equal(reader.length, 3)
  56. })
  57. it("can read a 3 byte positive integer - highest", function() {
  58. var reader = new BerReader(Buffer.from([0x02, 0x03, 0x7e, 0xde, 0x03]))
  59. assert.equal(reader.readInt(), 0x7ede03)
  60. assert.equal(reader.length, 0x03)
  61. })
  62. it("can read a 4 byte positive integer - lowest", function() {
  63. var reader = new BerReader(Buffer.from([0x02, 0x04, 0x00, 0x80, 0x00, 0x00]))
  64. assert.equal(reader.readInt(), 8388608)
  65. assert.equal(reader.length, 4)
  66. })
  67. it("can read a 4 byte positive integer - middle", function() {
  68. var reader = new BerReader(Buffer.from([0x02, 0x04, 0x7e, 0xde, 0x03, 0x01]))
  69. assert.equal(reader.readInt(), 2128478977)
  70. assert.equal(reader.length, 4)
  71. })
  72. it("can read a 4 byte positive integer - highest", function() {
  73. var reader = new BerReader(Buffer.from([0x02, 0x04, 0x7f, 0xff, 0xff, 0xff]))
  74. assert.equal(reader.readInt(), 2147483647)
  75. assert.equal(reader.length, 4)
  76. })
  77. it("can read a 5 byte positive integer - lowest", function() {
  78. var reader = new BerReader(Buffer.from([0x02, 0x05, 0x00, 0x80, 0x00, 0x00, 0x00]))
  79. assert.equal(reader.readInt(), 2147483648)
  80. assert.equal(reader.length, 5)
  81. })
  82. it("can read a 5 byte positive integer - middle", function() {
  83. var reader = new BerReader(Buffer.from([0x02, 0x05, 0x00, 0x8b, 0xde, 0x03, 0x01]))
  84. assert.equal(reader.readInt(), 2346582785)
  85. assert.equal(reader.length, 5)
  86. })
  87. it("can read a 5 byte positive integer - highest", function() {
  88. var reader = new BerReader(Buffer.from([0x02, 0x05, 0x00, 0xff, 0xff, 0xff, 0xff]))
  89. assert.equal(reader.readInt(), 4294967295)
  90. assert.equal(reader.length, 5)
  91. })
  92. it("can read a 1 byte negative integer - lowest", function() {
  93. var reader = new BerReader(Buffer.from([0x02, 0x01, 0x80]))
  94. assert.equal(reader.readInt(), -128)
  95. assert.equal(reader.length, 1)
  96. })
  97. it("can read a 1 byte negative integer - middle", function() {
  98. var reader = new BerReader(Buffer.from([0x02, 0x01, 0xdc]))
  99. assert.equal(reader.readInt(), -36)
  100. assert.equal(reader.length, 1)
  101. })
  102. it("can read a 1 byte negative integer - highest", function() {
  103. var reader = new BerReader(Buffer.from([0x02, 0x01, 0xff]))
  104. assert.equal(reader.readInt(), -1)
  105. assert.equal(reader.length, 1)
  106. })
  107. it("can read a 2 byte negative integer - lowest", function() {
  108. var reader = new BerReader(Buffer.from([0x02, 0x02, 0x80, 0x00]))
  109. assert.equal(reader.readInt(), -32768)
  110. assert.equal(reader.length, 2)
  111. })
  112. it("can read a 2 byte negative integer - middle", function() {
  113. var reader = new BerReader(Buffer.from([0x02, 0x02, 0xc0, 0x4e]))
  114. assert.equal(reader.readInt(), -16306)
  115. assert.equal(reader.length, 2)
  116. })
  117. it("can read a 2 byte negative integer - highest", function() {
  118. var reader = new BerReader(Buffer.from([0x02, 0x02, 0xff, 0x7f]))
  119. assert.equal(reader.readInt(), -129)
  120. assert.equal(reader.length, 2)
  121. })
  122. it("can read a 3 byte negative integer - lowest", function() {
  123. var reader = new BerReader(Buffer.from([0x02, 0x03, 0x80, 0x00, 0x00]))
  124. assert.equal(reader.readInt(), -8388608)
  125. assert.equal(reader.length, 3)
  126. })
  127. it("can read a 3 byte negative integer - middle", function() {
  128. var reader = new BerReader(Buffer.from([0x02, 0x03, 0xff, 0x00, 0x19]))
  129. assert.equal(reader.readInt(), -65511)
  130. assert.equal(reader.length, 3)
  131. })
  132. it("can read a 3 byte negative integer - highest", function() {
  133. var reader = new BerReader(Buffer.from([0x02, 0x03, 0xff, 0x7f, 0xff]))
  134. assert.equal(reader.readInt(), -32769)
  135. assert.equal(reader.length, 3)
  136. })
  137. it("can read a 4 byte negative integer - lowest", function() {
  138. var reader = new BerReader(Buffer.from([0x02, 0x04, 0x80, 0x00, 0x00, 0x00]))
  139. assert.equal(reader.readInt(), -2147483648)
  140. assert.equal(reader.length, 4)
  141. })
  142. it("can read a 4 byte negative integer - middle", function() {
  143. var reader = new BerReader(Buffer.from([0x02, 0x04, 0x91, 0x7c, 0x22, 0x1f]))
  144. assert.equal(reader.readInt(), -1854135777)
  145. assert.equal(reader.length, 4)
  146. })
  147. it("can read a 4 byte negative integer - highest", function() {
  148. var reader = new BerReader(Buffer.from([0x02, 0x04, 0xff, 0x7f, 0xff, 0xff]))
  149. assert.equal(reader.readInt(), -8388609)
  150. assert.equal(reader.length, 4)
  151. })
  152. })
  153. describe("readBoolean()", function() {
  154. it("can read a true value", function() {
  155. var reader = new BerReader(Buffer.from([0x01, 0x01, 0xff]))
  156. assert.equal(reader.readBoolean(), true)
  157. assert.equal(reader.length, 0x01)
  158. })
  159. it("can read a false value", function() {
  160. var reader = new BerReader(Buffer.from([0x01, 0x01, 0x00]))
  161. assert.equal(reader.readBoolean(), false)
  162. assert.equal(reader.length, 0x01)
  163. })
  164. })
  165. describe("readEnumeration()", function() {
  166. it("can read a value", function() {
  167. var reader = new BerReader(Buffer.from([0x0a, 0x01, 0x20]))
  168. assert.equal(reader.readEnumeration(), 0x20, 'wrong value')
  169. assert.equal(reader.length, 0x01, 'wrong length')
  170. })
  171. })
  172. describe("readOID()", function() {
  173. it("does not convert to unsigned", function() {
  174. // Make sure 2887117176 is NOT converted to -1407850120
  175. var buffer = Buffer.from([6, 18, 43, 6, 1, 4, 1, 245, 12, 1, 1, 5, 1, 1, 19, 138, 224, 215, 210, 120])
  176. var reader = new BerReader(buffer)
  177. assert.equal(reader.readOID(), "1.3.6.1.4.1.14988.1.1.5.1.1.19.2887117176")
  178. assert.equal(reader.length, 18)
  179. })
  180. })
  181. describe("readString()", function() {
  182. it("can read a value", function() {
  183. var string = 'cn=foo,ou=unit,o=test'
  184. var buffer = Buffer.alloc(string.length + 2)
  185. buffer[0] = 0x04
  186. buffer[1] = Buffer.byteLength(string)
  187. buffer.write(string, 2)
  188. var reader = new BerReader(buffer)
  189. assert.equal(reader.readString(), string)
  190. assert.equal(reader.length, string.length)
  191. })
  192. })
  193. describe("readSequence()", function() {
  194. it("can read a sequence", function() {
  195. var reader = new BerReader(Buffer.from([0x30, 0x03, 0x01, 0x01, 0xff]))
  196. assert.equal(reader.readSequence(), 0x30)
  197. assert.equal(reader.length, 0x03)
  198. assert.equal(reader.readBoolean(), true)
  199. assert.equal(reader.length, 0x01)
  200. })
  201. })
  202. describe("readBitString()", function() {
  203. it("can read a bit string", function() {
  204. var reader = new BerReader(Buffer.from([0x03, 0x07, 0x04, 0x0a, 0x3b, 0x5f, 0x29, 0x1c, 0xd0]))
  205. assert.equal(reader.readBitString(), '00001010001110110101111100101001000111001101')
  206. assert.equal(reader.length, 7)
  207. })
  208. })
  209. describe("complex sequences", function() {
  210. it("are processed correctly", function() {
  211. var buffer = Buffer.alloc(14);
  212. // An anonymous LDAP v3 BIND request
  213. buffer[0] = 0x30 // Sequence
  214. buffer[1] = 12 // len
  215. buffer[2] = 0x02 // ASN.1 Integer
  216. buffer[3] = 1 // len
  217. buffer[4] = 0x04 // msgid (make up 4)
  218. buffer[5] = 0x60 // Bind Request
  219. buffer[6] = 7 // len
  220. buffer[7] = 0x02 // ASN.1 Integer
  221. buffer[8] = 1 // len
  222. buffer[9] = 0x03 // v3
  223. buffer[10] = 0x04 // String (bind dn)
  224. buffer[11] = 0 // len
  225. buffer[12] = 0x80 // ContextSpecific (choice)
  226. buffer[13] = 0 // simple bind
  227. var reader = new BerReader(buffer)
  228. assert.equal(reader.readSequence(), 48)
  229. assert.equal(reader.length, 12)
  230. assert.equal(reader.readInt(), 4)
  231. assert.equal(reader.readSequence(), 96)
  232. assert.equal(reader.length, 7)
  233. assert.equal(reader.readInt(), 3)
  234. assert.equal(reader.readString(), "")
  235. assert.equal(reader.length, 0)
  236. assert.equal(reader.readByte(), 0x80)
  237. assert.equal(reader.readByte(), 0)
  238. assert.equal(null, reader.readByte())
  239. })
  240. })
  241. describe("long strings", function() {
  242. it("can be parsed", function() {
  243. var buffer = Buffer.alloc(256)
  244. var string = "2;649;CN=Red Hat CS 71GA Demo,O=Red Hat CS 71GA Demo,C=US;"
  245. + "CN=RHCS Agent - admin01,UID=admin01,O=redhat,C=US [1] This is "
  246. + "Teena Vradmin's description."
  247. buffer[0] = 0x04
  248. buffer[1] = 0x81
  249. buffer[2] = 0x94
  250. buffer.write(string, 3)
  251. var reader = new BerReader(buffer.slice(0, 3 + string.length));
  252. assert.equal(reader.readString(), string)
  253. })
  254. })
  255. })