/tests/slice.js

https://github.com/ronomon/mime · JavaScript · 184 lines · 182 code · 2 blank · 0 comment · 34 complexity · 06ac49ec2672d54d251a12cf470c1f0c MD5 · raw file

  1. var common = require('./_common.js');
  2. var random = common.random;
  3. var Test = common.Test;
  4. var MIME = require('../index.js');
  5. var namespace = 'MIME.slice';
  6. function assertFlag(key, value) {
  7. if (typeof value !== 'number') throw key + ' must be a number';
  8. if (Math.floor(value) !== value) throw key + ' must be an integer';
  9. if (value < 1) throw key + ' must be at least 1';
  10. if (value === 1) return;
  11. if (value % 2 !== 0) throw key + ' must be divisible by 2';
  12. if ((value & (value - 1)) !== 0) throw key + ' must be a power of 2';
  13. }
  14. function generateASCII() {
  15. var length = Math.round(random() * 8);
  16. var buffer = Buffer.alloc(length);
  17. while (length--) buffer[length] = Math.floor(random() * 128);
  18. return buffer;
  19. }
  20. function generateBuffer() {
  21. var buffers = [];
  22. buffers.push(generateWhitespace());
  23. buffers.push(generateQuotes());
  24. buffers.push(generateASCII());
  25. buffers.push(generateQuotes());
  26. buffers.push(generateWhitespace());
  27. return Buffer.concat(buffers);
  28. }
  29. function generateQuotes() {
  30. if (random() < 0.5) return Buffer.alloc(0);
  31. var length = Math.round(random() * 4);
  32. var buffer = Buffer.alloc(length);
  33. while (length--) buffer[length] = random() < 0.5 ? 34 : 32;
  34. return buffer;
  35. }
  36. function generateWhitespace() {
  37. if (random() < 0.5) return Buffer.alloc(0);
  38. var map = [9, 10, 13, 32];
  39. var length = Math.round(random() * 8);
  40. var buffer = Buffer.alloc(length);
  41. while (length--) buffer[length] = map[Math.floor(random() * map.length)];
  42. return buffer;
  43. }
  44. function slice(source, sourceStart, sourceLength, flags) {
  45. var string = source.toString('binary', sourceStart, sourceLength);
  46. if (flags & MIME.TRIM) {
  47. string = string.replace(/^[\t\n\r ]+|[\t\n\r ]+$/g, '');
  48. }
  49. if (flags & MIME.LOWERCASE) {
  50. string = string.toLowerCase();
  51. } else if (flags & MIME.UPPERCASE) {
  52. string = string.toUpperCase();
  53. }
  54. var target = Buffer.from(string, 'binary');
  55. if (flags & MIME.ASCII) return target.toString('ascii');
  56. if (sourceStart === 0 && sourceLength === source.length) {
  57. if (target.equals(source)) return source;
  58. }
  59. return target;
  60. }
  61. var tests = 1000;
  62. while (tests--) {
  63. var source = generateBuffer();
  64. var sourceHash = common.hash(source);
  65. var sourceStart = 0;
  66. if (random() < 0.5) {
  67. sourceStart = Math.floor(random() * source.length);
  68. }
  69. var sourceLength = source.length;
  70. if (random() < 0.5) {
  71. var remainder = sourceLength - sourceStart;
  72. sourceLength -= Math.floor(random() * remainder);
  73. }
  74. if (sourceStart > sourceLength) {
  75. throw new Error('sourceStart > sourceLength');
  76. }
  77. if (sourceLength < 0) {
  78. throw new Error('sourceLength < 0');
  79. }
  80. var flags = 0;
  81. var ascii = false;
  82. if (random() < 0.4) {
  83. ascii = true;
  84. assertFlag('ASCII', MIME.ASCII);
  85. flags |= MIME.ASCII;
  86. Test.equal(
  87. flags & MIME.ASCII,
  88. MIME.ASCII,
  89. namespace,
  90. 'ASCII'
  91. );
  92. }
  93. var lowercase = false;
  94. var uppercase = false;
  95. if (random() < 0.4) {
  96. lowercase = true;
  97. assertFlag('LOWERCASE', MIME.LOWERCASE);
  98. flags |= MIME.LOWERCASE;
  99. Test.equal(
  100. flags & MIME.LOWERCASE,
  101. MIME.LOWERCASE,
  102. namespace,
  103. 'LOWERCASE'
  104. );
  105. } else if (random() < 0.4) {
  106. uppercase = true;
  107. assertFlag('UPPERCASE', MIME.UPPERCASE);
  108. flags |= MIME.UPPERCASE;
  109. Test.equal(
  110. flags & MIME.UPPERCASE,
  111. MIME.UPPERCASE,
  112. namespace,
  113. 'UPPERCASE'
  114. );
  115. }
  116. if (random() < 0.4) {
  117. assertFlag('TRIM', MIME.TRIM);
  118. flags |= MIME.TRIM;
  119. Test.equal(
  120. flags & MIME.TRIM,
  121. MIME.TRIM,
  122. namespace,
  123. 'TRIM'
  124. );
  125. }
  126. var targetActual = MIME.slice(
  127. source,
  128. sourceStart,
  129. sourceLength,
  130. flags
  131. );
  132. var targetExpected = slice(
  133. source,
  134. sourceStart,
  135. sourceLength,
  136. flags
  137. );
  138. Test.equal(common.hash(source), sourceHash, namespace, 'source');
  139. Test.equal(sourceStart, sourceStart, namespace, 'sourceStart');
  140. Test.equal(sourceLength, sourceLength, namespace, 'sourceLength');
  141. Test.equal(source.length, source.length, namespace, 'source.length');
  142. Test.equal(
  143. targetActual.length,
  144. targetExpected.length,
  145. namespace,
  146. 'target.length'
  147. );
  148. Test.equal(
  149. common.hash(targetActual),
  150. common.hash(targetExpected),
  151. namespace,
  152. 'target'
  153. );
  154. if (ascii) {
  155. Test.equal(typeof targetActual === 'string', true, namespace, 'string');
  156. var targetString = targetActual;
  157. } else {
  158. Test.equal(Buffer.isBuffer(targetActual), true, namespace, 'buffer');
  159. var targetString = targetActual.toString('binary');
  160. Test.equal(
  161. targetActual === source,
  162. targetActual.equals(source),
  163. namespace,
  164. 'pointer'
  165. );
  166. }
  167. if (lowercase) {
  168. Test.equal(
  169. /[A-Z]/.test(targetString),
  170. false,
  171. namespace,
  172. 'uppercase'
  173. );
  174. } else if (uppercase) {
  175. Test.equal(
  176. /[a-z]/.test(targetString),
  177. false,
  178. namespace,
  179. 'lowercase'
  180. );
  181. }
  182. }