Ver Fonte

Merge pull request #12 from hazzik/tests

Add tests
Martin Kleppe há 11 anos atrás
pai
commit
256fc8b7e9
7 ficheiros alterados com 228 adições e 14 exclusões
  1. 1 0
      .gitignore
  2. 8 0
      .npmignore
  3. 56 0
      Gruntfile.js
  4. 16 14
      jsfuck.js
  5. 94 0
      output.txt
  6. 6 0
      package.json
  7. 47 0
      test/jsfuck_test.js

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
+node_modules
 .DS_Store

+ 8 - 0
.npmignore

@@ -0,0 +1,8 @@
+docs
+test
+.DS_Store
+.editorconfig
+.gitattributes
+.jshintrc
+.travis.yml
+Gruntfile.js

+ 56 - 0
Gruntfile.js

@@ -0,0 +1,56 @@
+/*global module:false*/
+module.exports = function(grunt) {
+
+  // Project configuration.
+  grunt.initConfig({
+    // Task configuration.
+    jshint: {
+      options: {
+        curly: true,
+        eqeqeq: true,
+        immed: true,
+        latedef: true,
+        newcap: true,
+        noarg: true,
+        sub: true,
+        undef: true,
+        unused: true,
+        boss: true,
+        eqnull: true,
+        node: true,
+        globals: {
+          jQuery: true,
+          window: true,
+        }
+      },
+      gruntfile: {
+        src: 'Gruntfile.js'
+      },
+      lib_test: {
+        src: ['jsfuck.js', 'lib/**/*.js', 'test/**/*.js']
+      }
+    },
+    nodeunit: {
+      files: ['test/**/*_test.js']
+    },
+    watch: {
+      gruntfile: {
+        files: '<%= jshint.gruntfile.src %>',
+        tasks: ['jshint:gruntfile']
+      },
+      lib_test: {
+        files: '<%= jshint.lib_test.src %>',
+        tasks: ['jshint:lib_test', 'nodeunit']
+      }
+    }
+  });
+
+  // These plugins provide necessary tasks.
+  grunt.loadNpmTasks('grunt-contrib-nodeunit');
+  grunt.loadNpmTasks('grunt-contrib-jshint');
+  grunt.loadNpmTasks('grunt-contrib-watch');
+
+  // Default task.
+  grunt.registerTask('default', ['jshint', 'nodeunit']);
+
+};

+ 16 - 14
jsfuck.js

