From b024ca101ec0c274595407158edad945bec7868d Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Fri, 5 Apr 2019 21:33:27 +0200 Subject: [PATCH] Dive site: make UUID generation deterministic Instead of using a random UUID, use an SHA1 hash of name, description and notes (if defined). This is necessary for testing. Signed-off-by: Berthold Stoeger --- core/divesite.c | 26 ++++++++++++++++++++------ dives/TestDiveDivelogsDE.xml | 4 ++-- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/core/divesite.c b/core/divesite.c index 8757aeed7..85ce8da37 100644 --- a/core/divesite.c +++ b/core/divesite.c @@ -6,6 +6,7 @@ #include "divelist.h" #include "membuffer.h" #include "table.h" +#include "sha1.h" #include @@ -129,14 +130,27 @@ static MAKE_REMOVE(dive_site_table, struct dive_site *, dive_site) int add_dive_site_to_table(struct dive_site *ds, struct dive_site_table *ds_table) { + /* If the site doesn't yet have an UUID, create a new one. + * Make this deterministic for testing. */ + if (!ds->uuid) { + SHA_CTX ctx; + uint32_t csum[5]; + + SHA1_Init(&ctx); + if (ds->name) + SHA1_Update(&ctx, ds->name, strlen(ds->name)); + if (ds->description) + SHA1_Update(&ctx, ds->description, strlen(ds->description)); + if (ds->notes) + SHA1_Update(&ctx, ds->notes, strlen(ds->notes)); + SHA1_Final((unsigned char *)csum, &ctx); + ds->uuid = csum[0]; + } + /* Take care to never have the same uuid twice. This could happen on * reimport of a log where the dive sites have diverged */ - while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL) { - ds->uuid = rand() & 0xff; - ds->uuid |= (rand() & 0xff) << 8; - ds->uuid |= (rand() & 0xff) << 16; - ds->uuid |= (rand() & 0xff) << 24; - } + while (ds->uuid == 0 || get_dive_site_by_uuid(ds->uuid, ds_table) != NULL) + ++ds->uuid; int idx = dive_site_table_get_insertion_index(ds_table, ds); add_to_dive_site_table(ds_table, idx, ds); diff --git a/dives/TestDiveDivelogsDE.xml b/dives/TestDiveDivelogsDE.xml index 38045119d..fe56f858f 100644 --- a/dives/TestDiveDivelogsDE.xml +++ b/dives/TestDiveDivelogsDE.xml @@ -4,11 +4,11 @@ - + - + Linus Second deep dive Linus and I planned and executed - Alex just watching