Bläddra i källkod

Add unittest for util

ithewei 4 år sedan
förälder
incheckning
b79bb6f519
8 ändrade filer med 181 tillägg och 25 borttagningar
  1. 3 0
      Makefile
  2. 4 0
      scripts/unittest.sh
  3. 13 0
      unittest/CMakeLists.txt
  4. 118 0
      unittest/base64_test.c
  5. 16 7
      unittest/md5_test.c
  6. 16 7
      unittest/sha1_test.c
  7. 8 9
      util/base64.c
  8. 3 2
      util/base64.h

+ 3 - 0
Makefile

@@ -171,6 +171,9 @@ unittest: prepare
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/hmutex_test       unittest/hmutex_test.c        base/htime.c   -pthread
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/connect_test      unittest/connect_test.c       base/hsocket.c base/htime.c
 	$(CC)  -g -Wall -O0 -std=c99   -I. -Ibase            -o bin/socketpair_test   unittest/socketpair_test.c    base/hsocket.c
+	$(CC)  -g -Wall -O0 -std=c99   -I. -Iutil            -o bin/base64            unittest/base64_test.c        util/base64.c
+	$(CC)  -g -Wall -O0 -std=c99   -I. -Iutil            -o bin/md5               unittest/md5_test.c           util/md5.c
+	$(CC)  -g -Wall -O0 -std=c99   -I. -Iutil            -o bin/sha1              unittest/sha1_test.c          util/sha1.c
 	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/hstring_test      unittest/hstring_test.cpp     cpputil/hstring.cpp
 	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/hpath_test        unittest/hpath_test.cpp       cpputil/hpath.cpp
 	$(CXX) -g -Wall -O0 -std=c++11 -I. -Ibase -Icpputil  -o bin/ls                unittest/listdir_test.cpp     cpputil/hdir.cpp

+ 4 - 0
scripts/unittest.sh

@@ -10,6 +10,10 @@ bin/mkdir_p 123/456
 bin/ls
 bin/rmdir_p 123/456
 
+bin/base64
+bin/md5
+bin/sha1
+
 bin/defer_test
 bin/hstring_test
 bin/hpath_test

+ 13 - 0
unittest/CMakeLists.txt

@@ -28,6 +28,16 @@ target_include_directories(connect_test PRIVATE .. ../base)
 add_executable(socketpair_test socketpair_test.c ../base/hsocket.c)
 target_include_directories(socketpair_test PRIVATE .. ../base)
 
+# ------util------
+add_executable(base64 base64_test.c ../util/base64.c)
+target_include_directories(base64 PRIVATE .. ../util)
+
+add_executable(md5 md5_test.c ../util/md5.c)
+target_include_directories(md5 PRIVATE .. ../util)
+
+add_executable(sha1 sha1_test.c ../util/sha1.c)
+target_include_directories(sha1 PRIVATE .. ../util)
+
 # ------cpputil------
 add_executable(hstring_test hstring_test.cpp ../cpputil/hstring.cpp)
 target_include_directories(hstring_test PRIVATE .. ../base ../cpputil)
