]> git.puffer.fish Git - matthieu/frr.git/commitdiff
lib: add frr_version_cmp()
authorDavid Lamparter <equinox@opensourcerouting.org>
Fri, 26 Oct 2018 13:25:25 +0000 (15:25 +0200)
committerDavid Lamparter <equinox@diac24.net>
Fri, 6 Dec 2019 14:13:32 +0000 (15:13 +0100)
This just compares 2 version strings.

Signed-off-by: David Lamparter <equinox@diac24.net>
lib/defaults.c [new file with mode: 0644]
lib/defaults.h [new file with mode: 0644]
lib/libfrr.c
lib/subdir.am
tests/.gitignore
tests/lib/test_versioncmp.c [new file with mode: 0644]
tests/lib/test_versioncmp.py [new file with mode: 0644]
tests/subdir.am

diff --git a/lib/defaults.c b/lib/defaults.c
new file mode 100644 (file)
index 0000000..cfc8fa2
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * FRR switchable defaults.
+ * Copyright (c) 2017-2019  David Lamparter, for NetDEF, Inc.
+ *
+ * Permission to use, copy, modify, and 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.
+ */
+
+#include <zebra.h>
+
+#include "defaults.h"
+#include "libfrr.h"
+#include "version.h"
+
+static int version_value(int ch)
+{
+       /* non-ASCII shouldn't happen */
+       if (ch < 0 || ch >= 128)
+               return 2;
+
+       /* ~foo sorts older than nothing */
+       if (ch == '~')
+               return 0;
+       if (ch == '\0')
+               return 1;
+       if (isalpha(ch))
+               return 0x100 + tolower(ch);
+
+       /* punctuation and digits (and everything else) */
+       return 0x200 + ch;
+}
+
+int frr_version_cmp(const char *aa, const char *bb)
+{
+       const char *apos = aa, *bpos = bb;
+
+       /* || is correct, we won't scan past the end of a string since that
+        * doesn't compare equal to anything else */
+       while (apos[0] || bpos[0]) {
+               if (isdigit((unsigned char)apos[0]) &&
+                   isdigit((unsigned char)bpos[0])) {
+                       unsigned long av, bv;
+                       char *aend = NULL, *bend = NULL;
+
+                       av = strtoul(apos, &aend, 10);
+                       bv = strtoul(bpos, &bend, 10);
+                       if (av < bv)
+                               return -1;
+                       if (av > bv)
+                               return 1;
+
+                       apos = aend;
+                       bpos = bend;
+                       continue;
+               }
+
+               int a = version_value(*apos++);
+               int b = version_value(*bpos++);
+
+               if (a < b)
+                       return -1;
+               if (a > b)
+                       return 1;
+       }
+       return 0;
+}
diff --git a/lib/defaults.h b/lib/defaults.h
new file mode 100644 (file)
index 0000000..c215341
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * FRR switchable defaults.
+ * Copyright (C) 2017-2019  David Lamparter for NetDEF, Inc.
+ *
+ * Permission to use, copy, modify, and 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.
+ */
+
+#ifndef _FRR_DEFAULTS_H
+#define _FRR_DEFAULTS_H
+
+/* like strcmp(), but with version ordering */
+extern int frr_version_cmp(const char *aa, const char *bb);
+
+#endif /* _FRR_DEFAULTS_H */
index 5f292c989c16be7e0a94e5f8bbee30710d87d679..1dd7e932c58b450757aebc84fc3f87140204a964 100644 (file)
@@ -175,7 +175,6 @@ static const struct optspec os_user = {"u:g:",
                                       "  -g, --group        Group to run as\n",
                                       lo_user};
 
