Path: blob/main/crypto/krb5/src/util/support/t_hashtab.c
34889 views
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/* util/support/t_hash.c - tests for hash table code */2/*3* Copyright (C) 2018 by the Massachusetts Institute of Technology.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* * Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12*13* * Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in15* the documentation and/or other materials provided with the16* distribution.17*18* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS19* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT20* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE22* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,23* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES24* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR25* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)26* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,27* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)28* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED29* OF THE POSSIBILITY OF SUCH DAMAGE.30*/3132/* hash.c has no linker dependencies, so we can simply include its source code33* to test its static functions and look inside its structures. */34#include "hashtab.c"3536/* These match the sip64 test vectors in the reference C implementation of37* siphash at https://github.com/veorq/SipHash */38const uint64_t vectors[64] = {390x726FDB47DD0E0E31,400x74F839C593DC67FD,410x0D6C8009D9A94F5A,420x85676696D7FB7E2D,430xCF2794E0277187B7,440x18765564CD99A68D,450xCBC9466E58FEE3CE,460xAB0200F58B01D137,470x93F5F5799A932462,480x9E0082DF0BA9E4B0,490x7A5DBBC594DDB9F3,500xF4B32F46226BADA7,510x751E8FBC860EE5FB,520x14EA5627C0843D90,530xF723CA908E7AF2EE,540xA129CA6149BE45E5,550x3F2ACC7F57C29BDB,560x699AE9F52CBE4794,570x4BC1B3F0968DD39C,580xBB6DC91DA77961BD,590xBED65CF21AA2EE98,600xD0F2CBB02E3B67C7,610x93536795E3A33E88,620xA80C038CCD5CCEC8,630xB8AD50C6F649AF94,640xBCE192DE8A85B8EA,650x17D835B85BBB15F3,660x2F2E6163076BCFAD,670xDE4DAAACA71DC9A5,680xA6A2506687956571,690xAD87A3535C49EF28,700x32D892FAD841C342,710x7127512F72F27CCE,720xA7F32346F95978E3,730x12E0B01ABB051238,740x15E034D40FA197AE,750x314DFFBE0815A3B4,760x027990F029623981,770xCADCD4E59EF40C4D,780x9ABFD8766A33735C,790x0E3EA96B5304A7D0,800xAD0C42D6FC585992,810x187306C89BC215A9,820xD4A60ABCF3792B95,830xF935451DE4F21DF2,840xA9538F0419755787,850xDB9ACDDFF56CA510,860xD06C98CD5C0975EB,870xE612A3CB9ECBA951,880xC766E62CFCADAF96,890xEE64435A9752FE72,900xA192D576B245165A,910x0A8787BF8ECB74B2,920x81B3E73D20B49B6F,930x7FA8220BA3B2ECEA,940x245731C13CA42499,950xB78DBFAF3A8D83BD,960xEA1AD565322A1A0B,970x60E61C23A3795013,980x6606D7E446282B93,990x6CA4ECB15C5F91E1,1000x9F626DA15C9625F3,1010xE51B38608EF25F57,1020x958A324CEB064572103};104105static void106test_siphash(void)107{108uint8_t seq[64];109uint64_t k0, k1, hval;110size_t i;111112for (i = 0; i < sizeof(seq); i++)113seq[i] = i;114k0 = load_64_le(seq);115k1 = load_64_le(seq + 8);116117for (i = 0; i < sizeof(seq); i++) {118hval = siphash24(seq, i, k0, k1);119assert(hval == vectors[i]);120}121}122123static void124test_hashtab(void)125{126int st;127struct k5_hashtab *ht;128size_t i;129char zeros[100] = { 0 };130131st = k5_hashtab_create(NULL, 4, &ht);132assert(st == 0 && ht != NULL && ht->nentries == 0);133134st = k5_hashtab_add(ht, "abc", 3, &st);135assert(st == 0 && ht->nentries == 1);136assert(k5_hashtab_get(ht, "abc", 3) == &st);137assert(k5_hashtab_get(ht, "bcde", 4) == NULL);138139st = k5_hashtab_add(ht, "bcde", 4, &ht);140assert(st == 0 && ht->nentries == 2);141assert(k5_hashtab_get(ht, "abc", 3) == &st);142assert(k5_hashtab_get(ht, "bcde", 4) == &ht);143144k5_hashtab_remove(ht, "abc", 3);145assert(ht->nentries == 1);146assert(k5_hashtab_get(ht, "abc", 3) == NULL);147assert(k5_hashtab_get(ht, "bcde", 4) == &ht);148149k5_hashtab_remove(ht, "bcde", 4);150assert(ht->nentries == 0);151assert(k5_hashtab_get(ht, "abc", 3) == NULL);152assert(k5_hashtab_get(ht, "bcde", 4) == NULL);153154for (i = 0; i < sizeof(zeros); i++) {155st = k5_hashtab_add(ht, zeros, i, zeros + i);156assert(st == 0 && ht->nentries == i + 1 && ht->nbuckets >= i + 1);157}158for (i = 0; i < sizeof(zeros); i++) {159assert(k5_hashtab_get(ht, zeros, i) == zeros + i);160k5_hashtab_remove(ht, zeros, i);161assert(ht->nentries == sizeof(zeros) - i - 1);162if (i > 0)163assert(k5_hashtab_get(ht, zeros, i - 1) == NULL);164}165166k5_hashtab_free(ht);167}168169int170main(void)171{172test_siphash();173test_hashtab();174return 0;175}176177178