@@ -83,6 +93,9 @@ add_custom_target(unittest DEPENDS
     hmutex_test
     connect_test
     socketpair_test
+    base64
+    md5
+    sha1
     hstring_test
     hpath_test
     ls

+ 118 - 0
unittest/base64_test.c

@@ -0,0 +1,118 @@
+/*
+ * @build: gcc -o bin/base64 unittest/base64_test.c util/base64.c -I. -Iutil
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "base64.h"
+
+static void test() {
+    unsigned char in[] = "0123456789";
+    // test encode
+    int encoded_size = BASE64_ENCODE_OUT_SIZE(10);
+    char* encoded = (char*)malloc(encoded_size + 1);
+    encoded_size = hv_base64_encode(in, 10, encoded);
+    encoded[encoded_size] = '\0';
+    assert(strcmp(encoded, "MDEyMzQ1Njc4OQ==") == 0);
+    // test decode
+    int decoded_size = BASE64_DECODE_OUT_SIZE(encoded_size);
+    unsigned char* decoded = (unsigned char*)malloc(decoded_size);
+    decoded_size = hv_base64_decode(encoded, encoded_size, decoded);
+    assert(decoded_size == 10 && memcmp(in, decoded, decoded_size) == 0);
+
+    free(encoded);
+    free(decoded);
+}
+
+int main(int argc, char* argv[]) {
+    test();
+
+    if (argc < 3) {
+        printf("Usage: base64 infile outfile\n");
+        printf("       base64 -d infile outfile\n");
+        return -10;
+    }
+    else if (argc == 3) {
+        // encode file
+        const char* infile = argv[1];
+        const char* outfile = argv[2];
+
+        FILE* infp = fopen(infile, "rb");
+        if (infp == NULL) {
+            printf("Open file '%s' failed!\n", infile);
+            return -20;
+        }
+        fseek(infp, 0, SEEK_END);
+        long filesize = ftell(infp);
+        // printf("filesize=%ld\n", filesize);
+        fseek(infp, 0, SEEK_SET);
+        unsigned char* filebuf = (unsigned char*)malloc(filesize);
+        size_t nread = fread(filebuf, 1, filesize, infp);
+        assert(nread == filesize);
+
+        int encoded_size = BASE64_ENCODE_OUT_SIZE(filesize);
+        char* encoded = (char*)malloc(encoded_size + 1);
+        encoded_size = hv_base64_encode(filebuf, filesize, encoded);
+        encoded[encoded_size] = '\0';
+
+        FILE* outfp = fopen(outfile, "w");
+        if (outfp == NULL) {
+            printf("Save file '%s' failed!\n", infile);
+            return -20;
+        }
+        size_t nwrite = fwrite(encoded, 1, encoded_size, outfp);
+        assert(nwrite == encoded_size);
+
+        free(filebuf);
+        free(encoded);
+        fclose(infp);
+        fclose(outfp);
+    }
+    else if (argc == 4) {
+        const char* flags = argv[1];
+        if (flags[0] == '-' && flags[1] == 'd') {
+            // decode file
+            const char* infile = argv[2];
+            const char* outfile = argv[3];
+            FILE* infp = fopen(infile, "r");
+            if (infp == NULL) {
+                printf("Open file '%s' failed!\n", infile);
+                return -20;
+            }
+            fseek(infp, 0, SEEK_END);
+            long filesize = ftell(infp);
+            // printf("filesize=%ld\n", filesize);
+            fseek(infp, 0, SEEK_SET);
+            char* filebuf = (char*)malloc(filesize);
+            size_t nread = fread(filebuf, 1, filesize, infp);
+            assert(nread == filesize);
+
+            int decoded_size = BASE64_DECODE_OUT_SIZE(filesize);
+            unsigned char* decoded = (unsigned char*)malloc(decoded_size);
+            decoded_size = hv_base64_decode(filebuf, filesize, decoded);
+
+            FILE* outfp = fopen(outfile, "wb");
+            if (outfp == NULL) {
+                printf("Save file '%s' failed!\n", infile);
+                return -20;
+            }
+            size_t nwrite = fwrite(decoded, 1, decoded_size, outfp);
+            assert(nwrite == decoded_size);
+
+            free(filebuf);
+            free(decoded);
+            fclose(infp);
+            fclose(outfp);
+        }
+        else {
+            printf("Unrecognized flags '%s'\n", flags);
+            return -40;
+        }
+    }
+
+    return 0;
+}

+ 16 - 7
unittest/md5_test.c

@@ -10,17 +10,23 @@
 
 #include "md5.h"
 
+static void test() {
+    unsigned char ch = '1';
+    char md5[33] = {0};
+    hv_md5_hex(&ch, 1, md5, sizeof(md5));
+    assert(strcmp(md5, "c4ca4238a0b923820dcc509a6f75849b") == 0);
+}
+
 int main(int argc, char* argv[]) {
+    test();
+
     if (argc < 2) {
         printf("Usage: md5 file\n");
         printf("       md5 -s string\n");
         return -10;
     }
 
-    unsigned char ch = '1';
-    char md5[33];
-    hv_md5_hex(&ch, 1, md5, sizeof(md5));
-    assert(strcmp(md5, "c4ca4238a0b923820dcc509a6f75849b") == 0);
+    char md5[33] = {0};
 
     if (argc == 2) {
         const char* filepath = argv[1];
@@ -33,10 +39,13 @@ int main(int argc, char* argv[]) {
         long filesize = ftell(fp);
         // printf("filesize=%ld\n", filesize);
         fseek(fp, 0, SEEK_SET);
-        unsigned char* buf = (unsigned char*)malloc(filesize);
-        size_t nread = fread(buf, 1, filesize, fp);
+        unsigned char* filebuf = (unsigned char*)malloc(filesize);
+        size_t nread = fread(filebuf, 1, filesize, fp);
         assert(nread == filesize);
-        hv_md5_hex(buf, filesize, md5, sizeof(md5));
+        hv_md5_hex(filebuf, filesize, md5, sizeof(md5));
+
+        free(filebuf);
+        fclose(fp);
     }
     else if (argc == 3) {
         const char* flags = argv[1];

+ 16 - 7
unittest/sha1_test.c

@@ -10,17 +10,23 @@
 
 #include "sha1.h"
 
+static void test() {
+    unsigned char ch = '1';
+    char sha1[41] = {0};
+    hv_sha1_hex(&ch, 1, sha1, sizeof(sha1));
+    assert(strcmp(sha1, "356a192b7913b04c54574d18c28d46e6395428ab") == 0);
+}
+
 int main(int argc, char* argv[]) {
+    test();
+
     if (argc < 2) {
         printf("Usage: sha1 file\n");
         printf("       sha1 -s string\n");
         return -10;
     }
 
-    unsigned char ch = '1';
-    char sha1[41];
-    hv_sha1_hex(&ch, 1, sha1, sizeof(sha1));
-    assert(strcmp(sha1, "356a192b7913b04c54574d18c28d46e6395428ab") == 0);
+    char sha1[41] = {0};
 
     if (argc == 2) {
         const char* filepath = argv[1];
@@ -33,10 +39,13 @@ int main(int argc, char* argv[]) {
         long filesize = ftell(fp);
         // printf("filesize=%ld\n", filesize);
         fseek(fp, 0, SEEK_SET);
-        unsigned char* buf = (unsigned char*)malloc(filesize);
-        size_t nread = fread(buf, 1, filesize, fp);
+        unsigned char* filebuf = (unsigned char*)malloc(filesize);
+        size_t nread = fread(filebuf, 1, filesize, fp);
         assert(nread == filesize);
-        hv_sha1_hex(buf, filesize, sha1, sizeof(sha1));
+        hv_sha1_hex(filebuf, filesize, sha1, sizeof(sha1));
+
+        free(filebuf);
+        fclose(fp);
     }
     else if (argc == 3) {
         const char* flags = argv[1];

+ 8 - 9
util/base64.c

@@ -51,9 +51,9 @@ static const signed char base64de[] = {
 };
 
 int hv_base64_encode(const unsigned char *in, unsigned int inlen, char *out) {
-    unsigned int i, j;
+    unsigned int i = 0, j = 0;
 
-    for (i = j = 0; i < inlen; i++) {
+    for (; i < inlen; i++) {
         int s = i % 3;
 
         switch (s) {
@@ -82,22 +82,22 @@ int hv_base64_encode(const unsigned char *in, unsigned int inlen, char *out) {
         out[j++] = BASE64_PAD;
     }
 
-    return BASE64_OK;
+    return j;
 }
 
 int hv_base64_decode(const char *in, unsigned int inlen, unsigned char *out) {
-    unsigned int i, j;
+    unsigned int i = 0, j = 0;
 
-    for (i = j = 0; i < inlen; i++) {
+    for (; i < inlen; i++) {
         int c;
         int s = i % 4;
 
         if (in[i] == '=')
-            return BASE64_OK;
+            return j;
 
         if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST ||
             (c = base64de[in[i] - BASE64DE_FIRST]) == -1)
-            return BASE64_INVALID;
+            return -1;
 
         switch (s) {
         case 0:
@@ -122,6 +122,5 @@ int hv_base64_decode(const char *in, unsigned int inlen, unsigned char *out) {
         }
     }
 
-    return BASE64_OK;
+    return j;
 }
-

+ 3 - 2
util/base64.h

@@ -3,14 +3,15 @@
 
 #include "hexport.h"
 
-enum {BASE64_OK = 0, BASE64_INVALID};
-
 #define BASE64_ENCODE_OUT_SIZE(s)   (((s) + 2) / 3 * 4)
 #define BASE64_DECODE_OUT_SIZE(s)   (((s)) / 4 * 3)
 
 BEGIN_EXTERN_C
 
+// @return encoded size
 HV_EXPORT int hv_base64_encode(const unsigned char *in, unsigned int inlen, char *out);
+
+// @return decoded size
 HV_EXPORT int hv_base64_decode(const char *in, unsigned int inlen, unsigned char *out);
 
 END_EXTERN_C