-
 bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len,
                      const char *path)
 {
index be059c89463eb90687d6069df9bcc2e447eed30d..cb6fa7a3b81c050098c8adb2d29e0deba7f1b265 100644 (file)
@@ -18,6 +18,7 @@ lib_libfrr_la_SOURCES = \
        lib/command_parse.y \
        lib/csv.c \
        lib/debug.c \
+       lib/defaults.c \
        lib/distribute.c \
        lib/ferr.c \
        lib/filter.c \
@@ -157,6 +158,7 @@ pkginclude_HEADERS += \
        lib/csv.h \
        lib/db.h \
        lib/debug.h \
+       lib/defaults.h \
        lib/distribute.h \
        lib/ferr.h \
        lib/filter.h \
index 6252cea823aace5395a706abb584b2f79159778a..5414cb8cc914a1b510631c0a7ed3dd3b2415a11b 100644 (file)
@@ -45,6 +45,7 @@
 /lib/test_timer_performance
 /lib/test_ttable
 /lib/test_typelist
+/lib/test_versioncmp
 /lib/test_zlog
 /lib/test_zmq
 /ospf6d/test_lsdb
diff --git a/tests/lib/test_versioncmp.c b/tests/lib/test_versioncmp.c
new file mode 100644 (file)
index 0000000..bb819e3
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * frr_version_cmp() tests
+ * Copyright (C) 2018  David Lamparter for NetDEF, Inc.
+ *
+ * This program 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 program 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.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; see the file COPYING; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include <zebra.h>
+#include <defaults.h>
+
+static const char *rel(int x)
+{
+       if (x < 0)
+               return "<";
+       if (x > 0)
+               return ">";
+       return "==";
+}
+
+static int fail;
+
+static void compare(const char *a, const char *b, int expect)
+{
+       int result = frr_version_cmp(a, b);
+
+       if (expect == result)
+               printf("\"%s\" %s \"%s\"\n", a, rel(result), b);
+       else {
+               printf("\"%s\" %s \"%s\", expected %s!\n", a, rel(result), b,
+                               rel(expect));
+               fail = 1;
+       }
+}
+
+int main(int argc, char **argv)
+{
+       compare("", "", 0);
+       compare("1", "1", 0);
+       compare("1.0", "1.00", 0);
+       compare("10.0", "1", 1);
+       compare("10.0", "2", 1);
+       compare("2.1", "10.0", -1);
+       compare("1.1.1", "1.1.0", 1);
+       compare("1.0a", "1.0", 1);
+       compare("1.0a", "1.0b", -1);
+       compare("1.0a10", "1.0a2", 1);
+       compare("1.00a2", "1.0a2", 0);
+       compare("1.00a2", "1.0a3", -1);
+       compare("1.0-dev", "1.0", 1);
+       compare("1.0~foo", "1.0", -1);
+       compare("1.0~1", "1.0~0", 1);
+       compare("1.00~1", "1.0~0", 1);
+       printf("final tally: %s\n", fail ? "FAILED" : "ok");
+       return fail;
+}
diff --git a/tests/lib/test_versioncmp.py b/tests/lib/test_versioncmp.py
new file mode 100644 (file)
index 0000000..0990757
--- /dev/null
@@ -0,0 +1,6 @@
+import frrtest
+
+class TestVersionCmp(frrtest.TestMultiOut):
+    program = './test_versioncmp'
+
+TestVersionCmp.exit_cleanly()
index 270c0811b49cb169de79f3310c9f387ccb532d5c..d87d348949378044d2f9968ca06c1412ad2b8867 100644 (file)
@@ -69,6 +69,7 @@ check_PROGRAMS = \
        tests/lib/test_timer_performance \
        tests/lib/test_ttable \
        tests/lib/test_typelist \
+       tests/lib/test_versioncmp \
        tests/lib/test_zlog \
        tests/lib/test_graph \
        tests/lib/cli/test_cli \
@@ -293,6 +294,10 @@ tests_lib_test_typelist_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_typelist_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_typelist_LDADD = $(ALL_TESTS_LDADD)
 tests_lib_test_typelist_SOURCES = tests/lib/test_typelist.c tests/helpers/c/prng.c
+tests_lib_test_versioncmp_CFLAGS = $(TESTS_CFLAGS)
+tests_lib_test_versioncmp_CPPFLAGS = $(TESTS_CPPFLAGS)
+tests_lib_test_versioncmp_LDADD = $(ALL_TESTS_LDADD)
+tests_lib_test_versioncmp_SOURCES = tests/lib/test_versioncmp.c
 tests_lib_test_zlog_CFLAGS = $(TESTS_CFLAGS)
 tests_lib_test_zlog_CPPFLAGS = $(TESTS_CPPFLAGS)
 tests_lib_test_zlog_LDADD = $(ALL_TESTS_LDADD)
@@ -344,6 +349,7 @@ EXTRA_DIST += \
        tests/lib/test_ttable.py \
        tests/lib/test_ttable.refout \
        tests/lib/test_typelist.py \
+       tests/lib/test_versioncmp.py \
        tests/lib/test_zlog.py \
        tests/lib/test_graph.py \
        tests/lib/test_graph.refout \