header.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. var tap = require("tap")
  2. var TarHeader = require("../lib/header.js")
  3. var tar = require("../tar.js")
  4. var fs = require("fs")
  5. var headers =
  6. { "a.txt file header":
  7. [ "612e747874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030303036343420003035373736312000303030303234200030303030303030303430312031313635313336303333332030313234353100203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000757374617200303069736161637300000000000000000000000000000000000000000000000000007374616666000000000000000000000000000000000000000000000000000000303030303030200030303030303020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  8. , { cksumValid: true
  9. , path: 'a.txt'
  10. , mode: 420
  11. , uid: 24561
  12. , gid: 20
  13. , size: 257
  14. , mtime: 1319493851
  15. , cksum: 5417
  16. , type: '0'
  17. , linkpath: ''
  18. , ustar: 'ustar\0'
  19. , ustarver: '00'
  20. , uname: 'isaacs'
  21. , gname: 'staff'
  22. , devmaj: 0
  23. , devmin: 0
  24. , fill: '' }
  25. ]
  26. , "omega pax": // the extended header from omega tar.
  27. [ "5061784865616465722fcea92e74787400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030303036343420003035373736312000303030303234200030303030303030303137302031313534333731303631312030313530353100207800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000757374617200303069736161637300000000000000000000000000000000000000000000000000007374616666000000000000000000000000000000000000000000000000000000303030303030200030303030303020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  28. , { cksumValid: true
  29. , path: 'PaxHeader/Ω.txt'
  30. , mode: 420
  31. , uid: 24561
  32. , gid: 20
  33. , size: 120
  34. , mtime: 1301254537
  35. , cksum: 6697
  36. , type: 'x'
  37. , linkpath: ''
  38. , ustar: 'ustar\0'
  39. , ustarver: '00'
  40. , uname: 'isaacs'
  41. , gname: 'staff'
  42. , devmaj: 0
  43. , devmin: 0
  44. , fill: '' } ]
  45. , "omega file header":
  46. [ "cea92e7478740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030303036343420003035373736312000303030303234200030303030303030303030322031313534333731303631312030313330373200203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000757374617200303069736161637300000000000000000000000000000000000000000000000000007374616666000000000000000000000000000000000000000000000000000000303030303030200030303030303020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  47. , { cksumValid: true
  48. , path: 'Ω.txt'
  49. , mode: 420
  50. , uid: 24561
  51. , gid: 20
  52. , size: 2
  53. , mtime: 1301254537
  54. , cksum: 5690
  55. , type: '0'
  56. , linkpath: ''
  57. , ustar: 'ustar\0'
  58. , ustarver: '00'
  59. , uname: 'isaacs'
  60. , gname: 'staff'
  61. , devmaj: 0
  62. , devmin: 0
  63. , fill: '' } ]
  64. , "foo.js file header":
  65. [ "666f6f2e6a730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030303036343420003035373736312000303030303234200030303030303030303030342031313534333637303734312030313236313700203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000757374617200303069736161637300000000000000000000000000000000000000000000000000007374616666000000000000000000000000000000000000000000000000000000303030303030200030303030303020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  66. , { cksumValid: true
  67. , path: 'foo.js'
  68. , mode: 420
  69. , uid: 24561
  70. , gid: 20
  71. , size: 4
  72. , mtime: 1301246433
  73. , cksum: 5519
  74. , type: '0'
  75. , linkpath: ''
  76. , ustar: 'ustar\0'
  77. , ustarver: '00'
  78. , uname: 'isaacs'
  79. , gname: 'staff'
  80. , devmaj: 0
  81. , devmin: 0
  82. , fill: '' }
  83. ]
  84. , "b.txt file header":
  85. [ "622e747874000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030303036343420003035373736312000303030303234200030303030303030313030302031313635313336303637372030313234363100203000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000757374617200303069736161637300000000000000000000000000000000000000000000000000007374616666000000000000000000000000000000000000000000000000000000303030303030200030303030303020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  86. , { cksumValid: true
  87. , path: 'b.txt'
  88. , mode: 420
  89. , uid: 24561
  90. , gid: 20
  91. , size: 512
  92. , mtime: 1319494079
  93. , cksum: 5425
  94. , type: '0'
  95. , linkpath: ''
  96. , ustar: 'ustar\0'
  97. , ustarver: '00'
  98. , uname: 'isaacs'
  99. , gname: 'staff'
  100. , devmaj: 0
  101. , devmin: 0
  102. , fill: '' }
  103. ]
  104. , "deep nested file":
  105. [ "636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363633030303634342000303537373631200030303030323420003030303030303030313434203131363532313531353333203034333331340020300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000075737461720030306973616163730000000000000000000000000000000000000000000000000000737461666600000000000000000000000000000000000000000000000000000030303030303020003030303030302000722f652f612f6c2f6c2f792f2d2f642f652f652f702f2d2f662f6f2f6c2f642f652f722f2d2f702f612f742f680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
  106. , { cksumValid: true,
  107. path: 'r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-/p/a/t/h/cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'
  108. , mode: 420
  109. , uid: 24561
  110. , gid: 20
  111. , size: 100
  112. , mtime: 1319687003
  113. , cksum: 18124
  114. , type: '0'
  115. , linkpath: ''
  116. , ustar: 'ustar\0'
  117. , ustarver: '00'
  118. , uname: 'isaacs'
  119. , gname: 'staff'
  120. , devmaj: 0
  121. , devmin: 0
  122. , fill: '' }
  123. ]
  124. }
  125. tap.test("parsing", function (t) {
  126. Object.keys(headers).forEach(function (name) {
  127. var h = headers[name]
  128. , header = new Buffer(h[0], "hex")
  129. , expect = h[1]
  130. , parsed = new TarHeader(header)
  131. // console.error(parsed)
  132. t.has(parsed, expect, "parse " + name)
  133. })
  134. t.end()
  135. })
  136. tap.test("encoding", function (t) {
  137. Object.keys(headers).forEach(function (name) {
  138. var h = headers[name]
  139. , expect = new Buffer(h[0], "hex")
  140. , encoded = TarHeader.encode(h[1])
  141. // might have slightly different bytes, since the standard
  142. // isn't very strict, but should have the same semantics
  143. // checkSum will be different, but cksumValid will be true
  144. var th = new TarHeader(encoded)
  145. delete h[1].block
  146. delete h[1].needExtended
  147. delete h[1].cksum
  148. t.has(th, h[1], "fields "+name)
  149. })
  150. t.end()
  151. })
  152. // test these manually. they're a bit rare to find in the wild
  153. tap.test("parseNumeric tests", function (t) {
  154. var parseNumeric = TarHeader.parseNumeric
  155. , numbers =
  156. { "303737373737373700": 2097151
  157. , "30373737373737373737373700": 8589934591
  158. , "303030303036343400": 420
  159. , "800000ffffffffffff": 281474976710655
  160. , "ffffff000000000001": -281474976710654
  161. , "ffffff000000000000": -281474976710655
  162. , "800000000000200000": 2097152
  163. , "8000000000001544c5": 1393861
  164. , "ffffffffffff1544c5": -15383354 }
  165. Object.keys(numbers).forEach(function (n) {
  166. var b = new Buffer(n, "hex")
  167. t.equal(parseNumeric(b), numbers[n], n + " === " + numbers[n])
  168. })
  169. t.end()
  170. })