]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libuuid: test time-based UUID generation
authorThomas Weißschuh <thomas@t-8ch.de>
Thu, 16 May 2024 17:05:01 +0000 (19:05 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Thu, 16 May 2024 21:45:09 +0000 (23:45 +0200)
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
libuuid/meson.build
libuuid/src/Makemodule.am
libuuid/src/gen_uuid.c
meson.build
tests/commands.sh
tests/expected/uuid/time [new file with mode: 0644]
tests/ts/uuid/time [new file with mode: 0755]

index b67b3d5e7225029c9dda01556c38e9584ddf5388..7046da930fdf843251a63dcff9fc8f88ce80c79a 100644 (file)
@@ -14,6 +14,7 @@ lib_uuid_sources = '''
 '''.split()
 
 predefined_c = files('src/predefined.c')
+pack_c = files('src/pack.c')
 unpack_c = files('src/unpack.c')
 unparse_c = files('src/unparse.c')
 
@@ -57,3 +58,11 @@ if build_libuuid
     meson.override_dependency('uuid', uuid_dep)
   endif
 endif
+
+test_uuid_time_sources = files('src/gen_uuid.c') + [
+  pack_c,
+  unpack_c,
+  randutils_c,
+  md5_c,
+  sha1_c,
+]
index be27811230261a359cacd04eb034a42398c1fe57..e36363697176bb94ed975c5d182f8c8b87af6675 100644 (file)
@@ -4,6 +4,12 @@ test_uuid_parser_SOURCES = libuuid/src/test_uuid.c
 test_uuid_parser_LDADD = libuuid.la $(SOCKET_LIBS) $(LDADD)
 test_uuid_parser_CFLAGS = $(AM_CFLAGS) -I$(ul_libuuid_incdir)
 
+check_PROGRAMS += test_uuid_time
+test_uuid_time_SOURCES = libuuid/src/gen_uuid.c \
+       libuuid/src/pack.c libuuid/src/unpack.c lib/randutils.c lib/md5.c lib/sha1.c
+test_uuid_time_LDADD = libuuid.la $(SOCKET_LIBS) $(LDADD)
+test_uuid_time_CFLAGS = $(AM_CFLAGS) -I$(ul_libuuid_incdir) -DTEST_PROGRAM
+
 # includes
 uuidincdir = $(includedir)/uuid
 uuidinc_HEADERS = libuuid/src/uuid.h
index e80e371c47371b24c55de36475db77148702852e..806454448d04098cb64ec5507ac3b7059c647b97 100644 (file)
@@ -121,6 +121,17 @@ static int getuid (void)
 }
 #endif
 
+#ifdef TEST_PROGRAM
+#define gettimeofday gettimeofday_fixed
+
+static int gettimeofday_fixed(struct timeval *tv, void *tz __attribute__((unused)))
+{
+       tv->tv_sec = 1645557742;
+       tv->tv_usec = 123456;
+       return 0;
+}
+#endif
+
 /*
  * Get the ethernet hardware address, if we can find it...
  *
@@ -454,7 +465,7 @@ error:
        return -1;
 }
 
-#if defined(HAVE_UUIDD) && defined(HAVE_SYS_UN_H)
+#if defined(HAVE_UUIDD) && defined(HAVE_SYS_UN_H) && !defined(TEST_PROGRAM)
 
 /*
  * Try using the uuidd daemon to generate the UUID
@@ -800,3 +811,25 @@ void uuid_generate_sha1(uuid_t out, const uuid_t ns, const char *name, size_t le
        uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x5000;
        uuid_pack(&uu, out);
 }
+
+#ifdef TEST_PROGRAM
+int main(void)
+{
+       char buf[UUID_STR_LEN];
+       uuid_t uuid;
+
+       uuid_generate_time(uuid);
+       uuid_unparse(uuid, buf);
+       printf("%s\n", buf);
+
+       uuid_generate_time_v6(uuid);
+       uuid_unparse(uuid, buf);
+       printf("%s\n", buf);
+
+       uuid_generate_time_v7(uuid);
+       uuid_unparse(uuid, buf);
+       printf("%s\n", buf);
+
+       return 0;
+}
+#endif
index 8b3149102c21c3f55f47a8bc7238818152281659..3df8918c4619283726481bba779d4e9c3711ba24 100644 (file)
@@ -3468,6 +3468,17 @@ if not is_disabler(exe)
   exes += exe
 endif
 
+exe = executable(
+  'test_uuid_time',
+  test_uuid_time_sources,
+  include_directories : [dir_include],
+  link_with : lib_uuid,
+  c_args : '-DTEST_PROGRAM',
+  build_by_default : program_tests)
+if not is_disabler(exe)
+  exes += exe
+endif
+
 ############################################################
 
 libfdisk_tests_cflags = ['-DTEST_PROGRAM']
index 9eef92ccbb722f9ec3aed243022b5fefd39feebc..3af675962ac8f958f9b5d9f05e4c9871c59b6244 100644 (file)
@@ -53,6 +53,7 @@ TS_HELPER_SYSINFO="${ts_helpersdir}test_sysinfo"
 TS_HELPER_TIOCSTI="${ts_helpersdir}test_tiocsti"
 TS_HELPER_UUID_PARSER="${ts_helpersdir}test_uuid_parser"
 TS_HELPER_UUID_NAMESPACE="${ts_helpersdir}test_uuid_namespace"
+TS_HELPER_UUID_TIME="${ts_helpersdir}test_uuid_time"
 TS_HELPER_MBSENCODE="${ts_helpersdir}test_mbsencode"
 TS_HELPER_CAL="${ts_helpersdir}test_cal"
 TS_HELPER_LAST_FUZZ="${ts_helpersdir}test_last_fuzz"
diff --git a/tests/expected/uuid/time b/tests/expected/uuid/time
new file mode 100644 (file)
index 0000000..e850d68
--- /dev/null
@@ -0,0 +1,4 @@
+VARIANT TYPE       TIME
+DCE     time-based 2022-02-22 19:22:22,123456+00:00
+DCE     time-v6    2022-02-22 19:22:22,123456+00:00
+DCE     time-v7    2022-02-22 19:22:22,123000+00:00
diff --git a/tests/ts/uuid/time b/tests/ts/uuid/time
new file mode 100755 (executable)
index 0000000..f8d283d
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/bash
+
+# This file is part of util-linux.
+#
+# This file is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="uuid_time"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_HELPER_UUID_TIME"
+ts_check_test_command "$TS_CMD_UUIDPARSE"
+
+$TS_HELPER_UUID_TIME | TZ=UTC $TS_CMD_UUIDPARSE -o VARIANT,TYPE,TIME \
+       >> $TS_OUTPUT 2>> $TS_ERRLOG
+
+ts_finalize