fix compilation warnings (have missed the -Wall -Werror on my tests)
improve test output messages (eg: PASS: <test>, FAIL: <test>, SUMMARY: X tests 
/ Y failures)

Signed-off-by: geraldo netto <geraldone...@gmail.com>
---
 tests/tst-crypt.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/tests/tst-crypt.c b/tests/tst-crypt.c
index ffc5293..7759891 100644
--- a/tests/tst-crypt.c
+++ b/tests/tst-crypt.c
@@ -2,12 +2,30 @@
 #include <stdio.h>
 #include <string.h>
 #include <crypt.h>
+#include <stdbool.h>
+
+// compile:
+// gcc tst-crypt.c -lcrypt -o crypt -Wall -Werror
+
+
+int tests = 0, fails = 0;
+
+void report(const char* name, bool passed) {
+       static const char* status[] = {"FAIL", "PASS"};
+       printf("%s: %s\n", status[passed], name);
+       tests += 1;
+       fails += !passed;
+}
 
 char* concat_str(char* first_block, char* second_block) {
        static char tmp[10];
        memset(tmp, 0, sizeof(tmp));
-       strncat(tmp, first_block, sizeof(first_block));
-       strncat(tmp, second_block, sizeof(second_block));
+
+       size_t tmp_len = strlen(first_block);
+       strncat(tmp, first_block, tmp_len);
+
+       tmp_len = strlen(second_block);
+       strncat(tmp, second_block, tmp_len);
        return tmp;
 }
 
@@ -21,16 +39,16 @@ int main(void) {
        char result4[] = 
"$6$Ab(d3$TB6UIPV7sprvpcQh2esPr2/ye4FTp9lLft8yAj.2x/HcTXPwzGDxdK/tIF10DdVdV9Z2hhc3MaosUBS3fdueZ1";
 
        // 20338EPKt5xtY
-       if (strcmp(result1, crypt(password, salt)) != 0) return -1;
+       report("crypt (des)", strcmp(result1, crypt(password, salt)) == 0);
 
        // $1$Ab(d3$.SgUCw1AqIVAiXBaS6kzU.
-       if (strcmp(result2, crypt(password, concat_str((char*) "$1$", 
password))) != 0) return -2;
+       report("crypt (md5)", strcmp(result2, crypt(password, 
concat_str((char*) "$1$", password))) == 0);
 
        // $5$Ab(d3$00nkgDrj2dM68IbqFTZPJ.a3mgai49mY.Ezq5ey0xY0
-       if (strcmp(result3, crypt(password, concat_str((char*) "$5$", 
password))) != 0) return -3;
+       report("crypt (sha-256)", strcmp(result3, crypt(password, 
concat_str((char*) "$5$", password))) == 0);
 
        // 
$6$Ab(d3$TB6UIPV7sprvpcQh2esPr2/ye4FTp9lLft8yAj.2x/HcTXPwzGDxdK/tIF10DdVdV9Z2hhc3MaosUBS3fdueZ1
-       if (strcmp(result4, crypt(password, concat_str((char*) "$6$", 
password))) !=0) return -4;
+       report("crypt (sha-512)", strcmp(result4, crypt(password, 
concat_str((char*) "$6$", password))) == 0);
 
        // encrypt
        // http://man7.org/linux/man-pages/man3/encrypt.3.html
@@ -56,7 +74,7 @@ int main(void) {
                txt[8] = '\0';
        }
 
-       if (strcmp(password, txt) == 0) return -5;
+       report("crypt (encrypt)", strcmp(password, txt) != 0);
 
        encrypt(buf, 1);
        for (i = 0; i < 8; i++) {
@@ -67,5 +85,7 @@ int main(void) {
                txt[8] = '\0';
        }
 
-       return (strcmp(password, txt) == 0) ? 0 : -6;
+       report("crypt (decrypt)", strcmp(password, txt) == 0);
+       printf("SUMMARY: %u tests / %u failures\n", tests, fails);
+       return fails;
 }
\ No newline at end of file
-- 
2.7.4

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to