_binary From 1600ebd6c6b14e9e67fefd5b80aa2045792e8f7d Mon Sep 17 00:00:00 2001
From: Akash Patel <akash3664@yahoo.com>
Date: Sat, 5 Apr 2014 03:07:33 -0700
Subject: [PATCH] sha1 test case

---
 tests/index.php            |   7 ++-
 tests/java_script_test.php |  53 +++++++++++++++++++++++
 tests/sha1_test.php        | 104 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 162 insertions(+), 2 deletions(-)
 create mode 100644 tests/java_script_test.php
 create mode 100644 tests/sha1_test.php

diff --git a/tests/index.php b/tests/index.php
index 015c18d..e02cca2 100644
--- a/tests/index.php
+++ b/tests/index.php
@@ -173,9 +173,11 @@ function runTest($name)
     $class_name = getClassNameFromFileName($name);
     echo "<h2>$class_name</h2>";
     require_once $name;
-
     $test = new $class_name();
-    $results = $test->run();
+    if($test instanceof JavaScriptTest) {
+        $test->sha1TestCase();
+    } else {
+        $results = $test->run();
     ?>
     <table border="1" summary="Displays info about this test case">
     <?php
@@ -208,6 +210,7 @@ function runTest($name)
     ?>
     </table>
     <?php
+    }
 }
 
 /**
diff --git a/tests/java_script_test.php b/tests/java_script_test.php
new file mode 100644
index 0000000..77c4a40
--- /dev/null
+++ b/tests/java_script_test.php
@@ -0,0 +1,53 @@
+<?php
+/**
+ *  SeekQuarry/Yioop --
+ *  Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ *  Copyright (C) 2009 - 2014  Chris Pollett chris@pollett.org
+ *
+ *  LICENSE:
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  END LICENSE
+ *
+ * @author Chris Pollett chris@pollett.org
+ * @package seek_quarry
+ * @subpackage test
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2009 - 2014
+ * @filesource
+ */
+ 
+ /** Super class of all the test classes testing javascript functions.
+ *  @author Chris Pollett
+ *  @package seek_quarry
+ *  @subpackage test
+ */
+ class JavaScriptTest extends UnitTest
+ {
+    function setUp()
+    {
+    
+    }
+    
+    function tearDown()
+    {
+    
+    }
+ 
+ }
+ 
+ ?>
\ No newline at end of file
diff --git a/tests/sha1_test.php b/tests/sha1_test.php
new file mode 100644
index 0000000..2997ac4
--- /dev/null
+++ b/tests/sha1_test.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ *  SeekQuarry/Yioop --
+ *  Open Source Pure PHP Search Engine, Crawler, and Indexer
+ *
+ *  Copyright (C) 2009 - 2014  Chris Pollett chris@pollett.org
+ *
+ *  LICENSE:
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ *  END LICENSE
+ *
+ * @author Chris Pollett chris@pollett.org
+ * @package seek_quarry
+ * @subpackage test
+ * @license http://www.gnu.org/licenses/ GPL3
+ * @link http://www.seekquarry.com/
+ * @copyright 2009 - 2014
+ * @filesource
+ */
+require_once ('java_script_test.php');
+
+/**
+ *  Used to test the implementation of Sha1 function of javascript.
+ *  @author Chris Pollett
+ *  @package seek_quarry
+ *  @subpackage test
+ */
+class Sha1Test extends JavaScriptTest
+{
+     /**
+     * Define numner of test cases
+     * @var int
+     */
+    const NUM_TEST_CASES = 5;
+    
+    function sha1TestCase()
+    {
+        $time = time();
+        $input_value = array();
+        $k=0;
+        for($i=0;$i<self::NUM_TEST_CASES;$i++)
+        {
+            $random_string = md5($time.rand(1,1000));
+            $sha1 = sha1($random_string);
+            $input_value[$k++] = $sha1;
+            $input_value[$k++] = $random_string;
+        }   
+        $js_array = json_encode($input_value);
+        ?>
+        <html>
+        <body>
+        <table>
+        <div id="sha1Test">
+        </div>
+        <head>
+        <script type="text/javascript" src="../scripts/hashcash.js" ></script>
+        <script type="text/javascript">
+        var input_array = <?php echo $js_array; ?>;
+        var total_test_cases = <?php echo self::NUM_TEST_CASES?>;
+        var cell, row, table;
+        var result,color;
+        var i = 0,counter=0;
+        while(i<input_array.length) {
+            if(input_array[i++] == generateSha1(input_array[i++])) {
+                counter++;
+            }
+        }
+        result = counter+"/"+total_test_cases+" Test Passed";
+        if(total_test_cases == counter){
+            color='lightgreen';
+        } else {
+            color='red';
+        }
+        table = document.createElement('table');
+        table.setAttribute('border','1');
+        row = table.insertRow(0);
+        cell = row.insertCell(0);
+        cell.style.fontWeight = 'bold';
+        cell.innerHTML = "generateSha1TestCase";
+        cell = row.insertCell(1);
+        cell.setAttribute("style","background-color: "+color+";");
+        cell.innerHTML = result;
+        document.getElementById("sha1Test").appendChild(table);
+        </script>
+        </head>
+        </body>
+        </html>
+<?php
+        }
+    }
+?>
\ No newline at end of file
-- 
1.8.0.msysgit.0

