From 964038edbae2c67e570ad7e39b3849897f20ace9 Mon Sep 17 00:00:00 2001
From: Fabian Mauchle <fabian.mauchle@switch.ch>
Date: Fri, 17 May 2019 17:10:24 +0200
Subject: [PATCH] fix minor valgrind reports in unit tests

---
 tests/t_resizeattr.c     |  17 +++---
 tests/t_rewrite.c        | 124 +++++++++++++++++++++++----------------
 tests/t_rewrite_config.c |   1 +
 3 files changed, 81 insertions(+), 61 deletions(-)

diff --git a/tests/t_resizeattr.c b/tests/t_resizeattr.c
index 2b5214c..db20d7c 100644
--- a/tests/t_resizeattr.c
+++ b/tests/t_resizeattr.c
@@ -10,24 +10,23 @@ int test_resize(int start_size, int target_size, uint8_t shouldfail) {
 
     uint8_t *value = malloc(start_size);
     struct tlv *attr;
+    int result = 1;
 
     memset(value, 42, start_size);
     attr = maketlv(1,start_size,value);
 
     if (!resizeattr(attr, target_size))
-        return shouldfail;
+        result = shouldfail;
     else if (shouldfail)
-        return 0;
-
-    if (attr->l != target_size)
-        return 0;
-
-    if (memcmp(attr->v, value, target_size <= start_size ? target_size : start_size))
-        return 0;
+        result = 0;
+    else if (attr->l != target_size)
+        result = 0;
+    else if (memcmp(attr->v, value, target_size <= start_size ? target_size : start_size))
+        result = 0;
 
     freetlv(attr);
     free(value);
-    return 1;
+    return result;
 }
 
 int main (int argc, char *argv[])
diff --git a/tests/t_rewrite.c b/tests/t_rewrite.c
index fe67fd5..6793be8 100644
--- a/tests/t_rewrite.c
+++ b/tests/t_rewrite.c
@@ -46,14 +46,20 @@ void _list_clear(struct list *list) {
         free(data);
 }
 
+void _tlv_list_clear(struct list *list) {
+    struct tlv *tlv;
+    while ( (tlv = (struct tlv *)list_shift(list)) )
+        freetlv(tlv);
+}
+
 void _reset_rewrite(struct rewrite *rewrite) {
     rewrite->whitelist_mode = 0;
     rewrite->removeattrs = NULL;
     rewrite->removevendorattrs = NULL;
-    _list_clear(rewrite->addattrs);
+    _tlv_list_clear(rewrite->addattrs);
     _list_clear(rewrite->modattrs);
     _list_clear(rewrite->modvattrs);
-    _list_clear(rewrite->supattrs);
+    _tlv_list_clear(rewrite->supattrs);
 }
 
 int
@@ -87,8 +93,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - empty rewrite\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -105,8 +111,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - removeattrs\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -126,8 +132,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - removevendorattrs full\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -147,8 +153,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - removevendorattrs last element\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -165,8 +171,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - removevendorattrs non-rfc\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -192,8 +198,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - removevendorattrs sub-attribute\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -208,8 +214,8 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - addattribute simple\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -230,8 +236,8 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - addattribute with existing attributes\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -244,8 +250,8 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - addattribute null\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -261,8 +267,8 @@ main (int argc, char *argv[])
         printf("ok %d - addattribute too big\n", testcount++);
 
         free(value);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -277,8 +283,8 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - suppattrs non existing\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -295,8 +301,8 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - suppattrs existing\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -315,8 +321,8 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - suppattrs vendor\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -340,8 +346,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify attribute no match\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -365,8 +372,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify attribute match full replace\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -390,8 +398,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify attribute match full replace\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -421,8 +430,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify attribute max length\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -444,8 +454,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify attribute too long\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -468,8 +479,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify attribute regex replace\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -494,8 +506,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify vendor\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -526,8 +539,9 @@ main (int argc, char *argv[])
             printf("not ");
         printf("ok %d - modify vendor too long\n", testcount++);
 
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        regfree(&regex);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -546,8 +560,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - whitelistattrs\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
 
     }
@@ -571,8 +585,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - whitelistvendorattrs\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -598,8 +612,8 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - whitelistvendorattrs\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
@@ -624,11 +638,17 @@ main (int argc, char *argv[])
         if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
             printf("not ");
         printf("ok %d - whitelistvendorattrs\n", testcount++);
-        _list_clear(origattrs);
-        _list_clear(expectedattrs);
+        _tlv_list_clear(origattrs);
+        _tlv_list_clear(expectedattrs);
         _reset_rewrite(&rewrite);
     }
 
+    list_destroy(origattrs);
+    list_destroy(expectedattrs);
+    list_destroy(rewrite.addattrs);
+    list_destroy(rewrite.modattrs);
+    list_destroy(rewrite.modvattrs);
+    list_destroy(rewrite.supattrs);
 
     return 0;
 }
diff --git a/tests/t_rewrite_config.c b/tests/t_rewrite_config.c
index 1717b08..97ae7fc 100644
--- a/tests/t_rewrite_config.c
+++ b/tests/t_rewrite_config.c
@@ -48,6 +48,7 @@ main (int argc, char *argv[])
         printf("not ok %d - rewrite ocnfig\n", numtests++);
     }
 
+    freetlv(expected);
 
     return 0;
 }