Rewrite lhash_test in C++.

Use a std::map as the dummy lhash and use unique_ptr. This also improves
the test to check on pointer equality; we wish to ensure the lhash
stores the particular pointer value we asked for.

dummy_lh now also owns the pointers. It makes things simpler and since
LHASH doesn't free things, we weren't getting anything out of testing
that.

Change-Id: I97159175ca79a5874586650f272a7846100395e1
Reviewed-on: https://boringssl-review.googlesource.com/12976
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/lhash/CMakeLists.txt b/crypto/lhash/CMakeLists.txt
index 4f4dea7..bd6005b 100644
--- a/crypto/lhash/CMakeLists.txt
+++ b/crypto/lhash/CMakeLists.txt
@@ -11,7 +11,7 @@
 add_executable(
   lhash_test
 
-  lhash_test.c
+  lhash_test.cc
 
   $<TARGET_OBJECTS:test_support>
 )
diff --git a/crypto/lhash/lhash_test.c b/crypto/lhash/lhash_test.c
deleted file mode 100644
index 309b765..0000000
--- a/crypto/lhash/lhash_test.c
+++ /dev/null
@@ -1,203 +0,0 @@
-/* Copyright (c) 2014, Google Inc.
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
- * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
- * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
-
-#if !defined(_POSIX_C_SOURCE)
-#define _POSIX_C_SOURCE 201410L
-#endif
-
-#include <openssl/crypto.h>
-#include <openssl/lhash.h>
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-struct dummy_lhash_node {
-  char *s;
-  struct dummy_lhash_node *next;
-};
-
-struct dummy_lhash {
-  struct dummy_lhash_node *head;
-};
-
-static void dummy_lh_free(struct dummy_lhash *lh) {
-  struct dummy_lhash_node *cur, *next;
-
-  for (cur = lh->head; cur != NULL; cur = next) {
-    next = cur->next;
-    free(cur->s);
-    free(cur);
-  }
-}
-
-static size_t dummy_lh_num_items(const struct dummy_lhash *lh) {
-  size_t count = 0;
-  struct dummy_lhash_node *cur;
-
-  for (cur = lh->head; cur != NULL; cur = cur->next) {
-    count++;
-  }
-
-  return count;
-}
-
-static char *dummy_lh_retrieve(struct dummy_lhash *lh, const char *s) {
-  struct dummy_lhash_node *cur;
-
-  for (cur = lh->head; cur != NULL; cur = cur->next) {
-    if (strcmp(cur->s, s) == 0) {
-      return cur->s;
-    }
-  }
-
-  return NULL;
-}
-
-static int dummy_lh_insert(struct dummy_lhash *lh, char **old_data, char *s) {
-  struct dummy_lhash_node *node, *cur;
-
-  for (cur = lh->head; cur != NULL; cur = cur->next) {
-    if (strcmp(cur->s, s) == 0) {
-      *old_data = cur->s;
-      cur->s = s;
-      return 1;
-    }
-  }
-
-  node = malloc(sizeof(struct dummy_lhash_node));
-  *old_data = NULL;
-  node->s = s;
-  node->next = lh->head;
-  lh->head = node;
-  return 1;
-}
-
-static char *dummy_lh_delete(struct dummy_lhash *lh, const void *s) {
-  struct dummy_lhash_node *cur, **next_ptr;
-  char *ret;
-
-  next_ptr = &lh->head;
-  for (cur = lh->head; cur != NULL; cur = cur->next) {
-    if (strcmp(cur->s, s) == 0) {
-      ret = cur->s;
-      *next_ptr = cur->next;
-      free(cur);
-      return ret;
-    }
-    next_ptr = &cur->next;
-  }
-
-  return NULL;
-}
-
-static char *rand_string(void) {
-  unsigned len = 1 + (rand() % 3);
-  char *ret = malloc(len + 1);
-  unsigned i;
-
-  for (i = 0; i < len; i++) {
-    ret[i] = '0' + (rand() & 7);
-  }
-  ret[i] = 0;
-
-  return ret;
-}
-
-int main(int argc, char **argv) {
-  _LHASH *lh;
-  struct dummy_lhash dummy_lh = {NULL};
-  unsigned i;
-
-  CRYPTO_library_init();
-
-  lh = lh_new(NULL, NULL);
-  if (lh == NULL) {
-    return 1;
-  }
-
-  for (i = 0; i < 100000; i++) {
-    unsigned action;
-    char *s, *s1, *s2;
-
-    if (dummy_lh_num_items(&dummy_lh) != lh_num_items(lh)) {
-      fprintf(stderr, "Length mismatch\n");
-      return 1;
-    }
-
-    action = rand() % 3;
-    switch (action) {
-      case 0:
-        s = rand_string();
-        s1 = (char *)lh_retrieve(lh, s);
-        s2 = dummy_lh_retrieve(&dummy_lh, s);
-        if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
-          fprintf(stderr, "lh_retrieve failure\n");
-          abort();
-        }
-        free(s);
-        break;
-
-      case 1:
-        s = rand_string();
-        lh_insert(lh, (void **)&s1, s);
-#if defined(OPENSSL_WINDOWS)
-        dummy_lh_insert(&dummy_lh, &s2, _strdup(s));
-#else
-        dummy_lh_insert(&dummy_lh, &s2, strdup(s));
-#endif
-
-        if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
-          fprintf(stderr, "lh_insert failure\n");
-          abort();
-        }
-
-        if (s1) {
-          free(s1);
-        }
-        if (s2) {
-          free(s2);
-        }
-        break;
-
-      case 2:
-        s = rand_string();
-        s1 = lh_delete(lh, s);
-        s2 = dummy_lh_delete(&dummy_lh, s);
-
-        if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
-          fprintf(stderr, "lh_insert failure\n");
-          abort();
-        }
-
-        if (s1) {
-          free(s1);
-        }
-        if (s2) {
-          free(s2);
-        }
-        free(s);
-        break;
-
-      default:
-        abort();
-    }
-  }
-
-  lh_doall(lh, free);
-  lh_free(lh);
-  dummy_lh_free(&dummy_lh);
-  printf("PASS\n");
-  return 0;
-}
diff --git a/crypto/lhash/lhash_test.cc b/crypto/lhash/lhash_test.cc
new file mode 100644
index 0000000..e65c0b4
--- /dev/null
+++ b/crypto/lhash/lhash_test.cc
@@ -0,0 +1,131 @@
+/* Copyright (c) 2014, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#if !defined(_POSIX_C_SOURCE)
+#define _POSIX_C_SOURCE 201410L
+#endif
+
+#include <openssl/crypto.h>
+#include <openssl/lhash.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <memory>
+#include <map>
+#include <string>
+#include <utility>
+
+
+static std::unique_ptr<char[]> RandString(void) {
+  unsigned len = 1 + (rand() % 3);
+  std::unique_ptr<char[]> ret(new char[len + 1]);
+
+  for (unsigned i = 0; i < len; i++) {
+    ret[i] = '0' + (rand() & 7);
+  }
+  ret[len] = 0;
+
+  return ret;
+}
+
+struct FreeLHASH {
+  void operator()(_LHASH *lh) { lh_free(lh); }
+};
+
+static const char *Lookup(
+    std::map<std::string, std::unique_ptr<char[]>> *dummy_lh, const char *key) {
+  // Using operator[] implicitly inserts into the map.
+  auto iter = dummy_lh->find(key);
+  if (iter == dummy_lh->end()) {
+    return nullptr;
+  }
+  return iter->second.get();
+}
+
+int main(int argc, char **argv) {
+  CRYPTO_library_init();
+
+  std::unique_ptr<_LHASH, FreeLHASH> lh(lh_new(NULL, NULL));
+  if (!lh) {
+    return 1;
+  }
+
+  // lh is expected to store a canonical instance of each string. dummy_lh
+  // mirrors what it stores for comparison. It also manages ownership of the
+  // pointers.
+  std::map<std::string, std::unique_ptr<char[]>> dummy_lh;
+
+  for (unsigned i = 0; i < 100000; i++) {
+    if (dummy_lh.size() != lh_num_items(lh.get())) {
+      fprintf(stderr, "Length mismatch\n");
+      return 1;
+    }
+
+    enum Action {
+      kRetrieve = 0,
+      kInsert,
+      kDelete,
+    };
+
+    Action action = static_cast<Action>(rand() % 3);
+    switch (action) {
+      case kRetrieve: {
+        std::unique_ptr<char[]> key = RandString();
+        void *value = lh_retrieve(lh.get(), key.get());
+        if (value != Lookup(&dummy_lh, key.get())) {
+          fprintf(stderr, "lh_retrieve failure\n");
+          return 1;
+        }
+        break;
+      }
+
+      case kInsert: {
+        std::unique_ptr<char[]> key = RandString();
+        void *previous;
+        if (!lh_insert(lh.get(), &previous, key.get())) {
+          return 1;
+        }
+
+        if (previous != Lookup(&dummy_lh, key.get())) {
+          fprintf(stderr, "lh_insert failure\n");
+          return 1;
+        }
+
+        dummy_lh[key.get()] = std::move(key);
+        break;
+      }
+
+      case kDelete: {
+        std::unique_ptr<char[]> key = RandString();
+        void *value = lh_delete(lh.get(), key.get());
+
+        if (value != Lookup(&dummy_lh, key.get())) {
+          fprintf(stderr, "lh_delete failure\n");
+          return 1;
+        }
+
+        dummy_lh.erase(key.get());
+        break;
+      }
+
+      default:
+        abort();
+    }
+  }
+
+  printf("PASS\n");
+  return 0;
+}