113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci#include "curlcheck.h" 2513498266Sopenharmony_ci 2613498266Sopenharmony_ci#define ENABLE_CURLX_PRINTF 2713498266Sopenharmony_ci#include "curlx.h" 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include "hash.h" 3013498266Sopenharmony_ci 3113498266Sopenharmony_ci#include "memdebug.h" /* LAST include file */ 3213498266Sopenharmony_ci 3313498266Sopenharmony_cistatic struct Curl_hash hash_static; 3413498266Sopenharmony_cistatic const int slots = 3; 3513498266Sopenharmony_ci 3613498266Sopenharmony_cistatic void mydtor(void *p) 3713498266Sopenharmony_ci{ 3813498266Sopenharmony_ci /* Data are statically allocated */ 3913498266Sopenharmony_ci (void)p; /* unused */ 4013498266Sopenharmony_ci} 4113498266Sopenharmony_ci 4213498266Sopenharmony_cistatic CURLcode unit_setup(void) 4313498266Sopenharmony_ci{ 4413498266Sopenharmony_ci Curl_hash_init(&hash_static, slots, Curl_hash_str, 4513498266Sopenharmony_ci Curl_str_key_compare, mydtor); 4613498266Sopenharmony_ci return CURLE_OK; 4713498266Sopenharmony_ci} 4813498266Sopenharmony_ci 4913498266Sopenharmony_cistatic void unit_stop(void) 5013498266Sopenharmony_ci{ 5113498266Sopenharmony_ci Curl_hash_destroy(&hash_static); 5213498266Sopenharmony_ci} 5313498266Sopenharmony_ci 5413498266Sopenharmony_ciUNITTEST_START 5513498266Sopenharmony_ci char key1[] = "key1"; 5613498266Sopenharmony_ci char key2[] = "key2b"; 5713498266Sopenharmony_ci char key3[] = "key3"; 5813498266Sopenharmony_ci char key4[] = "key4"; 5913498266Sopenharmony_ci char notakey[] = "notakey"; 6013498266Sopenharmony_ci char *nodep; 6113498266Sopenharmony_ci int rc; 6213498266Sopenharmony_ci 6313498266Sopenharmony_ci /* Ensure the key hashes are as expected in order to test both hash 6413498266Sopenharmony_ci collisions and a full table. Unfortunately, the hashes can vary 6513498266Sopenharmony_ci between architectures. */ 6613498266Sopenharmony_ci if(Curl_hash_str(key1, strlen(key1), slots) != 1 || 6713498266Sopenharmony_ci Curl_hash_str(key2, strlen(key2), slots) != 0 || 6813498266Sopenharmony_ci Curl_hash_str(key3, strlen(key3), slots) != 2 || 6913498266Sopenharmony_ci Curl_hash_str(key4, strlen(key4), slots) != 1) 7013498266Sopenharmony_ci fprintf(stderr, "Warning: hashes are not computed as expected on this " 7113498266Sopenharmony_ci "architecture; test coverage will be less comprehensive\n"); 7213498266Sopenharmony_ci 7313498266Sopenharmony_ci nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), &key1); 7413498266Sopenharmony_ci fail_unless(nodep, "insertion into hash failed"); 7513498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1)); 7613498266Sopenharmony_ci fail_unless(nodep == key1, "hash retrieval failed"); 7713498266Sopenharmony_ci 7813498266Sopenharmony_ci nodep = Curl_hash_add(&hash_static, &key2, strlen(key2), &key2); 7913498266Sopenharmony_ci fail_unless(nodep, "insertion into hash failed"); 8013498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2)); 8113498266Sopenharmony_ci fail_unless(nodep == key2, "hash retrieval failed"); 8213498266Sopenharmony_ci 8313498266Sopenharmony_ci nodep = Curl_hash_add(&hash_static, &key3, strlen(key3), &key3); 8413498266Sopenharmony_ci fail_unless(nodep, "insertion into hash failed"); 8513498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3)); 8613498266Sopenharmony_ci fail_unless(nodep == key3, "hash retrieval failed"); 8713498266Sopenharmony_ci 8813498266Sopenharmony_ci /* The fourth element exceeds the number of slots & collides */ 8913498266Sopenharmony_ci nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4); 9013498266Sopenharmony_ci fail_unless(nodep, "insertion into hash failed"); 9113498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4)); 9213498266Sopenharmony_ci fail_unless(nodep == key4, "hash retrieval failed"); 9313498266Sopenharmony_ci 9413498266Sopenharmony_ci /* Make sure all elements are still accessible */ 9513498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1)); 9613498266Sopenharmony_ci fail_unless(nodep == key1, "hash retrieval failed"); 9713498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2)); 9813498266Sopenharmony_ci fail_unless(nodep == key2, "hash retrieval failed"); 9913498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3)); 10013498266Sopenharmony_ci fail_unless(nodep == key3, "hash retrieval failed"); 10113498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4)); 10213498266Sopenharmony_ci fail_unless(nodep == key4, "hash retrieval failed"); 10313498266Sopenharmony_ci 10413498266Sopenharmony_ci /* Delete the second of two entries in a bucket */ 10513498266Sopenharmony_ci rc = Curl_hash_delete(&hash_static, &key4, strlen(key4)); 10613498266Sopenharmony_ci fail_unless(rc == 0, "hash delete failed"); 10713498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1)); 10813498266Sopenharmony_ci fail_unless(nodep == key1, "hash retrieval failed"); 10913498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4)); 11013498266Sopenharmony_ci fail_unless(!nodep, "hash retrieval should have failed"); 11113498266Sopenharmony_ci 11213498266Sopenharmony_ci /* Insert that deleted node again */ 11313498266Sopenharmony_ci nodep = Curl_hash_add(&hash_static, &key4, strlen(key4), &key4); 11413498266Sopenharmony_ci fail_unless(nodep, "insertion into hash failed"); 11513498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4)); 11613498266Sopenharmony_ci fail_unless(nodep == key4, "hash retrieval failed"); 11713498266Sopenharmony_ci 11813498266Sopenharmony_ci /* Delete the first of two entries in a bucket */ 11913498266Sopenharmony_ci rc = Curl_hash_delete(&hash_static, &key1, strlen(key1)); 12013498266Sopenharmony_ci fail_unless(rc == 0, "hash delete failed"); 12113498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1)); 12213498266Sopenharmony_ci fail_unless(!nodep, "hash retrieval should have failed"); 12313498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4)); 12413498266Sopenharmony_ci fail_unless(nodep == key4, "hash retrieval failed"); 12513498266Sopenharmony_ci 12613498266Sopenharmony_ci /* Delete the remaining one of two entries in a bucket */ 12713498266Sopenharmony_ci rc = Curl_hash_delete(&hash_static, &key4, strlen(key4)); 12813498266Sopenharmony_ci fail_unless(rc == 0, "hash delete failed"); 12913498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1)); 13013498266Sopenharmony_ci fail_unless(!nodep, "hash retrieval should have failed"); 13113498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key4, strlen(key4)); 13213498266Sopenharmony_ci fail_unless(!nodep, "hash retrieval should have failed"); 13313498266Sopenharmony_ci 13413498266Sopenharmony_ci /* Delete an already deleted node */ 13513498266Sopenharmony_ci rc = Curl_hash_delete(&hash_static, &key4, strlen(key4)); 13613498266Sopenharmony_ci fail_unless(rc, "hash delete should have failed"); 13713498266Sopenharmony_ci 13813498266Sopenharmony_ci /* Replace an existing node */ 13913498266Sopenharmony_ci nodep = Curl_hash_add(&hash_static, &key1, strlen(key1), ¬akey); 14013498266Sopenharmony_ci fail_unless(nodep, "insertion into hash failed"); 14113498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key1, strlen(key1)); 14213498266Sopenharmony_ci fail_unless(nodep == notakey, "hash retrieval failed"); 14313498266Sopenharmony_ci 14413498266Sopenharmony_ci /* Make sure all remaining elements are still accessible */ 14513498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key2, strlen(key2)); 14613498266Sopenharmony_ci fail_unless(nodep == key2, "hash retrieval failed"); 14713498266Sopenharmony_ci nodep = Curl_hash_pick(&hash_static, &key3, strlen(key3)); 14813498266Sopenharmony_ci fail_unless(nodep == key3, "hash retrieval failed"); 14913498266Sopenharmony_ci 15013498266Sopenharmony_ci /* Clean up */ 15113498266Sopenharmony_ci Curl_hash_clean(&hash_static); 15213498266Sopenharmony_ci 15313498266Sopenharmony_ciUNITTEST_STOP 154