test-object-codec.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. var modes = require('../lib/modes.js');
  2. var bodec = require('bodec');
  3. var sha1 = require('git-sha1');
  4. var run = require('./run.js');
  5. // The thing we mean to test.
  6. var codec = require('../lib/object-codec.js');
  7. var blobHash, treeHash, commitHash, tagHash;
  8. var blob, tree, commit, tag;
  9. var blobBin, treeBin, commitBin, tagBin;
  10. run([
  11. function testEncodeBlob() {
  12. blob = bodec.fromUnicode("Hello World\n");
  13. blobBin = codec.frame({type: "blob", body: blob});
  14. blobHash = sha1(blobBin);
  15. if (blobHash !== '557db03de997c86a4a028e1ebd3a1ceb225be238') {
  16. throw new Error("Invalid blob hash");
  17. }
  18. },
  19. function testEncodeBlobInvalidType() {
  20. try {
  21. codec.frame({type: "blob", body: "Not a binary value"});
  22. }
  23. catch (err) {
  24. return;
  25. }
  26. throw new Error("Expected an error when passin in a non-binary blob");
  27. },
  28. function testEncodeTree() {
  29. tree = {
  30. "greeting.txt": {
  31. mode: modes.file,
  32. hash: blobHash
  33. }
  34. };
  35. treeBin = codec.frame({type: "tree", body: tree});
  36. treeHash = sha1(treeBin);
  37. if (treeHash !== "648fc86e8557bdabbc2c828a19535f833727fa62") {
  38. throw new Error("Invalid tree hash");
  39. }
  40. },
  41. function testTreeSort() {
  42. var tree = {
  43. "README.md": {"mode":modes.blob,"hash":"42bd87a816800cb87646e95b71273983a71a26dc"},
  44. "a.js": {"mode":modes.blob,"hash":"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"},
  45. "a-js": {"mode":modes.blob,"hash":"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"},
  46. "b": {"mode":modes.blob,"hash":"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"},
  47. "b-js": {"mode":modes.blob,"hash":"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"},
  48. "c": {"mode":modes.blob,"hash":"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"},
  49. "c.js": {"mode":modes.blob,"hash":"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391"},
  50. "a": {"mode":modes.tree,"hash":"496d6428b9cf92981dc9495211e6e1120fb6f2ba"},
  51. "b.js": {"mode":modes.tree,"hash":"496d6428b9cf92981dc9495211e6e1120fb6f2ba"},
  52. "c-js": {"mode":modes.tree,"hash":"496d6428b9cf92981dc9495211e6e1120fb6f2ba"},
  53. };
  54. var treeBin = codec.frame({type: "tree", body: tree});
  55. var treeHash = sha1(treeBin);
  56. if (treeHash !== "f78893bf52bc695f343372d4210c8c0803c7c4db") {
  57. throw new Error("Invalid tree hash");
  58. }
  59. },
  60. function testEncodeCommit() {
  61. var person = {
  62. name: "Tim Caswell",
  63. email: "tim@creationix.com",
  64. date: {
  65. seconds: 1391790884,
  66. offset: 7 * 60
  67. }
  68. };
  69. commit = {
  70. tree: treeHash,
  71. author: person,
  72. committer: person,
  73. message: "Test Commit\n",
  74. parents: []
  75. };
  76. commitBin = codec.frame({type: "commit", body: commit});
  77. commitHash = sha1(commitBin);
  78. if (commitHash !== "500c37fc17988b90c82d812a2d6fc25b15354bf2") {
  79. throw new Error("Invalid commit hash");
  80. }
  81. },
  82. function testEncodeTag() {
  83. tag = {
  84. object: commitHash,
  85. type: "commit",
  86. tag: "mytag",
  87. tagger: {
  88. name: "Tim Caswell",
  89. email: "tim@creationix.com",
  90. date: {
  91. seconds: 1391790910,
  92. offset: 7 * 60
  93. }
  94. },
  95. message: "Tag it!\n"
  96. };
  97. tagBin = codec.frame({type: "tag", body: tag});
  98. tagHash = sha1(tagBin);
  99. if (tagHash !== "49522787662a0183652dc9cafa5c008b5a0e0c2a") {
  100. throw new Error("Invalid tag hash");
  101. }
  102. },
  103. function testDecodeTag() {
  104. var obj = codec.deframe(tagBin, true);
  105. if (obj.type !== "tag") throw new Error("Invalid type");
  106. if (!(obj.body.object === tag.object && obj.body.message === tag.message)) {
  107. throw new Error("Problem decoding");
  108. }
  109. },
  110. function testDecodeCommit() {
  111. var obj = codec.deframe(commitBin, true);
  112. if (obj.type !== "commit") throw new Error("Invalid type");
  113. if (!(obj.body.tree === commit.tree &&
  114. obj.body.message === commit.message &&
  115. obj.body.author.date.seconds === commit.author.date.seconds)) {
  116. throw new Error("Problem decoding");
  117. }
  118. },
  119. function testDecodeTree() {
  120. var obj = codec.deframe(treeBin, true);
  121. if (obj.type !== "tree") throw new Error("Invalid type");
  122. if (obj.body["greeting.txt"].hash !== tree["greeting.txt"].hash) {
  123. throw new Error("Problem decoding");
  124. }
  125. },
  126. function testDecodeBlob() {
  127. var obj = codec.deframe(blobBin, true);
  128. if (obj.type !== "blob") throw new Error("Invalid type");
  129. if (bodec.toUnicode(obj.body) !== bodec.toUnicode(blob)) {
  130. throw new Error("Problem decoding");
  131. }
  132. },
  133. function testUnicodeFilePath() {
  134. var name = "æðelen";
  135. var tree = {};
  136. tree[name] = {
  137. mode: modes.file,
  138. hash: blobHash
  139. };
  140. var bin = codec.frame({type:"tree", body: tree});
  141. var obj = codec.deframe(bin, true);
  142. var newName = Object.keys(obj.body)[0];
  143. if (newName !== name) {
  144. console.log(newName + " != " + name);
  145. throw new Error("Problem storing and retrieving utf8 paths");
  146. }
  147. if (obj.body[name].hash !== tree[name].hash) {
  148. throw new Error("Problem decoding hash hex");
  149. }
  150. },
  151. function testUnicodeCommit() {
  152. var person = {
  153. name: "Laȝamon",
  154. email: "laȝamon@chronicles-of-england.org",
  155. date: {
  156. seconds: 1391790910,
  157. offset: 7 * 60
  158. }
  159. };
  160. var commit = {
  161. tree: treeHash,
  162. author: person,
  163. committer: person,
  164. message: "An preost wes on leoden, Laȝamon was ihoten\nHe wes Leovenaðes sone -- liðe him be Drihten\n",
  165. parents: []
  166. };
  167. var bin = codec.frame({type:"commit", body:commit});
  168. var obj = codec.deframe(bin, true);
  169. if (commit.author.name !== obj.body.author.name ||
  170. commit.author.email !== obj.body.author.email ||
  171. commit.message !== obj.body.message) {
  172. console.log([obj.body.author, obj.body.message]);
  173. throw new Error("Problem decoding utf8 parts in commit");
  174. }
  175. },
  176. function testUnicodeTag() {
  177. var tag = {
  178. object: commitHash,
  179. type: "commit",
  180. tag: "Laȝamon",
  181. tagger: {
  182. name: "Laȝamon",
  183. email: "laȝamon@chronicles-of-england.org",
  184. date: {
  185. seconds: 1391790910,
  186. offset: 7 * 60
  187. }
  188. },
  189. message: "He wonede at Ernleȝe at æðelen are chirechen,\nUppen Sevarne staþe, sel þar him þuhte,\nOnfest Radestone, þer he bock radde.\n"
  190. };
  191. var bin = codec.frame({type:"tag", body:tag});
  192. var obj = codec.deframe(bin, true);
  193. if (tag.tagger.name !== obj.body.tagger.name ||
  194. tag.tagger.email !== obj.body.tagger.email ||
  195. tag.message !== obj.body.message) {
  196. console.log([obj.body.tagger, obj.body.message]);
  197. throw new Error("Problem decoding utf8 parts in tag");
  198. }
  199. },
  200. function testBinaryBlob() {
  201. var blob = bodec.create(256);
  202. for (var i = 0; i < 256; i++) { blob[i] = i; }
  203. var bin = codec.frame({type:"blob",body:blob});
  204. var obj = codec.deframe(bin, true);
  205. if (bodec.toRaw(blob) !== bodec.toRaw(obj.body)) {
  206. throw new Error("Problem decoding binary blob");
  207. }
  208. }
  209. ]);