summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-02 19:59:41 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-02 19:59:41 +0100
commit992a1b3d2653ed527c2a301f80140bc35d84e832 (patch)
tree73981b8a28a0368f2b74b0369b1ecf649c908f77
parent20ceb53ec1c1bc18face8e831292dfe81fed3817 (diff)
Switch from ImageMagick to GraphicsMagick for deterministic data; added tests
-rw-r--r--common.mk4
-rw-r--r--qrcode.cpp10
-rw-r--r--qrcode.h8
-rw-r--r--tests/Makefile7
-rw-r--r--tests/test-compiledsql.cpp57
-rw-r--r--tests/test-qrcode.cpp140
-rw-r--r--tests/test-whiteboard.cpp57
-rw-r--r--whiteboard.cpp6
8 files changed, 277 insertions, 12 deletions
diff --git a/common.mk b/common.mk
index 2555a6d..30e6abb 100644
--- a/common.mk
+++ b/common.mk
@@ -60,8 +60,8 @@ else
LIBS+=-lstdc++ -lstdc++fs
endif
-CXXFLAGS+=$(shell pkg-config --cflags qrcodegencpp Magick++ fmt sqlite3)
+CXXFLAGS+=$(shell pkg-config --cflags qrcodegencpp GraphicsMagick++ fmt sqlite3)
LIBS+=-lfcgi -lboost_filesystem -lpthread
-LIBS+=-lSQLiteCpp $(shell pkg-config --libs qrcodegencpp Magick++ fmt sqlite3)
+LIBS+=-lSQLiteCpp $(shell pkg-config --libs qrcodegencpp GraphicsMagick++ fmt sqlite3)
LIBS+=-lreichwein
diff --git a/qrcode.cpp b/qrcode.cpp
index 5e45afc..da747a5 100644
--- a/qrcode.cpp
+++ b/qrcode.cpp
@@ -4,12 +4,17 @@
#include <qrcodegen/QrCode.hpp>
-#include <ImageMagick-6/Magick++.h>
+#include <Magick++.h>
using namespace qrcodegen;
using namespace Magick;
-std::string getQRCode(const std::string& data)
+void QRCode::init()
+{
+ Magick::InitializeMagick(NULL);
+}
+
+std::string QRCode::getQRCode(const std::string& data)
{
QrCode qrc {QrCode::encodeText(data.c_str(), QrCode::Ecc::MEDIUM)};
@@ -17,7 +22,6 @@ std::string getQRCode(const std::string& data)
Image image(fmt::format("{0}x{0}", size).c_str(), "white");
image.type(GrayscaleType);
- //image.size(fmt::format("{0}x{0}", size));
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
diff --git a/qrcode.h b/qrcode.h
index f6546f0..f37c318 100644
--- a/qrcode.h
+++ b/qrcode.h
@@ -2,4 +2,10 @@
#include <string>
-std::string getQRCode(const std::string& data);
+namespace QRCode
+{
+ void init();
+
+ // returns PNG file contents
+ std::string getQRCode(const std::string& data);
+}
diff --git a/tests/Makefile b/tests/Makefile
index 057eae9..dd357b3 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -14,7 +14,10 @@ endif
UNITS=storage.cpp config.cpp compiledsql.cpp qrcode.cpp whiteboard.cpp
UNITTESTS=test-config.cpp \
- test-storage.cpp
+ test-storage.cpp \
+ test-compiledsql.cpp \
+ test-qrcode.cpp \
+ test-whiteboard.cpp
CXXFLAGS+=\
-I/usr/src/googletest/googletest/include \
@@ -28,7 +31,7 @@ test: unittests
ifeq ($(CXXTYPE),clang++)
LLVM_PROFILE_FILE="unittests.profraw" ./unittests
$(LLVMPROFDATA) merge -sparse unittests.profraw -o unittests.profdata
- $(LLVMCOV) report --ignore-filename-regex='google' --ignore-filename-regex='test-' --show-region-summary=0 -instr-profile unittests.profdata unittests
+ $(LLVMCOV) report --ignore-filename-regex='google' --ignore-filename-regex='test-' --ignore-filename-regex='Magick' --show-region-summary=0 -instr-profile unittests.profdata unittests
else
./unittests
#gcov-12 storage.o
diff --git a/tests/test-compiledsql.cpp b/tests/test-compiledsql.cpp
new file mode 100644
index 0000000..4fe29e0
--- /dev/null
+++ b/tests/test-compiledsql.cpp
@@ -0,0 +1,57 @@
+#include <gtest/gtest.h>
+
+#include <filesystem>
+#include <memory>
+#include <string>
+#include <system_error>
+
+#include "libreichwein/file.h"
+
+#include "config.h"
+#include "storage.h"
+#include "whiteboard.h"
+
+namespace fs = std::filesystem;
+
+namespace {
+ const std::string testConfigFilename{"./test.conf"};
+ const std::string testDbFilename{"./whiteboard.db3"};
+}
+
+class CompiledSQLTest: public ::testing::Test
+{
+protected:
+ CompiledSQLTest(){
+ }
+
+ ~CompiledSQLTest() override{
+ }
+
+ void SetUp() override
+ {
+ File::setFile(testConfigFilename, R"CONFIG(
+<config>
+ <datapath>.</datapath>
+ <maxage>2592000</maxage>
+</config>
+)CONFIG");
+ std::error_code ec;
+ fs::remove(testDbFilename, ec);
+
+ m_config = std::make_shared<Config>(testConfigFilename);
+ }
+
+ void TearDown() override
+ {
+ std::error_code ec;
+ fs::remove(testDbFilename, ec);
+ fs::remove(testConfigFilename, ec);
+ }
+
+ std::shared_ptr<Config> m_config;
+};
+
+TEST_F(CompiledSQLTest, connection)
+{
+}
+
diff --git a/tests/test-qrcode.cpp b/tests/test-qrcode.cpp
new file mode 100644
index 0000000..96cfdb0
--- /dev/null
+++ b/tests/test-qrcode.cpp
@@ -0,0 +1,140 @@
+#include <gtest/gtest.h>
+
+#include <filesystem>
+#include <memory>
+#include <string>
+#include <system_error>
+
+#include "libreichwein/file.h"
+
+#include "qrcode.h"
+
+namespace fs = std::filesystem;
+
+class QRCodeTest: public ::testing::Test
+{
+protected:
+ QRCodeTest(){
+ }
+
+ ~QRCodeTest() override{
+ }
+
+ void SetUp() override
+ {
+ QRCode::init();
+ }
+
+ void TearDown() override
+ {
+ }
+
+};
+
+TEST_F(QRCodeTest, empty)
+{
+ unsigned char empty_png[] = {
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
+ 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x15,
+ 0x10, 0x00, 0x00, 0x00, 0x00, 0xdc, 0xec, 0x26, 0x09, 0x00, 0x00, 0x00,
+ 0xa7, 0x49, 0x44, 0x41, 0x54, 0x28, 0xcf, 0x7d, 0x92, 0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// Test prepared with:
+ //File::setFile("empty.png", QRCode::getQRCode(""));
+ //then: xxd -i tests/empty.png >> tests/test-qrcode.cpp
+
+ EXPECT_EQ(QRCode::getQRCode(""), std::string(reinterpret_cast<char*>(&empty_png[0]), sizeof(empty_png)));
+}
+
+TEST_F(QRCodeTest, simple)
+{
+ unsigned char simple_png[] = {
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
+ 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x15, 0x00, 0x00, 0x00, 0x15,
+ 0x10, 0x00, 0x00, 0x00, 0x00, 0xdc, 0xec, 0x26, 0x09, 0x00, 0x00, 0x00,
+ 0xa5, 0x49, 0x44, 0x41, 0x54, 0x28, 0xcf, 0x75, 0x52, 0xd1, 0x0e, 0xc0,
+ 0x40, 0x04, 0x6b, 0x17, 0xff, 0xff, 0xcb, 0xf6, 0xe0, 0x4c, 0x39, 0x93,
+ 0x6c, 0x09, 0x47, 0x55, 0x01, 0x9a, 0xb9, 0xbb, 0xbb, 0x03, 0xf9, 0xa9,
+ 0x99, 0x86, 0x48, 0x80, 0x8c, 0xe4, 0x2c, 0xd5, 0xb7, 0xcf, 0x2d, 0xa4,
+ 0xfa, 0xeb, 0x9b, 0x61, 0xb5, 0xd9, 0x1c, 0x00, 0x9e, 0x2d, 0x2d, 0x88,
+ 0xcc, 0xb8, 0xdd, 0x18, 0xa4, 0x3b, 0xa9, 0xad, 0x7f, 0x1b, 0xff, 0x29,
+ 0xc0, 0xbd, 0x8a, 0x5c, 0xf0, 0x52, 0xc7, 0x2e, 0x90, 0x2a, 0x91, 0x51,
+ 0x4b, 0xfa, 0xc9, 0x2e, 0xfc, 0x40, 0x55, 0x6c, 0xb2, 0xe1, 0xf5, 0x4e,
+ 0xd3, 0x7f, 0xaa, 0xf2, 0xa6, 0x52, 0x24, 0x48, 0x77, 0xd9, 0xfa, 0x9c,
+ 0x7c, 0xae, 0xf7, 0x52, 0xa0, 0xab, 0xaa, 0x71, 0xeb, 0x5b, 0xa9, 0x21,
+ 0xfa, 0x50, 0x67, 0x5b, 0xe3, 0x7a, 0xd0, 0x67, 0x8f, 0x0e, 0xe4, 0x59,
+ 0x6c, 0xa4, 0x94, 0x96, 0xda, 0x29, 0xca, 0x80, 0xf5, 0x5c, 0x52, 0x53,
+ 0x3d, 0x9a, 0xa3, 0xcb, 0x7d, 0xaf, 0x93, 0xf9, 0xa1, 0xd2, 0xb9, 0xea,
+ 0xc6, 0x92, 0xf1, 0x7a, 0x0f, 0xaa, 0xee, 0x8c, 0x03, 0x2f, 0x88, 0x71,
+ 0xb5, 0xba, 0xf0, 0x88, 0xba, 0xb0, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
+ 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
+ };
+ // Test prepared with:
+ //File::setFile("simple.png", QRCode::getQRCode("abc"));
+ //then: xxd -i tests/simple.png >> tests/test-qrcode.cpp
+ EXPECT_EQ(QRCode::getQRCode("abc"), std::string(reinterpret_cast<char*>(&simple_png[0]), sizeof(simple_png)));
+}
+
+TEST_F(QRCodeTest, url)
+{
+ unsigned char url_png[] = {
+ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d,
+ 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x1d, 0x00, 0x00, 0x00, 0x1d,
+ 0x10, 0x00, 0x00, 0x00, 0x00, 0x23, 0x68, 0xe4, 0x90, 0x00, 0x00, 0x01,
+ 0x1f, 0x49, 0x44, 0x41, 0x54, 0x38, 0xcb, 0x75, 0x54, 0xdb, 0x0e, 0xc5,
+ 0x30, 0x08, 0x92, 0xa5, 0xff, 0xff, 0xcb, 0x9c, 0x07, 0xe7, 0x41, 0xb4,
+ 0x6b, 0xb2, 0x64, 0x36, 0x5e, 0xa8, 0xa0, 0x11, 0x76, 0xc8, 0x08, 0x92,
+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 0xb6, 0xe1, 0xbf,
+ 0xc2, 0x98, 0x97, 0x5d, 0x43, 0xaa, 0x5f, 0xdb, 0xca, 0x6f, 0x57, 0xd3,
+ 0xee, 0xe4, 0xec, 0x3e, 0x9c, 0x4d, 0x8e, 0x81, 0xc2, 0x2d, 0x5d, 0x56,
+ 0x7e, 0x32, 0x44, 0xbc, 0xed, 0x6a, 0x1a, 0x87, 0xf4, 0x2a, 0xe8, 0x6b,
+ 0xad, 0x75, 0x51, 0xfa, 0x66, 0xf4, 0x07, 0x01, 0x97, 0x8d, 0x08, 0x90,
+ 0x09, 0xb5, 0xef, 0x64, 0xe7, 0xbf, 0x49, 0x62, 0x8e, 0xfa, 0xec, 0xfb,
+ 0x26, 0xad, 0x8d, 0xba, 0x3b, 0xf5, 0xf6, 0xf8, 0x22, 0x95, 0xf7, 0x0f,
+ 0x4c, 0x20, 0xaf, 0x2d, 0xca, 0xaf, 0x0d, 0xc5, 0x00, 0x00, 0x00, 0x00,
+ 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
+ };
+ // Test prepared with:
+ //File::setFile("url.png", QRCode::getQRCode("https://reichwein.it/whiteboard"));
+ //then: xxd -i tests/url.png >> tests/test-qrcode.cpp
+ EXPECT_EQ(QRCode::getQRCode("https://reichwein.it/whiteboard"), std::string(reinterpret_cast<char*>(&url_png[0]), sizeof(url_png)));
+}
+
+TEST_F(QRCodeTest, deterministic)
+{
+ EXPECT_EQ(QRCode::getQRCode("data1"), QRCode::getQRCode("data1"));
+}
+
+TEST_F(QRCodeTest, different_data)
+{
+ EXPECT_NE(QRCode::getQRCode("data1"), QRCode::getQRCode("data2"));
+}
diff --git a/tests/test-whiteboard.cpp b/tests/test-whiteboard.cpp
new file mode 100644
index 0000000..6fe476d
--- /dev/null
+++ b/tests/test-whiteboard.cpp
@@ -0,0 +1,57 @@
+#include <gtest/gtest.h>
+
+#include <filesystem>
+#include <memory>
+#include <string>
+#include <system_error>
+
+#include "libreichwein/file.h"
+
+#include "config.h"
+#include "storage.h"
+#include "whiteboard.h"
+
+namespace fs = std::filesystem;
+
+namespace {
+ const std::string testConfigFilename{"./test.conf"};
+ const std::string testDbFilename{"./whiteboard.db3"};
+}
+
+class WhiteboardTest: public ::testing::Test
+{
+protected:
+ WhiteboardTest(){
+ }
+
+ ~WhiteboardTest() override{
+ }
+
+ void SetUp() override
+ {
+ File::setFile(testConfigFilename, R"CONFIG(
+<config>
+ <datapath>.</datapath>
+ <maxage>2592000</maxage>
+</config>
+)CONFIG");
+ std::error_code ec;
+ fs::remove(testDbFilename, ec);
+
+ m_config = std::make_shared<Config>(testConfigFilename);
+ }
+
+ void TearDown() override
+ {
+ std::error_code ec;
+ fs::remove(testDbFilename, ec);
+ fs::remove(testConfigFilename, ec);
+ }
+
+ std::shared_ptr<Config> m_config;
+};
+
+TEST_F(WhiteboardTest, connection)
+{
+}
+
diff --git a/whiteboard.cpp b/whiteboard.cpp
index 3abccad..75472ba 100644
--- a/whiteboard.cpp
+++ b/whiteboard.cpp
@@ -24,8 +24,6 @@
#include <boost/algorithm/string/trim.hpp>
#include <boost/property_tree/xml_parser.hpp>
-#include <ImageMagick-6/Magick++.h>
-
#include "libreichwein/file.h"
#include "config.h"
@@ -116,7 +114,7 @@ int Whiteboard::run(int argc, char* argv[])
std::thread storage_cleanup_thread(std::bind(&Whiteboard::storage_cleanup, this));
- Magick::InitializeMagick(NULL); // for qrcode.cpp
+ QRCode::init();
int result = FCGX_Init();
if (result != 0) { // error on init
@@ -204,7 +202,7 @@ int Whiteboard::run(int argc, char* argv[])
if (url.size() > 1000)
throw std::runtime_error("URL too big");
- std::string pngdata {getQRCode(url)};
+ std::string pngdata {QRCode::getQRCode(url)};
FCGX_PutS("Content-Type: image/png\r\n", request.out);
FCGX_FPrintF(request.out, "Content-Length: %d\r\n\r\n", pngdata.size());