From 1b00331a88c396ab8c94188855074ed907a5bd66 Mon Sep 17 00:00:00 2001
From: "Alexander A. Klimov" <alexander.klimov@icinga.com>
Date: Fri, 22 Feb 2019 16:58:26 +0100
Subject: [PATCH] Test Utility::ComparePasswords()

---
 test/CMakeLists.txt   |  3 ++
 test/base-utility.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
 create mode 100644 test/base-utility.cpp

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index c78f2732d..714d6a768 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -36,6 +36,7 @@ set(base_test_SOURCES
   base-string.cpp
   base-timer.cpp
   base-type.cpp
+  base-utility.cpp
   base-value.cpp
   config-ops.cpp
   icinga-checkresult.cpp
@@ -120,6 +121,8 @@ add_boost_test(base
     base_type/assign
     base_type/byname
     base_type/instantiate
+    base_utility/comparepasswords_works
+    base_utility/comparepasswords_issafe
     base_value/scalar
     base_value/convert
     base_value/format
diff --git a/test/base-utility.cpp b/test/base-utility.cpp
new file mode 100644
index 000000000..fdc1a17d2
--- /dev/null
+++ b/test/base-utility.cpp
@@ -0,0 +1,70 @@
+/******************************************************************************
+ * Icinga 2                                                                   *
+ * Copyright (C) 2012-2018 Icinga Development Team (https://icinga.com/)      *
+ *                                                                            *
+ * 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 St, Fifth Floor, Boston, MA 02110-1301, USA.             *
+ ******************************************************************************/
+
+#include "base/utility.hpp"
+#include <chrono>
+#include <BoostTestTargetConfig.h>
+
+using namespace icinga;
+
+BOOST_AUTO_TEST_SUITE(base_utility)
+
+BOOST_AUTO_TEST_CASE(comparepasswords_works)
+{
+	BOOST_CHECK(Utility::ComparePasswords("", ""));
+
+	BOOST_CHECK(!Utility::ComparePasswords("x", ""));
+	BOOST_CHECK(!Utility::ComparePasswords("", "x"));
+
+	BOOST_CHECK(Utility::ComparePasswords("x", "x"));
+	BOOST_CHECK(!Utility::ComparePasswords("x", "y"));
+
+	BOOST_CHECK(Utility::ComparePasswords("abcd", "abcd"));
+	BOOST_CHECK(!Utility::ComparePasswords("abc", "abcd"));
+	BOOST_CHECK(!Utility::ComparePasswords("abcde", "abcd"));
+}
+
+BOOST_AUTO_TEST_CASE(comparepasswords_issafe)
+{
+	using std::chrono::duration_cast;
+	using std::chrono::microseconds;
+	using std::chrono::steady_clock;
+
+	String a, b;
+
+	a.Append(200000001, 'a');
+	b.Append(200000002, 'b');
+
+	auto start1 (steady_clock::now());
+
+	Utility::ComparePasswords(a, a);
+
+	auto duration1 (steady_clock::now() - start1);
+
+	auto start2 (steady_clock::now());
+
+	Utility::ComparePasswords(a, b);
+
+	auto duration2 (steady_clock::now() - start2);
+
+	double diff = (double)duration_cast<microseconds>(duration1).count() / (double)duration_cast<microseconds>(duration2).count();
+	BOOST_CHECK(0.9 <= diff && diff <= 1.1);
+}
+
+BOOST_AUTO_TEST_SUITE_END()