ENGINE-488: added functionality to allow deduplication where needed; added deduplication for key counting in reencrypt tests. sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 27 Mar 2019 17:00:40 +0100
branchsync
changeset 340361dbe005c428
parent 3401 9d9142a1490e
child 3404 8b7d693bebfe
ENGINE-488: added functionality to allow deduplication where needed; added deduplication for key counting in reencrypt tests.
src/stringlist.c
src/stringlist.h
test/include/StringlistTests.h
test/src/engine_tests/ReencryptPlusExtraKeysTests.cc
test/src/engine_tests/StringlistTests.cc
     1.1 --- a/src/stringlist.c	Wed Mar 27 10:02:28 2019 +0100
     1.2 +++ b/src/stringlist.c	Wed Mar 27 17:00:40 2019 +0100
     1.3 @@ -229,6 +229,50 @@
     1.4      return stringlist;
     1.5  }
     1.6  
     1.7 +static stringlist_t* stringlist_multi_delete(stringlist_t* stringlist, const char* value) {
     1.8 +    if (stringlist == NULL || !stringlist->value)
     1.9 +        return stringlist;
    1.10 +    
    1.11 +    stringlist_t* list_curr = stringlist;
    1.12 +    stringlist_t* prev_ptr = NULL;
    1.13 +    
    1.14 +    while (list_curr) {
    1.15 +        if (strcmp(list_curr->value, value) == 0) {
    1.16 +            stringlist_t* victim = list_curr;
    1.17 +            if (prev_ptr)
    1.18 +                prev_ptr->next = list_curr->next;    
    1.19 +            else
    1.20 +                stringlist = list_curr->next;
    1.21 +            
    1.22 +            list_curr = list_curr->next;
    1.23 +
    1.24 +            victim->next = NULL;
    1.25 +            
    1.26 +            free_stringlist(victim);
    1.27 +        }
    1.28 +        else {
    1.29 +            prev_ptr = list_curr;
    1.30 +            list_curr = list_curr->next;
    1.31 +        }
    1.32 +    }
    1.33 +    return stringlist;
    1.34 +}
    1.35 +
    1.36 +
    1.37 +
    1.38 +void dedup_stringlist(stringlist_t* stringlist) {
    1.39 +    if (stringlist == NULL || !stringlist->value)
    1.40 +        return;
    1.41 +        
    1.42 +    stringlist_t* list_curr = stringlist;
    1.43 +
    1.44 +    while (list_curr && list_curr->next) {
    1.45 +        stringlist_t* new_next = stringlist_multi_delete(list_curr->next, list_curr->value);
    1.46 +        list_curr->next = new_next;
    1.47 +        list_curr = list_curr->next;
    1.48 +    }    
    1.49 +}
    1.50 +
    1.51  DYNAMIC_API void free_stringlist(stringlist_t *stringlist)
    1.52  {
    1.53      stringlist_t *curr = stringlist;
     2.1 --- a/src/stringlist.h	Wed Mar 27 10:02:28 2019 +0100
     2.2 +++ b/src/stringlist.h	Wed Mar 27 17:00:40 2019 +0100
     2.3 @@ -135,7 +135,8 @@
     2.4  
     2.5  stringlist_t* stringlist_search(stringlist_t* head, const char* value);
     2.6  
     2.7 +void dedup_stringlist(stringlist_t* stringlist);
     2.8 +
     2.9  #ifdef __cplusplus
    2.10  }
    2.11  #endif
    2.12 -
     3.1 --- a/test/include/StringlistTests.h	Wed Mar 27 10:02:28 2019 +0100
     3.2 +++ b/test/include/StringlistTests.h	Wed Mar 27 17:00:40 2019 +0100
     3.3 @@ -14,6 +14,7 @@
     3.4          StringlistTests(string suitename, string test_home_dir);
     3.5      private:
     3.6          void check_stringlists();
     3.7 +        void check_dedup_stringlist();
     3.8  };
     3.9  
    3.10  #endif
     4.1 --- a/test/src/engine_tests/ReencryptPlusExtraKeysTests.cc	Wed Mar 27 10:02:28 2019 +0100
     4.2 +++ b/test/src/engine_tests/ReencryptPlusExtraKeysTests.cc	Wed Mar 27 17:00:40 2019 +0100
     4.3 @@ -173,6 +173,10 @@
     4.4      
     4.5      int i = 0;
     4.6      
     4.7 +    if (keys->next)
     4.8 +    dedup_stringlist(keys->next);
     4.9 +;
    4.10 +    
    4.11      for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
    4.12      {
    4.13          if (i == 0) {
    4.14 @@ -287,6 +291,9 @@
    4.15      
    4.16      i = 0;
    4.17      
    4.18 +    if (keys->next)
    4.19 +        dedup_stringlist(keys->next);
    4.20 +    
    4.21      for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
    4.22      {
    4.23          if (i == 0) {
    4.24 @@ -403,6 +410,10 @@
    4.25      
    4.26      i = 0;
    4.27      
    4.28 +    if (keys->next)
    4.29 +    dedup_stringlist(keys->next);
    4.30 +;
    4.31 +    
    4.32      for (stringlist_t* kl = keys; kl && kl->value; kl = kl->next, i++)
    4.33      {
    4.34          if (i == 0) {
     5.1 --- a/test/src/engine_tests/StringlistTests.cc	Wed Mar 27 10:02:28 2019 +0100
     5.2 +++ b/test/src/engine_tests/StringlistTests.cc	Wed Mar 27 17:00:40 2019 +0100
     5.3 @@ -19,6 +19,8 @@
     5.4      EngineTestSuite::EngineTestSuite(suitename, test_home_dir) {            
     5.5      add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StringlistTests::check_stringlists"),
     5.6                                                                        static_cast<Func>(&StringlistTests::check_stringlists)));
     5.7 +    add_test_to_suite(std::pair<std::string, void (Test::Suite::*)()>(string("StringlistTests::check_dedup_stringlist"),
     5.8 +                                                                      static_cast<Func>(&StringlistTests::check_dedup_stringlist)));
     5.9  }
    5.10  
    5.11  void StringlistTests::check_stringlists() {
    5.12 @@ -119,3 +121,116 @@
    5.13      
    5.14      cout << "done.\n";
    5.15  }
    5.16 +
    5.17 +void StringlistTests::check_dedup_stringlist() {
    5.18 +    const char* str1 = "Your Mama";
    5.19 +    const char* str2 = "And your Papa";
    5.20 +    const char* str3 = "And your little dog too!";
    5.21 +    const char* str4 = "Meh";
    5.22 +    
    5.23 +    stringlist_t* s_list = NULL;
    5.24 +    dedup_stringlist(s_list);
    5.25 +    TEST_ASSERT(s_list == NULL);
    5.26 +    
    5.27 +    s_list = new_stringlist(NULL);
    5.28 +    dedup_stringlist(s_list);    
    5.29 +    TEST_ASSERT(s_list->value == NULL);
    5.30 +    
    5.31 +    stringlist_add(s_list, str1);
    5.32 +    dedup_stringlist(s_list);
    5.33 +    TEST_ASSERT(s_list->value);
    5.34 +    TEST_ASSERT(strcmp(s_list->value, str1) == 0);
    5.35 +    TEST_ASSERT(!s_list->next);
    5.36 +
    5.37 +    // Add same value
    5.38 +    stringlist_add(s_list, str1);
    5.39 +    dedup_stringlist(s_list);
    5.40 +    TEST_ASSERT(s_list->value);
    5.41 +    TEST_ASSERT(strcmp(s_list->value, str1) == 0);
    5.42 +    TEST_ASSERT(!s_list->next);
    5.43 +
    5.44 +    stringlist_add(s_list, str1);
    5.45 +    stringlist_add(s_list, str2);
    5.46 +    dedup_stringlist(s_list);
    5.47 +    TEST_ASSERT(s_list->value);
    5.48 +    TEST_ASSERT(strcmp(s_list->value, str1) == 0);
    5.49 +    TEST_ASSERT(s_list->next);
    5.50 +    TEST_ASSERT(!s_list->next->next);
    5.51 +    TEST_ASSERT(s_list->next->value);
    5.52 +    TEST_ASSERT(strcmp(s_list->next->value, str2) == 0);    
    5.53 +
    5.54 +    free_stringlist(s_list);
    5.55 +    s_list = new_stringlist(str1);
    5.56 +    
    5.57 +    stringlist_add(s_list, str1);
    5.58 +    stringlist_add(s_list, str1);
    5.59 +    stringlist_add(s_list, str1);
    5.60 +    stringlist_add(s_list, str1);
    5.61 +    stringlist_add(s_list, str1);
    5.62 +    stringlist_add(s_list, str1);
    5.63 +    stringlist_add(s_list, str1);
    5.64 +    stringlist_add(s_list, str1);
    5.65 +    stringlist_add(s_list, str1);
    5.66 +    stringlist_add(s_list, str1);
    5.67 +    stringlist_add(s_list, str1);
    5.68 +    stringlist_add(s_list, str1);
    5.69 +    stringlist_add(s_list, str1);
    5.70 +    stringlist_add(s_list, str1);
    5.71 +    stringlist_add(s_list, str1);
    5.72 +    dedup_stringlist(s_list);
    5.73 +    TEST_ASSERT(s_list->value);
    5.74 +    TEST_ASSERT(strcmp(s_list->value, str1) == 0);
    5.75 +    TEST_ASSERT(!s_list->next);
    5.76 +
    5.77 +    free_stringlist(s_list);
    5.78 +    s_list = new_stringlist(str1);
    5.79 +
    5.80 +    stringlist_add(s_list, str1);
    5.81 +    stringlist_add(s_list, str1);
    5.82 +    stringlist_add(s_list, str1);
    5.83 +    stringlist_add(s_list, str1);
    5.84 +    stringlist_add(s_list, str1);
    5.85 +    stringlist_add(s_list, str1);
    5.86 +    stringlist_add(s_list, str1);
    5.87 +    stringlist_add(s_list, str1);
    5.88 +    stringlist_add(s_list, str1);
    5.89 +    stringlist_add(s_list, str1);
    5.90 +    stringlist_add(s_list, str1);
    5.91 +    stringlist_add(s_list, str1);
    5.92 +    stringlist_add(s_list, str1);
    5.93 +    stringlist_add(s_list, str2);
    5.94 +    stringlist_add(s_list, str1);
    5.95 +    dedup_stringlist(s_list);
    5.96 +    TEST_ASSERT(s_list->value);
    5.97 +    TEST_ASSERT(strcmp(s_list->value, str1) == 0);
    5.98 +    TEST_ASSERT(s_list->next);
    5.99 +    TEST_ASSERT(!s_list->next->next);
   5.100 +    TEST_ASSERT(s_list->next->value);
   5.101 +    TEST_ASSERT(strcmp(s_list->next->value, str2) == 0);    
   5.102 +
   5.103 +    free_stringlist(s_list);
   5.104 +    s_list = new_stringlist(str3);
   5.105 +
   5.106 +    stringlist_add(s_list, str2);
   5.107 +    stringlist_add(s_list, str3);
   5.108 +    stringlist_add(s_list, str1);
   5.109 +    stringlist_add(s_list, str3);
   5.110 +    stringlist_add(s_list, str2);
   5.111 +    stringlist_add(s_list, str1);
   5.112 +    stringlist_add(s_list, str4);
   5.113 +    stringlist_add(s_list, str3);
   5.114 +
   5.115 +    dedup_stringlist(s_list);
   5.116 +    TEST_ASSERT(s_list->next);
   5.117 +    TEST_ASSERT(s_list->next->next);
   5.118 +    TEST_ASSERT(s_list->next->next->next);
   5.119 +    TEST_ASSERT(!s_list->next->next->next->next);
   5.120 +    TEST_ASSERT(s_list->value);
   5.121 +    TEST_ASSERT(strcmp(s_list->value, str3) == 0);    
   5.122 +    TEST_ASSERT(s_list->next->value);
   5.123 +    TEST_ASSERT(strcmp(s_list->next->value, str2) == 0);    
   5.124 +    TEST_ASSERT(s_list->next->next->value);
   5.125 +    TEST_ASSERT(strcmp(s_list->next->next->value, str1) == 0);    
   5.126 +    TEST_ASSERT(s_list->next->next->next->value);
   5.127 +    TEST_ASSERT(strcmp(s_list->next->next->next->value, str4) == 0);    
   5.128 +}