@@ -138,7 +138,7 @@
   }
 
   function replaceMap(){
-    var character = "", value, original;
+    var character = "", value, original, i, key;
 
     function replace(pattern, replacement){
       value = value.replace(
@@ -147,7 +147,7 @@
       );
     }
 
-    for (var i = MIN; i <= MAX; i++){
+    for (i = MIN; i <= MAX; i++){
       character = String.fromCharCode(i);
       value = MAPPING[character];
       original = value;
@@ -175,11 +175,9 @@
 
   function replaceStrings(){
     var regEx = /[^\[\]\(\)\!\+]{1}/g,
-      all, value, missing;
-
+      all, value, missing,
       count = MAX - MIN;
 
-
     function findMissing(){
       var all, value, done = false;
 
@@ -198,24 +196,28 @@
       return done;
     }
 
+    function mappingReplacer(a, b){
+      return b.split("").join("+");
+    }
+
+    function valueReplacer(c) {
+      return missing[c] ? c : MAPPING[c];  
+    }
+
     for (all in MAPPING){
-      MAPPING[all] = MAPPING[all].replace(/\"([^\"]+)\"/gi, function(a, b){
-        return b.split("").join("+");
-      });
+      MAPPING[all] = MAPPING[all].replace(/\"([^\"]+)\"/gi, mappingReplacer);
     }
 
     while (findMissing()){
       for (all in missing){
         value = MAPPING[all];
-        value = value.replace(regEx, function(c){
-          return missing[c] ? c : MAPPING[c];
-        });
+        value = value.replace(regEx, valueReplacer);
 
         MAPPING[all] = value;
         missing[all] = value;
       }
 
-      if (count-- == 0){
+      if (count-- === 0){
         console.error("Could not compile the following chars:", missing);
       }
     }
@@ -255,7 +257,7 @@
     if (wrapWithEval){
       output = "[][" + encode("filter") + "]" +
         "[" + encode("constructor") + "]" +
-        "(" + output + ")()"
+        "(" + output + ")()";
     }
 
     return output;
@@ -269,4 +271,4 @@
   self.JSFuck = {
     encode: encode
   };
-})(typeof(exports) == "undefined" ? window : exports);
+})(typeof(exports) === "undefined" ? window : exports);

+ 94 - 0
output.txt

@@ -0,0 +1,94 @@
+` ` 214
+`!` 13936
+`"` 951
+`#` 13945
+`$` 13950
+`%` 7131
+`&` 13960
+`'` 13965
+`(` 209
+`)` 214
+`*` 13944
+`+` 75
+`,` 666
+`-` 4080
+`.` 3974
+`/` 408
+`0` 8
+`1` 10
+`2` 14
+`3` 19
+`4` 24
+`5` 29
+`6` 34
+`7` 39
+`8` 44
+`9` 49
+`:` 18057
+`;` 16328
+`<` 392
+`=` 947
+`>` 398
+`?` 13947
+`@` 13944
+`A` 1011
+`B` 1012
+`C` 13955
+`D` 13960
+`E` 13965
+`F` 1172
+`G` 13975
+`H` 13980
+`I` 75
+`J` 13954
+`K` 16333
+`L` 14123
+`M` 13961
+`N` 21
+`O` 13952
+`P` 13949
+`Q` 13951
+`R` 13955
+`S` 1014
+`T` 13965
+`U` 13970
+`V` 13975
+`W` 13980
+`X` 13985
+`Y` 13990
+`Z` 13959
+`[` 4010
+`\` 14128
+`]` 13966
+`^` 13969
+`_` 13957
+``` 13954
+`a` 20
+`b` 2399
+`c` 189
+`d` 27
+`e` 30
+`f` 18
+`g` 999
+`h` 2428
+`i` 42
+`j` 2438
+`k` 2403
+`l` 24
+`m` 983
+`n` 23
+`o` 204
+`p` 2427
+`q` 2432
+`r` 21
+`s` 29
+`t` 19
+`u` 21
+`v` 2419
+`w` 2428
+`x` 2438
+`y` 106
+`z` 2458
+`{` 224
+`|` 14138
+`}` 13976

+ 6 - 0
package.json

@@ -24,5 +24,11 @@
   ],
   "bugs": {
     "url": "https://github.com/aemkei/jsfuck/issues"
+  },
+  "devDependencies": {
+    "grunt": "~0.4.1",
+    "grunt-contrib-nodeunit": "~0.2.0",
+    "grunt-contrib-watch": "~0.5.3",
+    "grunt-contrib-jshint": "~0.6.4"
   }
 }

+ 47 - 0
test/jsfuck_test.js

@@ -0,0 +1,47 @@
+/*jshint -W061 */
+'use strict';
+
+var JSFuck = require('../jsfuck.js').JSFuck,
+	test_encode = function (test, value) {
+		var encoded = JSFuck.encode(value),
+			unencoded = eval(encoded);
+
+		test.strictEqual(value, unencoded, 'encoding "' + value + '" failed');
+	};
+var MIN = 32, MAX = 126;
+var fs = require('fs');
+
+exports['integration'] = {
+	'test': function(test) {
+		var file = fs.openSync('output.txt', 'w+');
+
+		for (var i = MIN; i < MAX; i++) {
+			var c = String.fromCharCode(i),
+				encoded = JSFuck.encode(c);
+			fs.writeSync(file, '`' + c + '` ' + encoded.length + '\n');
+		}
+
+		fs.closeSync(file);
+		test.done();
+	}
+};
+
+var test = function (c, test) {
+	test_encode(test, c);
+	test.done();
+};
+
+var createTest = function (input) {
+	exports['encode_tests']['encode "'+input+'"'] = test.bind(undefined, input);
+};
+
+exports['encode_tests'] = {};
+
+createTest('false');
+createTest('falsefalsetrue');
+createTest('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
+createTest('abcdefghijklmnopqrstuvwxyz');
+
+for(var i=MIN; i<MAX ;i++) {
+	createTest(String.fromCharCode(i));
+}