Node-Red configuration
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

unittests_lib-ber-writer.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. var asn1 = require("../")
  2. var assert = require("assert")
  3. var BerWriter = asn1.BerWriter
  4. describe("lib/ber/writer.js", function() {
  5. describe("writeByte()", function() {
  6. it("can write a value", function() {
  7. var writer = new BerWriter()
  8. writer.writeByte(0xC2)
  9. var buffer = writer.buffer
  10. assert.equal(buffer.length, 1)
  11. assert.equal(buffer[0], 0xc2)
  12. })
  13. })
  14. describe("writeInt()", function() {
  15. it("can write zero", function() {
  16. var writer = new BerWriter()
  17. writer.writeInt(0)
  18. var buffer = writer.buffer
  19. assert.equal(buffer.length, 3)
  20. assert.equal(buffer[0], 0x02)
  21. assert.equal(buffer[1], 0x01)
  22. assert.equal(buffer[2], 0x00)
  23. })
  24. it("can write 1 byte positive integers - lowest", function() {
  25. var writer = new BerWriter()
  26. writer.writeInt(1)
  27. var buffer = writer.buffer
  28. assert.equal(buffer.length, 3)
  29. assert.equal(buffer[0], 0x02)
  30. assert.equal(buffer[1], 0x01)
  31. assert.equal(buffer[2], 0x01)
  32. })
  33. it("can write 1 byte positive integers - middle", function() {
  34. var writer = new BerWriter()
  35. writer.writeInt(101)
  36. var buffer = writer.buffer
  37. assert.equal(buffer.length, 3)
  38. assert.equal(buffer[0], 0x02)
  39. assert.equal(buffer[1], 0x01)
  40. assert.equal(buffer[2], 0x65)
  41. })
  42. it("can write 1 byte positive integers - highest", function() {
  43. var writer = new BerWriter()
  44. writer.writeInt(127)
  45. var buffer = writer.buffer
  46. assert.equal(buffer.length, 3)
  47. assert.equal(buffer[0], 0x02)
  48. assert.equal(buffer[1], 0x01)
  49. assert.equal(buffer[2], 0x7f)
  50. })
  51. it("can write 2 byte positive integers - lowest", function() {
  52. var writer = new BerWriter()
  53. writer.writeInt(128)
  54. var buffer = writer.buffer
  55. assert.equal(buffer.length, 4)
  56. assert.equal(buffer[0], 0x02)
  57. assert.equal(buffer[1], 0x02)
  58. assert.equal(buffer[2], 0x00)
  59. assert.equal(buffer[3], 0x80)
  60. })
  61. it("can write 2 byte positive integers - middle", function() {
  62. var writer = new BerWriter()
  63. writer.writeInt(9374)
  64. var buffer = writer.buffer
  65. assert.equal(buffer.length, 4)
  66. assert.equal(buffer[0], 0x02)
  67. assert.equal(buffer[1], 0x02)
  68. assert.equal(buffer[2], 0x24)
  69. assert.equal(buffer[3], 0x9e)
  70. })
  71. it("can write 2 byte positive integers - highest", function() {
  72. var writer = new BerWriter()
  73. writer.writeInt(32767)
  74. var buffer = writer.buffer
  75. assert.equal(buffer.length, 4)
  76. assert.equal(buffer[0], 0x02)
  77. assert.equal(buffer[1], 0x02)
  78. assert.equal(buffer[2], 0x7f)
  79. assert.equal(buffer[3], 0xff)
  80. })
  81. it("can write 3 byte positive integers - lowest", function() {
  82. var writer = new BerWriter()
  83. writer.writeInt(32768)
  84. var buffer = writer.buffer
  85. assert.equal(buffer.length, 5)
  86. assert.equal(buffer[0], 0x02)
  87. assert.equal(buffer[1], 0x03)
  88. assert.equal(buffer[2], 0x00)
  89. assert.equal(buffer[3], 0x80)
  90. assert.equal(buffer[4], 0x00)
  91. })
  92. it("can write 3 byte positive integers - middle", function() {
  93. var writer = new BerWriter()
  94. writer.writeInt(5938243)
  95. var buffer = writer.buffer
  96. assert.equal(buffer.length, 5)
  97. assert.equal(buffer[0], 0x02)
  98. assert.equal(buffer[1], 0x03)
  99. assert.equal(buffer[2], 0x5a)
  100. assert.equal(buffer[3], 0x9c)
  101. assert.equal(buffer[4], 0x43)
  102. })
  103. it("can write 3 byte positive integers - highest", function() {
  104. var writer = new BerWriter()
  105. writer.writeInt(8388607)
  106. var buffer = writer.buffer
  107. assert.equal(buffer.length, 5)
  108. assert.equal(buffer[0], 0x02)
  109. assert.equal(buffer[1], 0x03)
  110. assert.equal(buffer[2], 0x7f)
  111. assert.equal(buffer[3], 0xff)
  112. assert.equal(buffer[4], 0xff)
  113. })
  114. it("can write 4 byte positive integers - lowest", function() {
  115. var writer = new BerWriter()
  116. writer.writeInt(8388608)
  117. var buffer = writer.buffer
  118. assert.equal(buffer.length, 6)
  119. assert.equal(buffer[0], 0x02)
  120. assert.equal(buffer[1], 0x04)
  121. assert.equal(buffer[2], 0x00)
  122. assert.equal(buffer[3], 0x80)
  123. assert.equal(buffer[4], 0x00)
  124. assert.equal(buffer[5], 0x00)
  125. })
  126. it("can write 4 byte positive integers - middle", function() {
  127. var writer = new BerWriter()
  128. writer.writeInt(1483722690)
  129. var buffer = writer.buffer
  130. assert.equal(buffer.length, 6)
  131. assert.equal(buffer[0], 0x02)
  132. assert.equal(buffer[1], 0x04)
  133. assert.equal(buffer[2], 0x58)
  134. assert.equal(buffer[3], 0x6f)
  135. assert.equal(buffer[4], 0xcf)
  136. assert.equal(buffer[5], 0xc2)
  137. })
  138. it("can write 4 byte positive integers - highest", function() {
  139. var writer = new BerWriter()
  140. writer.writeInt(2147483647)
  141. var buffer = writer.buffer
  142. assert.equal(buffer.length, 6)
  143. assert.equal(buffer[0], 0x02)
  144. assert.equal(buffer[1], 0x04)
  145. assert.equal(buffer[2], 0x7f)
  146. assert.equal(buffer[3], 0xff)
  147. assert.equal(buffer[4], 0xff)
  148. assert.equal(buffer[5], 0xff)
  149. })
  150. it("can write 5 byte positive integers - lowest", function() {
  151. var writer = new BerWriter()
  152. writer.writeInt(2147483648)
  153. var buffer = writer.buffer
  154. assert.equal(buffer.length, 7)
  155. assert.equal(buffer[0], 0x02)
  156. assert.equal(buffer[1], 0x05)
  157. assert.equal(buffer[2], 0x00)
  158. assert.equal(buffer[3], 0x80)
  159. assert.equal(buffer[4], 0x00)
  160. assert.equal(buffer[5], 0x00)
  161. assert.equal(buffer[6], 0x00)
  162. })
  163. it("can write 5 byte positive integers - middle", function() {
  164. var writer = new BerWriter()
  165. writer.writeInt(3843548325)
  166. var buffer = writer.buffer
  167. assert.equal(buffer.length, 7)
  168. assert.equal(buffer[0], 0x02)
  169. assert.equal(buffer[1], 0x05)
  170. assert.equal(buffer[2], 0x00)
  171. assert.equal(buffer[3], 0xe5)
  172. assert.equal(buffer[4], 0x17)
  173. assert.equal(buffer[5], 0xe4)
  174. assert.equal(buffer[6], 0xa5)
  175. })
  176. it("can write 5 byte positive integers - highest", function() {
  177. var writer = new BerWriter()
  178. writer.writeInt(4294967295)
  179. var buffer = writer.buffer
  180. assert.equal(buffer.length, 7)
  181. assert.equal(buffer[0], 0x02)
  182. assert.equal(buffer[1], 0x05)
  183. assert.equal(buffer[2], 0x00)
  184. assert.equal(buffer[3], 0xff)
  185. assert.equal(buffer[4], 0xff)
  186. assert.equal(buffer[5], 0xff)
  187. assert.equal(buffer[6], 0xff)
  188. })
  189. it("can write 1 byte negative integers - lowest", function() {
  190. var writer = new BerWriter()
  191. writer.writeInt(-128)
  192. var buffer = writer.buffer
  193. assert.equal(buffer.length, 3)
  194. assert.equal(buffer[0], 0x02)
  195. assert.equal(buffer[1], 0x01)
  196. assert.equal(buffer[2], 0x80)
  197. })
  198. it("can write 1 byte negative integers - middle", function() {
  199. var writer = new BerWriter()
  200. writer.writeInt(-73)
  201. var buffer = writer.buffer
  202. assert.equal(buffer.length, 3)
  203. assert.equal(buffer[0], 0x02)
  204. assert.equal(buffer[1], 0x01)
  205. assert.equal(buffer[2], 0xb7)
  206. })
  207. it("can write 1 byte negative integers - highest", function() {
  208. var writer = new BerWriter()
  209. writer.writeInt(-1)
  210. var buffer = writer.buffer
  211. assert.equal(buffer.length, 3)
  212. assert.equal(buffer[0], 0x02)
  213. assert.equal(buffer[1], 0x01)
  214. assert.equal(buffer[2], 0xff)
  215. })
  216. it("can write 2 byte negative integers - lowest", function() {
  217. var writer = new BerWriter()
  218. writer.writeInt(-32768)
  219. var buffer = writer.buffer
  220. assert.equal(buffer.length, 4)
  221. assert.equal(buffer[0], 0x02)
  222. assert.equal(buffer[1], 0x02)
  223. assert.equal(buffer[2], 0x80)
  224. assert.equal(buffer[3], 0x00)
  225. })
  226. it("can write 2 byte negative integers - middle", function() {
  227. var writer = new BerWriter()
  228. writer.writeInt(-22400)
  229. var buffer = writer.buffer
  230. assert.equal(buffer.length, 4)
  231. assert.equal(buffer[0], 0x02)
  232. assert.equal(buffer[1], 0x02)
  233. assert.equal(buffer[2], 0xa8)
  234. assert.equal(buffer[3], 0x80)
  235. })
  236. it("can write 2 byte negative integers - highest", function() {
  237. var writer = new BerWriter()
  238. writer.writeInt(-129)
  239. var buffer = writer.buffer
  240. assert.equal(buffer.length, 4)
  241. assert.equal(buffer[0], 0x02)
  242. assert.equal(buffer[1], 0x02)
  243. assert.equal(buffer[2], 0xff)
  244. assert.equal(buffer[3], 0x7f)
  245. })
  246. it("can write 3 byte negative integers - lowest", function() {
  247. var writer = new BerWriter()
  248. writer.writeInt(-8388608)
  249. var buffer = writer.buffer
  250. assert.equal(buffer.length, 5)
  251. assert.equal(buffer[0], 0x02)
  252. assert.equal(buffer[1], 0x03)
  253. assert.equal(buffer[2], 0x80)
  254. assert.equal(buffer[3], 0x00)
  255. assert.equal(buffer[4], 0x00)
  256. })
  257. it("can write 3 byte negative integers - middle", function() {
  258. var writer = new BerWriter()
  259. writer.writeInt(-481653)
  260. var buffer = writer.buffer
  261. assert.equal(buffer.length, 5)
  262. assert.equal(buffer[0], 0x02)
  263. assert.equal(buffer[1], 0x03)
  264. assert.equal(buffer[2], 0xf8)
  265. assert.equal(buffer[3], 0xa6)
  266. assert.equal(buffer[4], 0x8b)
  267. })
  268. it("can write 3 byte negative integers - highest", function() {
  269. var writer = new BerWriter()
  270. writer.writeInt(-32769)
  271. var buffer = writer.buffer
  272. assert.equal(buffer.length, 5)
  273. assert.equal(buffer[0], 0x02)
  274. assert.equal(buffer[1], 0x03)
  275. assert.equal(buffer[2], 0xff)
  276. assert.equal(buffer[3], 0x7f)
  277. assert.equal(buffer[4], 0xff)
  278. })
  279. it("can write 4 byte negative integers - lowest", function() {
  280. var writer = new BerWriter()
  281. writer.writeInt(-2147483648)
  282. var buffer = writer.buffer
  283. assert.equal(buffer.length, 6)
  284. assert.equal(buffer[0], 0x02)
  285. assert.equal(buffer[1], 0x04)
  286. assert.equal(buffer[2], 0x80)
  287. assert.equal(buffer[3], 0x00)
  288. assert.equal(buffer[4], 0x00)
  289. assert.equal(buffer[5], 0x00)
  290. })
  291. it("can write 4 byte negative integers - middle", function() {
  292. var writer = new BerWriter()
  293. writer.writeInt(-1522904131)
  294. var buffer = writer.buffer
  295. assert.equal(buffer.length, 6)
  296. assert.equal(buffer[0], 0x02)
  297. assert.equal(buffer[1], 0x04)
  298. assert.equal(buffer[2], 0xa5)
  299. assert.equal(buffer[3], 0x3a)
  300. assert.equal(buffer[4], 0x53)
  301. assert.equal(buffer[5], 0xbd)
  302. })
  303. it("can write 4 byte negative integers - highest", function() {
  304. var writer = new BerWriter()
  305. writer.writeInt(-8388609)
  306. var buffer = writer.buffer
  307. assert.equal(buffer.length, 6)
  308. assert.equal(buffer[0], 0x02)
  309. assert.equal(buffer[1], 0x04)
  310. assert.equal(buffer[2], 0xff)
  311. assert.equal(buffer[3], 0x7f)
  312. assert.equal(buffer[4], 0xff)
  313. assert.equal(buffer[5], 0xff)
  314. })
  315. })
  316. describe("writeBoolean()", function() {
  317. it("can write a true and false value", function() {
  318. var writer = new BerWriter()
  319. writer.writeBoolean(true)
  320. writer.writeBoolean(false)
  321. var buffer = writer.buffer
  322. assert.equal(buffer.length, 6)
  323. assert.equal(buffer[0], 0x01)
  324. assert.equal(buffer[1], 0x01)
  325. assert.equal(buffer[2], 0xff)
  326. assert.equal(buffer[3], 0x01)
  327. assert.equal(buffer[4], 0x01)
  328. assert.equal(buffer[5], 0x00)
  329. })
  330. })
  331. describe("writeString()", function() {
  332. it("can write a value", function() {
  333. var writer = new BerWriter()
  334. writer.writeString("hello world")
  335. var buffer = writer.buffer
  336. assert.equal(buffer.length, 13)
  337. assert.equal(buffer[0], 0x04)
  338. assert.equal(buffer[1], 11)
  339. assert.equal(buffer.slice(2).toString("utf8"), "hello world")
  340. })
  341. })
  342. describe("writeBuffer()", function() {
  343. it("can write a value", function() {
  344. var writer = new BerWriter()
  345. writer.writeString("hello world")
  346. var expected = Buffer.from([
  347. 0x04, 0x0b, 0x30, 0x09, 0x02, 0x01, 0x0f, 0x01,
  348. 0x01, 0xff, 0x01, 0x01, 0xff
  349. ])
  350. writer.writeBuffer(expected.slice(2, expected.length), 0x04)
  351. buffer = writer.buffer;
  352. assert.equal(buffer.length, 26)
  353. assert.equal(buffer[0], 0x04)
  354. assert.equal(buffer[1], 11)
  355. assert.equal(buffer.slice(2, 13).toString("utf8"), "hello world")
  356. assert.equal(buffer[13], expected[0])
  357. assert.equal(buffer[14], expected[1])
  358. for (var i = 13, j = 0; i < buffer.length && j < expected.length; i++, j++)
  359. assert.equal(buffer[i], expected[j])
  360. })
  361. })
  362. describe("writeStringArray()", function() {
  363. it("can write an array of strings", function() {
  364. var writer = new BerWriter()
  365. writer.writeStringArray(["hello world", "fubar!"])
  366. var buffer = writer.buffer
  367. assert.equal(buffer.length, 21)
  368. assert.equal(buffer[0], 0x04)
  369. assert.equal(buffer[1], 11)
  370. assert.equal(buffer.slice(2, 13).toString("utf8"), "hello world")
  371. assert.equal(buffer[13], 0x04)
  372. assert.equal(buffer[14], 6)
  373. assert.equal(buffer.slice(15).toString("utf8"), "fubar!")
  374. })
  375. })
  376. describe("oversized data", function() {
  377. it("results in a buffer resize", function() {
  378. var writer = new BerWriter({size: 2})
  379. writer.writeString("hello world")
  380. var buffer = writer.buffer
  381. assert.equal(buffer.length, 13)
  382. assert.equal(buffer[0], 0x04)
  383. assert.equal(buffer[1], 11)
  384. assert.equal(buffer.slice(2).toString("utf8"), "hello world")
  385. })
  386. })
  387. describe("complex sequences", function() {
  388. it("are processed correctly", function() {
  389. var writer = new BerWriter({size: 25})
  390. writer.startSequence()
  391. writer.writeString("hello world")
  392. writer.endSequence()
  393. var buffer = writer.buffer
  394. assert.equal(buffer.length, 15)
  395. assert.equal(buffer[0], 0x30)
  396. assert.equal(buffer[1], 13)
  397. assert.equal(buffer[2], 0x04)
  398. assert.equal(buffer[3], 11)
  399. assert.equal(buffer.slice(4).toString("utf8"), "hello world")
  400. })
  401. })
  402. describe("nested sequences", function() {
  403. it("are processed correctly", function() {
  404. var writer = new BerWriter({size: 25})
  405. writer.startSequence()
  406. writer.writeString("hello world")
  407. writer.startSequence()
  408. writer.writeString("hello world")
  409. writer.endSequence()
  410. writer.endSequence()
  411. var buffer = writer.buffer
  412. assert.equal(buffer.length, 30)
  413. assert.equal(buffer[0], 0x30)
  414. assert.equal(buffer[1], 28)
  415. assert.equal(buffer[2], 0x04)
  416. assert.equal(buffer[3], 11)
  417. assert.equal(buffer.slice(4, 15).toString("utf8"), "hello world")
  418. assert.equal(buffer[15], 0x30)
  419. assert.equal(buffer[16], 13)
  420. assert.equal(buffer[17], 0x04)
  421. assert.equal(buffer[18], 11)
  422. assert.equal(buffer.slice(19, 30).toString("utf8"), "hello world")
  423. })
  424. })
  425. describe("multiple sequences", function() {
  426. it("are processed correctly", function() {
  427. // An anonymous LDAP v3 BIND request
  428. var dn = "cn=foo,ou=unit,o=test"
  429. var writer = new BerWriter()
  430. writer.startSequence()
  431. writer.writeInt(3)
  432. writer.startSequence(0x60)
  433. writer.writeInt(3)
  434. writer.writeString(dn)
  435. writer.writeByte(0x80)
  436. writer.writeByte(0x00)
  437. writer.endSequence()
  438. writer.endSequence()
  439. var buffer = writer.buffer
  440. assert.equal(buffer.length, 35)
  441. assert.equal(buffer[0], 0x30)
  442. assert.equal(buffer[1], 33)
  443. assert.equal(buffer[2], 0x02)
  444. assert.equal(buffer[3], 1)
  445. assert.equal(buffer[4], 0x03)
  446. assert.equal(buffer[5], 0x60)
  447. assert.equal(buffer[6], 28)
  448. assert.equal(buffer[7], 0x02)
  449. assert.equal(buffer[8], 1)
  450. assert.equal(buffer[9], 0x03)
  451. assert.equal(buffer[10], 0x04)
  452. assert.equal(buffer[11], dn.length)
  453. assert.equal(buffer.slice(12, 33).toString("utf8"), dn)
  454. assert.equal(buffer[33], 0x80)
  455. assert.equal(buffer[34], 0x00)
  456. })
  457. })
  458. describe("writeOID()", function() {
  459. it("can write a value", function() {
  460. var writer = new BerWriter()
  461. writer.writeOID("1.2.840.113549.1.1.1")
  462. var buffer = writer.buffer
  463. assert.equal(buffer.toString("hex"), "06092a864886f70d010101")
  464. })
  465. })
  466. })