From e13bfa1110d3b0ef72686d5a234ce97cd139f379 Mon Sep 17 00:00:00 2001 From: Alexey Sokolov Date: Sat, 5 Oct 2013 23:30:27 +0400 Subject: [PATCH] Use gtest for EscapeTest --- .travis.yml | 2 + Makefile.in | 43 ++++++++++++++++++++- test/ConfigTest.cpp | 16 ++++++-- test/EscapeTest.cpp | 91 ++++++++++++++++++++++----------------------- test/Makefile.in | 2 +- 5 files changed, 101 insertions(+), 53 deletions(-) diff --git a/.travis.yml b/.travis.yml index a8848219..6036703e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ compiler: before_install: - sudo apt-get update -qq - sudo apt-get install -qq libperl-dev python3-dev tcl-dev libsasl2-dev + - apt-cache show swig - ( cd ~ && wget http://prdownloads.sourceforge.net/swig/swig-2.0.8.tar.gz && tar xvf swig-2.0.8.tar.gz && cd swig-2.0.8 && ./configure && make && sudo make install ) script: - ./bootstrap.sh @@ -12,6 +13,7 @@ script: - cd build - ../configure --enable-perl --enable-python --enable-tcl --enable-cyrus - make + - make test - sudo make install notifications: irc: diff --git a/Makefile.in b/Makefile.in index f1dcd0c4..a9ca9e6d 100644 --- a/Makefile.in +++ b/Makefile.in @@ -39,6 +39,9 @@ LIB_SRCS := $(addprefix src/,$(LIB_SRCS)) BIN_SRCS := src/main.cpp LIB_OBJS := $(patsubst %cpp,%o,$(LIB_SRCS)) BIN_OBJS := $(patsubst %cpp,%o,$(BIN_SRCS)) +TESTS := EscapeTest +TESTS := $(addprefix test/,$(addsuffix .o,$(TESTS))) +GTEST_VER := 1.7.0 CLEAN := znc src/*.o core core.* .version_extra .depend modules/.depend DISTCLEAN := Makefile config.log config.status znc-buildmod \ modules/Makefile man/Makefile znc.pc znc-uninstalled.pc @@ -58,7 +61,7 @@ E=@\# C= endif -.PHONY: all man modules clean distclean install version_extra_recompile +.PHONY: all man modules clean distclean install version_extra_recompile test .SECONDARY: all: znc man modules $(LIBZNC) @@ -72,11 +75,20 @@ OBJS := $(BIN_OBJS) $(LIB_OBJS) znc: $(OBJS) $(E) Linking znc... $(Q)$(CXX) $(LDFLAGS) -o $@ $(OBJS) $(LIBS) + +unittest: $(LIB_OBJS) test/gtest-all.o test/gtest-main.o $(TESTS) + $(E) Linking testznc.. + $(Q)$(CXX) $(LDFLAGS) -o $@ $(LIB_OBJS) test/gtest-all.o test/gtest-main.o $(TESTS) $(LIBS) + else znc: $(BIN_OBJS) $(LIBZNC) $(E) Linking znc... $(Q)$(CXX) $(LDFLAGS) -o $@ $(BIN_OBJS) -L. -lznc -Wl,-rpath,$(LIBZNCDIR) $(LIBS) +unittest: $(LIBZNC) test/gtest-all.o test/gtest-main.o $(TESTS) + $(E) Linking testznc... + $(Q)$(CXX) $(LDFLAGS) -o $@ $(BIN_OBJS) test/gtest-all.o test/gtest-main.o $(TESTS) -L. -lznc -Wl,-rpath,$(LIBZNCDIR) $(LIBS) + $(LIBZNC): $(LIB_OBJS) $(E) Linking $(LIBZNC)... $(Q)$(CXX) $(LDFLAGS) -shared -o $@ $(LIB_OBJS) $(LIBS) @@ -101,6 +113,32 @@ src/%.o: src/%.cpp Makefile $(E) Building core object $*... $(Q)$(CXX) $(CXXFLAGS) -c -o $@ $< -MD -MF .depend/$*.dep -MT $@ +test/%.o: test/%.cpp Makefile + @mkdir -p .depend test + $(E) Building test object $*... + $(Q)$(CXX) $(CXXFLAGS) -Itest/gtest-$(GTEST_VER)/fused-src -c -o $@ $< -MD -MF .depend/$*.dep -MT $@ + +test/gtest-all.o: test/gtest-$(GTEST_VER)/fused-src/gtest/gtest-all.cc Makefile + @mkdir -p .depend test + $(E) Building test object gtest-all... + $(Q)$(CXX) $(CXXFLAGS) -Itest/gtest-$(GTEST_VER)/fused-src -c -o $@ $< -MD -MF .depend/gtest-all.dep -MT $@ + +test/gtest-main.o: test/gtest-$(GTEST_VER)/fused-src/gtest/gtest_main.cc Makefile + @mkdir -p .depend test + $(E) Building test object gtest-main... + $(Q)$(CXX) $(CXXFLAGS) -Itest/gtest-$(GTEST_VER)/fused-src -c -o $@ $< -MD -MF .depend/gtest-main.dep -MT $@ + +test/gtest.zip: + $(E) Downloading GoogleTest $(GTEST_VER) + $(Q)wget http://googletest.googlecode.com/files/gtest-$(GTEST_VER).zip -O $@ + +test/gtest-$(GTEST_VER)/fused-src/gtest/gtest-all.cc test/gtest-$(GTEST_VER)/fused-src/gtest/gtest_main.cc: test/gtest.zip + $(E) Unpacking GoogleTest $(GTEST_VER) + $(Q)unzip $^ -d test/ + $(Q)test -r test/gtest-$(GTEST_VER)/fused-src/gtest/gtest-all.cc + $(Q)test -r test/gtest-$(GTEST_VER)/fused-src/gtest/gtest_main.cc + $(Q)touch -c test/gtest-$(GTEST_VER)/fused-src/gtest/gtest-all.cc test/gtest-$(GTEST_VER)/fused-src/gtest/gtest_main.cc + ifneq "THIS_IS_NOT_NIGHTLY" "" # If git commit was changed since previous build, add a phony target to dependencies, forcing version.o to be recompiled # Nightlies have pregenerated version.cpp @@ -160,4 +198,7 @@ uninstall: rmdir $(DESTDIR)$(PKGCONFIGDIR) @echo "Successfully uninstalled, but empty directories were left behind" +test: unittest + $(Q)./unittest + -include $(wildcard .depend/*.dep) diff --git a/test/ConfigTest.cpp b/test/ConfigTest.cpp index 48aa9567..1adf40e2 100644 --- a/test/ConfigTest.cpp +++ b/test/ConfigTest.cpp @@ -1,9 +1,17 @@ /* - * Copyright (C) 2004-2013 See the AUTHORS file for details. + * Copyright (C) 2004-2013 ZNC, see the NOTICE file for details. * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 as published - * by the Free Software Foundation. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ #include "znc/ZNCDebug.h" diff --git a/test/EscapeTest.cpp b/test/EscapeTest.cpp index 9121281f..8966a4dc 100644 --- a/test/EscapeTest.cpp +++ b/test/EscapeTest.cpp @@ -1,54 +1,51 @@ /* - * Copyright (C) 2004-2013 See the AUTHORS file for details. + * Copyright (C) 2004-2013 ZNC, see the NOTICE file for details. * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 as published - * by the Free Software Foundation. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ -#include "znc/ZNCDebug.h" +#include +#include -static int testEqual(const CString& a, const CString& b, const CString& what) -{ - if (a == b) - return 0; - std::cout << what << " failed for '" << b << "', result is '" << a << "'\n"; - return 1; +class EscapeTest : public ::testing::Test { +protected: + void testEncode(const CString& in, const CString& expectedOut, const CString& sformat, CString::EEscape format) { + CString out; + + SCOPED_TRACE("Format: " + sformat); + + // Encode, then decode again and check we still got the same string + out = in.Escape_n(CString::EASCII, format); + EXPECT_EQ(expectedOut, out); + out = out.Escape_n(format, CString::EASCII); + EXPECT_EQ(in, out); + } + + void testString(const CString& in, const CString& url, + const CString& html, const CString& sql) { + SCOPED_TRACE("String: " + in); + + testEncode(in, url, "url", CString::EURL); + testEncode(in, html, "html", CString::EHTML); + testEncode(in, sql, "sql", CString::ESQL); + } +}; + +TEST_F(EscapeTest, Test) { + // input url html sql + testString("abcdefg", "abcdefg", "abcdefg", "abcdefg"); + testString("\n\t\r", "%0A%09%0D", "\n\t\r", "\\n\\t\\r"); + testString("'\"", "%27%22", "'"", "\\'\\\""); + testString("&<>", "%26%3C%3E", "&<>", "&<>"); } -static int testString(const CString& in, const CString& url, - const CString& html, const CString& sql) { - CString out; - int errors = 0; - - // Encode, then decode again and check we still got the same string - - out = in.Escape_n(CString::EASCII, CString::EURL); - errors += testEqual(out, url, "EURL encode"); - out = out.Escape_n(CString::EURL, CString::EASCII); - errors += testEqual(out, in, "EURL decode"); - - out = in.Escape_n(CString::EASCII, CString::EHTML); - errors += testEqual(out, html, "EHTML encode"); - out = out.Escape_n(CString::EHTML, CString::EASCII); - errors += testEqual(out, in, "EHTML decode"); - - out = in.Escape_n(CString::EASCII, CString::ESQL); - errors += testEqual(out, sql, "ESQL encode"); - out = out.Escape_n(CString::ESQL, CString::EASCII); - errors += testEqual(out, in, "ESQL decode"); - - return errors; -} - -int main() { - unsigned int failed = 0; - - // input url html sql - failed += testString("abcdefg", "abcdefg", "abcdefg", "abcdefg"); - failed += testString("\n\t\r", "%0A%09%0D", "\n\t\r", "\\n\\t\\r"); - failed += testString("'\"", "%27%22", "'"", "\\'\\\""); - failed += testString("&<>", "%26%3C%3E", "&<>", "&<>"); - - return failed; -} diff --git a/test/Makefile.in b/test/Makefile.in index 7397fce1..aba3c9e2 100644 --- a/test/Makefile.in +++ b/test/Makefile.in @@ -9,7 +9,7 @@ CXXFLAGS := -I$(srcdir)/../include -I../include @CPPFLAGS@ @CXXFLAGS@ LDFLAGS := @LDFLAGS@ LIBS := @LIBS@ -TARGETS := ConfigTest EscapeTest +TARGETS := ConfigTest OBJS := $(addsuffix .o, $(TARGETS)) ZNC_OBJS := Config.o ZNCDebug.o FileUtils.o Utils.o ZNCString.o MD5.o SHA256.o ZNC_OBJS := $(addprefix ../src/, $(ZNC_OBJS))