From 3ac1b2d952ea200e66187c40e58729ebc84d8f11 Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Fri, 18 Jun 2010 22:47:29 +0000
Subject: [PATCH] - Added runtime and compiletime version information
---
include/polarssl/config.h | 5 ++
include/polarssl/version.h | 74 ++++++++++++++++++++++++
library/CMakeLists.txt | 1 +
library/Makefile | 2 +-
library/version.c | 46 +++++++++++++++
tests/CMakeLists.txt | 1 +
tests/Makefile | 7 ++-
tests/suites/test_suite_version.data | 5 ++
tests/suites/test_suite_version.function | 60 +++++++++++++++++++
9 files changed, 199 insertions(+), 2 deletions(-)
create mode 100644 include/polarssl/version.h
create mode 100644 library/version.c
create mode 100644 tests/suites/test_suite_version.data
create mode 100644 tests/suites/test_suite_version.function
diff --git a/include/polarssl/config.h b/include/polarssl/config.h
index 64ec89e22..ca8782804 100644
--- a/include/polarssl/config.h
+++ b/include/polarssl/config.h
@@ -76,6 +76,11 @@
*/
#define POLARSSL_SELF_TEST
+/*
+ * Enable run-time version information functions
+ */
+#define POLARSSL_VERSION_C
+
/*
* Enable the prime-number generation code.
*/
diff --git a/include/polarssl/version.h b/include/polarssl/version.h
new file mode 100644
index 000000000..88cb6093b
--- /dev/null
+++ b/include/polarssl/version.h
@@ -0,0 +1,74 @@
+/**
+ * \file version.h
+ *
+ * Copyright (C) 2006-2010, Paul Bakker
+ * All rights reserved.
+ *
+ * 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; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * This set of compile-time defines and run-time variables can be used to
+ * determine the version number of the PolarSSL library used.
+ */
+#ifndef POLARSSL_VERSION_H
+#define POLARSSL_VERSION_H
+
+#include "polarssl/config.h"
+
+/**
+ * The version number x.y.z is split into three parts.
+ * Major, Minor, Patchlevel
+ */
+#define POLARSSL_VERSION_MAJOR 0
+#define POLARSSL_VERSION_MINOR 13
+#define POLARSSL_VERSION_PATCH 1
+
+/**
+ * The single version number has the following structure:
+ * MMNNPP00
+ * Major version | Minor version | Patch version
+ */
+#define POLARSSL_VERSION_NUMBER 0x000D0100
+#define POLARSSL_VERSION_STRING "0.13.1"
+#define POLARSSL_VERSION_STRING_FULL "PolarSSL 0.13.1"
+
+#if defined(POLARSSL_VERSION_C)
+
+/**
+ * Get the version number.
+ *
+ * @return The constructed version number in the format
+ * MMNNPP00 (Major, Minor, Patch).
+ */
+unsigned int version_get_number();
+
+/**
+ * Get the version string ("x.y.z").
+ *
+ * @param string The string that will receive the value.
+ * (Should be at least 9 bytes in size)
+ */
+void version_get_string( char *string );
+
+/**
+ * Get the full version string ("PolarSSL x.y.z").
+ *
+ * @param string The string that will receive the value.
+ * (Should be at least 18 bytes in size)
+ */
+void version_get_string_full( char *string );
+
+#endif /* POLARSSL_VERSION_C */
+
+#endif /* version.h */
diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
index c545d56e3..9e84ca65b 100644
--- a/library/CMakeLists.txt
+++ b/library/CMakeLists.txt
@@ -22,6 +22,7 @@ add_library(polarssl STATIC
ssl_srv.c
ssl_tls.c
timing.c
+ version.c
x509parse.c
xtea.c
)
diff --git a/library/Makefile b/library/Makefile
index 205c6a2f0..e27f2bfd8 100644
--- a/library/Makefile
+++ b/library/Makefile
@@ -25,7 +25,7 @@ OBJS= aes.o arc4.o base64.o \
sha1.o sha2.o sha4.o \
ssl_cli.o ssl_srv.o ssl_tls.o \
timing.o x509parse.o xtea.o \
- camellia.o
+ camellia.o version.o
.SILENT:
diff --git a/library/version.c b/library/version.c
new file mode 100644
index 000000000..a43e48888
--- /dev/null
+++ b/library/version.c
@@ -0,0 +1,46 @@
+/*
+ * Version information
+ *
+ * Copyright (C) 2006-2010, Paul Bakker
+ * All rights reserved.
+ *
+ * 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; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "polarssl/config.h"
+
+#if defined(POLARSSL_VERSION_C)
+
+#include "polarssl/version.h"
+#include
+
+const char version[] = POLARSSL_VERSION_STRING;
+
+unsigned int version_get_number()
+{
+ return POLARSSL_VERSION_NUMBER;
+}
+
+void version_get_string( char *string )
+{
+ memcpy( string, POLARSSL_VERSION_STRING, sizeof( POLARSSL_VERSION_STRING ) );
+}
+
+void version_get_string_full( char *string )
+{
+ memcpy( string, POLARSSL_VERSION_STRING_FULL, sizeof( POLARSSL_VERSION_STRING_FULL ) );
+}
+
+#endif /* POLARSSL_VERSION_C */
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 5f7311276..7c1fa7206 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -26,3 +26,4 @@ add_test_suite(xtea)
add_test_suite(dhm)
add_test_suite(x509parse)
add_test_suite(debug)
+add_test_suite(version)
diff --git a/tests/Makefile b/tests/Makefile
index ee6507af8..c90d8b3ae 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -14,7 +14,8 @@ APPS = test_suite_aes test_suite_arc4 \
test_suite_hmac_shax test_suite_mdx \
test_suite_mpi test_suite_rsa \
test_suite_shax test_suite_x509parse\
- test_suite_xtea test_suite_debug
+ test_suite_xtea test_suite_debug \
+ test_suite_version
.SILENT:
@@ -80,6 +81,10 @@ test_suite_debug: test_suite_debug.c ../library/libpolarssl.a
echo " CC $@.c"
$(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
+test_suite_version: test_suite_version.c ../library/libpolarssl.a
+ echo " CC $@.c"
+ $(CC) $(CFLAGS) $(OFLAGS) $@.c $(LDFLAGS) -o $@
+
clean:
rm -f $(APPS) *.c
diff --git a/tests/suites/test_suite_version.data b/tests/suites/test_suite_version.data
new file mode 100644
index 000000000..6d75b0f37
--- /dev/null
+++ b/tests/suites/test_suite_version.data
@@ -0,0 +1,5 @@
+Check compiletime library version
+check_compiletime_version:"0.13.1"
+
+Check runtime library version
+check_runtime_version:"0.13.1"
diff --git a/tests/suites/test_suite_version.function b/tests/suites/test_suite_version.function
new file mode 100644
index 000000000..958857e18
--- /dev/null
+++ b/tests/suites/test_suite_version.function
@@ -0,0 +1,60 @@
+BEGIN_HEADER
+#include
+END_HEADER
+
+BEGIN_CASE
+check_compiletime_version:version_str
+{
+ char build_str[100];
+ char build_str_full[100];
+ unsigned int build_int;
+
+ memset( build_str, 0, 100 );
+ memset( build_str_full, 0, 100 );
+
+ snprintf (build_str, 100, "%d.%d.%d", POLARSSL_VERSION_MAJOR,
+ POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH );
+
+ snprintf( build_str_full, 100, "PolarSSL %d.%d.%d", POLARSSL_VERSION_MAJOR,
+ POLARSSL_VERSION_MINOR, POLARSSL_VERSION_PATCH );
+
+ build_int = POLARSSL_VERSION_MAJOR << 24 |
+ POLARSSL_VERSION_MINOR << 16 |
+ POLARSSL_VERSION_PATCH << 8;
+
+ TEST_ASSERT( build_int == POLARSSL_VERSION_NUMBER );
+ TEST_ASSERT( strcmp( build_str, POLARSSL_VERSION_STRING ) == 0 );
+ TEST_ASSERT( strcmp( build_str_full, POLARSSL_VERSION_STRING_FULL ) == 0 );
+ TEST_ASSERT( strcmp( {version_str}, POLARSSL_VERSION_STRING ) == 0 );
+}
+END_CASE
+
+BEGIN_CASE
+check_runtime_version:version_str
+{
+ char build_str[100];
+ char get_str[100];
+ char build_str_full[100];
+ char get_str_full[100];
+ unsigned int get_int;
+
+ memset( build_str, 0, 100 );
+ memset( get_str, 0, 100 );
+ memset( build_str_full, 0, 100 );
+ memset( get_str_full, 0, 100 );
+
+ get_int = version_get_number();
+ version_get_string( get_str );
+ version_get_string_full( get_str_full );
+
+ snprintf( build_str, 100, "%d.%d.%d",
+ (get_int >> 24) & 0xFF,
+ (get_int >> 16) & 0xFF,
+ (get_int >> 8) & 0xFF );
+ snprintf( build_str_full, 100, "PolarSSL %s", {version_str} );
+
+ TEST_ASSERT( strcmp( build_str, {version_str} ) == 0 );
+ TEST_ASSERT( strcmp( build_str_full, get_str_full ) == 0 );
+ TEST_ASSERT( strcmp( {version_str}, get_str ) == 0 );
+}
+END_CASE