Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

creatorTest.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. 'use strict';
  2. var fs = require('fs');
  3. var path = require('path');
  4. var nodeunit = require('nodeunit');
  5. var bplistParser = require('bplist-parser');
  6. var bplistCreator = require('../');
  7. module.exports = {
  8. // 'iTunes Small': function(test) {
  9. // var file = path.join(__dirname, "iTunes-small.bplist");
  10. // testFile(test, file);
  11. // },
  12. 'sample1': function(test) {
  13. var file = path.join(__dirname, "sample1.bplist");
  14. testFile(test, file);
  15. },
  16. 'sample2': function(test) {
  17. var file = path.join(__dirname, "sample2.bplist");
  18. testFile(test, file);
  19. },
  20. 'binary data': function(test) {
  21. var file = path.join(__dirname, "binaryData.bplist");
  22. testFile(test, file);
  23. },
  24. 'airplay': function(test) {
  25. var file = path.join(__dirname, "airplay.bplist");
  26. testFile(test, file);
  27. },
  28. // 'utf16': function(test) {
  29. // var file = path.join(__dirname, "utf16.bplist");
  30. // testFile(test, file);
  31. // },
  32. // 'uid': function(test) {
  33. // var file = path.join(__dirname, "uid.bplist");
  34. // testFile(test, file);
  35. // }
  36. };
  37. function testFile(test, file) {
  38. fs.readFile(file, function(err, fileData) {
  39. if (err) {
  40. return test.done(err);
  41. }
  42. bplistParser.parseFile(file, function(err, dicts) {
  43. if (err) {
  44. return test.done(err);
  45. }
  46. // airplay overrides
  47. if (dicts && dicts[0] && dicts[0].loadedTimeRanges && dicts[0].loadedTimeRanges[0] && dicts[0].loadedTimeRanges[0].hasOwnProperty('start')) {
  48. dicts[0].loadedTimeRanges[0].start = {
  49. bplistOverride: true,
  50. type: 'double',
  51. value: dicts[0].loadedTimeRanges[0].start
  52. };
  53. }
  54. if (dicts && dicts[0] && dicts[0].loadedTimeRanges && dicts[0].seekableTimeRanges[0] && dicts[0].seekableTimeRanges[0].hasOwnProperty('start')) {
  55. dicts[0].seekableTimeRanges[0].start = {
  56. bplistOverride: true,
  57. type: 'double',
  58. value: dicts[0].seekableTimeRanges[0].start
  59. };
  60. }
  61. if (dicts && dicts[0] && dicts[0].hasOwnProperty('rate')) {
  62. dicts[0].rate = {
  63. bplistOverride: true,
  64. type: 'double',
  65. value: dicts[0].rate
  66. };
  67. }
  68. // utf16
  69. if (dicts && dicts[0] && dicts[0].hasOwnProperty('NSHumanReadableCopyright')) {
  70. dicts[0].NSHumanReadableCopyright = {
  71. bplistOverride: true,
  72. type: 'string-utf16',
  73. value: dicts[0].NSHumanReadableCopyright
  74. };
  75. }
  76. if (dicts && dicts[0] && dicts[0].hasOwnProperty('CFBundleExecutable')) {
  77. dicts[0].CFBundleExecutable = {
  78. bplistOverride: true,
  79. type: 'string',
  80. value: dicts[0].CFBundleExecutable
  81. };
  82. }
  83. if (dicts && dicts[0] && dicts[0].CFBundleURLTypes && dicts[0].CFBundleURLTypes[0] && dicts[0].CFBundleURLTypes[0].hasOwnProperty('CFBundleURLSchemes')) {
  84. dicts[0].CFBundleURLTypes[0].CFBundleURLSchemes[0] = {
  85. bplistOverride: true,
  86. type: 'string',
  87. value: dicts[0].CFBundleURLTypes[0].CFBundleURLSchemes[0]
  88. };
  89. }
  90. if (dicts && dicts[0] && dicts[0].hasOwnProperty('CFBundleDisplayName')) {
  91. dicts[0].CFBundleDisplayName = {
  92. bplistOverride: true,
  93. type: 'string',
  94. value: dicts[0].CFBundleDisplayName
  95. };
  96. }
  97. if (dicts && dicts[0] && dicts[0].hasOwnProperty('DTPlatformBuild')) {
  98. dicts[0].DTPlatformBuild = {
  99. bplistOverride: true,
  100. type: 'string',
  101. value: dicts[0].DTPlatformBuild
  102. };
  103. }
  104. var buf = bplistCreator(dicts);
  105. compareBuffers(test, buf, fileData);
  106. return test.done();
  107. });
  108. });
  109. }
  110. function compareBuffers(test, buf1, buf2) {
  111. if (buf1.length !== buf2.length) {
  112. printBuffers(buf1, buf2);
  113. return test.fail("buffer size mismatch. found: " + buf1.length + ", expected: " + buf2.length + ".");
  114. }
  115. for (var i = 0; i < buf1.length; i++) {
  116. if (buf1[i] !== buf2[i]) {
  117. printBuffers(buf1, buf2);
  118. return test.fail("buffer mismatch at offset 0x" + i.toString(16) + ". found: 0x" + buf1[i].toString(16) + ", expected: 0x" + buf2[i].toString(16) + ".");
  119. }
  120. }
  121. }
  122. function printBuffers(buf1, buf2) {
  123. var i, t;
  124. for (var lineOffset = 0; lineOffset < buf1.length || lineOffset < buf2.length; lineOffset += 16) {
  125. var line = '';
  126. t = ('000000000' + lineOffset.toString(16));
  127. line += t.substr(t.length - 8) + ': ';
  128. for (i = 0; i < 16; i++) {
  129. if (i == 8) {
  130. line += ' ';
  131. }
  132. if (lineOffset + i < buf1.length) {
  133. t = ('00' + buf1[lineOffset + i].toString(16));
  134. line += t.substr(t.length - 2) + ' ';
  135. } else {
  136. line += ' ';
  137. }
  138. }
  139. line += ' ';
  140. for (i = 0; i < 16; i++) {
  141. if (lineOffset + i < buf1.length) {
  142. t = String.fromCharCode(buf1[lineOffset + i]);
  143. if (t < ' ' || t > '~') {
  144. t = '.';
  145. }
  146. line += t;
  147. } else {
  148. line += ' ';
  149. }
  150. }
  151. line += ' - ';
  152. for (i = 0; i < 16; i++) {
  153. if (i == 8) {
  154. line += ' ';
  155. }
  156. if (lineOffset + i < buf2.length) {
  157. t = ('00' + buf2[lineOffset + i].toString(16));
  158. line += t.substr(t.length - 2) + ' ';
  159. } else {
  160. line += ' ';
  161. }
  162. }
  163. line += ' ';
  164. for (i = 0; i < 16; i++) {
  165. if (lineOffset + i < buf2.length) {
  166. t = String.fromCharCode(buf2[lineOffset + i]);
  167. if (t < ' ' || t > '~') {
  168. t = '.';
  169. }
  170. line += t;
  171. } else {
  172. line += ' ';
  173. }
  174. }
  175. console.log(line);
  176. }
  177. }