From 5102d3e5691dd36fd72c2678e6604a4535190500 Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Mon, 15 Jun 2020 12:58:54 -0700 Subject: [PATCH 1/9] swss: Add support to swss for gearbox phys * builds on support for multiple switches in sonic-sairedis * adds gearsync daemon * parsers for gearbox-related configuration files, plus unit tests * gearboxutils for determining if gearbox supported and other functions * changes to portsorch and orchagent to support gearbox creation and attributes HLD is located at https://github.com/Azure/SONiC/blob/b817a12fd89520d3fd26bbc5897487928e7f6de7/doc/gearbox/gearbox_mgr_design.md Signed-off-by: syd.logan@broadcom.com --- Makefile.am | 3 +- cfgmgr/Makefile.am | 2 +- configure.ac | 2 + gearsyncd/Makefile.am | 15 + gearsyncd/gearboxparser.cpp | 248 ++ gearsyncd/gearboxparser.h | 29 + gearsyncd/gearparserbase.cpp | 67 + gearsyncd/gearparserbase.h | 62 + gearsyncd/gearsyncd.cpp | 164 + gearsyncd/phyparser.cpp | 398 +++ gearsyncd/phyparser.h | 32 + gearsyncd/tests/Makefile.am | 21 + ...rbox_config_empty_ethernet_line_lanes.json | 49 + ...ox_config_empty_ethernet_system_lanes.json | 49 + .../gearbox_config_invalid_array.json | 49 + .../negative/gearbox_config_invalid_json.json | 49 + ...earbox_config_invalid_phy_config_file.json | 49 + ...gearbox_config_missing_ethernet_index.json | 47 + ...nfig_missing_ethernet_line_lane_speed.json | 49 + ...ox_config_missing_ethernet_line_lanes.json | 48 + ...earbox_config_missing_ethernet_phy_id.json | 48 + ...ig_missing_ethernet_system_lane_speed.json | 49 + ..._config_missing_ethernet_system_lanes.json | 48 + .../gearbox_config_missing_interfaces.json | 26 + .../gearbox_config_missing_phy_address.json | 44 + .../gearbox_config_missing_phy_bus_id.json | 44 + ...earbox_config_missing_phy_config_file.json | 44 + ...rbox_config_missing_phy_firmware_path.json | 44 + .../gearbox_config_missing_phy_id.json | 45 + .../gearbox_config_missing_phy_lib_name.json | 44 + .../gearbox_config_missing_phy_name.json | 44 + ...gearbox_config_missing_phy_phy_access.json | 44 + ...nfig_missing_phy_sai_init_config_file.json | 45 + .../negative/gearbox_config_missing_phys.json | 23 + .../negative/phy_config_invalid_array.json | 146 + .../negative/phy_config_invalid_boolean.json | 180 + .../negative/phy_config_invalid_boolean2.json | 181 + .../negative/phy_config_invalid_json.json | 180 + .../negative/phy_config_lanes_missing_id.json | 179 + ..._config_lanes_missing_line_rx_lanemap.json | 179 + ..._lanes_missing_line_to_system_lanemap.json | 179 + ..._config_lanes_missing_line_tx_lanemap.json | 179 + ...hy_config_lanes_missing_local_lane_id.json | 179 + .../phy_config_lanes_missing_mdio_addr.json | 179 + .../phy_config_lanes_missing_rx_polarity.json | 179 + .../phy_config_lanes_missing_system_side.json | 179 + .../phy_config_lanes_missing_tx_polarity.json | 179 + .../negative/phy_config_missing_address.json | 180 + .../negative/phy_config_missing_lanes.json | 46 + .../negative/phy_config_missing_mode.json | 145 + .../negative/phy_config_missing_name.json | 145 + .../phy_config_missing_port_ref_clk.json | 145 + .../phy_config_ports_empty_line_lanes.json | 146 + .../phy_config_ports_empty_system_lanes.json | 146 + .../negative/phy_config_ports_missing_id.json | 179 + ...g_ports_missing_line_adver_asym_pause.json | 179 + ...fig_ports_missing_line_adver_auto_neg.json | 179 + ...y_config_ports_missing_line_adver_fec.json | 179 + ...g_ports_missing_line_adver_media_type.json | 179 + ...config_ports_missing_line_adver_speed.json | 179 + ...hy_config_ports_missing_line_auto_neg.json | 179 + .../phy_config_ports_missing_line_fec.json | 179 + ...y_config_ports_missing_line_intf_type.json | 179 + ..._config_ports_missing_line_lane_speed.json | 179 + .../phy_config_ports_missing_line_lanes.json | 145 + ...hy_config_ports_missing_line_loopback.json | 179 + ..._config_ports_missing_line_media_type.json | 179 + .../phy_config_ports_missing_line_speed.json | 179 + ...hy_config_ports_missing_line_training.json | 179 + .../phy_config_ports_missing_mdio_addr.json | 179 + ..._config_ports_missing_system_auto_neg.json | 179 + .../phy_config_ports_missing_system_fec.json | 179 + ...onfig_ports_missing_system_lane_speed.json | 179 + ...phy_config_ports_missing_system_lanes.json | 145 + ..._config_ports_missing_system_loopback.json | 179 + ...phy_config_ports_missing_system_speed.json | 178 + ..._config_ports_missing_system_training.json | 179 + .../configs/positive/gearbox_config_1.json | 49 + .../tests/configs/positive/phy1_config_1.json | 180 + .../tests/configs/positive/phy2_config_1.json | 115 + gearsyncd/tests/cunit/Automated.c | 623 ++++ gearsyncd/tests/cunit/Automated.cpp | 623 ++++ gearsyncd/tests/cunit/Automated.h | 90 + gearsyncd/tests/cunit/Basic.c | 334 ++ gearsyncd/tests/cunit/Basic.cpp | 334 ++ gearsyncd/tests/cunit/Basic.h | 113 + gearsyncd/tests/cunit/CUError.c | 231 ++ gearsyncd/tests/cunit/CUError.cpp | 231 ++ gearsyncd/tests/cunit/CUError.h | 199 ++ gearsyncd/tests/cunit/CUnit.h | 383 +++ gearsyncd/tests/cunit/CUnit_intl.h | 62 + gearsyncd/tests/cunit/Console.c | 735 +++++ gearsyncd/tests/cunit/Console.cpp | 735 +++++ gearsyncd/tests/cunit/Console.h | 60 + gearsyncd/tests/cunit/MyMem.c | 585 ++++ gearsyncd/tests/cunit/MyMem.cpp | 585 ++++ gearsyncd/tests/cunit/MyMem.h | 104 + gearsyncd/tests/cunit/TestDB.c | 2773 ++++++++++++++++ gearsyncd/tests/cunit/TestDB.cpp | 2773 ++++++++++++++++ gearsyncd/tests/cunit/TestDB.h | 914 ++++++ gearsyncd/tests/cunit/TestRun.c | 2923 +++++++++++++++++ gearsyncd/tests/cunit/TestRun.cpp | 2913 ++++++++++++++++ gearsyncd/tests/cunit/TestRun.h | 444 +++ gearsyncd/tests/cunit/Util.c | 609 ++++ gearsyncd/tests/cunit/Util.cpp | 609 ++++ gearsyncd/tests/cunit/Util.h | 158 + gearsyncd/tests/testgearparser.cpp | 150 + gearsyncd/tests/testphyparser.cpp | 193 ++ lib/gearboxutils.cpp | 459 +++ lib/gearboxutils.h | 112 + orchagent/Makefile.am | 3 +- orchagent/main.cpp | 57 +- orchagent/portsorch.cpp | 573 +++- orchagent/portsorch.h | 36 +- orchagent/saihelper.cpp | 113 +- orchagent/saihelper.h | 5 +- portsyncd/Makefile.am | 4 +- tests/mock_tests/Makefile.am | 3 +- 118 files changed, 31432 insertions(+), 86 deletions(-) create mode 100644 gearsyncd/Makefile.am create mode 100644 gearsyncd/gearboxparser.cpp create mode 100644 gearsyncd/gearboxparser.h create mode 100644 gearsyncd/gearparserbase.cpp create mode 100644 gearsyncd/gearparserbase.h create mode 100644 gearsyncd/gearsyncd.cpp create mode 100644 gearsyncd/phyparser.cpp create mode 100644 gearsyncd/phyparser.h create mode 100644 gearsyncd/tests/Makefile.am create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_line_lanes.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_system_lanes.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_invalid_array.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_invalid_json.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_invalid_phy_config_file.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_index.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lane_speed.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lanes.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_phy_id.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lane_speed.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lanes.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_interfaces.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_address.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_bus_id.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_config_file.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_firmware_path.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_id.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_lib_name.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_name.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_phy_access.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phy_sai_init_config_file.json create mode 100644 gearsyncd/tests/configs/negative/gearbox_config_missing_phys.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_invalid_array.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_invalid_boolean.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_invalid_boolean2.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_invalid_json.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_id.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_rx_lanemap.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_to_system_lanemap.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_tx_lanemap.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_local_lane_id.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_mdio_addr.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_rx_polarity.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_system_side.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_lanes_missing_tx_polarity.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_missing_address.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_missing_lanes.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_missing_mode.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_missing_name.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_missing_port_ref_clk.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_empty_line_lanes.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_empty_system_lanes.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_id.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_asym_pause.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_auto_neg.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_fec.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_media_type.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_speed.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_auto_neg.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_fec.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_intf_type.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lane_speed.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lanes.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_loopback.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_media_type.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_speed.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_line_training.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_mdio_addr.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_system_auto_neg.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_system_fec.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lane_speed.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lanes.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_system_loopback.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_system_speed.json create mode 100644 gearsyncd/tests/configs/negative/phy_config_ports_missing_system_training.json create mode 100644 gearsyncd/tests/configs/positive/gearbox_config_1.json create mode 100644 gearsyncd/tests/configs/positive/phy1_config_1.json create mode 100644 gearsyncd/tests/configs/positive/phy2_config_1.json create mode 100644 gearsyncd/tests/cunit/Automated.c create mode 100644 gearsyncd/tests/cunit/Automated.cpp create mode 100644 gearsyncd/tests/cunit/Automated.h create mode 100644 gearsyncd/tests/cunit/Basic.c create mode 100644 gearsyncd/tests/cunit/Basic.cpp create mode 100644 gearsyncd/tests/cunit/Basic.h create mode 100644 gearsyncd/tests/cunit/CUError.c create mode 100644 gearsyncd/tests/cunit/CUError.cpp create mode 100644 gearsyncd/tests/cunit/CUError.h create mode 100644 gearsyncd/tests/cunit/CUnit.h create mode 100644 gearsyncd/tests/cunit/CUnit_intl.h create mode 100644 gearsyncd/tests/cunit/Console.c create mode 100644 gearsyncd/tests/cunit/Console.cpp create mode 100644 gearsyncd/tests/cunit/Console.h create mode 100644 gearsyncd/tests/cunit/MyMem.c create mode 100644 gearsyncd/tests/cunit/MyMem.cpp create mode 100644 gearsyncd/tests/cunit/MyMem.h create mode 100644 gearsyncd/tests/cunit/TestDB.c create mode 100644 gearsyncd/tests/cunit/TestDB.cpp create mode 100644 gearsyncd/tests/cunit/TestDB.h create mode 100644 gearsyncd/tests/cunit/TestRun.c create mode 100644 gearsyncd/tests/cunit/TestRun.cpp create mode 100644 gearsyncd/tests/cunit/TestRun.h create mode 100644 gearsyncd/tests/cunit/Util.c create mode 100644 gearsyncd/tests/cunit/Util.cpp create mode 100644 gearsyncd/tests/cunit/Util.h create mode 100644 gearsyncd/tests/testgearparser.cpp create mode 100644 gearsyncd/tests/testphyparser.cpp create mode 100644 lib/gearboxutils.cpp create mode 100644 lib/gearboxutils.h diff --git a/Makefile.am b/Makefile.am index b2e8154888..6fa155d678 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,4 @@ - -SUBDIRS = fpmsyncd neighsyncd portsyncd mclagsyncd natsyncd orchagent swssconfig cfgmgr tests +SUBDIRS = fpmsyncd neighsyncd portsyncd mclagsyncd natsyncd orchagent swssconfig cfgmgr tests gearsyncd gearsyncd/tests if HAVE_LIBTEAM SUBDIRS += teamsyncd diff --git a/cfgmgr/Makefile.am b/cfgmgr/Makefile.am index 7178cad1c6..001c6f1f99 100644 --- a/cfgmgr/Makefile.am +++ b/cfgmgr/Makefile.am @@ -1,4 +1,4 @@ -INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/orchagent -I $(top_srcdir)/warmrestart -I $(top_srcdir)/orchagent/flex_counter +INCLUDES = -I$(top_srcdir)/lib -I $(top_srcdir) -I $(top_srcdir)/orchagent -I $(top_srcdir)/warmrestart -I $(top_srcdir)/orchagent/flex_counter CFLAGS_SAI = -I /usr/include/sai LIBNL_CFLAGS = -I/usr/include/libnl3 LIBNL_LIBS = -lnl-genl-3 -lnl-route-3 -lnl-3 diff --git a/configure.ac b/configure.ac index 2d5c1fc106..81e2752457 100644 --- a/configure.ac +++ b/configure.ac @@ -89,6 +89,8 @@ AC_CONFIG_FILES([ orchagent/Makefile fpmsyncd/Makefile neighsyncd/Makefile + gearsyncd/Makefile + gearsyncd/tests/Makefile natsyncd/Makefile portsyncd/Makefile teamsyncd/Makefile diff --git a/gearsyncd/Makefile.am b/gearsyncd/Makefile.am new file mode 100644 index 0000000000..34bffb7633 --- /dev/null +++ b/gearsyncd/Makefile.am @@ -0,0 +1,15 @@ +INCLUDES = -I $(top_srcdir)/lib -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I $(top_srcdir)/cfgmgr + +bin_PROGRAMS = gearsyncd + +if DEBUG +DBGFLAGS = -ggdb -DDEBUG +else +DBGFLAGS = -g +endif + +gearsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp gearsyncd.cpp gearparserbase.cpp gearboxparser.cpp phyparser.cpp $(top_srcdir)/cfgmgr/shellcmd.h + +gearsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(ASAN_CFLAGS) + +gearsyncd_LDADD = -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS) $(ASAN_LDFLAGS) diff --git a/gearsyncd/gearboxparser.cpp b/gearsyncd/gearboxparser.cpp new file mode 100644 index 0000000000..80809fb27d --- /dev/null +++ b/gearsyncd/gearboxparser.cpp @@ -0,0 +1,248 @@ +/* + * Copyright 2019-2020 Broadcom Inc. + * + * 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 "gearboxparser.h" +#include "phyparser.h" +#include + +void +GearboxParser::notifyGearboxConfigDone(bool success) +{ + swss::ProducerStateTable *p = getProducerStateTable().get(); + + swss::FieldValueTuple finish_notice("success", std::to_string(success)); + std::vector attrs = { finish_notice }; + + p->set("GearboxConfigDone", attrs); +} + +bool +GearboxParser::parse() +{ + json root; + + try { + root = getJSONRoot(); + } catch (const std::exception& e) { + SWSS_LOG_ERROR("JSON root not parseable"); + return false; + } + + json phys, phy, interfaces, interface, val, lanes; + + std::vector attrs; + + try { + phys = root["phys"]; + if (phys.size() == 0) { + SWSS_LOG_ERROR("zero-sized 'phys' field in gearbox configuration"); + return false; + } + } catch (const std::exception& e) { + SWSS_LOG_ERROR("unable to read gearbox configuration (invalid format)"); + return false; + } + + if (true) { + for (uint32_t iter = 0; iter < phys.size(); iter++) { + phy = phys[iter]; + try { + attrs.clear(); + swss::FieldValueTuple attr; + if (phy.find("phy_id") == phy.end()) { + SWSS_LOG_ERROR("missing 'phy_id' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["phy_id"]; + int phyId = val.get(); + attr = std::make_pair("phy_id", std::to_string(phyId)); + attrs.push_back(attr); + if (phy.find("name") == phy.end()) { + SWSS_LOG_ERROR("missing 'name' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["name"]; + std::string name(val.get()); + attr = std::make_pair("name", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("address") == phy.end()) { + SWSS_LOG_ERROR("missing 'address' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["address"]; + attr = std::make_pair("address", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("lib_name") == phy.end()) { + SWSS_LOG_ERROR("missing 'lib_name' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["lib_name"]; + attr = std::make_pair("lib_name", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("firmware_path") == phy.end()) { + SWSS_LOG_ERROR("missing 'firmware_path' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["firmware_path"]; + attr = std::make_pair("firmware_path", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("config_file") == phy.end()) { + SWSS_LOG_ERROR("missing 'config_file' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["config_file"]; + std::string cfgFile(val.get()); + attr = std::make_pair("config_file", cfgFile); + attrs.push_back(attr); + if (phy.find("sai_init_config_file") == phy.end()) { + SWSS_LOG_ERROR("missing 'sai_init_config_file' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["sai_init_config_file"]; + std::string bcmCfgFile(std::string(val.get())); + attr = std::make_pair("sai_init_config_file", bcmCfgFile); + attrs.push_back(attr); + if (phy.find("phy_access") == phy.end()) { + SWSS_LOG_ERROR("missing 'phy_access' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["phy_access"]; + attr = std::make_pair("phy_access", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("bus_id") == phy.end()) { + SWSS_LOG_ERROR("missing 'bus_id' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["bus_id"]; + attr = std::make_pair("bus_id", std::to_string(val.get())); + attrs.push_back(attr); + std::string key; + key = "phy:" + std::to_string(phyId); + if (getWriteToDb() == true) { + writeToDb(key, attrs); + } + PhyParser p; + p.setPhyId(phyId); + p.setWriteToDb(getWriteToDb()); + p.setConfigPath(cfgFile); + if (p.parse() == false) { + SWSS_LOG_ERROR("phy parser failed to parse item %d in gearbox configuration", iter); + return false; + } + } catch (const std::exception& e) { + SWSS_LOG_ERROR("unable to read 'phys' item %d in gearbox configuration (invalid format)", iter); + return false; + } + } + } else { + SWSS_LOG_ERROR("missing 'phys' field in gearbox configuration"); + return false; + } + + if (root.find("interfaces") != root.end()) { + interfaces = root["interfaces"]; // vec + if (interfaces.size() == 0) { + SWSS_LOG_ERROR("zero-sized 'interfaces' field in gearbox configuration"); + return false; + } + for (uint32_t iter = 0; iter < interfaces.size(); iter++) { + attrs.clear(); + interface = interfaces[iter]; + try { + swss::FieldValueTuple attr; + + if (interface.find("name") == interface.end()) { + SWSS_LOG_ERROR("missing 'name' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + val = interface["name"]; + attr = std::make_pair("name", std::string(val.get())); + attrs.push_back(attr); + + if (interface.find("index") == interface.end()) { + SWSS_LOG_ERROR("missing 'index' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + val = interface["index"]; + int index = val.get(); + attr = std::make_pair("index", std::to_string(index)); + attrs.push_back(attr); + + if (interface.find("phy_id") == interface.end()) { + SWSS_LOG_ERROR("missing 'phy_id' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + val = interface["phy_id"]; + attr = std::make_pair("phy_id", std::to_string(val.get())); + attrs.push_back(attr); + + if (interface.find("system_lanes") != interface.end()) { + lanes = interface["system_lanes"]; // vec + std::string laneStr(""); + if (lanes.size() == 0) { + SWSS_LOG_ERROR("zero-sized 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) { + val = lanes[iter2]; + if (laneStr.length() > 0) { + laneStr += ","; + } + laneStr += std::to_string(val.get()); + } + attr = std::make_pair("system_lanes", laneStr); + attrs.push_back(attr); + } else { + SWSS_LOG_ERROR("missing 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + + if (interface.find("line_lanes") != interface.end()) { + lanes = interface["line_lanes"]; // vec + std::string laneStr(""); + if (lanes.size() == 0) { + SWSS_LOG_ERROR("zero-sized 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) { + val = lanes[iter2]; + if (laneStr.length() > 0) { + laneStr += ","; + } + laneStr += std::to_string(val.get()); + } + attr = std::make_pair("line_lanes", laneStr); + attrs.push_back(attr); + } else { + SWSS_LOG_ERROR("missing 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + std::string key; + key = "interface:" + std::to_string(index); + if (getWriteToDb() == true) { + writeToDb(key, attrs); + } + } catch (const std::exception& e) { + SWSS_LOG_ERROR("unable to read 'interfaces' item %d in gearbox configuration (invalid format)", iter); + return false; + } + } + } else { + SWSS_LOG_ERROR("unable to read 'interfaces' item in gearbox configuration"); + return false; + } + return true; +} diff --git a/gearsyncd/gearboxparser.h b/gearsyncd/gearboxparser.h new file mode 100644 index 0000000000..9620d7bfeb --- /dev/null +++ b/gearsyncd/gearboxparser.h @@ -0,0 +1,29 @@ +/* + * Copyright 2019 Broadcom Inc. + * + * 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. + */ + +#if !defined(__GEARBOX_PARSER_H__) +#define __GEARBOX_PARSER_H__ + +#include "gearparserbase.h" + +class GearboxParser: public GearParserBase +{ +public: + bool parse(); + void notifyGearboxConfigDone(bool success); +}; + +#endif /* __GEARBOX_PARSER_H__ */ diff --git a/gearsyncd/gearparserbase.cpp b/gearsyncd/gearparserbase.cpp new file mode 100644 index 0000000000..3f1c949b2f --- /dev/null +++ b/gearsyncd/gearparserbase.cpp @@ -0,0 +1,67 @@ +/* + * Copyright 2019 Broadcom Inc. + * + * 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 "gearparserbase.h" +#include +#include +#include + +void +GearParserBase::init() { + m_writeToDb = false; + m_rootInit = false; + m_applDb = std::unique_ptr{new swss::DBConnector(APPL_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, 0)}; + m_producerStateTable = std::unique_ptr{new swss::ProducerStateTable(m_applDb.get(), APP_GEARBOX_TABLE_NAME)}; +} + +GearParserBase::GearParserBase() { + init(); +} + +GearParserBase::~GearParserBase() { +} + +json & +GearParserBase::getJSONRoot() +{ + // lazy instantiate + + if (m_rootInit == false) { + + std::ifstream infile(getConfigPath()); + if (infile.is_open()) + { + std::string jsonBuffer; + std::string line; + + while (getline(infile, line)) { + jsonBuffer += line; + } + infile.close(); + + m_root = json::parse(jsonBuffer.c_str()); + m_rootInit = true; + } + } + return m_root; +} + +bool +GearParserBase::writeToDb(std::string &key, std::vector &attrs) +{ + m_producerStateTable.get()->set(key.c_str(), attrs); + return true; +} diff --git a/gearsyncd/gearparserbase.h b/gearsyncd/gearparserbase.h new file mode 100644 index 0000000000..4454c0ba83 --- /dev/null +++ b/gearsyncd/gearparserbase.h @@ -0,0 +1,62 @@ +/* + * Copyright 2019 Broadcom Inc. + * + * 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. + */ + +#if !defined(__GEAR_PARSER_BASE_H__) +#define __GEAR_PARSER_BASE_H__ + +#include "dbconnector.h" +#include "producerstatetable.h" +#include +#include +#include + + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wshadow" +#include "swss/json.hpp" +#pragma GCC diagnostic pop + +using json = nlohmann::json; + +class GearParserBase +{ +public: + GearParserBase(); + virtual ~GearParserBase(); + virtual bool parse() = 0; + void setWriteToDb(bool val) {m_writeToDb = val;} + bool getWriteToDb() {return m_writeToDb;} + void setConfigPath(std::string &path) {m_cfgPath = path;} + const std::string getConfigPath() {return m_cfgPath;} + std::unique_ptr &getProducerStateTable() {return m_producerStateTable;} + +protected: + bool writeToDb(std::string &key, std::vector &attrs); + json &getJSONRoot(); + +private: + void init(); + std::unique_ptr m_cfgDb; + std::unique_ptr m_applDb; + std::unique_ptr m_stateDb; + std::unique_ptr m_producerStateTable; + std::string m_cfgPath; + bool m_writeToDb; + json m_root; + bool m_rootInit; +}; + +#endif // __GEAR_PARSER_BASE_H__ diff --git a/gearsyncd/gearsyncd.cpp b/gearsyncd/gearsyncd.cpp new file mode 100644 index 0000000000..f4082bb0be --- /dev/null +++ b/gearsyncd/gearsyncd.cpp @@ -0,0 +1,164 @@ +/* + * Copyright 2019 Broadcom Inc. + * + * 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 +#include +#include +#include +#include "dbconnector.h" +#include "producerstatetable.h" +#include "warm_restart.h" +#include "gearboxparser.h" +#include "gearboxutils.h" + +#include + +using namespace std; +using namespace swss; + +void usage() +{ + cout << "Usage: gearsyncd [-p gear_config.json]" << endl; + cout << " -p gearbox_config.json: import gearbox config" << endl; + cout << " use configDB data if not specified" << endl; +} + +bool handleGearboxConfigFile(string file, bool warm); +bool handleGearboxConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm); + +static void notifyGearboxConfigDone(ProducerStateTable &p, bool success) +{ + /* Notify that gearbox config successfully written */ + + FieldValueTuple finish_notice("success", to_string(success)); + std::vector attrs = { finish_notice }; + + p.set("GearboxConfigDone", attrs); +} + +int main(int argc, char **argv) +{ + Logger::linkToDbNative("gearsyncd"); + int opt; + string gearbox_config_file; + map gearbox_cfg_map; + GearboxUtils utils; + + while ((opt = getopt(argc, argv, "p:ht")) != -1 ) + { + switch (opt) + { + case 'p': + gearbox_config_file.assign(optarg); + break; + case 'h': + usage(); + return 1; + default: /* '?' */ + usage(); + return EXIT_FAILURE; + } + } + + DBConnector cfgDb(CONFIG_DB, DBConnector::DEFAULT_UNIXSOCKET, 0); + DBConnector applDb(APPL_DB, DBConnector::DEFAULT_UNIXSOCKET, 0); + ProducerStateTable producerStateTable(&applDb, APP_GEARBOX_TABLE_NAME); + + WarmStart::initialize("gearsyncd", "swss"); + WarmStart::checkWarmStart("gearsyncd", "swss"); + const bool warm = WarmStart::isWarmStart(); + + try + { + if (utils.platformHasGearbox() == false) + { + // no gearbox, move on + + notifyGearboxConfigDone(producerStateTable, true); + } + else if (!handleGearboxConfigFromConfigDB(producerStateTable, cfgDb, warm)) + { + // if gearbox config is missing in ConfigDB + // attempt to use gearbox_config.json + if (!gearbox_config_file.empty()) + { + handleGearboxConfigFile(gearbox_config_file, warm); + } + } + + while (true) + { + // placeholder, at some point we may select on cfgDb. + + sleep(1000); + } + } + catch (const std::exception& e) + { + cerr << "Exception \"" << e.what() << "\" had been thrown in deamon" << endl; + return EXIT_FAILURE; + } + return 1; +} + +bool handleGearboxConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool warm) +{ + cout << "Get gearbox configuration from ConfigDB..." << endl; + + Table table(&cfgDb, CFG_GEARBOX_TABLE_NAME); + std::vector ovalues; + std::vector keys; + table.getKeys(keys); + + if (keys.empty()) + { + cout << "No gearbox configuration in ConfigDB" << endl; + return false; + } + + for ( auto &k : keys ) + { + table.get(k, ovalues); + vector attrs; + for ( auto &v : ovalues ) + { + FieldValueTuple attr(v.first, v.second); + attrs.push_back(attr); + } + if (!warm) + { + p.set(k, attrs); + } + } + if (!warm) + { + notifyGearboxConfigDone(p, true); + } + + return true; +} + +bool handleGearboxConfigFile(string file, bool warm) +{ + GearboxParser parser; + bool ret; + + parser.setWriteToDb(true); + parser.setConfigPath(file); + ret = parser.parse(); + parser.notifyGearboxConfigDone(ret); // if (!warm....) + return ret; +} diff --git a/gearsyncd/phyparser.cpp b/gearsyncd/phyparser.cpp new file mode 100644 index 0000000000..a6db79d4c2 --- /dev/null +++ b/gearsyncd/phyparser.cpp @@ -0,0 +1,398 @@ +/* + * Copyright 2019-2020 Broadcom Inc. + * + * 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 "phyparser.h" + +bool +PhyParser::parse() +{ + json root; + + try { + root = getJSONRoot(); + } catch (const std::exception& e) { + SWSS_LOG_ERROR("JSON root not parseable"); + return false; + } + + if (false) { + return false; + } + + std::vector attrs; + swss::FieldValueTuple attr; + json val, vals, ports, port, lanes, lane; + std::string valsStr; + + try { + lanes = root["lanes"]; + if (lanes.size() == 0) { + SWSS_LOG_ERROR("zero-sized 'lanes' field in phy configuration"); + return false; + } + } catch (const std::exception& e) { + SWSS_LOG_ERROR("unable to read phy configuration (invalid format)"); + return false; + } + for (uint32_t iter = 0; iter < lanes.size(); iter++) { + lane = lanes[iter]; + try { + attrs.clear(); + + /* index */ + if (lane.find("index") == lane.end()) { + SWSS_LOG_ERROR("missing 'index' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["index"]; + int id = val.get(); + attr = std::make_pair("index", std::to_string(id)); + attrs.push_back(attr); + + /* system_side */ + if (lane.find("system_side") == lane.end()) { + SWSS_LOG_ERROR("missing 'system_side' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["system_side"]; + if (val.get() != false && val.get() != true) { + SWSS_LOG_ERROR("not a boolean: 'system_side' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + std::string systemSideStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("system_side", systemSideStr); + attrs.push_back(attr); + + /* local_lane_id */ + if (lane.find("local_lane_id") == lane.end()) { + SWSS_LOG_ERROR("missing 'local_lane_id' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["local_lane_id"]; + attr = std::make_pair("local_lane_id", std::to_string(val.get())); + attrs.push_back(attr); + + /* tx_polarity */ + if (lane.find("tx_polarity") == lane.end()) { + SWSS_LOG_ERROR("missing 'tx_polarity' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["tx_polarity"]; + attr = std::make_pair("tx_polarity", std::to_string(val.get())); + attrs.push_back(attr); + + /* rx_polarity */ + if (lane.find("rx_polarity") == lane.end()) { + SWSS_LOG_ERROR("missing 'rx_polarity' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["rx_polarity"]; + attr = std::make_pair("rx_polarity", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_tx_lanemap */ + if (lane.find("line_tx_lanemap") == lane.end()) { + SWSS_LOG_ERROR("missing 'line_tx_lanemap' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["line_tx_lanemap"]; + attr = std::make_pair("line_tx_lanemap", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_rx_lanemap */ + if (lane.find("line_rx_lanemap") == lane.end()) { + SWSS_LOG_ERROR("missing 'line_rx_lanemap' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["line_rx_lanemap"]; + attr = std::make_pair("line_rx_lanemap", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_to_system_lanemap */ + if (lane.find("line_to_system_lanemap") == lane.end()) { + SWSS_LOG_ERROR("missing 'line_to_system_lanemap' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["line_to_system_lanemap"]; + attr = std::make_pair("line_to_system_lanemap", std::to_string(val.get())); + attrs.push_back(attr); + + /* mdio_addr */ + if (lane.find("mdio_addr") == lane.end()) { + SWSS_LOG_ERROR("missing 'mdio_addr' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["mdio_addr"]; + attr = std::make_pair("mdio_addr", val.get()); + attrs.push_back(attr); + + std::string key; + key = "phy:" + std::to_string(getPhyId()) + ":lanes:" + std::to_string(id); + if (getWriteToDb() == true) { + writeToDb(key, attrs); + } + } catch (const std::exception& e) { + SWSS_LOG_ERROR("unable to read lanes configuration item %d (invalid format)", iter); + return false; + } + } + if (root.find("ports") != root.end()) { + ports = root["ports"]; + if (ports.size() == 0) { + SWSS_LOG_ERROR("zero-sized 'ports' field in phy configuration"); + return false; + } + for (uint32_t iter = 0; iter < ports.size(); iter++) { + port = ports[iter]; + try { + attrs.clear(); + swss::FieldValueTuple attr; + + /* index */ + if (port.find("index") == port.end()) { + SWSS_LOG_ERROR("missing 'index' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["index"]; + int id = val.get(); + attr = std::make_pair("index", std::to_string(id)); + attrs.push_back(attr); + + /* mdio_addr */ + if (port.find("mdio_addr") == port.end()) { + SWSS_LOG_ERROR("missing 'mdio_addr' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["mdio_addr"]; + attr = std::make_pair("mdio_addr", val.get()); + attrs.push_back(attr); + + /* system_speed */ + if (port.find("system_speed") == port.end()) { + SWSS_LOG_ERROR("missing 'system_speed' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_speed"]; + attr = std::make_pair("system_speed", std::to_string(val.get())); + attrs.push_back(attr); + + /* system_fec */ + if (port.find("system_fec") == port.end()) { + SWSS_LOG_ERROR("missing 'system_fec' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_fec"]; + attr = std::make_pair("system_fec", val.get()); + attrs.push_back(attr); + + /* system_auto_neg */ + if (port.find("system_auto_neg") == port.end()) { + SWSS_LOG_ERROR("missing 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_auto_neg"]; + if (val.get() != false && val.get() != true) { + SWSS_LOG_ERROR("not a boolean: 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string systemAutoNegStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("system_auto_neg", systemAutoNegStr); + attrs.push_back(attr); + + /* system_loopback */ + if (port.find("system_loopback") == port.end()) { + SWSS_LOG_ERROR("missing 'system_loopback' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_loopback"]; + attr = std::make_pair("system_loopback", val.get()); + attrs.push_back(attr); + + /* system_training */ + if (port.find("system_training") == port.end()) { + SWSS_LOG_ERROR("missing 'system_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_training"]; + if (val.get() != false && val.get() != true) { + SWSS_LOG_ERROR("not a boolean: 'system_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string systemTrainingStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("system_training", systemTrainingStr); + attrs.push_back(attr); + + /* line_speed */ + if (port.find("line_speed") == port.end()) { + SWSS_LOG_ERROR("missing 'line_speed' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_speed"]; + attr = std::make_pair("line_speed", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_fec */ + if (port.find("line_fec") == port.end()) { + SWSS_LOG_ERROR("missing 'line_fec' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_fec"]; + attr = std::make_pair("line_fec", val.get()); + attrs.push_back(attr); + + /* line_auto_neg */ + if (port.find("line_auto_neg") == port.end()) { + SWSS_LOG_ERROR("missing 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_auto_neg"]; + if (val.get() != false && val.get() != true) { + SWSS_LOG_ERROR("not a boolean: 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineAutoNegStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_auto_neg", lineAutoNegStr); + attrs.push_back(attr); + + /* line_media_type */ + if (port.find("line_media_type") == port.end()) { + SWSS_LOG_ERROR("missing 'line_media_type' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_media_type"]; + attr = std::make_pair("line_media_type", val.get()); + attrs.push_back(attr); + + /* line_intf_type */ + if (port.find("line_intf_type") == port.end()) { + SWSS_LOG_ERROR("missing 'line_intf_type' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_intf_type"]; + attr = std::make_pair("line_intf_type", val.get()); + attrs.push_back(attr); + + /* line_loopback */ + if (port.find("line_loopback") == port.end()) { + SWSS_LOG_ERROR("missing 'line_loopback' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_loopback"]; + attr = std::make_pair("line_loopback", val.get()); + attrs.push_back(attr); + + /* line_training */ + if (port.find("line_training") == port.end()) { + SWSS_LOG_ERROR("missing 'line_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_training"]; + if (val.get() != false && val.get() != true) { + SWSS_LOG_ERROR("not a boolean: 'line_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineTrainingStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_training", lineTrainingStr); + attrs.push_back(attr); + + /* line_adver_speed */ + if (port.find("line_adver_speed") == port.end()) { + SWSS_LOG_ERROR("missing 'line_adver_speed' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + vals = port["line_adver_speed"]; // vec + valsStr = ""; + for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) { + val = vals[iter1]; + if (valsStr.length() > 0) { + valsStr += ","; + } + valsStr += std::to_string(val.get()); + } + attr = std::make_pair("line_adver_speed", valsStr); + attrs.push_back(attr); + + /* line_adver_fec */ + if (port.find("line_adver_fec") == port.end()) { + SWSS_LOG_ERROR("missing 'line_adver_fec' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + vals = port["line_adver_fec"]; // vec + valsStr = ""; + for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) { + val = vals[iter1]; + if (valsStr.length() > 0) { + valsStr += ","; + } + valsStr += std::to_string(val.get()); + } + attr = std::make_pair("line_adver_fec", valsStr); + attrs.push_back(attr); + + /* line_adver_auto_neg */ + if (port.find("line_adver_auto_neg") == port.end()) { + SWSS_LOG_ERROR("missing 'line_adver_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_adver_auto_neg"]; + if (val.get() != false && val.get() != true) { + SWSS_LOG_ERROR("not a boolean: 'line_adver_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineAdverAutoNegStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_adver_auto_neg", lineAdverAutoNegStr); + attrs.push_back(attr); + + /* line_adver_asym_pause */ + if (port.find("line_adver_asym_pause") == port.end()) { + SWSS_LOG_ERROR("missing 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_adver_asym_pause"]; + if (val.get() != false && val.get() != true) { + SWSS_LOG_ERROR("not a boolean: 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineAdverAsymPauseStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_adver_asym_pause", lineAdverAsymPauseStr); + attrs.push_back(attr); + + /* line_adver_media_type */ + if (port.find("line_adver_media_type") == port.end()) { + SWSS_LOG_ERROR("missing 'line_adver_media_type' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_adver_media_type"]; + attr = std::make_pair("line_adver_media_type", val.get()); + attrs.push_back(attr); + + std::string key; + int phyId = getPhyId(); + key = "phy:" + std::to_string(phyId) + ":ports:" + std::to_string(id); + if (getWriteToDb() == true) { + writeToDb(key, attrs); + } + } catch (const std::exception& e) { + SWSS_LOG_ERROR("unable to read ports configuration item %d (invalid format): %s", iter, e.what()); + return false; + } + } + } else { + SWSS_LOG_ERROR("missing 'ports' field in phy configuration"); + return false; + } + return true; +} diff --git a/gearsyncd/phyparser.h b/gearsyncd/phyparser.h new file mode 100644 index 0000000000..65deead685 --- /dev/null +++ b/gearsyncd/phyparser.h @@ -0,0 +1,32 @@ +/* + * Copyright 2019 Broadcom Inc. + * + * 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. + */ + +#if !defined(__PHY_PARSER_H__) +#define __PHY_PARSER_H__ + +#include "gearparserbase.h" + +class PhyParser: public GearParserBase +{ +public: + bool parse(); + void setPhyId(int id) {m_phyId = id;}; + int getPhyId() {return m_phyId;}; +private: + int m_phyId; +}; + +#endif /* __PHY_PARSER_H__ */ diff --git a/gearsyncd/tests/Makefile.am b/gearsyncd/tests/Makefile.am new file mode 100644 index 0000000000..3b9f94e6e7 --- /dev/null +++ b/gearsyncd/tests/Makefile.am @@ -0,0 +1,21 @@ +INCLUDES = -I.. -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I $(top_srcdir)/cfgmgr + +noinst_PROGRAMS = testphyparser testgearparser +noinst_LIBRARIES = libcunit.a + +if DEBUG +DBGFLAGS = -ggdb -DDEBUG +else +DBGFLAGS = -g +endif + +testphyparser_SOURCES = testphyparser.cpp ../gearparserbase.cpp ../phyparser.cpp $(top_srcdir)/cfgmgr/shellcmd.h +testgearparser_SOURCES = testgearparser.cpp ../gearparserbase.cpp ../gearboxparser.cpp ../phyparser.cpp $(top_srcdir)/cfgmgr/shellcmd.h + +libcunit_a_SOURCES = cunit/Automated.c cunit/Console.c cunit/MyMem.c cunit/TestRun.c cunit/Basic.c cunit/CUError.c cunit/TestDB.c cunit/Util.c + +testphyparser_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(ASAN_CFLAGS) +testgearparser_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(ASAN_CFLAGS) + +testphyparser_LDADD = -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS) $(ASAN_LDFLAGS) libcunit.a +testgearparser_LDADD = -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS) $(ASAN_LDFLAGS) libcunit.a diff --git a/gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_line_lanes.json b/gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_line_lanes.json new file mode 100644 index 0000000000..16a790b519 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_line_lanes.json @@ -0,0 +1,49 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_system_lanes.json b/gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_system_lanes.json new file mode 100644 index 0000000000..86bedc7c1d --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_empty_ethernet_system_lanes.json @@ -0,0 +1,49 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_invalid_array.json b/gearsyncd/tests/configs/negative/gearbox_config_invalid_array.json new file mode 100644 index 0000000000..3533c0e54b --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_invalid_array.json @@ -0,0 +1,49 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": false, + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_invalid_json.json b/gearsyncd/tests/configs/negative/gearbox_config_invalid_json.json new file mode 100644 index 0000000000..eae5bc7aa2 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_invalid_json.json @@ -0,0 +1,49 @@ + + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_invalid_phy_config_file.json b/gearsyncd/tests/configs/negative/gearbox_config_invalid_phy_config_file.json new file mode 100644 index 0000000000..c567489538 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_invalid_phy_config_file.json @@ -0,0 +1,49 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy_config_does_not_exist.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_index.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_index.json new file mode 100644 index 0000000000..b863410e64 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_index.json @@ -0,0 +1,47 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lane_speed.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lane_speed.json new file mode 100644 index 0000000000..b3dea20955 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lane_speed.json @@ -0,0 +1,49 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lanes.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lanes.json new file mode 100644 index 0000000000..f245b8adf7 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_line_lanes.json @@ -0,0 +1,48 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [38,39] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_phy_id.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_phy_id.json new file mode 100644 index 0000000000..b76e613a44 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_phy_id.json @@ -0,0 +1,48 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lane_speed.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lane_speed.json new file mode 100644 index 0000000000..b3dea20955 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lane_speed.json @@ -0,0 +1,49 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lanes.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lanes.json new file mode 100644 index 0000000000..1a6a60dc0d --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_ethernet_system_lanes.json @@ -0,0 +1,48 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_interfaces.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_interfaces.json new file mode 100644 index 0000000000..84b2056780 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_interfaces.json @@ -0,0 +1,26 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_address.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_address.json new file mode 100644 index 0000000000..5989dd0856 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_address.json @@ -0,0 +1,44 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "phy_access": "MDIO(NPU)", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "phy_access": "I2C", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_bus_id.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_bus_id.json new file mode 100644 index 0000000000..1b37dfbec2 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_bus_id.json @@ -0,0 +1,44 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "phy_access": "MDIO(NPU)" + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "phy_access": "I2C", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_config_file.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_config_file.json new file mode 100644 index 0000000000..e9589d4daa --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_config_file.json @@ -0,0 +1,44 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "phy_access": "MDIO(NPU)", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "phy_access": "I2C", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_firmware_path.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_firmware_path.json new file mode 100644 index 0000000000..33cbfc176c --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_firmware_path.json @@ -0,0 +1,44 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "config_file": "tests/configs/positive/phy1_config_1.json", + "phy_access": "MDIO(NPU)", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "phy_access": "I2C", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_id.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_id.json new file mode 100644 index 0000000000..39b4a0e57a --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_id.json @@ -0,0 +1,45 @@ +{ + "phys": [ + { + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_lib_name.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_lib_name.json new file mode 100644 index 0000000000..3a204e07d7 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_lib_name.json @@ -0,0 +1,44 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "phy_access": "MDIO(NPU)", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "phy_access": "I2C", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_name.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_name.json new file mode 100644 index 0000000000..f74dfb154a --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_name.json @@ -0,0 +1,44 @@ +{ + "phys": [ + { + "phy_id": 0, + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "phy_access": "MDIO(NPU)", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "phy_access": "I2C", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_phy_access.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_phy_access.json new file mode 100644 index 0000000000..33fd9f368f --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_phy_access.json @@ -0,0 +1,44 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "phy_access": "I2C", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_sai_init_config_file.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_sai_init_config_file.json new file mode 100644 index 0000000000..fd28a59ef1 --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phy_sai_init_config_file.json @@ -0,0 +1,45 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/negative/gearbox_config_missing_phys.json b/gearsyncd/tests/configs/negative/gearbox_config_missing_phys.json new file mode 100644 index 0000000000..fda33ee3ba --- /dev/null +++ b/gearsyncd/tests/configs/negative/gearbox_config_missing_phys.json @@ -0,0 +1,23 @@ +{ + "interfaces": [ + { + "index": 1, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "index": 1, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} + diff --git a/gearsyncd/tests/configs/negative/phy_config_invalid_array.json b/gearsyncd/tests/configs/negative/phy_config_invalid_array.json new file mode 100644 index 0000000000..e5011b0bd7 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_invalid_array.json @@ -0,0 +1,146 @@ +{ + "name": "example-1", + "address": "0x1000", + "mode": "gearbox", + "port_ref_clk": "600Mhz", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": 12345, + "system_lane_speed": "50G", + "line_lanes": [30,31,32,33], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lanes": [40,41], + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_invalid_boolean.json b/gearsyncd/tests/configs/negative/phy_config_invalid_boolean.json new file mode 100644 index 0000000000..c4bc571102 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_invalid_boolean.json @@ -0,0 +1,180 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": 99999, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fed": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fed": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_invalid_boolean2.json b/gearsyncd/tests/configs/negative/phy_config_invalid_boolean2.json new file mode 100644 index 0000000000..31ae33eaf2 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_invalid_boolean2.json @@ -0,0 +1,181 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": "hello", + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fed": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fed": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_invalid_json.json b/gearsyncd/tests/configs/negative/phy_config_invalid_json.json new file mode 100644 index 0000000000..7d7a46428b --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_invalid_json.json @@ -0,0 +1,180 @@ + + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fed": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fed": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_id.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_id.json new file mode 100644 index 0000000000..3307b17a9d --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_id.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_rx_lanemap.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_rx_lanemap.json new file mode 100644 index 0000000000..79e6e16d8b --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_rx_lanemap.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_to_system_lanemap.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_to_system_lanemap.json new file mode 100644 index 0000000000..ff10837354 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_to_system_lanemap.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_tx_lanemap.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_tx_lanemap.json new file mode 100644 index 0000000000..71d053c99d --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_line_tx_lanemap.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_local_lane_id.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_local_lane_id.json new file mode 100644 index 0000000000..3ce5a327c5 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_local_lane_id.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_mdio_addr.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_mdio_addr.json new file mode 100644 index 0000000000..0817acacf7 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_mdio_addr.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38 + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_rx_polarity.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_rx_polarity.json new file mode 100644 index 0000000000..f4fafdcdcf --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_rx_polarity.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_system_side.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_system_side.json new file mode 100644 index 0000000000..1158244607 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_system_side.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_lanes_missing_tx_polarity.json b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_tx_polarity.json new file mode 100644 index 0000000000..4dcdd48f7f --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_lanes_missing_tx_polarity.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_missing_address.json b/gearsyncd/tests/configs/negative/phy_config_missing_address.json new file mode 100644 index 0000000000..ea28ff8695 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_missing_address.json @@ -0,0 +1,180 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_missing_lanes.json b/gearsyncd/tests/configs/negative/phy_config_missing_lanes.json new file mode 100644 index 0000000000..2b603612cc --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_missing_lanes.json @@ -0,0 +1,46 @@ +{ + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_missing_mode.json b/gearsyncd/tests/configs/negative/phy_config_missing_mode.json new file mode 100644 index 0000000000..f1b14c7b8c --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_missing_mode.json @@ -0,0 +1,145 @@ +{ + "name": "example-1", + "address": "0x1000", + "port_ref_clk": "600Mhz", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": [34,35], + "system_lane_speed": "50G", + "line_lanes": [30,31,32,33], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lanes": [40,41], + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_missing_name.json b/gearsyncd/tests/configs/negative/phy_config_missing_name.json new file mode 100644 index 0000000000..672ae777c1 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_missing_name.json @@ -0,0 +1,145 @@ +{ + "address": "0x1000", + "mode": "gearbox", + "port_ref_clk": "600Mhz", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": [34,35], + "system_lane_speed": "50G", + "line_lanes": [30,31,32,33], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lanes": [40,41], + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_missing_port_ref_clk.json b/gearsyncd/tests/configs/negative/phy_config_missing_port_ref_clk.json new file mode 100644 index 0000000000..6e481ec759 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_missing_port_ref_clk.json @@ -0,0 +1,145 @@ +{ + "name": "example-1", + "address": "0x1000", + "mode": "gearbox", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": [34,35], + "system_lane_speed": "50G", + "line_lanes": [30,31,32,33], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lanes": [40,41], + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_empty_line_lanes.json b/gearsyncd/tests/configs/negative/phy_config_ports_empty_line_lanes.json new file mode 100644 index 0000000000..ffb94cf33c --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_empty_line_lanes.json @@ -0,0 +1,146 @@ +{ + "name": "example-1", + "address": "0x1000", + "mode": "gearbox", + "port_ref_clk": "600Mhz", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": [34,35], + "system_lane_speed": "50G", + "line_lanes": [], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lanes": [40,41], + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_empty_system_lanes.json b/gearsyncd/tests/configs/negative/phy_config_ports_empty_system_lanes.json new file mode 100644 index 0000000000..fddf60b55c --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_empty_system_lanes.json @@ -0,0 +1,146 @@ +{ + "name": "example-1", + "address": "0x1000", + "mode": "gearbox", + "port_ref_clk": "600Mhz", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": [], + "system_lane_speed": "50G", + "line_lanes": [30,31,32,33], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lanes": [40,41], + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_id.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_id.json new file mode 100644 index 0000000000..dc3c326278 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_id.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_asym_pause.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_asym_pause.json new file mode 100644 index 0000000000..520c6e9c27 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_asym_pause.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_auto_neg.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_auto_neg.json new file mode 100644 index 0000000000..ad2b26fff4 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_auto_neg.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_fec.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_fec.json new file mode 100644 index 0000000000..1c7762be4e --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_fec.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_media_type.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_media_type.json new file mode 100644 index 0000000000..dba61a2a11 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_media_type.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_speed.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_speed.json new file mode 100644 index 0000000000..0645d4bb31 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_adver_speed.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_auto_neg.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_auto_neg.json new file mode 100644 index 0000000000..00dab90175 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_auto_neg.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_fec.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_fec.json new file mode 100644 index 0000000000..1d48128a73 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_fec.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_intf_type.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_intf_type.json new file mode 100644 index 0000000000..7c7c8fb1f5 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_intf_type.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lane_speed.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lane_speed.json new file mode 100644 index 0000000000..ed846b042d --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lane_speed.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lanes.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lanes.json new file mode 100644 index 0000000000..0b85fb99f0 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_lanes.json @@ -0,0 +1,145 @@ +{ + "name": "example-1", + "address": "0x1000", + "mode": "gearbox", + "port_ref_clk": "600Mhz", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": [34,35], + "system_lane_speed": "50G", + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lanes": [40,41], + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_loopback.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_loopback.json new file mode 100644 index 0000000000..87c89828d7 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_loopback.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_media_type.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_media_type.json new file mode 100644 index 0000000000..a6aeed9b6a --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_media_type.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_speed.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_speed.json new file mode 100644 index 0000000000..ed846b042d --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_speed.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_training.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_training.json new file mode 100644 index 0000000000..ed5647b0ee --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_line_training.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_mdio_addr.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_mdio_addr.json new file mode 100644 index 0000000000..1a24028f72 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_mdio_addr.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_auto_neg.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_auto_neg.json new file mode 100644 index 0000000000..fa247c4a89 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_auto_neg.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_fec.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_fec.json new file mode 100644 index 0000000000..1cffe24e5c --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_fec.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lane_speed.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lane_speed.json new file mode 100644 index 0000000000..d3970648ff --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lane_speed.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lanes.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lanes.json new file mode 100644 index 0000000000..59b6de87b8 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_lanes.json @@ -0,0 +1,145 @@ +{ + "name": "example-1", + "address": "0x1000", + "mode": "gearbox", + "port_ref_clk": "600Mhz", + "lanes": [ + { + "id": 30, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "id": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "id": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "id": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "id": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "id": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "id": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "id": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "id": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "mdio_addr": "0x1009" + }, + { + "id": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 1, + "mdio_addr": "0x1010" + }, + { + "id": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 1, + "mdio_addr": "0x1011" + }, + { + "id": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 1, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "id": 0, + "system_lanes": [34,35], + "system_lane_speed": "50G", + "line_lanes": [30,31,32,33], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x2000" + }, + { + "id": 1, + "system_lane_speed": "50G", + "line_lanes": [34,35,36,37], + "line_lane_speed": "25G", + "line_adver_speed": [], + "line_adver_fec": [], + "mdio_addr": "0x3000" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_loopback.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_loopback.json new file mode 100644 index 0000000000..cf04dcd39a --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_loopback.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_speed.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_speed.json new file mode 100644 index 0000000000..117a29ee36 --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_speed.json @@ -0,0 +1,178 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_training.json b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_training.json new file mode 100644 index 0000000000..d07e69324f --- /dev/null +++ b/gearsyncd/tests/configs/negative/phy_config_ports_missing_system_training.json @@ -0,0 +1,179 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/positive/gearbox_config_1.json b/gearsyncd/tests/configs/positive/gearbox_config_1.json new file mode 100644 index 0000000000..b3dea20955 --- /dev/null +++ b/gearsyncd/tests/configs/positive/gearbox_config_1.json @@ -0,0 +1,49 @@ +{ + "phys": [ + { + "phy_id": 0, + "name": "example-1", + "address": "0x1000", + "lib_name": "libsai_phy_example1.so", + "firmware_path": "/tmp/phy-example1.bin", + "config_file": "tests/configs/positive/phy1_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-1.bcm", + "phy_access": "mdio", + "bus_id": 0 + }, + { + "phy_id": 1, + "name": "example-2", + "address": "0x2000", + "lib_name": "libsai_phy_example2.so", + "firmware_path": "/tmp/phy-example2.bin", + "config_file": "tests/configs/positive/phy2_config_1.json", + "sai_init_config_file": "/usr/share/sonic/hwsku/example-2.bcm", + "phy_access": "i2c", + "bus_id": 1 + } + ], + "interfaces": [ + { + "name": "Ethernet0", + "index": 0, + "phy_id" : 0, + "system_lanes": [38,39], + "line_lanes": [30,31,32,33] + }, + { + "name": "Ethernet1", + "index": 1, + "phy_id" : 0, + "system_lanes": [40,41], + "line_lanes": [34,35,36,37] + }, + { + "name": "Ethernet2", + "index": 2, + "phy_id" : 1, + "system_lanes": [4,5], + "line_lanes": [0,1,2,3] + } + ] +} diff --git a/gearsyncd/tests/configs/positive/phy1_config_1.json b/gearsyncd/tests/configs/positive/phy1_config_1.json new file mode 100644 index 0000000000..ea28ff8695 --- /dev/null +++ b/gearsyncd/tests/configs/positive/phy1_config_1.json @@ -0,0 +1,180 @@ +{ + "lanes": [ + { + "index": 30, + "local_lane_id": 0, + "system_side": false, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 3, + "line_rx_lanemap": 3, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1001" + }, + { + "index": 31, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 38, + "mdio_addr": "0x1002" + }, + { + "index": 32, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1003" + }, + { + "index": 33, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 39, + "mdio_addr": "0x1004" + }, + { + "index": 34, + "system_side": false, + "local_lane_id": 4, + "line_tx_lanemap": 0, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1005" + }, + { + "index": 35, + "system_side": false, + "local_lane_id": 5, + "line_tx_lanemap": 1, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 1, + "line_to_system_lanemap": 40, + "mdio_addr": "0x1006" + }, + { + "index": 36, + "system_side": false, + "local_lane_id": 6, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 2, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1007" + }, + { + "index": 37, + "system_side": false, + "local_lane_id": 7, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 3, + "rx_polarity": 1, + "line_to_system_lanemap": 41, + "mdio_addr": "0x1008" + }, + { + "index": 38, + "system_side": true, + "local_lane_id": 0, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1009" + }, + { + "index": 39, + "system_side": true, + "local_lane_id": 1, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1010" + }, + { + "index": 40, + "system_side": true, + "local_lane_id": 2, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1011" + }, + { + "index": 41, + "system_side": true, + "local_lane_id": 3, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x1012" + } + ], + "ports": [ + { + "index": 0, + "mdio_addr": "0x2000", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "copper", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "copper" + }, + { + "index": 1, + "mdio_addr": "0x2001", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/configs/positive/phy2_config_1.json b/gearsyncd/tests/configs/positive/phy2_config_1.json new file mode 100644 index 0000000000..940d9087dd --- /dev/null +++ b/gearsyncd/tests/configs/positive/phy2_config_1.json @@ -0,0 +1,115 @@ +{ + "lanes": [ + { + "index": 0, + "system_side": false, + "local_lane_id": 0, + "line_tx_lanemap": 3, + "tx_polarity": 0, + "line_rx_lanemap": 0, + "rx_polarity": 0, + "line_to_system_lanemap": 4, + "mdio_addr": "0x3001" + }, + { + "index": 1, + "system_side": false, + "local_lane_id": 1, + "line_tx_lanemap": 2, + "tx_polarity": 0, + "line_rx_lanemap": 1, + "rx_polarity": 0, + "line_to_system_lanemap": 4, + "mdio_addr": "0x3002" + }, + { + "index": 2, + "system_side": false, + "local_lane_id": 2, + "line_tx_lanemap": 1, + "tx_polarity": 1, + "line_rx_lanemap": 2, + "rx_polarity": 0, + "line_to_system_lanemap": 5, + "mdio_addr": "0x3003" + }, + { + "index": 3, + "system_side": false, + "local_lane_id": 3, + "line_tx_lanemap": 0, + "tx_polarity": 1, + "line_rx_lanemap": 3, + "rx_polarity": 0, + "line_to_system_lanemap": 5, + "mdio_addr": "0x3004" + }, + { + "index": 4, + "system_side": true, + "local_lane_id": 4, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x3005" + }, + { + "index": 5, + "system_side": true, + "local_lane_id": 5, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x3006" + }, + { + "index": 6, + "system_side": true, + "local_lane_id": 6, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x3007" + }, + { + "index": 7, + "system_side": true, + "local_lane_id": 7, + "tx_polarity": 0, + "rx_polarity": 0, + "line_tx_lanemap": 0, + "line_rx_lanemap": 0, + "line_to_system_lanemap": 0, + "mdio_addr": "0x3008" + } + ], + "ports": [ + { + "index": 2, + "mdio_addr": "0x3100", + "system_speed": 25000, + "system_fec": "none", + "system_auto_neg": true, + "system_loopback": "none", + "system_training": false, + "line_speed": 50000, + "line_fec": "none", + "line_auto_neg": true, + "line_media_type": "fiber", + "line_intf_type": "none", + "line_loopback": "none", + "line_training": false, + "line_adver_speed": [], + "line_adver_fec": [], + "line_adver_auto_neg": false, + "line_adver_asym_pause": false, + "line_adver_media_type": "fiber" + } + ] +} diff --git a/gearsyncd/tests/cunit/Automated.c b/gearsyncd/tests/cunit/Automated.c new file mode 100644 index 0000000000..79d5a68054 --- /dev/null +++ b/gearsyncd/tests/cunit/Automated.c @@ -0,0 +1,623 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of the Automated Test Interface. + * + * Feb 2002 Initial implementation. (AK) + * + * 13/Feb/2002 Added initial automated interface functions to generate + * HTML based Run report. (AK) + * + * 23/Jul/2002 Changed HTML to XML Format file generation for Automated Tests. (AK) + * + * 27/Jul/2003 Fixed a bug which hinders the listing of all failures. (AK) + * + * 17-Jul-2004 New interface, doxygen comments, eliminate compiler warnings, + * automated_run_tests now assigns a generic file name if + * none has been supplied. (JDS) + * + * 30-Apr-2005 Added notification of failed suite cleanup function. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Automated test interface with xml result output (implementation). + */ +/** @addtogroup Automated + @{ +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "MyMem.h" +#include "Util.h" +#include "TestRun.h" +#include "Automated.h" +#include "CUnit_intl.h" + +#define MAX_FILENAME_LENGTH 1025 + +/*================================================================= + * Global / Static data definitions + *=================================================================*/ +static CU_pSuite f_pRunningSuite = NULL; /**< The running test suite. */ +static char f_szDefaultFileRoot[] = "CUnitAutomated"; /**< Default filename root for automated output files. */ +static char f_szTestListFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test listing file. */ +static char f_szTestResultFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test results file. */ +static FILE* f_pTestResultFile = NULL; /**< FILE pointer the test results file. */ + +static CU_BOOL f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; /**< Flag for keeping track of when a closing xml tag is required. */ + +/*================================================================= + * Static function forward declarations + *=================================================================*/ +static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename); + +static CU_ErrorCode initialize_result_file(const char* szFilename); +static CU_ErrorCode uninitialize_result_file(void); + +static void automated_run_all_tests(CU_pTestRegistry pRegistry); + +static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); +static void automated_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); +static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); +static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite); +static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +void CU_automated_run_tests(void) +{ + assert(NULL != CU_get_registry()); + + /* Ensure output makes it to screen at the moment of a SIGSEGV. */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + /* if a filename root hasn't been set, use the default one */ + if (0 == strlen(f_szTestResultFileName)) { + CU_set_output_filename(f_szDefaultFileRoot); + } + + if (CUE_SUCCESS != initialize_result_file(f_szTestResultFileName)) { + fprintf(stderr, "\n%s", _("ERROR - Failed to create/initialize the result file.")); + } + else { + /* set up the message handlers for writing xml output */ + CU_set_test_start_handler(automated_test_start_message_handler); + CU_set_test_complete_handler(automated_test_complete_message_handler); + CU_set_all_test_complete_handler(automated_all_tests_complete_message_handler); + CU_set_suite_init_failure_handler(automated_suite_init_failure_message_handler); + CU_set_suite_cleanup_failure_handler(automated_suite_cleanup_failure_message_handler); + + f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; + + automated_run_all_tests(NULL); + + if (CUE_SUCCESS != uninitialize_result_file()) { + fprintf(stderr, "\n%s", _("ERROR - Failed to close/uninitialize the result files.")); + } + } +} + +/*------------------------------------------------------------------------*/ +void CU_set_output_filename(const char* szFilenameRoot) +{ + const char* szListEnding = "-Listing.xml"; + const char* szResultEnding = "-Results.xml"; + + /* Construct the name for the listing file */ + if (NULL != szFilenameRoot) { + strncpy(f_szTestListFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); + } + else { + strncpy(f_szTestListFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); + } + + f_szTestListFileName[MAX_FILENAME_LENGTH - strlen(szListEnding) - 1] = '\0'; + strcat(f_szTestListFileName, szListEnding); + + /* Construct the name for the result file */ + if (NULL != szFilenameRoot) { + strncpy(f_szTestResultFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); + } + else { + strncpy(f_szTestResultFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); + } + + f_szTestResultFileName[MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1] = '\0'; + strcat(f_szTestResultFileName, szResultEnding); +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_list_tests_to_file() +{ + /* if a filename root hasn't been set, use the default one */ + if (0 == strlen(f_szTestListFileName)) { + CU_set_output_filename(f_szDefaultFileRoot); + } + + return automated_list_all_tests(CU_get_registry(), f_szTestListFileName); +} + +/*================================================================= + * Static function implementation + *=================================================================*/ +/** Runs the registered tests using the automated interface. + * If non-NULL. the specified registry is set as the active + * registry for running the tests. If NULL, then the default + * CUnit test registry is used. The actual test running is + * performed by CU_run_all_tests(). + * @param pRegistry The test registry to run. + */ +static void automated_run_all_tests(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = NULL; + + assert(NULL != f_pTestResultFile); + + f_pRunningSuite = NULL; + + if (NULL != pRegistry) { + pOldRegistry = CU_set_registry(pRegistry); + } + fprintf(f_pTestResultFile," \n"); + CU_run_all_tests(); + if (NULL != pRegistry) { + CU_set_registry(pOldRegistry); + } +} + +/*------------------------------------------------------------------------*/ +/** Initializes the test results file generated by the automated interface. + * A file stream is opened and header information is written. + */ +static CU_ErrorCode initialize_result_file(const char* szFilename) +{ + char* szTime; + time_t tTime = 0; + + CU_set_error(CUE_SUCCESS); + + if ((NULL == szFilename) || (strlen(szFilename) == 0)) { + CU_set_error(CUE_BAD_FILENAME); + } + else if (NULL == (f_pTestResultFile = fopen(szFilename, "w"))) { + CU_set_error(CUE_FOPEN_FAILED); + } + else { + setvbuf(f_pTestResultFile, NULL, _IONBF, 0); + + time(&tTime); + szTime = ctime(&tTime); + fprintf(f_pTestResultFile, + " \n" + " \n" + " Test run start - %s \n", + (NULL != szTime) ? szTime : ""); + } + + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at start of each test. + * The test result file must have been opened before this + * function is called (i.e. f_pTestResultFile non-NULL). + * @param pTest The test being run (non-NULL). + * @param pSuite The suite containing the test (non-NULL). + */ +static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + CU_UNREFERENCED_PARAMETER(pTest); /* not currently used */ + + assert(NULL != pTest); + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + /* write suite close/open tags if this is the 1st test for this szSuite */ + if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { + if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n", + pSuite->pName); + + f_bWriting_CUNIT_RUN_SUITE = CU_TRUE; + f_pRunningSuite = pSuite; + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of each test. + * @param pTest The test being run (non-NULL). + * @param pSuite The suite containing the test (non-NULL). + * @param pFailure Pointer to the 1st failure record for this test. + */ +static void automated_test_complete_message_handler(const CU_pTest pTest, + const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + char *szTemp = NULL; + size_t szTemp_len = 0; + size_t cur_len = 0; + CU_pFailureRecord pTempFailure = pFailure; + + CU_UNREFERENCED_PARAMETER(pSuite); /* pSuite is not used except in assertion */ + + assert(NULL != pTest); + assert(NULL != pTest->pName); + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + if (NULL != pTempFailure) { + + while (NULL != pTempFailure) { + + assert((NULL != pTempFailure->pSuite) && (pTempFailure->pSuite == pSuite)); + assert((NULL != pTempFailure->pTest) && (pTempFailure->pTest == pTest)); + + /* expand temporary char buffer if need more room */ + if (NULL != pTempFailure->strCondition) { + cur_len = CU_translated_strlen(pTempFailure->strCondition) + 1; + } + else { + cur_len = 1; + } + if (cur_len > szTemp_len) { + szTemp_len = cur_len; + if (NULL != szTemp) { + CU_FREE(szTemp); + } + szTemp = (char *)CU_MALLOC(szTemp_len); + } + + /* convert xml entities in strCondition (if present) */ + if (NULL != pTempFailure->strCondition) { + CU_translate_special_characters(pTempFailure->strCondition, szTemp, szTemp_len); + } + else { + szTemp[0] = '\0'; + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " %s \n" + " %u \n" + " %s \n" + " \n" + " \n", + pTest->pName, + (NULL != pTempFailure->strFileName) ? pTempFailure->strFileName : "", + pTempFailure->uiLineNumber, + szTemp); + pTempFailure = pTempFailure->pNext; + } + } + else { + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " \n" + " \n", + pTest->pName); + } + + if (NULL != szTemp) { + CU_FREE(szTemp); + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of all tests in a suite. + * @param pFailure Pointer to the test failure record list. + */ +static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) +{ + CU_pTestRegistry pRegistry = CU_get_registry(); + CU_pRunSummary pRunSummary = CU_get_run_summary(); + + CU_UNREFERENCED_PARAMETER(pFailure); /* not used */ + + assert(NULL != pRegistry); + assert(NULL != pRunSummary); + assert(NULL != f_pTestResultFile); + + if ((NULL != f_pRunningSuite) && (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE)) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + } + + fprintf(f_pTestResultFile, + " \n" + " \n"); + + fprintf(f_pTestResultFile, + " \n" + " %s \n" + " %u \n" + " %u \n" + " - NA - \n" + " %u \n" + " %u \n" + " \n", + _("Suites"), + pRegistry->uiNumberOfSuites, + pRunSummary->nSuitesRun, + pRunSummary->nSuitesFailed, + pRunSummary->nSuitesInactive + ); + + fprintf(f_pTestResultFile, + " \n" + " %s \n" + " %u \n" + " %u \n" + " %u \n" + " %u \n" + " %u \n" + " \n", + _("Test Cases"), + pRegistry->uiNumberOfTests, + pRunSummary->nTestsRun, + pRunSummary->nTestsRun - pRunSummary->nTestsFailed, + pRunSummary->nTestsFailed, + pRunSummary->nTestsInactive + ); + + fprintf(f_pTestResultFile, + " \n" + " %s \n" + " %u \n" + " %u \n" + " %u \n" + " %u \n" + " %s \n" + " \n" + " \n", + _("Assertions"), + pRunSummary->nAsserts, + pRunSummary->nAsserts, + pRunSummary->nAsserts - pRunSummary->nAssertsFailed, + pRunSummary->nAssertsFailed, + _("n/a") + ); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite initialization fails. + * @param pSuite The suite for which initialization failed. + */ +static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " %s \n" + " \n" + " \n", + pSuite->pName, + _("Suite Initialization Failed")); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite cleanup fails. + * @param pSuite The suite for which cleanup failed. + */ +static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " %s \n" + " \n" + " \n", + pSuite->pName, + _("Suite Cleanup Failed")); +} + +/*------------------------------------------------------------------------*/ +/** Finalizes and closes the results output file generated + * by the automated interface. + */ +static CU_ErrorCode uninitialize_result_file(void) +{ + + assert(NULL != f_pTestResultFile); + + CU_set_error(CUE_SUCCESS); + + fprintf(f_pTestResultFile,""); + + if (0 != fflush(f_pTestResultFile)) { + CU_set_error(CUE_FCLOSE_FAILED); + } + + if (0 != fclose(f_pTestResultFile)) { + CU_set_error(CUE_FCLOSE_FAILED); + } + + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +/** Generates an xml listing of all tests in all suites for the + * specified test registry. The output is directed to a file + * having the specified name. + * @param pRegistry Test registry for which to generate list (non-NULL). + * @param szFilename Non-NULL, non-empty string containing name for + * listing file. + * @return A CU_ErrorCode indicating the error status. + */ +static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename) +{ + CU_pSuite pSuite = NULL; + CU_pTest pTest = NULL; + FILE* pTestListFile = NULL; + char* szTime; + time_t tTime = 0; + + CU_set_error(CUE_SUCCESS); + + if (NULL == pRegistry) { + CU_set_error(CUE_NOREGISTRY); + } + else if ((NULL == szFilename) || (0 == strlen(szFilename))) { + CU_set_error(CUE_BAD_FILENAME); + } + else if (NULL == (pTestListFile = fopen(f_szTestListFileName, "w"))) { + CU_set_error(CUE_FOPEN_FAILED); + } + else { + setvbuf(pTestListFile, NULL, _IONBF, 0); + + fprintf(pTestListFile, + " \n" + " \n" + " \n" + " \n"); + + fprintf(pTestListFile, + " \n" + " %s \n" + " %u \n" + " \n", + _("Total Number of Suites"), + pRegistry->uiNumberOfSuites); + + fprintf(pTestListFile, + " \n" + " %s \n" + " %u \n" + " \n" + " \n", + _("Total Number of Test Cases"), + pRegistry->uiNumberOfTests); + + fprintf(pTestListFile, + " \n"); + + pSuite = pRegistry->pSuite; + while (NULL != pSuite) { + assert(NULL != pSuite->pName); + pTest = pSuite->pTest; + + fprintf(pTestListFile, + " \n" + " \n" + " %s \n" + " %s \n" + " %s \n" + " %s \n" + " %u \n" + " \n", + pSuite->pName, + (NULL != pSuite->pInitializeFunc) ? _("Yes") : _("No"), + (NULL != pSuite->pCleanupFunc) ? _("Yes") : _("No"), + (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No"), + pSuite->uiNumberOfTests); + + fprintf(pTestListFile, + " \n"); + while (NULL != pTest) { + assert(NULL != pTest->pName); + fprintf(pTestListFile, + " \n" + " %s \n" + " %s \n" + " \n", + pTest->pName, + (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No")); + pTest = pTest->pNext; + } + + fprintf(pTestListFile, + " \n" + " \n"); + + pSuite = pSuite->pNext; + } + + fprintf(pTestListFile, " \n"); + + time(&tTime); + szTime = ctime(&tTime); + fprintf(pTestListFile, + " %s" CU_VERSION " - %s \n" + "", + _("File Generated By CUnit v"), + (NULL != szTime) ? szTime : ""); + + if (0 != fclose(pTestListFile)) { + CU_set_error(CUE_FCLOSE_FAILED); + } + } + + return CU_get_error(); +} + +/** @} */ diff --git a/gearsyncd/tests/cunit/Automated.cpp b/gearsyncd/tests/cunit/Automated.cpp new file mode 100644 index 0000000000..79d5a68054 --- /dev/null +++ b/gearsyncd/tests/cunit/Automated.cpp @@ -0,0 +1,623 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of the Automated Test Interface. + * + * Feb 2002 Initial implementation. (AK) + * + * 13/Feb/2002 Added initial automated interface functions to generate + * HTML based Run report. (AK) + * + * 23/Jul/2002 Changed HTML to XML Format file generation for Automated Tests. (AK) + * + * 27/Jul/2003 Fixed a bug which hinders the listing of all failures. (AK) + * + * 17-Jul-2004 New interface, doxygen comments, eliminate compiler warnings, + * automated_run_tests now assigns a generic file name if + * none has been supplied. (JDS) + * + * 30-Apr-2005 Added notification of failed suite cleanup function. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Automated test interface with xml result output (implementation). + */ +/** @addtogroup Automated + @{ +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "MyMem.h" +#include "Util.h" +#include "TestRun.h" +#include "Automated.h" +#include "CUnit_intl.h" + +#define MAX_FILENAME_LENGTH 1025 + +/*================================================================= + * Global / Static data definitions + *=================================================================*/ +static CU_pSuite f_pRunningSuite = NULL; /**< The running test suite. */ +static char f_szDefaultFileRoot[] = "CUnitAutomated"; /**< Default filename root for automated output files. */ +static char f_szTestListFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test listing file. */ +static char f_szTestResultFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test results file. */ +static FILE* f_pTestResultFile = NULL; /**< FILE pointer the test results file. */ + +static CU_BOOL f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; /**< Flag for keeping track of when a closing xml tag is required. */ + +/*================================================================= + * Static function forward declarations + *=================================================================*/ +static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename); + +static CU_ErrorCode initialize_result_file(const char* szFilename); +static CU_ErrorCode uninitialize_result_file(void); + +static void automated_run_all_tests(CU_pTestRegistry pRegistry); + +static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); +static void automated_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); +static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); +static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite); +static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +void CU_automated_run_tests(void) +{ + assert(NULL != CU_get_registry()); + + /* Ensure output makes it to screen at the moment of a SIGSEGV. */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + /* if a filename root hasn't been set, use the default one */ + if (0 == strlen(f_szTestResultFileName)) { + CU_set_output_filename(f_szDefaultFileRoot); + } + + if (CUE_SUCCESS != initialize_result_file(f_szTestResultFileName)) { + fprintf(stderr, "\n%s", _("ERROR - Failed to create/initialize the result file.")); + } + else { + /* set up the message handlers for writing xml output */ + CU_set_test_start_handler(automated_test_start_message_handler); + CU_set_test_complete_handler(automated_test_complete_message_handler); + CU_set_all_test_complete_handler(automated_all_tests_complete_message_handler); + CU_set_suite_init_failure_handler(automated_suite_init_failure_message_handler); + CU_set_suite_cleanup_failure_handler(automated_suite_cleanup_failure_message_handler); + + f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; + + automated_run_all_tests(NULL); + + if (CUE_SUCCESS != uninitialize_result_file()) { + fprintf(stderr, "\n%s", _("ERROR - Failed to close/uninitialize the result files.")); + } + } +} + +/*------------------------------------------------------------------------*/ +void CU_set_output_filename(const char* szFilenameRoot) +{ + const char* szListEnding = "-Listing.xml"; + const char* szResultEnding = "-Results.xml"; + + /* Construct the name for the listing file */ + if (NULL != szFilenameRoot) { + strncpy(f_szTestListFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); + } + else { + strncpy(f_szTestListFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); + } + + f_szTestListFileName[MAX_FILENAME_LENGTH - strlen(szListEnding) - 1] = '\0'; + strcat(f_szTestListFileName, szListEnding); + + /* Construct the name for the result file */ + if (NULL != szFilenameRoot) { + strncpy(f_szTestResultFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); + } + else { + strncpy(f_szTestResultFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); + } + + f_szTestResultFileName[MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1] = '\0'; + strcat(f_szTestResultFileName, szResultEnding); +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_list_tests_to_file() +{ + /* if a filename root hasn't been set, use the default one */ + if (0 == strlen(f_szTestListFileName)) { + CU_set_output_filename(f_szDefaultFileRoot); + } + + return automated_list_all_tests(CU_get_registry(), f_szTestListFileName); +} + +/*================================================================= + * Static function implementation + *=================================================================*/ +/** Runs the registered tests using the automated interface. + * If non-NULL. the specified registry is set as the active + * registry for running the tests. If NULL, then the default + * CUnit test registry is used. The actual test running is + * performed by CU_run_all_tests(). + * @param pRegistry The test registry to run. + */ +static void automated_run_all_tests(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = NULL; + + assert(NULL != f_pTestResultFile); + + f_pRunningSuite = NULL; + + if (NULL != pRegistry) { + pOldRegistry = CU_set_registry(pRegistry); + } + fprintf(f_pTestResultFile," \n"); + CU_run_all_tests(); + if (NULL != pRegistry) { + CU_set_registry(pOldRegistry); + } +} + +/*------------------------------------------------------------------------*/ +/** Initializes the test results file generated by the automated interface. + * A file stream is opened and header information is written. + */ +static CU_ErrorCode initialize_result_file(const char* szFilename) +{ + char* szTime; + time_t tTime = 0; + + CU_set_error(CUE_SUCCESS); + + if ((NULL == szFilename) || (strlen(szFilename) == 0)) { + CU_set_error(CUE_BAD_FILENAME); + } + else if (NULL == (f_pTestResultFile = fopen(szFilename, "w"))) { + CU_set_error(CUE_FOPEN_FAILED); + } + else { + setvbuf(f_pTestResultFile, NULL, _IONBF, 0); + + time(&tTime); + szTime = ctime(&tTime); + fprintf(f_pTestResultFile, + " \n" + " \n" + " Test run start - %s \n", + (NULL != szTime) ? szTime : ""); + } + + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at start of each test. + * The test result file must have been opened before this + * function is called (i.e. f_pTestResultFile non-NULL). + * @param pTest The test being run (non-NULL). + * @param pSuite The suite containing the test (non-NULL). + */ +static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + CU_UNREFERENCED_PARAMETER(pTest); /* not currently used */ + + assert(NULL != pTest); + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + /* write suite close/open tags if this is the 1st test for this szSuite */ + if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { + if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n", + pSuite->pName); + + f_bWriting_CUNIT_RUN_SUITE = CU_TRUE; + f_pRunningSuite = pSuite; + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of each test. + * @param pTest The test being run (non-NULL). + * @param pSuite The suite containing the test (non-NULL). + * @param pFailure Pointer to the 1st failure record for this test. + */ +static void automated_test_complete_message_handler(const CU_pTest pTest, + const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + char *szTemp = NULL; + size_t szTemp_len = 0; + size_t cur_len = 0; + CU_pFailureRecord pTempFailure = pFailure; + + CU_UNREFERENCED_PARAMETER(pSuite); /* pSuite is not used except in assertion */ + + assert(NULL != pTest); + assert(NULL != pTest->pName); + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + if (NULL != pTempFailure) { + + while (NULL != pTempFailure) { + + assert((NULL != pTempFailure->pSuite) && (pTempFailure->pSuite == pSuite)); + assert((NULL != pTempFailure->pTest) && (pTempFailure->pTest == pTest)); + + /* expand temporary char buffer if need more room */ + if (NULL != pTempFailure->strCondition) { + cur_len = CU_translated_strlen(pTempFailure->strCondition) + 1; + } + else { + cur_len = 1; + } + if (cur_len > szTemp_len) { + szTemp_len = cur_len; + if (NULL != szTemp) { + CU_FREE(szTemp); + } + szTemp = (char *)CU_MALLOC(szTemp_len); + } + + /* convert xml entities in strCondition (if present) */ + if (NULL != pTempFailure->strCondition) { + CU_translate_special_characters(pTempFailure->strCondition, szTemp, szTemp_len); + } + else { + szTemp[0] = '\0'; + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " %s \n" + " %u \n" + " %s \n" + " \n" + " \n", + pTest->pName, + (NULL != pTempFailure->strFileName) ? pTempFailure->strFileName : "", + pTempFailure->uiLineNumber, + szTemp); + pTempFailure = pTempFailure->pNext; + } + } + else { + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " \n" + " \n", + pTest->pName); + } + + if (NULL != szTemp) { + CU_FREE(szTemp); + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of all tests in a suite. + * @param pFailure Pointer to the test failure record list. + */ +static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) +{ + CU_pTestRegistry pRegistry = CU_get_registry(); + CU_pRunSummary pRunSummary = CU_get_run_summary(); + + CU_UNREFERENCED_PARAMETER(pFailure); /* not used */ + + assert(NULL != pRegistry); + assert(NULL != pRunSummary); + assert(NULL != f_pTestResultFile); + + if ((NULL != f_pRunningSuite) && (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE)) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + } + + fprintf(f_pTestResultFile, + " \n" + " \n"); + + fprintf(f_pTestResultFile, + " \n" + " %s \n" + " %u \n" + " %u \n" + " - NA - \n" + " %u \n" + " %u \n" + " \n", + _("Suites"), + pRegistry->uiNumberOfSuites, + pRunSummary->nSuitesRun, + pRunSummary->nSuitesFailed, + pRunSummary->nSuitesInactive + ); + + fprintf(f_pTestResultFile, + " \n" + " %s \n" + " %u \n" + " %u \n" + " %u \n" + " %u \n" + " %u \n" + " \n", + _("Test Cases"), + pRegistry->uiNumberOfTests, + pRunSummary->nTestsRun, + pRunSummary->nTestsRun - pRunSummary->nTestsFailed, + pRunSummary->nTestsFailed, + pRunSummary->nTestsInactive + ); + + fprintf(f_pTestResultFile, + " \n" + " %s \n" + " %u \n" + " %u \n" + " %u \n" + " %u \n" + " %s \n" + " \n" + " \n", + _("Assertions"), + pRunSummary->nAsserts, + pRunSummary->nAsserts, + pRunSummary->nAsserts - pRunSummary->nAssertsFailed, + pRunSummary->nAssertsFailed, + _("n/a") + ); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite initialization fails. + * @param pSuite The suite for which initialization failed. + */ +static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " %s \n" + " \n" + " \n", + pSuite->pName, + _("Suite Initialization Failed")); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite cleanup fails. + * @param pSuite The suite for which cleanup failed. + */ +static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + assert(NULL != f_pTestResultFile); + + if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { + fprintf(f_pTestResultFile, + " \n" + " \n"); + f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; + } + + fprintf(f_pTestResultFile, + " \n" + " \n" + " %s \n" + " %s \n" + " \n" + " \n", + pSuite->pName, + _("Suite Cleanup Failed")); +} + +/*------------------------------------------------------------------------*/ +/** Finalizes and closes the results output file generated + * by the automated interface. + */ +static CU_ErrorCode uninitialize_result_file(void) +{ + + assert(NULL != f_pTestResultFile); + + CU_set_error(CUE_SUCCESS); + + fprintf(f_pTestResultFile,""); + + if (0 != fflush(f_pTestResultFile)) { + CU_set_error(CUE_FCLOSE_FAILED); + } + + if (0 != fclose(f_pTestResultFile)) { + CU_set_error(CUE_FCLOSE_FAILED); + } + + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +/** Generates an xml listing of all tests in all suites for the + * specified test registry. The output is directed to a file + * having the specified name. + * @param pRegistry Test registry for which to generate list (non-NULL). + * @param szFilename Non-NULL, non-empty string containing name for + * listing file. + * @return A CU_ErrorCode indicating the error status. + */ +static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename) +{ + CU_pSuite pSuite = NULL; + CU_pTest pTest = NULL; + FILE* pTestListFile = NULL; + char* szTime; + time_t tTime = 0; + + CU_set_error(CUE_SUCCESS); + + if (NULL == pRegistry) { + CU_set_error(CUE_NOREGISTRY); + } + else if ((NULL == szFilename) || (0 == strlen(szFilename))) { + CU_set_error(CUE_BAD_FILENAME); + } + else if (NULL == (pTestListFile = fopen(f_szTestListFileName, "w"))) { + CU_set_error(CUE_FOPEN_FAILED); + } + else { + setvbuf(pTestListFile, NULL, _IONBF, 0); + + fprintf(pTestListFile, + " \n" + " \n" + " \n" + " \n"); + + fprintf(pTestListFile, + " \n" + " %s \n" + " %u \n" + " \n", + _("Total Number of Suites"), + pRegistry->uiNumberOfSuites); + + fprintf(pTestListFile, + " \n" + " %s \n" + " %u \n" + " \n" + " \n", + _("Total Number of Test Cases"), + pRegistry->uiNumberOfTests); + + fprintf(pTestListFile, + " \n"); + + pSuite = pRegistry->pSuite; + while (NULL != pSuite) { + assert(NULL != pSuite->pName); + pTest = pSuite->pTest; + + fprintf(pTestListFile, + " \n" + " \n" + " %s \n" + " %s \n" + " %s \n" + " %s \n" + " %u \n" + " \n", + pSuite->pName, + (NULL != pSuite->pInitializeFunc) ? _("Yes") : _("No"), + (NULL != pSuite->pCleanupFunc) ? _("Yes") : _("No"), + (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No"), + pSuite->uiNumberOfTests); + + fprintf(pTestListFile, + " \n"); + while (NULL != pTest) { + assert(NULL != pTest->pName); + fprintf(pTestListFile, + " \n" + " %s \n" + " %s \n" + " \n", + pTest->pName, + (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No")); + pTest = pTest->pNext; + } + + fprintf(pTestListFile, + " \n" + " \n"); + + pSuite = pSuite->pNext; + } + + fprintf(pTestListFile, " \n"); + + time(&tTime); + szTime = ctime(&tTime); + fprintf(pTestListFile, + " %s" CU_VERSION " - %s \n" + "", + _("File Generated By CUnit v"), + (NULL != szTime) ? szTime : ""); + + if (0 != fclose(pTestListFile)) { + CU_set_error(CUE_FCLOSE_FAILED); + } + } + + return CU_get_error(); +} + +/** @} */ diff --git a/gearsyncd/tests/cunit/Automated.h b/gearsyncd/tests/cunit/Automated.h new file mode 100644 index 0000000000..2acc694684 --- /dev/null +++ b/gearsyncd/tests/cunit/Automated.h @@ -0,0 +1,90 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Automated Interface (generates HTML Report Files). + * + * Feb 2002 Initial implementation (AK) + * + * 13-Feb-2002 Single interface to automated_run_tests. (AK) + * + * 20-Jul-2004 New interface, doxygen comments. (JDS) + */ + +/** @file + * Automated testing interface with xml output (user interface). + */ +/** @addtogroup Automated + * @{ + */ + +#ifndef CUNIT_AUTOMATED_H_SEEN +#define CUNIT_AUTOMATED_H_SEEN + +#include "CUnit.h" +#include "TestDB.h" + +#ifdef __cplusplus +extern "C" { +#endif + +CU_EXPORT void CU_automated_run_tests(void); +/**< + * Runs CUnit tests using the automated interface. + * This function sets appropriate callback functions, initializes the + * test output files, and calls the appropriate functions to list the + * tests and run them. If an output file name root has not been + * specified using CU_set_output_filename(), a generic root will be + * applied. It is an error to call this function before the CUnit + * test registry has been initialized (check by assertion). + */ + +CU_EXPORT CU_ErrorCode CU_list_tests_to_file(void); +/**< + * Generates an xml file containing a list of all tests in all suites + * in the active registry. The output file will be named according to + * the most recent call to CU_set_output_filename(), or a default if + * not previously set. + * + * @return An error code indicating the error status. + */ + +CU_EXPORT void CU_set_output_filename(const char* szFilenameRoot); +/**< + * Sets the root file name for automated test output files. + * The strings "-Listing.xml" and "-Results.xml" are appended to the + * specified root to generate the filenames. If szFilenameRoot is + * empty, the default root ("CUnitAutomated") is used. + * + * @param szFilenameRoot String containing root to use for file names. + */ + +#ifdef USE_DEPRECATED_CUNIT_NAMES +/** Deprecated (version 1). @deprecated Use CU_automated_run_tests(). */ +#define automated_run_tests() CU_automated_run_tests() +/** Deprecated (version 1). @deprecated Use CU_set_output_filename(). */ +#define set_output_filename(x) CU_set_output_filename((x)) +#endif /* USE_DEPRECATED_CUNIT_NAMES */ + +#ifdef __cplusplus +} +#endif +#endif /* CUNIT_AUTOMATED_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/cunit/Basic.c b/gearsyncd/tests/cunit/Basic.c new file mode 100644 index 0000000000..181f3b9ef7 --- /dev/null +++ b/gearsyncd/tests/cunit/Basic.c @@ -0,0 +1,334 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2004-2006 Jerry St.Clair, Anil Kumar + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation for basic test runner interface. + * + * 11-Aug-2004 Initial implementation of basic test runner interface. (JDS) + * + * 8-Jan-2005 Fixed reporting bug (bug report cunit-Bugs-1093861). (JDS) + * + * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Basic interface with output to stdout. + */ +/** @addtogroup Basic + * @{ + */ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "Util.h" +#include "TestRun.h" +#include "Basic.h" +#include "CUnit_intl.h" + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +/** Pointer to the currently running suite. */ +CU_pSuite f_pRunningSuite = NULL; +/** Current run mode. */ +static CU_BasicRunMode f_run_mode = CU_BRM_NORMAL; + +/*================================================================= + * Forward declaration of module functions * + *=================================================================*/ +CU_ErrorCode basic_initialize(void); +static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry); +static CU_ErrorCode basic_run_suite(CU_pSuite pSuite); +static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest); + +static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); +static void basic_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailureList); +static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); +static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite); +static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +CU_ErrorCode CU_basic_run_tests(void) +{ + CU_ErrorCode error; + + if (NULL == CU_get_registry()) { + if (CU_BRM_SILENT != f_run_mode) + fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); + error = CUE_NOREGISTRY; + } + else if (CUE_SUCCESS == (error = basic_initialize())) + error = basic_run_all_tests(NULL); + + return error; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_basic_run_suite(CU_pSuite pSuite) +{ + CU_ErrorCode error; + + if (NULL == pSuite) + error = CUE_NOSUITE; + else if (CUE_SUCCESS == (error = basic_initialize())) + error = basic_run_suite(pSuite); + + return error; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest) +{ + CU_ErrorCode error; + + if (NULL == pSuite) + error = CUE_NOSUITE; + else if (NULL == pTest) + error = CUE_NOTEST; + else if (CUE_SUCCESS == (error = basic_initialize())) + error = basic_run_single_test(pSuite, pTest); + + return error; +} + +/*------------------------------------------------------------------------*/ +void CU_basic_set_mode(CU_BasicRunMode mode) +{ + f_run_mode = mode; +} + +/*------------------------------------------------------------------------*/ +CU_BasicRunMode CU_basic_get_mode(void) +{ + return f_run_mode; +} + +/*------------------------------------------------------------------------*/ +void CU_basic_show_failures(CU_pFailureRecord pFailure) +{ + int i; + + for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { + fprintf(stdout, "\n %d. %s:%u - %s", i, + (NULL != pFailure->strFileName) ? pFailure->strFileName : "", + pFailure->uiLineNumber, + (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); + } +} + +/*================================================================= + * Static module functions + *=================================================================*/ +/** Performs inialization actions for the basic interface. + * This includes setting output to unbuffered, printing a + * welcome message, and setting the test run handlers. + * @return An error code indicating the framework error condition. + */ +CU_ErrorCode basic_initialize(void) +{ + /* Unbuffered output so everything reaches the screen */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + CU_set_error(CUE_SUCCESS); + + if (CU_BRM_SILENT != f_run_mode) + fprintf(stdout, "\n\n %s" CU_VERSION + "\n %s\n\n", + _("CUnit - A unit testing framework for C - Version "), + _("http://cunit.sourceforge.net/")); + + CU_set_test_start_handler(basic_test_start_message_handler); + CU_set_test_complete_handler(basic_test_complete_message_handler); + CU_set_all_test_complete_handler(basic_all_tests_complete_message_handler); + CU_set_suite_init_failure_handler(basic_suite_init_failure_message_handler); + CU_set_suite_cleanup_failure_handler(basic_suite_cleanup_failure_message_handler); + + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +/** Runs all tests within the basic interface. + * If non-NULL, the test registry is changed to the specified registry + * before running the tests, and reset to the original registry when + * done. If NULL, the default CUnit test registry will be used. + * @param pRegistry The CU_pTestRegistry containing the tests + * to be run. If NULL, use the default registry. + * @return An error code indicating the error status + * during the test run. + */ +static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = NULL; + CU_ErrorCode result; + + f_pRunningSuite = NULL; + + if (NULL != pRegistry) + pOldRegistry = CU_set_registry(pRegistry); + result = CU_run_all_tests(); + if (NULL != pRegistry) + CU_set_registry(pOldRegistry); + return result; +} + +/*------------------------------------------------------------------------*/ +/** Runs a specified suite within the basic interface. + * @param pSuite The suite to be run (non-NULL). + * @return An error code indicating the error status + * during the test run. + */ +static CU_ErrorCode basic_run_suite(CU_pSuite pSuite) +{ + f_pRunningSuite = NULL; + return CU_run_suite(pSuite); +} + +/*------------------------------------------------------------------------*/ +/** Runs a single test for the specified suite within + * the console interface. + * @param pSuite The suite containing the test to be run (non-NULL). + * @param pTest The test to be run (non-NULL). + * @return An error code indicating the error status + * during the test run. + */ +static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest) +{ + f_pRunningSuite = NULL; + return CU_run_test(pSuite, pTest); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at start of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + */ +static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pTest); + + if (CU_BRM_VERBOSE == f_run_mode) { + assert(NULL != pTest->pName); + if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { + assert(NULL != pSuite->pName); + fprintf(stdout, "\n%s: %s", _("Suite"), pSuite->pName); + fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); + f_pRunningSuite = pSuite; + } + else { + fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); + } + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + * @param pFailure Pointer to the 1st failure record for this test. + */ +static void basic_test_complete_message_handler(const CU_pTest pTest, + const CU_pSuite pSuite, + const CU_pFailureRecord pFailureList) +{ + CU_pFailureRecord pFailure = pFailureList; + int i; + + assert(NULL != pSuite); + assert(NULL != pTest); + + if (NULL == pFailure) { + if (CU_BRM_VERBOSE == f_run_mode) { + fprintf(stdout, _("passed")); + } + } + else { + switch (f_run_mode) { + case CU_BRM_VERBOSE: + fprintf(stdout, _("FAILED")); + break; + case CU_BRM_NORMAL: + assert(NULL != pSuite->pName); + assert(NULL != pTest->pName); + fprintf(stdout, _("\nSuite %s, Test %s had failures:"), pSuite->pName, pTest->pName); + break; + default: /* gcc wants all enums covered. ok. */ + break; + } + if (CU_BRM_SILENT != f_run_mode) { + for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { + fprintf(stdout, "\n %d. %s:%u - %s", i, + (NULL != pFailure->strFileName) ? pFailure->strFileName : "", + pFailure->uiLineNumber, + (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); + } + } + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of all tests in a suite. + * @param pFailure Pointer to the test failure record list. + */ +static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) +{ + CU_UNREFERENCED_PARAMETER(pFailure); /* not used in basic interface */ + printf("\n\n"); + CU_print_run_results(stdout); + printf("\n"); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite initialization fails. + * @param pSuite The suite for which initialization failed. + */ +static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + if (CU_BRM_SILENT != f_run_mode) + fprintf(stdout, _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite cleanup fails. + * @param pSuite The suite for which cleanup failed. + */ +static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + if (CU_BRM_SILENT != f_run_mode) + fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); +} + +/** @} */ diff --git a/gearsyncd/tests/cunit/Basic.cpp b/gearsyncd/tests/cunit/Basic.cpp new file mode 100644 index 0000000000..181f3b9ef7 --- /dev/null +++ b/gearsyncd/tests/cunit/Basic.cpp @@ -0,0 +1,334 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2004-2006 Jerry St.Clair, Anil Kumar + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation for basic test runner interface. + * + * 11-Aug-2004 Initial implementation of basic test runner interface. (JDS) + * + * 8-Jan-2005 Fixed reporting bug (bug report cunit-Bugs-1093861). (JDS) + * + * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Basic interface with output to stdout. + */ +/** @addtogroup Basic + * @{ + */ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "Util.h" +#include "TestRun.h" +#include "Basic.h" +#include "CUnit_intl.h" + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +/** Pointer to the currently running suite. */ +CU_pSuite f_pRunningSuite = NULL; +/** Current run mode. */ +static CU_BasicRunMode f_run_mode = CU_BRM_NORMAL; + +/*================================================================= + * Forward declaration of module functions * + *=================================================================*/ +CU_ErrorCode basic_initialize(void); +static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry); +static CU_ErrorCode basic_run_suite(CU_pSuite pSuite); +static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest); + +static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); +static void basic_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailureList); +static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); +static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite); +static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +CU_ErrorCode CU_basic_run_tests(void) +{ + CU_ErrorCode error; + + if (NULL == CU_get_registry()) { + if (CU_BRM_SILENT != f_run_mode) + fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); + error = CUE_NOREGISTRY; + } + else if (CUE_SUCCESS == (error = basic_initialize())) + error = basic_run_all_tests(NULL); + + return error; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_basic_run_suite(CU_pSuite pSuite) +{ + CU_ErrorCode error; + + if (NULL == pSuite) + error = CUE_NOSUITE; + else if (CUE_SUCCESS == (error = basic_initialize())) + error = basic_run_suite(pSuite); + + return error; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest) +{ + CU_ErrorCode error; + + if (NULL == pSuite) + error = CUE_NOSUITE; + else if (NULL == pTest) + error = CUE_NOTEST; + else if (CUE_SUCCESS == (error = basic_initialize())) + error = basic_run_single_test(pSuite, pTest); + + return error; +} + +/*------------------------------------------------------------------------*/ +void CU_basic_set_mode(CU_BasicRunMode mode) +{ + f_run_mode = mode; +} + +/*------------------------------------------------------------------------*/ +CU_BasicRunMode CU_basic_get_mode(void) +{ + return f_run_mode; +} + +/*------------------------------------------------------------------------*/ +void CU_basic_show_failures(CU_pFailureRecord pFailure) +{ + int i; + + for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { + fprintf(stdout, "\n %d. %s:%u - %s", i, + (NULL != pFailure->strFileName) ? pFailure->strFileName : "", + pFailure->uiLineNumber, + (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); + } +} + +/*================================================================= + * Static module functions + *=================================================================*/ +/** Performs inialization actions for the basic interface. + * This includes setting output to unbuffered, printing a + * welcome message, and setting the test run handlers. + * @return An error code indicating the framework error condition. + */ +CU_ErrorCode basic_initialize(void) +{ + /* Unbuffered output so everything reaches the screen */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + CU_set_error(CUE_SUCCESS); + + if (CU_BRM_SILENT != f_run_mode) + fprintf(stdout, "\n\n %s" CU_VERSION + "\n %s\n\n", + _("CUnit - A unit testing framework for C - Version "), + _("http://cunit.sourceforge.net/")); + + CU_set_test_start_handler(basic_test_start_message_handler); + CU_set_test_complete_handler(basic_test_complete_message_handler); + CU_set_all_test_complete_handler(basic_all_tests_complete_message_handler); + CU_set_suite_init_failure_handler(basic_suite_init_failure_message_handler); + CU_set_suite_cleanup_failure_handler(basic_suite_cleanup_failure_message_handler); + + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +/** Runs all tests within the basic interface. + * If non-NULL, the test registry is changed to the specified registry + * before running the tests, and reset to the original registry when + * done. If NULL, the default CUnit test registry will be used. + * @param pRegistry The CU_pTestRegistry containing the tests + * to be run. If NULL, use the default registry. + * @return An error code indicating the error status + * during the test run. + */ +static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = NULL; + CU_ErrorCode result; + + f_pRunningSuite = NULL; + + if (NULL != pRegistry) + pOldRegistry = CU_set_registry(pRegistry); + result = CU_run_all_tests(); + if (NULL != pRegistry) + CU_set_registry(pOldRegistry); + return result; +} + +/*------------------------------------------------------------------------*/ +/** Runs a specified suite within the basic interface. + * @param pSuite The suite to be run (non-NULL). + * @return An error code indicating the error status + * during the test run. + */ +static CU_ErrorCode basic_run_suite(CU_pSuite pSuite) +{ + f_pRunningSuite = NULL; + return CU_run_suite(pSuite); +} + +/*------------------------------------------------------------------------*/ +/** Runs a single test for the specified suite within + * the console interface. + * @param pSuite The suite containing the test to be run (non-NULL). + * @param pTest The test to be run (non-NULL). + * @return An error code indicating the error status + * during the test run. + */ +static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest) +{ + f_pRunningSuite = NULL; + return CU_run_test(pSuite, pTest); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at start of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + */ +static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pTest); + + if (CU_BRM_VERBOSE == f_run_mode) { + assert(NULL != pTest->pName); + if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { + assert(NULL != pSuite->pName); + fprintf(stdout, "\n%s: %s", _("Suite"), pSuite->pName); + fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); + f_pRunningSuite = pSuite; + } + else { + fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); + } + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + * @param pFailure Pointer to the 1st failure record for this test. + */ +static void basic_test_complete_message_handler(const CU_pTest pTest, + const CU_pSuite pSuite, + const CU_pFailureRecord pFailureList) +{ + CU_pFailureRecord pFailure = pFailureList; + int i; + + assert(NULL != pSuite); + assert(NULL != pTest); + + if (NULL == pFailure) { + if (CU_BRM_VERBOSE == f_run_mode) { + fprintf(stdout, _("passed")); + } + } + else { + switch (f_run_mode) { + case CU_BRM_VERBOSE: + fprintf(stdout, _("FAILED")); + break; + case CU_BRM_NORMAL: + assert(NULL != pSuite->pName); + assert(NULL != pTest->pName); + fprintf(stdout, _("\nSuite %s, Test %s had failures:"), pSuite->pName, pTest->pName); + break; + default: /* gcc wants all enums covered. ok. */ + break; + } + if (CU_BRM_SILENT != f_run_mode) { + for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { + fprintf(stdout, "\n %d. %s:%u - %s", i, + (NULL != pFailure->strFileName) ? pFailure->strFileName : "", + pFailure->uiLineNumber, + (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); + } + } + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of all tests in a suite. + * @param pFailure Pointer to the test failure record list. + */ +static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) +{ + CU_UNREFERENCED_PARAMETER(pFailure); /* not used in basic interface */ + printf("\n\n"); + CU_print_run_results(stdout); + printf("\n"); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite initialization fails. + * @param pSuite The suite for which initialization failed. + */ +static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + if (CU_BRM_SILENT != f_run_mode) + fprintf(stdout, _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite cleanup fails. + * @param pSuite The suite for which cleanup failed. + */ +static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + if (CU_BRM_SILENT != f_run_mode) + fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); +} + +/** @} */ diff --git a/gearsyncd/tests/cunit/Basic.h b/gearsyncd/tests/cunit/Basic.h new file mode 100644 index 0000000000..d8a638de3e --- /dev/null +++ b/gearsyncd/tests/cunit/Basic.h @@ -0,0 +1,113 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2004-2006 Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Interface for simple test runner. + * + * 11-Aug-2004 Initial implementation of basic test runner interface. (JDS) + */ + +/** @file + * Basic interface with output to stdout. + */ +/** @addtogroup Basic + * @{ + */ + +#ifndef CUNIT_BASIC_H_SEEN +#define CUNIT_BASIC_H_SEEN + +#include "CUnit.h" +#include "TestDB.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** Run modes for the basic interface. */ +typedef enum { + CU_BRM_NORMAL = 0, /**< Normal mode - failures and run summary are printed [default]. */ + CU_BRM_SILENT, /**< Silent mode - no output is printed except framework error messages. */ + CU_BRM_VERBOSE /**< Verbose mode - maximum output of run details. */ +} CU_BasicRunMode; + +CU_EXPORT CU_ErrorCode CU_basic_run_tests(void); +/**< + * Runs all registered CUnit tests using the basic interface. + * The default CU_BasicRunMode is used unless it has been + * previously changed using CU_basic_set_mode(). The CUnit test + * registry must have been initialized before calling this function. + * + * @return A CU_ErrorCode indicating the framework error condition, including + * CUE_NOREGISTRY - Registry has not been initialized. + */ + +CU_EXPORT CU_ErrorCode CU_basic_run_suite(CU_pSuite pSuite); +/**< + * Runs all tests for a specific suite in the basic interface. + * If pSuite is NULL, the function returns without taking any + * action. The default CU_BasicRunMode is used unless it has + * been changed using CU_basic_set_mode(). + * + * @param pSuite The CU_Suite to run. + * @return A CU_ErrorCode indicating the framework error condition, including + * CUE_NOSUITE - pSuite was NULL. + */ + +CU_EXPORT CU_ErrorCode CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest); +/**< + * Runs a single test in a specific suite in the basic interface. + * If pSuite or pTest is NULL, the function returns without + * taking any action. The default CU_BasicRunMode is used unless + * it has been changed using CU_basic_set_mode. + * + * @param pSuite The CU_Suite holding the CU_Test to run. + * @param pTest The CU_Test to run. + * @return A CU_ErrorCode indicating the framework error condition, including + * CUE_NOSUITE - pSuite was NULL. + * CUE_NOTEST - pTest was NULL. + */ + +CU_EXPORT void CU_basic_set_mode(CU_BasicRunMode mode); +/**< Sets the run mode for the basic interface. + * @param mode The new CU_BasicRunMode for subsequent test + * runs using the basic interface. + */ + +CU_EXPORT CU_BasicRunMode CU_basic_get_mode(void); +/**< Retrieves the current run mode for the basic interface. + * @return The current CU_BasicRunMode setting for test + * runs using the basic interface. + */ + +CU_EXPORT void CU_basic_show_failures(CU_pFailureRecord pFailure); +/**< + * Prints a summary of run failures to stdout. + * This is provided for user convenience upon request, and does + * not take into account the current run mode. The failures are + * printed to stdout independent of the most recent run mode. + * + * @param pFailure List of CU_pFailureRecord's to output. + */ + +#ifdef __cplusplus +} +#endif +#endif /* CUNIT_BASIC_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/cunit/CUError.c b/gearsyncd/tests/cunit/CUError.c new file mode 100644 index 0000000000..41f9bc3207 --- /dev/null +++ b/gearsyncd/tests/cunit/CUError.c @@ -0,0 +1,231 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Error handling code used by CUnit + * + * 16-Jul-2004 Created access functions for error code, error action + * functions, messages for new error codes. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Error handling functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include + +#include "CUnit_intl.h" +#include "CUError.h" + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +/** Local variable holding the current error code. */ +static CU_ErrorCode g_error_number = CUE_SUCCESS; +/** Local variable holding the current error action code. */ +static CU_ErrorAction g_error_action = CUEA_IGNORE; + +/*================================================================= + * Private function forward declarations + *=================================================================*/ +static const char* get_error_desc(CU_ErrorCode error); + +#ifdef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS +void test_exit(int status); +#endif + +/*================================================================= + * Public API functions + *=================================================================*/ +void CU_set_error(CU_ErrorCode error) +{ + if ((error != CUE_SUCCESS) && (g_error_action == CUEA_ABORT)) { +#ifndef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS + fprintf(stderr, _("\nAborting due to error #%d: %s\n"), + (int)error, + get_error_desc(error)); + exit((int)error); +#else + test_exit(error); +#endif + } + + g_error_number = error; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_get_error(void) +{ + return g_error_number; +} + +/*------------------------------------------------------------------------*/ +const char* CU_get_error_msg(void) +{ + return get_error_desc(g_error_number); +} + +/*------------------------------------------------------------------------*/ +void CU_set_error_action(CU_ErrorAction action) +{ + g_error_action = action; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorAction CU_get_error_action(void) +{ + return g_error_action; +} + +/*================================================================= + * Private static function definitions + *=================================================================*/ +/** Internal function to look up the error message for a specified + * error code. An empty string is returned if iError is not a member + * of CU_ErrorCode. If you add an error code to enum CU_ErrorCode, + * be sure to add a corresponding error message here. + * + * @param iError CU_ErrorCode to look up. + * @return Pointer to a string containing the error message. + * @see CU_get_error_msg() + */ +static const char* get_error_desc(CU_ErrorCode iError) +{ + int iMaxIndex; + + static const char* ErrorDescription[] = { + N_("No Error."), /* CUE_SUCCESS - 0 */ + N_("Memory allocation failed."), /* CUE_NOMEMORY - 1 */ + "", + "", + "", + "", + "", + "", + "", + "", + N_("Test registry does not exist."), /* CUE_NOREGISTRY - 10 */ + N_("Registry already exists."), /* CUE_REGISTRY_EXISTS - 11 */ + "", + "", + "", + "", + "", + "", + "", + "", + N_("NULL suite not allowed."), /* CUE_NOSUITE - 20 */ + N_("Suite name cannot be NULL."), /* CUE_NO_SUITENAME - 21 */ + N_("Suite initialization function failed."), /* CUE_SINIT_FAILED - 22 */ + N_("Suite cleanup function failed."), /* CUE_SCLEAN_FAILED - 23 */ + N_("Suite having name already registered."), /* CUE_DUP_SUITE - 24 */ + N_("Requested suite is not active."), /* CUE_SUITE_INACTIVE - 25 */ + "", + "", + "", + "", + N_("NULL test or test function not allowed."),/* CUE_NOTEST - 30 */ + N_("Test name cannot be NULL."), /* CUE_NO_TESTNAME - 31 */ + N_("Test having this name already in suite."),/* CUE_DUP_TEST - 32 */ + N_("Test not registered in specified suite."),/* CUE_TEST_NOT_IN_SUITE - 33 */ + N_("Requested test is not active"), /* CUE_TEST_INACTIVE - 34 */ + "", + "", + "", + "", + "", + N_("Error opening file."), /* CUE_FOPEN_FAILED - 40 */ + N_("Error closing file."), /* CUE_FCLOSE_FAILED - 41 */ + N_("Bad file name."), /* CUE_BAD_FILENAME - 42 */ + N_("Error during write to file."), /* CUE_WRITE_ERROR - 43 */ + N_("Undefined Error") + }; + + iMaxIndex = (int)(sizeof(ErrorDescription)/sizeof(char *) - 1); + if ((int)iError < 0) { + return _(ErrorDescription[0]); + } + else if ((int)iError > iMaxIndex) { + return _(ErrorDescription[iMaxIndex]); + } + else { + return _(ErrorDescription[(int)iError]); + } +} + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +void test_cunit_CUError(void) +{ + CU_ErrorCode old_err = CU_get_error(); + CU_ErrorAction old_action = CU_get_error_action(); + + test_cunit_start_tests("CUError.c"); + + /* CU_set_error() & CU_get_error() */ + CU_set_error(CUE_NOMEMORY); + TEST(CU_get_error() != CUE_SUCCESS); + TEST(CU_get_error() == CUE_NOMEMORY); + + CU_set_error(CUE_NOREGISTRY); + TEST(CU_get_error() != CUE_SUCCESS); + TEST(CU_get_error() == CUE_NOREGISTRY); + + /* CU_get_error_msg() */ + CU_set_error(CUE_SUCCESS); + TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_SUCCESS))); + + CU_set_error(CUE_NOTEST); + TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOTEST))); + + CU_set_error(CUE_NOMEMORY); + TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOMEMORY))); + TEST(strcmp(CU_get_error_msg(), get_error_desc(CUE_SCLEAN_FAILED))); + + TEST(!strcmp(get_error_desc(100), "Undefined Error")); + + /* CU_set_error_action() & CU_get_error_action() */ + CU_set_error_action(CUEA_FAIL); + TEST(CU_get_error_action() != CUEA_IGNORE); + TEST(CU_get_error_action() == CUEA_FAIL); + TEST(CU_get_error_action() != CUEA_ABORT); + + CU_set_error_action(CUEA_ABORT); + TEST(CU_get_error_action() != CUEA_IGNORE); + TEST(CU_get_error_action() != CUEA_FAIL); + TEST(CU_get_error_action() == CUEA_ABORT); + + /* reset values */ + CU_set_error(old_err); + CU_set_error_action(old_action); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/CUError.cpp b/gearsyncd/tests/cunit/CUError.cpp new file mode 100644 index 0000000000..41f9bc3207 --- /dev/null +++ b/gearsyncd/tests/cunit/CUError.cpp @@ -0,0 +1,231 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Error handling code used by CUnit + * + * 16-Jul-2004 Created access functions for error code, error action + * functions, messages for new error codes. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Error handling functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include + +#include "CUnit_intl.h" +#include "CUError.h" + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +/** Local variable holding the current error code. */ +static CU_ErrorCode g_error_number = CUE_SUCCESS; +/** Local variable holding the current error action code. */ +static CU_ErrorAction g_error_action = CUEA_IGNORE; + +/*================================================================= + * Private function forward declarations + *=================================================================*/ +static const char* get_error_desc(CU_ErrorCode error); + +#ifdef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS +void test_exit(int status); +#endif + +/*================================================================= + * Public API functions + *=================================================================*/ +void CU_set_error(CU_ErrorCode error) +{ + if ((error != CUE_SUCCESS) && (g_error_action == CUEA_ABORT)) { +#ifndef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS + fprintf(stderr, _("\nAborting due to error #%d: %s\n"), + (int)error, + get_error_desc(error)); + exit((int)error); +#else + test_exit(error); +#endif + } + + g_error_number = error; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_get_error(void) +{ + return g_error_number; +} + +/*------------------------------------------------------------------------*/ +const char* CU_get_error_msg(void) +{ + return get_error_desc(g_error_number); +} + +/*------------------------------------------------------------------------*/ +void CU_set_error_action(CU_ErrorAction action) +{ + g_error_action = action; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorAction CU_get_error_action(void) +{ + return g_error_action; +} + +/*================================================================= + * Private static function definitions + *=================================================================*/ +/** Internal function to look up the error message for a specified + * error code. An empty string is returned if iError is not a member + * of CU_ErrorCode. If you add an error code to enum CU_ErrorCode, + * be sure to add a corresponding error message here. + * + * @param iError CU_ErrorCode to look up. + * @return Pointer to a string containing the error message. + * @see CU_get_error_msg() + */ +static const char* get_error_desc(CU_ErrorCode iError) +{ + int iMaxIndex; + + static const char* ErrorDescription[] = { + N_("No Error."), /* CUE_SUCCESS - 0 */ + N_("Memory allocation failed."), /* CUE_NOMEMORY - 1 */ + "", + "", + "", + "", + "", + "", + "", + "", + N_("Test registry does not exist."), /* CUE_NOREGISTRY - 10 */ + N_("Registry already exists."), /* CUE_REGISTRY_EXISTS - 11 */ + "", + "", + "", + "", + "", + "", + "", + "", + N_("NULL suite not allowed."), /* CUE_NOSUITE - 20 */ + N_("Suite name cannot be NULL."), /* CUE_NO_SUITENAME - 21 */ + N_("Suite initialization function failed."), /* CUE_SINIT_FAILED - 22 */ + N_("Suite cleanup function failed."), /* CUE_SCLEAN_FAILED - 23 */ + N_("Suite having name already registered."), /* CUE_DUP_SUITE - 24 */ + N_("Requested suite is not active."), /* CUE_SUITE_INACTIVE - 25 */ + "", + "", + "", + "", + N_("NULL test or test function not allowed."),/* CUE_NOTEST - 30 */ + N_("Test name cannot be NULL."), /* CUE_NO_TESTNAME - 31 */ + N_("Test having this name already in suite."),/* CUE_DUP_TEST - 32 */ + N_("Test not registered in specified suite."),/* CUE_TEST_NOT_IN_SUITE - 33 */ + N_("Requested test is not active"), /* CUE_TEST_INACTIVE - 34 */ + "", + "", + "", + "", + "", + N_("Error opening file."), /* CUE_FOPEN_FAILED - 40 */ + N_("Error closing file."), /* CUE_FCLOSE_FAILED - 41 */ + N_("Bad file name."), /* CUE_BAD_FILENAME - 42 */ + N_("Error during write to file."), /* CUE_WRITE_ERROR - 43 */ + N_("Undefined Error") + }; + + iMaxIndex = (int)(sizeof(ErrorDescription)/sizeof(char *) - 1); + if ((int)iError < 0) { + return _(ErrorDescription[0]); + } + else if ((int)iError > iMaxIndex) { + return _(ErrorDescription[iMaxIndex]); + } + else { + return _(ErrorDescription[(int)iError]); + } +} + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +void test_cunit_CUError(void) +{ + CU_ErrorCode old_err = CU_get_error(); + CU_ErrorAction old_action = CU_get_error_action(); + + test_cunit_start_tests("CUError.c"); + + /* CU_set_error() & CU_get_error() */ + CU_set_error(CUE_NOMEMORY); + TEST(CU_get_error() != CUE_SUCCESS); + TEST(CU_get_error() == CUE_NOMEMORY); + + CU_set_error(CUE_NOREGISTRY); + TEST(CU_get_error() != CUE_SUCCESS); + TEST(CU_get_error() == CUE_NOREGISTRY); + + /* CU_get_error_msg() */ + CU_set_error(CUE_SUCCESS); + TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_SUCCESS))); + + CU_set_error(CUE_NOTEST); + TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOTEST))); + + CU_set_error(CUE_NOMEMORY); + TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOMEMORY))); + TEST(strcmp(CU_get_error_msg(), get_error_desc(CUE_SCLEAN_FAILED))); + + TEST(!strcmp(get_error_desc(100), "Undefined Error")); + + /* CU_set_error_action() & CU_get_error_action() */ + CU_set_error_action(CUEA_FAIL); + TEST(CU_get_error_action() != CUEA_IGNORE); + TEST(CU_get_error_action() == CUEA_FAIL); + TEST(CU_get_error_action() != CUEA_ABORT); + + CU_set_error_action(CUEA_ABORT); + TEST(CU_get_error_action() != CUEA_IGNORE); + TEST(CU_get_error_action() != CUEA_FAIL); + TEST(CU_get_error_action() == CUEA_ABORT); + + /* reset values */ + CU_set_error(old_err); + CU_set_error_action(old_action); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/CUError.h b/gearsyncd/tests/cunit/CUError.h new file mode 100644 index 0000000000..c9943c1245 --- /dev/null +++ b/gearsyncd/tests/cunit/CUError.h @@ -0,0 +1,199 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Contains CUnit error codes which can be used externally. + * + * Aug 2001 Initial implementation. (AK) + * + * 02/Oct/2001 Added proper Eror Codes. (AK) + * + * 13-Oct-2001 Added Error Codes for Duplicate TestGroup and Test. (AK) + * + * 03-Aug-2004 Converted error code macros to an enum, doxygen comments, moved + * error handing code here, changed file name from Errno.h, added + * error codes for file open errors, added error action selection. (JDS) + * + * 05-Sep-2004 Added internal test interface. (JDS) + */ + +/** @file + * Error handling functions (user interface). + * CUnit uses a simple (and conventional) error handling strategy. + * Functions that can generate errors set (and usually return) an + * error code to indicate the run status. The error code can be + * inspected using the CU_get_error() function. A descriptive + * error message can be retrieved using CU_get_error_msg(). + */ +/** @addtogroup Framework + * @{ + */ + +#ifndef CUNIT_CUERROR_H_SEEN +#define CUNIT_CUERROR_H_SEEN + +#include + +/*------------------------------------------------------------------------*/ +/** CUnit error codes. + * If codes are added or removed, be sure to make a change to the + * error messages in CUError.c/get_error_desc(). + * @see CU_set_error() + * @see CU_get_error() + * @see CU_get_error_msg() + */ +typedef enum { + /* basic errors */ + CUE_SUCCESS = 0, /**< No error condition. */ + CUE_NOMEMORY = 1, /**< Memory allocation failed. */ + + /* Test Registry Level Errors */ + CUE_NOREGISTRY = 10, /**< Test registry not initialized. */ + CUE_REGISTRY_EXISTS = 11, /**< Attempt to CU_set_registry() without CU_cleanup_registry(). */ + + /* Test Suite Level Errors */ + CUE_NOSUITE = 20, /**< A required CU_pSuite pointer was NULL. */ + CUE_NO_SUITENAME = 21, /**< Required CU_Suite name not provided. */ + CUE_SINIT_FAILED = 22, /**< Suite initialization failed. */ + CUE_SCLEAN_FAILED = 23, /**< Suite cleanup failed. */ + CUE_DUP_SUITE = 24, /**< Duplicate suite name not allowed. */ + CUE_SUITE_INACTIVE = 25, /**< Test run initiated for an inactive suite. */ + + /* Test Case Level Errors */ + CUE_NOTEST = 30, /**< A required CU_pTest or CU_TestFunc pointer was NULL. */ + CUE_NO_TESTNAME = 31, /**< Required CU_Test name not provided. */ + CUE_DUP_TEST = 32, /**< Duplicate test case name not allowed. */ + CUE_TEST_NOT_IN_SUITE = 33, /**< Test not registered in specified suite. */ + CUE_TEST_INACTIVE = 34, /**< Test run initiated for an inactive test. */ + + /* File handling errors */ + CUE_FOPEN_FAILED = 40, /**< An error occurred opening a file. */ + CUE_FCLOSE_FAILED = 41, /**< An error occurred closing a file. */ + CUE_BAD_FILENAME = 42, /**< A bad filename was requested (NULL, empty, nonexistent, etc.). */ + CUE_WRITE_ERROR = 43 /**< An error occurred during a write to a file. */ +} CU_ErrorCode; + +/*------------------------------------------------------------------------*/ +/** CUnit error action codes. + * These are used to set the action desired when an error + * condition is detected in the CUnit framework. + * @see CU_set_error_action() + * @see CU_get_error_action() + */ +typedef enum CU_ErrorAction { + CUEA_IGNORE, /**< Runs should be continued when an error condition occurs (if possible). */ + CUEA_FAIL, /**< Runs should be stopped when an error condition occurs. */ + CUEA_ABORT /**< The application should exit() when an error conditions occurs. */ +} CU_ErrorAction; + +/* Error handling & reporting functions. */ + +#include "CUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +CU_EXPORT CU_ErrorCode CU_get_error(void); +/**< + * Retrieves the current CUnit framework error code. + * CUnit implementation functions set the error code to indicate the + * status of the most recent operation. In general, the CUnit functions + * will clear the code to CUE_SUCCESS, then reset it to a specific error + * code if an exception condition is encountered. Some functions + * return the code, others leave it to the user to inspect if desired. + * + * @return The current error condition code. + * @see CU_get_error_msg() + * @see CU_ErrorCode + */ + +CU_EXPORT const char* CU_get_error_msg(void); +/**< + * Retrieves a message corresponding to the current framework error code. + * CUnit implementation functions set the error code to indicate the + * of the most recent operation. In general, the CUnit functions will + * clear the code to CUE_SUCCESS, then reset it to a specific error + * code if an exception condition is encountered. This function allows + * the user to retrieve a descriptive error message corresponding to the + * error code set by the last operation. + * + * @return A message corresponding to the current error condition. + * @see CU_get_error() + * @see CU_ErrorCode + */ + +CU_EXPORT void CU_set_error_action(CU_ErrorAction action); +/**< + * Sets the action to take when a framework error condition occurs. + * This function should be used to specify the action to take + * when an error condition is encountered. The default action is + * CUEA_IGNORE, which results in errors being ignored and test runs + * being continued (if possible). A value of CUEA_FAIL causes test + * runs to stop as soon as an error condition occurs, while + * CU_ABORT causes the application to exit on any error. + * + * @param action CU_ErrorAction indicating the new error action. + * @see CU_get_error_action() + * @see CU_set_error() + * @see CU_ErrorAction + */ + +CU_EXPORT CU_ErrorAction CU_get_error_action(void); +/**< + * Retrieves the current framework error action code. + * + * @return The current error action code. + * @see CU_set_error_action() + * @see CU_set_error() + * @see CU_ErrorAction + */ + +#ifdef CUNIT_BUILD_TESTS +void test_cunit_CUError(void); +#endif + +/* Internal function - users should not generally call this function */ +CU_EXPORT void CU_set_error(CU_ErrorCode error); +/**< + * Sets the CUnit framework error code. + * This function is used internally by CUnit implementation functions + * when an error condition occurs within the framework. It should + * not generally be called by user code. NOTE that if the current + * error action is CUEA_ABORT, then calling this function will + * result in exit() being called for the current application. + * + * @param error CU_ErrorCode indicating the current error condition. + * @see CU_get_error() + * @see CU_get_error_msg() + * @see CU_ErrorCode + */ + +#ifdef __cplusplus +} +#endif + +#ifdef USE_DEPRECATED_CUNIT_NAMES +/** Deprecated (version 1). @deprecated Use CU_get_error_msg(). */ +#define get_error() CU_get_error_msg() +#endif /* USE_DEPRECATED_CUNIT_NAMES */ + +#endif /* CUNIT_CUERROR_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/cunit/CUnit.h b/gearsyncd/tests/cunit/CUnit.h new file mode 100644 index 0000000000..9592eda50c --- /dev/null +++ b/gearsyncd/tests/cunit/CUnit.h @@ -0,0 +1,383 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * ASSERT Macro definitions and general CUnit configuration definitions. + * + * 09/Aug/2001 ASSERT definitions. (AK) + * + * 12/Mar/2003 New Assert definitions. (AK) + * + * 27/Jul/2003 Modified ASSERT_XXX Macro definitions. (AK) + * + * 15-Jul-2004 New interface, changed action on assert failure to not + * return, provided _FATAL versions of assertions to return + * from test function on failure. (JDS) + * + * 01-Sep-2004 Modified assertions for setjmp/longjmp mechanism of + * aborting test runs, added CU_FAIL and CU_PASS macros. (JDS) + * + * 07-May-2005 Added CU_ prefix to remaining CUnit defines (BOOL, TRUE, + * FALSE, MAX_...). Added CU_UNREFERENCED_PARAMETER() define. (JDS) + */ + +/** @file + * Basic CUnit include file for user and system code. + * Defines macros for assertions for use in user test cases. + * Basic system macro definitions also appear here. + */ +/** @addtogroup Framework + * @{ + */ + +#ifndef CUNIT_CUNIT_H_SEEN +#define CUNIT_CUNIT_H_SEEN + +#include +#include + +/** CUnit version number. */ +#define CU_VERSION "2.1-2" + +/* Max string lengths for names (includes terminating NULL. */ +/** Maximum length of a test name string. */ +#define CU_MAX_TEST_NAME_LENGTH 256 +/** Maximim length of a suite name string. */ +#define CU_MAX_SUITE_NAME_LENGTH 256 + +/* Global type Definitions to be used for boolean operators. */ +#ifndef CU_BOOL + /** Boolean type for CUnit use. */ + #define CU_BOOL int +#endif + +#ifndef CU_TRUE + /** Boolean TRUE for CUnit use. */ + #define CU_TRUE 1 +#endif + +#ifndef CU_FALSE + /** Boolean FALSE for CUnit use. */ + #define CU_FALSE 0 +#endif + +#ifndef CU_UNREFERENCED_PARAMETER + /** Consistent approach to referencing unused parameters. */ + #define CU_UNREFERENCED_PARAMETER(x) (void)x +#endif + +#ifndef CU_MAX +# define CU_MAX(a,b) (((a) >= (b)) ? (a) : (b)) +#endif + +#ifndef CU_MIN +# define CU_MIN(a,b) (((a) >= (b)) ? (b) : (a)) +#endif + +#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) +# ifdef CU_DLL +# ifdef CU_BUILD_DLL +# define CU_EXPORT __declspec(dllexport) +# else +# define CU_EXPORT __declspec(dllimport) +# endif +# else +# define CU_EXPORT +# endif +# ifdef _MSC_VER +# define snprintf _snprintf +# endif +#else +# define CU_EXPORT +#endif /* WIN32 */ + +#include "CUError.h" +#include "TestDB.h" /* not needed here - included for user convenience */ +#include "TestRun.h" /* not needed here - include (after BOOL define) for user convenience */ + +/** Record a pass condition without performing a logical test. */ +#define CU_PASS(msg) \ + { CU_assertImplementation(CU_TRUE, __LINE__, ("CU_PASS(" #msg ")"), __FILE__, "", CU_FALSE); } + +/** Simple assertion. + * Reports failure with no other action. + */ +#define CU_ASSERT(value) \ + { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); } + +/** Simple assertion. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_FATAL(value) \ + { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); } + +/** Simple assertion. + * Reports failure with no other action. + */ +#define CU_TEST(value) \ + { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); } + +/** Simple assertion. + * Reports failure and causes test to abort. + */ +#define CU_TEST_FATAL(value) \ + { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); } + +/** Record a failure without performing a logical test. */ +#define CU_FAIL(msg) \ + { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL(" #msg ")"), __FILE__, "", CU_FALSE); } + +/** Record a failure without performing a logical test, and abort test. */ +#define CU_FAIL_FATAL(msg) \ + { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL_FATAL(" #msg ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that value is CU_TRUE. + * Reports failure with no other action. + */ +#define CU_ASSERT_TRUE(value) \ + { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE(" #value ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that value is CU_TRUE. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_TRUE_FATAL(value) \ + { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that value is CU_FALSE. + * Reports failure with no other action. + */ +#define CU_ASSERT_FALSE(value) \ + { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE(" #value ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that value is CU_FALSE. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_FALSE_FATAL(value) \ + { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that actual == expected. + * Reports failure with no other action. + */ +#define CU_ASSERT_EQUAL(actual, expected) \ + { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that actual == expected. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_EQUAL_FATAL(actual, expected) \ + { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that actual != expected. + * Reports failure with no other action. + */ +#define CU_ASSERT_NOT_EQUAL(actual, expected) \ + { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that actual != expected. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_NOT_EQUAL_FATAL(actual, expected) \ + { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that pointers actual == expected. + * Reports failure with no other action. + */ +#define CU_ASSERT_PTR_EQUAL(actual, expected) \ + { CU_assertImplementation(((void*)(actual) == (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that pointers actual == expected. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_PTR_EQUAL_FATAL(actual, expected) \ + { CU_assertImplementation(((void*)(actual) == (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that pointers actual != expected. + * Reports failure with no other action. + */ +#define CU_ASSERT_PTR_NOT_EQUAL(actual, expected) \ + { CU_assertImplementation(((void*)(actual) != (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that pointers actual != expected. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) \ + { CU_assertImplementation(((void*)(actual) != (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that pointer value is NULL. + * Reports failure with no other action. + */ +#define CU_ASSERT_PTR_NULL(value) \ + { CU_assertImplementation((NULL == (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL(" #value")"), __FILE__, "", CU_FALSE); } + +/** Asserts that pointer value is NULL. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_PTR_NULL_FATAL(value) \ + { CU_assertImplementation((NULL == (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); } + +/** Asserts that pointer value is not NULL. + * Reports failure with no other action. + */ +#define CU_ASSERT_PTR_NOT_NULL(value) \ + { CU_assertImplementation((NULL != (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", CU_FALSE); } + +/** Asserts that pointer value is not NULL. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_PTR_NOT_NULL_FATAL(value) \ + { CU_assertImplementation((NULL != (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); } + +/** Asserts that string actual == expected. + * Reports failure with no other action. + */ +#define CU_ASSERT_STRING_EQUAL(actual, expected) \ + { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that string actual == expected. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_STRING_EQUAL_FATAL(actual, expected) \ + { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that string actual != expected. + * Reports failure with no other action. + */ +#define CU_ASSERT_STRING_NOT_EQUAL(actual, expected) \ + { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that string actual != expected. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) \ + { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that string actual == expected with length specified. + * The comparison is limited to count characters. + * Reports failure with no other action. + */ +#define CU_ASSERT_NSTRING_EQUAL(actual, expected, count) \ + { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that string actual == expected with length specified. + * The comparison is limited to count characters. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) \ + { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL_FATAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that string actual != expected with length specified. + * The comparison is limited to count characters. + * Reports failure with no other action. + */ +#define CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) \ + { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that string actual != expected with length specified. + * The comparison is limited to count characters. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) \ + { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that double actual == expected within the specified tolerance. + * If actual is within granularity of expected, the assertion passes. + * Reports failure with no other action. + */ +#define CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) \ + { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that double actual == expected within the specified tolerance. + * If actual is within granularity of expected, the assertion passes. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) \ + { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE); } + +/** Asserts that double actual != expected within the specified tolerance. + * If actual is within granularity of expected, the assertion fails. + * Reports failure with no other action. + */ +#define CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) \ + { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE); } + +/** Asserts that double actual != expected within the specified tolerance. + * If actual is within granularity of expected, the assertion fails. + * Reports failure and causes test to abort. + */ +#define CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) \ + { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE); } + +#ifdef USE_DEPRECATED_CUNIT_NAMES + +#ifndef BOOL + /** Deprecated (version 2.0-2). @deprecated Use CU_BOOL. */ + #define BOOL int +#endif + +#ifndef TRUE + /** Deprecated (version 2.0-2). @deprecated Use CU_TRUE. */ + #define TRUE 1 +#endif + +#ifndef FALSE + /** Deprecated (version 2.0-2). @deprecated Use CU_FALSE. */ + #define FALSE 0 +#endif + +/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_TEST_NAME_LENGTH. */ +#define MAX_TEST_NAME_LENGTH 256 +/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_SUITE_NAME_LENGTH. */ +#define MAX_SUITE_NAME_LENGTH 256 + +/** Deprecated (version 1). @deprecated Use CU_ASSERT_FATAL. */ +#define ASSERT(value) { if (FALSE == (int)(value)) { CU_assertImplementation((BOOL)value, __LINE__, #value, __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_TRUE_FATAL. */ +#define ASSERT_TRUE(value) { if (FALSE == (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_TRUE(" #value ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_FALSE_FATAL. */ +#define ASSERT_FALSE(value) { if (FALSE != (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_FALSE(" #value ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_EQUAL_FATAL. */ +#define ASSERT_EQUAL(actual, expected) { if ((actual) != (expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_NOT_EQUAL_FATAL. */ +#define ASSERT_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_EQUAL_FATAL. */ +#define ASSERT_PTR_EQUAL(actual, expected) { if ((void*)(actual) != (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_EQUAL_FATAL. */ +#define ASSERT_PTR_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NULL_FATAL. */ +#define ASSERT_PTR_NULL(value) { if (NULL != (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NULL(" #value")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_NULL_FATAL. */ +#define ASSERT_PTR_NOT_NULL(value) { if (NULL == (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_EQUAL_FATAL. */ +#define ASSERT_STRING_EQUAL(actual, expected) { if (strcmp((const char*)actual, (const char*)expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_STRING_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_NOT_EQUAL_FATAL. */ +#define ASSERT_STRING_NOT_EQUAL(actual, expected) { if (!strcmp((const char*)actual, (const char*)expected)) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_STRING_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_EQUAL_FATAL. */ +#define ASSERT_NSTRING_EQUAL(actual, expected, count) { if (strncmp((const char*)actual, (const char*)expected, (size_t)count)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NSTRING_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_NOT_EQUAL_FATAL. */ +#define ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) { if (!strncmp((const char*)actual, (const char*)expected, (size_t)count)) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_NSTRING_NOT_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_EQUAL_FATAL. */ +#define ASSERT_DOUBLE_EQUAL(actual, expected, granularity) { if ((fabs((double)actual - expected) > fabs((double)granularity))) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_DOUBLE_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", FALSE); return; }} +/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL. */ +#define ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) { if ((fabs((double)actual - expected) <= fabs((double)granularity))) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_DOUBLE_NOT_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", FALSE); return; }} +#endif /* USE_DEPRECATED_CUNIT_NAMES */ + +#endif /* CUNIT_CUNIT_H_SEEN */ + +/** @} */ diff --git a/gearsyncd/tests/cunit/CUnit_intl.h b/gearsyncd/tests/cunit/CUnit_intl.h new file mode 100644 index 0000000000..813917b25c --- /dev/null +++ b/gearsyncd/tests/cunit/CUnit_intl.h @@ -0,0 +1,62 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2006 Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Internationalization support + * + * 05-May-2006 Initial implementation. (JDS) + */ + +/** @file + * Internal CUnit header supporting internationalization of + * CUnit framework & interfaces. + */ +/** @addtogroup Framework + * @{ + */ + +#ifndef CUNIT_CUNIT_INTL_H_SEEN +#define CUNIT_CUNIT_INTL_H_SEEN + +/* activate these when source preparation is complete +#include +#ifndef _ +# define _(String) gettext (String) +#endif +#ifndef gettext_noop +# define gettext_noop(String) String +#endif +#ifndef N_ +# define N_(String) gettext_noop (String) +#endif +*/ + +/* deactivate these when source preparation is complete */ +#undef _ +#define _(String) (String) +#undef N_ +#define N_(String) String +#undef textdomain +#define textdomain(Domain) +#undef bindtextdomain +#define bindtextdomain(Package, Directory) + +#endif /* CUNIT_CUNIT_INTL_H_SEEN */ + +/** @} */ diff --git a/gearsyncd/tests/cunit/Console.c b/gearsyncd/tests/cunit/Console.c new file mode 100644 index 0000000000..512e3e6987 --- /dev/null +++ b/gearsyncd/tests/cunit/Console.c @@ -0,0 +1,735 @@ + /* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of the Console Test Interface. + * + * Aug 2001 Initial implementation (AK) + * + * 19/Aug/2001 Added initial console interface functions without + * any run functionality. (AK) + * + * 24/Aug/2001 Added compare_strings, show_errors, list_suites, + * list_tests function declarations. (AK) + * + * 17-Jul-2004 New interface, doxygen comments, reformat console output. (JDS) + * + * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) + * + * 24-Apr-2006 Suite/test selection is now by number rather than name. + * Inactive suites/tests now reported. + * Interface supports (de)activation of tests/suites. + * Help function added for both menu levels. + * Option menu added. Immediate action on hotkeys + * without needing to , like curses. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Console test interface with interactive output (implementation). + */ +/** @addtogroup Console + @{ +*/ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "Util.h" +#include "TestRun.h" +#include "Console.h" +#include "CUnit_intl.h" + +extern int sai_driver_shell(); +extern int warmboot_enable, collect_syslog; + +/** Console interface status flag. */ +typedef enum +{ + CU_STATUS_CONTINUE = 1, /**< Continue processing commands in current menu. */ + CU_STATUS_MOVE_UP, /**< Move up to the previous menu. */ + CU_STATUS_STOP /**< Stop processing (user selected 'Quit'). */ +} CU_STATUS; + +/*================================================================= + * Global / Static data definitions + *=================================================================*/ +/** Pointer to the currently running suite. */ +static CU_pSuite f_pRunningSuite = NULL; + +/** Common width measurements for output formatting. */ +static size_t f_yes_width = 0; +static size_t f_no_width = 0; + +/*================================================================= + * Static function forward declarations + *=================================================================*/ +void console_registry_level_run(CU_pTestRegistry pRegistry); +static CU_STATUS console_suite_level_run(CU_pSuite pSuite); +static CU_STATUS console_set_options_run(void); + +static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry); +static CU_ErrorCode console_run_suite(CU_pSuite pSuite); +static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest); + +static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); +static void console_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); +static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); +static void console_suite_init_failure_message_handler(const CU_pSuite pSuite); +static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); + +static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest); +static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite); + +static void list_suites(CU_pTestRegistry pRegistry); +static void list_tests(CU_pSuite pSuite); +static void show_failures(void); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +void CU_console_run_tests(void) +{ + /* + * To avoid user from cribbing about the output not coming onto + * screen at the moment of SIGSEGV. + */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + fprintf(stdout, "\n\n %s" CU_VERSION + "\n %s\n", + _("CUnit - A Unit testing framework for C - Version "), + _("http://cunit.sourceforge.net/")); + + if (NULL == CU_get_registry()) { + fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); + CU_set_error(CUE_NOREGISTRY); + } + else { + f_yes_width = strlen(_("Yes")); + f_no_width = strlen(_("No")); + + CU_set_test_start_handler(console_test_start_message_handler); + CU_set_test_complete_handler(console_test_complete_message_handler); + CU_set_all_test_complete_handler(console_all_tests_complete_message_handler); + CU_set_suite_init_failure_handler(console_suite_init_failure_message_handler); + CU_set_suite_cleanup_failure_handler(console_suite_cleanup_failure_message_handler); + + console_registry_level_run(NULL); + } +} + +/*================================================================= + * Static function implementation + *=================================================================*/ +/** + * Main loop for console interface. + * Displays actions and responds based on user imput. If pRegistry + * is NULL, will use the default internal CUnit test registry. + * + * @param pRegistry The CU_pTestRegistry to use for testing. + */ +void console_registry_level_run(CU_pTestRegistry pRegistry) +{ + int chChoice; + CU_pSuite pSuite = NULL; + CU_STATUS eStatus = CU_STATUS_CONTINUE; + char szTemp[256]; + + while (CU_STATUS_CONTINUE == eStatus) + { + fprintf(stdout, "\n\n%s\n%s\n%s", + _("***************************** CUNIT CONSOLE - MAIN MENU *******************************************"), + _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), + _("Enter command: ")); + chChoice = toupper(getchar()); + if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ + + if (chChoice == _("R")[0]) { + console_run_all_tests(pRegistry); + } + + else if (chChoice == _("S")[0]) { + if (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { + assert(NULL != pSuite->pName); + fprintf(stdout, _("Suite '%s' selected."), pSuite->pName); + fprintf(stdout, "\n"); + if (CU_STATUS_STOP == console_suite_level_run(pSuite)) { + eStatus = CU_STATUS_STOP; + } + } + else { + fprintf(stdout, "\n%s\n", _("Suite not found.")); + } + } + + else if (chChoice == _("L")[0]) { + list_suites(pRegistry); + } + + else if (chChoice == _("A")[0]) { + while (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { + CU_set_suite_active(pSuite, (CU_FALSE == pSuite->fActive) ? CU_TRUE : CU_FALSE); + } + } + + else if (chChoice == _("F")[0]) { + show_failures(); + } + + else if (chChoice == _("O")[0]) { + console_set_options_run(); + } + + else if (chChoice == _("Q")[0]) { + eStatus = CU_STATUS_STOP; + } + + else if (chChoice == _("D")[0]) { + fprintf(stdout, "Hit enter to get drivshell prompt..\n"); + sai_driver_shell(); + } + + else if (chChoice == _("W")[0]) { + warmboot_enable = warmboot_enable ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", + warmboot_enable ? "enable" : "disable"); + } + + else if (chChoice == _("C")[0]) { + collect_syslog = collect_syslog ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", + collect_syslog ? "enable" : "disable"); + } + + else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { + fprintf(stdout, "\n%s\n", _("Commands: R - run all tests in all suites")); + fprintf(stdout, "%s\n", _(" S - Select a suite to run or modify")); + fprintf(stdout, "%s\n", _(" L - List all registered suites")); + fprintf(stdout, "%s\n", _(" A - Activate or deactivate a suite (toggle)")); + fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); + fprintf(stdout, "%s\n", _(" O - Set CUnit options")); + fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); + fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); + fprintf(stdout, "%s\n", _(" H - Show this help message")); + fprintf(stdout, "%s\n", _(" Q - Quit the application")); + } + } +} + +/*------------------------------------------------------------------------*/ +/** + * Runs a selected suite within the console interface. + * Displays actions and responds based on user imput. + * + * @param pSuite The suite to use for testing (non-NULL). + */ +static CU_STATUS console_suite_level_run(CU_pSuite pSuite) +{ + int chChoice; + CU_pTest pTest = NULL; + CU_STATUS eStatus = CU_STATUS_CONTINUE; + char szTemp[256]; + + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + while (CU_STATUS_CONTINUE == eStatus) { + + fprintf(stdout, "\n%s\n%s\n%s", + _("**************************** CUNIT CONSOLE - SUITE MENU ************************************************"), + _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), + _("Enter command: ")); + chChoice = toupper(getchar()); + if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ + + if (chChoice == _("R")[0]) { + console_run_suite(pSuite); + } + + else if (chChoice == _("S")[0]) { + if (CUE_SUCCESS == select_test(pSuite, &pTest)) { + console_run_single_test(pSuite, pTest); + } + else { + fprintf(stdout, "\n%s\n", _("Test not found.")); + } + } + + else if (chChoice == _("L")[0]) { + list_tests(pSuite); + } + + else if (chChoice == _("A")[0]) { + while (CUE_SUCCESS == select_test(pSuite, &pTest)) { + CU_set_test_active(pTest, (CU_FALSE == pTest->fActive) ? CU_TRUE : CU_FALSE); + } + } + + else if (chChoice == _("F")[0]) { + show_failures(); + } + + else if ((chChoice == _("M")[0]) || (chChoice == _("U")[0])) { + eStatus = CU_STATUS_MOVE_UP; + } + + else if (chChoice == _("O")[0]) { + console_set_options_run(); + } + + else if (chChoice == _("Q")[0]) { + eStatus = CU_STATUS_STOP; + } + + else if (chChoice == _("D")[0]) { + fprintf(stdout, "Hit enter to get drivshell prompt..\n"); + sai_driver_shell(); + } + + else if (chChoice == _("W")[0]) { + warmboot_enable = warmboot_enable ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", + warmboot_enable ? "enable" : "disable"); + } + + else if (chChoice == _("C")[0]) { + collect_syslog = collect_syslog ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", + collect_syslog ? "enable" : "disable"); + } + + else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { + fprintf(stdout, "\n"); + fprintf(stdout, _("Commands: R - run all tests in suite %s"), pSuite->pName); + fprintf(stdout, "\n"); + fprintf(stdout, "%s\n", _(" S - Select and run a test")); + fprintf(stdout, _(" L - List all tests registered in suite %s"), pSuite->pName); + fprintf(stdout, "\n"); + fprintf(stdout, "%s\n", _(" A - Activate or deactivate a test (toggle)")); + fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); + fprintf(stdout, "%s\n", _(" M - Move up to main menu")); + fprintf(stdout, "%s\n", _(" O - Set CUnit options")); + fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); + fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); + fprintf(stdout, "%s\n", _(" H - Show this help message")); + fprintf(stdout, "%s\n", _(" Q - Quit the application")); + } + } + return eStatus; +} + +/*------------------------------------------------------------------------*/ +/** + * Sets CUnit options interactively using console interface. + * Displays actions and responds based on user imput. + */ +static CU_STATUS console_set_options_run(void) +{ + int chChoice; + CU_STATUS eStatus = CU_STATUS_CONTINUE; + char szTemp[256]; + + while (CU_STATUS_CONTINUE == eStatus) { + fprintf(stdout, "\n%s\n", + _("***************** CUNIT CONSOLE - OPTIONS **************************")); + fprintf(stdout, _(" 1 - Inactive suites/tests treated as runtime failures %s"), + (CU_FALSE != CU_get_fail_on_inactive()) ? _("Yes") : _("No")); + fprintf(stdout, "\n********************************************************************\n"); + fprintf(stdout, "%s", + _("Enter number of option to change : ")); + chChoice = getchar(); + if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ + + switch (tolower(chChoice)) { + case '1': + CU_set_fail_on_inactive((CU_FALSE == CU_get_fail_on_inactive()) ? CU_TRUE : CU_FALSE); + break; + + default: + eStatus = CU_STATUS_MOVE_UP; + break; + } + } + return eStatus; +} + +/*------------------------------------------------------------------------*/ +/** + * Runs all tests within the console interface. + * The test registry is changed to the specified registry before running + * the tests, and reset to the original registry when done. If pRegistry + * is NULL, the default internal CUnit test registry is used. + * + * @param pRegistry The CU_pTestRegistry containing the tests to be run. + * @return An error code indicating the error status during the test run. + */ +static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = NULL; + CU_ErrorCode result; + + f_pRunningSuite = NULL; + + if (NULL != pRegistry) { + pOldRegistry = CU_set_registry(pRegistry); + } + result = CU_run_all_tests(); + if (NULL != pRegistry) { + CU_set_registry(pOldRegistry); + } + return result; +} + +/*------------------------------------------------------------------------*/ +/** + * Runs a specified suite within the console interface. + * + * @param pSuite The suite to be run (non-NULL). + * @return An error code indicating the error status during the test run. + */ +static CU_ErrorCode console_run_suite(CU_pSuite pSuite) +{ + f_pRunningSuite = NULL; + return CU_run_suite(pSuite); +} + +/*------------------------------------------------------------------------*/ +/** + ( Runs a specific test for the specified suite within the console interface. + * + * @param pSuite The suite containing the test to be run (non-NULL). + * @param pTest The test to be run (non-NULL). + * @return An error code indicating the error status during the test run. + */ +static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest) +{ + f_pRunningSuite = NULL; + return CU_run_test(pSuite, pTest); +} + +/*------------------------------------------------------------------------*/ +/** + * Reads the number of a test from standard input and locates the test + * at that position. A pointer to the located test is stored + * in ppTest upon return. + * + * @param pSuite The suite to be queried. + * @param ppTest Pointer to location to store the selected test. + * @return CUE_SUCCESS if a test was successfully selected, CUE_NOTEST + * otherwise. On return, ppTest points to the test selected, + * or NULL if none. + */ +static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest) +{ + char buffer[100]; + + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + *ppTest = NULL; + + if (0 == pSuite->uiNumberOfTests) { + fprintf(stdout, "\n"); + fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); + } + else { + list_tests(pSuite); + fprintf(stdout, "\n"); + fprintf(stdout, _("Enter number of test to select (1-%u) : "), + pSuite->uiNumberOfTests); + if (fgets(buffer, 100, stdin)); + + *ppTest = CU_get_test_by_index(atol(buffer), pSuite); + } + + return (NULL != *ppTest) ? CUE_SUCCESS : CUE_NOTEST; +} + +/*------------------------------------------------------------------------*/ +/** + * Reads the number of a suite from standard input and locates the suite + * at that position. If pRegistry is NULL, the default CUnit registry + * will be used. The located pSuite is returned in ppSuite. ppSuite + * will be NULL if there is no suite in the registry having the input name. + * Returns NULL if the suite is successfully located, non-NULL otherwise. + * + * @param pRegistry The CU_pTestRegistry to query. If NULL, use the + * default internal CUnit test registry. + * @param ppSuite Pointer to location to store the selected suite. + * @return CUE_SUCCESS if a suite was successfully selected, CUE_NOSUITE + * otherwise. On return, ppSuite points to the suite selected. + */ +static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite) +{ + char buffer[100]; + + if (NULL == pRegistry) { + pRegistry = CU_get_registry(); + } + + if (0 == pRegistry->uiNumberOfSuites) { + fprintf(stdout, "\n%s", _("No suites are registered.")); + *ppSuite = NULL; + } + else { + list_suites(pRegistry); + fprintf(stdout, "\n"); + fprintf(stdout, _("Enter number of suite to select (1-%u) : "), + pRegistry->uiNumberOfSuites); + if (fgets(buffer, 100, stdin)); + + *ppSuite = CU_get_suite_by_index(atol(buffer), pRegistry); + } + + return (NULL != *ppSuite) ? CUE_SUCCESS : CUE_NOSUITE; +} + +/*------------------------------------------------------------------------*/ +/** + * Lists the suites in a registry to standard output. + * @param pRegistry The CU_pTestRegistry to query (non-NULL). + */ +static void list_suites(CU_pTestRegistry pRegistry) +{ + CU_pSuite pCurSuite = NULL; + int i; + static int width[6]; + + if (NULL == pRegistry) { + pRegistry = CU_get_registry(); + } + + assert(NULL != pRegistry); + if (0 == pRegistry->uiNumberOfSuites) { + fprintf(stdout, "\n%s\n", _("No suites are registered.")); + return; + } + + assert(NULL != pRegistry->pSuite); + + /* only need to calculate formatting widths once */ + if (0 == width[0]) { + width[0] = CU_number_width(pRegistry->uiNumberOfSuites) + 1; + width[1] = 34; + width[2] = CU_MAX(strlen(_("Init?")), CU_MAX(f_yes_width, f_no_width)) + 1; + width[3] = CU_MAX(strlen(_("Cleanup?")), CU_MAX(f_yes_width, f_no_width)) + 1; + width[4] = CU_MAX(strlen(_("#Tests")), CU_number_width(pRegistry->uiNumberOfTests) + 1) + 1; + width[5] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; + } + + fprintf(stdout, "\n%s", _("--------------------- Registered Suites -----------------------------")); + fprintf(stdout, "\n%*s %-*s%*s%*s%*s%*s\n", + width[0], _("#"), + width[1], _("Suite Name"), + width[2], _("Init?"), + width[3], _("Cleanup?"), + width[4], _("#Tests"), + width[5], _("Active?")); + + for (i = 1, pCurSuite = pRegistry->pSuite; (NULL != pCurSuite); pCurSuite = pCurSuite->pNext, ++i) { + assert(NULL != pCurSuite->pName); + fprintf(stdout, "\n%*d. %-*.*s%*s%*s%*u%*s", + width[0], i, + width[1], width[1] - 1, pCurSuite->pName, + width[2]-1, (NULL != pCurSuite->pInitializeFunc) ? _("Yes") : _("No"), + width[3], (NULL != pCurSuite->pCleanupFunc) ? _("Yes") : _("No"), + width[4], pCurSuite->uiNumberOfTests, + width[5], (CU_FALSE != pCurSuite->fActive) ? _("Yes") : _("No")); + } + fprintf(stdout, "\n---------------------------------------------------------------------\n"); + fprintf(stdout, _("Total Number of Suites : %-u"), pRegistry->uiNumberOfSuites); + fprintf(stdout, "\n"); +} + +/*------------------------------------------------------------------------*/ +/** + * Lists the tests in a suite to standard output. + * @param pSuite The suite to query (non-NULL). + */ +static void list_tests(CU_pSuite pSuite) +{ + CU_pTest pCurTest = NULL; + unsigned int uiCount; + static int width[3]; + + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + if (0 == pSuite->uiNumberOfTests) { + fprintf(stdout, "\n"); + fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); + fprintf(stdout, "\n"); + return; + } + + assert(NULL != pSuite->pTest); + + /* only number of tests can change between calls */ + width[0] = CU_number_width(pSuite->uiNumberOfTests) + 1; + if (0 == width[1]) { + width[1] = 34; + width[2] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; + } + + fprintf(stdout, "\n%s", + _("----------------- Test List ------------------------------")); + fprintf(stdout, "\n%s%s\n", _("Suite: "), pSuite->pName); + fprintf(stdout, "\n%*s %-*s%*s\n", + width[0], _("#"), + width[1], _("Test Name"), + width[2], _("Active?")); + + for (uiCount = 1, pCurTest = pSuite->pTest ; + NULL != pCurTest ; + uiCount++, pCurTest = pCurTest->pNext) { + assert(NULL != pCurTest->pName); + fprintf(stdout, "\n%*u. %-*.*s%*s", + width[0], uiCount, + width[1], width[1]-1, pCurTest->pName, + width[2]-1, (CU_FALSE != pCurTest->fActive) ? _("Yes") : _("No")); + } + fprintf(stdout, "\n----------------------------------------------------------\n"); + fprintf(stdout, _("Total Number of Tests : %-u"), pSuite->uiNumberOfTests); + fprintf(stdout, "\n"); +} + +/*------------------------------------------------------------------------*/ +/** Displays the record of test failures on standard output. */ +static void show_failures(void) +{ + unsigned int i; + CU_pFailureRecord pFailure = CU_get_failure_list(); + + if (NULL == pFailure) { + fprintf(stdout, "\n%s\n", _("No failures.")); + } + else { + + fprintf(stdout, "\n%s", + _("--------------- Test Run Failures -------------------------")); + fprintf(stdout, "\n%s\n", + _(" src_file:line# : (suite:test) : failure_condition")); + + for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { + fprintf(stdout, "\n%d. %s:%u : (%s : %s) : %s", i, + (NULL != pFailure->strFileName) + ? pFailure->strFileName : "", + pFailure->uiLineNumber, + ((NULL != pFailure->pSuite) && (NULL != pFailure->pSuite->pName)) + ? pFailure->pSuite->pName : "", + ((NULL != pFailure->pTest) && (NULL != pFailure->pTest->pName)) + ? pFailure->pTest->pName : "", + (NULL != pFailure->strCondition) + ? pFailure->strCondition : ""); + } + fprintf(stdout, "\n-----------------------------------------------------------"); + fprintf(stdout, "\n"); + fprintf(stdout, _("Total Number of Failures : %-u"), i - 1); + fprintf(stdout, "\n"); + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at start of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + */ +static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + assert(NULL != pTest); + assert(NULL != pTest->pName); + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + /* Comparing the Addresses rather than the Group Names. */ + if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { + fprintf(stdout, _("\nRunning Suite : %s"), pSuite->pName); + fprintf(stdout, _("\n Running Test : %s"), pTest->pName); + f_pRunningSuite = pSuite; + } + else { + fprintf(stdout, _("\n Running Test : %s"), pTest->pName); + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + * @param pFailure Pointer to the 1st failure record for this test. + */ +static void console_test_complete_message_handler(const CU_pTest pTest, + const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + /* + * For console interface do nothing. This is useful only for the test + * interface where UI is involved. Just silence compiler warnings. + */ + CU_UNREFERENCED_PARAMETER(pTest); + CU_UNREFERENCED_PARAMETER(pSuite); + CU_UNREFERENCED_PARAMETER(pFailure); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of all tests in a suite. + * @param pFailure Pointer to the test failure record list. + */ +static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) +{ + CU_UNREFERENCED_PARAMETER(pFailure); /* not used in console interface */ + printf("\n\n"); + CU_print_run_results(stdout); + printf("\n"); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite initialization fails. + * @param pSuite The suite for which initialization failed. + */ +static void console_suite_init_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + fprintf(stdout, + _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite cleanup fails. + * @param pSuite The suite for which cleanup failed. + */ +static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); +} + +/** @} */ diff --git a/gearsyncd/tests/cunit/Console.cpp b/gearsyncd/tests/cunit/Console.cpp new file mode 100644 index 0000000000..512e3e6987 --- /dev/null +++ b/gearsyncd/tests/cunit/Console.cpp @@ -0,0 +1,735 @@ + /* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of the Console Test Interface. + * + * Aug 2001 Initial implementation (AK) + * + * 19/Aug/2001 Added initial console interface functions without + * any run functionality. (AK) + * + * 24/Aug/2001 Added compare_strings, show_errors, list_suites, + * list_tests function declarations. (AK) + * + * 17-Jul-2004 New interface, doxygen comments, reformat console output. (JDS) + * + * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) + * + * 24-Apr-2006 Suite/test selection is now by number rather than name. + * Inactive suites/tests now reported. + * Interface supports (de)activation of tests/suites. + * Help function added for both menu levels. + * Option menu added. Immediate action on hotkeys + * without needing to , like curses. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Console test interface with interactive output (implementation). + */ +/** @addtogroup Console + @{ +*/ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "Util.h" +#include "TestRun.h" +#include "Console.h" +#include "CUnit_intl.h" + +extern int sai_driver_shell(); +extern int warmboot_enable, collect_syslog; + +/** Console interface status flag. */ +typedef enum +{ + CU_STATUS_CONTINUE = 1, /**< Continue processing commands in current menu. */ + CU_STATUS_MOVE_UP, /**< Move up to the previous menu. */ + CU_STATUS_STOP /**< Stop processing (user selected 'Quit'). */ +} CU_STATUS; + +/*================================================================= + * Global / Static data definitions + *=================================================================*/ +/** Pointer to the currently running suite. */ +static CU_pSuite f_pRunningSuite = NULL; + +/** Common width measurements for output formatting. */ +static size_t f_yes_width = 0; +static size_t f_no_width = 0; + +/*================================================================= + * Static function forward declarations + *=================================================================*/ +void console_registry_level_run(CU_pTestRegistry pRegistry); +static CU_STATUS console_suite_level_run(CU_pSuite pSuite); +static CU_STATUS console_set_options_run(void); + +static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry); +static CU_ErrorCode console_run_suite(CU_pSuite pSuite); +static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest); + +static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); +static void console_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); +static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); +static void console_suite_init_failure_message_handler(const CU_pSuite pSuite); +static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); + +static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest); +static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite); + +static void list_suites(CU_pTestRegistry pRegistry); +static void list_tests(CU_pSuite pSuite); +static void show_failures(void); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +void CU_console_run_tests(void) +{ + /* + * To avoid user from cribbing about the output not coming onto + * screen at the moment of SIGSEGV. + */ + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + + fprintf(stdout, "\n\n %s" CU_VERSION + "\n %s\n", + _("CUnit - A Unit testing framework for C - Version "), + _("http://cunit.sourceforge.net/")); + + if (NULL == CU_get_registry()) { + fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); + CU_set_error(CUE_NOREGISTRY); + } + else { + f_yes_width = strlen(_("Yes")); + f_no_width = strlen(_("No")); + + CU_set_test_start_handler(console_test_start_message_handler); + CU_set_test_complete_handler(console_test_complete_message_handler); + CU_set_all_test_complete_handler(console_all_tests_complete_message_handler); + CU_set_suite_init_failure_handler(console_suite_init_failure_message_handler); + CU_set_suite_cleanup_failure_handler(console_suite_cleanup_failure_message_handler); + + console_registry_level_run(NULL); + } +} + +/*================================================================= + * Static function implementation + *=================================================================*/ +/** + * Main loop for console interface. + * Displays actions and responds based on user imput. If pRegistry + * is NULL, will use the default internal CUnit test registry. + * + * @param pRegistry The CU_pTestRegistry to use for testing. + */ +void console_registry_level_run(CU_pTestRegistry pRegistry) +{ + int chChoice; + CU_pSuite pSuite = NULL; + CU_STATUS eStatus = CU_STATUS_CONTINUE; + char szTemp[256]; + + while (CU_STATUS_CONTINUE == eStatus) + { + fprintf(stdout, "\n\n%s\n%s\n%s", + _("***************************** CUNIT CONSOLE - MAIN MENU *******************************************"), + _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), + _("Enter command: ")); + chChoice = toupper(getchar()); + if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ + + if (chChoice == _("R")[0]) { + console_run_all_tests(pRegistry); + } + + else if (chChoice == _("S")[0]) { + if (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { + assert(NULL != pSuite->pName); + fprintf(stdout, _("Suite '%s' selected."), pSuite->pName); + fprintf(stdout, "\n"); + if (CU_STATUS_STOP == console_suite_level_run(pSuite)) { + eStatus = CU_STATUS_STOP; + } + } + else { + fprintf(stdout, "\n%s\n", _("Suite not found.")); + } + } + + else if (chChoice == _("L")[0]) { + list_suites(pRegistry); + } + + else if (chChoice == _("A")[0]) { + while (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { + CU_set_suite_active(pSuite, (CU_FALSE == pSuite->fActive) ? CU_TRUE : CU_FALSE); + } + } + + else if (chChoice == _("F")[0]) { + show_failures(); + } + + else if (chChoice == _("O")[0]) { + console_set_options_run(); + } + + else if (chChoice == _("Q")[0]) { + eStatus = CU_STATUS_STOP; + } + + else if (chChoice == _("D")[0]) { + fprintf(stdout, "Hit enter to get drivshell prompt..\n"); + sai_driver_shell(); + } + + else if (chChoice == _("W")[0]) { + warmboot_enable = warmboot_enable ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", + warmboot_enable ? "enable" : "disable"); + } + + else if (chChoice == _("C")[0]) { + collect_syslog = collect_syslog ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", + collect_syslog ? "enable" : "disable"); + } + + else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { + fprintf(stdout, "\n%s\n", _("Commands: R - run all tests in all suites")); + fprintf(stdout, "%s\n", _(" S - Select a suite to run or modify")); + fprintf(stdout, "%s\n", _(" L - List all registered suites")); + fprintf(stdout, "%s\n", _(" A - Activate or deactivate a suite (toggle)")); + fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); + fprintf(stdout, "%s\n", _(" O - Set CUnit options")); + fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); + fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); + fprintf(stdout, "%s\n", _(" H - Show this help message")); + fprintf(stdout, "%s\n", _(" Q - Quit the application")); + } + } +} + +/*------------------------------------------------------------------------*/ +/** + * Runs a selected suite within the console interface. + * Displays actions and responds based on user imput. + * + * @param pSuite The suite to use for testing (non-NULL). + */ +static CU_STATUS console_suite_level_run(CU_pSuite pSuite) +{ + int chChoice; + CU_pTest pTest = NULL; + CU_STATUS eStatus = CU_STATUS_CONTINUE; + char szTemp[256]; + + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + while (CU_STATUS_CONTINUE == eStatus) { + + fprintf(stdout, "\n%s\n%s\n%s", + _("**************************** CUNIT CONSOLE - SUITE MENU ************************************************"), + _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), + _("Enter command: ")); + chChoice = toupper(getchar()); + if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ + + if (chChoice == _("R")[0]) { + console_run_suite(pSuite); + } + + else if (chChoice == _("S")[0]) { + if (CUE_SUCCESS == select_test(pSuite, &pTest)) { + console_run_single_test(pSuite, pTest); + } + else { + fprintf(stdout, "\n%s\n", _("Test not found.")); + } + } + + else if (chChoice == _("L")[0]) { + list_tests(pSuite); + } + + else if (chChoice == _("A")[0]) { + while (CUE_SUCCESS == select_test(pSuite, &pTest)) { + CU_set_test_active(pTest, (CU_FALSE == pTest->fActive) ? CU_TRUE : CU_FALSE); + } + } + + else if (chChoice == _("F")[0]) { + show_failures(); + } + + else if ((chChoice == _("M")[0]) || (chChoice == _("U")[0])) { + eStatus = CU_STATUS_MOVE_UP; + } + + else if (chChoice == _("O")[0]) { + console_set_options_run(); + } + + else if (chChoice == _("Q")[0]) { + eStatus = CU_STATUS_STOP; + } + + else if (chChoice == _("D")[0]) { + fprintf(stdout, "Hit enter to get drivshell prompt..\n"); + sai_driver_shell(); + } + + else if (chChoice == _("W")[0]) { + warmboot_enable = warmboot_enable ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", + warmboot_enable ? "enable" : "disable"); + } + + else if (chChoice == _("C")[0]) { + collect_syslog = collect_syslog ? 0 : 1; + fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", + collect_syslog ? "enable" : "disable"); + } + + else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { + fprintf(stdout, "\n"); + fprintf(stdout, _("Commands: R - run all tests in suite %s"), pSuite->pName); + fprintf(stdout, "\n"); + fprintf(stdout, "%s\n", _(" S - Select and run a test")); + fprintf(stdout, _(" L - List all tests registered in suite %s"), pSuite->pName); + fprintf(stdout, "\n"); + fprintf(stdout, "%s\n", _(" A - Activate or deactivate a test (toggle)")); + fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); + fprintf(stdout, "%s\n", _(" M - Move up to main menu")); + fprintf(stdout, "%s\n", _(" O - Set CUnit options")); + fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); + fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); + fprintf(stdout, "%s\n", _(" H - Show this help message")); + fprintf(stdout, "%s\n", _(" Q - Quit the application")); + } + } + return eStatus; +} + +/*------------------------------------------------------------------------*/ +/** + * Sets CUnit options interactively using console interface. + * Displays actions and responds based on user imput. + */ +static CU_STATUS console_set_options_run(void) +{ + int chChoice; + CU_STATUS eStatus = CU_STATUS_CONTINUE; + char szTemp[256]; + + while (CU_STATUS_CONTINUE == eStatus) { + fprintf(stdout, "\n%s\n", + _("***************** CUNIT CONSOLE - OPTIONS **************************")); + fprintf(stdout, _(" 1 - Inactive suites/tests treated as runtime failures %s"), + (CU_FALSE != CU_get_fail_on_inactive()) ? _("Yes") : _("No")); + fprintf(stdout, "\n********************************************************************\n"); + fprintf(stdout, "%s", + _("Enter number of option to change : ")); + chChoice = getchar(); + if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ + + switch (tolower(chChoice)) { + case '1': + CU_set_fail_on_inactive((CU_FALSE == CU_get_fail_on_inactive()) ? CU_TRUE : CU_FALSE); + break; + + default: + eStatus = CU_STATUS_MOVE_UP; + break; + } + } + return eStatus; +} + +/*------------------------------------------------------------------------*/ +/** + * Runs all tests within the console interface. + * The test registry is changed to the specified registry before running + * the tests, and reset to the original registry when done. If pRegistry + * is NULL, the default internal CUnit test registry is used. + * + * @param pRegistry The CU_pTestRegistry containing the tests to be run. + * @return An error code indicating the error status during the test run. + */ +static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = NULL; + CU_ErrorCode result; + + f_pRunningSuite = NULL; + + if (NULL != pRegistry) { + pOldRegistry = CU_set_registry(pRegistry); + } + result = CU_run_all_tests(); + if (NULL != pRegistry) { + CU_set_registry(pOldRegistry); + } + return result; +} + +/*------------------------------------------------------------------------*/ +/** + * Runs a specified suite within the console interface. + * + * @param pSuite The suite to be run (non-NULL). + * @return An error code indicating the error status during the test run. + */ +static CU_ErrorCode console_run_suite(CU_pSuite pSuite) +{ + f_pRunningSuite = NULL; + return CU_run_suite(pSuite); +} + +/*------------------------------------------------------------------------*/ +/** + ( Runs a specific test for the specified suite within the console interface. + * + * @param pSuite The suite containing the test to be run (non-NULL). + * @param pTest The test to be run (non-NULL). + * @return An error code indicating the error status during the test run. + */ +static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest) +{ + f_pRunningSuite = NULL; + return CU_run_test(pSuite, pTest); +} + +/*------------------------------------------------------------------------*/ +/** + * Reads the number of a test from standard input and locates the test + * at that position. A pointer to the located test is stored + * in ppTest upon return. + * + * @param pSuite The suite to be queried. + * @param ppTest Pointer to location to store the selected test. + * @return CUE_SUCCESS if a test was successfully selected, CUE_NOTEST + * otherwise. On return, ppTest points to the test selected, + * or NULL if none. + */ +static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest) +{ + char buffer[100]; + + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + *ppTest = NULL; + + if (0 == pSuite->uiNumberOfTests) { + fprintf(stdout, "\n"); + fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); + } + else { + list_tests(pSuite); + fprintf(stdout, "\n"); + fprintf(stdout, _("Enter number of test to select (1-%u) : "), + pSuite->uiNumberOfTests); + if (fgets(buffer, 100, stdin)); + + *ppTest = CU_get_test_by_index(atol(buffer), pSuite); + } + + return (NULL != *ppTest) ? CUE_SUCCESS : CUE_NOTEST; +} + +/*------------------------------------------------------------------------*/ +/** + * Reads the number of a suite from standard input and locates the suite + * at that position. If pRegistry is NULL, the default CUnit registry + * will be used. The located pSuite is returned in ppSuite. ppSuite + * will be NULL if there is no suite in the registry having the input name. + * Returns NULL if the suite is successfully located, non-NULL otherwise. + * + * @param pRegistry The CU_pTestRegistry to query. If NULL, use the + * default internal CUnit test registry. + * @param ppSuite Pointer to location to store the selected suite. + * @return CUE_SUCCESS if a suite was successfully selected, CUE_NOSUITE + * otherwise. On return, ppSuite points to the suite selected. + */ +static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite) +{ + char buffer[100]; + + if (NULL == pRegistry) { + pRegistry = CU_get_registry(); + } + + if (0 == pRegistry->uiNumberOfSuites) { + fprintf(stdout, "\n%s", _("No suites are registered.")); + *ppSuite = NULL; + } + else { + list_suites(pRegistry); + fprintf(stdout, "\n"); + fprintf(stdout, _("Enter number of suite to select (1-%u) : "), + pRegistry->uiNumberOfSuites); + if (fgets(buffer, 100, stdin)); + + *ppSuite = CU_get_suite_by_index(atol(buffer), pRegistry); + } + + return (NULL != *ppSuite) ? CUE_SUCCESS : CUE_NOSUITE; +} + +/*------------------------------------------------------------------------*/ +/** + * Lists the suites in a registry to standard output. + * @param pRegistry The CU_pTestRegistry to query (non-NULL). + */ +static void list_suites(CU_pTestRegistry pRegistry) +{ + CU_pSuite pCurSuite = NULL; + int i; + static int width[6]; + + if (NULL == pRegistry) { + pRegistry = CU_get_registry(); + } + + assert(NULL != pRegistry); + if (0 == pRegistry->uiNumberOfSuites) { + fprintf(stdout, "\n%s\n", _("No suites are registered.")); + return; + } + + assert(NULL != pRegistry->pSuite); + + /* only need to calculate formatting widths once */ + if (0 == width[0]) { + width[0] = CU_number_width(pRegistry->uiNumberOfSuites) + 1; + width[1] = 34; + width[2] = CU_MAX(strlen(_("Init?")), CU_MAX(f_yes_width, f_no_width)) + 1; + width[3] = CU_MAX(strlen(_("Cleanup?")), CU_MAX(f_yes_width, f_no_width)) + 1; + width[4] = CU_MAX(strlen(_("#Tests")), CU_number_width(pRegistry->uiNumberOfTests) + 1) + 1; + width[5] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; + } + + fprintf(stdout, "\n%s", _("--------------------- Registered Suites -----------------------------")); + fprintf(stdout, "\n%*s %-*s%*s%*s%*s%*s\n", + width[0], _("#"), + width[1], _("Suite Name"), + width[2], _("Init?"), + width[3], _("Cleanup?"), + width[4], _("#Tests"), + width[5], _("Active?")); + + for (i = 1, pCurSuite = pRegistry->pSuite; (NULL != pCurSuite); pCurSuite = pCurSuite->pNext, ++i) { + assert(NULL != pCurSuite->pName); + fprintf(stdout, "\n%*d. %-*.*s%*s%*s%*u%*s", + width[0], i, + width[1], width[1] - 1, pCurSuite->pName, + width[2]-1, (NULL != pCurSuite->pInitializeFunc) ? _("Yes") : _("No"), + width[3], (NULL != pCurSuite->pCleanupFunc) ? _("Yes") : _("No"), + width[4], pCurSuite->uiNumberOfTests, + width[5], (CU_FALSE != pCurSuite->fActive) ? _("Yes") : _("No")); + } + fprintf(stdout, "\n---------------------------------------------------------------------\n"); + fprintf(stdout, _("Total Number of Suites : %-u"), pRegistry->uiNumberOfSuites); + fprintf(stdout, "\n"); +} + +/*------------------------------------------------------------------------*/ +/** + * Lists the tests in a suite to standard output. + * @param pSuite The suite to query (non-NULL). + */ +static void list_tests(CU_pSuite pSuite) +{ + CU_pTest pCurTest = NULL; + unsigned int uiCount; + static int width[3]; + + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + if (0 == pSuite->uiNumberOfTests) { + fprintf(stdout, "\n"); + fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); + fprintf(stdout, "\n"); + return; + } + + assert(NULL != pSuite->pTest); + + /* only number of tests can change between calls */ + width[0] = CU_number_width(pSuite->uiNumberOfTests) + 1; + if (0 == width[1]) { + width[1] = 34; + width[2] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; + } + + fprintf(stdout, "\n%s", + _("----------------- Test List ------------------------------")); + fprintf(stdout, "\n%s%s\n", _("Suite: "), pSuite->pName); + fprintf(stdout, "\n%*s %-*s%*s\n", + width[0], _("#"), + width[1], _("Test Name"), + width[2], _("Active?")); + + for (uiCount = 1, pCurTest = pSuite->pTest ; + NULL != pCurTest ; + uiCount++, pCurTest = pCurTest->pNext) { + assert(NULL != pCurTest->pName); + fprintf(stdout, "\n%*u. %-*.*s%*s", + width[0], uiCount, + width[1], width[1]-1, pCurTest->pName, + width[2]-1, (CU_FALSE != pCurTest->fActive) ? _("Yes") : _("No")); + } + fprintf(stdout, "\n----------------------------------------------------------\n"); + fprintf(stdout, _("Total Number of Tests : %-u"), pSuite->uiNumberOfTests); + fprintf(stdout, "\n"); +} + +/*------------------------------------------------------------------------*/ +/** Displays the record of test failures on standard output. */ +static void show_failures(void) +{ + unsigned int i; + CU_pFailureRecord pFailure = CU_get_failure_list(); + + if (NULL == pFailure) { + fprintf(stdout, "\n%s\n", _("No failures.")); + } + else { + + fprintf(stdout, "\n%s", + _("--------------- Test Run Failures -------------------------")); + fprintf(stdout, "\n%s\n", + _(" src_file:line# : (suite:test) : failure_condition")); + + for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { + fprintf(stdout, "\n%d. %s:%u : (%s : %s) : %s", i, + (NULL != pFailure->strFileName) + ? pFailure->strFileName : "", + pFailure->uiLineNumber, + ((NULL != pFailure->pSuite) && (NULL != pFailure->pSuite->pName)) + ? pFailure->pSuite->pName : "", + ((NULL != pFailure->pTest) && (NULL != pFailure->pTest->pName)) + ? pFailure->pTest->pName : "", + (NULL != pFailure->strCondition) + ? pFailure->strCondition : ""); + } + fprintf(stdout, "\n-----------------------------------------------------------"); + fprintf(stdout, "\n"); + fprintf(stdout, _("Total Number of Failures : %-u"), i - 1); + fprintf(stdout, "\n"); + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at start of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + */ +static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + assert(NULL != pTest); + assert(NULL != pTest->pName); + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + /* Comparing the Addresses rather than the Group Names. */ + if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { + fprintf(stdout, _("\nRunning Suite : %s"), pSuite->pName); + fprintf(stdout, _("\n Running Test : %s"), pTest->pName); + f_pRunningSuite = pSuite; + } + else { + fprintf(stdout, _("\n Running Test : %s"), pTest->pName); + } +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of each test. + * @param pTest The test being run. + * @param pSuite The suite containing the test. + * @param pFailure Pointer to the 1st failure record for this test. + */ +static void console_test_complete_message_handler(const CU_pTest pTest, + const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + /* + * For console interface do nothing. This is useful only for the test + * interface where UI is involved. Just silence compiler warnings. + */ + CU_UNREFERENCED_PARAMETER(pTest); + CU_UNREFERENCED_PARAMETER(pSuite); + CU_UNREFERENCED_PARAMETER(pFailure); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called at completion of all tests in a suite. + * @param pFailure Pointer to the test failure record list. + */ +static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) +{ + CU_UNREFERENCED_PARAMETER(pFailure); /* not used in console interface */ + printf("\n\n"); + CU_print_run_results(stdout); + printf("\n"); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite initialization fails. + * @param pSuite The suite for which initialization failed. + */ +static void console_suite_init_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + fprintf(stdout, + _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); +} + +/*------------------------------------------------------------------------*/ +/** Handler function called when suite cleanup fails. + * @param pSuite The suite for which cleanup failed. + */ +static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) +{ + assert(NULL != pSuite); + assert(NULL != pSuite->pName); + + fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); +} + +/** @} */ diff --git a/gearsyncd/tests/cunit/Console.h b/gearsyncd/tests/cunit/Console.h new file mode 100644 index 0000000000..f5b37695f3 --- /dev/null +++ b/gearsyncd/tests/cunit/Console.h @@ -0,0 +1,60 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Contains Interface for console Run tests. + * + * Aug 2001 Initial implementation. (AK) + * + * 09/Aug/2001 Single interface to Console_run_tests. (AK) + * + * 20-Jul-2004 New interface, doxygen comments. (JDS) + */ + +/** @file + * Console interface with interactive output (user interface). + */ +/** @addtogroup Console + * @{ + */ + +#ifndef CUNIT_CONSOLE_H_SEEN +#define CUNIT_CONSOLE_H_SEEN + +#include "CUnit.h" +#include "TestDB.h" + +#ifdef __cplusplus +extern "C" { +#endif + +CU_EXPORT void CU_console_run_tests(void); +/**< Run registered CUnit tests using the console interface. */ + +#ifdef USE_DEPRECATED_CUNIT_NAMES +/** Deprecated (version 1). @deprecated Use CU_console_run_tests(). */ +#define console_run_tests() CU_console_run_tests() +#endif /* USE_DEPRECATED_CUNIT_NAMES */ + +#ifdef __cplusplus +} +#endif +#endif /* CUNIT_CONSOLE_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/cunit/MyMem.c b/gearsyncd/tests/cunit/MyMem.c new file mode 100644 index 0000000000..7ff8a3af9b --- /dev/null +++ b/gearsyncd/tests/cunit/MyMem.c @@ -0,0 +1,585 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Memory management functions used throughout CUnit. + * + * 13/Oct/2001 Moved some of the generic functions definitions from other + * files to this one so as to use the functions consitently. + * This file is not included in the distribution headers because + * it is used internally by CUnit. (AK) + * + * 18-Jul-2004 New interface, doxygen comments, made local functions & + * constants static, fixed reporting of memory tracking (valid + * vs invalid cycles), restructured memory tracking to detect + * reallocations & multiple deletions. (JDS) + * + * 24-Apr-2005 Changed type of allocated sizes to size_t to avoid + * signed-unsigned mismatch. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Memory management & reporting functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "MyMem.h" +#include "CUnit_intl.h" + +#ifdef MEMTRACE + +#define MAX_FILE_NAME_LENGTH 256 + +/** Default name for memory dump file. */ +static const char* f_szDefaultDumpFileName = "CUnit-Memory-Dump.xml"; +/**< Default name for memory dump file. */ + +#ifdef CUNIT_BUILD_TESTS +/** For testing use (only) to simulate memory exhaustion - + * if CU_FALSE, allocation requests will always fail and return NULL. + */ +static CU_BOOL f_bTestCunitMallocActive = CU_TRUE; +#endif + +/** Structure holding the details of a memory allocation/deallocation event. */ +typedef struct mem_event { + size_t Size; + unsigned int AllocLine; + char AllocFilename[MAX_FILE_NAME_LENGTH]; + unsigned int DeallocLine; + char DeallocFilename[MAX_FILE_NAME_LENGTH]; + struct mem_event* pNext; +} MEMORY_EVENT; +typedef MEMORY_EVENT* PMEMORY_EVENT; + +#define NOT_ALLOCATED 0 +#define NOT_DELETED 0 + +/** Structure holding the details of a memory node having allocation/deallocation events. */ +typedef struct mem_node { + void* pLocation; + unsigned int EventCount; + PMEMORY_EVENT pFirstEvent; + struct mem_node* pNext; +} MEMORY_NODE; +typedef MEMORY_NODE* PMEMORY_NODE; + +static PMEMORY_NODE f_pMemoryTrackerHead = NULL; /**< Head of double-linked list of memory nodes. */ +static unsigned int f_nMemoryNodes = 0; /**< Counter for memory nodes created. */ +/*------------------------------------------------------------------------*/ +/** Locate the memory node for the specified memory location (returns NULL if none). */ +static PMEMORY_NODE find_memory_node(void* pLocation) +{ + PMEMORY_NODE pMemoryNode = f_pMemoryTrackerHead; + while (NULL != pMemoryNode) { + if (pLocation == pMemoryNode->pLocation) { + break; + } + pMemoryNode = pMemoryNode->pNext; + } + + return pMemoryNode; +} +/*------------------------------------------------------------------------*/ +/** Create a new memory node for the specified memory location. */ +static PMEMORY_NODE create_memory_node(void* pLocation) +{ + PMEMORY_NODE pTempNode = NULL; + PMEMORY_NODE pMemoryNode = find_memory_node(pLocation); + + /* a memory node for pLocation should not exist yet */ + if (NULL == pMemoryNode) { + + pMemoryNode = (PMEMORY_NODE)malloc(sizeof(MEMORY_NODE)); + assert(NULL != pMemoryNode); + + pMemoryNode->pLocation = pLocation; + pMemoryNode->EventCount = 0; + pMemoryNode->pFirstEvent = NULL; + pMemoryNode->pNext = NULL; + + /* add new node to linked list */ + pTempNode = f_pMemoryTrackerHead; + if (NULL == pTempNode) { + f_pMemoryTrackerHead = pMemoryNode; + } + else { + while (NULL != pTempNode->pNext) { + pTempNode = pTempNode->pNext; + } + pTempNode->pNext = pMemoryNode; + } + + ++f_nMemoryNodes; + } + return pMemoryNode; +} +/*------------------------------------------------------------------------*/ +/** Add a new memory event having the specified parameters. */ +static PMEMORY_EVENT add_memory_event(PMEMORY_NODE pMemoryNode, + size_t size, + unsigned int alloc_line, + const char* alloc_filename) +{ + PMEMORY_EVENT pMemoryEvent = NULL; + PMEMORY_EVENT pTempEvent = NULL; + + assert (NULL != pMemoryNode); + + /* create and set up the new event */ + pMemoryEvent = (PMEMORY_EVENT)malloc(sizeof(MEMORY_EVENT)); + assert(NULL != pMemoryEvent); + + pMemoryEvent->Size = size; + pMemoryEvent->AllocLine = alloc_line; + strncpy(pMemoryEvent->AllocFilename, alloc_filename, (size_t) MAX_FILE_NAME_LENGTH-1); + pMemoryEvent->AllocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; + pMemoryEvent->DeallocLine = NOT_DELETED; + pMemoryEvent->DeallocFilename[0] = (char)0; + pMemoryEvent->pNext = NULL; + + /* add the new event to the end of the linked list */ + pTempEvent = pMemoryNode->pFirstEvent; + if (NULL == pTempEvent) { + pMemoryNode->pFirstEvent = pMemoryEvent; + } + else { + while (NULL != pTempEvent->pNext) { + pTempEvent = pTempEvent->pNext; + } + pTempEvent->pNext = pMemoryEvent; + } + + ++pMemoryNode->EventCount; + + return pMemoryEvent; +} +/*------------------------------------------------------------------------*/ +/** Record memory allocation event. */ +static PMEMORY_NODE allocate_memory(size_t nSize, + void* pLocation, + unsigned int uiAllocationLine, + const char* szAllocationFile) +{ + PMEMORY_NODE pMemoryNode = NULL; + + /* attempt to locate an existing record for this pLocation */ + pMemoryNode = find_memory_node(pLocation); + + /* pLocation not found - create a new event record */ + if (NULL == pMemoryNode) { + pMemoryNode = create_memory_node(pLocation); + } + + /* add the new event record */ + add_memory_event(pMemoryNode, nSize, uiAllocationLine, szAllocationFile); + + return pMemoryNode; +} + +/*------------------------------------------------------------------------*/ +/** Record memory deallocation event. */ +static void deallocate_memory(void* pLocation, unsigned int uiDeletionLine, const char* szDeletionFileName) +{ + PMEMORY_NODE pMemoryNode = NULL; + PMEMORY_EVENT pTempEvent = NULL; + + assert(0 != uiDeletionLine); + assert(NULL != szDeletionFileName); + + /* attempt to locate an existing record for this pLocation */ + pMemoryNode = find_memory_node(pLocation); + + /* if no entry, then an unallocated pointer was freed */ + if (NULL == pMemoryNode) { + pMemoryNode = create_memory_node(pLocation); + pTempEvent = add_memory_event(pMemoryNode, 0, NOT_ALLOCATED, ""); + } + else { + /* there should always be at least 1 event for an existing memory node */ + assert(NULL != pMemoryNode->pFirstEvent); + + /* locate last memory event for this pLocation */ + pTempEvent = pMemoryNode->pFirstEvent; + while (NULL != pTempEvent->pNext) { + pTempEvent = pTempEvent->pNext; + } + + /* if pointer has already been freed, create a new event for double deletion */ + if (NOT_DELETED != pTempEvent->DeallocLine) { + pTempEvent = add_memory_event(pMemoryNode, pTempEvent->Size, NOT_ALLOCATED, ""); + } + } + + pTempEvent->DeallocLine = uiDeletionLine; + strncpy(pTempEvent->DeallocFilename, szDeletionFileName, MAX_FILE_NAME_LENGTH-1); + pTempEvent->DeallocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; +} + +/*------------------------------------------------------------------------*/ +/** Custom calloc function with memory event recording. */ +void* CU_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName) +{ + void* pVoid = NULL; + +#ifdef CUNIT_BUILD_TESTS + if (CU_FALSE == f_bTestCunitMallocActive) { + return NULL; + } +#endif + + pVoid = calloc(nmemb, size); + if (NULL != pVoid) { + allocate_memory(nmemb * size, pVoid, uiLine, szFileName); + } + + return pVoid; +} + +/*------------------------------------------------------------------------*/ +/** Custom malloc function with memory event recording. */ +void* CU_malloc(size_t size, unsigned int uiLine, const char* szFileName) +{ + void* pVoid = NULL; + +#ifdef CUNIT_BUILD_TESTS + if (CU_FALSE == f_bTestCunitMallocActive) { + return NULL; + } +#endif + + pVoid = malloc(size); + if (NULL != pVoid) { + allocate_memory(size, pVoid, uiLine, szFileName); + } + + return pVoid; +} + +/*------------------------------------------------------------------------*/ +/** Custom free function with memory event recording. */ +void CU_free(void *ptr, unsigned int uiLine, const char* szFileName) +{ + deallocate_memory(ptr, uiLine, szFileName); + free(ptr); +} + +/*------------------------------------------------------------------------*/ +/** Custom realloc function with memory event recording. */ +void* CU_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName) +{ + void* pVoid = NULL; + + deallocate_memory(ptr, uiLine, szFileName); + +#ifdef CUNIT_BUILD_TESTS + if (CU_FALSE == f_bTestCunitMallocActive) { + free(ptr); + return NULL; + } +#endif + + pVoid = realloc(ptr, size); + + if (NULL != pVoid) { + allocate_memory(size, pVoid, uiLine, szFileName); + } + + return pVoid; +} + +/*------------------------------------------------------------------------*/ +/** Print a report of memory events to file. */ +void CU_dump_memory_usage(const char* szFilename) +{ + char* szDumpFileName = (char*)f_szDefaultDumpFileName; + unsigned int nValid; + unsigned int nInvalid; + PMEMORY_NODE pTempNode = NULL; + PMEMORY_EVENT pTempEvent = NULL; + FILE* pFile = NULL; + time_t tTime = 0; + + /* use the specified file name, if supplied) */ + if ((NULL != szFilename) && strlen(szFilename) > 0) { + szDumpFileName = (char*)szFilename; + } + + if (NULL == (pFile = fopen(szDumpFileName, "w"))) { + fprintf(stderr, _("Failed to open file \"%s\" : %s"), szDumpFileName, strerror(errno)); + return; + } + + setvbuf(pFile, NULL, _IONBF, 0); + + fprintf(pFile, "<\?xml version=\"1.0\" \?>"); + fprintf(pFile, "\n<\?xml-stylesheet type=\"text/xsl\" href=\"Memory-Dump.xsl\" \?>"); + fprintf(pFile, "\n"); + fprintf(pFile, "\n"); + fprintf(pFile, "\n "); + fprintf(pFile, "\n "); + + nValid = 0; + nInvalid = 0; + pTempNode = f_pMemoryTrackerHead; + while (NULL != pTempNode) { + fprintf(pFile, "\n "); + fprintf(pFile, "\n %p ", pTempNode->pLocation); + fprintf(pFile, "\n %u ", pTempNode->EventCount); + + pTempEvent = pTempNode->pFirstEvent; + while (NULL != pTempEvent) { + fprintf(pFile, "\n "); + fprintf(pFile, "\n %u ", pTempEvent->Size); + fprintf(pFile, "\n %s ", pTempEvent->AllocFilename); + fprintf(pFile, "\n %u ", pTempEvent->AllocLine); + fprintf(pFile, "\n %s ", pTempEvent->DeallocFilename); + fprintf(pFile, "\n %u ", pTempEvent->DeallocLine); + fprintf(pFile, "\n "); + + if ((0 != pTempEvent->AllocLine) && (0 != pTempEvent->DeallocLine)) { + ++nValid; + } + else { + ++nInvalid; + } + + pTempEvent = pTempEvent->pNext; + } + + fprintf(pFile, "\n "); + pTempNode = pTempNode->pNext; + } + + fprintf(pFile, "\n "); + + fprintf(pFile, "\n "); + fprintf(pFile, "\n %u ", nValid); + fprintf(pFile, "\n %u ", nInvalid); + fprintf(pFile, "\n %u ", nValid + nInvalid); + fprintf(pFile, "\n "); + + time(&tTime); + fprintf(pFile, "\n Memory Trace for CUnit Run at %s ", ctime(&tTime)); + fprintf(pFile, ""); + + fclose(pFile); +} + +#endif /* MEMTRACE */ + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +/** Deactivate CUnit memory allocation + * After calling this function, all Cunit memory + * allocation routines will fail and return NULL. + */ +void test_cunit_deactivate_malloc(void) +{ + f_bTestCunitMallocActive = CU_FALSE; +} + +/** Activate CUnit memory allocation + * After calling this function, all Cunit memory + * allocation routines will behave normally (allocating + * memory if it is available). + */ +void test_cunit_activate_malloc(void) +{ + f_bTestCunitMallocActive = CU_TRUE; +} + +/** Retrieve the number of memory events recorded for a given pointer. */ +unsigned int test_cunit_get_n_memevents(void* pLocation) +{ + PMEMORY_NODE pNode = find_memory_node(pLocation); + return (pNode) ? pNode->EventCount : 0; +} + +/** Retrieve the number of memory allocations recorded for a given pointer. */ +unsigned int test_cunit_get_n_allocations(void* pLocation) +{ + PMEMORY_NODE pNode = find_memory_node(pLocation); + PMEMORY_EVENT pEvent = NULL; + int result = 0; + + if (NULL != pNode) { + pEvent = pNode->pFirstEvent; + while (NULL != pEvent) { + if (pEvent->AllocLine != NOT_ALLOCATED) + ++result; + pEvent = pEvent->pNext; + } + } + + return result; +} + +/** Retrieve the number of memory deallocations recorded for a given pointer. */ +unsigned int test_cunit_get_n_deallocations(void* pLocation) +{ + PMEMORY_NODE pNode = find_memory_node(pLocation); + PMEMORY_EVENT pEvent = NULL; + int result = 0; + + if (NULL != pNode) { + pEvent = pNode->pFirstEvent; + while (NULL != pEvent) { + if (pEvent->DeallocLine != NOT_DELETED) + ++result; + pEvent = pEvent->pNext; + } + } + + return result; +} + +void test_CU_calloc(void) +{ + void* ptr1 = NULL; + void* ptr2 = calloc(2, sizeof(int)); + unsigned int n2 = test_cunit_get_n_memevents(ptr2); + + /* test allocation failure */ + test_cunit_deactivate_malloc(); + ptr1 = CU_CALLOC(2, sizeof(int)); + TEST(NULL == ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + test_cunit_activate_malloc(); + + /* normal allocation */ + ptr1 = CU_CALLOC(2, sizeof(int)); + TEST_FATAL(NULL != ptr1); + TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + + CU_FREE(ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(n2 == test_cunit_get_n_memevents(ptr2)); + + free(ptr2); +} + +void test_CU_malloc(void) +{ + void* ptr1 = NULL; + void* ptr2 = malloc(sizeof(int)); + unsigned int n2 = test_cunit_get_n_memevents(ptr2); + + /* test allocation failure */ + test_cunit_deactivate_malloc(); + ptr1 = CU_MALLOC(sizeof(int)); + TEST(NULL == ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + test_cunit_activate_malloc(); + + /* normal allocation */ + ptr1 = CU_MALLOC(sizeof(int)); + TEST_FATAL(NULL != ptr1); + TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + + CU_FREE(ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(n2 == test_cunit_get_n_memevents(ptr2)); + + free(ptr2); +} + +void test_CU_free(void) +{ + /* covered by other test functions */ +} + +void test_CU_realloc(void) +{ + void* ptr1 = CU_MALLOC(sizeof(int)); + void* ptr2 = malloc(sizeof(int)); + void* ptr3; + void* ptr4; + unsigned int n2 = test_cunit_get_n_memevents(ptr2); + + /* test allocation failure */ + test_cunit_deactivate_malloc(); + ptr1 = CU_REALLOC(ptr1, sizeof(long int)); + TEST(NULL == ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + test_cunit_activate_malloc(); + + /* normal allocation */ + ptr3 = CU_MALLOC(sizeof(int)); + TEST_FATAL(NULL != ptr3); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(test_cunit_get_n_allocations(ptr3) != test_cunit_get_n_deallocations(ptr3)); + + ptr4 = CU_REALLOC(ptr3, sizeof(long int)); + TEST_FATAL(NULL != ptr4); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + if (ptr3 != ptr4) + TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); + TEST(test_cunit_get_n_allocations(ptr4) != test_cunit_get_n_deallocations(ptr4)); + + CU_FREE(ptr4); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); + TEST(test_cunit_get_n_allocations(ptr4) == test_cunit_get_n_deallocations(ptr4)); + TEST(n2 == test_cunit_get_n_memevents(ptr2)); + + free(ptr2); +} + +/** The main internal testing function for MyMem.c. */ +void test_cunit_MyMem(void) +{ + test_cunit_start_tests("MyMem.c"); + + test_CU_calloc(); + test_CU_malloc(); + test_CU_free(); + test_CU_realloc(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/MyMem.cpp b/gearsyncd/tests/cunit/MyMem.cpp new file mode 100644 index 0000000000..7ff8a3af9b --- /dev/null +++ b/gearsyncd/tests/cunit/MyMem.cpp @@ -0,0 +1,585 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Memory management functions used throughout CUnit. + * + * 13/Oct/2001 Moved some of the generic functions definitions from other + * files to this one so as to use the functions consitently. + * This file is not included in the distribution headers because + * it is used internally by CUnit. (AK) + * + * 18-Jul-2004 New interface, doxygen comments, made local functions & + * constants static, fixed reporting of memory tracking (valid + * vs invalid cycles), restructured memory tracking to detect + * reallocations & multiple deletions. (JDS) + * + * 24-Apr-2005 Changed type of allocated sizes to size_t to avoid + * signed-unsigned mismatch. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Memory management & reporting functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "MyMem.h" +#include "CUnit_intl.h" + +#ifdef MEMTRACE + +#define MAX_FILE_NAME_LENGTH 256 + +/** Default name for memory dump file. */ +static const char* f_szDefaultDumpFileName = "CUnit-Memory-Dump.xml"; +/**< Default name for memory dump file. */ + +#ifdef CUNIT_BUILD_TESTS +/** For testing use (only) to simulate memory exhaustion - + * if CU_FALSE, allocation requests will always fail and return NULL. + */ +static CU_BOOL f_bTestCunitMallocActive = CU_TRUE; +#endif + +/** Structure holding the details of a memory allocation/deallocation event. */ +typedef struct mem_event { + size_t Size; + unsigned int AllocLine; + char AllocFilename[MAX_FILE_NAME_LENGTH]; + unsigned int DeallocLine; + char DeallocFilename[MAX_FILE_NAME_LENGTH]; + struct mem_event* pNext; +} MEMORY_EVENT; +typedef MEMORY_EVENT* PMEMORY_EVENT; + +#define NOT_ALLOCATED 0 +#define NOT_DELETED 0 + +/** Structure holding the details of a memory node having allocation/deallocation events. */ +typedef struct mem_node { + void* pLocation; + unsigned int EventCount; + PMEMORY_EVENT pFirstEvent; + struct mem_node* pNext; +} MEMORY_NODE; +typedef MEMORY_NODE* PMEMORY_NODE; + +static PMEMORY_NODE f_pMemoryTrackerHead = NULL; /**< Head of double-linked list of memory nodes. */ +static unsigned int f_nMemoryNodes = 0; /**< Counter for memory nodes created. */ +/*------------------------------------------------------------------------*/ +/** Locate the memory node for the specified memory location (returns NULL if none). */ +static PMEMORY_NODE find_memory_node(void* pLocation) +{ + PMEMORY_NODE pMemoryNode = f_pMemoryTrackerHead; + while (NULL != pMemoryNode) { + if (pLocation == pMemoryNode->pLocation) { + break; + } + pMemoryNode = pMemoryNode->pNext; + } + + return pMemoryNode; +} +/*------------------------------------------------------------------------*/ +/** Create a new memory node for the specified memory location. */ +static PMEMORY_NODE create_memory_node(void* pLocation) +{ + PMEMORY_NODE pTempNode = NULL; + PMEMORY_NODE pMemoryNode = find_memory_node(pLocation); + + /* a memory node for pLocation should not exist yet */ + if (NULL == pMemoryNode) { + + pMemoryNode = (PMEMORY_NODE)malloc(sizeof(MEMORY_NODE)); + assert(NULL != pMemoryNode); + + pMemoryNode->pLocation = pLocation; + pMemoryNode->EventCount = 0; + pMemoryNode->pFirstEvent = NULL; + pMemoryNode->pNext = NULL; + + /* add new node to linked list */ + pTempNode = f_pMemoryTrackerHead; + if (NULL == pTempNode) { + f_pMemoryTrackerHead = pMemoryNode; + } + else { + while (NULL != pTempNode->pNext) { + pTempNode = pTempNode->pNext; + } + pTempNode->pNext = pMemoryNode; + } + + ++f_nMemoryNodes; + } + return pMemoryNode; +} +/*------------------------------------------------------------------------*/ +/** Add a new memory event having the specified parameters. */ +static PMEMORY_EVENT add_memory_event(PMEMORY_NODE pMemoryNode, + size_t size, + unsigned int alloc_line, + const char* alloc_filename) +{ + PMEMORY_EVENT pMemoryEvent = NULL; + PMEMORY_EVENT pTempEvent = NULL; + + assert (NULL != pMemoryNode); + + /* create and set up the new event */ + pMemoryEvent = (PMEMORY_EVENT)malloc(sizeof(MEMORY_EVENT)); + assert(NULL != pMemoryEvent); + + pMemoryEvent->Size = size; + pMemoryEvent->AllocLine = alloc_line; + strncpy(pMemoryEvent->AllocFilename, alloc_filename, (size_t) MAX_FILE_NAME_LENGTH-1); + pMemoryEvent->AllocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; + pMemoryEvent->DeallocLine = NOT_DELETED; + pMemoryEvent->DeallocFilename[0] = (char)0; + pMemoryEvent->pNext = NULL; + + /* add the new event to the end of the linked list */ + pTempEvent = pMemoryNode->pFirstEvent; + if (NULL == pTempEvent) { + pMemoryNode->pFirstEvent = pMemoryEvent; + } + else { + while (NULL != pTempEvent->pNext) { + pTempEvent = pTempEvent->pNext; + } + pTempEvent->pNext = pMemoryEvent; + } + + ++pMemoryNode->EventCount; + + return pMemoryEvent; +} +/*------------------------------------------------------------------------*/ +/** Record memory allocation event. */ +static PMEMORY_NODE allocate_memory(size_t nSize, + void* pLocation, + unsigned int uiAllocationLine, + const char* szAllocationFile) +{ + PMEMORY_NODE pMemoryNode = NULL; + + /* attempt to locate an existing record for this pLocation */ + pMemoryNode = find_memory_node(pLocation); + + /* pLocation not found - create a new event record */ + if (NULL == pMemoryNode) { + pMemoryNode = create_memory_node(pLocation); + } + + /* add the new event record */ + add_memory_event(pMemoryNode, nSize, uiAllocationLine, szAllocationFile); + + return pMemoryNode; +} + +/*------------------------------------------------------------------------*/ +/** Record memory deallocation event. */ +static void deallocate_memory(void* pLocation, unsigned int uiDeletionLine, const char* szDeletionFileName) +{ + PMEMORY_NODE pMemoryNode = NULL; + PMEMORY_EVENT pTempEvent = NULL; + + assert(0 != uiDeletionLine); + assert(NULL != szDeletionFileName); + + /* attempt to locate an existing record for this pLocation */ + pMemoryNode = find_memory_node(pLocation); + + /* if no entry, then an unallocated pointer was freed */ + if (NULL == pMemoryNode) { + pMemoryNode = create_memory_node(pLocation); + pTempEvent = add_memory_event(pMemoryNode, 0, NOT_ALLOCATED, ""); + } + else { + /* there should always be at least 1 event for an existing memory node */ + assert(NULL != pMemoryNode->pFirstEvent); + + /* locate last memory event for this pLocation */ + pTempEvent = pMemoryNode->pFirstEvent; + while (NULL != pTempEvent->pNext) { + pTempEvent = pTempEvent->pNext; + } + + /* if pointer has already been freed, create a new event for double deletion */ + if (NOT_DELETED != pTempEvent->DeallocLine) { + pTempEvent = add_memory_event(pMemoryNode, pTempEvent->Size, NOT_ALLOCATED, ""); + } + } + + pTempEvent->DeallocLine = uiDeletionLine; + strncpy(pTempEvent->DeallocFilename, szDeletionFileName, MAX_FILE_NAME_LENGTH-1); + pTempEvent->DeallocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; +} + +/*------------------------------------------------------------------------*/ +/** Custom calloc function with memory event recording. */ +void* CU_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName) +{ + void* pVoid = NULL; + +#ifdef CUNIT_BUILD_TESTS + if (CU_FALSE == f_bTestCunitMallocActive) { + return NULL; + } +#endif + + pVoid = calloc(nmemb, size); + if (NULL != pVoid) { + allocate_memory(nmemb * size, pVoid, uiLine, szFileName); + } + + return pVoid; +} + +/*------------------------------------------------------------------------*/ +/** Custom malloc function with memory event recording. */ +void* CU_malloc(size_t size, unsigned int uiLine, const char* szFileName) +{ + void* pVoid = NULL; + +#ifdef CUNIT_BUILD_TESTS + if (CU_FALSE == f_bTestCunitMallocActive) { + return NULL; + } +#endif + + pVoid = malloc(size); + if (NULL != pVoid) { + allocate_memory(size, pVoid, uiLine, szFileName); + } + + return pVoid; +} + +/*------------------------------------------------------------------------*/ +/** Custom free function with memory event recording. */ +void CU_free(void *ptr, unsigned int uiLine, const char* szFileName) +{ + deallocate_memory(ptr, uiLine, szFileName); + free(ptr); +} + +/*------------------------------------------------------------------------*/ +/** Custom realloc function with memory event recording. */ +void* CU_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName) +{ + void* pVoid = NULL; + + deallocate_memory(ptr, uiLine, szFileName); + +#ifdef CUNIT_BUILD_TESTS + if (CU_FALSE == f_bTestCunitMallocActive) { + free(ptr); + return NULL; + } +#endif + + pVoid = realloc(ptr, size); + + if (NULL != pVoid) { + allocate_memory(size, pVoid, uiLine, szFileName); + } + + return pVoid; +} + +/*------------------------------------------------------------------------*/ +/** Print a report of memory events to file. */ +void CU_dump_memory_usage(const char* szFilename) +{ + char* szDumpFileName = (char*)f_szDefaultDumpFileName; + unsigned int nValid; + unsigned int nInvalid; + PMEMORY_NODE pTempNode = NULL; + PMEMORY_EVENT pTempEvent = NULL; + FILE* pFile = NULL; + time_t tTime = 0; + + /* use the specified file name, if supplied) */ + if ((NULL != szFilename) && strlen(szFilename) > 0) { + szDumpFileName = (char*)szFilename; + } + + if (NULL == (pFile = fopen(szDumpFileName, "w"))) { + fprintf(stderr, _("Failed to open file \"%s\" : %s"), szDumpFileName, strerror(errno)); + return; + } + + setvbuf(pFile, NULL, _IONBF, 0); + + fprintf(pFile, "<\?xml version=\"1.0\" \?>"); + fprintf(pFile, "\n<\?xml-stylesheet type=\"text/xsl\" href=\"Memory-Dump.xsl\" \?>"); + fprintf(pFile, "\n"); + fprintf(pFile, "\n"); + fprintf(pFile, "\n "); + fprintf(pFile, "\n "); + + nValid = 0; + nInvalid = 0; + pTempNode = f_pMemoryTrackerHead; + while (NULL != pTempNode) { + fprintf(pFile, "\n "); + fprintf(pFile, "\n %p ", pTempNode->pLocation); + fprintf(pFile, "\n %u ", pTempNode->EventCount); + + pTempEvent = pTempNode->pFirstEvent; + while (NULL != pTempEvent) { + fprintf(pFile, "\n "); + fprintf(pFile, "\n %u ", pTempEvent->Size); + fprintf(pFile, "\n %s ", pTempEvent->AllocFilename); + fprintf(pFile, "\n %u ", pTempEvent->AllocLine); + fprintf(pFile, "\n %s ", pTempEvent->DeallocFilename); + fprintf(pFile, "\n %u ", pTempEvent->DeallocLine); + fprintf(pFile, "\n "); + + if ((0 != pTempEvent->AllocLine) && (0 != pTempEvent->DeallocLine)) { + ++nValid; + } + else { + ++nInvalid; + } + + pTempEvent = pTempEvent->pNext; + } + + fprintf(pFile, "\n "); + pTempNode = pTempNode->pNext; + } + + fprintf(pFile, "\n "); + + fprintf(pFile, "\n "); + fprintf(pFile, "\n %u ", nValid); + fprintf(pFile, "\n %u ", nInvalid); + fprintf(pFile, "\n %u ", nValid + nInvalid); + fprintf(pFile, "\n "); + + time(&tTime); + fprintf(pFile, "\n Memory Trace for CUnit Run at %s ", ctime(&tTime)); + fprintf(pFile, ""); + + fclose(pFile); +} + +#endif /* MEMTRACE */ + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +/** Deactivate CUnit memory allocation + * After calling this function, all Cunit memory + * allocation routines will fail and return NULL. + */ +void test_cunit_deactivate_malloc(void) +{ + f_bTestCunitMallocActive = CU_FALSE; +} + +/** Activate CUnit memory allocation + * After calling this function, all Cunit memory + * allocation routines will behave normally (allocating + * memory if it is available). + */ +void test_cunit_activate_malloc(void) +{ + f_bTestCunitMallocActive = CU_TRUE; +} + +/** Retrieve the number of memory events recorded for a given pointer. */ +unsigned int test_cunit_get_n_memevents(void* pLocation) +{ + PMEMORY_NODE pNode = find_memory_node(pLocation); + return (pNode) ? pNode->EventCount : 0; +} + +/** Retrieve the number of memory allocations recorded for a given pointer. */ +unsigned int test_cunit_get_n_allocations(void* pLocation) +{ + PMEMORY_NODE pNode = find_memory_node(pLocation); + PMEMORY_EVENT pEvent = NULL; + int result = 0; + + if (NULL != pNode) { + pEvent = pNode->pFirstEvent; + while (NULL != pEvent) { + if (pEvent->AllocLine != NOT_ALLOCATED) + ++result; + pEvent = pEvent->pNext; + } + } + + return result; +} + +/** Retrieve the number of memory deallocations recorded for a given pointer. */ +unsigned int test_cunit_get_n_deallocations(void* pLocation) +{ + PMEMORY_NODE pNode = find_memory_node(pLocation); + PMEMORY_EVENT pEvent = NULL; + int result = 0; + + if (NULL != pNode) { + pEvent = pNode->pFirstEvent; + while (NULL != pEvent) { + if (pEvent->DeallocLine != NOT_DELETED) + ++result; + pEvent = pEvent->pNext; + } + } + + return result; +} + +void test_CU_calloc(void) +{ + void* ptr1 = NULL; + void* ptr2 = calloc(2, sizeof(int)); + unsigned int n2 = test_cunit_get_n_memevents(ptr2); + + /* test allocation failure */ + test_cunit_deactivate_malloc(); + ptr1 = CU_CALLOC(2, sizeof(int)); + TEST(NULL == ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + test_cunit_activate_malloc(); + + /* normal allocation */ + ptr1 = CU_CALLOC(2, sizeof(int)); + TEST_FATAL(NULL != ptr1); + TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + + CU_FREE(ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(n2 == test_cunit_get_n_memevents(ptr2)); + + free(ptr2); +} + +void test_CU_malloc(void) +{ + void* ptr1 = NULL; + void* ptr2 = malloc(sizeof(int)); + unsigned int n2 = test_cunit_get_n_memevents(ptr2); + + /* test allocation failure */ + test_cunit_deactivate_malloc(); + ptr1 = CU_MALLOC(sizeof(int)); + TEST(NULL == ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + test_cunit_activate_malloc(); + + /* normal allocation */ + ptr1 = CU_MALLOC(sizeof(int)); + TEST_FATAL(NULL != ptr1); + TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + + CU_FREE(ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(n2 == test_cunit_get_n_memevents(ptr2)); + + free(ptr2); +} + +void test_CU_free(void) +{ + /* covered by other test functions */ +} + +void test_CU_realloc(void) +{ + void* ptr1 = CU_MALLOC(sizeof(int)); + void* ptr2 = malloc(sizeof(int)); + void* ptr3; + void* ptr4; + unsigned int n2 = test_cunit_get_n_memevents(ptr2); + + /* test allocation failure */ + test_cunit_deactivate_malloc(); + ptr1 = CU_REALLOC(ptr1, sizeof(long int)); + TEST(NULL == ptr1); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + test_cunit_activate_malloc(); + + /* normal allocation */ + ptr3 = CU_MALLOC(sizeof(int)); + TEST_FATAL(NULL != ptr3); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(test_cunit_get_n_allocations(ptr3) != test_cunit_get_n_deallocations(ptr3)); + + ptr4 = CU_REALLOC(ptr3, sizeof(long int)); + TEST_FATAL(NULL != ptr4); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + if (ptr3 != ptr4) + TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); + TEST(test_cunit_get_n_allocations(ptr4) != test_cunit_get_n_deallocations(ptr4)); + + CU_FREE(ptr4); + TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); + TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); + TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); + TEST(test_cunit_get_n_allocations(ptr4) == test_cunit_get_n_deallocations(ptr4)); + TEST(n2 == test_cunit_get_n_memevents(ptr2)); + + free(ptr2); +} + +/** The main internal testing function for MyMem.c. */ +void test_cunit_MyMem(void) +{ + test_cunit_start_tests("MyMem.c"); + + test_CU_calloc(); + test_CU_malloc(); + test_CU_free(); + test_CU_realloc(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/MyMem.h b/gearsyncd/tests/cunit/MyMem.h new file mode 100644 index 0000000000..88a7e625e4 --- /dev/null +++ b/gearsyncd/tests/cunit/MyMem.h @@ -0,0 +1,104 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Contains Memory Related Defines to use internal routines to detect Memory Leak + * in Debug Versions + * + * 18/Jun/2002 Memory Debug Functions. (AK) + * + * 17-Jul-2004 New interface for global function names. (JDS) + * + * 05-Sep-2004 Added internal test interface. (JDS) + */ + +/** @file + * Memory management functions (user interface). + * Two versions of memory allocation/deallocation are available. + * If compiled with MEMTRACE defined, CUnit keeps track of all + * system allocations & deallocations. The memory record can + * then be reported using CU_CREATE_MEMORY_REPORT. Otherwise, + * standard system memory allocation is used without tracing. + */ +/** @addtogroup Framework + * @{ + */ + +#ifndef CUNIT_MYMEM_H_SEEN +#define CUNIT_MYMEM_H_SEEN + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef MEMTRACE + void* CU_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName); + void* CU_malloc(size_t size, unsigned int uiLine, const char* szFileName); + void CU_free(void *ptr, unsigned int uiLine, const char* szFileName); + void* CU_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName); + CU_EXPORT void CU_dump_memory_usage(const char*); + + /** c-allocate with memory tracking. */ + #define CU_CALLOC(x, y) CU_calloc((x), (y), __LINE__, __FILE__) + /** m-allocate with memory tracking. */ + #define CU_MALLOC(x) CU_malloc((x), __LINE__, __FILE__) + /** Free with memory tracking. */ + #define CU_FREE(x) CU_free((x), __LINE__, __FILE__) + /** Reallocate with memory tracking. */ + #define CU_REALLOC(x, y) CU_realloc((x), (y), __LINE__, __FILE__) + /** Generate report on tracked memory. */ + #define CU_CREATE_MEMORY_REPORT(x) CU_dump_memory_usage((x)) + /** Generate report on tracked memory (old macro). */ + #define CU_DUMP_MEMORY_USAGE(x) CU_dump_memory_usage((x)) +#else /* MEMTRACE */ + /** Standard calloc() if MEMTRACE not defined. */ + #define CU_CALLOC(x, y) calloc((x), (y)) + /** Standard malloc() if MEMTRACE not defined. */ + #define CU_MALLOC(x) malloc((x)) + /** Standard free() if MEMTRACE not defined. */ + #define CU_FREE(x) free((x)) + /** Standard realloc() if MEMTRACE not defined. */ + #define CU_REALLOC(x, y) realloc((x), (y)) + /** No-op if MEMTRACE not defined. */ + #define CU_CREATE_MEMORY_REPORT(x) + /** No-op if MEMTRACE not defined. */ + #define CU_DUMP_MEMORY_USAGE(x) +#endif /* MEMTRACE */ + +#ifdef CUNIT_BUILD_TESTS +/** Disable memory allocation for testing purposes. */ +void test_cunit_deactivate_malloc(void); +/** Enable memory allocation for testing purposes. */ +void test_cunit_activate_malloc(void); +/** Retrieve number of memory events for a given pointer */ +unsigned int test_cunit_get_n_memevents(void* pLocation); +/** Retrieve number of allocations for a given pointer */ +unsigned int test_cunit_get_n_allocations(void* pLocation); +/** Retrieve number of deallocations for a given pointer */ +unsigned int test_cunit_get_n_deallocations(void* pLocation); + +void test_cunit_MyMem(void); +#endif /* CUNIT_BUILD_TESTS */ + +#ifdef __cplusplus +} +#endif +#endif /* CUNIT_MYMEM_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/cunit/TestDB.c b/gearsyncd/tests/cunit/TestDB.c new file mode 100644 index 0000000000..d609f50549 --- /dev/null +++ b/gearsyncd/tests/cunit/TestDB.c @@ -0,0 +1,2773 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004,2005,2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of Registry/TestGroup/Testcase management Routines. + * + * Aug 2001 Initial implementation (AK) + * + * 09/Aug/2001 Added startup initialize/cleanup registry functions. (AK) + * + * 29/Aug/2001 Added Test and Group Add functions. (AK) + * + * 02/Oct/2001 Added Proper Error codes and Messages on the failure conditions. (AK) + * + * 13/Oct/2001 Added Code to Check for the Duplicate Group name and test name. (AK) + * + * 15-Jul-2004 Added doxygen comments, new interface, added assertions to + * internal functions, moved error handling code to CUError.c, + * added assertions to make sure no modification of registry + * during a run, bug fixes, changed CU_set_registry() so that it + * doesn't require cleaning the existing registry. (JDS) + * + * 24-Apr-2006 Removed constraint that suites/tests be uniquely named. + * Added ability to turn individual tests/suites on or off. + * Added lookup functions for suites/tests based on index. + * Moved doxygen comments for public API here to header. + * Modified internal unit tests to include these changes. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Management functions for tests, suites, and the test registry (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "MyMem.h" +#include "TestDB.h" +#include "TestRun.h" +#include "Util.h" +#include "CUnit_intl.h" + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +static CU_pTestRegistry f_pTestRegistry = NULL; /**< The active internal Test Registry. */ + +/*================================================================= + * Private function forward declarations + *=================================================================*/ +static void cleanup_test_registry(CU_pTestRegistry pRegistry); +static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean); +static void cleanup_suite(CU_pSuite pSuite); +static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite); +static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc); +static void cleanup_test(CU_pTest pTest); +static void insert_test(CU_pSuite pSuite, CU_pTest pTest); + +static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName); +static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +CU_ErrorCode CU_initialize_registry(void) +{ + CU_ErrorCode result; + + assert(CU_FALSE == CU_is_test_running()); + + CU_set_error(result = CUE_SUCCESS); + + if (NULL != f_pTestRegistry) { + CU_cleanup_registry(); + } + + f_pTestRegistry = CU_create_new_registry(); + if (NULL == f_pTestRegistry) { + CU_set_error(result = CUE_NOMEMORY); + } + + return result; +} + +/*------------------------------------------------------------------------*/ +CU_BOOL CU_registry_initialized(void) +{ + return (NULL == f_pTestRegistry) ? CU_FALSE : CU_TRUE; +} + +/*------------------------------------------------------------------------*/ +void CU_cleanup_registry(void) +{ + assert(CU_FALSE == CU_is_test_running()); + + CU_set_error(CUE_SUCCESS); + CU_destroy_existing_registry(&f_pTestRegistry); /* supposed to handle NULL ok */ + CU_clear_previous_results(); + CU_CREATE_MEMORY_REPORT(NULL); +} + +/*------------------------------------------------------------------------*/ +CU_pTestRegistry CU_get_registry(void) +{ + return f_pTestRegistry; +} + +/*------------------------------------------------------------------------*/ +CU_pTestRegistry CU_set_registry(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = f_pTestRegistry; + + assert(CU_FALSE == CU_is_test_running()); + + CU_set_error(CUE_SUCCESS); + f_pTestRegistry = pRegistry; + return pOldRegistry; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) +{ + CU_pSuite pRetValue = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + assert(CU_FALSE == CU_is_test_running()); + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + pRetValue = create_suite(strName, pInit, pClean); + if (NULL == pRetValue) { + error = CUE_NOMEMORY; + } + else { + if (CU_TRUE == suite_exists(f_pTestRegistry, strName)) { + error = CUE_DUP_SUITE; + } + insert_suite(f_pTestRegistry, pRetValue); + } + } + + CU_set_error(error); + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + pSuite->fActive = fNewActive; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else if (NULL == strNewName) { + result = CUE_NO_SUITENAME; + } + else { + CU_FREE(pSuite->pName); + pSuite->pName = (char *)CU_MALLOC(strlen(strNewName)+1); + strcpy(pSuite->pName, strNewName); + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + pSuite->pInitializeFunc = pNewInit; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + pSuite->pCleanupFunc = pNewClean; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite(const char *strName) +{ + CU_pSuite result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + result = CU_get_suite_by_name(strName, f_pTestRegistry); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite_at_pos(unsigned int pos) +{ + CU_pSuite result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else { + result = CU_get_suite_by_index(pos, f_pTestRegistry); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_suite_pos(CU_pSuite pSuite) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pSuite pCurrentSuite = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else { + pCurrentSuite = f_pTestRegistry->pSuite; + result = 1; + while ((NULL != pCurrentSuite) && (pCurrentSuite != pSuite)) { + ++result; + pCurrentSuite = pCurrentSuite->pNext; + } + if (pCurrentSuite == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_suite_pos_by_name(const char *strName) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pSuite pCurrentSuite = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + pCurrentSuite = f_pTestRegistry->pSuite; + result = 1; + while ((NULL != pCurrentSuite) && (0 != strcmp(pCurrentSuite->pName, strName))) { + ++result; + pCurrentSuite = pCurrentSuite->pNext; + } + if (pCurrentSuite == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc) +{ + CU_pTest pRetValue = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + assert(CU_FALSE == CU_is_test_running()); + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == strName) { + error = CUE_NO_TESTNAME; + } + else if(NULL == pTestFunc) { + error = CUE_NOTEST; + } + else { + pRetValue = create_test(strName, pTestFunc); + if (NULL == pRetValue) { + error = CUE_NOMEMORY; + } + else { + f_pTestRegistry->uiNumberOfTests++; + if (CU_TRUE == test_exists(pSuite, strName)) { + error = CUE_DUP_TEST; + } + insert_test(pSuite, pRetValue); + } + } + + CU_set_error(error); + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_test_active(CU_pTest pTest, CU_BOOL fNewActive) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pTest) { + result = CUE_NOTEST; + } + else { + pTest->fActive = fNewActive; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pTest) { + result = CUE_NOTEST; + } + else if (NULL == strNewName) { + result = CUE_NO_TESTNAME; + } + else { + CU_FREE(pTest->pName); + pTest->pName = (char *)CU_MALLOC(strlen(strNewName)+1); + strcpy(pTest->pName, strNewName); + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if ((NULL == pTest) || (NULL == pNewFunc)) { + result = CUE_NOTEST; + } + else { + pTest->pTestFunc = pNewFunc; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName) +{ + CU_pTest result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + result = CU_get_test_by_name(strName, pSuite); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos) +{ + CU_pTest result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else { + result = CU_get_test_by_index(pos, pSuite); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pTest pCurrentTest = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == pTest) { + error = CUE_NOTEST; + } + else { + pCurrentTest = pSuite->pTest; + result = 1; + while ((NULL != pCurrentTest) && (pCurrentTest != pTest)) { + ++result; + pCurrentTest = pCurrentTest->pNext; + } + if (pCurrentTest == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pTest pCurrentTest = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == strName) { + error = CUE_NO_TESTNAME; + } + else { + pCurrentTest = pSuite->pTest; + result = 1; + while ((NULL != pCurrentTest) && (0 != strcmp(pCurrentTest->pName, strName))) { + ++result; + pCurrentTest = pCurrentTest->pNext; + } + if (pCurrentTest == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +/* This section is based conceptually on code + * Copyright (C) 2004 Aurema Pty Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Derived from code contributed by K. Cheung and Aurema Pty Ltd. (thanks!) + * int test_group_register(test_group_t *tg) + * int test_suite_register(test_suite_t *ts) + */ +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_register_nsuites(int suite_count, ...) +{ + CU_SuiteInfo *pSuiteItem = NULL; + CU_TestInfo *pTestItem = NULL; + CU_pSuite pSuite = NULL; + + va_list argptr; + int i; + + va_start(argptr, suite_count); + + for (i=0 ; ipName; pSuiteItem++) { + if (NULL != (pSuite = CU_add_suite(pSuiteItem->pName, pSuiteItem->pInitFunc, pSuiteItem->pCleanupFunc))) { + for (pTestItem = pSuiteItem->pTests; NULL != pTestItem->pName; pTestItem++) { + if (NULL == CU_add_test(pSuite, pTestItem->pName, pTestItem->pTestFunc)) { + return CU_get_error(); + } + } + } + else { + return CU_get_error(); + } + } + } + } + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]) +{ + return CU_register_nsuites(1, suite_info); +} +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*================================================================= + * Private static function definitions + *=================================================================*/ +/*------------------------------------------------------------------------*/ +/** + * Internal function to clean up the specified test registry. + * cleanup_suite() will be called for each registered suite to perform + * cleanup of the associated test cases. Then, the suite's memory will + * be freed. Note that any pointers to tests or suites in pRegistry + * held by the user will be invalidated by this function. Severe problems + * can occur if this function is called during a test run involving pRegistry. + * Note that memory held for data members in the registry (e.g. pName) and + * the registry itself are not freed by this function. + * + * @see cleanup_suite() + * @see cleanup_test() + * @param pRegistry CU_pTestRegistry to clean up (non-NULL). + */ +static void cleanup_test_registry(CU_pTestRegistry pRegistry) +{ + CU_pSuite pCurSuite = NULL; + CU_pSuite pNextSuite = NULL; + + assert(NULL != pRegistry); + + pCurSuite = pRegistry->pSuite; + while (NULL != pCurSuite) { + pNextSuite = pCurSuite->pNext; + cleanup_suite(pCurSuite); + + CU_FREE(pCurSuite); + pCurSuite = pNextSuite; + } + pRegistry->pSuite = NULL; + pRegistry->uiNumberOfSuites = 0; + pRegistry->uiNumberOfTests = 0; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to create a new test suite having the specified parameters. + * This function creates a new test suite having the specified name and + * initialization/cleanup functions. The new suite is active for execution during + * test runs. The strName cannot be NULL (checked by assertion), but either or + * both function pointers can be. A pointer to the newly-created suite is returned, + * or NULL if there was an error allocating memory for the new suite. It is the + * responsibility of the caller to destroy the returned suite (use cleanup_suite() + * before freeing the returned pointer). + * + * @param strName Name for the new test suite (non-NULL). + * @param pInit Initialization function to call before running suite. + * @param pClean Cleanup function to call after running suite. + * @return A pointer to the newly-created suite (NULL if creation failed) + */ +static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) +{ + CU_pSuite pRetValue = (CU_pSuite)CU_MALLOC(sizeof(CU_Suite)); + + assert(NULL != strName); + + if (NULL != pRetValue) { + pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); + if (NULL != pRetValue->pName) { + strcpy(pRetValue->pName, strName); + pRetValue->fActive = CU_TRUE; + pRetValue->pInitializeFunc = pInit; + pRetValue->pCleanupFunc = pClean; + pRetValue->pTest = NULL; + pRetValue->pNext = NULL; + pRetValue->pPrev = NULL; + pRetValue->uiNumberOfTests = 0; + } + else { + CU_FREE(pRetValue); + pRetValue = NULL; + } + } + + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to clean up the specified test suite. + * Each test case registered with pSuite will be freed. Allocated memory held + * by the suite (i.e. the name) will also be deallocated. Severe problems can + * occur if this function is called during a test run involving pSuite. + * + * @param pSuite CU_pSuite to clean up (non-NULL). + * @see cleanup_test_registry() + * @see cleanup_test() + */ +static void cleanup_suite(CU_pSuite pSuite) +{ + CU_pTest pCurTest = NULL; + CU_pTest pNextTest = NULL; + + assert(NULL != pSuite); + + pCurTest = pSuite->pTest; + while (NULL != pCurTest) { + pNextTest = pCurTest->pNext; + + cleanup_test(pCurTest); + + CU_FREE(pCurTest); + pCurTest = pNextTest; + } + if (NULL != pSuite->pName) { + CU_FREE(pSuite->pName); + } + + pSuite->pName = NULL; + pSuite->pTest = NULL; + pSuite->uiNumberOfTests = 0; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to insert a suite into a registry. + * The suite name is assumed to be unique. Internally, the list of suites + * is a double-linked list, which this function manages. Insertion of NULL + * pSuites is not allowed (checked by assertion). Severe problems can occur + * if this function is called during a test run involving pRegistry. + * + * @param pRegistry CU_pTestRegistry to insert into (non-NULL). + * @param pSuite CU_pSuite to insert (non-NULL). + * @see insert_test() + */ +static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite) +{ + CU_pSuite pCurSuite = NULL; + + assert(NULL != pRegistry); + assert(NULL != pSuite); + + pCurSuite = pRegistry->pSuite; + + assert(pCurSuite != pSuite); + + pSuite->pNext = NULL; + pRegistry->uiNumberOfSuites++; + + /* if this is the 1st suite to be added... */ + if (NULL == pCurSuite) { + pRegistry->pSuite = pSuite; + pSuite->pPrev = NULL; + } + /* otherwise, add it to the end of the linked list... */ + else { + while (NULL != pCurSuite->pNext) { + pCurSuite = pCurSuite->pNext; + assert(pCurSuite != pSuite); + } + + pCurSuite->pNext = pSuite; + pSuite->pPrev = pCurSuite; + } +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to create a new test case having the specified parameters. + * This function creates a new test having the specified name and test function. + * The strName cannot be NULL (checked by assertion), but the function pointer + * may be. A pointer to the newly-created test is returned, or NULL if there + * was an error allocating memory for the new test. It is the responsibility + * of the caller to destroy the returned test (use cleanup_test() before freeing + * the returned pointer). + * + * @param strName Name for the new test. + * @param pTestFunc Test function to call when running this test. + * @return A pointer to the newly-created test (NULL if creation failed) + */ +static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc) +{ + CU_pTest pRetValue = (CU_pTest)CU_MALLOC(sizeof(CU_Test)); + + assert(NULL != strName); + + if (NULL != pRetValue) { + pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); + if (NULL != pRetValue->pName) { + strcpy(pRetValue->pName, strName); + pRetValue->fActive = CU_TRUE; + pRetValue->pTestFunc = pTestFunc; + pRetValue->pJumpBuf = NULL; + pRetValue->pNext = NULL; + pRetValue->pPrev = NULL; + } + else { + CU_FREE(pRetValue); + pRetValue = NULL; + } + } + + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to clean up the specified test. + * All memory associated with the test will be freed. Severe problems can + * occur if this function is called during a test run involving pTest. + * + * @param pTest CU_pTest to clean up (non-NULL). + * @see cleanup_test_registry() + * @see cleanup_suite() + */ +static void cleanup_test(CU_pTest pTest) +{ + assert(NULL != pTest); + + if (NULL != pTest->pName) { + CU_FREE(pTest->pName); + } + + pTest->pName = NULL; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to insert a test into a suite. + * The test name is assumed to be unique. Internally, the list of tests in + * a suite is a double-linked list, which this function manages. Neither + * pSuite nor pTest may be NULL (checked by assertion). Further, pTest must + * be an independent test (i.e. both pTest->pNext and pTest->pPrev == NULL), + * which is also checked by assertion. Severe problems can occur if this + * function is called during a test run involving pSuite. + * + * @param pSuite CU_pSuite to insert into (non-NULL). + * @param pTest CU_pTest to insert (non-NULL). + * @see insert_suite() + */ +static void insert_test(CU_pSuite pSuite, CU_pTest pTest) +{ + CU_pTest pCurTest = NULL; + + assert(NULL != pSuite); + assert(NULL != pTest); + assert(NULL == pTest->pNext); + assert(NULL == pTest->pPrev); + + pCurTest = pSuite->pTest; + + assert(pCurTest != pTest); + + pSuite->uiNumberOfTests++; + /* if this is the 1st suite to be added... */ + if (NULL == pCurTest) { + pSuite->pTest = pTest; + pTest->pPrev = NULL; + } + else { + while (NULL != pCurTest->pNext) { + pCurTest = pCurTest->pNext; + assert(pCurTest != pTest); + } + + pCurTest->pNext = pTest; + pTest->pPrev = pCurTest; + } +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to check whether a suite having a specified + * name already exists. + * + * @param pRegistry CU_pTestRegistry to check (non-NULL). + * @param szSuiteName Suite name to check (non-NULL). + * @return CU_TRUE if suite exists in the registry, CU_FALSE otherwise. + */ +static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName) +{ + CU_pSuite pSuite = NULL; + + assert(NULL != pRegistry); + assert(NULL != szSuiteName); + + pSuite = pRegistry->pSuite; + while (NULL != pSuite) { + if ((NULL != pSuite->pName) && (0 == CU_compare_strings(szSuiteName, pSuite->pName))) { + return CU_TRUE; + } + pSuite = pSuite->pNext; + } + + return CU_FALSE; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to check whether a test having a specified + * name is already registered in a given suite. + * + * @param pSuite CU_pSuite to check (non-NULL). + * @param szTestName Test case name to check (non-NULL). + * @return CU_TRUE if test exists in the suite, CU_FALSE otherwise. + */ +static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName) +{ + CU_pTest pTest = NULL; + + assert(NULL != pSuite); + assert(NULL != szTestName); + + pTest = pSuite->pTest; + while (NULL != pTest) { + if ((NULL != pTest->pName) && (0 == CU_compare_strings(szTestName, pTest->pName))) { + return CU_TRUE; + } + pTest = pTest->pNext; + } + + return CU_FALSE; +} + +/*================================================================= + * Public but primarily internal function definitions + *=================================================================*/ +CU_pTestRegistry CU_create_new_registry(void) +{ + CU_pTestRegistry pRegistry = (CU_pTestRegistry)CU_MALLOC(sizeof(CU_TestRegistry)); + if (NULL != pRegistry) { + pRegistry->pSuite = NULL; + pRegistry->uiNumberOfSuites = 0; + pRegistry->uiNumberOfTests = 0; + } + + return pRegistry; +} + +/*------------------------------------------------------------------------*/ +void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry) +{ + assert(NULL != ppRegistry); + + /* Note - CU_cleanup_registry counts on being able to pass NULL */ + + if (NULL != *ppRegistry) { + cleanup_test_registry(*ppRegistry); + } + CU_FREE(*ppRegistry); + *ppRegistry = NULL; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite_by_name(const char* szSuiteName, CU_pTestRegistry pRegistry) +{ + CU_pSuite pSuite = NULL; + CU_pSuite pCur = NULL; + + assert(NULL != pRegistry); + assert(NULL != szSuiteName); + + pCur = pRegistry->pSuite; + while (NULL != pCur) { + if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szSuiteName))) { + pSuite = pCur; + break; + } + pCur = pCur->pNext; + } + + return pSuite; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite_by_index(unsigned int index, CU_pTestRegistry pRegistry) +{ + CU_pSuite result = NULL; + unsigned int i; + + assert(NULL != pRegistry); + + if ((index > 0) && (index <= f_pTestRegistry->uiNumberOfSuites)) { + result = f_pTestRegistry->pSuite; + for (i=1 ; ipNext; + } + } + + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test_by_name(const char* szTestName, CU_pSuite pSuite) +{ + CU_pTest pTest = NULL; + CU_pTest pCur = NULL; + + assert(NULL != pSuite); + assert(NULL != szTestName); + + pCur = pSuite->pTest; + while (NULL != pCur) { + if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szTestName))) { + pTest = pCur; + break; + } + pCur = pCur->pNext; + } + + return pTest; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test_by_index(unsigned int index, CU_pSuite pSuite) +{ + CU_pTest result = NULL; + unsigned int i; + + assert(NULL != pSuite); + + if ((index > 0) && (index <= pSuite->uiNumberOfTests)) { + result = pSuite->pTest; + for (i=1 ; ipNext; + } + } + + return result; +} +/** @} */ + +/*------------------------------------------------------------------------*/ +/*------------------------------------------------------------------------*/ +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +static int sfunc1(void) +{ return 0; } + +static void test1(void) +{} + +static void test2(void) +{} + +/*--------------------------------------------------*/ +static void test_CU_initialize_registry(void) +{ + CU_pTestRegistry pReg = NULL; + unsigned int ndeallocs_before; + + /* initial state */ + TEST(NULL == CU_get_registry()); + TEST(CU_FALSE == CU_registry_initialized()); + + /* after normal initialization */ + TEST(CUE_SUCCESS == CU_initialize_registry()); + pReg = CU_get_registry(); + TEST_FATAL(NULL != pReg); + TEST(CU_TRUE == CU_registry_initialized()); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(NULL == pReg->pSuite); + + /* after reinitialization */ + TEST(0 < test_cunit_get_n_memevents(pReg)); + ndeallocs_before = test_cunit_get_n_deallocations(pReg); + TEST(CUE_SUCCESS == CU_initialize_registry()); + TEST((ndeallocs_before + 1) == test_cunit_get_n_deallocations(pReg)); + pReg = CU_get_registry(); + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(NULL == pReg->pSuite); + + /* after cleanup */ + CU_cleanup_registry(); + TEST(NULL == CU_get_registry()); + TEST(CU_FALSE == CU_registry_initialized()); + + /* if malloc fails */ + test_cunit_deactivate_malloc(); + TEST(CUE_NOMEMORY == CU_initialize_registry()); + TEST(NULL == CU_get_registry()); + TEST(CU_FALSE == CU_registry_initialized()); + test_cunit_activate_malloc(); +} + +/*--------------------------------------------------*/ +static void test_CU_cleanup_registry(void) +{ + /* make sure calling with uninitialized registry does not crash */ + CU_cleanup_registry(); + CU_cleanup_registry(); + CU_cleanup_registry(); + CU_cleanup_registry(); + CU_cleanup_registry(); + + /* nothing more to do over test_CU_initialize_registry() */ +} + +/*--------------------------------------------------*/ +/* test CU_add_suite() + * CU_get_suite_by_name() + * CU_get_suite_by_index() + */ +static void test_CU_add_suite(void) +{ + CU_pSuite pSuite = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTestRegistry pReg = NULL; + + CU_cleanup_registry(); /* make sure registry not initialized */ + + /* error condition - registry not initialized */ + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_NOREGISTRY == CU_get_error()); + TEST(NULL == pSuite); + + /* error condition - no name */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite = CU_add_suite(NULL, NULL, NULL); + TEST(CUE_NO_SUITENAME == CU_get_error()); + TEST(NULL == pSuite); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* warning condition - duplicate name */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + pSuite2 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_DUP_SUITE == CU_get_error()); + TEST(NULL != pSuite2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + TEST(!strcmp("suite1", pSuite->pName)); + TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite->pTest == NULL); /* no tests added yet */ + TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite->pNext == pSuite2); /* now have another suite */ + + TEST(!strcmp("suite1", pSuite2->pName)); + TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite2->pTest == NULL); /* no tests added yet */ + TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite2->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite2->pNext == NULL); /* end of the list */ + + /* error condition - memory allocation failure */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + test_cunit_deactivate_malloc(); + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_NOMEMORY == CU_get_error()); + TEST(NULL == pSuite); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + test_cunit_activate_malloc(); + + /* normal creation & cleanup */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == NULL); + + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite1", pReg) == pSuite); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == NULL); + TEST(pReg->pSuite == pSuite); + + TEST(!strcmp("suite1", pSuite->pName)); + TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite->pTest == NULL); /* no tests added yet */ + TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite->pNext == NULL); /* no more suites added yet */ + + pSuite2 = CU_add_suite("suite2", sfunc1, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite2", pReg) == pSuite2); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == pSuite2); + TEST(CU_get_suite_by_index(3, pReg) == NULL); + + pSuite3 = CU_add_suite("suite3", NULL, sfunc1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite3); + TEST(3 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite3", pReg) == pSuite3); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == pSuite2); + TEST(CU_get_suite_by_index(3, pReg) == pSuite3); + TEST(CU_get_suite_by_index(4, pReg) == NULL); + + pSuite4 = CU_add_suite("suite4", sfunc1, sfunc1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite4); + TEST(4 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite4", pReg) == pSuite4); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == pSuite2); + TEST(CU_get_suite_by_index(3, pReg) == pSuite3); + TEST(CU_get_suite_by_index(4, pReg) == pSuite4); + TEST(CU_get_suite_by_index(5, pReg) == NULL); + + /* test registry suite structures */ + TEST(pReg->pSuite == pSuite); + + TEST(!strcmp("suite1", pSuite->pName)); + TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite->pTest == NULL); /* no tests added yet */ + TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite->pNext == pSuite2); /* now have another suite */ + + TEST(!strcmp("suite2", pSuite2->pName)); + TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite2->pTest == NULL); /* no tests added yet */ + TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite2->pInitializeFunc == sfunc1); /* no init function */ + TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite2->pNext == pSuite3); /* next suite in list */ + + TEST(!strcmp("suite3", pSuite3->pName)); + TEST(pSuite3->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite3->pTest == NULL); /* no tests added yet */ + TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite3->pCleanupFunc == sfunc1); /* no cleanup function */ + TEST(pSuite3->pNext == pSuite4); /* next suite in list */ + + TEST(!strcmp("suite4", pSuite4->pName)); + TEST(pSuite4->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite4->pTest == NULL); /* no tests added yet */ + TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ + TEST(pSuite4->pCleanupFunc == sfunc1); /* no cleanup function */ + TEST(pSuite4->pNext == NULL); /* end of suite list */ + + TEST(0 != test_cunit_get_n_memevents(pSuite)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pSuite3)); + TEST(0 != test_cunit_get_n_memevents(pSuite4)); + + TEST(test_cunit_get_n_allocations(pSuite) != test_cunit_get_n_deallocations(pSuite)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); + + /* clean up everything and confirm deallocation */ + CU_cleanup_registry(); + + TEST(test_cunit_get_n_allocations(pSuite) == test_cunit_get_n_deallocations(pSuite)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); +} + +/*--------------------------------------------------*/ +/* test CU_set_suite_active() + * CU_set_suite_name() + * CU_set_suite_initfunc() + * CU_set_suite_cleanupfunc() + */ +static void test_CU_set_suite_attributes(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + + /* initialize system */ + CU_initialize_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST_FATAL(NULL != pSuite1); + TEST_FATAL(NULL != CU_add_test(pSuite1, "test1", test1)); + TEST_FATAL(NULL != CU_add_test(pSuite1, "test2", test1)); + + pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ + TEST_FATAL(NULL != pSuite2); + TEST_FATAL(NULL != CU_add_test(pSuite2, "test1", test1)); + TEST_FATAL(NULL != CU_add_test(pSuite2, "test2", test1)); + + /* test CU_set_suite_active() */ + TEST(CUE_NOSUITE == CU_set_suite_active(NULL, CU_FALSE)); /* error - NULL suite */ + + TEST(pSuite2->fActive == CU_TRUE); /* suites active on creation */ + TEST(pSuite1->fActive == CU_TRUE); + + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); + TEST(pSuite1->fActive == CU_TRUE); + TEST(pSuite2->fActive == CU_FALSE); + + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); + TEST(pSuite1->fActive == CU_FALSE); + TEST(pSuite2->fActive == CU_FALSE); + + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_TRUE)); + TEST(pSuite1->fActive == CU_FALSE); + TEST(pSuite2->fActive == CU_TRUE); + + /* test CU_set_suite_name() */ + TEST(CUE_NOSUITE == CU_set_suite_name(NULL, "null suite")); /* error - NULL suite */ + TEST(CUE_NO_SUITENAME == CU_set_suite_name(pSuite1, NULL)); /* error - NULL name */ + + TEST(!strcmp(pSuite1->pName, "suite1")); + TEST(!strcmp(pSuite2->pName, "suite2")); + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "This is my new name.")); + TEST(!strcmp(pSuite1->pName, "This is my new name.")); + TEST(!strcmp(pSuite2->pName, "suite2")); + + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "Never mind.")); + TEST(!strcmp(pSuite1->pName, "This is my new name.")); + TEST(!strcmp(pSuite2->pName, "Never mind.")); + + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "suite1")); + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "suite2")); + TEST(!strcmp(pSuite1->pName, "suite1")); + TEST(!strcmp(pSuite2->pName, "suite2")); + + /* test CU_set_suite_initfunc() */ + TEST(CUE_NOSUITE == CU_set_suite_initfunc(NULL, &sfunc1)); /* error - NULL suite */ + + TEST(pSuite1->pInitializeFunc == NULL); + TEST(pSuite2->pInitializeFunc == &sfunc1); + TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite1, &sfunc1)); + TEST(pSuite1->pInitializeFunc == &sfunc1); + TEST(pSuite2->pInitializeFunc == &sfunc1); + + TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite2, NULL)); + TEST(pSuite1->pInitializeFunc == &sfunc1); + TEST(pSuite2->pInitializeFunc == NULL); + + /* test CU_set_suite_cleanupfunc() */ + TEST(CUE_NOSUITE == CU_set_suite_cleanupfunc(NULL, &sfunc1)); + + TEST(pSuite1->pCleanupFunc == NULL); + TEST(pSuite2->pCleanupFunc == NULL); + TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite1, &sfunc1)); + TEST(pSuite1->pCleanupFunc == &sfunc1); + TEST(pSuite2->pCleanupFunc == NULL); + + TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite2, &sfunc1)); + TEST(pSuite1->pCleanupFunc == &sfunc1); + TEST(pSuite2->pCleanupFunc == &sfunc1); + + /* clean up */ + CU_cleanup_registry(); +} + +static void test_succeed(void) { CU_TEST(CU_TRUE); } +static void test_fail(void) { CU_TEST(CU_FALSE); } +static int suite_succeed(void) { return 0; } +static int suite_fail(void) { return 1; } + +/*--------------------------------------------------*/ +/* test CU_get_suite() + * CU_get_suite_at_pos() + * CU_get_suite_pos() + * CU_get_suite_pos_by_name() + */ +static void test_get_suite_functions(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pSuite pSuite5 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + CU_pTest pTest10 = NULL; + + /* error condition - registry not initialized */ + CU_cleanup_registry(); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_suite("suite1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_suite_at_pos(0)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos(pSuite1)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos_by_name("suite1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test9", test_succeed); + pSuite4 = CU_add_suite("suite4", NULL, suite_fail); + pTest10 = CU_add_test(pSuite4, "test10", test_succeed); + + /* error condition - invalid parameters */ + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_suite(NULL)); + TEST(CUE_NO_SUITENAME == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos(NULL)); + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos_by_name(NULL)); + TEST(CUE_NO_SUITENAME == CU_get_error()); + + /* normal operation - CU_get_suite() */ + + TEST(NULL == CU_get_suite("")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_suite("bad name")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_suite("suite3")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pSuite1 == CU_get_suite("suite1")); /* valid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite2 == CU_get_suite("suite2")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite4 == CU_get_suite("suite4")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_suite_at_pos() */ + + TEST(NULL == CU_get_suite_at_pos(0)); /* invalid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_suite_at_pos(5)); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pSuite1 == CU_get_suite_at_pos(1)); /* valid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite2 == CU_get_suite_at_pos(2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite3 == CU_get_suite_at_pos(3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite4 == CU_get_suite_at_pos(4)); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_suite_pos() */ + + pSuite5 = (CU_pSuite)malloc(sizeof(CU_Suite)); + TEST_FATAL(NULL != pSuite5); + + TEST(0 == CU_get_suite_pos(pSuite5)); /* invalid suite */ + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_suite_pos(pSuite1)); /* valid suites */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_suite_pos(pSuite2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(3 == CU_get_suite_pos(pSuite3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_suite_pos(pSuite4)); + TEST(CUE_SUCCESS == CU_get_error()); + + free(pSuite5); + + /* normal operation - CU_get_suite_pos_by_name() */ + + TEST(0 == CU_get_suite_pos_by_name("")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_suite_pos_by_name("suite3")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_suite_pos_by_name("suite1")); /* valid suites */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_suite_pos_by_name("suite2")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_suite_pos_by_name("suite4")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* clean up */ + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +/* test CU_add_test() + * CU_get_test_by_name() + * CU_get_test_by_index() + */ +static void test_CU_add_test(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTestRegistry pReg = NULL; + + CU_cleanup_registry(); + + /* error condition - registry not initialized */ + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_NOREGISTRY == CU_get_error()); + TEST(NULL == pTest1); + + CU_initialize_registry(); + pReg = CU_get_registry(); + + /* error condition - no suite */ + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_NOSUITE == CU_get_error()); + TEST(NULL == pTest1); + + /* error condition - no name */ + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + pTest1 = CU_add_test(pSuite1, NULL, test1); + TEST(CUE_NO_TESTNAME == CU_get_error()); + TEST(NULL == pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(0 == pSuite1->uiNumberOfTests); + + /* error condition - no test function */ + pTest1 = CU_add_test(pSuite1, "test1", NULL); + TEST(CUE_NOTEST == CU_get_error()); + TEST(NULL == pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(0 == pSuite1->uiNumberOfTests); + + /* warning condition - duplicate name */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(1 == pReg->uiNumberOfTests); + TEST(1 == pSuite1->uiNumberOfTests); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == NULL); + + pTest2 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_DUP_TEST == CU_get_error()); + TEST(NULL != pTest2); + TEST(1 == pReg->uiNumberOfSuites); + TEST(2 == pReg->uiNumberOfTests); + TEST(2 == pSuite1->uiNumberOfTests); + + TEST(!strcmp("test1", pTest1->pName)); + TEST(pTest1->fActive == CU_TRUE); + TEST(pTest1->pNext == pTest2); + TEST(pTest1->pJumpBuf == NULL); + TEST(pTest1->pTestFunc == test1); + TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == pTest2); + TEST(CU_get_test_by_index(3, pSuite1) == NULL); + + TEST(!strcmp("test1", pTest2->pName)); + TEST(pTest2->fActive == CU_TRUE); + TEST(pTest2->pNext == NULL); + TEST(pTest2->pJumpBuf == NULL); + TEST(pTest2->pTestFunc == test1); + TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); + + /* error condition - memory allocation failure */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + test_cunit_deactivate_malloc(); + pTest1 = CU_add_test(pSuite1, "test1", test1); + test_cunit_activate_malloc(); + TEST(CUE_NOMEMORY == CU_get_error()); + TEST(NULL == pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(0 == pSuite1->uiNumberOfTests); + + /* normal creation & cleanup */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + pSuite2 = CU_add_suite("suite2", sfunc1, sfunc1); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == NULL); + + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest1); + TEST(2 == pReg->uiNumberOfSuites); + TEST(1 == pReg->uiNumberOfTests); + TEST(1 == pSuite1->uiNumberOfTests); + TEST(0 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == NULL); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == NULL); + + pTest2 = CU_add_test(pSuite2, "test2", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(2 == pReg->uiNumberOfTests); + TEST(1 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest2); + TEST(CU_get_test_by_index(0, pSuite2) == NULL); + TEST(CU_get_test_by_index(1, pSuite2) == pTest2); + TEST(CU_get_test_by_index(2, pSuite2) == NULL); + + pTest3 = CU_add_test(pSuite1, "test3", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest3); + TEST(2 == pReg->uiNumberOfSuites); + TEST(3 == pReg->uiNumberOfTests); + TEST(2 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest2); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == pTest3); + TEST(CU_get_test_by_index(3, pSuite1) == NULL); + + pTest4 = CU_add_test(pSuite1, "test4", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest4); + TEST(2 == pReg->uiNumberOfSuites); + TEST(4 == pReg->uiNumberOfTests); + TEST(3 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest2); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == pTest3); + TEST(CU_get_test_by_index(3, pSuite1) == pTest4); + TEST(CU_get_test_by_index(4, pSuite1) == NULL); + + TEST(!strcmp("test1", pTest1->pName)); + TEST(pTest1->pNext == pTest3); + TEST(pTest1->pJumpBuf == NULL); + TEST(pTest1->pTestFunc == test1); + TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); + TEST(CU_get_test_by_name("test1", pSuite2) == NULL); + + TEST(!strcmp("test2", pTest2->pName)); + TEST(pTest2->pNext == NULL); + TEST(pTest2->pJumpBuf == NULL); + TEST(pTest2->pTestFunc == test1); + TEST(CU_get_test_by_name("test2", pSuite1) == NULL); + TEST(CU_get_test_by_name("test2", pSuite2) == pTest2); + + TEST(!strcmp("test3", pTest3->pName)); + TEST(pTest3->pNext == pTest4); + TEST(pTest3->pJumpBuf == NULL); + TEST(pTest3->pTestFunc == test1); + TEST(CU_get_test_by_name("test3", pSuite1) == pTest3); + TEST(CU_get_test_by_name("test3", pSuite2) == NULL); + + TEST(!strcmp("test4", pTest4->pName)); + TEST(pTest4->pNext == NULL); + TEST(pTest4->pJumpBuf == NULL); + TEST(pTest4->pTestFunc == test1); + TEST(CU_get_test_by_name("test4", pSuite1) == pTest4); + TEST(CU_get_test_by_name("test4", pSuite2) == NULL); + + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + TEST(0 != test_cunit_get_n_memevents(pTest3)); + TEST(0 != test_cunit_get_n_memevents(pTest4)); + + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); + + CU_cleanup_registry(); + + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); +} + +/*--------------------------------------------------*/ +/* test CU_set_test_active() + * CU_set_test_name() + * CU_set_test_func() + */ +static void test_CU_set_test_attributes(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + + /* initialize system */ + CU_initialize_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST_FATAL(NULL != pSuite1); + pTest1 = CU_add_test(pSuite1, "test1", test2); + TEST_FATAL(NULL != pTest1); + pTest2 = CU_add_test(pSuite1, "test2", test1); + TEST_FATAL(NULL != pTest2); + + pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ + TEST_FATAL(NULL != pSuite2); + pTest3 = CU_add_test(pSuite2, "test3", test2); + TEST_FATAL(NULL != pTest3); + pTest4 = CU_add_test(pSuite2, "test4", test1); + TEST_FATAL(NULL != pTest4); + + /* test CU_set_test_active() */ + TEST(CUE_NOTEST == CU_set_test_active(NULL, CU_FALSE)); /* error - NULL test */ + + TEST(CU_TRUE == pTest1->fActive); /* tests active on creation */ + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_TRUE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); + TEST(CU_TRUE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_FALSE == pTest3->fActive); + TEST(CU_FALSE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); + TEST(CU_FALSE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_FALSE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); + TEST(CU_FALSE == pTest1->fActive); + TEST(CU_FALSE == pTest2->fActive); + TEST(CU_FALSE == pTest3->fActive); + TEST(CU_FALSE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_TRUE)); + TEST(CU_TRUE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_TRUE == pTest4->fActive); + + /* test CU_set_test_name() */ + TEST(CUE_NOTEST == CU_set_test_name(NULL, "null test")); /* error - NULL test */ + TEST(CUE_NO_TESTNAME == CU_set_test_name(pTest1, NULL)); /* error - NULL name */ + + TEST(!strcmp("test1", pTest1->pName)); + TEST(!strcmp("test2", pTest2->pName)); + TEST(!strcmp("test3", pTest3->pName)); + TEST(!strcmp("test4", pTest4->pName)); + + TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "Aren't I a pretty girl?")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "Polly want a cracker.")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "@This is utter nonsense@")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "Yep!")); + + TEST(!strcmp("Aren't I a pretty girl?", pTest1->pName)); + TEST(!strcmp("Polly want a cracker.", pTest2->pName)); + TEST(!strcmp("@This is utter nonsense@", pTest3->pName)); + TEST(!strcmp("Yep!", pTest4->pName)); + + TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "test1")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "test2")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "test3")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "test4")); + + TEST(!strcmp("test1", pTest1->pName)); + TEST(!strcmp("test2", pTest2->pName)); + TEST(!strcmp("test3", pTest3->pName)); + TEST(!strcmp("test4", pTest4->pName)); + + /* test CU_set_test_func() */ + TEST(CUE_NOTEST == CU_set_test_func(NULL, &test1)); /* error - NULL test */ + TEST(CUE_NOTEST == CU_set_test_func(pTest1, NULL)); /* error - NULL test function */ + + TEST(&test2 == pTest1->pTestFunc); + TEST(&test1 == pTest2->pTestFunc); + TEST(&test2 == pTest3->pTestFunc); + TEST(&test1 == pTest4->pTestFunc); + + TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test1)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test2)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test1)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test2)); + + TEST(&test1 == pTest1->pTestFunc); + TEST(&test2 == pTest2->pTestFunc); + TEST(&test1 == pTest3->pTestFunc); + TEST(&test2 == pTest4->pTestFunc); + + TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test2)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test1)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test2)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test1)); + + /* clean up */ + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +/* test CU_get_test() + * CU_get_test_at_pos() + * CU_get_test_pos() + * CU_get_test_pos_by_name() + */ +static void test_get_test_functions(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + CU_pTest pTest10 = NULL; + + /* error condition - registry not initialized */ + CU_cleanup_registry(); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test(pSuite1, "test1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos(pSuite1, pTest1)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos_by_name(pSuite1, "test1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test9", test_succeed); + pSuite4 = CU_add_suite("suite4", NULL, suite_fail); + pTest10 = CU_add_test(pSuite4, "test10", test_succeed); + + /* error condition - invalid parameters */ + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test(NULL, "test1")); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test(pSuite1, NULL)); /* name NULL */ + TEST(CUE_NO_SUITENAME == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_at_pos(NULL, 1)); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos(NULL, pTest1)); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos(pSuite1, NULL)); /* test NULL */ + TEST(CUE_NOTEST == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos_by_name(NULL, "test1")); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos_by_name(pSuite1, NULL)); /* name NULL */ + TEST(CUE_NO_TESTNAME == CU_get_error()); + + /* normal operation - CU_get_test() */ + + TEST(NULL == CU_get_test(pSuite1, "")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test(pSuite2, "bad name")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test(pSuite1, "test3")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pTest1 == CU_get_test(pSuite1, "test1")); /* valid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest6 == CU_get_test(pSuite2, "test6")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest8 == CU_get_test(pSuite3, "test8")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_test_at_pos() */ + + TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); /* invalid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test_at_pos(pSuite1, 6)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test_at_pos(pSuite4, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pTest1 == CU_get_test_at_pos(pSuite1, 1)); /* valid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest2 == CU_get_test_at_pos(pSuite1, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest3 == CU_get_test_at_pos(pSuite1, 3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest4 == CU_get_test_at_pos(pSuite1, 4)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest5 == CU_get_test_at_pos(pSuite1, 5)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest6 == CU_get_test_at_pos(pSuite2, 1)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest7 == CU_get_test_at_pos(pSuite2, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest8 == CU_get_test_at_pos(pSuite3, 1)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest9 == CU_get_test_at_pos(pSuite3, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest10 == CU_get_test_at_pos(pSuite4, 1)); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_test_pos() */ + + TEST(0 == CU_get_test_pos(pSuite1, pTest6)); /* invalid tests */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_test_pos(pSuite4, pTest6)); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_test_pos(pSuite1, pTest1)); /* valid tests */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos(pSuite1, pTest2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(3 == CU_get_test_pos(pSuite1, pTest3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_test_pos(pSuite1, pTest4)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(5 == CU_get_test_pos(pSuite1, pTest5)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos(pSuite2, pTest6)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos(pSuite2, pTest7)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos(pSuite3, pTest8)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos(pSuite3, pTest9)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos(pSuite4, pTest10)); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_test_pos_by_name() */ + + TEST(0 == CU_get_test_pos_by_name(pSuite1, "")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_test_pos_by_name(pSuite1, "test9")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_test_pos_by_name(pSuite2, "test1")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_test_pos_by_name(pSuite1, "test1")); /* valid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos_by_name(pSuite1, "test2")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_test_pos_by_name(pSuite1, "test4")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos_by_name(pSuite2, "test6")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos_by_name(pSuite2, "test7")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos_by_name(pSuite3, "test8")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos_by_name(pSuite3, "test9")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos_by_name(pSuite4, "test10")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* clean up */ + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +static void test_CU_get_registry(void) +{ + CU_cleanup_registry(); + TEST(NULL == CU_get_registry()); + + CU_initialize_registry(); + TEST(NULL != CU_get_registry()); + TEST(f_pTestRegistry == CU_get_registry()); + + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +static void test_CU_set_registry(void) +{ + CU_pTestRegistry pReg1 = NULL; + CU_pTestRegistry pReg2 = NULL; + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pSuite2 = CU_add_suite("suite2", NULL, NULL); + + CU_add_test(pSuite1, "test1", test1); + CU_add_test(pSuite1, "test2", test1); + CU_add_test(pSuite2, "test1", test1); + CU_add_test(pSuite2, "test2", test1); + + pReg1 = CU_get_registry(); + + TEST(pReg1->pSuite == pSuite1); + TEST(pReg1->uiNumberOfSuites == 2); + TEST(pReg1->uiNumberOfTests == 4); + TEST(0 < test_cunit_get_n_memevents(pReg1)); + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + CU_set_registry(NULL); + + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + CU_cleanup_registry(); + + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + pReg2 = CU_create_new_registry(); + CU_set_registry(pReg2); + + TEST(pReg1->pSuite == pSuite1); + TEST(pReg1->uiNumberOfSuites == 2); + TEST(pReg1->uiNumberOfTests == 4); + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + TEST(CU_get_registry()->pSuite == NULL); + TEST(CU_get_registry()->uiNumberOfSuites == 0); + TEST(CU_get_registry()->uiNumberOfTests == 0); + TEST(0 < test_cunit_get_n_memevents(pReg2)); + TEST(test_cunit_get_n_allocations(pReg2) != test_cunit_get_n_deallocations(pReg2)); + + CU_cleanup_registry(); + + TEST(pReg1->pSuite == pSuite1); + TEST(pReg1->uiNumberOfSuites == 2); + TEST(pReg1->uiNumberOfTests == 4); + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + TEST(test_cunit_get_n_allocations(pReg2) == test_cunit_get_n_deallocations(pReg2)); + + CU_set_registry(pReg1); + CU_cleanup_registry(); + TEST(test_cunit_get_n_allocations(pReg1) == test_cunit_get_n_deallocations(pReg1)); +} + +/*--------------------------------------------------*/ +/* test CU_create_new_registry() + * CU_destroy_existing_registry() + */ +static void test_CU_create_new_registry(void) +{ + CU_pTestRegistry pReg = NULL; + CU_pTestRegistry pRegOld = NULL; + + CU_cleanup_registry(); + pReg = CU_create_new_registry(); + + TEST(NULL != pReg); + TEST(0 < test_cunit_get_n_memevents(pReg)); + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + + TEST(pReg->pSuite == NULL); + TEST(pReg->uiNumberOfSuites == 0); + TEST(pReg->uiNumberOfTests == 0); + + CU_cleanup_registry(); + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + + pRegOld = pReg; + CU_destroy_existing_registry(&pReg); + TEST(test_cunit_get_n_allocations(pRegOld) == test_cunit_get_n_deallocations(pRegOld)); + TEST(NULL == pReg); +} + +/*--------------------------------------------------*/ +static void test_cleanup_test_registry(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTestRegistry pReg = CU_create_new_registry(); + + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* create tests to register */ + pTest1 = create_test("test1", test1); + pTest2 = create_test("test2", NULL); + pTest3 = create_test("test3", test1); + pTest4 = create_test("", NULL); + + /* create suites to hold tests */ + pSuite1 = create_suite("suite1", NULL, NULL); + pSuite2 = create_suite("suite2", sfunc1, sfunc1); + insert_suite(pReg, pSuite1); + insert_suite(pReg, pSuite2); + + insert_test(pSuite1, pTest1); + insert_test(pSuite1, pTest2); + insert_test(pSuite1, pTest3); + insert_test(pSuite2, pTest4); + + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); /* not managed in primitive functions */ + TEST(3 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest4); + TEST(pTest1->pNext == pTest2); + TEST(pTest1->pPrev == NULL); + TEST(pTest2->pNext == pTest3); + TEST(pTest2->pPrev == pTest1); + TEST(pTest3->pNext == NULL); + TEST(pTest3->pPrev == pTest2); + TEST(pTest4->pNext == NULL); + TEST(pTest4->pPrev == NULL); + + TEST(0 != test_cunit_get_n_memevents(pReg)); + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + TEST(0 != test_cunit_get_n_memevents(pTest3)); + TEST(0 != test_cunit_get_n_memevents(pTest4)); + + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); + + cleanup_test_registry(pReg); + CU_FREE(pReg); + + TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); +} + +/*--------------------------------------------------*/ +/* test create_suite() + * cleanup_suite() + */ +static void test_create_suite(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + + /* error condition - memory allocation failure */ + test_cunit_deactivate_malloc(); + pSuite1 = create_suite("suite1", NULL, NULL); + TEST(NULL == pSuite1); + test_cunit_activate_malloc(); + + /* normal creation & cleanup */ + pSuite1 = create_suite("suite1", NULL, NULL); + TEST(NULL != pSuite1); + TEST(!strcmp("suite1", pSuite1->pName)); + TEST(pSuite1->pTest == NULL); /* no tests added yet */ + TEST(pSuite1->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite1->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite1->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite1->pNext == NULL); /* no more suites added yet */ + + pSuite2 = create_suite("suite2", sfunc1, NULL); + TEST(NULL != pSuite2); + TEST(!strcmp("suite2", pSuite2->pName)); + TEST(pSuite2->pTest == NULL); /* no tests added yet */ + TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite2->pInitializeFunc == sfunc1); /* init function */ + TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite2->pNext == NULL); /* no more suites added yet */ + + pSuite3 = create_suite("suite3", NULL, sfunc1); + TEST(NULL != pSuite3); + TEST(!strcmp("suite3", pSuite3->pName)); + TEST(pSuite3->pTest == NULL); /* no tests added yet */ + TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite3->pCleanupFunc == sfunc1); /* cleanup function */ + TEST(pSuite3->pNext == NULL); /* no more suites added yet */ + + pSuite4 = create_suite("suite4", sfunc1, sfunc1); + TEST(NULL != pSuite4); + TEST(!strcmp("suite4", pSuite4->pName)); + TEST(pSuite4->pTest == NULL); /* no tests added yet */ + TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ + TEST(pSuite4->pCleanupFunc == sfunc1); /* cleanup function */ + TEST(pSuite4->pNext == NULL); /* no more suites added yet */ + + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + cleanup_suite(pSuite1); + CU_FREE(pSuite1); + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + cleanup_suite(pSuite2); + CU_FREE(pSuite2); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + + TEST(0 != test_cunit_get_n_memevents(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); + cleanup_suite(pSuite3); + CU_FREE(pSuite3); + TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); + + TEST(0 != test_cunit_get_n_memevents(pSuite4)); + TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); + cleanup_suite(pSuite4); + CU_FREE(pSuite4); + TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); +} + +/*--------------------------------------------------*/ +/* test insert_suite() + * suite_exists() + */ +static void test_insert_suite(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTestRegistry pReg = CU_create_new_registry(); + + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(NULL == pReg->pSuite); + TEST(CU_FALSE == suite_exists(pReg, "suite1")); + TEST(CU_FALSE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + /* normal creation & cleanup */ + pSuite1 = create_suite("suite1", NULL, NULL); + insert_suite(pReg, pSuite1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_FALSE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + pSuite2 = create_suite("suite2", sfunc1, NULL); + insert_suite(pReg, pSuite2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == pSuite2); + TEST(pSuite2->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_TRUE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + pSuite3 = create_suite("suite3", NULL, sfunc1); + insert_suite(pReg, pSuite3); + TEST(3 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == pSuite2); + TEST(pSuite2->pNext == pSuite3); + TEST(pSuite3->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_TRUE == suite_exists(pReg, "suite2")); + TEST(CU_TRUE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + pSuite4 = create_suite("suite4", sfunc1, sfunc1); + insert_suite(pReg, pSuite4); + TEST(4 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == pSuite2); + TEST(pSuite2->pNext == pSuite3); + TEST(pSuite3->pNext == pSuite4); + TEST(pSuite4->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_TRUE == suite_exists(pReg, "suite2")); + TEST(CU_TRUE == suite_exists(pReg, "suite3")); + TEST(CU_TRUE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + TEST(0 != test_cunit_get_n_memevents(pReg)); + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pSuite3)); + TEST(0 != test_cunit_get_n_memevents(pSuite4)); + + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); + + cleanup_test_registry(pReg); + TEST(CU_FALSE == suite_exists(pReg, "suite1")); + TEST(CU_FALSE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + CU_FREE(pReg); + + TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); +} + +/*--------------------------------------------------*/ +static void test_create_test(void) +{ + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + + /* error condition - memory allocation failure */ + test_cunit_deactivate_malloc(); + pTest1 = create_test("test1", test1); + test_cunit_activate_malloc(); + TEST(NULL == pTest1); + + /* normal creation & cleanup */ + pTest1 = create_test("test1", test1); + TEST(NULL != pTest1); + TEST(pTest1->pTestFunc == test1); + TEST(!strcmp("test1", pTest1->pName)); + TEST(pTest1->pNext == NULL); + TEST(pTest1->pPrev == NULL); + TEST(pTest1->pJumpBuf == NULL); + + pTest2= create_test("test2", NULL); + TEST(NULL != pTest2); + TEST(pTest2->pTestFunc == NULL); + TEST(!strcmp("test2", pTest2->pName)); + TEST(pTest2->pNext == NULL); + TEST(pTest2->pPrev == NULL); + TEST(pTest2->pJumpBuf == NULL); + + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + + cleanup_test(pTest1); + CU_FREE(pTest1); + cleanup_test(pTest2); + CU_FREE(pTest2); + + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); +} + +/*--------------------------------------------------*/ +/* test insert_test() + * test_exists() + */ +static void test_insert_test(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + + /* create tests to register */ + pTest1 = create_test("test1", test1); + pTest2 = create_test("test2", NULL); + pTest3 = create_test("test3", test1); + pTest4 = create_test("", NULL); + + /* create suites to hold tests */ + pSuite1 = create_suite("suite1", NULL, NULL); + pSuite2 = create_suite("suite2", sfunc1, sfunc1); + + TEST(CU_FALSE == test_exists(pSuite1, "test1")); + TEST(CU_FALSE == test_exists(pSuite1, "test2")); + TEST(CU_FALSE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_FALSE == test_exists(pSuite2, "")); + + insert_test(pSuite1, pTest1); + insert_test(pSuite1, pTest2); + insert_test(pSuite1, pTest3); + insert_test(pSuite2, pTest4); + + TEST(CU_TRUE == test_exists(pSuite1, "test1")); + TEST(CU_TRUE == test_exists(pSuite1, "test2")); + TEST(CU_TRUE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_TRUE == test_exists(pSuite2, "")); + + TEST(3 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest4); + TEST(pTest1->pNext == pTest2); + TEST(pTest1->pPrev == NULL); + TEST(pTest2->pNext == pTest3); + TEST(pTest2->pPrev == pTest1); + TEST(pTest3->pNext == NULL); + TEST(pTest3->pPrev == pTest2); + TEST(pTest4->pNext == NULL); + TEST(pTest4->pPrev == NULL); + + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + TEST(0 != test_cunit_get_n_memevents(pTest3)); + TEST(0 != test_cunit_get_n_memevents(pTest4)); + + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); + + cleanup_suite(pSuite1); + + TEST(CU_FALSE == test_exists(pSuite1, "test1")); + TEST(CU_FALSE == test_exists(pSuite1, "test2")); + TEST(CU_FALSE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_TRUE == test_exists(pSuite2, "")); + + cleanup_suite(pSuite2); + + TEST(CU_FALSE == test_exists(pSuite1, "test1")); + TEST(CU_FALSE == test_exists(pSuite1, "test2")); + TEST(CU_FALSE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_FALSE == test_exists(pSuite2, "")); + + CU_FREE(pSuite1); + CU_FREE(pSuite2); + + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); +} + +/*--------------------------------------------------*/ +static void test_cleanup_test(void) +{ + char* pName; + CU_pTest pTest1 = create_test("test1", NULL); + + TEST_FATAL(NULL != pTest1); + + pName = pTest1->pName; + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pName)); + + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pName) != test_cunit_get_n_deallocations(pName)); + + cleanup_test(pTest1); + CU_FREE(pTest1); + + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pName) == test_cunit_get_n_deallocations(pName)); +} + +/*--------------------------------------------------*/ +static void group_A_case_1(void) +{ + CU_ASSERT_TRUE(1); +} + +static void group_A_case_2(void) +{ + CU_ASSERT_TRUE(2); +} + +static void group_B_case_1(void) +{ + CU_ASSERT_FALSE(1); +} + +static void group_B_case_2(void) +{ + CU_ASSERT_FALSE(2); +} + +static CU_TestInfo group_A_test_cases[] = { + { "1", group_A_case_1 }, + { "2", group_A_case_2 }, + CU_TEST_INFO_NULL, +}; + +static CU_TestInfo group_B_test_cases[] = { + { "1", group_B_case_1 }, + { "2", group_B_case_2 }, + CU_TEST_INFO_NULL, +}; + +static CU_TestInfo group_C_test_cases[] = { + { "1", group_B_case_1 }, + { "1", group_B_case_2 }, /* duplicate test name */ + CU_TEST_INFO_NULL, +}; + +static CU_SuiteInfo suites0[] = { + CU_SUITE_INFO_NULL, +}; + +static CU_SuiteInfo suites1[] = { + { "A1", NULL, NULL, group_A_test_cases }, + { "B1", NULL, NULL, group_B_test_cases }, + CU_SUITE_INFO_NULL, +}; + +static CU_SuiteInfo suites2[] = { + { "A2", NULL, NULL, group_A_test_cases }, + { "B2", NULL, NULL, group_B_test_cases }, + CU_SUITE_INFO_NULL, +}; + +static CU_SuiteInfo suites3[] = { + { "A3", NULL, NULL, group_A_test_cases }, + { "A3", NULL, NULL, group_C_test_cases }, /* duplicate suite name */ + CU_SUITE_INFO_NULL, +}; + +static void test_register_suite(void) +{ + CU_pTestRegistry pReg = NULL; + CU_ErrorCode status; + + if (CU_initialize_registry()) { + fprintf(stderr, "\nError initializing registry in test_register_suite()."); + return; + } + + pReg = CU_get_registry(); + + /* test initial condition */ + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with NULL */ + status = CU_register_suites(NULL); + TEST(CUE_SUCCESS == status); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with empty array */ + status = CU_register_suites(suites0); + TEST(CUE_SUCCESS == status); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with ok array */ + status = CU_register_suites(suites1); + TEST(CUE_SUCCESS == status); + TEST(2 == pReg->uiNumberOfSuites); + TEST(4 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with duplicate suite name */ + status = CU_register_suites(suites1); + TEST(CUE_SUCCESS == status); /* shaky - depends on order of operation in CU_register_suites() */ + TEST(4 == pReg->uiNumberOfSuites); + TEST(8 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with duplicate test name */ + status = CU_register_suites(suites3); + TEST(CUE_DUP_TEST == status); /* shaky - depends on order of operation in CU_register_suites() */ + TEST(6 == pReg->uiNumberOfSuites); + TEST(12 == pReg->uiNumberOfTests); + + CU_cleanup_registry(); + + if (CU_initialize_registry()) { + fprintf(stderr, "\nError initializing registry in test_register_suite()."); + return; + } + + pReg = CU_get_registry(); + + /* test CU_register_nsuites() with ok arrays */ + status = CU_register_nsuites(2, suites1, suites2); + TEST(CUE_SUCCESS == status); + TEST(4 == pReg->uiNumberOfSuites); + TEST(8 == pReg->uiNumberOfTests); +} + +/*--------------------------------------------------*/ +void test_cunit_TestDB(void) +{ + test_cunit_start_tests("TestDB.c"); + + test_CU_initialize_registry(); + test_CU_cleanup_registry(); + test_CU_add_suite(); + test_CU_set_suite_attributes(); + test_get_suite_functions(); + test_CU_add_test(); + test_CU_set_test_attributes(); + test_get_test_functions(); + test_CU_get_registry(); + test_CU_set_registry(); + test_CU_create_new_registry(); + test_cleanup_test_registry(); + test_create_suite(); + test_insert_suite(); + test_create_test(); + test_cleanup_test(); + test_insert_test(); + test_register_suite(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestDB.cpp b/gearsyncd/tests/cunit/TestDB.cpp new file mode 100644 index 0000000000..d609f50549 --- /dev/null +++ b/gearsyncd/tests/cunit/TestDB.cpp @@ -0,0 +1,2773 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004,2005,2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of Registry/TestGroup/Testcase management Routines. + * + * Aug 2001 Initial implementation (AK) + * + * 09/Aug/2001 Added startup initialize/cleanup registry functions. (AK) + * + * 29/Aug/2001 Added Test and Group Add functions. (AK) + * + * 02/Oct/2001 Added Proper Error codes and Messages on the failure conditions. (AK) + * + * 13/Oct/2001 Added Code to Check for the Duplicate Group name and test name. (AK) + * + * 15-Jul-2004 Added doxygen comments, new interface, added assertions to + * internal functions, moved error handling code to CUError.c, + * added assertions to make sure no modification of registry + * during a run, bug fixes, changed CU_set_registry() so that it + * doesn't require cleaning the existing registry. (JDS) + * + * 24-Apr-2006 Removed constraint that suites/tests be uniquely named. + * Added ability to turn individual tests/suites on or off. + * Added lookup functions for suites/tests based on index. + * Moved doxygen comments for public API here to header. + * Modified internal unit tests to include these changes. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + */ + +/** @file + * Management functions for tests, suites, and the test registry (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "MyMem.h" +#include "TestDB.h" +#include "TestRun.h" +#include "Util.h" +#include "CUnit_intl.h" + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +static CU_pTestRegistry f_pTestRegistry = NULL; /**< The active internal Test Registry. */ + +/*================================================================= + * Private function forward declarations + *=================================================================*/ +static void cleanup_test_registry(CU_pTestRegistry pRegistry); +static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean); +static void cleanup_suite(CU_pSuite pSuite); +static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite); +static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc); +static void cleanup_test(CU_pTest pTest); +static void insert_test(CU_pSuite pSuite, CU_pTest pTest); + +static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName); +static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +CU_ErrorCode CU_initialize_registry(void) +{ + CU_ErrorCode result; + + assert(CU_FALSE == CU_is_test_running()); + + CU_set_error(result = CUE_SUCCESS); + + if (NULL != f_pTestRegistry) { + CU_cleanup_registry(); + } + + f_pTestRegistry = CU_create_new_registry(); + if (NULL == f_pTestRegistry) { + CU_set_error(result = CUE_NOMEMORY); + } + + return result; +} + +/*------------------------------------------------------------------------*/ +CU_BOOL CU_registry_initialized(void) +{ + return (NULL == f_pTestRegistry) ? CU_FALSE : CU_TRUE; +} + +/*------------------------------------------------------------------------*/ +void CU_cleanup_registry(void) +{ + assert(CU_FALSE == CU_is_test_running()); + + CU_set_error(CUE_SUCCESS); + CU_destroy_existing_registry(&f_pTestRegistry); /* supposed to handle NULL ok */ + CU_clear_previous_results(); + CU_CREATE_MEMORY_REPORT(NULL); +} + +/*------------------------------------------------------------------------*/ +CU_pTestRegistry CU_get_registry(void) +{ + return f_pTestRegistry; +} + +/*------------------------------------------------------------------------*/ +CU_pTestRegistry CU_set_registry(CU_pTestRegistry pRegistry) +{ + CU_pTestRegistry pOldRegistry = f_pTestRegistry; + + assert(CU_FALSE == CU_is_test_running()); + + CU_set_error(CUE_SUCCESS); + f_pTestRegistry = pRegistry; + return pOldRegistry; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) +{ + CU_pSuite pRetValue = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + assert(CU_FALSE == CU_is_test_running()); + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + pRetValue = create_suite(strName, pInit, pClean); + if (NULL == pRetValue) { + error = CUE_NOMEMORY; + } + else { + if (CU_TRUE == suite_exists(f_pTestRegistry, strName)) { + error = CUE_DUP_SUITE; + } + insert_suite(f_pTestRegistry, pRetValue); + } + } + + CU_set_error(error); + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + pSuite->fActive = fNewActive; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else if (NULL == strNewName) { + result = CUE_NO_SUITENAME; + } + else { + CU_FREE(pSuite->pName); + pSuite->pName = (char *)CU_MALLOC(strlen(strNewName)+1); + strcpy(pSuite->pName, strNewName); + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + pSuite->pInitializeFunc = pNewInit; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + pSuite->pCleanupFunc = pNewClean; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite(const char *strName) +{ + CU_pSuite result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + result = CU_get_suite_by_name(strName, f_pTestRegistry); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite_at_pos(unsigned int pos) +{ + CU_pSuite result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else { + result = CU_get_suite_by_index(pos, f_pTestRegistry); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_suite_pos(CU_pSuite pSuite) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pSuite pCurrentSuite = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else { + pCurrentSuite = f_pTestRegistry->pSuite; + result = 1; + while ((NULL != pCurrentSuite) && (pCurrentSuite != pSuite)) { + ++result; + pCurrentSuite = pCurrentSuite->pNext; + } + if (pCurrentSuite == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_suite_pos_by_name(const char *strName) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pSuite pCurrentSuite = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + pCurrentSuite = f_pTestRegistry->pSuite; + result = 1; + while ((NULL != pCurrentSuite) && (0 != strcmp(pCurrentSuite->pName, strName))) { + ++result; + pCurrentSuite = pCurrentSuite->pNext; + } + if (pCurrentSuite == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc) +{ + CU_pTest pRetValue = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + assert(CU_FALSE == CU_is_test_running()); + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == strName) { + error = CUE_NO_TESTNAME; + } + else if(NULL == pTestFunc) { + error = CUE_NOTEST; + } + else { + pRetValue = create_test(strName, pTestFunc); + if (NULL == pRetValue) { + error = CUE_NOMEMORY; + } + else { + f_pTestRegistry->uiNumberOfTests++; + if (CU_TRUE == test_exists(pSuite, strName)) { + error = CUE_DUP_TEST; + } + insert_test(pSuite, pRetValue); + } + } + + CU_set_error(error); + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_test_active(CU_pTest pTest, CU_BOOL fNewActive) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pTest) { + result = CUE_NOTEST; + } + else { + pTest->fActive = fNewActive; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if (NULL == pTest) { + result = CUE_NOTEST; + } + else if (NULL == strNewName) { + result = CUE_NO_TESTNAME; + } + else { + CU_FREE(pTest->pName); + pTest->pName = (char *)CU_MALLOC(strlen(strNewName)+1); + strcpy(pTest->pName, strNewName); + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc) +{ + CU_ErrorCode result = CUE_SUCCESS; + + if ((NULL == pTest) || (NULL == pNewFunc)) { + result = CUE_NOTEST; + } + else { + pTest->pTestFunc = pNewFunc; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName) +{ + CU_pTest result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == strName) { + error = CUE_NO_SUITENAME; + } + else { + result = CU_get_test_by_name(strName, pSuite); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos) +{ + CU_pTest result = NULL; + CU_ErrorCode error = CUE_SUCCESS; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else { + result = CU_get_test_by_index(pos, pSuite); + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pTest pCurrentTest = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == pTest) { + error = CUE_NOTEST; + } + else { + pCurrentTest = pSuite->pTest; + result = 1; + while ((NULL != pCurrentTest) && (pCurrentTest != pTest)) { + ++result; + pCurrentTest = pCurrentTest->pNext; + } + if (pCurrentTest == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName) +{ + unsigned int result = 0; + CU_ErrorCode error = CUE_SUCCESS; + CU_pTest pCurrentTest = NULL; + + if (NULL == f_pTestRegistry) { + error = CUE_NOREGISTRY; + } + else if (NULL == pSuite) { + error = CUE_NOSUITE; + } + else if (NULL == strName) { + error = CUE_NO_TESTNAME; + } + else { + pCurrentTest = pSuite->pTest; + result = 1; + while ((NULL != pCurrentTest) && (0 != strcmp(pCurrentTest->pName, strName))) { + ++result; + pCurrentTest = pCurrentTest->pNext; + } + if (pCurrentTest == NULL) { + result = 0; + } + } + + CU_set_error(error); + return result; +} + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +/* This section is based conceptually on code + * Copyright (C) 2004 Aurema Pty Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Derived from code contributed by K. Cheung and Aurema Pty Ltd. (thanks!) + * int test_group_register(test_group_t *tg) + * int test_suite_register(test_suite_t *ts) + */ +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_register_nsuites(int suite_count, ...) +{ + CU_SuiteInfo *pSuiteItem = NULL; + CU_TestInfo *pTestItem = NULL; + CU_pSuite pSuite = NULL; + + va_list argptr; + int i; + + va_start(argptr, suite_count); + + for (i=0 ; ipName; pSuiteItem++) { + if (NULL != (pSuite = CU_add_suite(pSuiteItem->pName, pSuiteItem->pInitFunc, pSuiteItem->pCleanupFunc))) { + for (pTestItem = pSuiteItem->pTests; NULL != pTestItem->pName; pTestItem++) { + if (NULL == CU_add_test(pSuite, pTestItem->pName, pTestItem->pTestFunc)) { + return CU_get_error(); + } + } + } + else { + return CU_get_error(); + } + } + } + } + return CU_get_error(); +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]) +{ + return CU_register_nsuites(1, suite_info); +} +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +/*================================================================= + * Private static function definitions + *=================================================================*/ +/*------------------------------------------------------------------------*/ +/** + * Internal function to clean up the specified test registry. + * cleanup_suite() will be called for each registered suite to perform + * cleanup of the associated test cases. Then, the suite's memory will + * be freed. Note that any pointers to tests or suites in pRegistry + * held by the user will be invalidated by this function. Severe problems + * can occur if this function is called during a test run involving pRegistry. + * Note that memory held for data members in the registry (e.g. pName) and + * the registry itself are not freed by this function. + * + * @see cleanup_suite() + * @see cleanup_test() + * @param pRegistry CU_pTestRegistry to clean up (non-NULL). + */ +static void cleanup_test_registry(CU_pTestRegistry pRegistry) +{ + CU_pSuite pCurSuite = NULL; + CU_pSuite pNextSuite = NULL; + + assert(NULL != pRegistry); + + pCurSuite = pRegistry->pSuite; + while (NULL != pCurSuite) { + pNextSuite = pCurSuite->pNext; + cleanup_suite(pCurSuite); + + CU_FREE(pCurSuite); + pCurSuite = pNextSuite; + } + pRegistry->pSuite = NULL; + pRegistry->uiNumberOfSuites = 0; + pRegistry->uiNumberOfTests = 0; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to create a new test suite having the specified parameters. + * This function creates a new test suite having the specified name and + * initialization/cleanup functions. The new suite is active for execution during + * test runs. The strName cannot be NULL (checked by assertion), but either or + * both function pointers can be. A pointer to the newly-created suite is returned, + * or NULL if there was an error allocating memory for the new suite. It is the + * responsibility of the caller to destroy the returned suite (use cleanup_suite() + * before freeing the returned pointer). + * + * @param strName Name for the new test suite (non-NULL). + * @param pInit Initialization function to call before running suite. + * @param pClean Cleanup function to call after running suite. + * @return A pointer to the newly-created suite (NULL if creation failed) + */ +static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) +{ + CU_pSuite pRetValue = (CU_pSuite)CU_MALLOC(sizeof(CU_Suite)); + + assert(NULL != strName); + + if (NULL != pRetValue) { + pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); + if (NULL != pRetValue->pName) { + strcpy(pRetValue->pName, strName); + pRetValue->fActive = CU_TRUE; + pRetValue->pInitializeFunc = pInit; + pRetValue->pCleanupFunc = pClean; + pRetValue->pTest = NULL; + pRetValue->pNext = NULL; + pRetValue->pPrev = NULL; + pRetValue->uiNumberOfTests = 0; + } + else { + CU_FREE(pRetValue); + pRetValue = NULL; + } + } + + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to clean up the specified test suite. + * Each test case registered with pSuite will be freed. Allocated memory held + * by the suite (i.e. the name) will also be deallocated. Severe problems can + * occur if this function is called during a test run involving pSuite. + * + * @param pSuite CU_pSuite to clean up (non-NULL). + * @see cleanup_test_registry() + * @see cleanup_test() + */ +static void cleanup_suite(CU_pSuite pSuite) +{ + CU_pTest pCurTest = NULL; + CU_pTest pNextTest = NULL; + + assert(NULL != pSuite); + + pCurTest = pSuite->pTest; + while (NULL != pCurTest) { + pNextTest = pCurTest->pNext; + + cleanup_test(pCurTest); + + CU_FREE(pCurTest); + pCurTest = pNextTest; + } + if (NULL != pSuite->pName) { + CU_FREE(pSuite->pName); + } + + pSuite->pName = NULL; + pSuite->pTest = NULL; + pSuite->uiNumberOfTests = 0; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to insert a suite into a registry. + * The suite name is assumed to be unique. Internally, the list of suites + * is a double-linked list, which this function manages. Insertion of NULL + * pSuites is not allowed (checked by assertion). Severe problems can occur + * if this function is called during a test run involving pRegistry. + * + * @param pRegistry CU_pTestRegistry to insert into (non-NULL). + * @param pSuite CU_pSuite to insert (non-NULL). + * @see insert_test() + */ +static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite) +{ + CU_pSuite pCurSuite = NULL; + + assert(NULL != pRegistry); + assert(NULL != pSuite); + + pCurSuite = pRegistry->pSuite; + + assert(pCurSuite != pSuite); + + pSuite->pNext = NULL; + pRegistry->uiNumberOfSuites++; + + /* if this is the 1st suite to be added... */ + if (NULL == pCurSuite) { + pRegistry->pSuite = pSuite; + pSuite->pPrev = NULL; + } + /* otherwise, add it to the end of the linked list... */ + else { + while (NULL != pCurSuite->pNext) { + pCurSuite = pCurSuite->pNext; + assert(pCurSuite != pSuite); + } + + pCurSuite->pNext = pSuite; + pSuite->pPrev = pCurSuite; + } +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to create a new test case having the specified parameters. + * This function creates a new test having the specified name and test function. + * The strName cannot be NULL (checked by assertion), but the function pointer + * may be. A pointer to the newly-created test is returned, or NULL if there + * was an error allocating memory for the new test. It is the responsibility + * of the caller to destroy the returned test (use cleanup_test() before freeing + * the returned pointer). + * + * @param strName Name for the new test. + * @param pTestFunc Test function to call when running this test. + * @return A pointer to the newly-created test (NULL if creation failed) + */ +static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc) +{ + CU_pTest pRetValue = (CU_pTest)CU_MALLOC(sizeof(CU_Test)); + + assert(NULL != strName); + + if (NULL != pRetValue) { + pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); + if (NULL != pRetValue->pName) { + strcpy(pRetValue->pName, strName); + pRetValue->fActive = CU_TRUE; + pRetValue->pTestFunc = pTestFunc; + pRetValue->pJumpBuf = NULL; + pRetValue->pNext = NULL; + pRetValue->pPrev = NULL; + } + else { + CU_FREE(pRetValue); + pRetValue = NULL; + } + } + + return pRetValue; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to clean up the specified test. + * All memory associated with the test will be freed. Severe problems can + * occur if this function is called during a test run involving pTest. + * + * @param pTest CU_pTest to clean up (non-NULL). + * @see cleanup_test_registry() + * @see cleanup_suite() + */ +static void cleanup_test(CU_pTest pTest) +{ + assert(NULL != pTest); + + if (NULL != pTest->pName) { + CU_FREE(pTest->pName); + } + + pTest->pName = NULL; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to insert a test into a suite. + * The test name is assumed to be unique. Internally, the list of tests in + * a suite is a double-linked list, which this function manages. Neither + * pSuite nor pTest may be NULL (checked by assertion). Further, pTest must + * be an independent test (i.e. both pTest->pNext and pTest->pPrev == NULL), + * which is also checked by assertion. Severe problems can occur if this + * function is called during a test run involving pSuite. + * + * @param pSuite CU_pSuite to insert into (non-NULL). + * @param pTest CU_pTest to insert (non-NULL). + * @see insert_suite() + */ +static void insert_test(CU_pSuite pSuite, CU_pTest pTest) +{ + CU_pTest pCurTest = NULL; + + assert(NULL != pSuite); + assert(NULL != pTest); + assert(NULL == pTest->pNext); + assert(NULL == pTest->pPrev); + + pCurTest = pSuite->pTest; + + assert(pCurTest != pTest); + + pSuite->uiNumberOfTests++; + /* if this is the 1st suite to be added... */ + if (NULL == pCurTest) { + pSuite->pTest = pTest; + pTest->pPrev = NULL; + } + else { + while (NULL != pCurTest->pNext) { + pCurTest = pCurTest->pNext; + assert(pCurTest != pTest); + } + + pCurTest->pNext = pTest; + pTest->pPrev = pCurTest; + } +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to check whether a suite having a specified + * name already exists. + * + * @param pRegistry CU_pTestRegistry to check (non-NULL). + * @param szSuiteName Suite name to check (non-NULL). + * @return CU_TRUE if suite exists in the registry, CU_FALSE otherwise. + */ +static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName) +{ + CU_pSuite pSuite = NULL; + + assert(NULL != pRegistry); + assert(NULL != szSuiteName); + + pSuite = pRegistry->pSuite; + while (NULL != pSuite) { + if ((NULL != pSuite->pName) && (0 == CU_compare_strings(szSuiteName, pSuite->pName))) { + return CU_TRUE; + } + pSuite = pSuite->pNext; + } + + return CU_FALSE; +} + +/*------------------------------------------------------------------------*/ +/** + * Internal function to check whether a test having a specified + * name is already registered in a given suite. + * + * @param pSuite CU_pSuite to check (non-NULL). + * @param szTestName Test case name to check (non-NULL). + * @return CU_TRUE if test exists in the suite, CU_FALSE otherwise. + */ +static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName) +{ + CU_pTest pTest = NULL; + + assert(NULL != pSuite); + assert(NULL != szTestName); + + pTest = pSuite->pTest; + while (NULL != pTest) { + if ((NULL != pTest->pName) && (0 == CU_compare_strings(szTestName, pTest->pName))) { + return CU_TRUE; + } + pTest = pTest->pNext; + } + + return CU_FALSE; +} + +/*================================================================= + * Public but primarily internal function definitions + *=================================================================*/ +CU_pTestRegistry CU_create_new_registry(void) +{ + CU_pTestRegistry pRegistry = (CU_pTestRegistry)CU_MALLOC(sizeof(CU_TestRegistry)); + if (NULL != pRegistry) { + pRegistry->pSuite = NULL; + pRegistry->uiNumberOfSuites = 0; + pRegistry->uiNumberOfTests = 0; + } + + return pRegistry; +} + +/*------------------------------------------------------------------------*/ +void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry) +{ + assert(NULL != ppRegistry); + + /* Note - CU_cleanup_registry counts on being able to pass NULL */ + + if (NULL != *ppRegistry) { + cleanup_test_registry(*ppRegistry); + } + CU_FREE(*ppRegistry); + *ppRegistry = NULL; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite_by_name(const char* szSuiteName, CU_pTestRegistry pRegistry) +{ + CU_pSuite pSuite = NULL; + CU_pSuite pCur = NULL; + + assert(NULL != pRegistry); + assert(NULL != szSuiteName); + + pCur = pRegistry->pSuite; + while (NULL != pCur) { + if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szSuiteName))) { + pSuite = pCur; + break; + } + pCur = pCur->pNext; + } + + return pSuite; +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_suite_by_index(unsigned int index, CU_pTestRegistry pRegistry) +{ + CU_pSuite result = NULL; + unsigned int i; + + assert(NULL != pRegistry); + + if ((index > 0) && (index <= f_pTestRegistry->uiNumberOfSuites)) { + result = f_pTestRegistry->pSuite; + for (i=1 ; ipNext; + } + } + + return result; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test_by_name(const char* szTestName, CU_pSuite pSuite) +{ + CU_pTest pTest = NULL; + CU_pTest pCur = NULL; + + assert(NULL != pSuite); + assert(NULL != szTestName); + + pCur = pSuite->pTest; + while (NULL != pCur) { + if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szTestName))) { + pTest = pCur; + break; + } + pCur = pCur->pNext; + } + + return pTest; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_test_by_index(unsigned int index, CU_pSuite pSuite) +{ + CU_pTest result = NULL; + unsigned int i; + + assert(NULL != pSuite); + + if ((index > 0) && (index <= pSuite->uiNumberOfTests)) { + result = pSuite->pTest; + for (i=1 ; ipNext; + } + } + + return result; +} +/** @} */ + +/*------------------------------------------------------------------------*/ +/*------------------------------------------------------------------------*/ +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +static int sfunc1(void) +{ return 0; } + +static void test1(void) +{} + +static void test2(void) +{} + +/*--------------------------------------------------*/ +static void test_CU_initialize_registry(void) +{ + CU_pTestRegistry pReg = NULL; + unsigned int ndeallocs_before; + + /* initial state */ + TEST(NULL == CU_get_registry()); + TEST(CU_FALSE == CU_registry_initialized()); + + /* after normal initialization */ + TEST(CUE_SUCCESS == CU_initialize_registry()); + pReg = CU_get_registry(); + TEST_FATAL(NULL != pReg); + TEST(CU_TRUE == CU_registry_initialized()); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(NULL == pReg->pSuite); + + /* after reinitialization */ + TEST(0 < test_cunit_get_n_memevents(pReg)); + ndeallocs_before = test_cunit_get_n_deallocations(pReg); + TEST(CUE_SUCCESS == CU_initialize_registry()); + TEST((ndeallocs_before + 1) == test_cunit_get_n_deallocations(pReg)); + pReg = CU_get_registry(); + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(NULL == pReg->pSuite); + + /* after cleanup */ + CU_cleanup_registry(); + TEST(NULL == CU_get_registry()); + TEST(CU_FALSE == CU_registry_initialized()); + + /* if malloc fails */ + test_cunit_deactivate_malloc(); + TEST(CUE_NOMEMORY == CU_initialize_registry()); + TEST(NULL == CU_get_registry()); + TEST(CU_FALSE == CU_registry_initialized()); + test_cunit_activate_malloc(); +} + +/*--------------------------------------------------*/ +static void test_CU_cleanup_registry(void) +{ + /* make sure calling with uninitialized registry does not crash */ + CU_cleanup_registry(); + CU_cleanup_registry(); + CU_cleanup_registry(); + CU_cleanup_registry(); + CU_cleanup_registry(); + + /* nothing more to do over test_CU_initialize_registry() */ +} + +/*--------------------------------------------------*/ +/* test CU_add_suite() + * CU_get_suite_by_name() + * CU_get_suite_by_index() + */ +static void test_CU_add_suite(void) +{ + CU_pSuite pSuite = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTestRegistry pReg = NULL; + + CU_cleanup_registry(); /* make sure registry not initialized */ + + /* error condition - registry not initialized */ + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_NOREGISTRY == CU_get_error()); + TEST(NULL == pSuite); + + /* error condition - no name */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite = CU_add_suite(NULL, NULL, NULL); + TEST(CUE_NO_SUITENAME == CU_get_error()); + TEST(NULL == pSuite); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* warning condition - duplicate name */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + pSuite2 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_DUP_SUITE == CU_get_error()); + TEST(NULL != pSuite2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + TEST(!strcmp("suite1", pSuite->pName)); + TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite->pTest == NULL); /* no tests added yet */ + TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite->pNext == pSuite2); /* now have another suite */ + + TEST(!strcmp("suite1", pSuite2->pName)); + TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite2->pTest == NULL); /* no tests added yet */ + TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite2->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite2->pNext == NULL); /* end of the list */ + + /* error condition - memory allocation failure */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + test_cunit_deactivate_malloc(); + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_NOMEMORY == CU_get_error()); + TEST(NULL == pSuite); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + test_cunit_activate_malloc(); + + /* normal creation & cleanup */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == NULL); + + pSuite = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite1", pReg) == pSuite); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == NULL); + TEST(pReg->pSuite == pSuite); + + TEST(!strcmp("suite1", pSuite->pName)); + TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite->pTest == NULL); /* no tests added yet */ + TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite->pNext == NULL); /* no more suites added yet */ + + pSuite2 = CU_add_suite("suite2", sfunc1, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite2", pReg) == pSuite2); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == pSuite2); + TEST(CU_get_suite_by_index(3, pReg) == NULL); + + pSuite3 = CU_add_suite("suite3", NULL, sfunc1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite3); + TEST(3 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite3", pReg) == pSuite3); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == pSuite2); + TEST(CU_get_suite_by_index(3, pReg) == pSuite3); + TEST(CU_get_suite_by_index(4, pReg) == NULL); + + pSuite4 = CU_add_suite("suite4", sfunc1, sfunc1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pSuite4); + TEST(4 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(CU_get_suite_by_name("suite4", pReg) == pSuite4); + TEST(CU_get_suite_by_index(0, pReg) == NULL); + TEST(CU_get_suite_by_index(1, pReg) == pSuite); + TEST(CU_get_suite_by_index(2, pReg) == pSuite2); + TEST(CU_get_suite_by_index(3, pReg) == pSuite3); + TEST(CU_get_suite_by_index(4, pReg) == pSuite4); + TEST(CU_get_suite_by_index(5, pReg) == NULL); + + /* test registry suite structures */ + TEST(pReg->pSuite == pSuite); + + TEST(!strcmp("suite1", pSuite->pName)); + TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite->pTest == NULL); /* no tests added yet */ + TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite->pNext == pSuite2); /* now have another suite */ + + TEST(!strcmp("suite2", pSuite2->pName)); + TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite2->pTest == NULL); /* no tests added yet */ + TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite2->pInitializeFunc == sfunc1); /* no init function */ + TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite2->pNext == pSuite3); /* next suite in list */ + + TEST(!strcmp("suite3", pSuite3->pName)); + TEST(pSuite3->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite3->pTest == NULL); /* no tests added yet */ + TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite3->pCleanupFunc == sfunc1); /* no cleanup function */ + TEST(pSuite3->pNext == pSuite4); /* next suite in list */ + + TEST(!strcmp("suite4", pSuite4->pName)); + TEST(pSuite4->fActive == CU_TRUE); /* suite active on creation */ + TEST(pSuite4->pTest == NULL); /* no tests added yet */ + TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ + TEST(pSuite4->pCleanupFunc == sfunc1); /* no cleanup function */ + TEST(pSuite4->pNext == NULL); /* end of suite list */ + + TEST(0 != test_cunit_get_n_memevents(pSuite)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pSuite3)); + TEST(0 != test_cunit_get_n_memevents(pSuite4)); + + TEST(test_cunit_get_n_allocations(pSuite) != test_cunit_get_n_deallocations(pSuite)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); + + /* clean up everything and confirm deallocation */ + CU_cleanup_registry(); + + TEST(test_cunit_get_n_allocations(pSuite) == test_cunit_get_n_deallocations(pSuite)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); +} + +/*--------------------------------------------------*/ +/* test CU_set_suite_active() + * CU_set_suite_name() + * CU_set_suite_initfunc() + * CU_set_suite_cleanupfunc() + */ +static void test_CU_set_suite_attributes(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + + /* initialize system */ + CU_initialize_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST_FATAL(NULL != pSuite1); + TEST_FATAL(NULL != CU_add_test(pSuite1, "test1", test1)); + TEST_FATAL(NULL != CU_add_test(pSuite1, "test2", test1)); + + pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ + TEST_FATAL(NULL != pSuite2); + TEST_FATAL(NULL != CU_add_test(pSuite2, "test1", test1)); + TEST_FATAL(NULL != CU_add_test(pSuite2, "test2", test1)); + + /* test CU_set_suite_active() */ + TEST(CUE_NOSUITE == CU_set_suite_active(NULL, CU_FALSE)); /* error - NULL suite */ + + TEST(pSuite2->fActive == CU_TRUE); /* suites active on creation */ + TEST(pSuite1->fActive == CU_TRUE); + + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); + TEST(pSuite1->fActive == CU_TRUE); + TEST(pSuite2->fActive == CU_FALSE); + + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); + TEST(pSuite1->fActive == CU_FALSE); + TEST(pSuite2->fActive == CU_FALSE); + + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_TRUE)); + TEST(pSuite1->fActive == CU_FALSE); + TEST(pSuite2->fActive == CU_TRUE); + + /* test CU_set_suite_name() */ + TEST(CUE_NOSUITE == CU_set_suite_name(NULL, "null suite")); /* error - NULL suite */ + TEST(CUE_NO_SUITENAME == CU_set_suite_name(pSuite1, NULL)); /* error - NULL name */ + + TEST(!strcmp(pSuite1->pName, "suite1")); + TEST(!strcmp(pSuite2->pName, "suite2")); + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "This is my new name.")); + TEST(!strcmp(pSuite1->pName, "This is my new name.")); + TEST(!strcmp(pSuite2->pName, "suite2")); + + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "Never mind.")); + TEST(!strcmp(pSuite1->pName, "This is my new name.")); + TEST(!strcmp(pSuite2->pName, "Never mind.")); + + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "suite1")); + TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "suite2")); + TEST(!strcmp(pSuite1->pName, "suite1")); + TEST(!strcmp(pSuite2->pName, "suite2")); + + /* test CU_set_suite_initfunc() */ + TEST(CUE_NOSUITE == CU_set_suite_initfunc(NULL, &sfunc1)); /* error - NULL suite */ + + TEST(pSuite1->pInitializeFunc == NULL); + TEST(pSuite2->pInitializeFunc == &sfunc1); + TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite1, &sfunc1)); + TEST(pSuite1->pInitializeFunc == &sfunc1); + TEST(pSuite2->pInitializeFunc == &sfunc1); + + TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite2, NULL)); + TEST(pSuite1->pInitializeFunc == &sfunc1); + TEST(pSuite2->pInitializeFunc == NULL); + + /* test CU_set_suite_cleanupfunc() */ + TEST(CUE_NOSUITE == CU_set_suite_cleanupfunc(NULL, &sfunc1)); + + TEST(pSuite1->pCleanupFunc == NULL); + TEST(pSuite2->pCleanupFunc == NULL); + TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite1, &sfunc1)); + TEST(pSuite1->pCleanupFunc == &sfunc1); + TEST(pSuite2->pCleanupFunc == NULL); + + TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite2, &sfunc1)); + TEST(pSuite1->pCleanupFunc == &sfunc1); + TEST(pSuite2->pCleanupFunc == &sfunc1); + + /* clean up */ + CU_cleanup_registry(); +} + +static void test_succeed(void) { CU_TEST(CU_TRUE); } +static void test_fail(void) { CU_TEST(CU_FALSE); } +static int suite_succeed(void) { return 0; } +static int suite_fail(void) { return 1; } + +/*--------------------------------------------------*/ +/* test CU_get_suite() + * CU_get_suite_at_pos() + * CU_get_suite_pos() + * CU_get_suite_pos_by_name() + */ +static void test_get_suite_functions(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pSuite pSuite5 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + CU_pTest pTest10 = NULL; + + /* error condition - registry not initialized */ + CU_cleanup_registry(); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_suite("suite1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_suite_at_pos(0)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos(pSuite1)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos_by_name("suite1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test9", test_succeed); + pSuite4 = CU_add_suite("suite4", NULL, suite_fail); + pTest10 = CU_add_test(pSuite4, "test10", test_succeed); + + /* error condition - invalid parameters */ + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_suite(NULL)); + TEST(CUE_NO_SUITENAME == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos(NULL)); + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_suite_pos_by_name(NULL)); + TEST(CUE_NO_SUITENAME == CU_get_error()); + + /* normal operation - CU_get_suite() */ + + TEST(NULL == CU_get_suite("")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_suite("bad name")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_suite("suite3")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pSuite1 == CU_get_suite("suite1")); /* valid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite2 == CU_get_suite("suite2")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite4 == CU_get_suite("suite4")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_suite_at_pos() */ + + TEST(NULL == CU_get_suite_at_pos(0)); /* invalid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_suite_at_pos(5)); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pSuite1 == CU_get_suite_at_pos(1)); /* valid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite2 == CU_get_suite_at_pos(2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite3 == CU_get_suite_at_pos(3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pSuite4 == CU_get_suite_at_pos(4)); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_suite_pos() */ + + pSuite5 = (CU_pSuite)malloc(sizeof(CU_Suite)); + TEST_FATAL(NULL != pSuite5); + + TEST(0 == CU_get_suite_pos(pSuite5)); /* invalid suite */ + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_suite_pos(pSuite1)); /* valid suites */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_suite_pos(pSuite2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(3 == CU_get_suite_pos(pSuite3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_suite_pos(pSuite4)); + TEST(CUE_SUCCESS == CU_get_error()); + + free(pSuite5); + + /* normal operation - CU_get_suite_pos_by_name() */ + + TEST(0 == CU_get_suite_pos_by_name("")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_suite_pos_by_name("suite3")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_suite_pos_by_name("suite1")); /* valid suites */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_suite_pos_by_name("suite2")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_suite_pos_by_name("suite4")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* clean up */ + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +/* test CU_add_test() + * CU_get_test_by_name() + * CU_get_test_by_index() + */ +static void test_CU_add_test(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTestRegistry pReg = NULL; + + CU_cleanup_registry(); + + /* error condition - registry not initialized */ + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_NOREGISTRY == CU_get_error()); + TEST(NULL == pTest1); + + CU_initialize_registry(); + pReg = CU_get_registry(); + + /* error condition - no suite */ + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_NOSUITE == CU_get_error()); + TEST(NULL == pTest1); + + /* error condition - no name */ + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + pTest1 = CU_add_test(pSuite1, NULL, test1); + TEST(CUE_NO_TESTNAME == CU_get_error()); + TEST(NULL == pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(0 == pSuite1->uiNumberOfTests); + + /* error condition - no test function */ + pTest1 = CU_add_test(pSuite1, "test1", NULL); + TEST(CUE_NOTEST == CU_get_error()); + TEST(NULL == pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(0 == pSuite1->uiNumberOfTests); + + /* warning condition - duplicate name */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(1 == pReg->uiNumberOfTests); + TEST(1 == pSuite1->uiNumberOfTests); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == NULL); + + pTest2 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_DUP_TEST == CU_get_error()); + TEST(NULL != pTest2); + TEST(1 == pReg->uiNumberOfSuites); + TEST(2 == pReg->uiNumberOfTests); + TEST(2 == pSuite1->uiNumberOfTests); + + TEST(!strcmp("test1", pTest1->pName)); + TEST(pTest1->fActive == CU_TRUE); + TEST(pTest1->pNext == pTest2); + TEST(pTest1->pJumpBuf == NULL); + TEST(pTest1->pTestFunc == test1); + TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == pTest2); + TEST(CU_get_test_by_index(3, pSuite1) == NULL); + + TEST(!strcmp("test1", pTest2->pName)); + TEST(pTest2->fActive == CU_TRUE); + TEST(pTest2->pNext == NULL); + TEST(pTest2->pJumpBuf == NULL); + TEST(pTest2->pTestFunc == test1); + TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); + + /* error condition - memory allocation failure */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + test_cunit_deactivate_malloc(); + pTest1 = CU_add_test(pSuite1, "test1", test1); + test_cunit_activate_malloc(); + TEST(CUE_NOMEMORY == CU_get_error()); + TEST(NULL == pTest1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(0 == pSuite1->uiNumberOfTests); + + /* normal creation & cleanup */ + CU_initialize_registry(); + pReg = CU_get_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST(CUE_SUCCESS == CU_get_error()); + pSuite2 = CU_add_suite("suite2", sfunc1, sfunc1); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == NULL); + + pTest1 = CU_add_test(pSuite1, "test1", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest1); + TEST(2 == pReg->uiNumberOfSuites); + TEST(1 == pReg->uiNumberOfTests); + TEST(1 == pSuite1->uiNumberOfTests); + TEST(0 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == NULL); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == NULL); + + pTest2 = CU_add_test(pSuite2, "test2", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(2 == pReg->uiNumberOfTests); + TEST(1 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest2); + TEST(CU_get_test_by_index(0, pSuite2) == NULL); + TEST(CU_get_test_by_index(1, pSuite2) == pTest2); + TEST(CU_get_test_by_index(2, pSuite2) == NULL); + + pTest3 = CU_add_test(pSuite1, "test3", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest3); + TEST(2 == pReg->uiNumberOfSuites); + TEST(3 == pReg->uiNumberOfTests); + TEST(2 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest2); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == pTest3); + TEST(CU_get_test_by_index(3, pSuite1) == NULL); + + pTest4 = CU_add_test(pSuite1, "test4", test1); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL != pTest4); + TEST(2 == pReg->uiNumberOfSuites); + TEST(4 == pReg->uiNumberOfTests); + TEST(3 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest2); + TEST(CU_get_test_by_index(0, pSuite1) == NULL); + TEST(CU_get_test_by_index(1, pSuite1) == pTest1); + TEST(CU_get_test_by_index(2, pSuite1) == pTest3); + TEST(CU_get_test_by_index(3, pSuite1) == pTest4); + TEST(CU_get_test_by_index(4, pSuite1) == NULL); + + TEST(!strcmp("test1", pTest1->pName)); + TEST(pTest1->pNext == pTest3); + TEST(pTest1->pJumpBuf == NULL); + TEST(pTest1->pTestFunc == test1); + TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); + TEST(CU_get_test_by_name("test1", pSuite2) == NULL); + + TEST(!strcmp("test2", pTest2->pName)); + TEST(pTest2->pNext == NULL); + TEST(pTest2->pJumpBuf == NULL); + TEST(pTest2->pTestFunc == test1); + TEST(CU_get_test_by_name("test2", pSuite1) == NULL); + TEST(CU_get_test_by_name("test2", pSuite2) == pTest2); + + TEST(!strcmp("test3", pTest3->pName)); + TEST(pTest3->pNext == pTest4); + TEST(pTest3->pJumpBuf == NULL); + TEST(pTest3->pTestFunc == test1); + TEST(CU_get_test_by_name("test3", pSuite1) == pTest3); + TEST(CU_get_test_by_name("test3", pSuite2) == NULL); + + TEST(!strcmp("test4", pTest4->pName)); + TEST(pTest4->pNext == NULL); + TEST(pTest4->pJumpBuf == NULL); + TEST(pTest4->pTestFunc == test1); + TEST(CU_get_test_by_name("test4", pSuite1) == pTest4); + TEST(CU_get_test_by_name("test4", pSuite2) == NULL); + + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + TEST(0 != test_cunit_get_n_memevents(pTest3)); + TEST(0 != test_cunit_get_n_memevents(pTest4)); + + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); + + CU_cleanup_registry(); + + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); +} + +/*--------------------------------------------------*/ +/* test CU_set_test_active() + * CU_set_test_name() + * CU_set_test_func() + */ +static void test_CU_set_test_attributes(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + + /* initialize system */ + CU_initialize_registry(); + + pSuite1 = CU_add_suite("suite1", NULL, NULL); + TEST_FATAL(NULL != pSuite1); + pTest1 = CU_add_test(pSuite1, "test1", test2); + TEST_FATAL(NULL != pTest1); + pTest2 = CU_add_test(pSuite1, "test2", test1); + TEST_FATAL(NULL != pTest2); + + pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ + TEST_FATAL(NULL != pSuite2); + pTest3 = CU_add_test(pSuite2, "test3", test2); + TEST_FATAL(NULL != pTest3); + pTest4 = CU_add_test(pSuite2, "test4", test1); + TEST_FATAL(NULL != pTest4); + + /* test CU_set_test_active() */ + TEST(CUE_NOTEST == CU_set_test_active(NULL, CU_FALSE)); /* error - NULL test */ + + TEST(CU_TRUE == pTest1->fActive); /* tests active on creation */ + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_TRUE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); + TEST(CU_TRUE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_FALSE == pTest3->fActive); + TEST(CU_FALSE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); + TEST(CU_FALSE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_FALSE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); + TEST(CU_FALSE == pTest1->fActive); + TEST(CU_FALSE == pTest2->fActive); + TEST(CU_FALSE == pTest3->fActive); + TEST(CU_FALSE == pTest4->fActive); + + TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); + TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_TRUE)); + TEST(CU_TRUE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_TRUE == pTest4->fActive); + + /* test CU_set_test_name() */ + TEST(CUE_NOTEST == CU_set_test_name(NULL, "null test")); /* error - NULL test */ + TEST(CUE_NO_TESTNAME == CU_set_test_name(pTest1, NULL)); /* error - NULL name */ + + TEST(!strcmp("test1", pTest1->pName)); + TEST(!strcmp("test2", pTest2->pName)); + TEST(!strcmp("test3", pTest3->pName)); + TEST(!strcmp("test4", pTest4->pName)); + + TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "Aren't I a pretty girl?")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "Polly want a cracker.")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "@This is utter nonsense@")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "Yep!")); + + TEST(!strcmp("Aren't I a pretty girl?", pTest1->pName)); + TEST(!strcmp("Polly want a cracker.", pTest2->pName)); + TEST(!strcmp("@This is utter nonsense@", pTest3->pName)); + TEST(!strcmp("Yep!", pTest4->pName)); + + TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "test1")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "test2")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "test3")); + TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "test4")); + + TEST(!strcmp("test1", pTest1->pName)); + TEST(!strcmp("test2", pTest2->pName)); + TEST(!strcmp("test3", pTest3->pName)); + TEST(!strcmp("test4", pTest4->pName)); + + /* test CU_set_test_func() */ + TEST(CUE_NOTEST == CU_set_test_func(NULL, &test1)); /* error - NULL test */ + TEST(CUE_NOTEST == CU_set_test_func(pTest1, NULL)); /* error - NULL test function */ + + TEST(&test2 == pTest1->pTestFunc); + TEST(&test1 == pTest2->pTestFunc); + TEST(&test2 == pTest3->pTestFunc); + TEST(&test1 == pTest4->pTestFunc); + + TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test1)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test2)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test1)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test2)); + + TEST(&test1 == pTest1->pTestFunc); + TEST(&test2 == pTest2->pTestFunc); + TEST(&test1 == pTest3->pTestFunc); + TEST(&test2 == pTest4->pTestFunc); + + TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test2)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test1)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test2)); + TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test1)); + + /* clean up */ + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +/* test CU_get_test() + * CU_get_test_at_pos() + * CU_get_test_pos() + * CU_get_test_pos_by_name() + */ +static void test_get_test_functions(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + CU_pTest pTest10 = NULL; + + /* error condition - registry not initialized */ + CU_cleanup_registry(); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test(pSuite1, "test1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos(pSuite1, pTest1)); + TEST(CUE_NOREGISTRY == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos_by_name(pSuite1, "test1")); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test9", test_succeed); + pSuite4 = CU_add_suite("suite4", NULL, suite_fail); + pTest10 = CU_add_test(pSuite4, "test10", test_succeed); + + /* error condition - invalid parameters */ + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test(NULL, "test1")); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(NULL == CU_get_test(pSuite1, NULL)); /* name NULL */ + TEST(CUE_NO_SUITENAME == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_at_pos(NULL, 1)); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos(NULL, pTest1)); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos(pSuite1, NULL)); /* test NULL */ + TEST(CUE_NOTEST == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos_by_name(NULL, "test1")); /* suite NULL */ + TEST(CUE_NOSUITE == CU_get_error()); + + CU_set_error(CUE_SUCCESS); + TEST(0 == CU_get_test_pos_by_name(pSuite1, NULL)); /* name NULL */ + TEST(CUE_NO_TESTNAME == CU_get_error()); + + /* normal operation - CU_get_test() */ + + TEST(NULL == CU_get_test(pSuite1, "")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test(pSuite2, "bad name")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test(pSuite1, "test3")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pTest1 == CU_get_test(pSuite1, "test1")); /* valid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest6 == CU_get_test(pSuite2, "test6")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest8 == CU_get_test(pSuite3, "test8")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_test_at_pos() */ + + TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); /* invalid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test_at_pos(pSuite1, 6)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(NULL == CU_get_test_at_pos(pSuite4, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(pTest1 == CU_get_test_at_pos(pSuite1, 1)); /* valid positions */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest2 == CU_get_test_at_pos(pSuite1, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest3 == CU_get_test_at_pos(pSuite1, 3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest4 == CU_get_test_at_pos(pSuite1, 4)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest5 == CU_get_test_at_pos(pSuite1, 5)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest6 == CU_get_test_at_pos(pSuite2, 1)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest7 == CU_get_test_at_pos(pSuite2, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest8 == CU_get_test_at_pos(pSuite3, 1)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest9 == CU_get_test_at_pos(pSuite3, 2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(pTest10 == CU_get_test_at_pos(pSuite4, 1)); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_test_pos() */ + + TEST(0 == CU_get_test_pos(pSuite1, pTest6)); /* invalid tests */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_test_pos(pSuite4, pTest6)); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_test_pos(pSuite1, pTest1)); /* valid tests */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos(pSuite1, pTest2)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(3 == CU_get_test_pos(pSuite1, pTest3)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_test_pos(pSuite1, pTest4)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(5 == CU_get_test_pos(pSuite1, pTest5)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos(pSuite2, pTest6)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos(pSuite2, pTest7)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos(pSuite3, pTest8)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos(pSuite3, pTest9)); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos(pSuite4, pTest10)); + TEST(CUE_SUCCESS == CU_get_error()); + + /* normal operation - CU_get_test_pos_by_name() */ + + TEST(0 == CU_get_test_pos_by_name(pSuite1, "")); /* invalid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_test_pos_by_name(pSuite1, "test9")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(0 == CU_get_test_pos_by_name(pSuite2, "test1")); + TEST(CUE_SUCCESS == CU_get_error()); + + TEST(1 == CU_get_test_pos_by_name(pSuite1, "test1")); /* valid names */ + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos_by_name(pSuite1, "test2")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(4 == CU_get_test_pos_by_name(pSuite1, "test4")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos_by_name(pSuite2, "test6")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos_by_name(pSuite2, "test7")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos_by_name(pSuite3, "test8")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(2 == CU_get_test_pos_by_name(pSuite3, "test9")); + TEST(CUE_SUCCESS == CU_get_error()); + TEST(1 == CU_get_test_pos_by_name(pSuite4, "test10")); + TEST(CUE_SUCCESS == CU_get_error()); + + /* clean up */ + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +static void test_CU_get_registry(void) +{ + CU_cleanup_registry(); + TEST(NULL == CU_get_registry()); + + CU_initialize_registry(); + TEST(NULL != CU_get_registry()); + TEST(f_pTestRegistry == CU_get_registry()); + + CU_cleanup_registry(); +} + +/*--------------------------------------------------*/ +static void test_CU_set_registry(void) +{ + CU_pTestRegistry pReg1 = NULL; + CU_pTestRegistry pReg2 = NULL; + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pSuite2 = CU_add_suite("suite2", NULL, NULL); + + CU_add_test(pSuite1, "test1", test1); + CU_add_test(pSuite1, "test2", test1); + CU_add_test(pSuite2, "test1", test1); + CU_add_test(pSuite2, "test2", test1); + + pReg1 = CU_get_registry(); + + TEST(pReg1->pSuite == pSuite1); + TEST(pReg1->uiNumberOfSuites == 2); + TEST(pReg1->uiNumberOfTests == 4); + TEST(0 < test_cunit_get_n_memevents(pReg1)); + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + CU_set_registry(NULL); + + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + CU_cleanup_registry(); + + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + pReg2 = CU_create_new_registry(); + CU_set_registry(pReg2); + + TEST(pReg1->pSuite == pSuite1); + TEST(pReg1->uiNumberOfSuites == 2); + TEST(pReg1->uiNumberOfTests == 4); + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + + TEST(CU_get_registry()->pSuite == NULL); + TEST(CU_get_registry()->uiNumberOfSuites == 0); + TEST(CU_get_registry()->uiNumberOfTests == 0); + TEST(0 < test_cunit_get_n_memevents(pReg2)); + TEST(test_cunit_get_n_allocations(pReg2) != test_cunit_get_n_deallocations(pReg2)); + + CU_cleanup_registry(); + + TEST(pReg1->pSuite == pSuite1); + TEST(pReg1->uiNumberOfSuites == 2); + TEST(pReg1->uiNumberOfTests == 4); + TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); + TEST(test_cunit_get_n_allocations(pReg2) == test_cunit_get_n_deallocations(pReg2)); + + CU_set_registry(pReg1); + CU_cleanup_registry(); + TEST(test_cunit_get_n_allocations(pReg1) == test_cunit_get_n_deallocations(pReg1)); +} + +/*--------------------------------------------------*/ +/* test CU_create_new_registry() + * CU_destroy_existing_registry() + */ +static void test_CU_create_new_registry(void) +{ + CU_pTestRegistry pReg = NULL; + CU_pTestRegistry pRegOld = NULL; + + CU_cleanup_registry(); + pReg = CU_create_new_registry(); + + TEST(NULL != pReg); + TEST(0 < test_cunit_get_n_memevents(pReg)); + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + + TEST(pReg->pSuite == NULL); + TEST(pReg->uiNumberOfSuites == 0); + TEST(pReg->uiNumberOfTests == 0); + + CU_cleanup_registry(); + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + + pRegOld = pReg; + CU_destroy_existing_registry(&pReg); + TEST(test_cunit_get_n_allocations(pRegOld) == test_cunit_get_n_deallocations(pRegOld)); + TEST(NULL == pReg); +} + +/*--------------------------------------------------*/ +static void test_cleanup_test_registry(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTestRegistry pReg = CU_create_new_registry(); + + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* create tests to register */ + pTest1 = create_test("test1", test1); + pTest2 = create_test("test2", NULL); + pTest3 = create_test("test3", test1); + pTest4 = create_test("", NULL); + + /* create suites to hold tests */ + pSuite1 = create_suite("suite1", NULL, NULL); + pSuite2 = create_suite("suite2", sfunc1, sfunc1); + insert_suite(pReg, pSuite1); + insert_suite(pReg, pSuite2); + + insert_test(pSuite1, pTest1); + insert_test(pSuite1, pTest2); + insert_test(pSuite1, pTest3); + insert_test(pSuite2, pTest4); + + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); /* not managed in primitive functions */ + TEST(3 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest4); + TEST(pTest1->pNext == pTest2); + TEST(pTest1->pPrev == NULL); + TEST(pTest2->pNext == pTest3); + TEST(pTest2->pPrev == pTest1); + TEST(pTest3->pNext == NULL); + TEST(pTest3->pPrev == pTest2); + TEST(pTest4->pNext == NULL); + TEST(pTest4->pPrev == NULL); + + TEST(0 != test_cunit_get_n_memevents(pReg)); + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + TEST(0 != test_cunit_get_n_memevents(pTest3)); + TEST(0 != test_cunit_get_n_memevents(pTest4)); + + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); + + cleanup_test_registry(pReg); + CU_FREE(pReg); + + TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); +} + +/*--------------------------------------------------*/ +/* test create_suite() + * cleanup_suite() + */ +static void test_create_suite(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + + /* error condition - memory allocation failure */ + test_cunit_deactivate_malloc(); + pSuite1 = create_suite("suite1", NULL, NULL); + TEST(NULL == pSuite1); + test_cunit_activate_malloc(); + + /* normal creation & cleanup */ + pSuite1 = create_suite("suite1", NULL, NULL); + TEST(NULL != pSuite1); + TEST(!strcmp("suite1", pSuite1->pName)); + TEST(pSuite1->pTest == NULL); /* no tests added yet */ + TEST(pSuite1->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite1->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite1->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite1->pNext == NULL); /* no more suites added yet */ + + pSuite2 = create_suite("suite2", sfunc1, NULL); + TEST(NULL != pSuite2); + TEST(!strcmp("suite2", pSuite2->pName)); + TEST(pSuite2->pTest == NULL); /* no tests added yet */ + TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite2->pInitializeFunc == sfunc1); /* init function */ + TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ + TEST(pSuite2->pNext == NULL); /* no more suites added yet */ + + pSuite3 = create_suite("suite3", NULL, sfunc1); + TEST(NULL != pSuite3); + TEST(!strcmp("suite3", pSuite3->pName)); + TEST(pSuite3->pTest == NULL); /* no tests added yet */ + TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ + TEST(pSuite3->pCleanupFunc == sfunc1); /* cleanup function */ + TEST(pSuite3->pNext == NULL); /* no more suites added yet */ + + pSuite4 = create_suite("suite4", sfunc1, sfunc1); + TEST(NULL != pSuite4); + TEST(!strcmp("suite4", pSuite4->pName)); + TEST(pSuite4->pTest == NULL); /* no tests added yet */ + TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ + TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ + TEST(pSuite4->pCleanupFunc == sfunc1); /* cleanup function */ + TEST(pSuite4->pNext == NULL); /* no more suites added yet */ + + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + cleanup_suite(pSuite1); + CU_FREE(pSuite1); + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + cleanup_suite(pSuite2); + CU_FREE(pSuite2); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + + TEST(0 != test_cunit_get_n_memevents(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); + cleanup_suite(pSuite3); + CU_FREE(pSuite3); + TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); + + TEST(0 != test_cunit_get_n_memevents(pSuite4)); + TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); + cleanup_suite(pSuite4); + CU_FREE(pSuite4); + TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); +} + +/*--------------------------------------------------*/ +/* test insert_suite() + * suite_exists() + */ +static void test_insert_suite(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTestRegistry pReg = CU_create_new_registry(); + + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(NULL == pReg->pSuite); + TEST(CU_FALSE == suite_exists(pReg, "suite1")); + TEST(CU_FALSE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + /* normal creation & cleanup */ + pSuite1 = create_suite("suite1", NULL, NULL); + insert_suite(pReg, pSuite1); + TEST(1 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_FALSE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + pSuite2 = create_suite("suite2", sfunc1, NULL); + insert_suite(pReg, pSuite2); + TEST(2 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == pSuite2); + TEST(pSuite2->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_TRUE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + pSuite3 = create_suite("suite3", NULL, sfunc1); + insert_suite(pReg, pSuite3); + TEST(3 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == pSuite2); + TEST(pSuite2->pNext == pSuite3); + TEST(pSuite3->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_TRUE == suite_exists(pReg, "suite2")); + TEST(CU_TRUE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + pSuite4 = create_suite("suite4", sfunc1, sfunc1); + insert_suite(pReg, pSuite4); + TEST(4 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + TEST(pReg->pSuite == pSuite1); + TEST(pSuite1->pNext == pSuite2); + TEST(pSuite2->pNext == pSuite3); + TEST(pSuite3->pNext == pSuite4); + TEST(pSuite4->pNext == NULL); + TEST(CU_TRUE == suite_exists(pReg, "suite1")); + TEST(CU_TRUE == suite_exists(pReg, "suite2")); + TEST(CU_TRUE == suite_exists(pReg, "suite3")); + TEST(CU_TRUE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + + TEST(0 != test_cunit_get_n_memevents(pReg)); + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pSuite3)); + TEST(0 != test_cunit_get_n_memevents(pSuite4)); + + TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); + + cleanup_test_registry(pReg); + TEST(CU_FALSE == suite_exists(pReg, "suite1")); + TEST(CU_FALSE == suite_exists(pReg, "suite2")); + TEST(CU_FALSE == suite_exists(pReg, "suite3")); + TEST(CU_FALSE == suite_exists(pReg, "suite4")); + TEST(CU_FALSE == suite_exists(pReg, "suite5")); + TEST(CU_FALSE == suite_exists(pReg, "")); + CU_FREE(pReg); + + TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); + TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); +} + +/*--------------------------------------------------*/ +static void test_create_test(void) +{ + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + + /* error condition - memory allocation failure */ + test_cunit_deactivate_malloc(); + pTest1 = create_test("test1", test1); + test_cunit_activate_malloc(); + TEST(NULL == pTest1); + + /* normal creation & cleanup */ + pTest1 = create_test("test1", test1); + TEST(NULL != pTest1); + TEST(pTest1->pTestFunc == test1); + TEST(!strcmp("test1", pTest1->pName)); + TEST(pTest1->pNext == NULL); + TEST(pTest1->pPrev == NULL); + TEST(pTest1->pJumpBuf == NULL); + + pTest2= create_test("test2", NULL); + TEST(NULL != pTest2); + TEST(pTest2->pTestFunc == NULL); + TEST(!strcmp("test2", pTest2->pName)); + TEST(pTest2->pNext == NULL); + TEST(pTest2->pPrev == NULL); + TEST(pTest2->pJumpBuf == NULL); + + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + + cleanup_test(pTest1); + CU_FREE(pTest1); + cleanup_test(pTest2); + CU_FREE(pTest2); + + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); +} + +/*--------------------------------------------------*/ +/* test insert_test() + * test_exists() + */ +static void test_insert_test(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + + /* create tests to register */ + pTest1 = create_test("test1", test1); + pTest2 = create_test("test2", NULL); + pTest3 = create_test("test3", test1); + pTest4 = create_test("", NULL); + + /* create suites to hold tests */ + pSuite1 = create_suite("suite1", NULL, NULL); + pSuite2 = create_suite("suite2", sfunc1, sfunc1); + + TEST(CU_FALSE == test_exists(pSuite1, "test1")); + TEST(CU_FALSE == test_exists(pSuite1, "test2")); + TEST(CU_FALSE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_FALSE == test_exists(pSuite2, "")); + + insert_test(pSuite1, pTest1); + insert_test(pSuite1, pTest2); + insert_test(pSuite1, pTest3); + insert_test(pSuite2, pTest4); + + TEST(CU_TRUE == test_exists(pSuite1, "test1")); + TEST(CU_TRUE == test_exists(pSuite1, "test2")); + TEST(CU_TRUE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_TRUE == test_exists(pSuite2, "")); + + TEST(3 == pSuite1->uiNumberOfTests); + TEST(1 == pSuite2->uiNumberOfTests); + TEST(pSuite1->pTest == pTest1); + TEST(pSuite2->pTest == pTest4); + TEST(pTest1->pNext == pTest2); + TEST(pTest1->pPrev == NULL); + TEST(pTest2->pNext == pTest3); + TEST(pTest2->pPrev == pTest1); + TEST(pTest3->pNext == NULL); + TEST(pTest3->pPrev == pTest2); + TEST(pTest4->pNext == NULL); + TEST(pTest4->pPrev == NULL); + + TEST(0 != test_cunit_get_n_memevents(pSuite1)); + TEST(0 != test_cunit_get_n_memevents(pSuite2)); + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pTest2)); + TEST(0 != test_cunit_get_n_memevents(pTest3)); + TEST(0 != test_cunit_get_n_memevents(pTest4)); + + TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); + + cleanup_suite(pSuite1); + + TEST(CU_FALSE == test_exists(pSuite1, "test1")); + TEST(CU_FALSE == test_exists(pSuite1, "test2")); + TEST(CU_FALSE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_TRUE == test_exists(pSuite2, "")); + + cleanup_suite(pSuite2); + + TEST(CU_FALSE == test_exists(pSuite1, "test1")); + TEST(CU_FALSE == test_exists(pSuite1, "test2")); + TEST(CU_FALSE == test_exists(pSuite1, "test3")); + TEST(CU_FALSE == test_exists(pSuite1, "test4")); + TEST(CU_FALSE == test_exists(pSuite1, "")); + TEST(CU_FALSE == test_exists(pSuite2, "test1")); + TEST(CU_FALSE == test_exists(pSuite2, "test2")); + TEST(CU_FALSE == test_exists(pSuite2, "test3")); + TEST(CU_FALSE == test_exists(pSuite2, "test4")); + TEST(CU_FALSE == test_exists(pSuite2, "")); + + CU_FREE(pSuite1); + CU_FREE(pSuite2); + + TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); + TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); + TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); + TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); +} + +/*--------------------------------------------------*/ +static void test_cleanup_test(void) +{ + char* pName; + CU_pTest pTest1 = create_test("test1", NULL); + + TEST_FATAL(NULL != pTest1); + + pName = pTest1->pName; + TEST(0 != test_cunit_get_n_memevents(pTest1)); + TEST(0 != test_cunit_get_n_memevents(pName)); + + TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pName) != test_cunit_get_n_deallocations(pName)); + + cleanup_test(pTest1); + CU_FREE(pTest1); + + TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); + TEST(test_cunit_get_n_allocations(pName) == test_cunit_get_n_deallocations(pName)); +} + +/*--------------------------------------------------*/ +static void group_A_case_1(void) +{ + CU_ASSERT_TRUE(1); +} + +static void group_A_case_2(void) +{ + CU_ASSERT_TRUE(2); +} + +static void group_B_case_1(void) +{ + CU_ASSERT_FALSE(1); +} + +static void group_B_case_2(void) +{ + CU_ASSERT_FALSE(2); +} + +static CU_TestInfo group_A_test_cases[] = { + { "1", group_A_case_1 }, + { "2", group_A_case_2 }, + CU_TEST_INFO_NULL, +}; + +static CU_TestInfo group_B_test_cases[] = { + { "1", group_B_case_1 }, + { "2", group_B_case_2 }, + CU_TEST_INFO_NULL, +}; + +static CU_TestInfo group_C_test_cases[] = { + { "1", group_B_case_1 }, + { "1", group_B_case_2 }, /* duplicate test name */ + CU_TEST_INFO_NULL, +}; + +static CU_SuiteInfo suites0[] = { + CU_SUITE_INFO_NULL, +}; + +static CU_SuiteInfo suites1[] = { + { "A1", NULL, NULL, group_A_test_cases }, + { "B1", NULL, NULL, group_B_test_cases }, + CU_SUITE_INFO_NULL, +}; + +static CU_SuiteInfo suites2[] = { + { "A2", NULL, NULL, group_A_test_cases }, + { "B2", NULL, NULL, group_B_test_cases }, + CU_SUITE_INFO_NULL, +}; + +static CU_SuiteInfo suites3[] = { + { "A3", NULL, NULL, group_A_test_cases }, + { "A3", NULL, NULL, group_C_test_cases }, /* duplicate suite name */ + CU_SUITE_INFO_NULL, +}; + +static void test_register_suite(void) +{ + CU_pTestRegistry pReg = NULL; + CU_ErrorCode status; + + if (CU_initialize_registry()) { + fprintf(stderr, "\nError initializing registry in test_register_suite()."); + return; + } + + pReg = CU_get_registry(); + + /* test initial condition */ + TEST_FATAL(NULL != pReg); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with NULL */ + status = CU_register_suites(NULL); + TEST(CUE_SUCCESS == status); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with empty array */ + status = CU_register_suites(suites0); + TEST(CUE_SUCCESS == status); + TEST(0 == pReg->uiNumberOfSuites); + TEST(0 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with ok array */ + status = CU_register_suites(suites1); + TEST(CUE_SUCCESS == status); + TEST(2 == pReg->uiNumberOfSuites); + TEST(4 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with duplicate suite name */ + status = CU_register_suites(suites1); + TEST(CUE_SUCCESS == status); /* shaky - depends on order of operation in CU_register_suites() */ + TEST(4 == pReg->uiNumberOfSuites); + TEST(8 == pReg->uiNumberOfTests); + + /* test CU_register_suites() with duplicate test name */ + status = CU_register_suites(suites3); + TEST(CUE_DUP_TEST == status); /* shaky - depends on order of operation in CU_register_suites() */ + TEST(6 == pReg->uiNumberOfSuites); + TEST(12 == pReg->uiNumberOfTests); + + CU_cleanup_registry(); + + if (CU_initialize_registry()) { + fprintf(stderr, "\nError initializing registry in test_register_suite()."); + return; + } + + pReg = CU_get_registry(); + + /* test CU_register_nsuites() with ok arrays */ + status = CU_register_nsuites(2, suites1, suites2); + TEST(CUE_SUCCESS == status); + TEST(4 == pReg->uiNumberOfSuites); + TEST(8 == pReg->uiNumberOfTests); +} + +/*--------------------------------------------------*/ +void test_cunit_TestDB(void) +{ + test_cunit_start_tests("TestDB.c"); + + test_CU_initialize_registry(); + test_CU_cleanup_registry(); + test_CU_add_suite(); + test_CU_set_suite_attributes(); + test_get_suite_functions(); + test_CU_add_test(); + test_CU_set_test_attributes(); + test_get_test_functions(); + test_CU_get_registry(); + test_CU_set_registry(); + test_CU_create_new_registry(); + test_cleanup_test_registry(); + test_create_suite(); + test_insert_suite(); + test_create_test(); + test_cleanup_test(); + test_insert_test(); + test_register_suite(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestDB.h b/gearsyncd/tests/cunit/TestDB.h new file mode 100644 index 0000000000..ec07f921c1 --- /dev/null +++ b/gearsyncd/tests/cunit/TestDB.h @@ -0,0 +1,914 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Contains all the Type Definitions and functions declarations + * for the CUnit test database maintenance. + * + * Aug 2001 Initial implementation. (AK) + * + * 09/Aug/2001 Added Preprocessor conditionals for the file. (AK) + * + * 24/aug/2001 Made the linked list from SLL to DLL(doubly linked list). (AK) + * + * 31-Aug-2004 Restructured to eliminate global variables error_number, + * g_pTestRegistry; new interface, support for deprecated + * version 1 interface, moved error handling code to + * CUError.[ch], moved test run counts and _TestResult out + * of TestRegistry to TestRun.h. (JDS) + * + * 01-Sep-2004 Added jmp_buf to CU_Test. (JDS) + * + * 05-Sep-2004 Added internal test interface. (JDS) + * + * 15-Apr-2006 Removed constraint that suites/tests be uniquely named. + * Added ability to turn individual tests/suites on or off. + * Moved doxygen comments for public API here to header. (JDS) + */ + +/** @file + * Management functions for tests, suites, and the test registry. + * Unit testing in CUnit follows the common structure of unit + * tests aggregated in suites, which are themselves aggregated + * in a test registry. This module provides functions and + * typedef's to support the creation, registration, and manipulation + * of test cases, suites, and the registry. + */ +/** @addtogroup Framework + * @{ + */ + +#ifndef CUNIT_TESTDB_H_SEEN +#define CUNIT_TESTDB_H_SEEN + +#include /* jmp_buf */ + +#include "CUnit.h" +#include "CUError.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/*================================================================= + * Typedefs and Data Structures + *=================================================================*/ + +typedef int (*CU_InitializeFunc)(void); /**< Signature for suite initialization function. */ +typedef int (*CU_CleanupFunc)(void); /**< Signature for suite cleanup function. */ +typedef void (*CU_TestFunc)(void); /**< Signature for a testing function in a test case. */ + +/*----------------------------------------------------------------- + * CU_Test, CU_pTest + *-----------------------------------------------------------------*/ +/** CUnit test case data type. + * CU_Test is a double linked list of unit tests. Each test has + * a name, a callable test function, and a flag for whether the + * test is active and thus executed during a test run. A test + * also holds links to the next and previous tests in the list, + * as well as a jmp_buf reference for use in implementing fatal + * assertions.

+ * + * Generally, the linked list includes tests which are associated + * with each other in a CU_Suite. As a result, tests are run in + * the order in which they are added to a suite (see CU_add_test()). + *

+ * + * It is recommended that the name of each CU_Test in a suite have + * a unique name. Otherwise, only the first-registered test having + * a given name will be accessible by that name. There are no + * restrictions on the test function. This means that the same + * function could, in principle, be called more than once from + * different tests. + * + * @see CU_Suite + * @see CU_TestRegistry + */ +typedef struct CU_Test +{ + char* pName; /**< Test name. */ + CU_BOOL fActive; /**< Flag for whether test is executed during a run. */ + CU_TestFunc pTestFunc; /**< Pointer to the test function. */ + jmp_buf* pJumpBuf; /**< Jump buffer for setjmp/longjmp test abort mechanism. */ + + struct CU_Test* pNext; /**< Pointer to the next test in linked list. */ + struct CU_Test* pPrev; /**< Pointer to the previous test in linked list. */ + +} CU_Test; +typedef CU_Test* CU_pTest; /**< Pointer to a CUnit test case. */ + +/*----------------------------------------------------------------- + * CU_Suite, CU_pSuite + *-----------------------------------------------------------------*/ +/** CUnit suite data type. + * CU_Suite is a linked list of CU_Test containers. Each suite has + * a name, a count of registered unit tests, and a flag for whether + * the suite is active during test runs. It also holds pointers to + * optional initialization and cleanup functions. If non-NULL, these + * are called before and after running the suite's tests, respectively. + * In addition, the suite holds a pointer to the head of the linked + * list of associated CU_Test objects. Finally, pointers to the next + * and previous suites in the linked list are maintained.

+ * + * Generally, the linked list includes suites which are associated with + * each other in a CU_TestRegistry. As a result, suites are run in the + * order in which they are registered (see CU_add_suite()).

+ * + * It is recommended that name of each CU_Suite in a test registry have + * a unique name. Otherwise, only the first-registered suite having a + * given name will be accessible by name. There are no restrictions on + * the contained tests. This means that the same CU_Test could, in + * principle, be run more than once fron different suites. + * + * @see CU_Test + * @see CU_TestRegistry + */ +typedef struct CU_Suite +{ + char* pName; /**< Suite name. */ + CU_BOOL fActive; /**< Flag for whether suite is executed during a run. */ + CU_pTest pTest; /**< Pointer to the 1st test in the suite. */ + CU_InitializeFunc pInitializeFunc; /**< Pointer to the suite initialization function. */ + CU_CleanupFunc pCleanupFunc; /**< Pointer to the suite cleanup function. */ + + unsigned int uiNumberOfTests; /**< Number of tests in the suite. */ + struct CU_Suite* pNext; /**< Pointer to the next suite in linked list. */ + struct CU_Suite* pPrev; /**< Pointer to the previous suite in linked list. */ + +} CU_Suite; +typedef CU_Suite* CU_pSuite; /**< Pointer to a CUnit suite. */ + +/*----------------------------------------------------------------- + * CU_TestRegistry, CU_pTestRegistry + *-----------------------------------------------------------------*/ +/** CUnit test registry data type. + * CU_TestRegisty is the repository for suites containing unit tests. + * The test registry maintains a count of the number of CU_Suite + * objects contained in the registry, as well as a count of the total + * number of CU_Test objects associated with those suites. It also + * holds a pointer to the head of the linked list of CU_Suite objects. + *

+ * + * With this structure, the user will normally add suites implictly to + * the internal test registry using CU_add_suite(), and then add tests + * to each suite using CU_add_test(). Test runs are then initiated + * using one of the appropriate functions in TestRun.c via one of the + * user interfaces.

+ * + * Automatic creation and destruction of the internal registry and its + * objects is available using CU_initialize_registry() and + * CU_cleanup_registry(), respectively. For internal and testing + * purposes, the internal registry can be retrieved and assigned. + * Functions are also provided for creating and destroying independent + * registries.

+ * + * Note that earlier versions of CUnit also contained a pointer to a + * linked list of CU_FailureRecord objects (termed _TestResults). + * This has been removed from theregistry and relocated to TestRun.c. + * + * @see CU_Test + * @see CU_Suite + * @see CU_initialize_registry() + * @see CU_cleanup_registry() + * @see CU_get_registry() + * @see CU_set_registry() + * @see CU_create_new_registry() + * @see CU_destroy_existing_registry() + */ +typedef struct CU_TestRegistry +{ +#ifdef USE_DEPRECATED_CUNIT_NAMES + /** Union to support v1.1-1 member name. */ + union { + unsigned int uiNumberOfSuites; /**< Number of suites in the test registry. */ + unsigned int uiNumberOfGroups; /**< Deprecated (version 1). @deprecated Use uiNumberOfSuites. */ + }; + unsigned int uiNumberOfTests; /**< Number of tests in the test registry. */ + /** Union to support v1.1-1 member name. */ + union { + CU_pSuite pSuite; /**< Pointer to the 1st suite in the test registry. */ + CU_pSuite pGroup; /**< Deprecated (version 1). @deprecated Use pSuite. */ + }; +#else + unsigned int uiNumberOfSuites; /**< Number of registered suites in the registry. */ + unsigned int uiNumberOfTests; /**< Total number of registered tests in the registry. */ + CU_pSuite pSuite; /**< Pointer to the 1st suite in the test registry. */ +#endif +} CU_TestRegistry; +typedef CU_TestRegistry* CU_pTestRegistry; /**< Pointer to a CUnit test registry. */ + +/*================================================================= + * Public interface functions + *=================================================================*/ + +CU_EXPORT +CU_ErrorCode CU_initialize_registry(void); +/**< + * Initializes the framework test registry. + * Any existing registry is freed, including all stored suites + * and associated tests. It is not necessary to explicitly call + * CU_cleanup_registry() before reinitializing the framework. + * The most recent stored test results are also cleared.

+ * + * This function must not be called during a test run (checked + * by assertion) + * + * @return CUE_NOMEMORY if memory for the new registry cannot + * be allocated, CUE_SUCCESS otherwise. + * @see CU_cleanup_registry + * @see CU_get_registry + * @see CU_set_registry + * @see CU_registry_initialized + */ + +CU_EXPORT +void CU_cleanup_registry(void); +/**< + * Clears the test registry. + * The active test registry is freed, including all stored suites + * and associated tests. The most recent stored test results are + * also cleared. After calling this function, CUnit suites cannot + * be added until CU_initialize_registry() or CU_set_registry() is + * called. Further, any pointers to suites or test cases held by + * the user will be invalidated by calling this function.

+ * + * This function may be called multiple times without generating + * an error condition. However, this function must not be + * called during a test run (checked by assertion)

. + * + * @see CU_initialize_registry + * @see CU_get_registry + * @see CU_set_registry + */ + +CU_EXPORT CU_BOOL CU_registry_initialized(void); +/**< + * Checks whether the test registry has been initialized. + * + * @return CU_TRUE if the registry has been initialized, + * CU_FALSE otherwise. + * @see CU_initialize_registry + * @see CU_cleanup_registry + */ + +CU_EXPORT +CU_pSuite CU_add_suite(const char *strName, + CU_InitializeFunc pInit, + CU_CleanupFunc pClean); +/**< + * Creates a new test suite and adds it to the test registry. + * This function creates a new test suite having the specified + * name and initialization/cleanup functions and adds it to the + * test registry. The new suite will be active and able to be + * executed during a test run. The test registry must be + * initialized before calling this function (checked by assertion). + * pInit and pClean may be NULL, in which case no corresponding + * initialization of cleanup function will be called when the suite + * is run. strName may be empty ("") but may not be NULL.

+ * + * The return value is a pointer to the newly-created suite, or + * NULL if there was a problem with the suite creation or addition. + * An error code is also set for the framework. Note that if the + * name specified for the new suite is a duplicate, the suite will + * be created and added but the error code will be set to CUE_DUP_SUITE. + * The duplicate suite will not be accessible by name.

+ * + * NOTE - the CU_pSuite pointer returned should NOT BE FREED BY + * THE USER. The suite is freed by the CUnit system when + * CU_cleanup_registry() is called. This function must not + * be called during a test run (checked by assertion).

+ * + * CU_add_suite() sets the following error codes: + * - CUE_SUCCESS if no errors occurred. + * - CUE_NOREGISTRY if the registry has not been initialized. + * - CUE_NO_SUITENAME if strName is NULL. + * - CUE_DUP_SUITE if a suite having strName is already registered. + * - CUE_NOMEMORY if a memory allocation failed. + * + * @param strName Name for the new test suite (non-NULL). + * @param pInit Initialization function to call before running suite. + * @param pClean Cleanup function to call after running suite. + * @return A pointer to the newly-created suite (NULL if creation failed) + */ + +CU_EXPORT +CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive); +/**< + * Activates or deactivates a suite. + * Only activated suites can be executed during a test run. + * By default a suite is active upon creation, but can be deactivated + * by passing it along with CU_FALSE to this function. The suite + * can be reactivated by passing it along with CU_TRUE. The current + * value of the active flag is available as pSuite->fActive. If pSuite + * is NULL then error code CUE_NOSUITE is returned. + * + * @param pSuite Pointer to the suite to modify (non-NULL). + * @param fNewActive If CU_TRUE then the suite will be activated; + * if CU_FALSE it will be deactivated. + * @return Returns CUE_NOSUITE if pSuite is NULL, CUE_SUCCESS if all is well. + */ + +CU_EXPORT +CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName); +/**< + * Modifies the name of a suite. + * This function allows the name associated with a suite to + * be changed. It is not recommended that a suite name be changed, + * nor should it be necessary under most circumstances. However, + * this function is provided for those clients who need to change + * a suite's name. The current value of the suite's name + * is available as pSuite->pName. CUE_SUCCESS is returned if the + * function succeeds in changing the name. CUE_NOSUITE is returned if + * pSuite is NULL, and CUE_NO_SUITENAME if strNewName is NULL. + * + * @param pSuite Pointer to the suite to modify (non-NULL). + * @param strNewName Pointer to string containing new suite name (non-NULL). + * @return Returns CUE_NOSUITE if pSuite is NULL, CUE_NO_SUITENAME if + * strNewName is NULL, and CUE_SUCCESS if all is well. + */ + +CU_EXPORT +CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit); +/**< + * Modifies the initialization function of a suite. + * This function allows the initialization function associated with + * a suite to be changed. This is neither recommended nor should it + * be necessary under most circumstances. However, this function is + * provided for those clients who need to change the function. The + * current value of the function is available as pSuite->pInitializeFunc. + * CUE_SUCCESS is returned if the function succeeds, or CUE_NOSUITE if + * pSuite is NULL. pNewInit may be NULL, which indicates the suite has + * no initialization function. + * + * @param pSuite Pointer to the suite to modify (non-NULL). + * @param pNewInit Pointer to function to use to initialize suite. + * @return Returns CUE_NOSUITE if pSuite is NULL, and CUE_SUCCESS if + * all is well. + */ + +CU_EXPORT +CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean); +/**< + * Modifies the cleanup function of a suite. + * This function allows the cleanup function associated with a suite to + * be changed. This is neither recommended nor should it be necessary + * under most circumstances. However, this function is provided for those + * clients who need to change the function. The current value of the + * function is available as pSuite->pCleanupFunc. CUE_SUCCESS is returned + * if the function succeeds, or CUE_NOSUITE if pSuite is NULL. pNewClean + * may be NULL, which indicates the suite has no cleanup function. + * + * @param pSuite Pointer to the suite to modify (non-NULL). + * @param pNewClean Pointer to function to use to clean up suite. + * @return Returns CUE_NOSUITE if pSuite is NULL, and CUE_SUCCESS if + * all is well. + */ + +CU_EXPORT +CU_pSuite CU_get_suite(const char* strName); +/**< + * Retrieves the suite having the specified name. + * Searches the active test registry and returns a pointer to the 1st + * suite found. NULL is returned if no suite having the specified name + * is found. In addition, the framework error state is set to CUE_NOREGISTRY + * if the registry is not initialized or to CUE_NO_SUITENAME if strName is NULL. + * If the return value is NULL and framework error state is CUE_SUCCESS, then + * the search simply failed to find the specified name. + * Use CU_get_suite_at_pos() to retrieve a suite by position rather than name. + * + * @param strName The name of the suite to search for (non-NULL). + * @return Returns a pointer to the suite, or NULL if not found or an error occurred. + * @see CU_get_suite_at_pos() + */ + +CU_EXPORT +CU_pSuite CU_get_suite_at_pos(unsigned int pos); +/**< + * Retrieves the suite at the specified position. + * Iterates the active test registry and returns a pointer to the suite at + * position pos. pos is a 1-based index having valid values + * [1 .. CU_get_registry()->uiNumberOfSuites] and corresponds to the order in + * which suites were registered. If pos is invalid or an error occurs, 0 is + * returned. In addition, the framework error state is set to CUE_NOREGISTRY if + * the registry is not initialized, or CUE_SUCCESS if pos was invalid. Use + * CU_get_suite() to retrieve a suite by name rather than position. + * + * @param pos The 1-based position of the suite to fetch. + * @return Returns a pointer to the suite, or 0 if not found or an error occurred. + * @see CU_get_suite() + */ + +CU_EXPORT +unsigned int CU_get_suite_pos(CU_pSuite pSuite); +/**< + * Looks up the position of the specified suite. + * The position is a 1-based index of suites in the active test registry which + * corresponds to the order in which suites were registered. If pSuite is not + * found or an error occurs, 0 is returned. In addition, the framework error + * state is set to CUE_NOREGISTRY if the registry is not initialized, or + * CUE_NOSUITE if pSuite is NULL. The returned position may be used to retrieve + * the suite using CU_get_suite_by_pos(). + * + * @param pSuite Pointer to the suite to find (non-NULL). + * @return Returns the 1-based position of pSuite in the registry, or NULL if + * not found or an error occurred. + * @see CU_get_suite_by_pos() + * @see CU_get_suite_pos_by_name() + */ + +CU_EXPORT +unsigned int CU_get_suite_pos_by_name(const char* strName); +/**< + * Looks up the position of the suite having the specified name. + * The position is a 1-based index of suites in the active test registry which + * corresponds to the order in which suites were registered. If no suite has the + * specified name or an error occurs, 0 is returned. In addition, the framework error + * state is set to CUE_NOREGISTRY if the registry is not initialized, or + * CUE_NO_SUITENAME if strName is NULL. The search ends at the 1st suite found having + * name strName. The returned position may be used to retrieve the suite using + * CU_get_suite_by_pos(). + * + * @param strName Name of the suite to find (non-NULL). + * @return Returns the 1-based position of pSuite in the registry, or NULL if + * not found or an error occurred. + * @see CU_get_suite_by_pos() + * @see CU_get_suite_pos_by_name() + */ + +CU_EXPORT +CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc); +/**< + * This function creates a new test having the specified name + * and function, and adds it to the specified suite. The new test + * is active and able to be executed during a test run. At present, + * there is no mechanism for creating a test case independent of a + * suite. Neither pSuite, strName, nor pTestFunc may be NULL. + * + * The return value is a pointer to the newly-created test, or + * NULL if there was a problem with the test creation or addition. + * An error code is also set for the framework. Note that if the + * name specified for the new test is a duplicate within pSuite, + * the test will be created and added but the error code will be + * set to CUE_DUP_TEST. The duplicate test will not be accessible + * by name.

+ * + * NOTE - the CU_pTest pointer returned should NOT BE FREED BY + * THE USER. The test is freed by the CUnit system when + * CU_cleanup_registry() is called. This function must not + * be called during a test run (checked by assertion).

+ + * CU_add_test() sets the following error codes: + * - CUE_SUCCESS if no errors occurred. + * - CUE_NOREGISTRY if the registry has not been initialized. + * - CUE_NOSUITE if pSuite is NULL. + * - CUE_NO_TESTNAME if strName is NULL. + * - CUE_NOTEST if pTestFunc is NULL. + * - CUE_DUP_TEST if a test having strName is already registered to pSuite. + * - CUE_NOMEMORY if a memory allocation failed.

+ * + * @param pSuite Test suite to which to add new test (non-NULL). + * @param strName Name for the new test case (non-NULL). + * @param pTest Function to call when running the test (non-NULL). + * @return A pointer to the newly-created test (NULL if creation failed) + */ + +CU_EXPORT +CU_ErrorCode CU_set_test_active(CU_pTest pTest, CU_BOOL fNewActive); +/**< + * Activates or deactivates a specific test. + * Only activated tests can be executed during a test run. + * By default a test is active upon creation, but can be deactvated + * by passing it along with CU_FALSE to this function. The test + * can be reactivated by passing it along with CU_TRUE. + * The current value of the active flag is available as pTest->fActive. + * If pTest is NULL then error code CUE_NOTEST is returned. Otherwise + * CUE_SUCCESS is returned. + * + * @param pTest Pointer to the test to modify (non-NULL). + * @param fNewActive If CU_TRUE then test will be activated; + * if CU_FALSE it will be deactivated. + * @return Returns CUE_NOTEST if pTest is NULL, CUE_SUCCESS if all is well. +*/ + +CU_EXPORT +CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName); +/**< + * Modifies the name of a test. + * This function allows the name associated with a test to + * be changed. It is not recommended that a test name be changed, + * nor should it be necessary under most circumstances. However, + * this function is provided for those clients who need to change + * a test's name. The current value of the test's name is + * available as pTest->pName. CUE_SUCCESS is returned if the + * function succeeds in changing the name. CUE_NOTEST is returned if + * pTest is NULL, and CUE_NO_TESTNAME if strNewName is NULL. + * + * @param pTest Pointer to the test to modify (non-NULL). + * @param strNewName Pointer to string containing new test name (non-NULL). + * @return Returns CUE_NOTEST if pTest is NULL, CUE_NO_TESTNAME if + * strNewName is NULL, and CUE_SUCCESS if all is well. + */ + +CU_EXPORT +CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc); +/**< + * Modifies the test function of a test. + * This function allows the test function associated with a test to be + * changed. This is neither recommended nor should it be necessary under + * most circumstances. However, this function is provided for those + * clients who need to change the test function. The current value of + * the test function is available as pTest->pTestFunc. CUE_SUCCESS is + * returned if the function succeeds, or CUE_NOTEST if either pTest or + * pNewFunc is NULL. + * + * @param pTest Pointer to the test to modify (non-NULL). + * @param pNewFunc Pointer to function to use for test function (non-NULL). + * @return Returns CUE_NOTEST if pTest or pNewFunc is NULL, and CUE_SUCCESS + * if all is well. + */ + +CU_EXPORT +CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName); +/**< + * Retrieves the test having the specified name. + * Searches pSuite and returns a pointer to the 1st test found named strName. + * NULL is returned if no test having the specified name is found in pSuite. + * In addition, the framework error state is set as follows: + * - CUE_NOREGISTRY if the registry is not initialized + * - CUE_NOSUITE if pSuite is NULL + * - CUE_NO_TESTNAME if strName is NULL. + * + * If the return value is NULL and framework error state is CUE_SUCCESS, then + * the search simply failed to find the specified name. Use CU_get_test_at_pos() + * to retrieve a test by position rather than name. + * + * @param pSuite Pointer to the suite to search (non-NULL). + * @param strName The name of the test to search for (non-NULL). + * @return Returns a pointer to the test, or NULL if not found or an error occurred. + * @see CU_get_test_at_pos() + */ + +CU_EXPORT +CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos); +/**< + * Retrieves the test at the specified position in pSuite. + * Iterates the tests registered in pSuite and returns a pointer to the + * test at position pos. pos is a 1-based index having valid values + * [1 .. pSuite->uiNumberOfTests] and corresponds to the order in + * which tests were added to pSuite. If pos is invalid or an error occurs, 0 is + * returned. In addition, the framework error state is set as follows: + * - CUE_NOREGISTRY if the registry is not initialized + * - CUE_NOSUITE if pSuite is NULL + * Use CU_get_test() to retrieve a test by name rather than position. + * + * @param pSuite Pointer to the suite to search (non-NULL). + * @param pos The 1-based position of the test to fetch. + * @return Returns a pointer to the test, or 0 if not found or an error occurred. + * @see CU_get_test() + */ + +CU_EXPORT +unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest); +/**< + * Looks up the position of the specified test in pSuite. + * The position is a 1-based index of tests in pSuite which corresponds to the + * order in which tests were added. If pTest is not found or an error occurs, + * 0 is returned. In addition, the framework error state is set as follows: + * - CUE_NOREGISTRY if the registry is not initialized + * - CUE_NOSUITE if pSuite is NULL + * - CUE_NOTEST if pTest is NULL + * + * The returned position may be used to retrieve the test using CU_get_test_by_pos(). + * + * @param pSuite Pointer to the suite to search (non-NULL). + * @param pTest Pointer to the test to find (non-NULL). + * @return Returns the 1-based position of pTest in pSuite, or NULL if + * not found or an error occurred. + * @see CU_get_test_by_pos() + * @see CU_get_test_pos_by_name() + */ + +CU_EXPORT +unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName); +/**< + * Looks up the position of the test having the specified name in pSuite. + * The position is a 1-based index of tests in pSuite which corresponds to the order + * in which tests were added. If no test has the specified name or an error occurs, + * 0 is returned. In addition, the framework error state is set as follows: + * - CUE_NOREGISTRY if the registry is not initialized + * - CUE_NOSUITE if pSuite is NULL + * - CUE_NO_TESTNAME if strName is NULL + * The search ends at the 1st test found having name strName. The returned position + * may be used to retrieve the suite using CU_get_test_by_pos(). + * + * @param pSuite Pointer to the suite to search (non-NULL). + * @param strName Name of the test to find (non-NULL). + * @return Returns the 1-based position of pTest in pSuite, or NULL if + * not found or an error occurred. + * @see CU_get_test_by_pos() + * @see CU_get_test_pos_by_name() + */ + +#define CU_ADD_TEST(suite, test) (CU_add_test(suite, #test, (CU_TestFunc)test)) +/**< Shortcut macro for adding a test to a suite. */ + +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ +/* This section is based conceptually on code + * Copyright (C) 2004 Aurema Pty Ltd. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Derived from code contributed by K. Cheung and Aurema Pty Ltd. (thanks!) + * test_case_t, test_group_t, test_suite_t + */ + +/** + * Test case parameters structure. + * This data type is provided to assist CUnit users manage collections of + * tests and suites. It is intended to be used to build arrays of test case + * parameters that can be then be referred to in a CU_suite_info_t variable. + */ +typedef struct CU_TestInfo { + char *pName; /**< Test name. */ + CU_TestFunc pTestFunc; /**< Test function. */ +} CU_TestInfo; +typedef CU_TestInfo* CU_pTestInfo; /**< Pointer to CU_TestInfo type. */ + +/** + * Suite parameters. + * This data type is provided to assist CUnit users manage collections of + * tests and suites. It is intended to be used to build arrays of suite + * parameters that can be passed to a bulk registration function such as + * CU_register_suite() or CU_register_suites(). + */ +typedef struct CU_SuiteInfo { + char *pName; /**< Suite name. */ + CU_InitializeFunc pInitFunc; /**< Suite initialization function. */ + CU_CleanupFunc pCleanupFunc; /**< Suite cleanup function */ + CU_TestInfo *pTests; /**< Test case array - must be NULL terminated. */ +} CU_SuiteInfo; +typedef CU_SuiteInfo* CU_pSuiteInfo; /**< Pointer to CU_SuiteInfo type. */ + +#define CU_TEST_INFO_NULL { NULL, NULL } +/**< NULL CU_test_info_t to terminate arrays of tests. */ +#define CU_SUITE_INFO_NULL { NULL, NULL, NULL, NULL } +/**< NULL CU_suite_info_t to terminate arrays of suites. */ + + +CU_EXPORT CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]); +/**< + * Registers the suites in a single CU_SuiteInfo array. + * Multiple arrays can be registered using CU_register_nsuites(). + * + * @param suite_info NULL-terminated array of CU_SuiteInfo items to register. + * @return A CU_ErrorCode indicating the error status. + * @see CU_register_suites() + */ +CU_EXPORT CU_ErrorCode CU_register_nsuites(int suite_count, ...); +/**< + * Registers multiple suite arrays in CU_SuiteInfo format. + * The function accepts a variable number of suite arrays to be registered. + * The number of arrays is indicated by the value of the 1st argument, + * suite_count. Each suite in each array is registered with the CUnit test + * registry, along with all of the associated tests. + * + * @param suite_count The number of CU_SuiteInfo* arguments to follow. + * @param ... suite_count number of CU_SuiteInfo* arguments. NULLs are ignored. + * @return A CU_ErrorCode indicating the error status. + * @see CU_register_suites() + */ + +#ifdef USE_DEPRECATED_CUNIT_NAMES +typedef CU_TestInfo test_case_t; /**< Deprecated (version 1). @deprecated Use CU_TestInfo. */ +typedef CU_SuiteInfo test_group_t; /**< Deprecated (version 1). @deprecated Use CU_SuiteInfo. */ + +/** Deprecated (version 1). @deprecated Use CU_SuiteInfo and CU_TestInfo. */ +typedef struct test_suite { + char *name; /**< Suite name. Currently not used. */ + test_group_t *groups; /**< Test groups. This must be a NULL terminated array. */ +} test_suite_t; + +/** Deprecated (version 1). @deprecated Use CU_TEST_INFO_NULL. */ +#define TEST_CASE_NULL { NULL, NULL } +/** Deprecated (version 1). @deprecated Use CU_TEST_GROUP_NULL. */ +#define TEST_GROUP_NULL { NULL, NULL, NULL, NULL } + +/** Deprecated (version 1). @deprecated Use CU_register_suites(). */ +#define test_group_register(tg) CU_register_suites(tg) + +/** Deprecated (version 1). @deprecated Use CU_SuiteInfo and CU_register_suites(). */ +CU_EXPORT int test_suite_register(test_suite_t *ts) +{ + test_group_t *tg; + int error; + + for (tg = ts->groups; tg->pName; tg++) + if ((error = CU_register_suites(tg)) != CUE_SUCCESS) + return error; + + return CUE_SUCCESS; +} +#endif /* USE_DEPRECATED_CUNIT_NAMES */ +/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ + +#ifdef USE_DEPRECATED_CUNIT_NAMES +typedef CU_InitializeFunc InitializeFunc; /**< Deprecated (version 1). @deprecated Use CU_InitializeFunc. */ +typedef CU_CleanupFunc CleanupFunc; /**< Deprecated (version 1). @deprecated Use CU_CleanupFunc. */ +typedef CU_TestFunc TestFunc; /**< Deprecated (version 1). @deprecated Use CU_TestFunc. */ + +typedef CU_Test _TestCase; /**< Deprecated (version 1). @deprecated Use CU_Test. */ +typedef CU_pTest PTestCase; /**< Deprecated (version 1). @deprecated Use CU_pTest. */ + +typedef CU_Suite _TestGroup; /**< Deprecated (version 1). @deprecated Use CU_Suite. */ +typedef CU_pSuite PTestGroup; /**< Deprecated (version 1). @deprecated Use CU_pSuite. */ + +typedef CU_TestRegistry _TestRegistry; /**< Deprecated (version 1). @deprecated Use CU_TestRegistry. */ +typedef CU_pTestRegistry PTestRegistry; /**< Deprecated (version 1). @deprecated Use CU_pTestRegistry. */ + +/* Public interface functions */ +/** Deprecated (version 1). @deprecated Use CU_initialize_registry(). */ +#define initialize_registry() CU_initialize_registry() +/** Deprecated (version 1). @deprecated Use CU_cleanup_registry(). */ +#define cleanup_registry() CU_cleanup_registry() +/** Deprecated (version 1). @deprecated Use CU_add_suite(). */ +#define add_test_group(name, init, clean) CU_add_suite(name, init, clean) +/** Deprecated (version 1). @deprecated Use CU_add_test(). */ +#define add_test_case(group, name, test) CU_add_test(group, name, test) + +/* private internal CUnit testing functions */ +/** Deprecated (version 1). @deprecated Use CU_get_registry(). */ +#define get_registry() CU_get_registry() +/** Deprecated (version 1). @deprecated Use CU_set_registry(). */ +#define set_registry(reg) CU_set_registry((reg)) + +/** Deprecated (version 1). @deprecated Use CU_get_suite_by_name(). */ +#define get_group_by_name(group, reg) CU_get_suite_by_name(group, reg) +/** Deprecated (version 1). @deprecated Use CU_get_test_by_name(). */ +#define get_test_by_name(test, group) CU_get_test_by_name(test, group) + +/** Deprecated (version 1). @deprecated Use ADD_TEST_TO_SUITE. */ +#define ADD_TEST_TO_GROUP(group, test) (CU_add_test(group, #test, (CU_TestFunc)test)) +#endif /* USE_DEPRECATED_CUNIT_NAMES */ + +/*================================================================= + * Internal CUnit system functions. + * Should not be routinely called by users. + *=================================================================*/ + +CU_EXPORT CU_pTestRegistry CU_get_registry(void); +/**< + * Retrieves a pointer to the current test registry. + * Returns NULL if the registry has not been initialized using + * CU_initialize_registry(). Directly accessing the registry + * should not be necessary for most users. This function is + * provided primarily for internal and testing purposes. + * + * @return A pointer to the current registry (NULL if uninitialized). + * @see CU_initialize_registry + * @see CU_set_registry + */ + +CU_EXPORT CU_pTestRegistry CU_set_registry(CU_pTestRegistry pTestRegistry); +/**< + * Sets the registry to an existing CU_pTestRegistry instance. + * A pointer to the original registry is returned. Note that the + * original registry is not freed, and it becomes the caller's + * responsibility to do so. Directly accessing the registry + * should not be necessary for most users. This function is + * provided primarily for internal and testing purposes.

+ * + * This function must not be called during a test run (checked + * by assertion). + * + * @return A pointer to the original registry that was replaced. + * @see CU_initialize_registry + * @see CU_cleanup_registry + * @see CU_get_registry + */ + +CU_EXPORT CU_pTestRegistry CU_create_new_registry(void); +/**< + * Creates and initializes a new test registry. + * Returns a pointer to a new, initialized registry (NULL if memory could + * not be allocated). It is the caller's responsibility to destroy and free + * the new registry (unless it is made the active test registry using + * CU_set_registry()). + */ + +CU_EXPORT +void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry); +/**< + * Destroys and frees all memory for an existing test registry. + * The active test registry is destroyed by the CUnit system in + * CU_cleanup_registry(), so only call this function on registries created + * or held independently of the internal CUnit system.

+ * + * Once a registry is made the active test registry using CU_set_registry(), + * its destruction will be handled by the framework. ppRegistry may not be + * NULL (checked by assertion), but *ppRegistry can be NULL (in which case the + * function has no effect). Note that *ppRegistry will be set to NULL on return. + * + * @param ppRegistry Address of a pointer to the registry to destroy (non-NULL). + */ + +CU_EXPORT +CU_pSuite CU_get_suite_by_name(const char *szSuiteName, CU_pTestRegistry pRegistry); +/**< + * Retrieves a pointer to the suite having the specified name. + * Scans the pRegistry and returns a pointer to the first suite located + * having the specified name. Neither szSuiteName nor pRegistry may be + * NULL (checked by assertion). Clients should normally use CU_get_suite() + * instead, which automatically searches the active test registry. + * + * @param szSuiteName The name of the suite to locate (non-NULL). + * @param pRegistry The registry to scan (non-NULL). + * @return Pointer to the first suite having the specified name, + * NULL if not found. + * @see CU_get_suite() + */ + +CU_EXPORT +CU_pSuite CU_get_suite_by_index(unsigned int index, CU_pTestRegistry pRegistry); +/**< + * Retrieves a pointer to the suite at the specified (1-based) index. + * Iterates pRegistry and returns a pointer to the suite located at the + * specified index. pRegistry may not be NULL (checked by assertion). + * Clients should normally use CU_get_suite_at_pos() instead, which + * automatically searches the active test registry. + * + * @param index The 1-based index of the suite to find. + * @param pRegistry The registry to scan (non-NULL). + * @return Pointer to the suite at the specified index, or + * NULL if index is invalid. + * @see CU_get_suite_at_pos() + */ + +CU_EXPORT +CU_pTest CU_get_test_by_name(const char* szTestName, CU_pSuite pSuite); +/**< + * Retrieves a pointer to the test case in pSuite having the specified name. + * The first test case in pSuite having the specified name is returned, or + * NULL if not found. Neither szSuiteName nor pSuite may be NULL (checked + * by assertion). Clients should normally use CU_get_test() instead. + * + * @param szTestName The name of the test case to locate (non-NULL). + * @param pSuite The suite to scan (non-NULL). + * @return Pointer to the first test case having the specified name, + * NULL if not found. + * @see CU_get_test() + */ + +CU_EXPORT +CU_pTest CU_get_test_by_index(unsigned int index, CU_pSuite pSuite); +/**< + * Retrieves a pointer to the test at the specified (1-based) index. + * Iterates pSuite and returns a pointer to the test located at the + * specified index. pSuite may not be NULL (checked by assertion). + * Clients should normally use CU_get_test_at_pos() instead, which + * automatically searches the active test registry. + * + * @param index The 1-based index of the test to find. + * @param pRegistry The registry to scan (non-NULL). + * @return Pointer to the test at the specified index, or + * NULL if index is invalid. + * @see CU_get_test_at_pos() + */ + +#ifdef CUNIT_BUILD_TESTS +void test_cunit_TestDB(void); +#endif + +#ifdef __cplusplus +} +#endif +#endif /* CUNIT_TESTDB_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/cunit/TestRun.c b/gearsyncd/tests/cunit/TestRun.c new file mode 100644 index 0000000000..efd88e48fc --- /dev/null +++ b/gearsyncd/tests/cunit/TestRun.c @@ -0,0 +1,2923 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of Test Run Interface. + * + * Aug 2001 Initial implementaion (AK) + * + * 19/Aug/2001 Added initial registry/Suite/test framework implementation. (AK) + * + * 24/Aug/2001 Changed Data structure from SLL to DLL for all linked lists. (AK) + * + * 25/Nov/2001 Added notification for Suite Initialization failure condition. (AK) + * + * 5-Aug-2004 New interface, doxygen comments, moved add_failure on suite + * initialization so called even if a callback is not registered, + * moved CU_assertImplementation into TestRun.c, consolidated + * all run summary info out of CU_TestRegistry into TestRun.c, + * revised counting and reporting of run stats to cleanly + * differentiate suite, test, and assertion failures. (JDS) + * + * 1-Sep-2004 Modified CU_assertImplementation() and run_single_test() for + * setjmp/longjmp mechanism of aborting test runs, add asserts in + * CU_assertImplementation() to trap use outside a registered + * test function during an active test run. (JDS) + * + * 22-Sep-2004 Initial implementation of internal unit tests, added nFailureRecords + * to CU_Run_Summary, added CU_get_n_failure_records(), removed + * requirement for registry to be initialized in order to run + * CU_run_suite() and CU_run_test(). (JDS) + * + * 30-Apr-2005 Added callback for suite cleanup function failure, + * updated unit tests. (JDS) + * + * 23-Apr-2006 Added testing for suite/test deactivation, changing functions. + * Moved doxygen comments for public functions into header. + * Added type marker to CU_FailureRecord. + * Added support for tracking inactive suites/tests. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + * + * 02-Jun-2006 Added support for elapsed time. Added handlers for suite + * start and complete events. Reworked test run routines to + * better support these features, suite/test activation. (JDS) + */ + +/** @file + * Test run management functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include +#include +#if NOT_YET +#include +#else +#include +#endif +#include + +#include "CUnit.h" +#include "MyMem.h" +#include "TestDB.h" +#include "TestRun.h" +#include "Util.h" +#include "CUnit_intl.h" + +#define MAX_CMD_LEN 100 + +//Extern declaration +extern int collect_syslog; + +//Callback fn declaration +typedef void (*cunit_logger_cb)(void *fn); + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +static CU_BOOL f_bTestIsRunning = CU_FALSE; /**< Flag for whether a test run is in progress */ +static CU_pSuite f_pCurSuite = NULL; /**< Pointer to the suite currently being run. */ +static CU_pTest f_pCurTest = NULL; /**< Pointer to the test currently being run. */ + +/** CU_RunSummary to hold results of each test run. */ +static CU_RunSummary f_run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +/** CU_pFailureRecord to hold head of failure record list of each test run. */ +static CU_pFailureRecord f_failure_list = NULL; + +/** CU_pFailureRecord to hold head of failure record list of each test run. */ +static CU_pFailureRecord f_last_failure = NULL; + +/** Flag for whether inactive suites/tests are treated as failures. */ +static CU_BOOL f_failure_on_inactive = CU_TRUE; + +/** Variable for storage of start time for test run. */ +static clock_t f_start_time; + + +/** Pointer to the function to be called before running a suite. */ +static CU_SuiteStartMessageHandler f_pSuiteStartMessageHandler = NULL; + +/** Pointer to the function to be called before running a test. */ +static CU_TestStartMessageHandler f_pTestStartMessageHandler = NULL; + +/** Pointer to the function to be called after running a test. */ +static CU_TestCompleteMessageHandler f_pTestCompleteMessageHandler = NULL; + +/** Pointer to the function to be called after running a suite. */ +static CU_SuiteCompleteMessageHandler f_pSuiteCompleteMessageHandler = NULL; + +/** Pointer to the function to be called when all tests have been run. */ +static CU_AllTestsCompleteMessageHandler f_pAllTestsCompleteMessageHandler = NULL; + +/** Pointer to the function to be called if a suite initialization function returns an error. */ +static CU_SuiteInitFailureMessageHandler f_pSuiteInitFailureMessageHandler = NULL; + +/** Pointer to the function to be called if a suite cleanup function returns an error. */ +static CU_SuiteCleanupFailureMessageHandler f_pSuiteCleanupFailureMessageHandler = NULL; + +/*================================================================= + * Private function forward declarations + *=================================================================*/ +static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure); +static void cleanup_failure_list(CU_pFailureRecord* ppFailure); +static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary); +static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary); +static void add_failure(CU_pFailureRecord* ppFailure, + CU_pRunSummary pRunSummary, + CU_FailureType type, + unsigned int uiLineNumber, + const char *szCondition, + const char *szFileName, + CU_pSuite pSuite, + CU_pTest pTest); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +CU_BOOL CU_assertImplementation(CU_BOOL bValue, + unsigned int uiLine, + const char *strCondition, + const char *strFile, + const char *strFunction, + CU_BOOL bFatal) +{ + /* not used in current implementation - stop compiler warning */ + CU_UNREFERENCED_PARAMETER(strFunction); + + /* these should always be non-NULL (i.e. a test run is in progress) */ + assert(NULL != f_pCurSuite); + assert(NULL != f_pCurTest); + + ++f_run_summary.nAsserts; + if (CU_FALSE == bValue) { + ++f_run_summary.nAssertsFailed; + add_failure(&f_failure_list, &f_run_summary, CUF_AssertFailed, + uiLine, strCondition, strFile, f_pCurSuite, f_pCurTest); + + if ((CU_TRUE == bFatal) && (NULL != f_pCurTest->pJumpBuf)) { + longjmp(*(f_pCurTest->pJumpBuf), 1); + } + } + + return bValue; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartHandler) +{ + f_pSuiteStartMessageHandler = pSuiteStartHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartHandler) +{ + f_pTestStartMessageHandler = pTestStartHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteHandler) +{ + f_pTestCompleteMessageHandler = pTestCompleteHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteHandler) +{ + f_pSuiteCompleteMessageHandler = pSuiteCompleteHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteHandler) +{ + f_pAllTestsCompleteMessageHandler = pAllTestsCompleteHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureHandler) +{ + f_pSuiteInitFailureMessageHandler = pSuiteInitFailureHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureHandler) +{ + f_pSuiteCleanupFailureMessageHandler = pSuiteCleanupFailureHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteStartMessageHandler CU_get_suite_start_handler(void) +{ + return f_pSuiteStartMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_TestStartMessageHandler CU_get_test_start_handler(void) +{ + return f_pTestStartMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_TestCompleteMessageHandler CU_get_test_complete_handler(void) +{ + return f_pTestCompleteMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteCompleteMessageHandler CU_get_suite_complete_handler(void) +{ + return f_pSuiteCompleteMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_AllTestsCompleteMessageHandler CU_get_all_test_complete_handler(void) +{ + return f_pAllTestsCompleteMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteInitFailureMessageHandler CU_get_suite_init_failure_handler(void) +{ + return f_pSuiteInitFailureMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteCleanupFailureMessageHandler CU_get_suite_cleanup_failure_handler(void) +{ + return f_pSuiteCleanupFailureMessageHandler; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_suites_run(void) +{ + return f_run_summary.nSuitesRun; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_suites_failed(void) +{ + return f_run_summary.nSuitesFailed; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_suites_inactive(void) +{ + return f_run_summary.nSuitesInactive; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_tests_run(void) +{ + return f_run_summary.nTestsRun; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_tests_failed(void) +{ + return f_run_summary.nTestsFailed; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_tests_inactive(void) +{ + return f_run_summary.nTestsInactive; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_asserts(void) +{ + return f_run_summary.nAsserts; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_successes(void) +{ + return (f_run_summary.nAsserts - f_run_summary.nAssertsFailed); +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_failures(void) +{ + return f_run_summary.nAssertsFailed; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_failure_records(void) +{ + return f_run_summary.nFailureRecords; +} + +/*------------------------------------------------------------------------*/ +double CU_get_elapsed_time(void) +{ + if (CU_TRUE == f_bTestIsRunning) { + return ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + } + else { + return f_run_summary.ElapsedTime; + } +} + +/*------------------------------------------------------------------------*/ +CU_pFailureRecord CU_get_failure_list(void) +{ + return f_failure_list; +} + +/*------------------------------------------------------------------------*/ +CU_pRunSummary CU_get_run_summary(void) +{ + return &f_run_summary; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_run_all_tests(void) +{ + CU_pTestRegistry pRegistry = CU_get_registry(); + CU_pSuite pSuite = NULL; + CU_ErrorCode result = CUE_SUCCESS; + CU_ErrorCode result2; + + /* Clear results from the previous run */ + clear_previous_results(&f_run_summary, &f_failure_list); + + if (NULL == pRegistry) { + result = CUE_NOREGISTRY; + } + else { + /* test run is starting - set flag */ + f_bTestIsRunning = CU_TRUE; + f_start_time = clock(); + + pSuite = pRegistry->pSuite; + while ((NULL != pSuite) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { + result2 = run_single_suite(pSuite, &f_run_summary); + result = (CUE_SUCCESS == result) ? result2 : result; /* result = 1st error encountered */ + pSuite = pSuite->pNext; + } + + /* test run is complete - clear flag */ + f_bTestIsRunning = CU_FALSE; + f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + + if (NULL != f_pAllTestsCompleteMessageHandler) { + (*f_pAllTestsCompleteMessageHandler)(f_failure_list); + } + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_run_suite(CU_pSuite pSuite) +{ + CU_ErrorCode result = CUE_SUCCESS; + + /* Clear results from the previous run */ + clear_previous_results(&f_run_summary, &f_failure_list); + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + /* test run is starting - set flag */ + f_bTestIsRunning = CU_TRUE; + f_start_time = clock(); + + result = run_single_suite(pSuite, &f_run_summary); + + /* test run is complete - clear flag */ + f_bTestIsRunning = CU_FALSE; + f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + + /* run handler for overall completion, if any */ + if (NULL != f_pAllTestsCompleteMessageHandler) { + (*f_pAllTestsCompleteMessageHandler)(f_failure_list); + } + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest) +{ + CU_ErrorCode result = CUE_SUCCESS; + CU_ErrorCode result2; + + /* Clear results from the previous run */ + clear_previous_results(&f_run_summary, &f_failure_list); + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else if (NULL == pTest) { + result = CUE_NOTEST; + } + else if (CU_FALSE == pSuite->fActive) { + f_run_summary.nSuitesInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, + 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); + } + result = CUE_SUITE_INACTIVE; + } + else if ((NULL == pTest->pName) || (NULL == CU_get_test_by_name(pTest->pName, pSuite))) { + result = CUE_TEST_NOT_IN_SUITE; + } + else { + /* test run is starting - set flag */ + f_bTestIsRunning = CU_TRUE; + f_start_time = clock(); + + f_pCurTest = NULL; + f_pCurSuite = pSuite; + + /* run handler for suite start, if any */ + if (NULL != f_pSuiteStartMessageHandler) { + (*f_pSuiteStartMessageHandler)(pSuite); + } + + /* run the suite initialization function, if any */ + if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { + /* init function had an error - call handler, if any */ + if (NULL != f_pSuiteInitFailureMessageHandler) { + (*f_pSuiteInitFailureMessageHandler)(pSuite); + } + f_run_summary.nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, + _("Suite Initialization failed - Suite Skipped"), + _("CUnit System"), pSuite, NULL); + result = CUE_SINIT_FAILED; + } + /* reach here if no suite initialization, or if it succeeded */ + else { + result2 = run_single_test(pTest, &f_run_summary); + result = (CUE_SUCCESS == result) ? result2 : result; + + /* run the suite cleanup function, if any */ + if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { + /* cleanup function had an error - call handler, if any */ + if (NULL != f_pSuiteCleanupFailureMessageHandler) { + (*f_pSuiteCleanupFailureMessageHandler)(pSuite); + } + f_run_summary.nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, + 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); + result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; + } + } + + /* run handler for suite completion, if any */ + if (NULL != f_pSuiteCompleteMessageHandler) { + (*f_pSuiteCompleteMessageHandler)(pSuite, NULL); + } + + /* test run is complete - clear flag */ + f_bTestIsRunning = CU_FALSE; + f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + + /* run handler for overall completion, if any */ + if (NULL != f_pAllTestsCompleteMessageHandler) { + (*f_pAllTestsCompleteMessageHandler)(f_failure_list); + } + + f_pCurSuite = NULL; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +void CU_clear_previous_results(void) +{ + clear_previous_results(&f_run_summary, &f_failure_list); +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_current_suite(void) +{ + return f_pCurSuite; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_current_test(void) +{ + return f_pCurTest; +} + +/*------------------------------------------------------------------------*/ +CU_BOOL CU_is_test_running(void) +{ + return f_bTestIsRunning; +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive) +{ + f_failure_on_inactive = new_inactive; +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void) +{ + return f_failure_on_inactive; +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT void CU_print_run_results(FILE *file) +{ + char *summary_string; + + assert(NULL != file); + summary_string = CU_get_run_results_string(); + if (NULL != summary_string) { + fprintf(file, "%s", summary_string); + CU_FREE(summary_string); + } + else { + fprintf(file, _("An error occurred printing the run results.")); + } +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT char * CU_get_run_results_string(void) + +{ + CU_pRunSummary pRunSummary = &f_run_summary; + CU_pTestRegistry pRegistry = CU_get_registry(); + int width[9]; + size_t len; + char *result; + + assert(NULL != pRunSummary); + assert(NULL != pRegistry); + + width[0] = strlen(_("Run Summary:")); + width[1] = CU_MAX(6, + CU_MAX(strlen(_("Type")), + CU_MAX(strlen(_("suites")), + CU_MAX(strlen(_("tests")), + strlen(_("asserts")))))) + 1; + width[2] = CU_MAX(6, + CU_MAX(strlen(_("Total")), + CU_MAX(CU_number_width(pRegistry->uiNumberOfSuites), + CU_MAX(CU_number_width(pRegistry->uiNumberOfTests), + CU_number_width(pRunSummary->nAsserts))))) + 1; + width[3] = CU_MAX(6, + CU_MAX(strlen(_("Ran")), + CU_MAX(CU_number_width(pRunSummary->nSuitesRun), + CU_MAX(CU_number_width(pRunSummary->nTestsRun), + CU_number_width(pRunSummary->nAsserts))))) + 1; + width[4] = CU_MAX(6, + CU_MAX(strlen(_("Passed")), + CU_MAX(strlen(_("n/a")), + CU_MAX(CU_number_width(pRunSummary->nTestsRun - pRunSummary->nTestsFailed), + CU_number_width(pRunSummary->nAsserts - pRunSummary->nAssertsFailed))))) + 1; + width[5] = CU_MAX(6, + CU_MAX(strlen(_("Failed")), + CU_MAX(CU_number_width(pRunSummary->nSuitesFailed), + CU_MAX(CU_number_width(pRunSummary->nTestsFailed), + CU_number_width(pRunSummary->nAssertsFailed))))) + 1; + width[6] = CU_MAX(6, + CU_MAX(strlen(_("Inactive")), + CU_MAX(CU_number_width(pRunSummary->nSuitesInactive), + CU_MAX(CU_number_width(pRunSummary->nTestsInactive), + strlen(_("n/a")))))) + 1; + + width[7] = strlen(_("Elapsed time = ")); + width[8] = strlen(_(" seconds")); + + len = 13 + 4*(width[0] + width[1] + width[2] + width[3] + width[4] + width[5] + width[6]) + width[7] + width[8] + 1; + result = (char *)CU_MALLOC(len); + + if (NULL != result) { + snprintf(result, len, "%*s%*s%*s%*s%*s%*s%*s\n" /* if you change this, be sure */ + "%*s%*s%*u%*u%*s%*u%*u\n" /* to change the calculation of */ + "%*s%*s%*u%*u%*u%*u%*u\n" /* len above! */ + "%*s%*s%*u%*u%*u%*u%*s\n\n" + "%*s%8.3f%*s", + width[0], _("Run Summary:"), + width[1], _("Type"), + width[2], _("Total"), + width[3], _("Ran"), + width[4], _("Passed"), + width[5], _("Failed"), + width[6], _("Inactive"), + width[0], " ", + width[1], _("suites"), + width[2], pRegistry->uiNumberOfSuites, + width[3], pRunSummary->nSuitesRun, + width[4], _("n/a"), + width[5], pRunSummary->nSuitesFailed, + width[6], pRunSummary->nSuitesInactive, + width[0], " ", + width[1], _("tests"), + width[2], pRegistry->uiNumberOfTests, + width[3], pRunSummary->nTestsRun, + width[4], pRunSummary->nTestsRun - pRunSummary->nTestsFailed, + width[5], pRunSummary->nTestsFailed, + width[6], pRunSummary->nTestsInactive, + width[0], " ", + width[1], _("asserts"), + width[2], pRunSummary->nAsserts, + width[3], pRunSummary->nAsserts, + width[4], pRunSummary->nAsserts - pRunSummary->nAssertsFailed, + width[5], pRunSummary->nAssertsFailed, + width[6], _("n/a"), + width[7], _("Elapsed time = "), CU_get_elapsed_time(), /* makes sure time is updated */ + width[8], _(" seconds") + ); + result[len-1] = '\0'; + } + return result; +} + +/*================================================================= + * Static Function Definitions + *=================================================================*/ +/** + * Records a runtime failure. + * This function is called whenever a runtime failure occurs. + * This includes user assertion failures, suite initialization and + * cleanup failures, and inactive suites/tests when set as failures. + * This function records the details of the failure in a new + * failure record in the linked list of runtime failures. + * + * @param ppFailure Pointer to head of linked list of failure + * records to append with new failure record. + * If it points to a NULL pointer, it will be set + * to point to the new failure record. + * @param pRunSummary Pointer to CU_RunSummary keeping track of failure records + * (ignored if NULL). + * @param type Type of failure. + * @param uiLineNumber Line number of the failure, if applicable. + * @param szCondition Description of failure condition + * @param szFileName Name of file, if applicable + * @param pSuite The suite being run at time of failure + * @param pTest The test being run at time of failure + */ +static void add_failure(CU_pFailureRecord* ppFailure, + CU_pRunSummary pRunSummary, + CU_FailureType type, + unsigned int uiLineNumber, + const char *szCondition, + const char *szFileName, + CU_pSuite pSuite, + CU_pTest pTest) +{ + CU_pFailureRecord pFailureNew = NULL; + CU_pFailureRecord pTemp = NULL; + + assert(NULL != ppFailure); + + pFailureNew = (CU_pFailureRecord)CU_MALLOC(sizeof(CU_FailureRecord)); + + if (NULL == pFailureNew) { + return; + } + + pFailureNew->strFileName = NULL; + pFailureNew->strCondition = NULL; + if (NULL != szFileName) { + pFailureNew->strFileName = (char*)CU_MALLOC(strlen(szFileName) + 1); + if(NULL == pFailureNew->strFileName) { + CU_FREE(pFailureNew); + return; + } + strcpy(pFailureNew->strFileName, szFileName); + } + + if (NULL != szCondition) { + pFailureNew->strCondition = (char*)CU_MALLOC(strlen(szCondition) + 1); + if (NULL == pFailureNew->strCondition) { + if(NULL != pFailureNew->strFileName) { + CU_FREE(pFailureNew->strFileName); + } + CU_FREE(pFailureNew); + return; + } + strcpy(pFailureNew->strCondition, szCondition); + } + + pFailureNew->type = type; + pFailureNew->uiLineNumber = uiLineNumber; + pFailureNew->pTest = pTest; + pFailureNew->pSuite = pSuite; + pFailureNew->pNext = NULL; + pFailureNew->pPrev = NULL; + + pTemp = *ppFailure; + if (NULL != pTemp) { + while (NULL != pTemp->pNext) { + pTemp = pTemp->pNext; + } + pTemp->pNext = pFailureNew; + pFailureNew->pPrev = pTemp; + } + else { + *ppFailure = pFailureNew; + } + + if (NULL != pRunSummary) { + ++(pRunSummary->nFailureRecords); + } + f_last_failure = pFailureNew; +} + +/* + * Local function for result set initialization/cleanup. + */ +/*------------------------------------------------------------------------*/ +/** + * Initializes the run summary information in the specified structure. + * Resets the run counts to zero, and calls cleanup_failure_list() if + * failures were recorded by the last test run. Calling this function + * multiple times, while inefficient, will not cause an error condition. + * + * @param pRunSummary CU_RunSummary to initialize (non-NULL). + * @param ppFailure The failure record to clean (non-NULL). + * @see CU_clear_previous_results() + */ +static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure) +{ + assert(NULL != pRunSummary); + assert(NULL != ppFailure); + + pRunSummary->nSuitesRun = 0; + pRunSummary->nSuitesFailed = 0; + pRunSummary->nSuitesInactive = 0; + pRunSummary->nTestsRun = 0; + pRunSummary->nTestsFailed = 0; + pRunSummary->nTestsInactive = 0; + pRunSummary->nAsserts = 0; + pRunSummary->nAssertsFailed = 0; + pRunSummary->nFailureRecords = 0; + pRunSummary->ElapsedTime = 0.0; + + if (NULL != *ppFailure) { + cleanup_failure_list(ppFailure); + } + + f_last_failure = NULL; +} + +/*------------------------------------------------------------------------*/ +/** + * Frees all memory allocated for the linked list of test failure + * records. pFailure is reset to NULL after its list is cleaned up. + * + * @param ppFailure Pointer to head of linked list of + * CU_pFailureRecords to clean. + * @see CU_clear_previous_results() + */ +static void cleanup_failure_list(CU_pFailureRecord* ppFailure) +{ + CU_pFailureRecord pCurFailure = NULL; + CU_pFailureRecord pNextFailure = NULL; + + pCurFailure = *ppFailure; + + while (NULL != pCurFailure) { + + if (NULL != pCurFailure->strCondition) { + CU_FREE(pCurFailure->strCondition); + } + + if (NULL != pCurFailure->strFileName) { + CU_FREE(pCurFailure->strFileName); + } + + pNextFailure = pCurFailure->pNext; + CU_FREE(pCurFailure); + pCurFailure = pNextFailure; + } + + *ppFailure = NULL; +} + + +/* Function to collect syslog for a test */ +//FIXME find another bettwer way to do this, It is currentlly leaving tail procs in ps +void cunit_logger_fn(void *fn){ + char sys_cmd[MAX_CMD_LEN] = {'\0'}; + sprintf(sys_cmd, "tail -f /var/log/syslog > %s.txt", (char *)fn); + system("service syslog restart"); + system(sys_cmd); +} + +/*------------------------------------------------------------------------*/ +/** + * Runs all tests in a specified suite. + * Internal function to run all tests in a suite. The suite need + * not be registered in the test registry to be run. Only + * suites having their fActive flags set CU_TRUE will actually be + * run. If the CUnit framework is in an error condition after + * running a test, no additional tests are run. + * + * @param pSuite The suite containing the test (non-NULL). + * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). + * @return A CU_ErrorCode indicating the status of the run. + * @see CU_run_suite() for public interface function. + * @see CU_run_all_tests() for running all suites. + */ +static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary) +{ + CU_pTest pTest = NULL; + unsigned int nStartFailures; + /* keep track of the last failure BEFORE running the test */ + CU_pFailureRecord pLastFailure = f_last_failure; + CU_ErrorCode result = CUE_SUCCESS; + CU_ErrorCode result2; + + assert(NULL != pSuite); + assert(NULL != pRunSummary); + + nStartFailures = pRunSummary->nFailureRecords; + + f_pCurTest = NULL; + f_pCurSuite = pSuite; + + /* run handler for suite start, if any */ + if (NULL != f_pSuiteStartMessageHandler) { + (*f_pSuiteStartMessageHandler)(pSuite); + } + + /* run suite if it's active */ + if (CU_FALSE != pSuite->fActive) { + + /* run the suite initialization function, if any */ + if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { + /* init function had an error - call handler, if any */ + if (NULL != f_pSuiteInitFailureMessageHandler) { + (*f_pSuiteInitFailureMessageHandler)(pSuite); + } + pRunSummary->nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, + _("Suite Initialization failed - Suite Skipped"), + _("CUnit System"), pSuite, NULL); + result = CUE_SINIT_FAILED; + } + + /* reach here if no suite initialization, or if it succeeded */ + else { + pTest = pSuite->pTest; + while ((NULL != pTest) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { + if (CU_FALSE != pTest->fActive) { + result2 = run_single_test(pTest, pRunSummary); + result = (CUE_SUCCESS == result) ? result2 : result; + } + else { + f_run_summary.nTestsInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, + 0, _("Test inactive"), _("CUnit System"), pSuite, pTest); + result = CUE_TEST_INACTIVE; + } + } + pTest = pTest->pNext; + } + pRunSummary->nSuitesRun++; + + /* call the suite cleanup function, if any */ + if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { + if (NULL != f_pSuiteCleanupFailureMessageHandler) { + (*f_pSuiteCleanupFailureMessageHandler)(pSuite); + } + pRunSummary->nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, + 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); + result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; + } + } + } + + /* otherwise record inactive suite and failure if appropriate */ + else { + f_run_summary.nSuitesInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, + 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); + result = CUE_SUITE_INACTIVE; + } + } + + /* if additional failures have occurred... */ + if (pRunSummary->nFailureRecords > nStartFailures) { + if (NULL != pLastFailure) { + pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ + } + else { + pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ + } + } + else { + pLastFailure = NULL; /* no additional failure - set to NULL */ + } + + /* run handler for suite completion, if any */ + if (NULL != f_pSuiteCompleteMessageHandler) { + (*f_pSuiteCompleteMessageHandler)(pSuite, pLastFailure); + } + + f_pCurSuite = NULL; + return result; +} + +/*------------------------------------------------------------------------*/ +/** + * Runs a specific test. + * Internal function to run a test case. This includes calling + * any handler to be run before executing the test, running the + * test's function (if any), and calling any handler to be run + * after executing a test. Suite initialization and cleanup functions + * are not called by this function. A current suite must be set and + * active (checked by assertion). + * + * @param pTest The test to be run (non-NULL). + * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). + * @return A CU_ErrorCode indicating the status of the run. + * @see CU_run_test() for public interface function. + * @see CU_run_all_tests() for running all suites. + */ +static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary) +{ + volatile unsigned int nStartFailures; +#if NOT_YET + pthread_t tid; +#endif + int rv = 0; + cunit_logger_cb cb = cunit_logger_fn; + /* keep track of the last failure BEFORE running the test */ + volatile CU_pFailureRecord pLastFailure = f_last_failure; + jmp_buf buf; + CU_ErrorCode result = CUE_SUCCESS; + + assert(NULL != f_pCurSuite); + assert(CU_FALSE != f_pCurSuite->fActive); + assert(NULL != pTest); + assert(NULL != pRunSummary); + + nStartFailures = pRunSummary->nFailureRecords; + + f_pCurTest = pTest; + + if (NULL != f_pTestStartMessageHandler) { + (*f_pTestStartMessageHandler)(f_pCurTest, f_pCurSuite); + } + + /* run test if it is active */ + if (CU_FALSE != pTest->fActive) { + + /* set jmp_buf and run test */ + pTest->pJumpBuf = &buf; + if (0 == setjmp(buf)) { + if (NULL != pTest->pTestFunc) { +#if NOT_YET + if (collect_syslog) {//FIXME Currently WIP, It is not reliable for huge batch runs now + rv = pthread_create(&tid, NULL, (void *)cb, (void *)pTest->pName); + if (rv) { + fprintf(stdout, "thread create failed for collecting syslog for %s\n",(char *)pTest->pName); + } + } +#endif + (*pTest->pTestFunc)(); +#if NOT_YET + if (collect_syslog){ + sleep(1); + pthread_cancel(tid); + } +#endif + } + } + + pRunSummary->nTestsRun++; + } + else { + f_run_summary.nTestsInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, + 0, _("Test inactive"), _("CUnit System"), f_pCurSuite, f_pCurTest); + } + result = CUE_TEST_INACTIVE; + } + + /* if additional failures have occurred... */ + if (pRunSummary->nFailureRecords > nStartFailures) { + pRunSummary->nTestsFailed++; + if (NULL != pLastFailure) { + pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ + } + else { + pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ + } + } + else { + pLastFailure = NULL; /* no additional failure - set to NULL */ + } + + if (NULL != f_pTestCompleteMessageHandler) { + (*f_pTestCompleteMessageHandler)(f_pCurTest, f_pCurSuite, pLastFailure); + } + + pTest->pJumpBuf = NULL; + f_pCurTest = NULL; + + return result; +} + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +/** Types of framework events tracked by test system. */ +typedef enum TET { + SUITE_START = 1, + TEST_START, + TEST_COMPLETE, + SUITE_COMPLETE, + ALL_TESTS_COMPLETE, + SUITE_INIT_FAILED, + SUITE_CLEANUP_FAILED +} TestEventType; + +/** Test event structure for recording details of a framework event. */ +typedef struct TE { + TestEventType type; + CU_pSuite pSuite; + CU_pTest pTest; + CU_pFailureRecord pFailure; + struct TE * pNext; +} TestEvent, * pTestEvent; + +static int f_nTestEvents = 0; +static pTestEvent f_pFirstEvent = NULL; + +/** Creates & stores a test event record having the specified details. */ +static void add_test_event(TestEventType type, CU_pSuite psuite, + CU_pTest ptest, CU_pFailureRecord pfailure) +{ + pTestEvent pNewEvent = (pTestEvent)malloc(sizeof(TestEvent)); + pTestEvent pNextEvent = f_pFirstEvent; + + if (NULL == pNewEvent) { + fprintf(stderr, "Memory allocation failed in add_test_event()."); + exit(1); + } + + pNewEvent->type = type; + pNewEvent->pSuite = psuite; + pNewEvent->pTest = ptest; + pNewEvent->pFailure = pfailure; + pNewEvent->pNext = NULL; + + if (pNextEvent) { + while (pNextEvent->pNext) { + pNextEvent = pNextEvent->pNext; + } + pNextEvent->pNext = pNewEvent; + } + else { + f_pFirstEvent = pNewEvent; + } + ++f_nTestEvents; +} + +/** Deallocates all test event data. */ +static void clear_test_events(void) +{ + pTestEvent pCurrentEvent = f_pFirstEvent; + pTestEvent pNextEvent = NULL; + + while (pCurrentEvent) { + pNextEvent = pCurrentEvent->pNext; + free(pCurrentEvent); + pCurrentEvent = pNextEvent; + } + + f_pFirstEvent = NULL; + f_nTestEvents = 0; +} + +static void suite_start_handler(const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(NULL == CU_get_current_test()); + + add_test_event(SUITE_START, pSuite, NULL, NULL); +} + +static void test_start_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(pTest == CU_get_current_test()); + + add_test_event(TEST_START, pSuite, pTest, NULL); +} + +static void test_complete_handler(const CU_pTest pTest, const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(pTest == CU_get_current_test()); + + add_test_event(TEST_COMPLETE, pSuite, pTest, pFailure); +} + +static void suite_complete_handler(const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(NULL == CU_get_current_test()); + + add_test_event(SUITE_COMPLETE, pSuite, NULL, pFailure); +} + +static void test_all_complete_handler(const CU_pFailureRecord pFailure) +{ + TEST(!CU_is_test_running()); + + add_test_event(ALL_TESTS_COMPLETE, NULL, NULL, pFailure); +} + +static void suite_init_failure_handler(const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + + add_test_event(SUITE_INIT_FAILED, pSuite, NULL, NULL); +} + +static void suite_cleanup_failure_handler(const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + + add_test_event(SUITE_CLEANUP_FAILED, pSuite, NULL, NULL); +} + +/** + * Centralize test result testing - we're going to do it a lot! + * This is messy since we want to report the calling location upon failure. + * + * Via calling test functions tests: + * CU_get_number_of_suites_run() + * CU_get_number_of_suites_failed() + * CU_get_number_of_tests_run() + * CU_get_number_of_tests_failed() + * CU_get_number_of_asserts() + * CU_get_number_of_successes() + * CU_get_number_of_failures() + * CU_get_number_of_failure_records() + * CU_get_run_summary() + */ +static void do_test_results(unsigned int nSuitesRun, + unsigned int nSuitesFailed, + unsigned int nSuitesInactive, + unsigned int nTestsRun, + unsigned int nTestsFailed, + unsigned int nTestsInactive, + unsigned int nAsserts, + unsigned int nSuccesses, + unsigned int nFailures, + unsigned int nFailureRecords, + const char *file, + unsigned int line) +{ + char msg[500]; + CU_pRunSummary pRunSummary = NULL; + + if (nSuitesRun == CU_get_number_of_suites_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", + nSuitesRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nSuitesInactive == CU_get_number_of_suites_inactive()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_inactive() (called from %s:%u)", + nSuitesInactive, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nSuitesFailed == CU_get_number_of_suites_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", + nSuitesFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nTestsRun == CU_get_number_of_tests_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", + nTestsRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nTestsFailed == CU_get_number_of_tests_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", + nTestsFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nTestsInactive == CU_get_number_of_tests_inactive()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_inactive() (called from %s:%u)", + nTestsInactive, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nAsserts == CU_get_number_of_asserts()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", + nAsserts, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nSuccesses == CU_get_number_of_successes()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_successes() (called from %s:%u)", + nSuccesses, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nFailures == CU_get_number_of_failures()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", + nFailures, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nFailureRecords == CU_get_number_of_failure_records()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", + nFailureRecords, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + pRunSummary = CU_get_run_summary(); + + if (pRunSummary->nSuitesRun == CU_get_number_of_suites_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", + pRunSummary->nSuitesRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nSuitesFailed == CU_get_number_of_suites_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", + pRunSummary->nSuitesFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nTestsRun == CU_get_number_of_tests_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", + pRunSummary->nTestsRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nTestsFailed == CU_get_number_of_tests_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", + pRunSummary->nTestsFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nAsserts == CU_get_number_of_asserts()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", + pRunSummary->nAsserts, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nAssertsFailed == CU_get_number_of_failures()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", + pRunSummary->nAssertsFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nFailureRecords == CU_get_number_of_failure_records()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", + pRunSummary->nFailureRecords, file, line); + msg[499] = '\0'; + FAIL(msg); + } +} + +#define test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ + nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords) \ + do_test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ + nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords, \ + __FILE__, __LINE__) + +static void test_succeed(void) { CU_TEST(CU_TRUE); } +static void test_fail(void) { CU_TEST(CU_FALSE); } +static int suite_succeed(void) { return 0; } +static int suite_fail(void) { return 1; } + +/*-------------------------------------------------*/ +/* tests: + * CU_set_suite_start_handler() + * CU_set_test_start_handler() + * CU_set_test_complete_handler() + * CU_set_suite_complete_handler() + * CU_set_all_test_complete_handler() + * CU_set_suite_init_failure_handler() + * CU_set_suite_cleanup_failure_handler() + * CU_get_suite_start_handler() + * CU_get_test_start_handler() + * CU_get_test_complete_handler() + * CU_get_suite_complete_handler() + * CU_get_all_test_complete_handler() + * CU_get_suite_init_failure_handler() + * CU_get_suite_cleanup_failure_handler() + * CU_is_test_running() + * via handlers tests: + * CU_get_current_suite() + * CU_get_current_test() + */ +static void test_message_handlers(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + pTestEvent pEvent = NULL; + + TEST(!CU_is_test_running()); + + /* handlers should be NULL on startup */ + TEST(NULL == CU_get_suite_start_handler()); + TEST(NULL == CU_get_test_start_handler()); + TEST(NULL == CU_get_test_complete_handler()); + TEST(NULL == CU_get_suite_complete_handler()); + TEST(NULL == CU_get_all_test_complete_handler()); + TEST(NULL == CU_get_suite_init_failure_handler()); + TEST(NULL == CU_get_suite_cleanup_failure_handler()); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test3", test_succeed); + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest4 = CU_add_test(pSuite2, "test4", test_succeed); + pSuite3 = CU_add_suite("suite3", suite_succeed, suite_fail); + pTest5 = CU_add_test(pSuite3, "test5", test_fail); + + TEST_FATAL(CUE_SUCCESS == CU_get_error()); + + /* first run tests without handlers set */ + clear_test_events(); + CU_run_all_tests(); + + TEST(0 == f_nTestEvents); + TEST(NULL == f_pFirstEvent); + test_results(2,2,0,4,2,0,4,2,2,4); + + /* set handlers to local functions */ + CU_set_suite_start_handler(&suite_start_handler); + CU_set_test_start_handler(&test_start_handler); + CU_set_test_complete_handler(&test_complete_handler); + CU_set_suite_complete_handler(&suite_complete_handler); + CU_set_all_test_complete_handler(&test_all_complete_handler); + CU_set_suite_init_failure_handler(&suite_init_failure_handler); + CU_set_suite_cleanup_failure_handler(&suite_cleanup_failure_handler); + + /* confirm handlers set properly */ + TEST(suite_start_handler == CU_get_suite_start_handler()); + TEST(test_start_handler == CU_get_test_start_handler()); + TEST(test_complete_handler == CU_get_test_complete_handler()); + TEST(suite_complete_handler == CU_get_suite_complete_handler()); + TEST(test_all_complete_handler == CU_get_all_test_complete_handler()); + TEST(suite_init_failure_handler == CU_get_suite_init_failure_handler()); + TEST(suite_cleanup_failure_handler == CU_get_suite_cleanup_failure_handler()); + + /* run tests again with handlers set */ + clear_test_events(); + CU_run_all_tests(); + + TEST(17 == f_nTestEvents); + if (17 == f_nTestEvents) { + pEvent = f_pFirstEvent; + TEST(SUITE_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest1 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest1 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest2 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest2 == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest3 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest3 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_START == pEvent->type); + TEST(pSuite2 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_INIT_FAILED == pEvent->type); + TEST(pSuite2 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_COMPLETE == pEvent->type); + TEST(pSuite2 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_START == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(pTest5 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(pTest5 == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_CLEANUP_FAILED == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_COMPLETE == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(ALL_TESTS_COMPLETE == pEvent->type); + TEST(NULL == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + if (4 == CU_get_number_of_failure_records()) { + TEST(NULL != pEvent->pFailure->pNext); + TEST(NULL != pEvent->pFailure->pNext->pNext); + TEST(NULL != pEvent->pFailure->pNext->pNext->pNext); + TEST(NULL == pEvent->pFailure->pNext->pNext->pNext->pNext); + } + TEST(pEvent->pFailure == CU_get_failure_list()); + } + + test_results(2,2,0,4,2,0,4,2,2,4); + + /* clear handlers and run again */ + CU_set_suite_start_handler(NULL); + CU_set_test_start_handler(NULL); + CU_set_test_complete_handler(NULL); + CU_set_suite_complete_handler(NULL); + CU_set_all_test_complete_handler(NULL); + CU_set_suite_init_failure_handler(NULL); + CU_set_suite_cleanup_failure_handler(NULL); + + TEST(NULL == CU_get_suite_start_handler()); + TEST(NULL == CU_get_test_start_handler()); + TEST(NULL == CU_get_test_complete_handler()); + TEST(NULL == CU_get_suite_complete_handler()); + TEST(NULL == CU_get_all_test_complete_handler()); + TEST(NULL == CU_get_suite_init_failure_handler()); + TEST(NULL == CU_get_suite_cleanup_failure_handler()); + + clear_test_events(); + CU_run_all_tests(); + + TEST(0 == f_nTestEvents); + TEST(NULL == f_pFirstEvent); + test_results(2,2,0,4,2,0,4,2,2,4); + + CU_cleanup_registry(); + clear_test_events(); +} + +static CU_BOOL f_exit_called = CU_FALSE; + +/* intercept exit for testing of CUEA_ABORT action */ +void test_exit(int status) +{ + CU_UNREFERENCED_PARAMETER(status); /* not used */ + f_exit_called = CU_TRUE; +} + + +/*-------------------------------------------------*/ +static void test_CU_fail_on_inactive(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + + CU_set_error_action(CUEA_IGNORE); + CU_initialize_registry(); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest3 = CU_add_test(pSuite2, "test3", test_succeed); + pTest4 = CU_add_test(pSuite2, "test4", test_succeed); + + /* test initial conditions */ + TEST(CU_TRUE == CU_get_fail_on_inactive()); + TEST(CU_TRUE == pSuite1->fActive); + TEST(CU_TRUE == pSuite2->fActive); + TEST(CU_TRUE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_TRUE == pTest4->fActive); + + CU_set_fail_on_inactive(CU_TRUE); + TEST(CU_TRUE == CU_get_fail_on_inactive()); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ + test_results(1,1,0,2,1,0,2,1,1,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CU_FALSE == CU_get_fail_on_inactive()); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); + test_results(1,1,0,2,1,0,2,1,1,2); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* all suites inactive */ + test_results(0,0,2,0,0,0,0,0,0,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(0,0,2,0,0,0,0,0,0,0); + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* some suites inactive */ + test_results(1,0,1,2,1,0,2,1,1,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(1,0,1,2,1,0,2,1,1,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* all tests inactive */ + test_results(1,1,0,0,0,2,0,0,0,3); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); + test_results(1,1,0,0,0,2,0,0,0,1); + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some tests inactive */ + test_results(1,1,0,1,0,1,1,1,0,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); + test_results(1,1,0,1,0,1,1,1,0,1); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_test_active(pTest1, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some suites & tests inactive */ + test_results(1,0,1,1,1,1,1,0,1,3); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(1,0,1,1,1,1,1,0,1,1); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_test_active(pTest1, CU_TRUE); + + /* clean up */ + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +static void test_CU_run_all_tests(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + CU_pTest pTest10 = NULL; + + /* error - uninitialized registry (CUEA_IGNORE) */ + CU_cleanup_registry(); + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOREGISTRY == CU_run_all_tests()); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* error - uninitialized registry (CUEA_FAIL) */ + CU_cleanup_registry(); + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOREGISTRY == CU_run_all_tests()); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* error - uninitialized registry (CUEA_ABORT) */ + CU_cleanup_registry(); + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_all_tests(); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* run with no suites or tests registered */ + CU_initialize_registry(); + + CU_set_error_action(CUEA_IGNORE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(0,0,0,0,0,0,0,0,0,0); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name OK */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test9", test_succeed); + pSuite4 = CU_add_suite("suite4", NULL, suite_fail); + pTest10 = CU_add_test(pSuite4, "test10", test_succeed); + + TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); + TEST_FATAL(10 == CU_get_registry()->uiNumberOfTests); + + /* run all tests (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ + test_results(3,2,0,8,3,0,8,5,3,5); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_suite_active(pSuite4, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); /* suites inactive */ + test_results(0,0,4,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + test_results(0,0,4,0,0,0,0,0,0,4); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some suites inactive */ + test_results(1,1,2,2,1,0,2,1,1,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + test_results(1,1,2,2,1,0,2,1,1,4); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ + test_results(3,2,0,0,0,8,0,0,0,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(3,2,0,0,0,8,0,0,0,10); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ + test_results(3,2,0,4,0,4,4,4,0,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(3,2,0,4,0,4,4,4,0,6); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_TRUE); + + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + test_results(2,3,0,3,1,0,3,2,1,4); + + CU_set_suite_cleanupfunc(pSuite4, NULL); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite4, &suite_fail); + test_results(3,1,0,8,3,0,8,5,3,4); + + CU_set_test_func(pTest2, &test_succeed); + CU_set_test_func(pTest4, &test_succeed); + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ + CU_set_test_func(pTest2, &test_fail); + CU_set_test_func(pTest4, &test_fail); + CU_set_test_func(pTest8, &test_fail); + test_results(3,2,0,8,0,0,8,8,0,2); + + /* run all tests (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_suite_active(pSuite4, CU_TRUE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites inactive */ + test_results(2,1,2,6,2,0,6,4,2,3); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + test_results(1,0,1,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ + test_results(1,1,0,0,0,5,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_TRUE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ + test_results(1,1,0,2,2,3,2,0,2,3); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_TRUE); + + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + test_results(4,1,0,10,3,0,10,7,3,4); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_test_func(pTest1, &test_fail); + CU_set_test_func(pTest3, &test_fail); + CU_set_test_func(pTest5, &test_fail); + CU_set_test_func(pTest9, &test_fail); + CU_set_test_func(pTest10, &test_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ + CU_set_test_func(pTest1, &test_succeed); + CU_set_test_func(pTest3, &test_succeed); + CU_set_test_func(pTest5, &test_succeed); + CU_set_test_func(pTest9, &test_succeed); + CU_set_test_func(pTest10, &test_succeed); + test_results(1,1,0,5,5,0,5,0,5,6); + + /* run all tests (CUEA_ABORT) */ + f_exit_called = CU_FALSE; + CU_set_error_action(CUEA_ABORT); + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_suite_active(pSuite4, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); /* no suites active, so no abort() */ + TEST(CU_FALSE == f_exit_called); + test_results(0,0,4,0,0,0,0,0,0,0); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,1); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites active */ + TEST(CU_TRUE == f_exit_called); + test_results(3,1,1,8,3,0,8,5,3,4); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(1,0,1,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,5,0,0,0,1); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_TRUE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,2,2,3,2,0,2,3); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_TRUE); + + f_exit_called = CU_FALSE; + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,2,0,5,3,2,3); + + f_exit_called = CU_FALSE; + CU_set_test_func(pTest1, &test_fail); + CU_set_test_func(pTest3, &test_fail); + CU_set_test_func(pTest5, &test_fail); + CU_set_test_func(pTest9, &test_fail); + CU_set_test_func(pTest10, &test_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ + CU_set_test_func(pTest1, &test_succeed); + CU_set_test_func(pTest3, &test_succeed); + CU_set_test_func(pTest5, &test_succeed); + CU_set_test_func(pTest9, &test_succeed); + CU_set_test_func(pTest10, &test_succeed); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,5,0,5,0,5,6); + + /* clean up after testing */ + CU_set_error_action(CUEA_IGNORE); + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +static void test_CU_run_suite(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + + /* error - NULL suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOSUITE == CU_run_suite(NULL)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL suite (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOSUITE == CU_run_suite(NULL)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL suite (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_suite(NULL); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test3", test_succeed); + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test5", test_succeed); + pSuite2 = CU_add_suite("suite1", suite_fail, NULL); /* duplicate suite name OK */ + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite3", NULL, suite_fail); + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ + pSuite4 = CU_add_suite("suite4", NULL, NULL); + + TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); + TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); + + /* run each suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suites/tests active */ + test_results(1,0,0,5,2,0,5,3,2,2); + + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); + test_results(1,1,0,2,1,0,2,1,1,2); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); + test_results(1,0,0,0,0,0,0,0,0,0); + + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite3)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite3)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite3, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* all tests inactive */ + test_results(1,0,0,0,0,5,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); + test_results(1,0,0,0,0,5,0,0,0,5); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* some tests inactive */ + test_results(1,0,0,3,0,2,3,3,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); + test_results(1,0,0,3,0,2,3,3,0,2); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + test_results(0,1,0,0,0,0,0,0,0,1); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_test_func(pTest1, &test_fail); + CU_set_test_func(pTest3, &test_fail); + CU_set_test_func(pTest5, &test_fail); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ + CU_set_test_func(pTest1, &test_succeed); + CU_set_test_func(pTest3, &test_succeed); + CU_set_test_func(pTest5, &test_succeed); + test_results(1,0,0,5,5,0,5,0,5,5); + + /* run each suite (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ + test_results(1,0,0,5,2,0,5,3,2,2); + + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); + test_results(1,1,0,2,1,0,2,1,1,2); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); + test_results(1,0,0,0,0,0,0,0,0,0); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite1)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite1, CU_TRUE); + + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ + test_results(1,1,0,0,0,2,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + test_results(1,1,0,0,0,1,0,0,0,2); + + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ + test_results(1,1,0,1,1,1,1,0,1,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + test_results(1,1,0,1,1,1,1,0,1,3); + CU_set_test_active(pTest9, CU_TRUE); + + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + test_results(1,0,0,2,0,0,2,2,0,0); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_test_func(pTest2, &test_succeed); + CU_set_test_func(pTest4, &test_succeed); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ + CU_set_test_func(pTest2, &test_fail); + CU_set_test_func(pTest4, &test_fail); + test_results(1,0,0,5,0,0,5,5,0,0); + + /* run each suite (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ + TEST(CU_FALSE == f_exit_called); + test_results(1,0,0,5,2,0,5,3,2,2); + + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,2,1,0,2,1,1,2); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); + TEST(CU_FALSE == f_exit_called); + test_results(1,0,0,0,0,0,0,0,0,0); + + CU_set_suite_active(pSuite2, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* suite inactive, but not a failure */ + TEST(CU_FALSE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,0); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite2)); + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,2,0,0,0,1); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,1,0,0,0,2); + + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_TRUE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,1,0,1,1,1,0,1); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,1,0,0,0,2); + CU_set_test_active(pTest8, CU_TRUE); + + f_exit_called = CU_FALSE; + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,2,0,5,3,2,3); + + f_exit_called = CU_FALSE; + CU_set_test_func(pTest8, &test_succeed); + CU_set_test_func(pTest9, &test_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + CU_set_test_func(pTest9, &test_succeed); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,2,1,0,2,1,1,2); + + /* clean up after testing */ + CU_set_error_action(CUEA_IGNORE); + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +static void test_CU_run_test(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test3", test_succeed); + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test5", test_succeed); + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite2", NULL, suite_fail); /* duplicate suite name OK */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ + + TEST_FATAL(3 == CU_get_registry()->uiNumberOfSuites); + TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); + + /* error - NULL suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL suite (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_test(NULL, pTest1); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* error - NULL test (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); + TEST(CUE_NOTEST == CU_get_error()); + + /* error - NULL test (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); + TEST(CUE_NOTEST == CU_get_error()); + + /* error - NULL test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_test(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* error - test not in suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); + TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); + + /* error - NULL test (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); + TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); + + /* error - NULL test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_test(pSuite3, pTest1); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* run each test (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* all suite/tests active */ + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); + test_results(0,1,0,1,1,0,1,0,1,2); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite1, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); /* test inactive */ + test_results(0,0,0,0,0,1,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); + test_results(0,0,0,0,1,1,0,0,0,1); + CU_set_test_active(pTest1, CU_TRUE); + + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + test_results(0,1,0,0,0,0,0,0,0,1); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + test_results(0,1,0,1,0,0,1,1,0,1); + + /* run each test (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* suite/test active */ + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); + test_results(0,1,0,1,1,0,1,0,1,2); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest7, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); /* test inactive */ + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_test_active(pTest7, CU_TRUE); + + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + test_results(0,0,0,1,0,0,1,1,0,0); + + CU_set_suite_cleanupfunc(pSuite3, NULL); + TEST(CUE_SUCCESS == CU_run_test(pSuite3, pTest8)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite3, &suite_fail); + test_results(0,0,0,1,1,0,1,0,1,1); + + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + test_results(0,1,0,1,0,0,1,1,0,1); + + /* run each test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,1,0,1,0,1,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,1,0,1,0,1,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,1,0,1,0,1,2); + + f_exit_called = CU_FALSE; + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + f_exit_called = CU_FALSE; + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); /* suite inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + f_exit_called = CU_FALSE; + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest6, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); /* test inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_test_active(pTest6, CU_TRUE); + + f_exit_called = CU_FALSE; + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,0,0,1,1,0,1); + + f_exit_called = CU_FALSE; + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,0,0,1,1,0,1); + + /* clean up after testing */ + CU_set_error_action(CUEA_IGNORE); + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +/* tests CU_assertImplementation() + * CU_get_failure_list() + * CU_clear_previous_results() + */ +static void test_CU_assertImplementation(void) +{ + CU_Test dummy_test; + CU_Suite dummy_suite; + CU_pFailureRecord pFailure1 = NULL; + CU_pFailureRecord pFailure2 = NULL; + CU_pFailureRecord pFailure3 = NULL; + CU_pFailureRecord pFailure4 = NULL; + CU_pFailureRecord pFailure5 = NULL; + CU_pFailureRecord pFailure6 = NULL; + + CU_clear_previous_results(); + + TEST(NULL == CU_get_failure_list()); + TEST(0 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + /* fool CU_assertImplementation into thinking test run is in progress */ + f_pCurTest = &dummy_test; + f_pCurSuite = &dummy_suite; + + /* asserted value is CU_TRUE*/ + TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 100, "Nothing happened 0.", "dummy0.c", "dummy_func0", CU_FALSE)); + + TEST(NULL == CU_get_failure_list()); + TEST(1 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 101, "Nothing happened 1.", "dummy1.c", "dummy_func1", CU_FALSE)); + + TEST(NULL == CU_get_failure_list()); + TEST(2 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + /* asserted value is CU_FALSE */ + TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 102, "Something happened 2.", "dummy2.c", "dummy_func2", CU_FALSE)); + + TEST(NULL != CU_get_failure_list()); + TEST(3 == CU_get_number_of_asserts()); + TEST(1 == CU_get_number_of_failures()); + TEST(1 == CU_get_number_of_failure_records()); + + TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 103, "Something happened 3.", "dummy3.c", "dummy_func3", CU_FALSE)); + + TEST(NULL != CU_get_failure_list()); + TEST(4 == CU_get_number_of_asserts()); + TEST(2 == CU_get_number_of_failures()); + TEST(2 == CU_get_number_of_failure_records()); + + TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 104, "Something happened 4.", "dummy4.c", "dummy_func4", CU_FALSE)); + + TEST(NULL != CU_get_failure_list()); + TEST(5 == CU_get_number_of_asserts()); + TEST(3 == CU_get_number_of_failures()); + TEST(3 == CU_get_number_of_failure_records()); + + if (3 == CU_get_number_of_failure_records()) { + pFailure1 = CU_get_failure_list(); + TEST(102 == pFailure1->uiLineNumber); + TEST(!strcmp("dummy2.c", pFailure1->strFileName)); + TEST(!strcmp("Something happened 2.", pFailure1->strCondition)); + TEST(&dummy_test == pFailure1->pTest); + TEST(&dummy_suite == pFailure1->pSuite); + TEST(NULL != pFailure1->pNext); + TEST(NULL == pFailure1->pPrev); + + pFailure2 = pFailure1->pNext; + TEST(103 == pFailure2->uiLineNumber); + TEST(!strcmp("dummy3.c", pFailure2->strFileName)); + TEST(!strcmp("Something happened 3.", pFailure2->strCondition)); + TEST(&dummy_test == pFailure2->pTest); + TEST(&dummy_suite == pFailure2->pSuite); + TEST(NULL != pFailure2->pNext); + TEST(pFailure1 == pFailure2->pPrev); + + pFailure3 = pFailure2->pNext; + TEST(104 == pFailure3->uiLineNumber); + TEST(!strcmp("dummy4.c", pFailure3->strFileName)); + TEST(!strcmp("Something happened 4.", pFailure3->strCondition)); + TEST(&dummy_test == pFailure3->pTest); + TEST(&dummy_suite == pFailure3->pSuite); + TEST(NULL == pFailure3->pNext); + TEST(pFailure2 == pFailure3->pPrev); + } + else + FAIL("Unexpected number of failure records."); + + /* confirm destruction of failure records */ + pFailure4 = pFailure1; + pFailure5 = pFailure2; + pFailure6 = pFailure3; + TEST(0 != test_cunit_get_n_memevents(pFailure4)); + TEST(test_cunit_get_n_allocations(pFailure4) != test_cunit_get_n_deallocations(pFailure4)); + TEST(0 != test_cunit_get_n_memevents(pFailure5)); + TEST(test_cunit_get_n_allocations(pFailure5) != test_cunit_get_n_deallocations(pFailure5)); + TEST(0 != test_cunit_get_n_memevents(pFailure6)); + TEST(test_cunit_get_n_allocations(pFailure6) != test_cunit_get_n_deallocations(pFailure6)); + + CU_clear_previous_results(); + TEST(0 != test_cunit_get_n_memevents(pFailure4)); + TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); + TEST(0 != test_cunit_get_n_memevents(pFailure5)); + TEST(test_cunit_get_n_allocations(pFailure5) == test_cunit_get_n_deallocations(pFailure5)); + TEST(0 != test_cunit_get_n_memevents(pFailure6)); + TEST(test_cunit_get_n_allocations(pFailure6) == test_cunit_get_n_deallocations(pFailure6)); + TEST(0 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_successes()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + f_pCurTest = NULL; + f_pCurSuite = NULL; +} + +/*-------------------------------------------------*/ +static void test_add_failure(void) +{ + CU_Test test1; + CU_Suite suite1; + CU_pFailureRecord pFailure1 = NULL; + CU_pFailureRecord pFailure2 = NULL; + CU_pFailureRecord pFailure3 = NULL; + CU_pFailureRecord pFailure4 = NULL; + CU_RunSummary run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + /* test under memory exhaustion */ + test_cunit_deactivate_malloc(); + add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 100, "condition 0", "file0.c", &suite1, &test1); + TEST(NULL == pFailure1); + TEST(0 == run_summary.nFailureRecords); + test_cunit_activate_malloc(); + + /* normal operation */ + add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 101, "condition 1", "file1.c", &suite1, &test1); + TEST(1 == run_summary.nFailureRecords); + if (TEST(NULL != pFailure1)) { + TEST(101 == pFailure1->uiLineNumber); + TEST(!strcmp("condition 1", pFailure1->strCondition)); + TEST(!strcmp("file1.c", pFailure1->strFileName)); + TEST(&test1 == pFailure1->pTest); + TEST(&suite1 == pFailure1->pSuite); + TEST(NULL == pFailure1->pNext); + TEST(NULL == pFailure1->pPrev); + TEST(pFailure1 == f_last_failure); + TEST(0 != test_cunit_get_n_memevents(pFailure1)); + TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); + } + + add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 102, "condition 2", "file2.c", NULL, &test1); + TEST(2 == run_summary.nFailureRecords); + if (TEST(NULL != pFailure1)) { + TEST(101 == pFailure1->uiLineNumber); + TEST(!strcmp("condition 1", pFailure1->strCondition)); + TEST(!strcmp("file1.c", pFailure1->strFileName)); + TEST(&test1 == pFailure1->pTest); + TEST(&suite1 == pFailure1->pSuite); + TEST(NULL != pFailure1->pNext); + TEST(NULL == pFailure1->pPrev); + TEST(pFailure1 != f_last_failure); + TEST(0 != test_cunit_get_n_memevents(pFailure1)); + TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); + + if (TEST(NULL != (pFailure2 = pFailure1->pNext))) { + TEST(102 == pFailure2->uiLineNumber); + TEST(!strcmp("condition 2", pFailure2->strCondition)); + TEST(!strcmp("file2.c", pFailure2->strFileName)); + TEST(&test1 == pFailure2->pTest); + TEST(NULL == pFailure2->pSuite); + TEST(NULL == pFailure2->pNext); + TEST(pFailure1 == pFailure2->pPrev); + TEST(pFailure2 == f_last_failure); + TEST(0 != test_cunit_get_n_memevents(pFailure2)); + TEST(test_cunit_get_n_allocations(pFailure2) != test_cunit_get_n_deallocations(pFailure2)); + } + } + + pFailure3 = pFailure1; + pFailure4 = pFailure2; + clear_previous_results(&run_summary, &pFailure1); + + TEST(0 == run_summary.nFailureRecords); + TEST(0 != test_cunit_get_n_memevents(pFailure3)); + TEST(test_cunit_get_n_allocations(pFailure3) == test_cunit_get_n_deallocations(pFailure3)); + TEST(0 != test_cunit_get_n_memevents(pFailure4)); + TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); +} + +/*-------------------------------------------------*/ +void test_cunit_TestRun(void) +{ + test_cunit_start_tests("TestRun.c"); + + test_message_handlers(); + test_CU_fail_on_inactive(); + test_CU_run_all_tests(); + test_CU_run_suite(); + test_CU_run_test(); + test_CU_assertImplementation(); + test_add_failure(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestRun.cpp b/gearsyncd/tests/cunit/TestRun.cpp new file mode 100644 index 0000000000..ac6a735b01 --- /dev/null +++ b/gearsyncd/tests/cunit/TestRun.cpp @@ -0,0 +1,2913 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Implementation of Test Run Interface. + * + * Aug 2001 Initial implementaion (AK) + * + * 19/Aug/2001 Added initial registry/Suite/test framework implementation. (AK) + * + * 24/Aug/2001 Changed Data structure from SLL to DLL for all linked lists. (AK) + * + * 25/Nov/2001 Added notification for Suite Initialization failure condition. (AK) + * + * 5-Aug-2004 New interface, doxygen comments, moved add_failure on suite + * initialization so called even if a callback is not registered, + * moved CU_assertImplementation into TestRun.c, consolidated + * all run summary info out of CU_TestRegistry into TestRun.c, + * revised counting and reporting of run stats to cleanly + * differentiate suite, test, and assertion failures. (JDS) + * + * 1-Sep-2004 Modified CU_assertImplementation() and run_single_test() for + * setjmp/longjmp mechanism of aborting test runs, add asserts in + * CU_assertImplementation() to trap use outside a registered + * test function during an active test run. (JDS) + * + * 22-Sep-2004 Initial implementation of internal unit tests, added nFailureRecords + * to CU_Run_Summary, added CU_get_n_failure_records(), removed + * requirement for registry to be initialized in order to run + * CU_run_suite() and CU_run_test(). (JDS) + * + * 30-Apr-2005 Added callback for suite cleanup function failure, + * updated unit tests. (JDS) + * + * 23-Apr-2006 Added testing for suite/test deactivation, changing functions. + * Moved doxygen comments for public functions into header. + * Added type marker to CU_FailureRecord. + * Added support for tracking inactive suites/tests. (JDS) + * + * 02-May-2006 Added internationalization hooks. (JDS) + * + * 02-Jun-2006 Added support for elapsed time. Added handlers for suite + * start and complete events. Reworked test run routines to + * better support these features, suite/test activation. (JDS) + */ + +/** @file + * Test run management functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "MyMem.h" +#include "TestDB.h" +#include "TestRun.h" +#include "Util.h" +#include "CUnit_intl.h" + +#define MAX_CMD_LEN 100 + +//Extern declaration +extern int collect_syslog; + +//Callback fn declaration +typedef void (*cunit_logger_cb)(void *fn); + +/*================================================================= + * Global/Static Definitions + *=================================================================*/ +static CU_BOOL f_bTestIsRunning = CU_FALSE; /**< Flag for whether a test run is in progress */ +static CU_pSuite f_pCurSuite = NULL; /**< Pointer to the suite currently being run. */ +static CU_pTest f_pCurTest = NULL; /**< Pointer to the test currently being run. */ + +/** CU_RunSummary to hold results of each test run. */ +static CU_RunSummary f_run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +/** CU_pFailureRecord to hold head of failure record list of each test run. */ +static CU_pFailureRecord f_failure_list = NULL; + +/** CU_pFailureRecord to hold head of failure record list of each test run. */ +static CU_pFailureRecord f_last_failure = NULL; + +/** Flag for whether inactive suites/tests are treated as failures. */ +static CU_BOOL f_failure_on_inactive = CU_TRUE; + +/** Variable for storage of start time for test run. */ +static clock_t f_start_time; + + +/** Pointer to the function to be called before running a suite. */ +static CU_SuiteStartMessageHandler f_pSuiteStartMessageHandler = NULL; + +/** Pointer to the function to be called before running a test. */ +static CU_TestStartMessageHandler f_pTestStartMessageHandler = NULL; + +/** Pointer to the function to be called after running a test. */ +static CU_TestCompleteMessageHandler f_pTestCompleteMessageHandler = NULL; + +/** Pointer to the function to be called after running a suite. */ +static CU_SuiteCompleteMessageHandler f_pSuiteCompleteMessageHandler = NULL; + +/** Pointer to the function to be called when all tests have been run. */ +static CU_AllTestsCompleteMessageHandler f_pAllTestsCompleteMessageHandler = NULL; + +/** Pointer to the function to be called if a suite initialization function returns an error. */ +static CU_SuiteInitFailureMessageHandler f_pSuiteInitFailureMessageHandler = NULL; + +/** Pointer to the function to be called if a suite cleanup function returns an error. */ +static CU_SuiteCleanupFailureMessageHandler f_pSuiteCleanupFailureMessageHandler = NULL; + +/*================================================================= + * Private function forward declarations + *=================================================================*/ +static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure); +static void cleanup_failure_list(CU_pFailureRecord* ppFailure); +static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary); +static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary); +static void add_failure(CU_pFailureRecord* ppFailure, + CU_pRunSummary pRunSummary, + CU_FailureType type, + unsigned int uiLineNumber, + const char *szCondition, + const char *szFileName, + CU_pSuite pSuite, + CU_pTest pTest); + +/*================================================================= + * Public Interface functions + *=================================================================*/ +CU_BOOL CU_assertImplementation(CU_BOOL bValue, + unsigned int uiLine, + const char *strCondition, + const char *strFile, + const char *strFunction, + CU_BOOL bFatal) +{ + /* not used in current implementation - stop compiler warning */ + CU_UNREFERENCED_PARAMETER(strFunction); + + /* these should always be non-NULL (i.e. a test run is in progress) */ + assert(NULL != f_pCurSuite); + assert(NULL != f_pCurTest); + + ++f_run_summary.nAsserts; + if (CU_FALSE == bValue) { + ++f_run_summary.nAssertsFailed; + add_failure(&f_failure_list, &f_run_summary, CUF_AssertFailed, + uiLine, strCondition, strFile, f_pCurSuite, f_pCurTest); + + if ((CU_TRUE == bFatal) && (NULL != f_pCurTest->pJumpBuf)) { + longjmp(*(f_pCurTest->pJumpBuf), 1); + } + } + + return bValue; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartHandler) +{ + f_pSuiteStartMessageHandler = pSuiteStartHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartHandler) +{ + f_pTestStartMessageHandler = pTestStartHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteHandler) +{ + f_pTestCompleteMessageHandler = pTestCompleteHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteHandler) +{ + f_pSuiteCompleteMessageHandler = pSuiteCompleteHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteHandler) +{ + f_pAllTestsCompleteMessageHandler = pAllTestsCompleteHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureHandler) +{ + f_pSuiteInitFailureMessageHandler = pSuiteInitFailureHandler; +} + +/*------------------------------------------------------------------------*/ +void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureHandler) +{ + f_pSuiteCleanupFailureMessageHandler = pSuiteCleanupFailureHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteStartMessageHandler CU_get_suite_start_handler(void) +{ + return f_pSuiteStartMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_TestStartMessageHandler CU_get_test_start_handler(void) +{ + return f_pTestStartMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_TestCompleteMessageHandler CU_get_test_complete_handler(void) +{ + return f_pTestCompleteMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteCompleteMessageHandler CU_get_suite_complete_handler(void) +{ + return f_pSuiteCompleteMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_AllTestsCompleteMessageHandler CU_get_all_test_complete_handler(void) +{ + return f_pAllTestsCompleteMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteInitFailureMessageHandler CU_get_suite_init_failure_handler(void) +{ + return f_pSuiteInitFailureMessageHandler; +} + +/*------------------------------------------------------------------------*/ +CU_SuiteCleanupFailureMessageHandler CU_get_suite_cleanup_failure_handler(void) +{ + return f_pSuiteCleanupFailureMessageHandler; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_suites_run(void) +{ + return f_run_summary.nSuitesRun; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_suites_failed(void) +{ + return f_run_summary.nSuitesFailed; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_suites_inactive(void) +{ + return f_run_summary.nSuitesInactive; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_tests_run(void) +{ + return f_run_summary.nTestsRun; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_tests_failed(void) +{ + return f_run_summary.nTestsFailed; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_tests_inactive(void) +{ + return f_run_summary.nTestsInactive; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_asserts(void) +{ + return f_run_summary.nAsserts; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_successes(void) +{ + return (f_run_summary.nAsserts - f_run_summary.nAssertsFailed); +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_failures(void) +{ + return f_run_summary.nAssertsFailed; +} + +/*------------------------------------------------------------------------*/ +unsigned int CU_get_number_of_failure_records(void) +{ + return f_run_summary.nFailureRecords; +} + +/*------------------------------------------------------------------------*/ +double CU_get_elapsed_time(void) +{ + if (CU_TRUE == f_bTestIsRunning) { + return ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + } + else { + return f_run_summary.ElapsedTime; + } +} + +/*------------------------------------------------------------------------*/ +CU_pFailureRecord CU_get_failure_list(void) +{ + return f_failure_list; +} + +/*------------------------------------------------------------------------*/ +CU_pRunSummary CU_get_run_summary(void) +{ + return &f_run_summary; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_run_all_tests(void) +{ + CU_pTestRegistry pRegistry = CU_get_registry(); + CU_pSuite pSuite = NULL; + CU_ErrorCode result = CUE_SUCCESS; + CU_ErrorCode result2; + + /* Clear results from the previous run */ + clear_previous_results(&f_run_summary, &f_failure_list); + + if (NULL == pRegistry) { + result = CUE_NOREGISTRY; + } + else { + /* test run is starting - set flag */ + f_bTestIsRunning = CU_TRUE; + f_start_time = clock(); + + pSuite = pRegistry->pSuite; + while ((NULL != pSuite) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { + result2 = run_single_suite(pSuite, &f_run_summary); + result = (CUE_SUCCESS == result) ? result2 : result; /* result = 1st error encountered */ + pSuite = pSuite->pNext; + } + + /* test run is complete - clear flag */ + f_bTestIsRunning = CU_FALSE; + f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + + if (NULL != f_pAllTestsCompleteMessageHandler) { + (*f_pAllTestsCompleteMessageHandler)(f_failure_list); + } + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_run_suite(CU_pSuite pSuite) +{ + CU_ErrorCode result = CUE_SUCCESS; + + /* Clear results from the previous run */ + clear_previous_results(&f_run_summary, &f_failure_list); + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else { + /* test run is starting - set flag */ + f_bTestIsRunning = CU_TRUE; + f_start_time = clock(); + + result = run_single_suite(pSuite, &f_run_summary); + + /* test run is complete - clear flag */ + f_bTestIsRunning = CU_FALSE; + f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + + /* run handler for overall completion, if any */ + if (NULL != f_pAllTestsCompleteMessageHandler) { + (*f_pAllTestsCompleteMessageHandler)(f_failure_list); + } + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest) +{ + CU_ErrorCode result = CUE_SUCCESS; + CU_ErrorCode result2; + + /* Clear results from the previous run */ + clear_previous_results(&f_run_summary, &f_failure_list); + + if (NULL == pSuite) { + result = CUE_NOSUITE; + } + else if (NULL == pTest) { + result = CUE_NOTEST; + } + else if (CU_FALSE == pSuite->fActive) { + f_run_summary.nSuitesInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, + 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); + } + result = CUE_SUITE_INACTIVE; + } + else if ((NULL == pTest->pName) || (NULL == CU_get_test_by_name(pTest->pName, pSuite))) { + result = CUE_TEST_NOT_IN_SUITE; + } + else { + /* test run is starting - set flag */ + f_bTestIsRunning = CU_TRUE; + f_start_time = clock(); + + f_pCurTest = NULL; + f_pCurSuite = pSuite; + + /* run handler for suite start, if any */ + if (NULL != f_pSuiteStartMessageHandler) { + (*f_pSuiteStartMessageHandler)(pSuite); + } + + /* run the suite initialization function, if any */ + if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { + /* init function had an error - call handler, if any */ + if (NULL != f_pSuiteInitFailureMessageHandler) { + (*f_pSuiteInitFailureMessageHandler)(pSuite); + } + f_run_summary.nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, + _("Suite Initialization failed - Suite Skipped"), + _("CUnit System"), pSuite, NULL); + result = CUE_SINIT_FAILED; + } + /* reach here if no suite initialization, or if it succeeded */ + else { + result2 = run_single_test(pTest, &f_run_summary); + result = (CUE_SUCCESS == result) ? result2 : result; + + /* run the suite cleanup function, if any */ + if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { + /* cleanup function had an error - call handler, if any */ + if (NULL != f_pSuiteCleanupFailureMessageHandler) { + (*f_pSuiteCleanupFailureMessageHandler)(pSuite); + } + f_run_summary.nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, + 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); + result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; + } + } + + /* run handler for suite completion, if any */ + if (NULL != f_pSuiteCompleteMessageHandler) { + (*f_pSuiteCompleteMessageHandler)(pSuite, NULL); + } + + /* test run is complete - clear flag */ + f_bTestIsRunning = CU_FALSE; + f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; + + /* run handler for overall completion, if any */ + if (NULL != f_pAllTestsCompleteMessageHandler) { + (*f_pAllTestsCompleteMessageHandler)(f_failure_list); + } + + f_pCurSuite = NULL; + } + + CU_set_error(result); + return result; +} + +/*------------------------------------------------------------------------*/ +void CU_clear_previous_results(void) +{ + clear_previous_results(&f_run_summary, &f_failure_list); +} + +/*------------------------------------------------------------------------*/ +CU_pSuite CU_get_current_suite(void) +{ + return f_pCurSuite; +} + +/*------------------------------------------------------------------------*/ +CU_pTest CU_get_current_test(void) +{ + return f_pCurTest; +} + +/*------------------------------------------------------------------------*/ +CU_BOOL CU_is_test_running(void) +{ + return f_bTestIsRunning; +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive) +{ + f_failure_on_inactive = new_inactive; +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void) +{ + return f_failure_on_inactive; +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT void CU_print_run_results(FILE *file) +{ + char *summary_string; + + assert(NULL != file); + summary_string = CU_get_run_results_string(); + if (NULL != summary_string) { + fprintf(file, "%s", summary_string); + CU_FREE(summary_string); + } + else { + fprintf(file, _("An error occurred printing the run results.")); + } +} + +/*------------------------------------------------------------------------*/ +CU_EXPORT char * CU_get_run_results_string(void) + +{ + CU_pRunSummary pRunSummary = &f_run_summary; + CU_pTestRegistry pRegistry = CU_get_registry(); + int width[9]; + size_t len; + char *result; + + assert(NULL != pRunSummary); + assert(NULL != pRegistry); + + width[0] = strlen(_("Run Summary:")); + width[1] = CU_MAX(6, + CU_MAX(strlen(_("Type")), + CU_MAX(strlen(_("suites")), + CU_MAX(strlen(_("tests")), + strlen(_("asserts")))))) + 1; + width[2] = CU_MAX(6, + CU_MAX(strlen(_("Total")), + CU_MAX(CU_number_width(pRegistry->uiNumberOfSuites), + CU_MAX(CU_number_width(pRegistry->uiNumberOfTests), + CU_number_width(pRunSummary->nAsserts))))) + 1; + width[3] = CU_MAX(6, + CU_MAX(strlen(_("Ran")), + CU_MAX(CU_number_width(pRunSummary->nSuitesRun), + CU_MAX(CU_number_width(pRunSummary->nTestsRun), + CU_number_width(pRunSummary->nAsserts))))) + 1; + width[4] = CU_MAX(6, + CU_MAX(strlen(_("Passed")), + CU_MAX(strlen(_("n/a")), + CU_MAX(CU_number_width(pRunSummary->nTestsRun - pRunSummary->nTestsFailed), + CU_number_width(pRunSummary->nAsserts - pRunSummary->nAssertsFailed))))) + 1; + width[5] = CU_MAX(6, + CU_MAX(strlen(_("Failed")), + CU_MAX(CU_number_width(pRunSummary->nSuitesFailed), + CU_MAX(CU_number_width(pRunSummary->nTestsFailed), + CU_number_width(pRunSummary->nAssertsFailed))))) + 1; + width[6] = CU_MAX(6, + CU_MAX(strlen(_("Inactive")), + CU_MAX(CU_number_width(pRunSummary->nSuitesInactive), + CU_MAX(CU_number_width(pRunSummary->nTestsInactive), + strlen(_("n/a")))))) + 1; + + width[7] = strlen(_("Elapsed time = ")); + width[8] = strlen(_(" seconds")); + + len = 13 + 4*(width[0] + width[1] + width[2] + width[3] + width[4] + width[5] + width[6]) + width[7] + width[8] + 1; + result = (char *)CU_MALLOC(len); + + if (NULL != result) { + snprintf(result, len, "%*s%*s%*s%*s%*s%*s%*s\n" /* if you change this, be sure */ + "%*s%*s%*u%*u%*s%*u%*u\n" /* to change the calculation of */ + "%*s%*s%*u%*u%*u%*u%*u\n" /* len above! */ + "%*s%*s%*u%*u%*u%*u%*s\n\n" + "%*s%8.3f%*s", + width[0], _("Run Summary:"), + width[1], _("Type"), + width[2], _("Total"), + width[3], _("Ran"), + width[4], _("Passed"), + width[5], _("Failed"), + width[6], _("Inactive"), + width[0], " ", + width[1], _("suites"), + width[2], pRegistry->uiNumberOfSuites, + width[3], pRunSummary->nSuitesRun, + width[4], _("n/a"), + width[5], pRunSummary->nSuitesFailed, + width[6], pRunSummary->nSuitesInactive, + width[0], " ", + width[1], _("tests"), + width[2], pRegistry->uiNumberOfTests, + width[3], pRunSummary->nTestsRun, + width[4], pRunSummary->nTestsRun - pRunSummary->nTestsFailed, + width[5], pRunSummary->nTestsFailed, + width[6], pRunSummary->nTestsInactive, + width[0], " ", + width[1], _("asserts"), + width[2], pRunSummary->nAsserts, + width[3], pRunSummary->nAsserts, + width[4], pRunSummary->nAsserts - pRunSummary->nAssertsFailed, + width[5], pRunSummary->nAssertsFailed, + width[6], _("n/a"), + width[7], _("Elapsed time = "), CU_get_elapsed_time(), /* makes sure time is updated */ + width[8], _(" seconds") + ); + result[len-1] = '\0'; + } + return result; +} + +/*================================================================= + * Static Function Definitions + *=================================================================*/ +/** + * Records a runtime failure. + * This function is called whenever a runtime failure occurs. + * This includes user assertion failures, suite initialization and + * cleanup failures, and inactive suites/tests when set as failures. + * This function records the details of the failure in a new + * failure record in the linked list of runtime failures. + * + * @param ppFailure Pointer to head of linked list of failure + * records to append with new failure record. + * If it points to a NULL pointer, it will be set + * to point to the new failure record. + * @param pRunSummary Pointer to CU_RunSummary keeping track of failure records + * (ignored if NULL). + * @param type Type of failure. + * @param uiLineNumber Line number of the failure, if applicable. + * @param szCondition Description of failure condition + * @param szFileName Name of file, if applicable + * @param pSuite The suite being run at time of failure + * @param pTest The test being run at time of failure + */ +static void add_failure(CU_pFailureRecord* ppFailure, + CU_pRunSummary pRunSummary, + CU_FailureType type, + unsigned int uiLineNumber, + const char *szCondition, + const char *szFileName, + CU_pSuite pSuite, + CU_pTest pTest) +{ + CU_pFailureRecord pFailureNew = NULL; + CU_pFailureRecord pTemp = NULL; + + assert(NULL != ppFailure); + + pFailureNew = (CU_pFailureRecord)CU_MALLOC(sizeof(CU_FailureRecord)); + + if (NULL == pFailureNew) { + return; + } + + pFailureNew->strFileName = NULL; + pFailureNew->strCondition = NULL; + if (NULL != szFileName) { + pFailureNew->strFileName = (char*)CU_MALLOC(strlen(szFileName) + 1); + if(NULL == pFailureNew->strFileName) { + CU_FREE(pFailureNew); + return; + } + strcpy(pFailureNew->strFileName, szFileName); + } + + if (NULL != szCondition) { + pFailureNew->strCondition = (char*)CU_MALLOC(strlen(szCondition) + 1); + if (NULL == pFailureNew->strCondition) { + if(NULL != pFailureNew->strFileName) { + CU_FREE(pFailureNew->strFileName); + } + CU_FREE(pFailureNew); + return; + } + strcpy(pFailureNew->strCondition, szCondition); + } + + pFailureNew->type = type; + pFailureNew->uiLineNumber = uiLineNumber; + pFailureNew->pTest = pTest; + pFailureNew->pSuite = pSuite; + pFailureNew->pNext = NULL; + pFailureNew->pPrev = NULL; + + pTemp = *ppFailure; + if (NULL != pTemp) { + while (NULL != pTemp->pNext) { + pTemp = pTemp->pNext; + } + pTemp->pNext = pFailureNew; + pFailureNew->pPrev = pTemp; + } + else { + *ppFailure = pFailureNew; + } + + if (NULL != pRunSummary) { + ++(pRunSummary->nFailureRecords); + } + f_last_failure = pFailureNew; +} + +/* + * Local function for result set initialization/cleanup. + */ +/*------------------------------------------------------------------------*/ +/** + * Initializes the run summary information in the specified structure. + * Resets the run counts to zero, and calls cleanup_failure_list() if + * failures were recorded by the last test run. Calling this function + * multiple times, while inefficient, will not cause an error condition. + * + * @param pRunSummary CU_RunSummary to initialize (non-NULL). + * @param ppFailure The failure record to clean (non-NULL). + * @see CU_clear_previous_results() + */ +static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure) +{ + assert(NULL != pRunSummary); + assert(NULL != ppFailure); + + pRunSummary->nSuitesRun = 0; + pRunSummary->nSuitesFailed = 0; + pRunSummary->nSuitesInactive = 0; + pRunSummary->nTestsRun = 0; + pRunSummary->nTestsFailed = 0; + pRunSummary->nTestsInactive = 0; + pRunSummary->nAsserts = 0; + pRunSummary->nAssertsFailed = 0; + pRunSummary->nFailureRecords = 0; + pRunSummary->ElapsedTime = 0.0; + + if (NULL != *ppFailure) { + cleanup_failure_list(ppFailure); + } + + f_last_failure = NULL; +} + +/*------------------------------------------------------------------------*/ +/** + * Frees all memory allocated for the linked list of test failure + * records. pFailure is reset to NULL after its list is cleaned up. + * + * @param ppFailure Pointer to head of linked list of + * CU_pFailureRecords to clean. + * @see CU_clear_previous_results() + */ +static void cleanup_failure_list(CU_pFailureRecord* ppFailure) +{ + CU_pFailureRecord pCurFailure = NULL; + CU_pFailureRecord pNextFailure = NULL; + + pCurFailure = *ppFailure; + + while (NULL != pCurFailure) { + + if (NULL != pCurFailure->strCondition) { + CU_FREE(pCurFailure->strCondition); + } + + if (NULL != pCurFailure->strFileName) { + CU_FREE(pCurFailure->strFileName); + } + + pNextFailure = pCurFailure->pNext; + CU_FREE(pCurFailure); + pCurFailure = pNextFailure; + } + + *ppFailure = NULL; +} + + +/* Function to collect syslog for a test */ +//FIXME find another bettwer way to do this, It is currentlly leaving tail procs in ps +void cunit_logger_fn(void *fn){ + char sys_cmd[MAX_CMD_LEN] = {'\0'}; + sprintf(sys_cmd, "tail -f /var/log/syslog > %s.txt", (char *)fn); + system("service syslog restart"); + system(sys_cmd); +} + +/*------------------------------------------------------------------------*/ +/** + * Runs all tests in a specified suite. + * Internal function to run all tests in a suite. The suite need + * not be registered in the test registry to be run. Only + * suites having their fActive flags set CU_TRUE will actually be + * run. If the CUnit framework is in an error condition after + * running a test, no additional tests are run. + * + * @param pSuite The suite containing the test (non-NULL). + * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). + * @return A CU_ErrorCode indicating the status of the run. + * @see CU_run_suite() for public interface function. + * @see CU_run_all_tests() for running all suites. + */ +static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary) +{ + CU_pTest pTest = NULL; + unsigned int nStartFailures; + /* keep track of the last failure BEFORE running the test */ + CU_pFailureRecord pLastFailure = f_last_failure; + CU_ErrorCode result = CUE_SUCCESS; + CU_ErrorCode result2; + + assert(NULL != pSuite); + assert(NULL != pRunSummary); + + nStartFailures = pRunSummary->nFailureRecords; + + f_pCurTest = NULL; + f_pCurSuite = pSuite; + + /* run handler for suite start, if any */ + if (NULL != f_pSuiteStartMessageHandler) { + (*f_pSuiteStartMessageHandler)(pSuite); + } + + /* run suite if it's active */ + if (CU_FALSE != pSuite->fActive) { + + /* run the suite initialization function, if any */ + if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { + /* init function had an error - call handler, if any */ + if (NULL != f_pSuiteInitFailureMessageHandler) { + (*f_pSuiteInitFailureMessageHandler)(pSuite); + } + pRunSummary->nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, + _("Suite Initialization failed - Suite Skipped"), + _("CUnit System"), pSuite, NULL); + result = CUE_SINIT_FAILED; + } + + /* reach here if no suite initialization, or if it succeeded */ + else { + pTest = pSuite->pTest; + while ((NULL != pTest) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { + if (CU_FALSE != pTest->fActive) { + result2 = run_single_test(pTest, pRunSummary); + result = (CUE_SUCCESS == result) ? result2 : result; + } + else { + f_run_summary.nTestsInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, + 0, _("Test inactive"), _("CUnit System"), pSuite, pTest); + result = CUE_TEST_INACTIVE; + } + } + pTest = pTest->pNext; + } + pRunSummary->nSuitesRun++; + + /* call the suite cleanup function, if any */ + if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { + if (NULL != f_pSuiteCleanupFailureMessageHandler) { + (*f_pSuiteCleanupFailureMessageHandler)(pSuite); + } + pRunSummary->nSuitesFailed++; + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, + 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); + result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; + } + } + } + + /* otherwise record inactive suite and failure if appropriate */ + else { + f_run_summary.nSuitesInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, + 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); + result = CUE_SUITE_INACTIVE; + } + } + + /* if additional failures have occurred... */ + if (pRunSummary->nFailureRecords > nStartFailures) { + if (NULL != pLastFailure) { + pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ + } + else { + pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ + } + } + else { + pLastFailure = NULL; /* no additional failure - set to NULL */ + } + + /* run handler for suite completion, if any */ + if (NULL != f_pSuiteCompleteMessageHandler) { + (*f_pSuiteCompleteMessageHandler)(pSuite, pLastFailure); + } + + f_pCurSuite = NULL; + return result; +} + +/*------------------------------------------------------------------------*/ +/** + * Runs a specific test. + * Internal function to run a test case. This includes calling + * any handler to be run before executing the test, running the + * test's function (if any), and calling any handler to be run + * after executing a test. Suite initialization and cleanup functions + * are not called by this function. A current suite must be set and + * active (checked by assertion). + * + * @param pTest The test to be run (non-NULL). + * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). + * @return A CU_ErrorCode indicating the status of the run. + * @see CU_run_test() for public interface function. + * @see CU_run_all_tests() for running all suites. + */ +static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary) +{ + volatile unsigned int nStartFailures; + pthread_t tid; + int rv = 0; + cunit_logger_cb cb = cunit_logger_fn; + /* keep track of the last failure BEFORE running the test */ + volatile CU_pFailureRecord pLastFailure = f_last_failure; + jmp_buf buf; + CU_ErrorCode result = CUE_SUCCESS; + + assert(NULL != f_pCurSuite); + assert(CU_FALSE != f_pCurSuite->fActive); + assert(NULL != pTest); + assert(NULL != pRunSummary); + + nStartFailures = pRunSummary->nFailureRecords; + + f_pCurTest = pTest; + + if (NULL != f_pTestStartMessageHandler) { + (*f_pTestStartMessageHandler)(f_pCurTest, f_pCurSuite); + } + + /* run test if it is active */ + if (CU_FALSE != pTest->fActive) { + + /* set jmp_buf and run test */ + pTest->pJumpBuf = &buf; + if (0 == setjmp(buf)) { + if (NULL != pTest->pTestFunc) { + if (collect_syslog) {//FIXME Currently WIP, It is not reliable for huge batch runs now + rv = pthread_create(&tid, NULL, (void *)cb, (void *)pTest->pName); + if (rv) { + fprintf(stdout, "thread create failed for collecting syslog for %s\n",(char *)pTest->pName); + } + } + (*pTest->pTestFunc)(); + if (collect_syslog){ + sleep(1); + pthread_cancel(tid); + } + } + } + + pRunSummary->nTestsRun++; + } + else { + f_run_summary.nTestsInactive++; + if (CU_FALSE != f_failure_on_inactive) { + add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, + 0, _("Test inactive"), _("CUnit System"), f_pCurSuite, f_pCurTest); + } + result = CUE_TEST_INACTIVE; + } + + /* if additional failures have occurred... */ + if (pRunSummary->nFailureRecords > nStartFailures) { + pRunSummary->nTestsFailed++; + if (NULL != pLastFailure) { + pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ + } + else { + pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ + } + } + else { + pLastFailure = NULL; /* no additional failure - set to NULL */ + } + + if (NULL != f_pTestCompleteMessageHandler) { + (*f_pTestCompleteMessageHandler)(f_pCurTest, f_pCurSuite, pLastFailure); + } + + pTest->pJumpBuf = NULL; + f_pCurTest = NULL; + + return result; +} + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +/** Types of framework events tracked by test system. */ +typedef enum TET { + SUITE_START = 1, + TEST_START, + TEST_COMPLETE, + SUITE_COMPLETE, + ALL_TESTS_COMPLETE, + SUITE_INIT_FAILED, + SUITE_CLEANUP_FAILED +} TestEventType; + +/** Test event structure for recording details of a framework event. */ +typedef struct TE { + TestEventType type; + CU_pSuite pSuite; + CU_pTest pTest; + CU_pFailureRecord pFailure; + struct TE * pNext; +} TestEvent, * pTestEvent; + +static int f_nTestEvents = 0; +static pTestEvent f_pFirstEvent = NULL; + +/** Creates & stores a test event record having the specified details. */ +static void add_test_event(TestEventType type, CU_pSuite psuite, + CU_pTest ptest, CU_pFailureRecord pfailure) +{ + pTestEvent pNewEvent = (pTestEvent)malloc(sizeof(TestEvent)); + pTestEvent pNextEvent = f_pFirstEvent; + + if (NULL == pNewEvent) { + fprintf(stderr, "Memory allocation failed in add_test_event()."); + exit(1); + } + + pNewEvent->type = type; + pNewEvent->pSuite = psuite; + pNewEvent->pTest = ptest; + pNewEvent->pFailure = pfailure; + pNewEvent->pNext = NULL; + + if (pNextEvent) { + while (pNextEvent->pNext) { + pNextEvent = pNextEvent->pNext; + } + pNextEvent->pNext = pNewEvent; + } + else { + f_pFirstEvent = pNewEvent; + } + ++f_nTestEvents; +} + +/** Deallocates all test event data. */ +static void clear_test_events(void) +{ + pTestEvent pCurrentEvent = f_pFirstEvent; + pTestEvent pNextEvent = NULL; + + while (pCurrentEvent) { + pNextEvent = pCurrentEvent->pNext; + free(pCurrentEvent); + pCurrentEvent = pNextEvent; + } + + f_pFirstEvent = NULL; + f_nTestEvents = 0; +} + +static void suite_start_handler(const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(NULL == CU_get_current_test()); + + add_test_event(SUITE_START, pSuite, NULL, NULL); +} + +static void test_start_handler(const CU_pTest pTest, const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(pTest == CU_get_current_test()); + + add_test_event(TEST_START, pSuite, pTest, NULL); +} + +static void test_complete_handler(const CU_pTest pTest, const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(pTest == CU_get_current_test()); + + add_test_event(TEST_COMPLETE, pSuite, pTest, pFailure); +} + +static void suite_complete_handler(const CU_pSuite pSuite, + const CU_pFailureRecord pFailure) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + TEST(NULL == CU_get_current_test()); + + add_test_event(SUITE_COMPLETE, pSuite, NULL, pFailure); +} + +static void test_all_complete_handler(const CU_pFailureRecord pFailure) +{ + TEST(!CU_is_test_running()); + + add_test_event(ALL_TESTS_COMPLETE, NULL, NULL, pFailure); +} + +static void suite_init_failure_handler(const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + + add_test_event(SUITE_INIT_FAILED, pSuite, NULL, NULL); +} + +static void suite_cleanup_failure_handler(const CU_pSuite pSuite) +{ + TEST(CU_is_test_running()); + TEST(pSuite == CU_get_current_suite()); + + add_test_event(SUITE_CLEANUP_FAILED, pSuite, NULL, NULL); +} + +/** + * Centralize test result testing - we're going to do it a lot! + * This is messy since we want to report the calling location upon failure. + * + * Via calling test functions tests: + * CU_get_number_of_suites_run() + * CU_get_number_of_suites_failed() + * CU_get_number_of_tests_run() + * CU_get_number_of_tests_failed() + * CU_get_number_of_asserts() + * CU_get_number_of_successes() + * CU_get_number_of_failures() + * CU_get_number_of_failure_records() + * CU_get_run_summary() + */ +static void do_test_results(unsigned int nSuitesRun, + unsigned int nSuitesFailed, + unsigned int nSuitesInactive, + unsigned int nTestsRun, + unsigned int nTestsFailed, + unsigned int nTestsInactive, + unsigned int nAsserts, + unsigned int nSuccesses, + unsigned int nFailures, + unsigned int nFailureRecords, + const char *file, + unsigned int line) +{ + char msg[500]; + CU_pRunSummary pRunSummary = NULL; + + if (nSuitesRun == CU_get_number_of_suites_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", + nSuitesRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nSuitesInactive == CU_get_number_of_suites_inactive()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_inactive() (called from %s:%u)", + nSuitesInactive, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nSuitesFailed == CU_get_number_of_suites_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", + nSuitesFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nTestsRun == CU_get_number_of_tests_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", + nTestsRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nTestsFailed == CU_get_number_of_tests_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", + nTestsFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nTestsInactive == CU_get_number_of_tests_inactive()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_inactive() (called from %s:%u)", + nTestsInactive, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nAsserts == CU_get_number_of_asserts()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", + nAsserts, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nSuccesses == CU_get_number_of_successes()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_successes() (called from %s:%u)", + nSuccesses, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nFailures == CU_get_number_of_failures()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", + nFailures, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (nFailureRecords == CU_get_number_of_failure_records()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", + nFailureRecords, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + pRunSummary = CU_get_run_summary(); + + if (pRunSummary->nSuitesRun == CU_get_number_of_suites_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", + pRunSummary->nSuitesRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nSuitesFailed == CU_get_number_of_suites_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", + pRunSummary->nSuitesFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nTestsRun == CU_get_number_of_tests_run()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", + pRunSummary->nTestsRun, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nTestsFailed == CU_get_number_of_tests_failed()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", + pRunSummary->nTestsFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nAsserts == CU_get_number_of_asserts()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", + pRunSummary->nAsserts, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nAssertsFailed == CU_get_number_of_failures()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", + pRunSummary->nAssertsFailed, file, line); + msg[499] = '\0'; + FAIL(msg); + } + + if (pRunSummary->nFailureRecords == CU_get_number_of_failure_records()) { + PASS(); + } else { + snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", + pRunSummary->nFailureRecords, file, line); + msg[499] = '\0'; + FAIL(msg); + } +} + +#define test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ + nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords) \ + do_test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ + nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords, \ + __FILE__, __LINE__) + +static void test_succeed(void) { CU_TEST(CU_TRUE); } +static void test_fail(void) { CU_TEST(CU_FALSE); } +static int suite_succeed(void) { return 0; } +static int suite_fail(void) { return 1; } + +/*-------------------------------------------------*/ +/* tests: + * CU_set_suite_start_handler() + * CU_set_test_start_handler() + * CU_set_test_complete_handler() + * CU_set_suite_complete_handler() + * CU_set_all_test_complete_handler() + * CU_set_suite_init_failure_handler() + * CU_set_suite_cleanup_failure_handler() + * CU_get_suite_start_handler() + * CU_get_test_start_handler() + * CU_get_test_complete_handler() + * CU_get_suite_complete_handler() + * CU_get_all_test_complete_handler() + * CU_get_suite_init_failure_handler() + * CU_get_suite_cleanup_failure_handler() + * CU_is_test_running() + * via handlers tests: + * CU_get_current_suite() + * CU_get_current_test() + */ +static void test_message_handlers(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + pTestEvent pEvent = NULL; + + TEST(!CU_is_test_running()); + + /* handlers should be NULL on startup */ + TEST(NULL == CU_get_suite_start_handler()); + TEST(NULL == CU_get_test_start_handler()); + TEST(NULL == CU_get_test_complete_handler()); + TEST(NULL == CU_get_suite_complete_handler()); + TEST(NULL == CU_get_all_test_complete_handler()); + TEST(NULL == CU_get_suite_init_failure_handler()); + TEST(NULL == CU_get_suite_cleanup_failure_handler()); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test3", test_succeed); + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest4 = CU_add_test(pSuite2, "test4", test_succeed); + pSuite3 = CU_add_suite("suite3", suite_succeed, suite_fail); + pTest5 = CU_add_test(pSuite3, "test5", test_fail); + + TEST_FATAL(CUE_SUCCESS == CU_get_error()); + + /* first run tests without handlers set */ + clear_test_events(); + CU_run_all_tests(); + + TEST(0 == f_nTestEvents); + TEST(NULL == f_pFirstEvent); + test_results(2,2,0,4,2,0,4,2,2,4); + + /* set handlers to local functions */ + CU_set_suite_start_handler(&suite_start_handler); + CU_set_test_start_handler(&test_start_handler); + CU_set_test_complete_handler(&test_complete_handler); + CU_set_suite_complete_handler(&suite_complete_handler); + CU_set_all_test_complete_handler(&test_all_complete_handler); + CU_set_suite_init_failure_handler(&suite_init_failure_handler); + CU_set_suite_cleanup_failure_handler(&suite_cleanup_failure_handler); + + /* confirm handlers set properly */ + TEST(suite_start_handler == CU_get_suite_start_handler()); + TEST(test_start_handler == CU_get_test_start_handler()); + TEST(test_complete_handler == CU_get_test_complete_handler()); + TEST(suite_complete_handler == CU_get_suite_complete_handler()); + TEST(test_all_complete_handler == CU_get_all_test_complete_handler()); + TEST(suite_init_failure_handler == CU_get_suite_init_failure_handler()); + TEST(suite_cleanup_failure_handler == CU_get_suite_cleanup_failure_handler()); + + /* run tests again with handlers set */ + clear_test_events(); + CU_run_all_tests(); + + TEST(17 == f_nTestEvents); + if (17 == f_nTestEvents) { + pEvent = f_pFirstEvent; + TEST(SUITE_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest1 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest1 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest2 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest2 == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest3 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(pTest3 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_COMPLETE == pEvent->type); + TEST(pSuite1 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_START == pEvent->type); + TEST(pSuite2 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_INIT_FAILED == pEvent->type); + TEST(pSuite2 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_COMPLETE == pEvent->type); + TEST(pSuite2 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_START == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_START == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(pTest5 == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(TEST_COMPLETE == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(pTest5 == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_CLEANUP_FAILED == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL == pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(SUITE_COMPLETE == pEvent->type); + TEST(pSuite3 == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + + pEvent = pEvent->pNext; + TEST(ALL_TESTS_COMPLETE == pEvent->type); + TEST(NULL == pEvent->pSuite); + TEST(NULL == pEvent->pTest); + TEST(NULL != pEvent->pFailure); + if (4 == CU_get_number_of_failure_records()) { + TEST(NULL != pEvent->pFailure->pNext); + TEST(NULL != pEvent->pFailure->pNext->pNext); + TEST(NULL != pEvent->pFailure->pNext->pNext->pNext); + TEST(NULL == pEvent->pFailure->pNext->pNext->pNext->pNext); + } + TEST(pEvent->pFailure == CU_get_failure_list()); + } + + test_results(2,2,0,4,2,0,4,2,2,4); + + /* clear handlers and run again */ + CU_set_suite_start_handler(NULL); + CU_set_test_start_handler(NULL); + CU_set_test_complete_handler(NULL); + CU_set_suite_complete_handler(NULL); + CU_set_all_test_complete_handler(NULL); + CU_set_suite_init_failure_handler(NULL); + CU_set_suite_cleanup_failure_handler(NULL); + + TEST(NULL == CU_get_suite_start_handler()); + TEST(NULL == CU_get_test_start_handler()); + TEST(NULL == CU_get_test_complete_handler()); + TEST(NULL == CU_get_suite_complete_handler()); + TEST(NULL == CU_get_all_test_complete_handler()); + TEST(NULL == CU_get_suite_init_failure_handler()); + TEST(NULL == CU_get_suite_cleanup_failure_handler()); + + clear_test_events(); + CU_run_all_tests(); + + TEST(0 == f_nTestEvents); + TEST(NULL == f_pFirstEvent); + test_results(2,2,0,4,2,0,4,2,2,4); + + CU_cleanup_registry(); + clear_test_events(); +} + +static CU_BOOL f_exit_called = CU_FALSE; + +/* intercept exit for testing of CUEA_ABORT action */ +void test_exit(int status) +{ + CU_UNREFERENCED_PARAMETER(status); /* not used */ + f_exit_called = CU_TRUE; +} + + +/*-------------------------------------------------*/ +static void test_CU_fail_on_inactive(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + + CU_set_error_action(CUEA_IGNORE); + CU_initialize_registry(); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest3 = CU_add_test(pSuite2, "test3", test_succeed); + pTest4 = CU_add_test(pSuite2, "test4", test_succeed); + + /* test initial conditions */ + TEST(CU_TRUE == CU_get_fail_on_inactive()); + TEST(CU_TRUE == pSuite1->fActive); + TEST(CU_TRUE == pSuite2->fActive); + TEST(CU_TRUE == pTest1->fActive); + TEST(CU_TRUE == pTest2->fActive); + TEST(CU_TRUE == pTest3->fActive); + TEST(CU_TRUE == pTest4->fActive); + + CU_set_fail_on_inactive(CU_TRUE); + TEST(CU_TRUE == CU_get_fail_on_inactive()); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ + test_results(1,1,0,2,1,0,2,1,1,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CU_FALSE == CU_get_fail_on_inactive()); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); + test_results(1,1,0,2,1,0,2,1,1,2); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* all suites inactive */ + test_results(0,0,2,0,0,0,0,0,0,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(0,0,2,0,0,0,0,0,0,0); + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* some suites inactive */ + test_results(1,0,1,2,1,0,2,1,1,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(1,0,1,2,1,0,2,1,1,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* all tests inactive */ + test_results(1,1,0,0,0,2,0,0,0,3); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); + test_results(1,1,0,0,0,2,0,0,0,1); + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some tests inactive */ + test_results(1,1,0,1,0,1,1,1,0,2); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); + test_results(1,1,0,1,0,1,1,1,0,1); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_test_active(pTest1, CU_FALSE); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some suites & tests inactive */ + test_results(1,0,1,1,1,1,1,0,1,3); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(1,0,1,1,1,1,1,0,1,1); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_test_active(pTest1, CU_TRUE); + + /* clean up */ + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +static void test_CU_run_all_tests(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + CU_pTest pTest10 = NULL; + + /* error - uninitialized registry (CUEA_IGNORE) */ + CU_cleanup_registry(); + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOREGISTRY == CU_run_all_tests()); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* error - uninitialized registry (CUEA_FAIL) */ + CU_cleanup_registry(); + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOREGISTRY == CU_run_all_tests()); + TEST(CUE_NOREGISTRY == CU_get_error()); + + /* error - uninitialized registry (CUEA_ABORT) */ + CU_cleanup_registry(); + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_all_tests(); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* run with no suites or tests registered */ + CU_initialize_registry(); + + CU_set_error_action(CUEA_IGNORE); + TEST(CUE_SUCCESS == CU_run_all_tests()); + test_results(0,0,0,0,0,0,0,0,0,0); + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name OK */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test9", test_succeed); + pSuite4 = CU_add_suite("suite4", NULL, suite_fail); + pTest10 = CU_add_test(pSuite4, "test10", test_succeed); + + TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); + TEST_FATAL(10 == CU_get_registry()->uiNumberOfTests); + + /* run all tests (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ + test_results(3,2,0,8,3,0,8,5,3,5); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_suite_active(pSuite4, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); /* suites inactive */ + test_results(0,0,4,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + test_results(0,0,4,0,0,0,0,0,0,4); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some suites inactive */ + test_results(1,1,2,2,1,0,2,1,1,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + test_results(1,1,2,2,1,0,2,1,1,4); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ + test_results(3,2,0,0,0,8,0,0,0,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(3,2,0,0,0,8,0,0,0,10); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ + test_results(3,2,0,4,0,4,4,4,0,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(3,2,0,4,0,4,4,4,0,6); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_TRUE); + + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + test_results(2,3,0,3,1,0,3,2,1,4); + + CU_set_suite_cleanupfunc(pSuite4, NULL); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite4, &suite_fail); + test_results(3,1,0,8,3,0,8,5,3,4); + + CU_set_test_func(pTest2, &test_succeed); + CU_set_test_func(pTest4, &test_succeed); + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ + CU_set_test_func(pTest2, &test_fail); + CU_set_test_func(pTest4, &test_fail); + CU_set_test_func(pTest8, &test_fail); + test_results(3,2,0,8,0,0,8,8,0,2); + + /* run all tests (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_suite_active(pSuite4, CU_TRUE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites inactive */ + test_results(2,1,2,6,2,0,6,4,2,3); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + test_results(1,0,1,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ + test_results(1,1,0,0,0,5,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_TRUE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ + test_results(1,1,0,2,2,3,2,0,2,3); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_TRUE); + + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + test_results(4,1,0,10,3,0,10,7,3,4); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_test_func(pTest1, &test_fail); + CU_set_test_func(pTest3, &test_fail); + CU_set_test_func(pTest5, &test_fail); + CU_set_test_func(pTest9, &test_fail); + CU_set_test_func(pTest10, &test_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ + CU_set_test_func(pTest1, &test_succeed); + CU_set_test_func(pTest3, &test_succeed); + CU_set_test_func(pTest5, &test_succeed); + CU_set_test_func(pTest9, &test_succeed); + CU_set_test_func(pTest10, &test_succeed); + test_results(1,1,0,5,5,0,5,0,5,6); + + /* run all tests (CUEA_ABORT) */ + f_exit_called = CU_FALSE; + CU_set_error_action(CUEA_ABORT); + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_suite_active(pSuite4, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_all_tests()); /* no suites active, so no abort() */ + TEST(CU_FALSE == f_exit_called); + test_results(0,0,4,0,0,0,0,0,0,0); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,1); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites active */ + TEST(CU_TRUE == f_exit_called); + test_results(3,1,1,8,3,0,8,5,3,4); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(1,0,1,5,2,0,5,3,2,3); + + CU_set_suite_active(pSuite1, CU_TRUE); + CU_set_suite_active(pSuite2, CU_TRUE); + CU_set_suite_active(pSuite3, CU_TRUE); + CU_set_suite_active(pSuite4, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_FALSE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,5,0,0,0,1); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_FALSE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_test_active(pTest10, CU_TRUE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,2,2,3,2,0,2,3); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); + TEST(CU_TRUE == f_exit_called); + test_results(1,0,0,0,0,1,0,0,0,1); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_test_active(pTest6, CU_TRUE); + CU_set_test_active(pTest7, CU_TRUE); + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_TRUE); + CU_set_test_active(pTest10, CU_TRUE); + + f_exit_called = CU_FALSE; + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,2,0,5,3,2,3); + + f_exit_called = CU_FALSE; + CU_set_test_func(pTest1, &test_fail); + CU_set_test_func(pTest3, &test_fail); + CU_set_test_func(pTest5, &test_fail); + CU_set_test_func(pTest9, &test_fail); + CU_set_test_func(pTest10, &test_fail); + TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ + CU_set_test_func(pTest1, &test_succeed); + CU_set_test_func(pTest3, &test_succeed); + CU_set_test_func(pTest5, &test_succeed); + CU_set_test_func(pTest9, &test_succeed); + CU_set_test_func(pTest10, &test_succeed); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,5,0,5,0,5,6); + + /* clean up after testing */ + CU_set_error_action(CUEA_IGNORE); + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +static void test_CU_run_suite(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pSuite pSuite4 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + + /* error - NULL suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOSUITE == CU_run_suite(NULL)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL suite (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOSUITE == CU_run_suite(NULL)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL suite (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_suite(NULL); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test3", test_succeed); + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test5", test_succeed); + pSuite2 = CU_add_suite("suite1", suite_fail, NULL); /* duplicate suite name OK */ + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite3", NULL, suite_fail); + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ + pSuite4 = CU_add_suite("suite4", NULL, NULL); + + TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); + TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); + + /* run each suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suites/tests active */ + test_results(1,0,0,5,2,0,5,3,2,2); + + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); + test_results(1,1,0,2,1,0,2,1,1,2); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); + test_results(1,0,0,0,0,0,0,0,0,0); + + CU_set_suite_active(pSuite3, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite3)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite3)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite3, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_FALSE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* all tests inactive */ + test_results(1,0,0,0,0,5,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); + test_results(1,0,0,0,0,5,0,0,0,5); + + CU_set_test_active(pTest1, CU_TRUE); + CU_set_test_active(pTest2, CU_FALSE); + CU_set_test_active(pTest3, CU_TRUE); + CU_set_test_active(pTest4, CU_FALSE); + CU_set_test_active(pTest5, CU_TRUE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* some tests inactive */ + test_results(1,0,0,3,0,2,3,3,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); + test_results(1,0,0,3,0,2,3,3,0,2); + CU_set_test_active(pTest2, CU_TRUE); + CU_set_test_active(pTest4, CU_TRUE); + + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + test_results(0,1,0,0,0,0,0,0,0,1); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_test_func(pTest1, &test_fail); + CU_set_test_func(pTest3, &test_fail); + CU_set_test_func(pTest5, &test_fail); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ + CU_set_test_func(pTest1, &test_succeed); + CU_set_test_func(pTest3, &test_succeed); + CU_set_test_func(pTest5, &test_succeed); + test_results(1,0,0,5,5,0,5,0,5,5); + + /* run each suite (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ + test_results(1,0,0,5,2,0,5,3,2,2); + + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); + test_results(1,1,0,2,1,0,2,1,1,2); + + TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); + test_results(1,0,0,0,0,0,0,0,0,0); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite1)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite1, CU_TRUE); + + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ + test_results(1,1,0,0,0,2,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + test_results(1,1,0,0,0,1,0,0,0,2); + + CU_set_test_active(pTest8, CU_TRUE); + CU_set_test_active(pTest9, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ + test_results(1,1,0,1,1,1,1,0,1,2); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + test_results(1,1,0,1,1,1,1,0,1,3); + CU_set_test_active(pTest9, CU_TRUE); + + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + test_results(1,0,0,2,0,0,2,2,0,0); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(1,1,0,5,2,0,5,3,2,3); + + CU_set_test_func(pTest2, &test_succeed); + CU_set_test_func(pTest4, &test_succeed); + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ + CU_set_test_func(pTest2, &test_fail); + CU_set_test_func(pTest4, &test_fail); + test_results(1,0,0,5,0,0,5,5,0,0); + + /* run each suite (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ + TEST(CU_FALSE == f_exit_called); + test_results(1,0,0,5,2,0,5,3,2,2); + + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,2,1,0,2,1,1,2); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); + TEST(CU_FALSE == f_exit_called); + test_results(1,0,0,0,0,0,0,0,0,0); + + CU_set_suite_active(pSuite2, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* suite inactive, but not a failure */ + TEST(CU_FALSE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,0); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite2)); + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_FALSE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,2,0,0,0,1); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,1,0,0,0,2); + + CU_set_test_active(pTest8, CU_FALSE); + CU_set_test_active(pTest9, CU_TRUE); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,1,0,1,1,1,0,1); + f_exit_called = CU_FALSE; + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,0,0,1,0,0,0,2); + CU_set_test_active(pTest8, CU_TRUE); + + f_exit_called = CU_FALSE; + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,5,2,0,5,3,2,3); + + f_exit_called = CU_FALSE; + CU_set_test_func(pTest8, &test_succeed); + CU_set_test_func(pTest9, &test_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + CU_set_test_func(pTest9, &test_succeed); + TEST(CU_TRUE == f_exit_called); + test_results(1,1,0,2,1,0,2,1,1,2); + + /* clean up after testing */ + CU_set_error_action(CUEA_IGNORE); + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +static void test_CU_run_test(void) +{ + CU_pSuite pSuite1 = NULL; + CU_pSuite pSuite2 = NULL; + CU_pSuite pSuite3 = NULL; + CU_pTest pTest1 = NULL; + CU_pTest pTest2 = NULL; + CU_pTest pTest3 = NULL; + CU_pTest pTest4 = NULL; + CU_pTest pTest5 = NULL; + CU_pTest pTest6 = NULL; + CU_pTest pTest7 = NULL; + CU_pTest pTest8 = NULL; + CU_pTest pTest9 = NULL; + + /* register some suites and tests */ + CU_initialize_registry(); + pSuite1 = CU_add_suite("suite1", NULL, NULL); + pTest1 = CU_add_test(pSuite1, "test1", test_succeed); + pTest2 = CU_add_test(pSuite1, "test2", test_fail); + pTest3 = CU_add_test(pSuite1, "test3", test_succeed); + pTest4 = CU_add_test(pSuite1, "test4", test_fail); + pTest5 = CU_add_test(pSuite1, "test5", test_succeed); + pSuite2 = CU_add_suite("suite2", suite_fail, NULL); + pTest6 = CU_add_test(pSuite2, "test6", test_succeed); + pTest7 = CU_add_test(pSuite2, "test7", test_succeed); + pSuite3 = CU_add_suite("suite2", NULL, suite_fail); /* duplicate suite name OK */ + pTest8 = CU_add_test(pSuite3, "test8", test_fail); + pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ + + TEST_FATAL(3 == CU_get_registry()->uiNumberOfSuites); + TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); + + /* error - NULL suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL suite (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); + TEST(CUE_NOSUITE == CU_get_error()); + + /* error - NULL test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_test(NULL, pTest1); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* error - NULL test (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); + TEST(CUE_NOTEST == CU_get_error()); + + /* error - NULL test (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); + TEST(CUE_NOTEST == CU_get_error()); + + /* error - NULL test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_test(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* error - test not in suite (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); + TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); + + /* error - NULL test (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); + TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); + + /* error - NULL test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + CU_run_test(pSuite3, pTest1); + TEST(CU_TRUE == f_exit_called); + f_exit_called = CU_FALSE; + + /* run each test (CUEA_IGNORE) */ + CU_set_error_action(CUEA_IGNORE); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* all suite/tests active */ + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); + test_results(0,1,0,1,1,0,1,0,1,2); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_suite_active(pSuite1, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite1, CU_TRUE); + + CU_set_test_active(pTest1, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); /* test inactive */ + test_results(0,0,0,0,0,1,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); + test_results(0,0,0,0,1,1,0,0,0,1); + CU_set_test_active(pTest1, CU_TRUE); + + CU_set_suite_initfunc(pSuite1, &suite_fail); + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite1, NULL); + test_results(0,1,0,0,0,0,0,0,0,1); + + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + test_results(0,1,0,1,0,0,1,1,0,1); + + /* run each test (CUEA_FAIL) */ + CU_set_error_action(CUEA_FAIL); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* suite/test active */ + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); + test_results(0,0,0,1,1,0,1,0,1,1); + + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); + test_results(0,0,0,1,0,0,1,1,0,0); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + test_results(0,1,0,0,0,0,0,0,0,1); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); + test_results(0,1,0,1,1,0,1,0,1,2); + + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); /* suite inactive */ + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest7, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); /* test inactive */ + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_test_active(pTest7, CU_TRUE); + + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + test_results(0,0,0,1,0,0,1,1,0,0); + + CU_set_suite_cleanupfunc(pSuite3, NULL); + TEST(CUE_SUCCESS == CU_run_test(pSuite3, pTest8)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite3, &suite_fail); + test_results(0,0,0,1,1,0,1,0,1,1); + + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + test_results(0,1,0,1,0,0,1,1,0,1); + + /* run each test (CUEA_ABORT) */ + CU_set_error_action(CUEA_ABORT); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,1,0,1,0,1,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,1,0,1,0,1,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + + f_exit_called = CU_FALSE; + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,1,0,1,0,1,2); + + f_exit_called = CU_FALSE; + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,0,0,1,1,0,1); + + CU_set_suite_active(pSuite2, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + f_exit_called = CU_FALSE; + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); /* suite inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,0); + CU_set_fail_on_inactive(CU_TRUE); + f_exit_called = CU_FALSE; + TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); + TEST(CU_TRUE == f_exit_called); + test_results(0,0,1,0,0,0,0,0,0,1); + CU_set_suite_active(pSuite2, CU_TRUE); + + CU_set_test_active(pTest6, CU_FALSE); + CU_set_fail_on_inactive(CU_FALSE); + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); /* test inactive */ + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_fail_on_inactive(CU_TRUE); + f_exit_called = CU_FALSE; + TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,0,0,0,0,0,0,1); + CU_set_test_active(pTest6, CU_TRUE); + + f_exit_called = CU_FALSE; + CU_set_suite_initfunc(pSuite2, NULL); + TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ + CU_set_suite_initfunc(pSuite2, &suite_fail); + TEST(CU_FALSE == f_exit_called); + test_results(0,0,0,1,0,0,1,1,0,0); + + f_exit_called = CU_FALSE; + CU_set_suite_cleanupfunc(pSuite1, &suite_fail); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ + CU_set_suite_cleanupfunc(pSuite1, NULL); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,0,0,1,1,0,1); + + f_exit_called = CU_FALSE; + CU_set_test_func(pTest8, &test_succeed); + TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ + CU_set_test_func(pTest8, &test_fail); + TEST(CU_TRUE == f_exit_called); + test_results(0,1,0,1,0,0,1,1,0,1); + + /* clean up after testing */ + CU_set_error_action(CUEA_IGNORE); + CU_cleanup_registry(); +} + +/*-------------------------------------------------*/ +/* tests CU_assertImplementation() + * CU_get_failure_list() + * CU_clear_previous_results() + */ +static void test_CU_assertImplementation(void) +{ + CU_Test dummy_test; + CU_Suite dummy_suite; + CU_pFailureRecord pFailure1 = NULL; + CU_pFailureRecord pFailure2 = NULL; + CU_pFailureRecord pFailure3 = NULL; + CU_pFailureRecord pFailure4 = NULL; + CU_pFailureRecord pFailure5 = NULL; + CU_pFailureRecord pFailure6 = NULL; + + CU_clear_previous_results(); + + TEST(NULL == CU_get_failure_list()); + TEST(0 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + /* fool CU_assertImplementation into thinking test run is in progress */ + f_pCurTest = &dummy_test; + f_pCurSuite = &dummy_suite; + + /* asserted value is CU_TRUE*/ + TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 100, "Nothing happened 0.", "dummy0.c", "dummy_func0", CU_FALSE)); + + TEST(NULL == CU_get_failure_list()); + TEST(1 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 101, "Nothing happened 1.", "dummy1.c", "dummy_func1", CU_FALSE)); + + TEST(NULL == CU_get_failure_list()); + TEST(2 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + /* asserted value is CU_FALSE */ + TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 102, "Something happened 2.", "dummy2.c", "dummy_func2", CU_FALSE)); + + TEST(NULL != CU_get_failure_list()); + TEST(3 == CU_get_number_of_asserts()); + TEST(1 == CU_get_number_of_failures()); + TEST(1 == CU_get_number_of_failure_records()); + + TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 103, "Something happened 3.", "dummy3.c", "dummy_func3", CU_FALSE)); + + TEST(NULL != CU_get_failure_list()); + TEST(4 == CU_get_number_of_asserts()); + TEST(2 == CU_get_number_of_failures()); + TEST(2 == CU_get_number_of_failure_records()); + + TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 104, "Something happened 4.", "dummy4.c", "dummy_func4", CU_FALSE)); + + TEST(NULL != CU_get_failure_list()); + TEST(5 == CU_get_number_of_asserts()); + TEST(3 == CU_get_number_of_failures()); + TEST(3 == CU_get_number_of_failure_records()); + + if (3 == CU_get_number_of_failure_records()) { + pFailure1 = CU_get_failure_list(); + TEST(102 == pFailure1->uiLineNumber); + TEST(!strcmp("dummy2.c", pFailure1->strFileName)); + TEST(!strcmp("Something happened 2.", pFailure1->strCondition)); + TEST(&dummy_test == pFailure1->pTest); + TEST(&dummy_suite == pFailure1->pSuite); + TEST(NULL != pFailure1->pNext); + TEST(NULL == pFailure1->pPrev); + + pFailure2 = pFailure1->pNext; + TEST(103 == pFailure2->uiLineNumber); + TEST(!strcmp("dummy3.c", pFailure2->strFileName)); + TEST(!strcmp("Something happened 3.", pFailure2->strCondition)); + TEST(&dummy_test == pFailure2->pTest); + TEST(&dummy_suite == pFailure2->pSuite); + TEST(NULL != pFailure2->pNext); + TEST(pFailure1 == pFailure2->pPrev); + + pFailure3 = pFailure2->pNext; + TEST(104 == pFailure3->uiLineNumber); + TEST(!strcmp("dummy4.c", pFailure3->strFileName)); + TEST(!strcmp("Something happened 4.", pFailure3->strCondition)); + TEST(&dummy_test == pFailure3->pTest); + TEST(&dummy_suite == pFailure3->pSuite); + TEST(NULL == pFailure3->pNext); + TEST(pFailure2 == pFailure3->pPrev); + } + else + FAIL("Unexpected number of failure records."); + + /* confirm destruction of failure records */ + pFailure4 = pFailure1; + pFailure5 = pFailure2; + pFailure6 = pFailure3; + TEST(0 != test_cunit_get_n_memevents(pFailure4)); + TEST(test_cunit_get_n_allocations(pFailure4) != test_cunit_get_n_deallocations(pFailure4)); + TEST(0 != test_cunit_get_n_memevents(pFailure5)); + TEST(test_cunit_get_n_allocations(pFailure5) != test_cunit_get_n_deallocations(pFailure5)); + TEST(0 != test_cunit_get_n_memevents(pFailure6)); + TEST(test_cunit_get_n_allocations(pFailure6) != test_cunit_get_n_deallocations(pFailure6)); + + CU_clear_previous_results(); + TEST(0 != test_cunit_get_n_memevents(pFailure4)); + TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); + TEST(0 != test_cunit_get_n_memevents(pFailure5)); + TEST(test_cunit_get_n_allocations(pFailure5) == test_cunit_get_n_deallocations(pFailure5)); + TEST(0 != test_cunit_get_n_memevents(pFailure6)); + TEST(test_cunit_get_n_allocations(pFailure6) == test_cunit_get_n_deallocations(pFailure6)); + TEST(0 == CU_get_number_of_asserts()); + TEST(0 == CU_get_number_of_successes()); + TEST(0 == CU_get_number_of_failures()); + TEST(0 == CU_get_number_of_failure_records()); + + f_pCurTest = NULL; + f_pCurSuite = NULL; +} + +/*-------------------------------------------------*/ +static void test_add_failure(void) +{ + CU_Test test1; + CU_Suite suite1; + CU_pFailureRecord pFailure1 = NULL; + CU_pFailureRecord pFailure2 = NULL; + CU_pFailureRecord pFailure3 = NULL; + CU_pFailureRecord pFailure4 = NULL; + CU_RunSummary run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + /* test under memory exhaustion */ + test_cunit_deactivate_malloc(); + add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 100, "condition 0", "file0.c", &suite1, &test1); + TEST(NULL == pFailure1); + TEST(0 == run_summary.nFailureRecords); + test_cunit_activate_malloc(); + + /* normal operation */ + add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 101, "condition 1", "file1.c", &suite1, &test1); + TEST(1 == run_summary.nFailureRecords); + if (TEST(NULL != pFailure1)) { + TEST(101 == pFailure1->uiLineNumber); + TEST(!strcmp("condition 1", pFailure1->strCondition)); + TEST(!strcmp("file1.c", pFailure1->strFileName)); + TEST(&test1 == pFailure1->pTest); + TEST(&suite1 == pFailure1->pSuite); + TEST(NULL == pFailure1->pNext); + TEST(NULL == pFailure1->pPrev); + TEST(pFailure1 == f_last_failure); + TEST(0 != test_cunit_get_n_memevents(pFailure1)); + TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); + } + + add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 102, "condition 2", "file2.c", NULL, &test1); + TEST(2 == run_summary.nFailureRecords); + if (TEST(NULL != pFailure1)) { + TEST(101 == pFailure1->uiLineNumber); + TEST(!strcmp("condition 1", pFailure1->strCondition)); + TEST(!strcmp("file1.c", pFailure1->strFileName)); + TEST(&test1 == pFailure1->pTest); + TEST(&suite1 == pFailure1->pSuite); + TEST(NULL != pFailure1->pNext); + TEST(NULL == pFailure1->pPrev); + TEST(pFailure1 != f_last_failure); + TEST(0 != test_cunit_get_n_memevents(pFailure1)); + TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); + + if (TEST(NULL != (pFailure2 = pFailure1->pNext))) { + TEST(102 == pFailure2->uiLineNumber); + TEST(!strcmp("condition 2", pFailure2->strCondition)); + TEST(!strcmp("file2.c", pFailure2->strFileName)); + TEST(&test1 == pFailure2->pTest); + TEST(NULL == pFailure2->pSuite); + TEST(NULL == pFailure2->pNext); + TEST(pFailure1 == pFailure2->pPrev); + TEST(pFailure2 == f_last_failure); + TEST(0 != test_cunit_get_n_memevents(pFailure2)); + TEST(test_cunit_get_n_allocations(pFailure2) != test_cunit_get_n_deallocations(pFailure2)); + } + } + + pFailure3 = pFailure1; + pFailure4 = pFailure2; + clear_previous_results(&run_summary, &pFailure1); + + TEST(0 == run_summary.nFailureRecords); + TEST(0 != test_cunit_get_n_memevents(pFailure3)); + TEST(test_cunit_get_n_allocations(pFailure3) == test_cunit_get_n_deallocations(pFailure3)); + TEST(0 != test_cunit_get_n_memevents(pFailure4)); + TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); +} + +/*-------------------------------------------------*/ +void test_cunit_TestRun(void) +{ + test_cunit_start_tests("TestRun.c"); + + test_message_handlers(); + test_CU_fail_on_inactive(); + test_CU_run_all_tests(); + test_CU_run_suite(); + test_CU_run_test(); + test_CU_assertImplementation(); + test_add_failure(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestRun.h b/gearsyncd/tests/cunit/TestRun.h new file mode 100644 index 0000000000..51072f492a --- /dev/null +++ b/gearsyncd/tests/cunit/TestRun.h @@ -0,0 +1,444 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Contains Interface to Run tests. + * + * Aug 2001 Initial implementation. (AK) + * + * 09/Aug/2001 Contains generic run tests interface which can be used + * for any type of frontend interface framework. (AK) + * + * 24/Nov/2001 Added Handler for Group Initialization failure condition. (AK) + * + * 05-Aug-2004 New interface. Since these should be internal functions, + * no support for deprecated version 1 names provided now, + * eliminated global variables for current test & suite, + * moved (renamed) _TestResult here from TestDB.h. (JDS) + * + * 05-Sep-2004 Added internal test interface. (JDS) + * + * 23-Apr-2006 Moved doxygen comments into header. + * Added type marker to CU_FailureRecord. + * Added support for tracking inactive suites/tests. (JDS) + * + * 08-May-2006 Moved CU_print_run_results() functionality from + * console/basic test complete handler. (JDS) + * + * 24-May-2006 Added callbacks for suite start and complete events. + * Added tracking/reported of elapsed time. (JDS) + */ + +/** @file + * Test run management functions (user interface). + * The TestRun module implements functions supporting the running + * of tests elements (suites and tests). This includes functions for + * running suites and tests, retrieving the number of tests/suites run, + * and managing callbacks during the run process.

+ * + * The callback mechanism works as follows. The CUnit runtime system + * supports the registering and calling of functions at the start and end + * of each test, when all tests are complete, and when a suite + * initialialization function returns an error. This allows clients to + * perform actions associated with these events such as output formatting + * and reporting. + */ +/** @addtogroup Framework + * @{ + */ + +#ifndef CUNIT_TESTRUN_H_SEEN +#define CUNIT_TESTRUN_H_SEEN + +#include "CUnit.h" +#include "CUError.h" +#include "TestDB.h" +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/** Types of failures occurring during test runs. */ +typedef enum CU_FailureTypes +{ + CUF_SuiteInactive = 1, /**< Inactive suite was run. */ + CUF_SuiteInitFailed, /**< Suite initialization function failed. */ + CUF_SuiteCleanupFailed, /**< Suite cleanup function failed. */ + CUF_TestInactive, /**< Inactive test was run. */ + CUF_AssertFailed /**< CUnit assertion failed during test run. */ +} CU_FailureType; /**< Failure type. */ + +/* CU_FailureRecord type definition. */ +/** Data type for holding assertion failure information (linked list). */ +typedef struct CU_FailureRecord +{ + CU_FailureType type; /**< Failure type. */ + unsigned int uiLineNumber; /**< Line number of failure. */ + char* strFileName; /**< Name of file where failure occurred. */ + char* strCondition; /**< Test condition which failed. */ + CU_pTest pTest; /**< Test containing failure. */ + CU_pSuite pSuite; /**< Suite containing test having failure. */ + + struct CU_FailureRecord* pNext; /**< Pointer to next record in linked list. */ + struct CU_FailureRecord* pPrev; /**< Pointer to previous record in linked list. */ + +} CU_FailureRecord; +typedef CU_FailureRecord* CU_pFailureRecord; /**< Pointer to CU_FailureRecord. */ + +/* CU_RunSummary type definition. */ +/** Data type for holding statistics and assertion failures for a test run. */ +typedef struct CU_RunSummary +{ + unsigned int nSuitesRun; /**< Number of suites completed during run. */ + unsigned int nSuitesFailed; /**< Number of suites for which initialization failed. */ + unsigned int nSuitesInactive; /**< Number of suites which were inactive. */ + unsigned int nTestsRun; /**< Number of tests completed during run. */ + unsigned int nTestsFailed; /**< Number of tests containing failed assertions. */ + unsigned int nTestsInactive; /**< Number of tests which were inactive (in active suites). */ + unsigned int nAsserts; /**< Number of assertions tested during run. */ + unsigned int nAssertsFailed; /**< Number of failed assertions. */ + unsigned int nFailureRecords; /**< Number of failure records generated. */ + double ElapsedTime; /**< Elapsed time for run in seconds. */ +} CU_RunSummary; +typedef CU_RunSummary* CU_pRunSummary; /**< Pointer to CU_RunSummary. */ + +/*-------------------------------------------------------------------- + * Type Definitions for Message Handlers. + *--------------------------------------------------------------------*/ +typedef void (*CU_SuiteStartMessageHandler)(const CU_pSuite pSuite); +/**< Message handler called at the start of a suite. pSuite will not be null. */ + +typedef void (*CU_TestStartMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite); +/**< Message handler called at the start of a test. + * The parameters are the test and suite being run. The test run is + * considered in progress when the message handler is called. + * Neither pTest nor pSuite may be null. + */ + +typedef void (*CU_TestCompleteMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite, + const CU_pFailureRecord pFailure); +/**< Message handler called at the completion of a test. + * The parameters are the test and suite being run, plus a pointer to + * the first failure record applicable to this test. If the test did + * not have any assertion failures, pFailure will be NULL. The test run + * is considered in progress when the message handler is called. + */ + +typedef void (*CU_SuiteCompleteMessageHandler)(const CU_pSuite pSuite, + const CU_pFailureRecord pFailure); +/**< Message handler called at the completion of a suite. + * The parameters are suite being run, plus a pointer to the first failure + * record applicable to this suite. If the suite and it's tests did not + * have any failures, pFailure will be NULL. The test run is considered + * in progress when the message handler is called. + */ + +typedef void (*CU_AllTestsCompleteMessageHandler)(const CU_pFailureRecord pFailure); +/**< Message handler called at the completion of a test run. + * The parameter is a pointer to the linked list holding the failure + * records for the test run. The test run is considered completed + * when the message handler is called. + */ + +typedef void (*CU_SuiteInitFailureMessageHandler)(const CU_pSuite pSuite); +/**< Message handler called when a suite initializer fails. + * The test run is considered in progress when the message handler is called. + */ + +typedef void (*CU_SuiteCleanupFailureMessageHandler)(const CU_pSuite pSuite); +/**< Message handler called when a suite cleanup function fails. + * The test run is considered in progress when the message handler is called. + */ + +/*-------------------------------------------------------------------- + * Get/Set functions for Message Handlers + *--------------------------------------------------------------------*/ +CU_EXPORT void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartMessage); +/**< Sets the message handler to call before each suite is run. */ +CU_EXPORT void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartMessage); +/**< Sets the message handler to call before each test is run. */ +CU_EXPORT void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteMessage); +/**< Sets the message handler to call after each test is run. */ +CU_EXPORT void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteMessage); +/**< Sets the message handler to call after each suite is run. */ +CU_EXPORT void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteMessage); +/**< Sets the message handler to call after all tests have been run. */ +CU_EXPORT void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureMessage); +/**< Sets the message handler to call when a suite initialization function returns an error. */ +CU_EXPORT void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureMessage); +/**< Sets the message handler to call when a suite cleanup function returns an error. */ + +CU_EXPORT CU_SuiteStartMessageHandler CU_get_suite_start_handler(void); +/**< Retrieves the message handler called before each suite is run. */ +CU_EXPORT CU_TestStartMessageHandler CU_get_test_start_handler(void); +/**< Retrieves the message handler called before each test is run. */ +CU_EXPORT CU_TestCompleteMessageHandler CU_get_test_complete_handler(void); +/**< Retrieves the message handler called after each test is run. */ +CU_EXPORT CU_SuiteCompleteMessageHandler CU_get_suite_complete_handler(void); +/**< Retrieves the message handler called after each suite is run. */ +CU_EXPORT CU_AllTestsCompleteMessageHandler CU_get_all_test_complete_handler(void); +/**< Retrieves the message handler called after all tests are run. */ +CU_EXPORT CU_SuiteInitFailureMessageHandler CU_get_suite_init_failure_handler(void); +/**< Retrieves the message handler called when a suite initialization error occurs. */ +CU_EXPORT CU_SuiteCleanupFailureMessageHandler CU_get_suite_cleanup_failure_handler(void); +/**< Retrieves the message handler called when a suite cleanup error occurs. */ + +/*-------------------------------------------------------------------- + * Functions for running registered tests and suites. + *--------------------------------------------------------------------*/ +CU_EXPORT CU_ErrorCode CU_run_all_tests(void); +/**< + * Runs all tests in all suites registered in the test registry. + * The suites are run in the order registered in the test registry. + * For each suite, it is first checked to make sure it is active. + * Any initialization function is then called, the suite is run + * using run_single_suite(), and finally any suite cleanup function + * is called. If an error condition (other than CUE_NOREGISTRY) + * occurs during the run, the action depends on the current error + * action (see CU_set_error_action()). An inactive suite is not + * considered an error for this function. Note that the run + * statistics (counts of tests, successes, failures) are cleared + * each time this function is run, even if it is unsuccessful. + * + * @return A CU_ErrorCode indicating the first error condition + * encountered while running the tests. + * @see CU_run_suite() to run the tests in a specific suite. + * @see CU_run_test() for run a specific test only. + */ + +CU_EXPORT CU_ErrorCode CU_run_suite(CU_pSuite pSuite); +/**< + * Runs all tests in a specified suite. + * The suite need not be registered in the test registry to be + * run. It does, however, need to have its fActive flag set to + * CU_TRUE.

+ * + * Any initialization function for the suite is first called, + * then the suite is run using run_single_suite(), and any suite + * cleanup function is called. Note that the run statistics + * (counts of tests, successes, failures) are initialized each + * time this function is called even if it is unsuccessful. If + * an error condition occurs during the run, the action depends + * on the current error action (see CU_set_error_action()). + * + * @param pSuite The suite containing the test (non-NULL) + * @return A CU_ErrorCode indicating the first error condition + * encountered while running the suite. CU_run_suite() + * sets and returns CUE_NOSUITE if pSuite is NULL, or + * CUE_SUITE_INACTIVE if the requested suite is not + * activated. Other error codes can be set during suite + * initialization or cleanup or during test runs. + * @see CU_run_all_tests() to run all suites. + * @see CU_run_test() to run a single test in a specific suite. + */ + +CU_EXPORT CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest); +/**< + * Runs a specific test in a specified suite. + * The suite need not be registered in the test registry to be run, + * although the test must be registered in the specified suite. + * Any initialization function for the suite is first + * called, then the test is run using run_single_test(), and + * any suite cleanup function is called. Note that the + * run statistics (counts of tests, successes, failures) + * will be initialized each time this function is called even + * if it is not successful. Both the suite and test specified + * must be active for the test to be run. The suite is not + * considered to be run, although it may be counted as a failed + * suite if the intialization or cleanup functions fail. + * + * @param pSuite The suite containing the test (non-NULL) + * @param pTest The test to run (non-NULL) + * @return A CU_ErrorCode indicating the first error condition + * encountered while running the suite. CU_run_test() + * sets and returns CUE_NOSUITE if pSuite is NULL, + * CUE_NOTEST if pTest is NULL, CUE_SUITE_INACTIVE if + * pSuite is not active, CUE_TEST_NOT_IN_SUITE + * if pTest is not registered in pSuite, and CU_TEST_INACTIVE + * if pTest is not active. Other error codes can be set during + * suite initialization or cleanup or during the test run. + * @see CU_run_all_tests() to run all tests/suites. + * @see CU_run_suite() to run all tests in a specific suite. + */ + +/*-------------------------------------------------------------------- + * Functions for setting runtime behavior. + *--------------------------------------------------------------------*/ +CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive); +/**< + * Sets whether an inactive suite or test is treated as a failure. + * If CU_TRUE, then failure records will be generated for inactive + * suites or tests encountered during a test run. The default is + * CU_TRUE so that the client is reminded that the framewrork + * contains inactive suites/tests. Set to CU_FALSE to turn off + * this behavior. + * + * @param new_inactive New setting for whether to treat inactive + * suites and tests as failures during a test + * run (CU_TRUE) or not (CU_FALSE). + * @see CU_get_fail_on_failure() + */ + +CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void); +/**< + * Retrieves the current setting for whether inactive suites/tests + * are treated as failures. If CU_TRUE then failure records will + * be generated for inactive suites encountered during a test run. + * + * @return CU_TRUE if inactive suites/tests are failures, CU_FALSE if not. + * @see CU_set_fail_on_inactive() + */ + +/*-------------------------------------------------------------------- + * Functions for getting information about the previous test run. + *--------------------------------------------------------------------*/ +CU_EXPORT unsigned int CU_get_number_of_suites_run(void); +/**< Retrieves the number of suites completed during the previous run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_suites_failed(void); +/**< Retrieves the number of suites which failed to initialize during the previous run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_suites_inactive(void); +/**< Retrieves the number of inactive suites found during the previous run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_tests_run(void); +/**< Retrieves the number of tests completed during the previous run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_tests_failed(void); +/**< Retrieves the number of tests containing failed assertions during the previous run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_tests_inactive(void); +/**< Retrieves the number of inactive tests found during the previous run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_asserts(void); +/**< Retrieves the number of assertions processed during the last run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_successes(void); +/**< Retrieves the number of successful assertions during the last run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_failures(void); +/**< Retrieves the number of failed assertions during the last run (reset each run). */ +CU_EXPORT unsigned int CU_get_number_of_failure_records(void); +/**< + * Retrieves the number failure records created during the previous run (reset each run). + * Note that this may be more than the number of failed assertions, since failure + * records may also be created for failed suite initialization and cleanup. + */ +CU_EXPORT double CU_get_elapsed_time(void); +/**< + * Retrieves the elapsed time for the last run in seconds (reset each run). + * This function will calculate the current elapsed time if the test run has not + * yet completed. This is in contrast to the run summary returned by + * CU_get_run_summary(), for which the elapsed time is not updated until the + * end of the run. + */ +CU_EXPORT CU_pFailureRecord CU_get_failure_list(void); +/**< + * Retrieves the head of the linked list of failures which occurred during the + * last run (reset each run). Note that the pointer returned is invalidated + * when the client initiates a run using CU_run_all_tests(), CU_run_suite(), + * or CU_run_test(). + */ +CU_EXPORT CU_pRunSummary CU_get_run_summary(void); +/**< + * Retrieves the entire run summary for the last test run (reset each run). + * The run counts and stats contained in the run summary are updated + * throughout a test run. Note, however, that the elapsed time is not + * updated until after all suites/tests are run but before the "all tests + * complete" message handler is called (if any). To get the elapsed + * time during a test run, use CU_get_elapsed_time() instead. + */ + +CU_EXPORT char * CU_get_run_results_string(void); +/**< + * Creates a string and fills it with a summary of the current run results. + * The run summary presents data for the suites, tests, and assertions + * encountered during the run, as well as the elapsed time. The data + * presented include the number of registered, run, passed, failed, and + * inactive entities for each, as well as the elapsed time. This function + * can be called at any time, although the test registry must have been + * initialized (checked by assertion). The returned string is owned by + * the caller and should be deallocated using CU_FREE(). NULL is returned + * if there is an error allocating the new string. + * + * @return A new string containing the run summary (owned by caller). + */ + +CU_EXPORT void CU_print_run_results(FILE *file); +/**< + * Prints a summary of the current run results to file. + * The run summary is the same as returned by CU_get_run_results_string(). + * Note that no newlines are printed before or after the report, so any + * positioning must be performed before/after calling this function. The + * report itself extends over several lines broken by '\n' characters. + * file may not be NULL (checked by assertion). + * + * @param file Pointer to stream to receive the printed summary (non-NULL). + */ + +/*-------------------------------------------------------------------- + * Functions for internal & testing use. + *--------------------------------------------------------------------*/ +CU_EXPORT CU_pSuite CU_get_current_suite(void); +/**< Retrieves a pointer to the currently-running suite (NULL if none). */ +CU_EXPORT CU_pTest CU_get_current_test(void); +/**< Retrievea a pointer to the currently-running test (NULL if none). */ +CU_EXPORT CU_BOOL CU_is_test_running(void); +/**< Returns CU_TRUE if a test run is in progress, + * CU_TRUE otherwise. + */ + +CU_EXPORT void CU_clear_previous_results(void); +/**< + * Initializes the run summary information stored from the previous test run. + * Resets the run counts to zero, and frees any memory associated with + * failure records. Calling this function multiple times, while inefficient, + * will not cause an error condition. + * @see clear_previous_results() + */ + +CU_EXPORT CU_BOOL CU_assertImplementation(CU_BOOL bValue, + unsigned int uiLine, + const char *strCondition, + const char *strFile, + const char *strFunction, + CU_BOOL bFatal); +/**< + * Assertion implementation function. + * All CUnit assertions reduce to a call to this function. It should only be + * called during an active test run (checked by assertion). This means that CUnit + * assertions should only be used in registered test functions during a test run. + * + * @param bValue Value of the assertion (CU_TRUE or CU_FALSE). + * @param uiLine Line number of failed test statement. + * @param strCondition String containing logical test that failed. + * @param strFile Source file where test statement failed. + * @param strFunction Function where test statement failed. + * @param bFatal CU_TRUE to abort test (via longjmp()), CU_FALSE to continue test. + * @return As a convenience, returns the value of the assertion (i.e. bValue). + */ + +#ifdef USE_DEPRECATED_CUNIT_NAMES +typedef CU_FailureRecord _TestResult; /**< @deprecated Use CU_FailureRecord. */ +typedef CU_pFailureRecord PTestResult; /**< @deprecated Use CU_pFailureRecord. */ +#endif /* USE_DEPRECATED_CUNIT_NAMES */ + +#ifdef CUNIT_BUILD_TESTS +void test_cunit_TestRun(void); +#endif + +#ifdef __cplusplus +} +#endif +#endif /* CUNIT_TESTRUN_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/cunit/Util.c b/gearsyncd/tests/cunit/Util.c new file mode 100644 index 0000000000..8e21922b9e --- /dev/null +++ b/gearsyncd/tests/cunit/Util.c @@ -0,0 +1,609 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Generic (internal) utility functions used across CUnit. + * These were originally distributed across the other CUnit + * source files, but were consolidated here for consistency. + * + * 13/Oct/2001 Initial implementation (AK) + * + * 26/Jul/2003 Added a function to convert a string containing special + * characters into escaped character for XML/HTML usage. (AK) + * + * 16-Jul-2004 New interface, doxygen comments. (JDS) + * + * 17-Apr-2006 Added CU_translated_strlen() and CU_number_width(). + * Fixed off-by-1 error in CU_translate_special_characters(), + * modifying implementation & results in some cases. User can + * now tell if conversion failed. (JDS) + */ + +/** @file + * Utility functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "Util.h" + + +/*------------------------------------------------------------------------*/ +/** + * Structure containing mappings of special characters to xml entity codes. + * special_char's in the CU_bindings array will be translated during calls + * to CU_translate_special_characters(). Add additional chars/replacements + * or modify existing ones to change the behavior upon translation. + */ +static const struct bindings { + const char special_char; /**< Special character. */ + const char *replacement; /**< Entity code for special character. */ +} CU_bindings [] = { + {'&', "&"}, + {'>', ">"}, + {'<', "<"} +}; + +/*------------------------------------------------------------------------*/ +/** + * Checks whether a character is a special xml character. + * This function performs a lookup of the specified character in + * the CU_bindings structure. If it is a special character, its + * index into the CU_bindings array is returned. If not, -1 is returned. + * + * @param ch The character to check + * @return Index into CU_bindings if a special character, -1 otherwise. + */ +static int get_index(const char ch) +{ + int length = sizeof(CU_bindings)/sizeof(CU_bindings[0]); + int counter; + + for (counter = 0; counter < length && CU_bindings[counter].special_char != ch; ++counter) { + ; + } + + return (counter < length ? counter : -1); +} + +size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen) +{ +/* old implementation + size_t count = 0; + size_t src = 0; + size_t dest = 0; + size_t length = 0; + int conv_index; + + assert(NULL != szSrc); + assert(NULL != szDest); + + length = strlen(szSrc); + memset(szDest, 0, maxlen); + while ((dest < maxlen) && (src < length)) { + + if ((-1 != (conv_index = get_index(szSrc[src]))) && + ((dest + strlen(CU_bindings[conv_index].replacement)) < maxlen)) { + strcat(szDest, CU_bindings[conv_index].replacement); + dest += strlen(CU_bindings[conv_index].replacement); + ++count; + } else { + szDest[dest++] = szSrc[src]; + } + + ++src; + } + + return count; +*/ + size_t count = 0; + size_t repl_len; + int conv_index; + char *dest_start = szDest; + + assert(NULL != szSrc); + assert(NULL != szDest); + + /* only process if destination buffer not 0-length */ + if (maxlen > 0) { + + while ((maxlen > 0) && (*szSrc != '\0')) { + conv_index = get_index(*szSrc); + if (-1 != conv_index) { + if (maxlen > (repl_len = strlen(CU_bindings[conv_index].replacement))) { + memcpy(szDest, CU_bindings[conv_index].replacement, repl_len); + szDest += repl_len; + maxlen -= repl_len; + ++count; + } else { + maxlen = 0; /* ran out of room - abort conversion */ + break; + } + } else { + *szDest++ = *szSrc; + --maxlen; + } + ++szSrc; + } + + if (0 == maxlen) { + *dest_start = '\0'; /* ran out of room - return empty string in szDest */ + count = 0; + } else { + *szDest = '\0'; /* had room - make sure szDest has a terminating \0 */ + } + } + return count; +} + +/*------------------------------------------------------------------------*/ +size_t CU_translated_strlen(const char* szSrc) +{ + size_t count = 0; + int conv_index; + + assert(NULL != szSrc); + + while (*szSrc != '\0') { + if (-1 != (conv_index = get_index(*szSrc))) { + count += strlen(CU_bindings[conv_index].replacement); + } else { + ++count; + } + ++szSrc; + } + return count; +} + +/*------------------------------------------------------------------------*/ +int CU_compare_strings(const char* szSrc, const char* szDest) +{ + assert(NULL != szSrc); + assert(NULL != szDest); + + while (('\0' != *szSrc) && ('\0' != *szDest) && (toupper(*szSrc) == toupper(*szDest))) { + szSrc++; + szDest++; + } + + return (int)(*szSrc - *szDest); +} + +/*------------------------------------------------------------------------*/ +void CU_trim(char* szString) +{ + CU_trim_left(szString); + CU_trim_right(szString); +} + +/*------------------------------------------------------------------------*/ +void CU_trim_left(char* szString) +{ + int nOffset = 0; + char* szSrc = szString; + char* szDest = szString; + + assert(NULL != szString); + + /* Scan for the spaces in the starting of string. */ + for (; '\0' != *szSrc; szSrc++, nOffset++) { + if (!isspace(*szSrc)) { + break; + } + } + + for(; (0 != nOffset) && ('\0' != (*szDest = *szSrc)); szSrc++, szDest++) { + ; + } +} + +/*------------------------------------------------------------------------*/ +void CU_trim_right(char* szString) +{ + size_t nLength; + char* szSrc = szString; + + assert(NULL != szString); + nLength = strlen(szString); + /* + * Scan for specs in the end of string. + */ + for (; (0 != nLength) && isspace(*(szSrc + nLength - 1)); nLength--) { + ; + } + + *(szSrc + nLength) = '\0'; +} + +/*------------------------------------------------------------------------*/ +size_t CU_number_width(int number) +{ + char buf[33]; + + snprintf(buf, 33, "%d", number); + buf[32] = '\0'; + return (strlen(buf)); +} + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +/* Keep BUF_LEN even or trouble ensues below... */ +#define BUF_LEN 1000 +#define MAX_LEN BUF_LEN/2 + +static void test_CU_translate_special_characters(void) +{ + char dest_buf[BUF_LEN]; + char *dest = dest_buf + MAX_LEN; + char ref_buf[BUF_LEN]; + const int mask_char = 0x01; /* char written to buffer */ + + /* set up reference buffer for testing of translated strings */ + memset(ref_buf, mask_char, BUF_LEN); + + /* empty src */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); + + /* 1 char src */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("#", dest, 0)); + TEST(!strncmp(dest_buf, ref_buf, BUF_LEN)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("#", dest, 1)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("&", dest, 2)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("&", dest, 4)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+4), ref_buf, MAX_LEN-4)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("&", dest, 5)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(1 == CU_translate_special_characters("&", dest, 6)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "&\0", 6)); + TEST(!strncmp((dest+6), ref_buf, MAX_LEN-6)); + + /* maxlen=0 */ + memset(dest_buf, mask_char, BUF_LEN); + strcpy(dest, "random initialized string"); + TEST(0 == CU_translate_special_characters("some <", dest, 0)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strcmp(dest, "random initialized string")); + TEST(!strncmp(dest+strlen(dest)+1, ref_buf, MAX_LEN-strlen(dest)-1)); + + /* maxlen < len(converted szSrc) */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 1)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 2)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 5)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 10)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+10), ref_buf, MAX_LEN-10)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 20)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+20), ref_buf, MAX_LEN-20)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 24)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+24), ref_buf, MAX_LEN-24)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 25)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+25), ref_buf, MAX_LEN-25)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 37)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+37), ref_buf, MAX_LEN-37)); + + /* maxlen > len(converted szSrc) */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(4 == CU_translate_special_characters("some <", dest, 38)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "some <<string & another>\0", 38)); + TEST(!strncmp((dest+38), ref_buf, MAX_LEN-38)); + + /* maxlen > len(converted szSrc) */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(4 == CU_translate_special_characters("some <", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "some <<string & another>\0", 38)); + + /* no special characters */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some string or another", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "some string or another\0", 23)); + + /* only special characters */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(11 == CU_translate_special_characters("<><><<>>&&&", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strcmp(dest, "<><><<>>&&&")); +} + +static void test_CU_translated_strlen(void) +{ + /* empty src */ + TEST(0 == CU_translated_strlen("")); + + /* 1 char src */ + TEST(1 == CU_translated_strlen("#")); + TEST(5 == CU_translated_strlen("&")); + TEST(4 == CU_translated_strlen("<")); + TEST(4 == CU_translated_strlen(">")); + TEST(1 == CU_translated_strlen("?")); + + /* 2 char src */ + TEST(2 == CU_translated_strlen("#@")); + TEST(10 == CU_translated_strlen("&&")); + TEST(9 == CU_translated_strlen(">&")); + + /* longer src */ + TEST(37 == CU_translated_strlen("some <")); + TEST(22 == CU_translated_strlen("some string or another")); + TEST(47 == CU_translated_strlen("<><><<>>&&&")); +} + +static void test_CU_compare_strings(void) +{ + TEST(0 == CU_compare_strings("","")); + TEST(0 == CU_compare_strings("@","@")); + TEST(0 == CU_compare_strings("D","d")); + TEST(0 == CU_compare_strings("s1","s1")); + TEST(0 == CU_compare_strings("s1","S1")); + TEST(0 != CU_compare_strings("s1","s12")); + TEST(0 == CU_compare_strings("this is string 1","tHIS iS sTRING 1")); + TEST(0 == CU_compare_strings("i have \t a tab!","I have \t a tab!")); + TEST(0 != CU_compare_strings("not the same"," not the same")); +} + +static void test_CU_trim(void) +{ + char string[MAX_LEN]; + + strcpy(string, ""); + CU_trim(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim(string); + TEST(!strcmp("", string)); + + strcpy(string, " b"); + CU_trim(string); + TEST(!strcmp("b", string)); + + strcpy(string, " B"); + CU_trim(string); + TEST(!strcmp("B", string)); + + strcpy(string, "s "); + CU_trim(string); + TEST(!strcmp("s", string)); + + strcpy(string, "S "); + CU_trim(string); + TEST(!strcmp("S", string)); + + strcpy(string, " 5 "); + CU_trim(string); + TEST(!strcmp("5", string)); + + strcpy(string, "~ & ^ ( ^ "); + CU_trim(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^"); + CU_trim(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^ "); + CU_trim(string); + TEST(!strcmp("~ & ^ ( ^", string)); +} + +static void test_CU_trim_left(void) +{ + char string[MAX_LEN]; + + strcpy(string, ""); + CU_trim_left(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_left(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_left(string); + TEST(!strcmp("", string)); + + strcpy(string, " b"); + CU_trim_left(string); + TEST(!strcmp("b", string)); + + strcpy(string, " B"); + CU_trim_left(string); + TEST(!strcmp("B", string)); + + strcpy(string, "s "); + CU_trim_left(string); + TEST(!strcmp("s ", string)); + + strcpy(string, "S "); + CU_trim_left(string); + TEST(!strcmp("S ", string)); + + strcpy(string, " 5 "); + CU_trim_left(string); + TEST(!strcmp("5 ", string)); + + strcpy(string, "~ & ^ ( ^ "); + CU_trim_left(string); + TEST(!strcmp("~ & ^ ( ^ ", string)); + + strcpy(string, " ~ & ^ ( ^"); + CU_trim_left(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^ "); + CU_trim_left(string); + TEST(!strcmp("~ & ^ ( ^ ", string)); +} + +static void test_CU_trim_right(void) +{ + char string[MAX_LEN]; + + strcpy(string, ""); + CU_trim_right(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_right(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_right(string); + TEST(!strcmp("", string)); + + strcpy(string, " b"); + CU_trim_right(string); + TEST(!strcmp(" b", string)); + + strcpy(string, " B"); + CU_trim_right(string); + TEST(!strcmp(" B", string)); + + strcpy(string, "s "); + CU_trim_right(string); + TEST(!strcmp("s", string)); + + strcpy(string, "S "); + CU_trim_right(string); + TEST(!strcmp("S", string)); + + strcpy(string, " 5 "); + CU_trim_right(string); + TEST(!strcmp(" 5", string)); + + strcpy(string, "~ & ^ ( ^ "); + CU_trim_right(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^"); + CU_trim_right(string); + TEST(!strcmp(" ~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^ "); + CU_trim_right(string); + TEST(!strcmp(" ~ & ^ ( ^", string)); +} + +static void test_CU_number_width(void) +{ + TEST(1 == CU_number_width(0)); + TEST(1 == CU_number_width(1)); + TEST(2 == CU_number_width(-1)); + TEST(4 == CU_number_width(2346)); + TEST(7 == CU_number_width(-257265)); + TEST(9 == CU_number_width(245723572)); + TEST(9 == CU_number_width(-45622572)); +} + +void test_cunit_Util(void) +{ + + test_cunit_start_tests("Util.c"); + + test_CU_translate_special_characters(); + test_CU_translated_strlen(); + test_CU_compare_strings(); + test_CU_trim(); + test_CU_trim_left(); + test_CU_trim_right(); + test_CU_number_width(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/Util.cpp b/gearsyncd/tests/cunit/Util.cpp new file mode 100644 index 0000000000..8e21922b9e --- /dev/null +++ b/gearsyncd/tests/cunit/Util.cpp @@ -0,0 +1,609 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Generic (internal) utility functions used across CUnit. + * These were originally distributed across the other CUnit + * source files, but were consolidated here for consistency. + * + * 13/Oct/2001 Initial implementation (AK) + * + * 26/Jul/2003 Added a function to convert a string containing special + * characters into escaped character for XML/HTML usage. (AK) + * + * 16-Jul-2004 New interface, doxygen comments. (JDS) + * + * 17-Apr-2006 Added CU_translated_strlen() and CU_number_width(). + * Fixed off-by-1 error in CU_translate_special_characters(), + * modifying implementation & results in some cases. User can + * now tell if conversion failed. (JDS) + */ + +/** @file + * Utility functions (implementation). + */ +/** @addtogroup Framework + @{ +*/ + +#include +#include +#include +#include +#include + +#include "CUnit.h" +#include "TestDB.h" +#include "Util.h" + + +/*------------------------------------------------------------------------*/ +/** + * Structure containing mappings of special characters to xml entity codes. + * special_char's in the CU_bindings array will be translated during calls + * to CU_translate_special_characters(). Add additional chars/replacements + * or modify existing ones to change the behavior upon translation. + */ +static const struct bindings { + const char special_char; /**< Special character. */ + const char *replacement; /**< Entity code for special character. */ +} CU_bindings [] = { + {'&', "&"}, + {'>', ">"}, + {'<', "<"} +}; + +/*------------------------------------------------------------------------*/ +/** + * Checks whether a character is a special xml character. + * This function performs a lookup of the specified character in + * the CU_bindings structure. If it is a special character, its + * index into the CU_bindings array is returned. If not, -1 is returned. + * + * @param ch The character to check + * @return Index into CU_bindings if a special character, -1 otherwise. + */ +static int get_index(const char ch) +{ + int length = sizeof(CU_bindings)/sizeof(CU_bindings[0]); + int counter; + + for (counter = 0; counter < length && CU_bindings[counter].special_char != ch; ++counter) { + ; + } + + return (counter < length ? counter : -1); +} + +size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen) +{ +/* old implementation + size_t count = 0; + size_t src = 0; + size_t dest = 0; + size_t length = 0; + int conv_index; + + assert(NULL != szSrc); + assert(NULL != szDest); + + length = strlen(szSrc); + memset(szDest, 0, maxlen); + while ((dest < maxlen) && (src < length)) { + + if ((-1 != (conv_index = get_index(szSrc[src]))) && + ((dest + strlen(CU_bindings[conv_index].replacement)) < maxlen)) { + strcat(szDest, CU_bindings[conv_index].replacement); + dest += strlen(CU_bindings[conv_index].replacement); + ++count; + } else { + szDest[dest++] = szSrc[src]; + } + + ++src; + } + + return count; +*/ + size_t count = 0; + size_t repl_len; + int conv_index; + char *dest_start = szDest; + + assert(NULL != szSrc); + assert(NULL != szDest); + + /* only process if destination buffer not 0-length */ + if (maxlen > 0) { + + while ((maxlen > 0) && (*szSrc != '\0')) { + conv_index = get_index(*szSrc); + if (-1 != conv_index) { + if (maxlen > (repl_len = strlen(CU_bindings[conv_index].replacement))) { + memcpy(szDest, CU_bindings[conv_index].replacement, repl_len); + szDest += repl_len; + maxlen -= repl_len; + ++count; + } else { + maxlen = 0; /* ran out of room - abort conversion */ + break; + } + } else { + *szDest++ = *szSrc; + --maxlen; + } + ++szSrc; + } + + if (0 == maxlen) { + *dest_start = '\0'; /* ran out of room - return empty string in szDest */ + count = 0; + } else { + *szDest = '\0'; /* had room - make sure szDest has a terminating \0 */ + } + } + return count; +} + +/*------------------------------------------------------------------------*/ +size_t CU_translated_strlen(const char* szSrc) +{ + size_t count = 0; + int conv_index; + + assert(NULL != szSrc); + + while (*szSrc != '\0') { + if (-1 != (conv_index = get_index(*szSrc))) { + count += strlen(CU_bindings[conv_index].replacement); + } else { + ++count; + } + ++szSrc; + } + return count; +} + +/*------------------------------------------------------------------------*/ +int CU_compare_strings(const char* szSrc, const char* szDest) +{ + assert(NULL != szSrc); + assert(NULL != szDest); + + while (('\0' != *szSrc) && ('\0' != *szDest) && (toupper(*szSrc) == toupper(*szDest))) { + szSrc++; + szDest++; + } + + return (int)(*szSrc - *szDest); +} + +/*------------------------------------------------------------------------*/ +void CU_trim(char* szString) +{ + CU_trim_left(szString); + CU_trim_right(szString); +} + +/*------------------------------------------------------------------------*/ +void CU_trim_left(char* szString) +{ + int nOffset = 0; + char* szSrc = szString; + char* szDest = szString; + + assert(NULL != szString); + + /* Scan for the spaces in the starting of string. */ + for (; '\0' != *szSrc; szSrc++, nOffset++) { + if (!isspace(*szSrc)) { + break; + } + } + + for(; (0 != nOffset) && ('\0' != (*szDest = *szSrc)); szSrc++, szDest++) { + ; + } +} + +/*------------------------------------------------------------------------*/ +void CU_trim_right(char* szString) +{ + size_t nLength; + char* szSrc = szString; + + assert(NULL != szString); + nLength = strlen(szString); + /* + * Scan for specs in the end of string. + */ + for (; (0 != nLength) && isspace(*(szSrc + nLength - 1)); nLength--) { + ; + } + + *(szSrc + nLength) = '\0'; +} + +/*------------------------------------------------------------------------*/ +size_t CU_number_width(int number) +{ + char buf[33]; + + snprintf(buf, 33, "%d", number); + buf[32] = '\0'; + return (strlen(buf)); +} + +/** @} */ + +#ifdef CUNIT_BUILD_TESTS +#include "test_cunit.h" + +/* Keep BUF_LEN even or trouble ensues below... */ +#define BUF_LEN 1000 +#define MAX_LEN BUF_LEN/2 + +static void test_CU_translate_special_characters(void) +{ + char dest_buf[BUF_LEN]; + char *dest = dest_buf + MAX_LEN; + char ref_buf[BUF_LEN]; + const int mask_char = 0x01; /* char written to buffer */ + + /* set up reference buffer for testing of translated strings */ + memset(ref_buf, mask_char, BUF_LEN); + + /* empty src */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); + + /* 1 char src */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("#", dest, 0)); + TEST(!strncmp(dest_buf, ref_buf, BUF_LEN)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("#", dest, 1)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("&", dest, 2)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("&", dest, 4)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+4), ref_buf, MAX_LEN-4)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("&", dest, 5)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(1 == CU_translate_special_characters("&", dest, 6)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "&\0", 6)); + TEST(!strncmp((dest+6), ref_buf, MAX_LEN-6)); + + /* maxlen=0 */ + memset(dest_buf, mask_char, BUF_LEN); + strcpy(dest, "random initialized string"); + TEST(0 == CU_translate_special_characters("some <", dest, 0)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strcmp(dest, "random initialized string")); + TEST(!strncmp(dest+strlen(dest)+1, ref_buf, MAX_LEN-strlen(dest)-1)); + + /* maxlen < len(converted szSrc) */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 1)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 2)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 5)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 10)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+10), ref_buf, MAX_LEN-10)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 20)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+20), ref_buf, MAX_LEN-20)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 24)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+24), ref_buf, MAX_LEN-24)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 25)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+25), ref_buf, MAX_LEN-25)); + + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some <", dest, 37)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "\0", 1)); + TEST(!strncmp((dest+37), ref_buf, MAX_LEN-37)); + + /* maxlen > len(converted szSrc) */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(4 == CU_translate_special_characters("some <", dest, 38)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "some <<string & another>\0", 38)); + TEST(!strncmp((dest+38), ref_buf, MAX_LEN-38)); + + /* maxlen > len(converted szSrc) */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(4 == CU_translate_special_characters("some <", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "some <<string & another>\0", 38)); + + /* no special characters */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(0 == CU_translate_special_characters("some string or another", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strncmp(dest, "some string or another\0", 23)); + + /* only special characters */ + memset(dest_buf, mask_char, BUF_LEN); + TEST(11 == CU_translate_special_characters("<><><<>>&&&", dest, MAX_LEN)); + TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); + TEST(!strcmp(dest, "<><><<>>&&&")); +} + +static void test_CU_translated_strlen(void) +{ + /* empty src */ + TEST(0 == CU_translated_strlen("")); + + /* 1 char src */ + TEST(1 == CU_translated_strlen("#")); + TEST(5 == CU_translated_strlen("&")); + TEST(4 == CU_translated_strlen("<")); + TEST(4 == CU_translated_strlen(">")); + TEST(1 == CU_translated_strlen("?")); + + /* 2 char src */ + TEST(2 == CU_translated_strlen("#@")); + TEST(10 == CU_translated_strlen("&&")); + TEST(9 == CU_translated_strlen(">&")); + + /* longer src */ + TEST(37 == CU_translated_strlen("some <")); + TEST(22 == CU_translated_strlen("some string or another")); + TEST(47 == CU_translated_strlen("<><><<>>&&&")); +} + +static void test_CU_compare_strings(void) +{ + TEST(0 == CU_compare_strings("","")); + TEST(0 == CU_compare_strings("@","@")); + TEST(0 == CU_compare_strings("D","d")); + TEST(0 == CU_compare_strings("s1","s1")); + TEST(0 == CU_compare_strings("s1","S1")); + TEST(0 != CU_compare_strings("s1","s12")); + TEST(0 == CU_compare_strings("this is string 1","tHIS iS sTRING 1")); + TEST(0 == CU_compare_strings("i have \t a tab!","I have \t a tab!")); + TEST(0 != CU_compare_strings("not the same"," not the same")); +} + +static void test_CU_trim(void) +{ + char string[MAX_LEN]; + + strcpy(string, ""); + CU_trim(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim(string); + TEST(!strcmp("", string)); + + strcpy(string, " b"); + CU_trim(string); + TEST(!strcmp("b", string)); + + strcpy(string, " B"); + CU_trim(string); + TEST(!strcmp("B", string)); + + strcpy(string, "s "); + CU_trim(string); + TEST(!strcmp("s", string)); + + strcpy(string, "S "); + CU_trim(string); + TEST(!strcmp("S", string)); + + strcpy(string, " 5 "); + CU_trim(string); + TEST(!strcmp("5", string)); + + strcpy(string, "~ & ^ ( ^ "); + CU_trim(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^"); + CU_trim(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^ "); + CU_trim(string); + TEST(!strcmp("~ & ^ ( ^", string)); +} + +static void test_CU_trim_left(void) +{ + char string[MAX_LEN]; + + strcpy(string, ""); + CU_trim_left(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_left(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_left(string); + TEST(!strcmp("", string)); + + strcpy(string, " b"); + CU_trim_left(string); + TEST(!strcmp("b", string)); + + strcpy(string, " B"); + CU_trim_left(string); + TEST(!strcmp("B", string)); + + strcpy(string, "s "); + CU_trim_left(string); + TEST(!strcmp("s ", string)); + + strcpy(string, "S "); + CU_trim_left(string); + TEST(!strcmp("S ", string)); + + strcpy(string, " 5 "); + CU_trim_left(string); + TEST(!strcmp("5 ", string)); + + strcpy(string, "~ & ^ ( ^ "); + CU_trim_left(string); + TEST(!strcmp("~ & ^ ( ^ ", string)); + + strcpy(string, " ~ & ^ ( ^"); + CU_trim_left(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^ "); + CU_trim_left(string); + TEST(!strcmp("~ & ^ ( ^ ", string)); +} + +static void test_CU_trim_right(void) +{ + char string[MAX_LEN]; + + strcpy(string, ""); + CU_trim_right(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_right(string); + TEST(!strcmp("", string)); + + strcpy(string, " "); + CU_trim_right(string); + TEST(!strcmp("", string)); + + strcpy(string, " b"); + CU_trim_right(string); + TEST(!strcmp(" b", string)); + + strcpy(string, " B"); + CU_trim_right(string); + TEST(!strcmp(" B", string)); + + strcpy(string, "s "); + CU_trim_right(string); + TEST(!strcmp("s", string)); + + strcpy(string, "S "); + CU_trim_right(string); + TEST(!strcmp("S", string)); + + strcpy(string, " 5 "); + CU_trim_right(string); + TEST(!strcmp(" 5", string)); + + strcpy(string, "~ & ^ ( ^ "); + CU_trim_right(string); + TEST(!strcmp("~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^"); + CU_trim_right(string); + TEST(!strcmp(" ~ & ^ ( ^", string)); + + strcpy(string, " ~ & ^ ( ^ "); + CU_trim_right(string); + TEST(!strcmp(" ~ & ^ ( ^", string)); +} + +static void test_CU_number_width(void) +{ + TEST(1 == CU_number_width(0)); + TEST(1 == CU_number_width(1)); + TEST(2 == CU_number_width(-1)); + TEST(4 == CU_number_width(2346)); + TEST(7 == CU_number_width(-257265)); + TEST(9 == CU_number_width(245723572)); + TEST(9 == CU_number_width(-45622572)); +} + +void test_cunit_Util(void) +{ + + test_cunit_start_tests("Util.c"); + + test_CU_translate_special_characters(); + test_CU_translated_strlen(); + test_CU_compare_strings(); + test_CU_trim(); + test_CU_trim_left(); + test_CU_trim_right(); + test_CU_number_width(); + + test_cunit_end_tests(); +} + +#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/Util.h b/gearsyncd/tests/cunit/Util.h new file mode 100644 index 0000000000..c03085b45b --- /dev/null +++ b/gearsyncd/tests/cunit/Util.h @@ -0,0 +1,158 @@ +/* + * CUnit - A Unit testing framework library for C. + * Copyright (C) 2001 Anil Kumar + * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Contains Type Definitions for some generic functions used across + * CUnit project files. + * + * 13/Oct/2001 Moved some of the generic functions declarations from + * other files to this one so as to use the functions + * consitently. This file is not included in the distribution + * headers because it is used internally by CUnit. (AK) + * + * 20-Jul-2004 New interface, support for deprecated version 1 names. (JDS) + * + * 5-Sep-2004 Added internal test interface. (JDS) + * + * 17-Apr-2006 Added CU_translated_strlen() and CU_number_width(). + * Removed CUNIT_MAX_STRING_LENGTH - dangerous since not enforced. + * Fixed off-by-1 error in CU_translate_special_characters(), + * modifying implementation & results in some cases. User can + * now tell if conversion failed. (JDS) + */ + +/** @file + * Utility functions (user interface). + */ +/** @addtogroup Framework + * @{ + */ + +#ifndef CUNIT_UTIL_H_SEEN +#define CUNIT_UTIL_H_SEEN + +#include "CUnit.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define CUNIT_MAX_ENTITY_LEN 5 +/**< Maximum number of characters in a translated xml entity. */ + +CU_EXPORT size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen); +/**< + * Converts special characters in szSrc to xml entity codes and stores + * result in szDest. Currently conversion of '&', '<', and '>' is supported. + * Note that conversion to entities increases the length of the converted + * string. The greatest conversion size increase would be a string + * consisting entirely of entity characters of converted length + * CUNIT_MAX_ENTITY_LEN. Neither szSrc nor szDest may be NULL + * (checked by assertion).

+ * + * maxlen gives the maximum number of characters in the translated string. + * If szDest does not have enough room to hold the converted string, the + * return value will be zero and szDest will contain an empty string. + * If this occurs, the remaining characters in szDest may be overwritten + * in an unspecified manner. It is the caller's responsibility to make + * sure there is sufficient room in szDest to hold the converted string. + * CU_translated_strlen() may be used to calculate the length of buffer + * required (remember to add 1 for the terminating \0). + * + * @param szSrc Source string to convert (non-NULL). + * @param szDest Location to hold the converted string (non-NULL). + * @param maxlen Maximum number of characters szDest can hold. + * @return The number of special characters converted (always 0 if + * szDest did not have enough room to hold converted string). + */ + +CU_EXPORT size_t CU_translated_strlen(const char *szSrc); +/**< + * Calculates the length of a translated string. + * This function calculates the buffer length required to hold a string + * after processing with CU_translate_special_characters(). The returned + * length does not include space for the terminating '\0' character. + * szSrc may not be NULL (checked by assertion). + * + * @param szSrc Source string to analyze (non-NULL). + * @return The number of characters szSrc will expand to when converted. + */ + +CU_EXPORT int CU_compare_strings(const char *szSrc, const char *szDest); +/**< + * Case-insensitive string comparison. Neither string pointer + * can be NULL (checked by assertion). + * + * @param szSrc 1st string to compare (non-NULL). + * @param szDest 2nd string to compare (non-NULL). + * @return 0 if the strings are equal, non-zero otherwise. + */ + +CU_EXPORT void CU_trim_left(char *szString); +/**< + * Trims leading whitespace from the specified string. + * @param szString The string to trim. + */ + +CU_EXPORT void CU_trim_right(char *szString); +/**< + * Trims trailing whitespace from the specified string. + * @param szString The string to trim. + */ + +CU_EXPORT void CU_trim(char *szString); +/**< + * Trims leading and trailing whitespace from the specified string. + * @param szString The string to trim. + */ + +CU_EXPORT size_t CU_number_width(int number); +/**< + * Calulates the number of places required to display + * number in decimal. + */ + +#ifdef CUNIT_BUILD_TESTS +void test_cunit_Util(void); +#endif + +#ifdef __cplusplus +} +#endif + +#ifdef USE_DEPRECATED_CUNIT_NAMES +#define CUNIT_MAX_STRING_LENGTH 1024 +/**< Maximum string length. */ +#define translate_special_characters(src, dest, len) CU_translate_special_characters(src, dest, len) +/**< Deprecated (version 1). @deprecated Use CU_translate_special_characters(). */ +#define compare_strings(src, dest) CU_compare_strings(src, dest) +/**< Deprecated (version 1). @deprecated Use CU_compare_strings(). */ + +#define trim_left(str) CU_trim_left(str) +/**< Deprecated (version 1). @deprecated Use CU_trim_left(). */ +#define trim_right(str) CU_trim_right(str) +/**< Deprecated (version 1). @deprecated Use CU_trim_right(). */ +#define trim(str) CU_trim(str) +/**< Deprecated (version 1). @deprecated Use CU_trim(). */ + +#endif /* USE_DEPRECATED_CUNIT_NAMES */ + +#endif /* CUNIT_UTIL_H_SEEN */ +/** @} */ diff --git a/gearsyncd/tests/testgearparser.cpp b/gearsyncd/tests/testgearparser.cpp new file mode 100644 index 0000000000..e01c85bb19 --- /dev/null +++ b/gearsyncd/tests/testgearparser.cpp @@ -0,0 +1,150 @@ +/* + * Copyright 2019 Broadcom Inc. + * + * 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 "gearboxparser.h" +#include +#include +#include "cunit/Basic.h" +#include "logger.h" + +void usage() +{ + std::cout << "Usage: testgearparser" << std::endl; +} + +bool handleGearboxConfigFile(std::string file, bool warm); + +void positiveConfigTest() +{ + bool ret; + + ret = handleGearboxConfigFile("tests/configs/positive/gearbox_config_1.json", false); + CU_ASSERT_EQUAL(ret, true); +} + +void negativeConfigTest() +{ + bool ret; + + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_array.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_json.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phys.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_name.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_address.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_lib_name.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_firmware_path.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_config_file.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_sai_init_config_file.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_phy_access.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_bus_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_phy_config_file.json", false); + CU_ASSERT_EQUAL(ret, false); + + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_interfaces.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_index.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_phy_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_line_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_system_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_empty_ethernet_system_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_empty_ethernet_line_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + + //ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_line_lane_speed.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_system_lane_speed.json", false); + //CU_ASSERT_EQUAL(ret, false); +} + +int unitTestMain() +{ + CU_pSuite pSuite = NULL; + CU_pTest pTest = NULL; + int ret = 0; + + if (CUE_SUCCESS != CU_initialize_registry()) { + printf("%s: cunit failed to initialize registry\n", __FUNCTION__); + ret = CU_get_error(); + } else { + + CU_basic_set_mode(CU_BRM_VERBOSE); + + // Run the tests and show the run summary + pSuite = CU_add_suite("gearbox_config_suite", 0, 0); + if (NULL == pSuite) { + printf("%s: cunit failed to create suite\n", __FUNCTION__); + CU_cleanup_registry(); + ret = CU_get_error(); + } + } + + // Add the test to the suite + if (ret == 0) { + pTest = CU_add_test(pSuite, "gearbox_positive_config_test", positiveConfigTest); + if (pTest != NULL) { + pTest = CU_add_test(pSuite, "gearbox_negative_config_test", negativeConfigTest); + } + + if (pTest == NULL) { + CU_cleanup_registry(); + printf("%s: cunit failed to add test\n", __FUNCTION__); + ret = CU_get_error(); + } + } + + if (ret == 0) { + CU_basic_run_tests(); + } + return ret; +} + +bool handleGearboxConfigFile(std::string file, bool warm) +{ + GearboxParser p; + bool ret; + + p.setWriteToDb(true); + p.setConfigPath(file); + ret = p.parse(); + return ret; +} + +int main(int argc, char **argv) +{ + swss::Logger::linkToDbNative("testgearparser"); + + unitTestMain(); + return 1; +} + diff --git a/gearsyncd/tests/testphyparser.cpp b/gearsyncd/tests/testphyparser.cpp new file mode 100644 index 0000000000..fa66680609 --- /dev/null +++ b/gearsyncd/tests/testphyparser.cpp @@ -0,0 +1,193 @@ +/* + * Copyright 2019 Broadcom Inc. + * + * 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 "phyparser.h" +#include +#include +#include "cunit/Basic.h" +#include "logger.h" + +void usage() +{ + std::cout << "Usage: testphyparser" << std::endl; +} + +bool handlePhyConfigFile(std::string file, bool warm); + +void positiveConfigTest() +{ + bool ret; + + ret = handlePhyConfigFile("tests/configs/positive/phy1_config_1.json", false); + CU_ASSERT_EQUAL(ret, true); + ret = handlePhyConfigFile("tests/configs/positive/phy2_config_1.json", false); + CU_ASSERT_EQUAL(ret, true); +} + +void negativeConfigTest() +{ + bool ret; + + ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_json.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_system_side.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_local_lane_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_tx_polarity.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_rx_polarity.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_tx_lanemap.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_rx_lanemap.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_to_system_lanemap.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_mdio_addr.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_mdio_addr.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_speed.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_fec.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_auto_neg.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_loopback.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_training.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_speed.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_fec.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_auto_neg.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_media_type.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_intf_type.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_loopback.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_training.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_speed.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_fec.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_auto_neg.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_asym_pause.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_media_type.json", false); + CU_ASSERT_EQUAL(ret, false); + + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_lanes.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_lane_speed.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_lanes.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_lane_speed.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_boolean.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_boolean2.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_address.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_array.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_mode.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_name.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_port_ref_clk.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_empty_line_lanes.json", false); + //CU_ASSERT_EQUAL(ret, false); + //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_empty_system_lanes.json", false); + //CU_ASSERT_EQUAL(ret, false); +} + +int unitTestMain() +{ + CU_pSuite pSuite = NULL; + CU_pTest pTest = NULL; + int ret = 0; + + if (CUE_SUCCESS != CU_initialize_registry()) { + printf("%s: cunit failed to initialize registry\n", __FUNCTION__); + ret = CU_get_error(); + } else { + + CU_basic_set_mode(CU_BRM_VERBOSE); + + // Run the tests and show the run summary + pSuite = CU_add_suite("phy_config_suite", 0, 0); + if (NULL == pSuite) { + printf("%s: cunit failed to create suite\n", __FUNCTION__); + CU_cleanup_registry(); + ret = CU_get_error(); + } + } + + // Add the test to the suite + if (ret == 0) { + pTest = CU_add_test(pSuite, "phy_positive_config_test", positiveConfigTest); + + if (pTest != NULL) { + pTest = CU_add_test(pSuite, "phy_negative_config_test", negativeConfigTest); + } + + if (pTest == NULL) { + CU_cleanup_registry(); + printf("%s: cunit failed to add test\n", __FUNCTION__); + ret = CU_get_error(); + } + } + if (ret == 0) { + CU_basic_run_tests(); + } + return ret; +} + +bool handlePhyConfigFile(std::string file, bool warm) +{ + PhyParser p; + bool ret; + + p.setWriteToDb(true); + p.setConfigPath(file); + ret = p.parse(); + return ret; +} + +int main(int argc, char **argv) +{ + swss::Logger::linkToDbNative("testphyparser"); + + unitTestMain(); + return 1; +} + diff --git a/lib/gearboxutils.cpp b/lib/gearboxutils.cpp new file mode 100644 index 0000000000..bcdd9e2896 --- /dev/null +++ b/lib/gearboxutils.cpp @@ -0,0 +1,459 @@ +/* + * Copyright 2019-2020 Broadcom Inc. + * + * 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 "gearboxutils.h" + +namespace swss { + +std::tuple GearboxUtils::parseGearboxKey(std::string key_str) +{ + /* + * The Gearbox key string has a few formats based on the number of tokens. + * Parse and return accordingly; + * phy::ports: rtn: ports + * phy::lanes: rtn: lanes + * phy: rtn: phy + * interface: rtn: interface + */ + + std::string str1, str2, str3; + std::vector token = tokenize(key_str, ':'); + + if (token.size() == 4) + { + str1 = token.at(2); + str2 = token.at(1); + str3 = token.at(3); + } + else if (token.size() == 2) + { + str1 = token.at(0); + str2 = token.at(1); + } + + SWSS_LOG_DEBUG("Parsed key:%s, Return:(%s)(%s)(%s)", key_str.c_str(), str1.c_str(), str2.c_str(), str3.c_str()); + + return make_tuple(str1, str2, str3); +} + +bool GearboxUtils::platformHasGearbox() +{ + bool ret = false; + + if (access("/usr/share/sonic/hwsku/gearbox_config.json", F_OK) != -1) + { + ret = true; + } + return ret; +} + +bool GearboxUtils::isGearboxConfigDone(Table &gearboxTable) +{ + std::vector tuples; + bool gearboxConfigDone = false; + + // this will return false if GearboxConfigDone is not set in the database + + gearboxConfigDone = gearboxTable.get("GearboxConfigDone", tuples); + return gearboxConfigDone; +} + +bool GearboxUtils::isGearboxConfigDone(Table *gearboxTable) +{ + std::vector tuples; + bool gearboxConfigDone = false; + + // this will return false if GearboxConfigDone is not set in the database + + gearboxConfigDone = gearboxTable->get("GearboxConfigDone", tuples); + return gearboxConfigDone; +} + +bool GearboxUtils::isGearboxEnabled(Table *gearboxTable) +{ + int count = 10; // 10 seconds is more than enough time + bool gearboxDone = false; + + SWSS_LOG_ENTER(); + + if (platformHasGearbox() != true) + { + return gearboxDone; + } + + // wait until gearbox table is ready + while (count > 0) + { + if (!isGearboxConfigDone(gearboxTable)) + { + sleep(1); + count--; + continue; + } + else + { + gearboxDone = true; + break; + } + } + + return gearboxDone; +} + +std::map GearboxUtils::loadPhyMap(Table *gearboxTable) +{ + std::vector ovalues; + std::tuple keyt; + std::vector keys; + + SWSS_LOG_ENTER(); + + gearboxTable->getKeys(keys); + + if (keys.empty()) + { + SWSS_LOG_ERROR("No Gearbox records in ApplDB!"); + return gearboxPhyMap; + } + + for (auto &k : keys) + { + keyt = parseGearboxKey(k); + + if (std::get<0>(keyt).compare("phy") == 0) + { + gearbox_phy_t phy = {}; + + gearboxTable->get(k, ovalues); + for (auto &val : ovalues) + { + if (val.first == "phy_id") + { + phy.phy_id = std::stoi(val.second); + } + else if (val.first == "phy_oid") + { + // oid is from create_switch (not config) + phy.phy_oid = val.second; + } + else if (val.first == "name") + { + phy.name = val.second; + } + else if (val.first == "lib_name") + { + phy.lib_name = val.second; + } + else if (val.first == "firmware_path") + { + phy.firmware = val.second; + } + else if (val.first == "config_file") + { + phy.config_file = val.second; + } + else if (val.first == "sai_init_config_file") + { + phy.sai_init_config_file = val.second; + } + else if (val.first == "phy_access") + { + phy.access = val.second; + } + else if (val.first == "address") + { + phy.address = std::stoi(val.second); + } + else if (val.first == "bus_id") + { + phy.bus_id = std::stoi(val.second); + } + } + gearboxPhyMap[phy.phy_id] = phy; + } + } + + return gearboxPhyMap; +} + +std::map GearboxUtils::loadInterfaceMap(Table *gearboxTable) +{ + std::vector ovalues; + std::tuple keyt; + std::vector keys; + + SWSS_LOG_ENTER(); + + gearboxTable->getKeys(keys); + + if (keys.empty()) + { + SWSS_LOG_ERROR("No Gearbox records in ApplDB!"); + return gearboxInterfaceMap; + } + + for (auto &k : keys) + { + keyt = parseGearboxKey(k); + + if (std::get<0>(keyt).compare("interface") == 0) + { + gearbox_interface_t interface = {}; + + gearboxTable->get(k, ovalues); + for (auto &val : ovalues) + { + if (val.first == "index") + { + interface.index = std::stoi(val.second); + SWSS_LOG_DEBUG("BOX interface = %d", interface.index); + } + else if (val.first == "phy_id") + { + interface.phy_id = std::stoi(val.second); + SWSS_LOG_DEBUG("BOX phy_id = %d", interface.phy_id); + } + else if (val.first == "line_lanes") + { + std::stringstream ss(val.second); + + for (int i; ss >> i;) + { + SWSS_LOG_DEBUG("Parsed key:%s, val:%s,inserted %d", val.first.c_str(), val.second.c_str(), i); + interface.line_lanes.insert(i); + if (ss.peek() == ',') + { + ss.ignore(); + } + } + } + else if (val.first == "system_lanes") + { + std::stringstream ss(val.second); + + for (int i; ss >> i;) + { + SWSS_LOG_DEBUG("Parsed key:%s, val:%s,inserted %d", val.first.c_str(), val.second.c_str(), i); + interface.system_lanes.insert(i); + if (ss.peek() == ',') + { + ss.ignore(); + } + } + } + } + gearboxInterfaceMap[interface.index] = interface; + } + } + + return gearboxInterfaceMap; +} + +std::map GearboxUtils::loadLaneMap(Table *gearboxTable) +{ + std::vector ovalues; + std::tuple keyt; + std::vector keys; + + SWSS_LOG_ENTER(); + + gearboxTable->getKeys(keys); + + if (keys.empty()) + { + SWSS_LOG_ERROR("No Gearbox records in ApplDB!"); + return gearboxLaneMap; + } + + for (auto &k : keys) + { + keyt = parseGearboxKey(k); + + if (std::get<0>(keyt).compare("lanes") == 0) + { + gearbox_lane_t lane = {}; + + gearboxTable->get(k, ovalues); + for (auto &val : ovalues) + { + if (val.first == "index") + { + lane.index = std::stoi(val.second); + } + else if (val.first == "tx_polarity") + { + lane.tx_polarity = std::stoi(val.second); + } + else if (val.first == "rx_polarity") + { + lane.rx_polarity = std::stoi(val.second); + } + else if (val.first == "line_tx_lanemap") + { + lane.line_tx_lanemap = std::stoi(val.second); + } + else if (val.first == "line_rx_lanemap") + { + lane.line_rx_lanemap = std::stoi(val.second); + } + else if (val.first == "line_to_system_lanemap") + { + lane.line_to_system_lanemap = std::stoi(val.second); + } + else if (val.first == "mdio_addr") + { + lane.mdio_addr = val.second; + } + else if (val.first == "system_side") + { + lane.system_side = (val.second == "true") ? true : false; + } + } + gearboxLaneMap[lane.index] = lane; + } + } + + return gearboxLaneMap; +} + +std::map GearboxUtils::loadPortMap(Table *gearboxTable) +{ + std::vector ovalues; + std::tuple keyt; + std::vector keys; + + SWSS_LOG_ENTER(); + + gearboxTable->getKeys(keys); + + if (keys.empty()) + { + SWSS_LOG_ERROR("No Gearbox records in ApplDB!"); + return gearboxPortMap; + } + + for (auto &k : keys) + { + keyt = parseGearboxKey(k); + + if (std::get<0>(keyt).compare("ports") == 0) + { + gearbox_port_t port = {}; + + gearboxTable->get(k, ovalues); + for (auto &val : ovalues) + { + if (val.first == "index") + { + port.index = std::stoi(val.second); + } + else if (val.first == "mdio_addr") + { + port.mdio_addr = val.second; + } + else if (val.first == "system_speed") + { + port.system_speed = std::stoi(val.second); + } + else if (val.first == "system_fec") + { + port.system_fec = val.second; + } + else if (val.first == "system_auto_neg") + { + port.system_auto_neg = (val.second == "true") ? true : false; + } + else if (val.first == "system_loopback") + { + port.system_loopback = val.second; + } + else if (val.first == "system_training") + { + port.system_training = (val.second == "true") ? true : false; + } + else if (val.first == "line_speed") + { + port.line_speed = std::stoi(val.second); + } + else if (val.first == "line_fec") + { + port.line_fec = val.second; + } + else if (val.first == "line_auto_neg") + { + port.line_auto_neg = (val.second == "true") ? true : false; + } + else if (val.first == "line_media_type") + { + port.line_media_type = val.second; + } + else if (val.first == "line_intf_type") + { + port.line_intf_type = val.second; + } + else if (val.first == "line_loopback") + { + port.line_loopback = val.second; + } + else if (val.first == "line_training") + { + port.line_training = (val.second == "true") ? true : false; + } + else if (val.first == "line_adver_speed") + { + std::stringstream ss(val.second); + for (int i; ss >> i;) + { + port.line_adver_speed.insert(i); + if (ss.peek() == ',') + { + ss.ignore(); + } + } + } + else if (val.first == "line_adver_fec") + { + std::stringstream ss(val.second); + for (int i; ss >> i;) + { + port.line_adver_fec.insert(i); + if (ss.peek() == ',') + { + ss.ignore(); + } + } + } + else if (val.first == "line_adver_auto_neg") + { + port.line_adver_auto_neg = (val.second == "true") ? true : false; + } + else if (val.first == "line_adver_asym_pause") + { + port.line_adver_asym_pause = (val.second == "true") ? true : false; + } + else if (val.first == "line_adver_media_type") + { + port.line_adver_media_type = val.second; + } + } + gearboxPortMap[port.index] = port; + } + } + + return gearboxPortMap; +} + +} diff --git a/lib/gearboxutils.h b/lib/gearboxutils.h new file mode 100644 index 0000000000..e8ffc85c96 --- /dev/null +++ b/lib/gearboxutils.h @@ -0,0 +1,112 @@ +/* + * Copyright 2019-2020 Broadcom Inc. + * + * 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. + */ + +#ifndef SWSS_COMMON_GEARBOX_UTILS_H +#define SWSS_COMMON_GEARBOX_UTILS_H + +#include +#include +#include +#include +#include +#include +#include + +#include "tokenize.h" +#include "table.h" + +namespace swss { + +typedef struct +{ + int phy_id; + std::string phy_oid; + std::string name; + std::string lib_name; + std::string firmware; + std::string firmware_major_version; + std::string sai_init_config_file; + std::string config_file; + std::string access; + uint32_t address; + uint32_t bus_id; +} gearbox_phy_t; + +typedef struct +{ + int index; + int phy_id; + std::set line_lanes; + std::set system_lanes; +} gearbox_interface_t; + +typedef struct +{ + int index; + bool system_side; + int tx_polarity; + int rx_polarity; + int line_tx_lanemap; + int line_rx_lanemap; + int line_to_system_lanemap; + std::string mdio_addr; +} gearbox_lane_t; + +typedef struct +{ + int index; + std::string mdio_addr; + int system_speed; + std::string system_fec; + bool system_auto_neg; + std::string system_loopback; + bool system_training; + int line_speed; + std::string line_fec; + bool line_auto_neg; + std::string line_media_type; + std::string line_intf_type; + std::string line_loopback; + bool line_training; + std::set line_adver_speed; + std::set line_adver_fec; + bool line_adver_auto_neg; + bool line_adver_asym_pause; + std::string line_adver_media_type; +} gearbox_port_t; + +class GearboxUtils +{ + private: + std::map gearboxPhyMap; + std::map gearboxInterfaceMap; + std::map gearboxLaneMap; + std::map gearboxPortMap; + std::tuple parseGearboxKey(std::string key_str); + public: + bool platformHasGearbox(); + bool isGearboxConfigDone(Table &gearboxTable); + bool isGearboxConfigDone(Table *gearboxTable); + bool isGearboxEnabled(Table *gearboxTable); + std::map loadPhyMap(Table *gearboxTable); + std::map loadInterfaceMap(Table *gearboxTable); + std::map loadLaneMap(Table *gearboxTable); + std::map loadPortMap(Table *gearboxTable); +}; + +} + +#endif /* SWSS_COMMON_GEARBOX_UTILS_H */ diff --git a/orchagent/Makefile.am b/orchagent/Makefile.am index d431557b5c..e4a988b65d 100644 --- a/orchagent/Makefile.am +++ b/orchagent/Makefile.am @@ -1,4 +1,4 @@ -INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I flex_counter -I debug_counter +INCLUDES = -I $(top_srcdir)/lib -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I flex_counter -I debug_counter CFLAGS_SAI = -I /usr/include/sai @@ -25,6 +25,7 @@ endif orchagent_SOURCES = \ main.cpp \ + $(top_srcdir)/lib/gearboxutils.cpp \ orchdaemon.cpp \ orch.cpp \ notifications.cpp \ diff --git a/orchagent/main.cpp b/orchagent/main.cpp index d373c1120a..3292dcb480 100644 --- a/orchagent/main.cpp +++ b/orchagent/main.cpp @@ -23,10 +23,12 @@ extern "C" { #include #include "orchdaemon.h" +#include "sai_serialize.h" #include "saihelper.h" #include "notifications.h" #include #include "warm_restart.h" +#include "gearboxutils.h" using namespace std; using namespace swss; @@ -99,7 +101,43 @@ void syncd_apply_view() { SWSS_LOG_ERROR("Failed to notify syncd APPLY_VIEW %d", status); exit(EXIT_FAILURE); + } +} + +/* + * If Gearbox is enabled... + * Create and initialize the external Gearbox PHYs. Upon success, store the + * new PHY OID in the database to be used later when creating the Gearbox + * ports. +*/ +void init_gearbox_phys(DBConnector *applDb) +{ + Table* tmpGearboxTable = new Table(applDb, "_GEARBOX_TABLE"); + map gearboxPhyMap; + GearboxUtils gearbox; + + if (gearbox.isGearboxEnabled(tmpGearboxTable)) + { + gearboxPhyMap = gearbox.loadPhyMap(tmpGearboxTable); + SWSS_LOG_DEBUG("BOX: gearboxPhyMap size = %d.", (int) gearboxPhyMap.size()); + + for (auto it = gearboxPhyMap.begin(); it != gearboxPhyMap.end(); ++it) + { + SWSS_LOG_NOTICE("BOX: Initialize PHY %d.", it->first); + + if (initSaiPhyApi(&it->second) != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("BOX: Failed to initialize PHY %d.", it->first); + } + else + { + SWSS_LOG_NOTICE("BOX: Created new PHY phy_id:%d phy_oid:%s.", it->second.phy_id, it->second.phy_oid.c_str()); + tmpGearboxTable->hset("phy:"+to_string(it->second.phy_id), "phy_oid", it->second.phy_oid.c_str()); + tmpGearboxTable->hset("phy:"+to_string(it->second.phy_id), "firmware_major_version", it->second.firmware_major_version.c_str()); + } + } } + delete tmpGearboxTable; } int main(int argc, char **argv) @@ -213,6 +251,16 @@ int main(int argc, char **argv) attr.value.ptr = (void *)on_port_state_change; attrs.push_back(attr); +#if 0 + attr.id = SAI_SWITCH_ATTR_HARDWARE_ACCESS_BUS; + attr.value.s32 = SAI_SWITCH_HARDWARE_ACCESS_BUS_MDIO; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_PLATFROM_CONTEXT; + attr.value.u64 = (sai_uint64_t) 0xffffffffffffffff; + attrs.push_back(attr); +#endif + attr.id = SAI_SWITCH_ATTR_SHUTDOWN_REQUEST_NOTIFY; attr.value.ptr = (void *)on_switch_shutdown_request; attrs.push_back(attr); @@ -224,6 +272,11 @@ int main(int argc, char **argv) attrs.push_back(attr); } + /* Must be last Attribute */ + attr.id = SAI_REDIS_SWITCH_ATTR_CONTEXT; + attr.value.u64 = gSwitchId; + attrs.push_back(attr); + // SAI_REDIS_SWITCH_ATTR_SYNC_MODE attribute only setBuffer and g_syncMode to true // since it is not using ASIC_DB, we can execute it before create_switch // when g_syncMode is set to true here, create_switch will wait the response from syncd @@ -258,7 +311,7 @@ int main(int argc, char **argv) status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr); if (status != SAI_STATUS_SUCCESS) { - SWSS_LOG_ERROR("Failed to get MAC address from switch, rv:%d", status); + SWSS_LOG_ERROR("Fail to get MAC address from switch, rv:%d", status); exit(EXIT_FAILURE); } else @@ -326,6 +379,8 @@ int main(int argc, char **argv) DBConnector config_db("CONFIG_DB", 0); DBConnector state_db("STATE_DB", 0); + init_gearbox_phys(&appl_db); + auto orchDaemon = make_shared(&appl_db, &config_db, &state_db); if (!orchDaemon->init()) diff --git a/orchagent/portsorch.cpp b/orchagent/portsorch.cpp index cdd28e4999..d41ae931ab 100755 --- a/orchagent/portsorch.cpp +++ b/orchagent/portsorch.cpp @@ -2,6 +2,7 @@ #include "intfsorch.h" #include "bufferorch.h" #include "neighorch.h" +#include "gearboxutils.h" #include #include @@ -74,6 +75,32 @@ static map learn_mode_map = { "notification", SAI_BRIDGE_PORT_FDB_LEARNING_MODE_FDB_NOTIFICATION} }; +static map media_type_map = +{ + { "fiber", SAI_PORT_MEDIA_TYPE_FIBER }, + { "copper", SAI_PORT_MEDIA_TYPE_COPPER } +}; + +static map loopback_mode_map = +{ + { "none", SAI_PORT_INTERNAL_LOOPBACK_MODE_NONE }, + { "phy", SAI_PORT_INTERNAL_LOOPBACK_MODE_PHY }, + { "mac", SAI_PORT_INTERNAL_LOOPBACK_MODE_MAC } +}; + +static map interface_type_map = +{ + { "none", SAI_PORT_INTERFACE_TYPE_NONE }, + { "cr", SAI_PORT_INTERFACE_TYPE_CR }, + { "cr4", SAI_PORT_INTERFACE_TYPE_CR4 }, + { "sr", SAI_PORT_INTERFACE_TYPE_SR }, + { "sr4", SAI_PORT_INTERFACE_TYPE_SR4 }, + { "lr", SAI_PORT_INTERFACE_TYPE_LR }, + { "lr4", SAI_PORT_INTERFACE_TYPE_LR4 }, + { "kr", SAI_PORT_INTERFACE_TYPE_KR }, + { "kr4", SAI_PORT_INTERFACE_TYPE_KR4 } +}; + const vector port_stat_ids = { SAI_PORT_STAT_IF_IN_OCTETS, @@ -143,14 +170,16 @@ static char* hostif_vlan_tag[] = { }; /* * Initialize PortsOrch - * 0) By default, a switch has one CPU port, one 802.1Q bridge, and one default + * 0) If Gearbox is enabled, then initialize the external PHYs as defined in + * the GEARBOX_TABLE. + * 1) By default, a switch has one CPU port, one 802.1Q bridge, and one default * VLAN. All ports are in .1Q bridge as bridge ports, and all bridge ports * are in default VLAN as VLAN members. - * 1) Query switch CPU port. - * 2) Query ports associated with lane mappings - * 3) Query switch .1Q bridge and all its bridge ports. - * 4) Query switch default VLAN and all its VLAN members. - * 5) Remove each VLAN member from default VLAN and each bridge port from .1Q + * 2) Query switch CPU port. + * 3) Query ports associated with lane mappings + * 4) Query switch .1Q bridge and all its bridge ports. + * 5) Query switch default VLAN and all its VLAN members. + * 6) Remove each VLAN member from default VLAN and each bridge port from .1Q * bridge. By design, SONiC switch starts with all bridge ports removed from * default VLAN and all ports removed from .1Q bridge. */ @@ -174,6 +203,9 @@ PortsOrch::PortsOrch(DBConnector *db, vector &tableNames) /* Initialize port table */ m_portTable = unique_ptr(new Table(db, APP_PORT_TABLE_NAME)); + /* Initialize gearbox */ + m_gearboxTable = unique_ptr
(new Table(db, "_GEARBOX_TABLE")); + /* Initialize queue tables */ m_queueTable = unique_ptr
(new Table(m_counter_db.get(), COUNTERS_QUEUE_NAME_MAP)); m_queuePortTable = unique_ptr
(new Table(m_counter_db.get(), COUNTERS_QUEUE_PORT_MAP)); @@ -189,6 +221,8 @@ PortsOrch::PortsOrch(DBConnector *db, vector &tableNames) m_flexCounterTable = unique_ptr(new ProducerTable(m_flex_db.get(), FLEX_COUNTER_TABLE)); m_flexCounterGroupTable = unique_ptr(new ProducerTable(m_flex_db.get(), FLEX_COUNTER_GROUP_TABLE)); + initGearbox(); + string queueWmSha, pgWmSha; string queueWmPluginName = "watermark_queue.lua"; string pgWmPluginName = "watermark_pg.lua"; @@ -425,6 +459,38 @@ bool PortsOrch::isConfigDone() return m_portConfigState == PORT_CONFIG_DONE; } +/* Use this method to retrieve the desired port if the destination port is a Gearbox port. + * For example, if Gearbox is enabled on a specific physical interface, + * the destination port may be the PHY or LINE side of the external PHY. + * The original port id is returned if it's not a Gearbox configured port. + */ +bool PortsOrch::getDestPortId(sai_object_id_t src_port_id, dest_port_type_t port_type, sai_object_id_t &des_port_id) +{ + bool status = false; + des_port_id = src_port_id; + + if (m_gearboxEnabled) + { + if (m_gearboxPortListLaneMap.find(src_port_id) != m_gearboxPortListLaneMap.end()) + { + if (PHY_PORT_TYPE == port_type) + { + des_port_id = get<0>(m_gearboxPortListLaneMap[src_port_id]); + SWSS_LOG_DEBUG("BOX: port id:%" PRIx64 " has a phy-side port id:%" PRIx64, src_port_id, des_port_id); + status = true; + } + else if (LINE_PORT_TYPE == port_type) + { + des_port_id = get<1>(m_gearboxPortListLaneMap[src_port_id]); + SWSS_LOG_DEBUG("BOX: port id:%" PRIx64 " has a line-side port id:%" PRIx64, src_port_id, des_port_id); + status = true; + } + } + } + + return status; +} + bool PortsOrch::isPortAdminUp(const string &alias) { auto it = m_portList.find(alias); @@ -679,23 +745,26 @@ void PortsOrch::getCpuPort(Port &port) port = m_cpuPort; } -bool PortsOrch::setPortAdminStatus(sai_object_id_t id, bool up) +bool PortsOrch::setPortAdminStatus(Port &port, bool state) { SWSS_LOG_ENTER(); sai_attribute_t attr; attr.id = SAI_PORT_ATTR_ADMIN_STATE; - attr.value.booldata = up; + attr.value.booldata = state; - sai_status_t status = sai_port_api->set_port_attribute(id, &attr); - if (status != SAI_STATUS_SUCCESS) + if (sai_port_api->set_port_attribute(port.m_port_id, &attr) != SAI_STATUS_SUCCESS) { SWSS_LOG_ERROR("Failed to set admin status %s to port pid:%" PRIx64, - up ? "UP" : "DOWN", id); + state ? "UP" : "DOWN", port.m_port_id); return false; } + SWSS_LOG_INFO("Set admin status %s to port pid:%" PRIx64, - up ? "UP" : "DOWN", id); + state ? "UP" : "DOWN", port.m_port_id); + + setGearboxPortsAttr(port, SAI_PORT_ATTR_ADMIN_STATE, &state); + return true; } @@ -703,6 +772,8 @@ bool PortsOrch::getPortAdminStatus(sai_object_id_t id, bool &up) { SWSS_LOG_ENTER(); + getDestPortId(id, LINE_PORT_TYPE, id); + sai_attribute_t attr; attr.id = SAI_PORT_ATTR_ADMIN_STATE; @@ -738,7 +809,7 @@ bool PortsOrch::setPortMtu(sai_object_id_t id, sai_uint32_t mtu) return true; } -bool PortsOrch::setPortFec(sai_object_id_t id, sai_port_fec_mode_t mode) +bool PortsOrch::setPortFec(Port &port, sai_port_fec_mode_t mode) { SWSS_LOG_ENTER(); @@ -746,15 +817,16 @@ bool PortsOrch::setPortFec(sai_object_id_t id, sai_port_fec_mode_t mode) attr.id = SAI_PORT_ATTR_FEC_MODE; attr.value.s32 = mode; - sai_status_t status = sai_port_api->set_port_attribute(id, &attr); - if (status != SAI_STATUS_SUCCESS) + if (sai_port_api->set_port_attribute(port.m_port_id, &attr) != SAI_STATUS_SUCCESS) { - SWSS_LOG_ERROR("Failed to set fec mode %d to port pid:%" PRIx64, - mode, id); + SWSS_LOG_ERROR("Failed to set fec mode %d to port pid:%" PRIx64, mode, port.m_port_id); return false; } - SWSS_LOG_INFO("Set fec mode %d to port pid:%" PRIx64, - mode, id); + + SWSS_LOG_INFO("Set fec mode %d to port pid:%" PRIx64, mode, port.m_port_id); + + setGearboxPortsAttr(port, SAI_PORT_ATTR_FEC_MODE, &mode); + return true; } @@ -1386,31 +1458,147 @@ bool PortsOrch::isSpeedSupported(const std::string& alias, sai_object_id_t port_ return std::find(supp_speeds.begin(), supp_speeds.end(), speed) != supp_speeds.end(); } -bool PortsOrch::setPortSpeed(sai_object_id_t port_id, sai_uint32_t speed) +/* + * If Gearbox is enabled and this is a Gearbox port then set the attributes accordingly. + */ +bool PortsOrch::setGearboxPortsAttr(Port &port, sai_port_attr_t id, void *value) { + bool status; + + status = setGearboxPortAttr(port, PHY_PORT_TYPE, id, value); + + if (status == true) + { + status = setGearboxPortAttr(port, LINE_PORT_TYPE, id, value); + } + + return status; +} + +/* + * If Gearbox is enabled and this is a Gearbox port then set the specific lane attribute. + * Note: the appl_db is also updated (Gearbox config_db tables are TBA). + */ +bool PortsOrch::setGearboxPortAttr(Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value) +{ + sai_status_t status = SAI_STATUS_SUCCESS; + sai_object_id_t dest_port_id; + sai_attribute_t attr; + string speed_attr; + uint32_t lane_speed = 0; + sai_uint32_t speed = 0; + uint32_t lanes = 0; + SWSS_LOG_ENTER(); + + if (m_gearboxEnabled) + { + if (getDestPortId(port.m_port_id, port_type, dest_port_id) == true) + { + switch (id) + { + case SAI_PORT_ATTR_FEC_MODE: + attr.id = id; + attr.value.s32 = *static_cast(value); + SWSS_LOG_NOTICE("BOX: Set %s FEC_MODE %d", port.m_alias.c_str(), attr.value.s32); + break; + case SAI_PORT_ATTR_ADMIN_STATE: + attr.id = id; + attr.value.booldata = *static_cast(value); + SWSS_LOG_NOTICE("BOX: Set %s ADMIN_STATE %d", port.m_alias.c_str(), attr.value.booldata); + break; + case SAI_PORT_ATTR_SPEED: + switch (port_type) + { + case PHY_PORT_TYPE: + lanes = static_cast(m_gearboxInterfaceMap[port.m_index].system_lanes.size()); + speed_attr = "system_speed"; + break; + case LINE_PORT_TYPE: + lanes = static_cast(m_gearboxInterfaceMap[port.m_index].line_lanes.size()); + speed_attr = "line_speed"; + break; + default: + return false; + } + + // Gearbox expects speed per lane + speed = *static_cast(value); + if (speed % lanes == 0) + { + lane_speed = speed / lanes; + } + if (isSpeedSupported(port.m_alias, dest_port_id, lane_speed)) + { + // Gearbox may not implement speed check, so + // invalidate speed if it doesn't make sense. + if (to_string(lane_speed).size() < 5) + { + lane_speed = 0; + } + + attr.id = SAI_PORT_ATTR_SPEED; + attr.value.u32 = lane_speed; + } + SWSS_LOG_NOTICE("BOX: Set %s lane %s %d", port.m_alias.c_str(), speed_attr.c_str(), lane_speed); + break; + default: + return false; + } + + status = sai_port_api->set_port_attribute(dest_port_id, &attr); + if (status == SAI_STATUS_SUCCESS) + { + if (id == SAI_PORT_ATTR_SPEED) + { + string key = "phy:"+to_string(m_gearboxInterfaceMap[port.m_index].phy_id)+":ports:"+to_string(port.m_index); + m_gearboxTable->hset(key, speed_attr, to_string(lane_speed)); + SWSS_LOG_NOTICE("BOX: Updated APPL_DB key:%s %s %d", key.c_str(), speed_attr.c_str(), lane_speed); + } + } + else + { + SWSS_LOG_ERROR("BOX: Failed to set %s port attribute %d", port.m_alias.c_str(), id); + } + } + } + + return status == SAI_STATUS_SUCCESS; +} + +bool PortsOrch::setPortSpeed(Port &port, sai_uint32_t speed) +{ sai_attribute_t attr; sai_status_t status; + SWSS_LOG_ENTER(); + attr.id = SAI_PORT_ATTR_SPEED; attr.value.u32 = speed; - status = sai_port_api->set_port_attribute(port_id, &attr); + status = sai_port_api->set_port_attribute(port.m_port_id, &attr); + + if (status == SAI_STATUS_SUCCESS) + { + setGearboxPortsAttr(port, SAI_PORT_ATTR_SPEED, &speed); + } return status == SAI_STATUS_SUCCESS; } -bool PortsOrch::getPortSpeed(sai_object_id_t port_id, sai_uint32_t &speed) +bool PortsOrch::getPortSpeed(sai_object_id_t id, sai_uint32_t &speed) { SWSS_LOG_ENTER(); + getDestPortId(id, LINE_PORT_TYPE, id); + sai_attribute_t attr; sai_status_t status; attr.id = SAI_PORT_ATTR_SPEED; attr.value.u32 = 0; - status = sai_port_api->get_port_attribute(port_id, 1, &attr); + status = sai_port_api->get_port_attribute(id, 1, &attr); if (status == SAI_STATUS_SUCCESS) speed = attr.value.u32; @@ -1602,7 +1790,7 @@ string PortsOrch::getPriorityGroupWatermarkFlexCounterTableKey(string key) return string(PG_WATERMARK_STAT_COUNTER_FLEX_COUNTER_GROUP) + ":" + key; } -bool PortsOrch::initPort(const string &alias, const set &lane_set) +bool PortsOrch::initPort(const string &alias, const int index, const set &lane_set) { SWSS_LOG_ENTER(); @@ -1620,15 +1808,20 @@ bool PortsOrch::initPort(const string &alias, const set &lane_set) { Port p(alias, Port::PHY); - p.m_index = static_cast(m_portList.size()); // TODO: Assume no deletion of physical port + p.m_index = index; p.m_port_id = id; /* Initialize the port and create corresponding host interface */ if (initializePort(p)) { + /* Create associated Gearbox lane mapping */ + initGearboxPort(p); + /* Add port to port list */ m_portList[alias] = p; m_port_ref_count[alias] = 0; + m_portOidToIndex[id] = index; + /* Add port name map to counter table */ FieldValueTuple tuple(p.m_alias, sai_serialize_object_id(p.m_port_id)); vector fields; @@ -1648,7 +1841,7 @@ bool PortsOrch::initPort(const string &alias, const set &lane_set) m_portList[alias].m_init = true; - SWSS_LOG_NOTICE("Initialized port %s", alias.c_str()); + SWSS_LOG_ERROR("Initialized port %s", alias.c_str()); } else { @@ -1847,9 +2040,16 @@ void PortsOrch::doPortTask(Consumer &consumer) uint32_t speed = 0; string learn_mode; int an = -1; + int index = -1; for (auto i : kfvFieldsValues(t)) { + /* Set interface index */ + if (fvField(i) == "index") + { + index = (int)stoul(fvValue(i)); + } + /* Get lane information of a physical port and initialize the port */ if (fvField(i) == "lanes") { @@ -1926,7 +2126,7 @@ void PortsOrch::doPortTask(Consumer &consumer) /* Collect information about all received ports */ if (lane_set.size()) { - m_lanesAliasSpeedMap[lane_set] = make_tuple(alias, speed, an, fec_mode); + m_lanesAliasSpeedMap[lane_set] = make_tuple(alias, speed, an, fec_mode, index); } // TODO: @@ -1968,7 +2168,7 @@ void PortsOrch::doPortTask(Consumer &consumer) } } - if (!initPort(get<0>(it->second), it->first)) + if (!initPort(get<0>(it->second), get<4>(it->second), it->first)) { throw runtime_error("PortsOrch initialization failure."); } @@ -2051,65 +2251,75 @@ void PortsOrch::doPortTask(Consumer &consumer) * 3. Set port admin status to DOWN before changing the speed * 4. Set port speed */ - if (speed != 0 && speed != p.m_speed) - { - m_portList[alias] = p; - if (p.m_autoneg) - { - if (setPortAdvSpeed(p.m_port_id, speed)) - { - SWSS_LOG_NOTICE("Set port %s advertised speed to %u", alias.c_str(), speed); - } - else - { - SWSS_LOG_ERROR("Failed to set port %s advertised speed to %u", alias.c_str(), speed); - it++; - continue; - } - } - else + SWSS_LOG_DEBUG("Set port %s speed to %u -> %u", alias.c_str(), p.m_speed, speed); + if (speed != 0) + { + if (speed != p.m_speed) { - if (!isSpeedSupported(alias, p.m_port_id, speed)) - { - it++; - continue; - } + m_portList[alias] = p; - if (p.m_admin_state_up) + if (p.m_autoneg) { - /* Bring port down before applying speed */ - if (!setPortAdminStatus(p.m_port_id, false)) + if (setPortAdvSpeed(p.m_port_id, speed)) { - SWSS_LOG_ERROR("Failed to set port %s admin status DOWN to set speed", alias.c_str()); - it++; - continue; + SWSS_LOG_NOTICE("Set port %s advertised speed to %u", alias.c_str(), speed); } - - p.m_admin_state_up = false; - m_portList[alias] = p; - - if (!setPortSpeed(p.m_port_id, speed)) + else { - SWSS_LOG_ERROR("Failed to set port %s speed to %u", alias.c_str(), speed); + SWSS_LOG_ERROR("Failed to set port %s advertised speed to %u", alias.c_str(), speed); it++; continue; } } else { - /* Port is already down, setting speed */ - if (!setPortSpeed(p.m_port_id, speed)) + if (!isSpeedSupported(alias, p.m_port_id, speed)) { - SWSS_LOG_ERROR("Failed to set port %s speed to %u", alias.c_str(), speed); it++; continue; } + + if (p.m_admin_state_up) + { + /* Bring port down before applying speed */ + if (!setPortAdminStatus(p, false)) + { + SWSS_LOG_ERROR("Failed to set port %s admin status DOWN to set speed", alias.c_str()); + it++; + continue; + } + + p.m_admin_state_up = false; + m_portList[alias] = p; + + if (!setPortSpeed(p, speed)) + { + SWSS_LOG_ERROR("Failed to set port %s speed to %u", alias.c_str(), speed); + it++; + continue; + } + } + else + { + /* Port is already down, setting speed */ + if (!setPortSpeed(p, speed)) + { + SWSS_LOG_ERROR("Failed to set port %s speed to %u", alias.c_str(), speed); + it++; + continue; + } + } + SWSS_LOG_NOTICE("Set port %s speed to %u", alias.c_str(), speed); } - SWSS_LOG_NOTICE("Set port %s speed to %u", alias.c_str(), speed); + p.m_speed = speed; + m_portList[alias] = p; + } + else + { + /* Always update Gearbox speed on Gearbox ports */ + setGearboxPortsAttr(p, SAI_PORT_ATTR_SPEED, &speed); } - p.m_speed = speed; - m_portList[alias] = p; } if (mtu != 0 && mtu != p.m_mtu) @@ -2140,7 +2350,7 @@ void PortsOrch::doPortTask(Consumer &consumer) if (p.m_fec_mode != fec_mode_map[fec_mode]) { p.m_fec_mode = fec_mode_map[fec_mode]; - if (setPortFec(p.m_port_id, p.m_fec_mode)) + if (setPortFec(p, p.m_fec_mode)) { m_portList[alias] = p; SWSS_LOG_NOTICE("Set port %s fec to %s", alias.c_str(), fec_mode.c_str()); @@ -2247,7 +2457,7 @@ void PortsOrch::doPortTask(Consumer &consumer) /* Last step set port admin status */ if (!admin_status.empty() && (p.m_admin_state_up != (admin_status == "up"))) { - if (setPortAdminStatus(p.m_port_id, admin_status == "up")) + if (setPortAdminStatus(p, admin_status == "up")) { p.m_admin_state_up = (admin_status == "up"); m_portList[alias] = p; @@ -4005,3 +4215,226 @@ void PortsOrch::getPortSerdesVal(const std::string& val_str, lane_values.push_back(lane_val); } } + +/* + * If Gearbox is enabled (wait for GearboxConfigDone), + * then initialize global storage maps + */ +void PortsOrch::initGearbox() +{ + GearboxUtils gearbox; + Table* tmpGearboxTable = m_gearboxTable.get(); + m_gearboxEnabled = gearbox.isGearboxEnabled(tmpGearboxTable); + + SWSS_LOG_ENTER(); + + if (m_gearboxEnabled) + { + m_gearboxPhyMap = gearbox.loadPhyMap(tmpGearboxTable); + m_gearboxInterfaceMap = gearbox.loadInterfaceMap(tmpGearboxTable); + m_gearboxLaneMap = gearbox.loadLaneMap(tmpGearboxTable); + m_gearboxPortMap = gearbox.loadPortMap(tmpGearboxTable); + + SWSS_LOG_NOTICE("BOX: m_gearboxPhyMap size = %d.", (int) m_gearboxPhyMap.size()); + SWSS_LOG_NOTICE("BOX: m_gearboxInterfaceMap size = %d.", (int) m_gearboxInterfaceMap.size()); + SWSS_LOG_NOTICE("BOX: m_gearboxLaneMap size = %d.", (int) m_gearboxLaneMap.size()); + SWSS_LOG_NOTICE("BOX: m_gearboxPortMap size = %d.", (int) m_gearboxPortMap.size()); + } +} + +/* + * Create both the system-side and line-side gearbox ports for the associated + * PHY and connect the ports. + * + */ +bool PortsOrch::initGearboxPort(Port &port) +{ + vector attrs; + vector lanes; + vector vals; + sai_attribute_t attr; + sai_object_id_t systemPort; + sai_object_id_t linePort; + sai_object_id_t connector; + sai_object_id_t phyOid; + sai_status_t status; + string phyOidStr; + int phy_id; + + SWSS_LOG_ENTER(); + + if (m_gearboxEnabled) + { + if (m_gearboxInterfaceMap.find(port.m_index) != m_gearboxInterfaceMap.end()) + { + SWSS_LOG_NOTICE("BOX: port_id:0x%lx index:%d alias:%s", port.m_port_id, port.m_index, port.m_alias.c_str()); + + phy_id = m_gearboxInterfaceMap[port.m_index].phy_id; + phyOidStr = m_gearboxPhyMap[phy_id].phy_oid; + + if (phyOidStr.size() == 0) + { + SWSS_LOG_ERROR("BOX: Gearbox PHY phy_id:%d has an invalid phy_oid", phy_id); + return false; + } + + sai_deserialize_object_id(phyOidStr, phyOid); + + /* Create SYSTEM-SIDE port */ + attrs.clear(); + + attr.id = SAI_PORT_ATTR_ADMIN_STATE; + attr.value.booldata = port.m_admin_state_up; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_SPEED; + attr.value.u32 = (uint32_t) m_gearboxPortMap[port.m_index].system_speed; + if (isSpeedSupported(port.m_alias, port.m_port_id, attr.value.u32)) + { + attrs.push_back(attr); + } + + attr.id = SAI_PORT_ATTR_AUTO_NEG_MODE; + attr.value.booldata = m_gearboxPortMap[port.m_index].system_auto_neg; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_FEC_MODE; + attr.value.s32 = fec_mode_map[m_gearboxPortMap[port.m_index].system_fec]; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_INTERNAL_LOOPBACK_MODE; + attr.value.u32 = loopback_mode_map[m_gearboxPortMap[port.m_index].system_loopback]; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_LINK_TRAINING_ENABLE; + attr.value.booldata = m_gearboxPortMap[port.m_index].system_training; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_HW_LANE_LIST; + lanes.assign(m_gearboxInterfaceMap[port.m_index].system_lanes.begin(), m_gearboxInterfaceMap[port.m_index].system_lanes.end()); + attr.value.u32list.list = lanes.data(); + attr.value.u32list.count = static_cast(lanes.size()); + attrs.push_back(attr); + + for (uint32_t i = 0; i < attr.value.u32list.count; i++) + { + SWSS_LOG_DEBUG("BOX: list[%d] = %d", i, attr.value.u32list.list[i]); + } + + status = sai_port_api->create_port(&systemPort, phyOid, static_cast(attrs.size()), attrs.data()); + if (status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("BOX: Failed to create Gearbox system-side port for alias:%s port_id:0x%lx index:%d status:%d", + port.m_alias.c_str(), port.m_port_id, port.m_index, status); + return false; + } + SWSS_LOG_NOTICE("BOX: Created Gearbox system-side port 0x%lx for alias:%s index:%d", + systemPort, port.m_alias.c_str(), port.m_index); + + /* Create LINE-SIDE port */ + attrs.clear(); + + attr.id = SAI_PORT_ATTR_ADMIN_STATE; + attr.value.booldata = port.m_admin_state_up; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_SPEED; + attr.value.u32 = (uint32_t) m_gearboxPortMap[port.m_index].line_speed; + if (isSpeedSupported(port.m_alias, port.m_port_id, attr.value.u32)) + { + attrs.push_back(attr); + } + + attr.id = SAI_PORT_ATTR_AUTO_NEG_MODE; + attr.value.booldata = m_gearboxPortMap[port.m_index].line_auto_neg; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_FEC_MODE; + attr.value.s32 = fec_mode_map[m_gearboxPortMap[port.m_index].line_fec]; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_MEDIA_TYPE; + attr.value.u32 = media_type_map[m_gearboxPortMap[port.m_index].line_media_type]; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_INTERNAL_LOOPBACK_MODE; + attr.value.u32 = loopback_mode_map[m_gearboxPortMap[port.m_index].line_loopback]; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_LINK_TRAINING_ENABLE; + attr.value.booldata = m_gearboxPortMap[port.m_index].line_training; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_INTERFACE_TYPE; + attr.value.u32 = interface_type_map[m_gearboxPortMap[port.m_index].line_intf_type]; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_ADVERTISED_SPEED; + vals.assign(m_gearboxPortMap[port.m_index].line_adver_speed.begin(), m_gearboxPortMap[port.m_index].line_adver_speed.end()); + attr.value.u32list.list = vals.data(); + attr.value.u32list.count = static_cast(vals.size()); + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_ADVERTISED_FEC_MODE; + vals.assign(m_gearboxPortMap[port.m_index].line_adver_fec.begin(), m_gearboxPortMap[port.m_index].line_adver_fec.end()); + attr.value.u32list.list = vals.data(); + attr.value.u32list.count = static_cast(vals.size()); + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_ADVERTISED_AUTO_NEG_MODE; + attr.value.booldata = m_gearboxPortMap[port.m_index].line_adver_auto_neg; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_ADVERTISED_ASYMMETRIC_PAUSE_MODE; + attr.value.booldata = m_gearboxPortMap[port.m_index].line_adver_asym_pause; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_ADVERTISED_MEDIA_TYPE; + attr.value.u32 = media_type_map[m_gearboxPortMap[port.m_index].line_adver_media_type]; + attrs.push_back(attr); + + attr.id = SAI_PORT_ATTR_HW_LANE_LIST; + lanes.assign(m_gearboxInterfaceMap[port.m_index].line_lanes.begin(), m_gearboxInterfaceMap[port.m_index].line_lanes.end()); + attr.value.u32list.list = lanes.data(); + attr.value.u32list.count = static_cast(lanes.size()); + attrs.push_back(attr); + + for (uint32_t i = 0; i < attr.value.u32list.count; i++) + { + SWSS_LOG_DEBUG("BOX: list[%d] = %d", i, attr.value.u32list.list[i]); + } + + status = sai_port_api->create_port(&linePort, phyOid, static_cast(attrs.size()), attrs.data()); + if (status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("BOX: Failed to create Gearbox line-side port for alias:%s port_id:0x%lx index:%d status:%d", + port.m_alias.c_str(), port.m_port_id, port.m_index, status); + return false; + } + SWSS_LOG_NOTICE("BOX: Created Gearbox line-side port 0x%lx for alias:%s index:%d", + linePort, port.m_alias.c_str(), port.m_index); + + /* Connect SYSTEM-SIDE to LINE-SIDE */ + attrs.clear(); + + attr.id = SAI_PORT_CONNECTOR_ATTR_SYSTEM_SIDE_PORT_ID; + attr.value.oid = systemPort; + attrs.push_back(attr); + attr.id = SAI_PORT_CONNECTOR_ATTR_LINE_SIDE_PORT_ID; + attr.value.oid = linePort; + attrs.push_back(attr); + + status = sai_port_api->create_port_connector(&connector, phyOid, static_cast(attrs.size()), attrs.data()); + if (status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("BOX: Failed to connect Gearbox system-side:0x%lx to line-side:0x%lx; status:%d", systemPort, linePort, status); + return false; + } + + SWSS_LOG_NOTICE("BOX: Connected Gearbox ports; system-side:0x%lx to line-side:0x%lx", systemPort, linePort); + m_gearboxPortListLaneMap[port.m_port_id] = make_tuple(systemPort, linePort); + } + } + return true; +} + diff --git a/orchagent/portsorch.h b/orchagent/portsorch.h index 5b7662470a..0e04a291a8 100755 --- a/orchagent/portsorch.h +++ b/orchagent/portsorch.h @@ -10,6 +10,9 @@ #include "macaddress.h" #include "producertable.h" #include "flex_counter_manager.h" +#include "gearboxutils.h" +#include "saihelper.h" + #define FCS_LEN 4 #define VLAN_TAG_LEN 4 @@ -125,6 +128,7 @@ class PortsOrch : public Orch, public Subject unique_ptr
m_counterTable; unique_ptr
m_counterLagTable; unique_ptr
m_portTable; + unique_ptr
m_gearboxTable; unique_ptr
m_queueTable; unique_ptr
m_queuePortTable; unique_ptr
m_queueIndexTable; @@ -159,11 +163,26 @@ class PortsOrch : public Orch, public Subject PORT_CONFIG_DONE, } port_config_state_t; + typedef enum + { + MAC_PORT_TYPE, + PHY_PORT_TYPE, + LINE_PORT_TYPE, + } dest_port_type_t; + + bool m_gearboxEnabled = false; + map m_gearboxPhyMap; + map m_gearboxInterfaceMap; + map m_gearboxLaneMap; + map m_gearboxPortMap; + map> m_gearboxPortListLaneMap; + port_config_state_t m_portConfigState = PORT_CONFIG_MISSING; sai_uint32_t m_portCount; map, sai_object_id_t> m_portListLaneMap; - map, tuple> m_lanesAliasSpeedMap; + map, tuple> m_lanesAliasSpeedMap; map m_portList; + unordered_map m_portOidToIndex; map m_port_ref_count; unordered_set m_pendingPortSet; @@ -210,22 +229,25 @@ class PortsOrch : public Orch, public Subject bool addPort(const set &lane_set, uint32_t speed, int an=0, string fec=""); sai_status_t removePort(sai_object_id_t port_id); - bool initPort(const string &alias, const set &lane_set); + bool initPort(const string &alias, const int index, const set &lane_set); void deInitPort(string alias, sai_object_id_t port_id); - bool setPortAdminStatus(sai_object_id_t id, bool up); + bool setPortAdminStatus(Port &port, bool up); bool getPortAdminStatus(sai_object_id_t id, bool& up); bool setPortMtu(sai_object_id_t id, sai_uint32_t mtu); bool setPortPvid (Port &port, sai_uint32_t pvid); bool getPortPvid(Port &port, sai_uint32_t &pvid); - bool setPortFec(sai_object_id_t id, sai_port_fec_mode_t mode); + bool setPortFec(Port &port, sai_port_fec_mode_t mode); bool setPortPfcAsym(Port &port, string pfc_asym); + bool getDestPortId(sai_object_id_t src_port_id, dest_port_type_t port_type, sai_object_id_t &des_port_id); bool setBridgePortAdminStatus(sai_object_id_t id, bool up); bool isSpeedSupported(const std::string& alias, sai_object_id_t port_id, sai_uint32_t speed); - bool setPortSpeed(sai_object_id_t port_id, sai_uint32_t speed); - bool getPortSpeed(sai_object_id_t port_id, sai_uint32_t &speed); + bool setPortSpeed(Port &port, sai_uint32_t speed); + bool getPortSpeed(sai_object_id_t id, sai_uint32_t &speed); + bool setGearboxPortsAttr(Port &port, sai_port_attr_t id, void *value); + bool setGearboxPortAttr(Port &port, dest_port_type_t port_type, sai_port_attr_t id, void *value); bool setPortAdvSpeed(sai_object_id_t port_id, sai_uint32_t speed); @@ -249,6 +271,8 @@ class PortsOrch : public Orch, public Subject vector &serdes_val); bool getSaiAclBindPointType(Port::Type type, sai_acl_bind_point_type_t &sai_acl_bind_type); + void initGearbox(); + bool initGearboxPort(Port &port); }; #endif /* SWSS_PORTSORCH_H */ diff --git a/orchagent/saihelper.cpp b/orchagent/saihelper.cpp index ec990d605e..7372ec48c7 100644 --- a/orchagent/saihelper.cpp +++ b/orchagent/saihelper.cpp @@ -5,16 +5,23 @@ extern "C" { #include "saiextensions.h" } +#include #include #include #include #include +#include +#include +#include #include "timestamp.h" +#include "sai_serialize.h" #include "saihelper.h" using namespace std; using namespace swss; +#define CONTEXT_CFG_FILE "/usr/share/sonic/hwsku/context_config.json" + /* Initialize all global api pointers */ sai_switch_api_t* sai_switch_api; sai_bridge_api_t* sai_bridge_api; @@ -51,8 +58,29 @@ extern bool gSwssRecord; extern ofstream gRecordOfs; extern string gRecordFile; +static map hardware_access_map = +{ + { "mdio", SAI_SWITCH_HARDWARE_ACCESS_BUS_MDIO }, + { "i2c", SAI_SWITCH_HARDWARE_ACCESS_BUS_I2C }, + { "cpld", SAI_SWITCH_HARDWARE_ACCESS_BUS_CPLD } +}; + map gProfileMap; +sai_status_t mdio_read(uint64_t platform_context, + uint32_t mdio_addr, uint32_t reg_addr, + uint32_t number_of_registers, uint32_t *data) +{ + return SAI_STATUS_FAILURE; +} + +sai_status_t mdio_write(uint64_t platform_context, + uint32_t mdio_addr, uint32_t reg_addr, + uint32_t number_of_registers, uint32_t *data) +{ + return SAI_STATUS_FAILURE; +} + const char *test_profile_get_value ( _In_ sai_switch_profile_id_t profile_id, _In_ const char *variable) @@ -106,6 +134,12 @@ void initSaiApi() { SWSS_LOG_ENTER(); + if (ifstream(CONTEXT_CFG_FILE)) + { + SWSS_LOG_NOTICE("Context config file %s exists", CONTEXT_CFG_FILE); + gProfileMap[SAI_REDIS_KEY_CONTEXT_CONFIG] = CONTEXT_CFG_FILE; + } + sai_api_initialize(0, (const sai_service_method_table_t *)&test_services); sai_api_query(SAI_API_SWITCH, (void **)&sai_switch_api); @@ -225,8 +259,85 @@ void initSaiRedis(const string &record_location) if (status != SAI_STATUS_SUCCESS) { - SWSS_LOG_ERROR("Failed to notify syncd INIT_VIEW, rv:%d", status); + SWSS_LOG_ERROR("Failed to notify syncd INIT_VIEW, rv:%d gSwitchId %lx", status, gSwitchId); exit(EXIT_FAILURE); } SWSS_LOG_NOTICE("Notify syncd INIT_VIEW"); } + +sai_status_t initSaiPhyApi(swss::gearbox_phy_t *phy) +{ + sai_object_id_t phyOid; + sai_attribute_t attr; + vector attrs; + sai_status_t status; + + SWSS_LOG_ENTER(); + + attr.id = SAI_SWITCH_ATTR_INIT_SWITCH; + attr.value.booldata = true; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_TYPE; + attr.value.u32 = SAI_SWITCH_TYPE_PHY; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_SWITCH_PROFILE_ID; + attr.value.u32 = 0; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_SWITCH_HARDWARE_INFO; + attr.value.s8list.count = 0; + attr.value.s8list.list = 0; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_FIRMWARE_LOAD_METHOD; + attr.value.u32 = SAI_SWITCH_FIRMWARE_LOAD_METHOD_NONE; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_REGISTER_READ; + attr.value.ptr = (void *) mdio_read; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_REGISTER_WRITE; + attr.value.ptr = (void *) mdio_write; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_HARDWARE_ACCESS_BUS; + attr.value.u32 = hardware_access_map[phy->access]; + attrs.push_back(attr); + + attr.id = SAI_SWITCH_ATTR_PLATFROM_CONTEXT; + attr.value.u64 = phy->address; + attrs.push_back(attr); + + /* Must be last Attribute */ + attr.id = SAI_REDIS_SWITCH_ATTR_CONTEXT; + attr.value.u64 = phy->phy_id; + attrs.push_back(attr); + + status = sai_switch_api->create_switch(&phyOid, (uint32_t)attrs.size(), attrs.data()); + + if (status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("BOX: Failed to create PHY:%d rtn:%d", phy->phy_id, status); + return status; + } + SWSS_LOG_NOTICE("BOX: Created PHY:%d Oid:0x%lx", phy->phy_id, phyOid); + + phy->phy_oid = sai_serialize_object_id(phyOid); + + attr.id = SAI_SWITCH_ATTR_FIRMWARE_MAJOR_VERSION; + status = sai_switch_api->get_switch_attribute(phyOid, 1, &attr); + if (status != SAI_STATUS_SUCCESS) + { + SWSS_LOG_ERROR("BOX: Failed to get firmware major version:%d rtn:%d", phy->phy_id, status); + return status; + } + else + { + phy->firmware_major_version = string(attr.value.chardata); + } + + return status; +} diff --git a/orchagent/saihelper.h b/orchagent/saihelper.h index 8e733ef558..34bef06a67 100644 --- a/orchagent/saihelper.h +++ b/orchagent/saihelper.h @@ -1,6 +1,9 @@ #pragma once +#include "gearboxutils.h" + #include void initSaiApi(); -void initSaiRedis(const std::string &record_location); \ No newline at end of file +void initSaiRedis(const std::string &record_location); +sai_status_t initSaiPhyApi(swss::gearbox_phy_t *phy); diff --git a/portsyncd/Makefile.am b/portsyncd/Makefile.am index 430eadf882..96865e2887 100644 --- a/portsyncd/Makefile.am +++ b/portsyncd/Makefile.am @@ -1,4 +1,4 @@ -INCLUDES = -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I $(top_srcdir)/cfgmgr +INCLUDES = -I $(top_srcdir)/lib -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I $(top_srcdir)/cfgmgr bin_PROGRAMS = portsyncd @@ -8,7 +8,7 @@ else DBGFLAGS = -g endif -portsyncd_SOURCES = portsyncd.cpp linksync.cpp $(top_srcdir)/cfgmgr/shellcmd.h +portsyncd_SOURCES = $(top_srcdir)/lib/gearboxutils.cpp portsyncd.cpp linksync.cpp $(top_srcdir)/cfgmgr/shellcmd.h portsyncd_CFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) portsyncd_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) diff --git a/tests/mock_tests/Makefile.am b/tests/mock_tests/Makefile.am index a7f89475a6..d8a8036fb1 100644 --- a/tests/mock_tests/Makefile.am +++ b/tests/mock_tests/Makefile.am @@ -1,7 +1,7 @@ FLEX_CTR_DIR = $(top_srcdir)/orchagent/flex_counter DEBUG_CTR_DIR = $(top_srcdir)/orchagent/debug_counter -INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) +INCLUDES = -I $(FLEX_CTR_DIR) -I $(DEBUG_CTR_DIR) -I $(top_srcdir)/lib CFLAGS_SAI = -I /usr/include/sai @@ -31,6 +31,7 @@ tests_SOURCES = aclorch_ut.cpp \ mock_table.cpp \ mock_hiredis.cpp \ mock_redisreply.cpp \ + $(top_srcdir)/lib/gearboxutils.cpp \ $(top_srcdir)/orchagent/orchdaemon.cpp \ $(top_srcdir)/orchagent/orch.cpp \ $(top_srcdir)/orchagent/notifications.cpp \ From 44b34de942efd61da6bc1090531247ee6049a550 Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Tue, 16 Jun 2020 13:48:32 -0700 Subject: [PATCH 2/9] bring in schema.h for table name definitions --- gearsyncd/gearsyncd.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/gearsyncd/gearsyncd.cpp b/gearsyncd/gearsyncd.cpp index f4082bb0be..0708c78cc6 100644 --- a/gearsyncd/gearsyncd.cpp +++ b/gearsyncd/gearsyncd.cpp @@ -23,6 +23,7 @@ #include "warm_restart.h" #include "gearboxparser.h" #include "gearboxutils.h" +#include "schema.h" #include From 11b1e7672ec9fcd22ae392665272c19bb7fb7141 Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Thu, 18 Jun 2020 14:11:46 -0700 Subject: [PATCH 3/9] fixes to confirm to coding standard - braces, indentation --- gearsyncd/gearboxparser.cpp | 455 ++++++++-------- gearsyncd/gearboxparser.h | 4 +- gearsyncd/gearparserbase.cpp | 54 +- gearsyncd/gearparserbase.h | 38 +- gearsyncd/gearsyncd.cpp | 24 +- gearsyncd/phyparser.cpp | 803 +++++++++++++++-------------- gearsyncd/phyparser.h | 8 +- gearsyncd/tests/testgearparser.cpp | 190 +++---- gearsyncd/tests/testphyparser.cpp | 259 +++++----- lib/gearboxutils.cpp | 3 +- orchagent/main.cpp | 12 +- orchagent/saihelper.cpp | 4 +- 12 files changed, 960 insertions(+), 894 deletions(-) diff --git a/gearsyncd/gearboxparser.cpp b/gearsyncd/gearboxparser.cpp index 80809fb27d..85acb680be 100644 --- a/gearsyncd/gearboxparser.cpp +++ b/gearsyncd/gearboxparser.cpp @@ -18,8 +18,7 @@ #include "phyparser.h" #include -void -GearboxParser::notifyGearboxConfigDone(bool success) +void GearboxParser::notifyGearboxConfigDone(bool success) { swss::ProducerStateTable *p = getProducerStateTable().get(); @@ -29,220 +28,258 @@ GearboxParser::notifyGearboxConfigDone(bool success) p->set("GearboxConfigDone", attrs); } -bool -GearboxParser::parse() +bool GearboxParser::parse() { - json root; + json root; - try { - root = getJSONRoot(); - } catch (const std::exception& e) { - SWSS_LOG_ERROR("JSON root not parseable"); - return false; - } + try + { + root = getJSONRoot(); + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("JSON root not parseable"); + return false; + } - json phys, phy, interfaces, interface, val, lanes; + json phys, phy, interfaces, interface, val, lanes; - std::vector attrs; + std::vector attrs; - try { - phys = root["phys"]; - if (phys.size() == 0) { - SWSS_LOG_ERROR("zero-sized 'phys' field in gearbox configuration"); - return false; + try + { + phys = root["phys"]; + if (phys.size() == 0) + { + SWSS_LOG_ERROR("zero-sized 'phys' field in gearbox configuration"); + return false; + } + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("unable to read gearbox configuration (invalid format)"); + return false; } - } catch (const std::exception& e) { - SWSS_LOG_ERROR("unable to read gearbox configuration (invalid format)"); - return false; - } - - if (true) { - for (uint32_t iter = 0; iter < phys.size(); iter++) { - phy = phys[iter]; - try { - attrs.clear(); - swss::FieldValueTuple attr; - if (phy.find("phy_id") == phy.end()) { - SWSS_LOG_ERROR("missing 'phy_id' field in 'phys' item %d in gearbox configuration", iter); - return false; + + for (uint32_t iter = 0; iter < phys.size(); iter++) + { + phy = phys[iter]; + try { + attrs.clear(); + swss::FieldValueTuple attr; + if (phy.find("phy_id") == phy.end()) + { + SWSS_LOG_ERROR("missing 'phy_id' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["phy_id"]; + int phyId = val.get(); + attr = std::make_pair("phy_id", std::to_string(phyId)); + attrs.push_back(attr); + if (phy.find("name") == phy.end()) + { + SWSS_LOG_ERROR("missing 'name' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["name"]; + std::string name(val.get()); + attr = std::make_pair("name", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("address") == phy.end()) { + SWSS_LOG_ERROR("missing 'address' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["address"]; + attr = std::make_pair("address", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("lib_name") == phy.end()) + { + SWSS_LOG_ERROR("missing 'lib_name' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["lib_name"]; + attr = std::make_pair("lib_name", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("firmware_path") == phy.end()) + { + SWSS_LOG_ERROR("missing 'firmware_path' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["firmware_path"]; + attr = std::make_pair("firmware_path", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("config_file") == phy.end()) + { + SWSS_LOG_ERROR("missing 'config_file' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["config_file"]; + std::string cfgFile(val.get()); + attr = std::make_pair("config_file", cfgFile); + attrs.push_back(attr); + if (phy.find("sai_init_config_file") == phy.end()) + { + SWSS_LOG_ERROR("missing 'sai_init_config_file' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["sai_init_config_file"]; + std::string bcmCfgFile(std::string(val.get())); + attr = std::make_pair("sai_init_config_file", bcmCfgFile); + attrs.push_back(attr); + if (phy.find("phy_access") == phy.end()) + { + SWSS_LOG_ERROR("missing 'phy_access' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["phy_access"]; + attr = std::make_pair("phy_access", std::string(val.get())); + attrs.push_back(attr); + if (phy.find("bus_id") == phy.end()) + { + SWSS_LOG_ERROR("missing 'bus_id' field in 'phys' item %d in gearbox configuration", iter); + return false; + } + val = phy["bus_id"]; + attr = std::make_pair("bus_id", std::to_string(val.get())); + attrs.push_back(attr); + std::string key; + key = "phy:" + std::to_string(phyId); + if (getWriteToDb() == true) + { + writeToDb(key, attrs); + } + PhyParser p; + p.setPhyId(phyId); + p.setWriteToDb(getWriteToDb()); + p.setConfigPath(cfgFile); + if (p.parse() == false) + { + SWSS_LOG_ERROR("phy parser failed to parse item %d in gearbox configuration", iter); + return false; + } + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("unable to read 'phys' item %d in gearbox configuration (invalid format)", iter); + return false; } - val = phy["phy_id"]; - int phyId = val.get(); - attr = std::make_pair("phy_id", std::to_string(phyId)); - attrs.push_back(attr); - if (phy.find("name") == phy.end()) { - SWSS_LOG_ERROR("missing 'name' field in 'phys' item %d in gearbox configuration", iter); - return false; + } + + if (root.find("interfaces") != root.end()) + { + interfaces = root["interfaces"]; // vec + if (interfaces.size() == 0) + { + SWSS_LOG_ERROR("zero-sized 'interfaces' field in gearbox configuration"); + return false; } - val = phy["name"]; - std::string name(val.get()); - attr = std::make_pair("name", std::string(val.get())); - attrs.push_back(attr); - if (phy.find("address") == phy.end()) { - SWSS_LOG_ERROR("missing 'address' field in 'phys' item %d in gearbox configuration", iter); - return false; - } - val = phy["address"]; - attr = std::make_pair("address", std::string(val.get())); - attrs.push_back(attr); - if (phy.find("lib_name") == phy.end()) { - SWSS_LOG_ERROR("missing 'lib_name' field in 'phys' item %d in gearbox configuration", iter); - return false; - } - val = phy["lib_name"]; - attr = std::make_pair("lib_name", std::string(val.get())); - attrs.push_back(attr); - if (phy.find("firmware_path") == phy.end()) { - SWSS_LOG_ERROR("missing 'firmware_path' field in 'phys' item %d in gearbox configuration", iter); - return false; - } - val = phy["firmware_path"]; - attr = std::make_pair("firmware_path", std::string(val.get())); - attrs.push_back(attr); - if (phy.find("config_file") == phy.end()) { - SWSS_LOG_ERROR("missing 'config_file' field in 'phys' item %d in gearbox configuration", iter); - return false; - } - val = phy["config_file"]; - std::string cfgFile(val.get()); - attr = std::make_pair("config_file", cfgFile); - attrs.push_back(attr); - if (phy.find("sai_init_config_file") == phy.end()) { - SWSS_LOG_ERROR("missing 'sai_init_config_file' field in 'phys' item %d in gearbox configuration", iter); - return false; - } - val = phy["sai_init_config_file"]; - std::string bcmCfgFile(std::string(val.get())); - attr = std::make_pair("sai_init_config_file", bcmCfgFile); - attrs.push_back(attr); - if (phy.find("phy_access") == phy.end()) { - SWSS_LOG_ERROR("missing 'phy_access' field in 'phys' item %d in gearbox configuration", iter); - return false; - } - val = phy["phy_access"]; - attr = std::make_pair("phy_access", std::string(val.get())); - attrs.push_back(attr); - if (phy.find("bus_id") == phy.end()) { - SWSS_LOG_ERROR("missing 'bus_id' field in 'phys' item %d in gearbox configuration", iter); - return false; - } - val = phy["bus_id"]; - attr = std::make_pair("bus_id", std::to_string(val.get())); - attrs.push_back(attr); - std::string key; - key = "phy:" + std::to_string(phyId); - if (getWriteToDb() == true) { - writeToDb(key, attrs); - } - PhyParser p; - p.setPhyId(phyId); - p.setWriteToDb(getWriteToDb()); - p.setConfigPath(cfgFile); - if (p.parse() == false) { - SWSS_LOG_ERROR("phy parser failed to parse item %d in gearbox configuration", iter); - return false; - } - } catch (const std::exception& e) { - SWSS_LOG_ERROR("unable to read 'phys' item %d in gearbox configuration (invalid format)", iter); - return false; - } - } - } else { - SWSS_LOG_ERROR("missing 'phys' field in gearbox configuration"); - return false; - } - - if (root.find("interfaces") != root.end()) { - interfaces = root["interfaces"]; // vec - if (interfaces.size() == 0) { - SWSS_LOG_ERROR("zero-sized 'interfaces' field in gearbox configuration"); - return false; - } - for (uint32_t iter = 0; iter < interfaces.size(); iter++) { - attrs.clear(); - interface = interfaces[iter]; - try { - swss::FieldValueTuple attr; - - if (interface.find("name") == interface.end()) { - SWSS_LOG_ERROR("missing 'name' field in 'interfaces' item %d in gearbox configuration", iter); - return false; - } - val = interface["name"]; - attr = std::make_pair("name", std::string(val.get())); - attrs.push_back(attr); - - if (interface.find("index") == interface.end()) { - SWSS_LOG_ERROR("missing 'index' field in 'interfaces' item %d in gearbox configuration", iter); - return false; - } - val = interface["index"]; - int index = val.get(); - attr = std::make_pair("index", std::to_string(index)); - attrs.push_back(attr); - - if (interface.find("phy_id") == interface.end()) { - SWSS_LOG_ERROR("missing 'phy_id' field in 'interfaces' item %d in gearbox configuration", iter); - return false; - } - val = interface["phy_id"]; - attr = std::make_pair("phy_id", std::to_string(val.get())); - attrs.push_back(attr); - - if (interface.find("system_lanes") != interface.end()) { - lanes = interface["system_lanes"]; // vec - std::string laneStr(""); - if (lanes.size() == 0) { - SWSS_LOG_ERROR("zero-sized 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); - return false; - } - for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) { - val = lanes[iter2]; - if (laneStr.length() > 0) { - laneStr += ","; - } - laneStr += std::to_string(val.get()); - } - attr = std::make_pair("system_lanes", laneStr); - attrs.push_back(attr); - } else { - SWSS_LOG_ERROR("missing 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); - return false; - } - - if (interface.find("line_lanes") != interface.end()) { - lanes = interface["line_lanes"]; // vec - std::string laneStr(""); - if (lanes.size() == 0) { - SWSS_LOG_ERROR("zero-sized 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); - return false; - } - for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) { - val = lanes[iter2]; - if (laneStr.length() > 0) { - laneStr += ","; - } - laneStr += std::to_string(val.get()); - } - attr = std::make_pair("line_lanes", laneStr); - attrs.push_back(attr); - } else { - SWSS_LOG_ERROR("missing 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); - return false; - } - std::string key; - key = "interface:" + std::to_string(index); - if (getWriteToDb() == true) { - writeToDb(key, attrs); - } - } catch (const std::exception& e) { - SWSS_LOG_ERROR("unable to read 'interfaces' item %d in gearbox configuration (invalid format)", iter); - return false; - } + for (uint32_t iter = 0; iter < interfaces.size(); iter++) + { + attrs.clear(); + interface = interfaces[iter]; + try + { + swss::FieldValueTuple attr; + + if (interface.find("name") == interface.end()) + { + SWSS_LOG_ERROR("missing 'name' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + val = interface["name"]; + attr = std::make_pair("name", std::string(val.get())); + attrs.push_back(attr); + + if (interface.find("index") == interface.end()) + { + SWSS_LOG_ERROR("missing 'index' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + val = interface["index"]; + int index = val.get(); + attr = std::make_pair("index", std::to_string(index)); + attrs.push_back(attr); + + if (interface.find("phy_id") == interface.end()) + { + SWSS_LOG_ERROR("missing 'phy_id' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + val = interface["phy_id"]; + attr = std::make_pair("phy_id", std::to_string(val.get())); + attrs.push_back(attr); + + if (interface.find("system_lanes") != interface.end()) + { + lanes = interface["system_lanes"]; // vec + std::string laneStr(""); + if (lanes.size() == 0) + { + SWSS_LOG_ERROR("zero-sized 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) + { + val = lanes[iter2]; + if (laneStr.length() > 0) + { + laneStr += ","; + } + laneStr += std::to_string(val.get()); + } + attr = std::make_pair("system_lanes", laneStr); + attrs.push_back(attr); + } + else + { + SWSS_LOG_ERROR("missing 'system_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + + if (interface.find("line_lanes") != interface.end()) + { + lanes = interface["line_lanes"]; // vec + std::string laneStr(""); + if (lanes.size() == 0) + { + SWSS_LOG_ERROR("zero-sized 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + for (uint32_t iter2 = 0; iter2 < lanes.size(); iter2++) + { + val = lanes[iter2]; + if (laneStr.length() > 0) + { + laneStr += ","; + } + laneStr += std::to_string(val.get()); + } + attr = std::make_pair("line_lanes", laneStr); + attrs.push_back(attr); + } + else + { + SWSS_LOG_ERROR("missing 'line_lanes' field in 'interfaces' item %d in gearbox configuration", iter); + return false; + } + std::string key; + key = "interface:" + std::to_string(index); + if (getWriteToDb() == true) + { + writeToDb(key, attrs); + } + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("unable to read 'interfaces' item %d in gearbox configuration (invalid format)", iter); + return false; + } + } + } + else + { + SWSS_LOG_ERROR("unable to read 'interfaces' item in gearbox configuration"); + return false; } - } else { - SWSS_LOG_ERROR("unable to read 'interfaces' item in gearbox configuration"); - return false; - } - return true; + return true; } diff --git a/gearsyncd/gearboxparser.h b/gearsyncd/gearboxparser.h index 9620d7bfeb..caea1dafe6 100644 --- a/gearsyncd/gearboxparser.h +++ b/gearsyncd/gearboxparser.h @@ -22,8 +22,8 @@ class GearboxParser: public GearParserBase { public: - bool parse(); - void notifyGearboxConfigDone(bool success); + bool parse(); + void notifyGearboxConfigDone(bool success); }; #endif /* __GEARBOX_PARSER_H__ */ diff --git a/gearsyncd/gearparserbase.cpp b/gearsyncd/gearparserbase.cpp index 3f1c949b2f..72aa786e98 100644 --- a/gearsyncd/gearparserbase.cpp +++ b/gearsyncd/gearparserbase.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019 Broadcom Inc. + * Copyright 2020 Broadcom Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,48 +20,50 @@ #include void -GearParserBase::init() { +GearParserBase::init() +{ m_writeToDb = false; m_rootInit = false; m_applDb = std::unique_ptr{new swss::DBConnector(APPL_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, 0)}; m_producerStateTable = std::unique_ptr{new swss::ProducerStateTable(m_applDb.get(), APP_GEARBOX_TABLE_NAME)}; } -GearParserBase::GearParserBase() { - init(); +GearParserBase::GearParserBase() +{ + init(); } -GearParserBase::~GearParserBase() { +GearParserBase::~GearParserBase() +{ } -json & -GearParserBase::getJSONRoot() +json & GearParserBase::getJSONRoot() { - // lazy instantiate - - if (m_rootInit == false) { + // lazy instantiate - std::ifstream infile(getConfigPath()); - if (infile.is_open()) + if (m_rootInit == false) { - std::string jsonBuffer; - std::string line; + std::ifstream infile(getConfigPath()); + if (infile.is_open()) + { + std::string jsonBuffer; + std::string line; - while (getline(infile, line)) { - jsonBuffer += line; - } - infile.close(); + while (getline(infile, line)) + { + jsonBuffer += line; + } + infile.close(); - m_root = json::parse(jsonBuffer.c_str()); - m_rootInit = true; + m_root = json::parse(jsonBuffer.c_str()); + m_rootInit = true; + } } - } - return m_root; + return m_root; } -bool -GearParserBase::writeToDb(std::string &key, std::vector &attrs) +bool GearParserBase::writeToDb(std::string &key, std::vector &attrs) { - m_producerStateTable.get()->set(key.c_str(), attrs); - return true; + m_producerStateTable.get()->set(key.c_str(), attrs); + return true; } diff --git a/gearsyncd/gearparserbase.h b/gearsyncd/gearparserbase.h index 4454c0ba83..5b3b176ba1 100644 --- a/gearsyncd/gearparserbase.h +++ b/gearsyncd/gearparserbase.h @@ -34,29 +34,29 @@ using json = nlohmann::json; class GearParserBase { public: - GearParserBase(); - virtual ~GearParserBase(); - virtual bool parse() = 0; - void setWriteToDb(bool val) {m_writeToDb = val;} - bool getWriteToDb() {return m_writeToDb;} - void setConfigPath(std::string &path) {m_cfgPath = path;} - const std::string getConfigPath() {return m_cfgPath;} - std::unique_ptr &getProducerStateTable() {return m_producerStateTable;} + GearParserBase(); + virtual ~GearParserBase(); + virtual bool parse() = 0; + void setWriteToDb(bool val) {m_writeToDb = val;} + bool getWriteToDb() {return m_writeToDb;} + void setConfigPath(std::string &path) {m_cfgPath = path;} + const std::string getConfigPath() {return m_cfgPath;} + std::unique_ptr &getProducerStateTable() {return m_producerStateTable;} protected: - bool writeToDb(std::string &key, std::vector &attrs); - json &getJSONRoot(); + bool writeToDb(std::string &key, std::vector &attrs); + json &getJSONRoot(); private: - void init(); - std::unique_ptr m_cfgDb; - std::unique_ptr m_applDb; - std::unique_ptr m_stateDb; - std::unique_ptr m_producerStateTable; - std::string m_cfgPath; - bool m_writeToDb; - json m_root; - bool m_rootInit; + void init(); + std::unique_ptr m_cfgDb; + std::unique_ptr m_applDb; + std::unique_ptr m_stateDb; + std::unique_ptr m_producerStateTable; + std::string m_cfgPath; + bool m_writeToDb; + json m_root; + bool m_rootInit; }; #endif // __GEAR_PARSER_BASE_H__ diff --git a/gearsyncd/gearsyncd.cpp b/gearsyncd/gearsyncd.cpp index 0708c78cc6..3bf90fad01 100644 --- a/gearsyncd/gearsyncd.cpp +++ b/gearsyncd/gearsyncd.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019 Broadcom Inc. + * Copyright 2020 Broadcom Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,9 +32,9 @@ using namespace swss; void usage() { - cout << "Usage: gearsyncd [-p gear_config.json]" << endl; - cout << " -p gearbox_config.json: import gearbox config" << endl; - cout << " use configDB data if not specified" << endl; + cout << "Usage: gearsyncd [-p gear_config.json]" << endl; + cout << " -p gearbox_config.json: import gearbox config" << endl; + cout << " use configDB data if not specified" << endl; } bool handleGearboxConfigFile(string file, bool warm); @@ -154,12 +154,12 @@ bool handleGearboxConfigFromConfigDB(ProducerStateTable &p, DBConnector &cfgDb, bool handleGearboxConfigFile(string file, bool warm) { - GearboxParser parser; - bool ret; - - parser.setWriteToDb(true); - parser.setConfigPath(file); - ret = parser.parse(); - parser.notifyGearboxConfigDone(ret); // if (!warm....) - return ret; + GearboxParser parser; + bool ret; + + parser.setWriteToDb(true); + parser.setConfigPath(file); + ret = parser.parse(); + parser.notifyGearboxConfigDone(ret); // if (!warm....) + return ret; } diff --git a/gearsyncd/phyparser.cpp b/gearsyncd/phyparser.cpp index a6db79d4c2..eb93bd6584 100644 --- a/gearsyncd/phyparser.cpp +++ b/gearsyncd/phyparser.cpp @@ -16,383 +16,438 @@ #include "phyparser.h" -bool -PhyParser::parse() +bool PhyParser::parse() { - json root; - - try { - root = getJSONRoot(); - } catch (const std::exception& e) { - SWSS_LOG_ERROR("JSON root not parseable"); - return false; - } - - if (false) { - return false; - } - - std::vector attrs; - swss::FieldValueTuple attr; - json val, vals, ports, port, lanes, lane; - std::string valsStr; - - try { - lanes = root["lanes"]; - if (lanes.size() == 0) { - SWSS_LOG_ERROR("zero-sized 'lanes' field in phy configuration"); + json root; + + try + { + root = getJSONRoot(); + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("JSON root not parseable"); return false; } - } catch (const std::exception& e) { - SWSS_LOG_ERROR("unable to read phy configuration (invalid format)"); - return false; - } - for (uint32_t iter = 0; iter < lanes.size(); iter++) { - lane = lanes[iter]; - try { - attrs.clear(); - - /* index */ - if (lane.find("index") == lane.end()) { - SWSS_LOG_ERROR("missing 'index' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["index"]; - int id = val.get(); - attr = std::make_pair("index", std::to_string(id)); - attrs.push_back(attr); - - /* system_side */ - if (lane.find("system_side") == lane.end()) { - SWSS_LOG_ERROR("missing 'system_side' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["system_side"]; - if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'system_side' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - std::string systemSideStr = val.get() == true ? "true" : "false"; - attr = std::make_pair("system_side", systemSideStr); - attrs.push_back(attr); - - /* local_lane_id */ - if (lane.find("local_lane_id") == lane.end()) { - SWSS_LOG_ERROR("missing 'local_lane_id' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["local_lane_id"]; - attr = std::make_pair("local_lane_id", std::to_string(val.get())); - attrs.push_back(attr); - - /* tx_polarity */ - if (lane.find("tx_polarity") == lane.end()) { - SWSS_LOG_ERROR("missing 'tx_polarity' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["tx_polarity"]; - attr = std::make_pair("tx_polarity", std::to_string(val.get())); - attrs.push_back(attr); - - /* rx_polarity */ - if (lane.find("rx_polarity") == lane.end()) { - SWSS_LOG_ERROR("missing 'rx_polarity' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["rx_polarity"]; - attr = std::make_pair("rx_polarity", std::to_string(val.get())); - attrs.push_back(attr); - - /* line_tx_lanemap */ - if (lane.find("line_tx_lanemap") == lane.end()) { - SWSS_LOG_ERROR("missing 'line_tx_lanemap' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["line_tx_lanemap"]; - attr = std::make_pair("line_tx_lanemap", std::to_string(val.get())); - attrs.push_back(attr); - - /* line_rx_lanemap */ - if (lane.find("line_rx_lanemap") == lane.end()) { - SWSS_LOG_ERROR("missing 'line_rx_lanemap' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["line_rx_lanemap"]; - attr = std::make_pair("line_rx_lanemap", std::to_string(val.get())); - attrs.push_back(attr); - - /* line_to_system_lanemap */ - if (lane.find("line_to_system_lanemap") == lane.end()) { - SWSS_LOG_ERROR("missing 'line_to_system_lanemap' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["line_to_system_lanemap"]; - attr = std::make_pair("line_to_system_lanemap", std::to_string(val.get())); - attrs.push_back(attr); - - /* mdio_addr */ - if (lane.find("mdio_addr") == lane.end()) { - SWSS_LOG_ERROR("missing 'mdio_addr' field of item %d of 'lanes' field in phy configuration", iter); - return false; - } - val = lane["mdio_addr"]; - attr = std::make_pair("mdio_addr", val.get()); - attrs.push_back(attr); - - std::string key; - key = "phy:" + std::to_string(getPhyId()) + ":lanes:" + std::to_string(id); - if (getWriteToDb() == true) { - writeToDb(key, attrs); - } - } catch (const std::exception& e) { - SWSS_LOG_ERROR("unable to read lanes configuration item %d (invalid format)", iter); - return false; - } - } - if (root.find("ports") != root.end()) { - ports = root["ports"]; - if (ports.size() == 0) { - SWSS_LOG_ERROR("zero-sized 'ports' field in phy configuration"); - return false; + + std::vector attrs; + swss::FieldValueTuple attr; + json val, vals, ports, port, lanes, lane; + std::string valsStr; + + try + { + lanes = root["lanes"]; + if (lanes.size() == 0) + { + SWSS_LOG_ERROR("zero-sized 'lanes' field in phy configuration"); + return false; + } + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("unable to read phy configuration (invalid format)"); + return false; + } + for (uint32_t iter = 0; iter < lanes.size(); iter++) + { + lane = lanes[iter]; + try + { + attrs.clear(); + + /* index */ + if (lane.find("index") == lane.end()) + { + SWSS_LOG_ERROR("missing 'index' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["index"]; + int id = val.get(); + attr = std::make_pair("index", std::to_string(id)); + attrs.push_back(attr); + + /* system_side */ + if (lane.find("system_side") == lane.end()) + { + SWSS_LOG_ERROR("missing 'system_side' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["system_side"]; + if (val.get() != false && val.get() != true) + { + SWSS_LOG_ERROR("not a boolean: 'system_side' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + std::string systemSideStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("system_side", systemSideStr); + attrs.push_back(attr); + + /* local_lane_id */ + if (lane.find("local_lane_id") == lane.end()) + { + SWSS_LOG_ERROR("missing 'local_lane_id' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["local_lane_id"]; + attr = std::make_pair("local_lane_id", std::to_string(val.get())); + attrs.push_back(attr); + + /* tx_polarity */ + if (lane.find("tx_polarity") == lane.end()) + { + SWSS_LOG_ERROR("missing 'tx_polarity' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["tx_polarity"]; + attr = std::make_pair("tx_polarity", std::to_string(val.get())); + attrs.push_back(attr); + + /* rx_polarity */ + if (lane.find("rx_polarity") == lane.end()) + { + SWSS_LOG_ERROR("missing 'rx_polarity' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["rx_polarity"]; + attr = std::make_pair("rx_polarity", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_tx_lanemap */ + if (lane.find("line_tx_lanemap") == lane.end()) + { + SWSS_LOG_ERROR("missing 'line_tx_lanemap' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["line_tx_lanemap"]; + attr = std::make_pair("line_tx_lanemap", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_rx_lanemap */ + if (lane.find("line_rx_lanemap") == lane.end()) + { + SWSS_LOG_ERROR("missing 'line_rx_lanemap' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["line_rx_lanemap"]; + attr = std::make_pair("line_rx_lanemap", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_to_system_lanemap */ + if (lane.find("line_to_system_lanemap") == lane.end()) + { + SWSS_LOG_ERROR("missing 'line_to_system_lanemap' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["line_to_system_lanemap"]; + attr = std::make_pair("line_to_system_lanemap", std::to_string(val.get())); + attrs.push_back(attr); + + /* mdio_addr */ + if (lane.find("mdio_addr") == lane.end()) + { + SWSS_LOG_ERROR("missing 'mdio_addr' field of item %d of 'lanes' field in phy configuration", iter); + return false; + } + val = lane["mdio_addr"]; + attr = std::make_pair("mdio_addr", val.get()); + attrs.push_back(attr); + + std::string key; + key = "phy:" + std::to_string(getPhyId()) + ":lanes:" + std::to_string(id); + if (getWriteToDb() == true) + { + writeToDb(key, attrs); + } + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("unable to read lanes configuration item %d (invalid format)", iter); + return false; + } } - for (uint32_t iter = 0; iter < ports.size(); iter++) { - port = ports[iter]; - try { - attrs.clear(); - swss::FieldValueTuple attr; - - /* index */ - if (port.find("index") == port.end()) { - SWSS_LOG_ERROR("missing 'index' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["index"]; - int id = val.get(); - attr = std::make_pair("index", std::to_string(id)); - attrs.push_back(attr); - - /* mdio_addr */ - if (port.find("mdio_addr") == port.end()) { - SWSS_LOG_ERROR("missing 'mdio_addr' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["mdio_addr"]; - attr = std::make_pair("mdio_addr", val.get()); - attrs.push_back(attr); - - /* system_speed */ - if (port.find("system_speed") == port.end()) { - SWSS_LOG_ERROR("missing 'system_speed' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["system_speed"]; - attr = std::make_pair("system_speed", std::to_string(val.get())); - attrs.push_back(attr); - - /* system_fec */ - if (port.find("system_fec") == port.end()) { - SWSS_LOG_ERROR("missing 'system_fec' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["system_fec"]; - attr = std::make_pair("system_fec", val.get()); - attrs.push_back(attr); - - /* system_auto_neg */ - if (port.find("system_auto_neg") == port.end()) { - SWSS_LOG_ERROR("missing 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["system_auto_neg"]; - if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - std::string systemAutoNegStr = val.get() == true ? "true" : "false"; - attr = std::make_pair("system_auto_neg", systemAutoNegStr); - attrs.push_back(attr); - - /* system_loopback */ - if (port.find("system_loopback") == port.end()) { - SWSS_LOG_ERROR("missing 'system_loopback' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["system_loopback"]; - attr = std::make_pair("system_loopback", val.get()); - attrs.push_back(attr); - - /* system_training */ - if (port.find("system_training") == port.end()) { - SWSS_LOG_ERROR("missing 'system_training' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["system_training"]; - if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'system_training' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - std::string systemTrainingStr = val.get() == true ? "true" : "false"; - attr = std::make_pair("system_training", systemTrainingStr); - attrs.push_back(attr); - - /* line_speed */ - if (port.find("line_speed") == port.end()) { - SWSS_LOG_ERROR("missing 'line_speed' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_speed"]; - attr = std::make_pair("line_speed", std::to_string(val.get())); - attrs.push_back(attr); - - /* line_fec */ - if (port.find("line_fec") == port.end()) { - SWSS_LOG_ERROR("missing 'line_fec' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_fec"]; - attr = std::make_pair("line_fec", val.get()); - attrs.push_back(attr); - - /* line_auto_neg */ - if (port.find("line_auto_neg") == port.end()) { - SWSS_LOG_ERROR("missing 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_auto_neg"]; - if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - std::string lineAutoNegStr = val.get() == true ? "true" : "false"; - attr = std::make_pair("line_auto_neg", lineAutoNegStr); - attrs.push_back(attr); - - /* line_media_type */ - if (port.find("line_media_type") == port.end()) { - SWSS_LOG_ERROR("missing 'line_media_type' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_media_type"]; - attr = std::make_pair("line_media_type", val.get()); - attrs.push_back(attr); - - /* line_intf_type */ - if (port.find("line_intf_type") == port.end()) { - SWSS_LOG_ERROR("missing 'line_intf_type' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_intf_type"]; - attr = std::make_pair("line_intf_type", val.get()); - attrs.push_back(attr); - - /* line_loopback */ - if (port.find("line_loopback") == port.end()) { - SWSS_LOG_ERROR("missing 'line_loopback' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_loopback"]; - attr = std::make_pair("line_loopback", val.get()); - attrs.push_back(attr); - - /* line_training */ - if (port.find("line_training") == port.end()) { - SWSS_LOG_ERROR("missing 'line_training' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_training"]; - if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'line_training' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - std::string lineTrainingStr = val.get() == true ? "true" : "false"; - attr = std::make_pair("line_training", lineTrainingStr); - attrs.push_back(attr); - - /* line_adver_speed */ - if (port.find("line_adver_speed") == port.end()) { - SWSS_LOG_ERROR("missing 'line_adver_speed' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - vals = port["line_adver_speed"]; // vec - valsStr = ""; - for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) { - val = vals[iter1]; - if (valsStr.length() > 0) { - valsStr += ","; - } - valsStr += std::to_string(val.get()); - } - attr = std::make_pair("line_adver_speed", valsStr); - attrs.push_back(attr); - - /* line_adver_fec */ - if (port.find("line_adver_fec") == port.end()) { - SWSS_LOG_ERROR("missing 'line_adver_fec' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - vals = port["line_adver_fec"]; // vec - valsStr = ""; - for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) { - val = vals[iter1]; - if (valsStr.length() > 0) { - valsStr += ","; - } - valsStr += std::to_string(val.get()); - } - attr = std::make_pair("line_adver_fec", valsStr); - attrs.push_back(attr); - - /* line_adver_auto_neg */ - if (port.find("line_adver_auto_neg") == port.end()) { - SWSS_LOG_ERROR("missing 'line_adver_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_adver_auto_neg"]; - if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'line_adver_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - std::string lineAdverAutoNegStr = val.get() == true ? "true" : "false"; - attr = std::make_pair("line_adver_auto_neg", lineAdverAutoNegStr); - attrs.push_back(attr); - - /* line_adver_asym_pause */ - if (port.find("line_adver_asym_pause") == port.end()) { - SWSS_LOG_ERROR("missing 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_adver_asym_pause"]; - if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - std::string lineAdverAsymPauseStr = val.get() == true ? "true" : "false"; - attr = std::make_pair("line_adver_asym_pause", lineAdverAsymPauseStr); - attrs.push_back(attr); - - /* line_adver_media_type */ - if (port.find("line_adver_media_type") == port.end()) { - SWSS_LOG_ERROR("missing 'line_adver_media_type' field of item %d of 'ports' field in phy configuration", iter); - return false; - } - val = port["line_adver_media_type"]; - attr = std::make_pair("line_adver_media_type", val.get()); - attrs.push_back(attr); - - std::string key; - int phyId = getPhyId(); - key = "phy:" + std::to_string(phyId) + ":ports:" + std::to_string(id); - if (getWriteToDb() == true) { - writeToDb(key, attrs); - } - } catch (const std::exception& e) { - SWSS_LOG_ERROR("unable to read ports configuration item %d (invalid format): %s", iter, e.what()); - return false; - } + if (root.find("ports") != root.end()) + { + ports = root["ports"]; + if (ports.size() == 0) + { + SWSS_LOG_ERROR("zero-sized 'ports' field in phy configuration"); + return false; + } + for (uint32_t iter = 0; iter < ports.size(); iter++) + { + port = ports[iter]; + try + { + attrs.clear(); + swss::FieldValueTuple attr; + + /* index */ + if (port.find("index") == port.end()) + { + SWSS_LOG_ERROR("missing 'index' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["index"]; + int id = val.get(); + attr = std::make_pair("index", std::to_string(id)); + attrs.push_back(attr); + + /* mdio_addr */ + if (port.find("mdio_addr") == port.end()) + { + SWSS_LOG_ERROR("missing 'mdio_addr' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["mdio_addr"]; + attr = std::make_pair("mdio_addr", val.get()); + attrs.push_back(attr); + + /* system_speed */ + if (port.find("system_speed") == port.end()) + { + SWSS_LOG_ERROR("missing 'system_speed' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_speed"]; + attr = std::make_pair("system_speed", std::to_string(val.get())); + attrs.push_back(attr); + + /* system_fec */ + if (port.find("system_fec") == port.end()) + { + SWSS_LOG_ERROR("missing 'system_fec' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_fec"]; + attr = std::make_pair("system_fec", val.get()); + attrs.push_back(attr); + + /* system_auto_neg */ + if (port.find("system_auto_neg") == port.end()) + { + SWSS_LOG_ERROR("missing 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_auto_neg"]; + if (val.get() != false && val.get() != true) + { + SWSS_LOG_ERROR("not a boolean: 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string systemAutoNegStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("system_auto_neg", systemAutoNegStr); + attrs.push_back(attr); + + /* system_loopback */ + if (port.find("system_loopback") == port.end()) + { + SWSS_LOG_ERROR("missing 'system_loopback' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_loopback"]; + attr = std::make_pair("system_loopback", val.get()); + attrs.push_back(attr); + + /* system_training */ + if (port.find("system_training") == port.end()) + { + SWSS_LOG_ERROR("missing 'system_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["system_training"]; + if (val.get() != false && val.get() != true) + { + SWSS_LOG_ERROR("not a boolean: 'system_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string systemTrainingStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("system_training", systemTrainingStr); + attrs.push_back(attr); + + /* line_speed */ + if (port.find("line_speed") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_speed' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_speed"]; + attr = std::make_pair("line_speed", std::to_string(val.get())); + attrs.push_back(attr); + + /* line_fec */ + if (port.find("line_fec") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_fec' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_fec"]; + attr = std::make_pair("line_fec", val.get()); + attrs.push_back(attr); + + /* line_auto_neg */ + if (port.find("line_auto_neg") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_auto_neg"]; + if (val.get() != false && val.get() != true) + { + SWSS_LOG_ERROR("not a boolean: 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineAutoNegStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_auto_neg", lineAutoNegStr); + attrs.push_back(attr); + + /* line_media_type */ + if (port.find("line_media_type") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_media_type' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_media_type"]; + attr = std::make_pair("line_media_type", val.get()); + attrs.push_back(attr); + + /* line_intf_type */ + if (port.find("line_intf_type") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_intf_type' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_intf_type"]; + attr = std::make_pair("line_intf_type", val.get()); + attrs.push_back(attr); + + /* line_loopback */ + if (port.find("line_loopback") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_loopback' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_loopback"]; + attr = std::make_pair("line_loopback", val.get()); + attrs.push_back(attr); + + /* line_training */ + if (port.find("line_training") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_training"]; + if (val.get() != false && val.get() != true) + { + SWSS_LOG_ERROR("not a boolean: 'line_training' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineTrainingStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_training", lineTrainingStr); + attrs.push_back(attr); + + /* line_adver_speed */ + if (port.find("line_adver_speed") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_adver_speed' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + vals = port["line_adver_speed"]; // vec + valsStr = ""; + for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) + { + val = vals[iter1]; + if (valsStr.length() > 0) + { + valsStr += ","; + } + valsStr += std::to_string(val.get()); + } + attr = std::make_pair("line_adver_speed", valsStr); + attrs.push_back(attr); + + /* line_adver_fec */ + if (port.find("line_adver_fec") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_adver_fec' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + vals = port["line_adver_fec"]; // vec + valsStr = ""; + for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) + { + val = vals[iter1]; + if (valsStr.length() > 0) + { + valsStr += ","; + } + valsStr += std::to_string(val.get()); + } + attr = std::make_pair("line_adver_fec", valsStr); + attrs.push_back(attr); + + /* line_adver_auto_neg */ + if (port.find("line_adver_auto_neg") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_adver_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_adver_auto_neg"]; + if (val.get() != false && val.get() != true) + { + SWSS_LOG_ERROR("not a boolean: 'line_adver_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineAdverAutoNegStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_adver_auto_neg", lineAdverAutoNegStr); + attrs.push_back(attr); + + /* line_adver_asym_pause */ + if (port.find("line_adver_asym_pause") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_adver_asym_pause"]; + if (val.get() != false && val.get() != true) + { + SWSS_LOG_ERROR("not a boolean: 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + std::string lineAdverAsymPauseStr = val.get() == true ? "true" : "false"; + attr = std::make_pair("line_adver_asym_pause", lineAdverAsymPauseStr); + attrs.push_back(attr); + + /* line_adver_media_type */ + if (port.find("line_adver_media_type") == port.end()) + { + SWSS_LOG_ERROR("missing 'line_adver_media_type' field of item %d of 'ports' field in phy configuration", iter); + return false; + } + val = port["line_adver_media_type"]; + attr = std::make_pair("line_adver_media_type", val.get()); + attrs.push_back(attr); + + std::string key; + int phyId = getPhyId(); + key = "phy:" + std::to_string(phyId) + ":ports:" + std::to_string(id); + if (getWriteToDb() == true) + { + writeToDb(key, attrs); + } + } + catch (const std::exception& e) + { + SWSS_LOG_ERROR("unable to read ports configuration item %d (invalid format): %s", iter, e.what()); + return false; + } + } + } + else + { + SWSS_LOG_ERROR("missing 'ports' field in phy configuration"); + return false; } - } else { - SWSS_LOG_ERROR("missing 'ports' field in phy configuration"); - return false; - } - return true; + return true; } diff --git a/gearsyncd/phyparser.h b/gearsyncd/phyparser.h index 65deead685..f37125e23d 100644 --- a/gearsyncd/phyparser.h +++ b/gearsyncd/phyparser.h @@ -22,11 +22,11 @@ class PhyParser: public GearParserBase { public: - bool parse(); - void setPhyId(int id) {m_phyId = id;}; - int getPhyId() {return m_phyId;}; + bool parse(); + void setPhyId(int id) {m_phyId = id;}; + int getPhyId() {return m_phyId;}; private: - int m_phyId; + int m_phyId; }; #endif /* __PHY_PARSER_H__ */ diff --git a/gearsyncd/tests/testgearparser.cpp b/gearsyncd/tests/testgearparser.cpp index e01c85bb19..2642258999 100644 --- a/gearsyncd/tests/testgearparser.cpp +++ b/gearsyncd/tests/testgearparser.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019 Broadcom Inc. + * Copyright 2020 Broadcom Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,122 +29,124 @@ bool handleGearboxConfigFile(std::string file, bool warm); void positiveConfigTest() { - bool ret; + bool ret; - ret = handleGearboxConfigFile("tests/configs/positive/gearbox_config_1.json", false); - CU_ASSERT_EQUAL(ret, true); + ret = handleGearboxConfigFile("tests/configs/positive/gearbox_config_1.json", false); + CU_ASSERT_EQUAL(ret, true); } void negativeConfigTest() { - bool ret; - - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_array.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_json.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phys.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_id.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_name.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_address.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_lib_name.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_firmware_path.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_config_file.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_sai_init_config_file.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_phy_access.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_bus_id.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_phy_config_file.json", false); - CU_ASSERT_EQUAL(ret, false); - - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_interfaces.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_index.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_phy_id.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_line_lanes.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_system_lanes.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_empty_ethernet_system_lanes.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_empty_ethernet_line_lanes.json", false); - CU_ASSERT_EQUAL(ret, false); - - //ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_line_lane_speed.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_system_lane_speed.json", false); - //CU_ASSERT_EQUAL(ret, false); + bool ret; + + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_array.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_json.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phys.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_name.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_address.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_lib_name.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_firmware_path.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_config_file.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_sai_init_config_file.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_phy_access.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_phy_bus_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_invalid_phy_config_file.json", false); + CU_ASSERT_EQUAL(ret, false); + + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_interfaces.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_index.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_phy_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_line_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_missing_ethernet_system_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_empty_ethernet_system_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handleGearboxConfigFile("tests/configs/negative/gearbox_config_empty_ethernet_line_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); } int unitTestMain() { - CU_pSuite pSuite = NULL; - CU_pTest pTest = NULL; - int ret = 0; - - if (CUE_SUCCESS != CU_initialize_registry()) { - printf("%s: cunit failed to initialize registry\n", __FUNCTION__); - ret = CU_get_error(); - } else { - - CU_basic_set_mode(CU_BRM_VERBOSE); - - // Run the tests and show the run summary - pSuite = CU_add_suite("gearbox_config_suite", 0, 0); - if (NULL == pSuite) { - printf("%s: cunit failed to create suite\n", __FUNCTION__); - CU_cleanup_registry(); - ret = CU_get_error(); + CU_pSuite pSuite = NULL; + CU_pTest pTest = NULL; + int ret = 0; + + if (CUE_SUCCESS != CU_initialize_registry()) + { + printf("%s: cunit failed to initialize registry\n", __FUNCTION__); + ret = CU_get_error(); + } + else + { + CU_basic_set_mode(CU_BRM_VERBOSE); + + // Run the tests and show the run summary + pSuite = CU_add_suite("gearbox_config_suite", 0, 0); + if (NULL == pSuite) + { + printf("%s: cunit failed to create suite\n", __FUNCTION__); + CU_cleanup_registry(); + ret = CU_get_error(); + } } - } - // Add the test to the suite - if (ret == 0) { - pTest = CU_add_test(pSuite, "gearbox_positive_config_test", positiveConfigTest); - if (pTest != NULL) { - pTest = CU_add_test(pSuite, "gearbox_negative_config_test", negativeConfigTest); + // Add the test to the suite + if (ret == 0) + { + pTest = CU_add_test(pSuite, "gearbox_positive_config_test", positiveConfigTest); + if (pTest != NULL) + { + pTest = CU_add_test(pSuite, "gearbox_negative_config_test", negativeConfigTest); + } + + if (pTest == NULL) + { + CU_cleanup_registry(); + printf("%s: cunit failed to add test\n", __FUNCTION__); + ret = CU_get_error(); + } } - if (pTest == NULL) { - CU_cleanup_registry(); - printf("%s: cunit failed to add test\n", __FUNCTION__); - ret = CU_get_error(); + if (ret == 0) + { + CU_basic_run_tests(); } - } - - if (ret == 0) { - CU_basic_run_tests(); - } - return ret; + return ret; } bool handleGearboxConfigFile(std::string file, bool warm) { - GearboxParser p; - bool ret; + GearboxParser p; + bool ret; - p.setWriteToDb(true); - p.setConfigPath(file); - ret = p.parse(); - return ret; + p.setWriteToDb(true); + p.setConfigPath(file); + ret = p.parse(); + return ret; } int main(int argc, char **argv) { - swss::Logger::linkToDbNative("testgearparser"); + swss::Logger::linkToDbNative("testgearparser"); - unitTestMain(); - return 1; + unitTestMain(); + return 1; } diff --git a/gearsyncd/tests/testphyparser.cpp b/gearsyncd/tests/testphyparser.cpp index fa66680609..4acc6f634a 100644 --- a/gearsyncd/tests/testphyparser.cpp +++ b/gearsyncd/tests/testphyparser.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2019 Broadcom Inc. + * Copyright 2020 Broadcom Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,165 +29,146 @@ bool handlePhyConfigFile(std::string file, bool warm); void positiveConfigTest() { - bool ret; + bool ret; - ret = handlePhyConfigFile("tests/configs/positive/phy1_config_1.json", false); - CU_ASSERT_EQUAL(ret, true); - ret = handlePhyConfigFile("tests/configs/positive/phy2_config_1.json", false); - CU_ASSERT_EQUAL(ret, true); + ret = handlePhyConfigFile("tests/configs/positive/phy1_config_1.json", false); + CU_ASSERT_EQUAL(ret, true); + ret = handlePhyConfigFile("tests/configs/positive/phy2_config_1.json", false); + CU_ASSERT_EQUAL(ret, true); } void negativeConfigTest() { - bool ret; - - ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_json.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_lanes.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_id.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_system_side.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_local_lane_id.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_tx_polarity.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_rx_polarity.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_tx_lanemap.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_rx_lanemap.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_to_system_lanemap.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_mdio_addr.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_id.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_mdio_addr.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_speed.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_fec.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_auto_neg.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_loopback.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_training.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_speed.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_fec.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_auto_neg.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_media_type.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_intf_type.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_loopback.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_training.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_speed.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_fec.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_auto_neg.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_asym_pause.json", false); - CU_ASSERT_EQUAL(ret, false); - ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_media_type.json", false); - CU_ASSERT_EQUAL(ret, false); - - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_lanes.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_lane_speed.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_lanes.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_lane_speed.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_boolean.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_boolean2.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_address.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_array.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_mode.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_name.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_port_ref_clk.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_empty_line_lanes.json", false); - //CU_ASSERT_EQUAL(ret, false); - //ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_empty_system_lanes.json", false); - //CU_ASSERT_EQUAL(ret, false); + bool ret; + + ret = handlePhyConfigFile("tests/configs/negative/phy_config_invalid_json.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_missing_lanes.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_system_side.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_local_lane_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_tx_polarity.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_rx_polarity.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_tx_lanemap.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_rx_lanemap.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_line_to_system_lanemap.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_lanes_missing_mdio_addr.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_id.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_mdio_addr.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_speed.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_fec.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_auto_neg.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_loopback.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_system_training.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_speed.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_fec.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_auto_neg.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_media_type.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_intf_type.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_loopback.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_training.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_speed.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_fec.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_auto_neg.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_asym_pause.json", false); + CU_ASSERT_EQUAL(ret, false); + ret = handlePhyConfigFile("tests/configs/negative/phy_config_ports_missing_line_adver_media_type.json", false); + CU_ASSERT_EQUAL(ret, false); } int unitTestMain() { - CU_pSuite pSuite = NULL; - CU_pTest pTest = NULL; - int ret = 0; - - if (CUE_SUCCESS != CU_initialize_registry()) { - printf("%s: cunit failed to initialize registry\n", __FUNCTION__); - ret = CU_get_error(); - } else { - - CU_basic_set_mode(CU_BRM_VERBOSE); - - // Run the tests and show the run summary - pSuite = CU_add_suite("phy_config_suite", 0, 0); - if (NULL == pSuite) { - printf("%s: cunit failed to create suite\n", __FUNCTION__); - CU_cleanup_registry(); - ret = CU_get_error(); - } - } - - // Add the test to the suite - if (ret == 0) { - pTest = CU_add_test(pSuite, "phy_positive_config_test", positiveConfigTest); - - if (pTest != NULL) { - pTest = CU_add_test(pSuite, "phy_negative_config_test", negativeConfigTest); + CU_pSuite pSuite = NULL; + CU_pTest pTest = NULL; + int ret = 0; + + if (CUE_SUCCESS != CU_initialize_registry()) + { + printf("%s: cunit failed to initialize registry\n", __FUNCTION__); + ret = CU_get_error(); + } + else + { + + CU_basic_set_mode(CU_BRM_VERBOSE); + + // Run the tests and show the run summary + pSuite = CU_add_suite("phy_config_suite", 0, 0); + if (NULL == pSuite) + { + printf("%s: cunit failed to create suite\n", __FUNCTION__); + CU_cleanup_registry(); + ret = CU_get_error(); + } } - if (pTest == NULL) { - CU_cleanup_registry(); - printf("%s: cunit failed to add test\n", __FUNCTION__); - ret = CU_get_error(); + // Add the test to the suite + if (ret == 0) + { + pTest = CU_add_test(pSuite, "phy_positive_config_test", positiveConfigTest); + + if (pTest != NULL) + { + pTest = CU_add_test(pSuite, "phy_negative_config_test", negativeConfigTest); + } + + if (pTest == NULL) + { + CU_cleanup_registry(); + printf("%s: cunit failed to add test\n", __FUNCTION__); + ret = CU_get_error(); + } + } + if (ret == 0) + { + CU_basic_run_tests(); } - } - if (ret == 0) { - CU_basic_run_tests(); - } - return ret; + return ret; } bool handlePhyConfigFile(std::string file, bool warm) { - PhyParser p; - bool ret; + PhyParser p; + bool ret; - p.setWriteToDb(true); - p.setConfigPath(file); - ret = p.parse(); - return ret; + p.setWriteToDb(true); + p.setConfigPath(file); + ret = p.parse(); + return ret; } int main(int argc, char **argv) { - swss::Logger::linkToDbNative("testphyparser"); + swss::Logger::linkToDbNative("testphyparser"); - unitTestMain(); - return 1; + unitTestMain(); + return 1; } diff --git a/lib/gearboxutils.cpp b/lib/gearboxutils.cpp index bcdd9e2896..d17ed3a02f 100644 --- a/lib/gearboxutils.cpp +++ b/lib/gearboxutils.cpp @@ -226,7 +226,7 @@ std::map GearboxUtils::loadInterfaceMap(Table *gearbox interface.phy_id = std::stoi(val.second); SWSS_LOG_DEBUG("BOX phy_id = %d", interface.phy_id); } - else if (val.first == "line_lanes") + else if (val.first == "line_lanes") { std::stringstream ss(val.second); @@ -452,7 +452,6 @@ std::map GearboxUtils::loadPortMap(Table *gearboxTable) gearboxPortMap[port.index] = port; } } - return gearboxPortMap; } diff --git a/orchagent/main.cpp b/orchagent/main.cpp index 3292dcb480..9da230bfb6 100644 --- a/orchagent/main.cpp +++ b/orchagent/main.cpp @@ -251,16 +251,6 @@ int main(int argc, char **argv) attr.value.ptr = (void *)on_port_state_change; attrs.push_back(attr); -#if 0 - attr.id = SAI_SWITCH_ATTR_HARDWARE_ACCESS_BUS; - attr.value.s32 = SAI_SWITCH_HARDWARE_ACCESS_BUS_MDIO; - attrs.push_back(attr); - - attr.id = SAI_SWITCH_ATTR_PLATFROM_CONTEXT; - attr.value.u64 = (sai_uint64_t) 0xffffffffffffffff; - attrs.push_back(attr); -#endif - attr.id = SAI_SWITCH_ATTR_SHUTDOWN_REQUEST_NOTIFY; attr.value.ptr = (void *)on_switch_shutdown_request; attrs.push_back(attr); @@ -311,7 +301,7 @@ int main(int argc, char **argv) status = sai_switch_api->get_switch_attribute(gSwitchId, 1, &attr); if (status != SAI_STATUS_SUCCESS) { - SWSS_LOG_ERROR("Fail to get MAC address from switch, rv:%d", status); + SWSS_LOG_ERROR("Failed to get MAC address from switch, rv:%d", status); exit(EXIT_FAILURE); } else diff --git a/orchagent/saihelper.cpp b/orchagent/saihelper.cpp index 7372ec48c7..67a67223cc 100644 --- a/orchagent/saihelper.cpp +++ b/orchagent/saihelper.cpp @@ -71,14 +71,14 @@ sai_status_t mdio_read(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, uint32_t number_of_registers, uint32_t *data) { - return SAI_STATUS_FAILURE; + return SAI_STATUS_FAILURE; } sai_status_t mdio_write(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, uint32_t number_of_registers, uint32_t *data) { - return SAI_STATUS_FAILURE; + return SAI_STATUS_FAILURE; } const char *test_profile_get_value ( From 782e68513f9301ded8a1939605e522c9f2491414 Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Tue, 23 Jun 2020 10:48:06 -0700 Subject: [PATCH 4/9] fix a few braces/indents --- gearsyncd/gearboxparser.cpp | 11 ++++++----- gearsyncd/gearparserbase.cpp | 8 ++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/gearsyncd/gearboxparser.cpp b/gearsyncd/gearboxparser.cpp index 85acb680be..65e0b4f05d 100644 --- a/gearsyncd/gearboxparser.cpp +++ b/gearsyncd/gearboxparser.cpp @@ -50,10 +50,10 @@ bool GearboxParser::parse() { phys = root["phys"]; if (phys.size() == 0) - { - SWSS_LOG_ERROR("zero-sized 'phys' field in gearbox configuration"); - return false; - } + { + SWSS_LOG_ERROR("zero-sized 'phys' field in gearbox configuration"); + return false; + } } catch (const std::exception& e) { @@ -64,7 +64,8 @@ bool GearboxParser::parse() for (uint32_t iter = 0; iter < phys.size(); iter++) { phy = phys[iter]; - try { + try + { attrs.clear(); swss::FieldValueTuple attr; if (phy.find("phy_id") == phy.end()) diff --git a/gearsyncd/gearparserbase.cpp b/gearsyncd/gearparserbase.cpp index 72aa786e98..c6cae36253 100644 --- a/gearsyncd/gearparserbase.cpp +++ b/gearsyncd/gearparserbase.cpp @@ -22,10 +22,10 @@ void GearParserBase::init() { - m_writeToDb = false; - m_rootInit = false; - m_applDb = std::unique_ptr{new swss::DBConnector(APPL_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, 0)}; - m_producerStateTable = std::unique_ptr{new swss::ProducerStateTable(m_applDb.get(), APP_GEARBOX_TABLE_NAME)}; + m_writeToDb = false; + m_rootInit = false; + m_applDb = std::unique_ptr{new swss::DBConnector(APPL_DB, swss::DBConnector::DEFAULT_UNIXSOCKET, 0)}; + m_producerStateTable = std::unique_ptr{new swss::ProducerStateTable(m_applDb.get(), APP_GEARBOX_TABLE_NAME)}; } GearParserBase::GearParserBase() From cd4d09eedaef51975b69a221a23a3bbff6a012ea Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Wed, 24 Jun 2020 17:33:10 -0700 Subject: [PATCH 5/9] replace ifdef guards with #pragma once --- gearsyncd/gearboxparser.h | 6 ++---- gearsyncd/gearparserbase.h | 6 ++---- gearsyncd/phyparser.h | 6 ++---- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/gearsyncd/gearboxparser.h b/gearsyncd/gearboxparser.h index caea1dafe6..dc5ec39683 100644 --- a/gearsyncd/gearboxparser.h +++ b/gearsyncd/gearboxparser.h @@ -1,5 +1,5 @@ /* - * Copyright 2019 Broadcom Inc. + * Copyright 2019-2020 Broadcom Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,8 +14,7 @@ * limitations under the License. */ -#if !defined(__GEARBOX_PARSER_H__) -#define __GEARBOX_PARSER_H__ +#pragma once #include "gearparserbase.h" @@ -26,4 +25,3 @@ class GearboxParser: public GearParserBase void notifyGearboxConfigDone(bool success); }; -#endif /* __GEARBOX_PARSER_H__ */ diff --git a/gearsyncd/gearparserbase.h b/gearsyncd/gearparserbase.h index 5b3b176ba1..d9db7556d9 100644 --- a/gearsyncd/gearparserbase.h +++ b/gearsyncd/gearparserbase.h @@ -1,5 +1,5 @@ /* - * Copyright 2019 Broadcom Inc. + * Copyright 2019-2020 Broadcom Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,8 +14,7 @@ * limitations under the License. */ -#if !defined(__GEAR_PARSER_BASE_H__) -#define __GEAR_PARSER_BASE_H__ +#pragma once #include "dbconnector.h" #include "producerstatetable.h" @@ -59,4 +58,3 @@ class GearParserBase bool m_rootInit; }; -#endif // __GEAR_PARSER_BASE_H__ diff --git a/gearsyncd/phyparser.h b/gearsyncd/phyparser.h index f37125e23d..dcb42d70d5 100644 --- a/gearsyncd/phyparser.h +++ b/gearsyncd/phyparser.h @@ -1,5 +1,5 @@ /* - * Copyright 2019 Broadcom Inc. + * Copyright 2019-2020 Broadcom Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,8 +14,7 @@ * limitations under the License. */ -#if !defined(__PHY_PARSER_H__) -#define __PHY_PARSER_H__ +#pragma once #include "gearparserbase.h" @@ -29,4 +28,3 @@ class PhyParser: public GearParserBase int m_phyId; }; -#endif /* __PHY_PARSER_H__ */ From f7b593951936b795bf8d8e92f714a36a2659fabc Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Wed, 24 Jun 2020 17:33:10 -0700 Subject: [PATCH 6/9] remove placeholder while loop in gearsyncd --- gearsyncd/gearsyncd.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/gearsyncd/gearsyncd.cpp b/gearsyncd/gearsyncd.cpp index 3bf90fad01..a447416da3 100644 --- a/gearsyncd/gearsyncd.cpp +++ b/gearsyncd/gearsyncd.cpp @@ -99,17 +99,10 @@ int main(int argc, char **argv) handleGearboxConfigFile(gearbox_config_file, warm); } } - - while (true) - { - // placeholder, at some point we may select on cfgDb. - - sleep(1000); - } } catch (const std::exception& e) { - cerr << "Exception \"" << e.what() << "\" had been thrown in deamon" << endl; + cerr << "Exception \"" << e.what() << "\" had been thrown in gearsyncd daemon" << endl; return EXIT_FAILURE; } return 1; From a895347715fd0bf9b11827192338745bef524629 Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Wed, 1 Jul 2020 10:08:50 -0700 Subject: [PATCH 7/9] remove cunit sources, Makefile.am for gearsyncd tests --- Makefile.am | 2 +- configure.ac | 1 - gearsyncd/tests/Makefile.am | 21 - gearsyncd/tests/cunit/Automated.c | 623 ------ gearsyncd/tests/cunit/Automated.cpp | 623 ------ gearsyncd/tests/cunit/Automated.h | 90 - gearsyncd/tests/cunit/Basic.c | 334 --- gearsyncd/tests/cunit/Basic.cpp | 334 --- gearsyncd/tests/cunit/Basic.h | 113 -- gearsyncd/tests/cunit/CUError.c | 231 --- gearsyncd/tests/cunit/CUError.cpp | 231 --- gearsyncd/tests/cunit/CUError.h | 199 -- gearsyncd/tests/cunit/CUnit.h | 383 ---- gearsyncd/tests/cunit/CUnit_intl.h | 62 - gearsyncd/tests/cunit/Console.c | 735 ------- gearsyncd/tests/cunit/Console.cpp | 735 ------- gearsyncd/tests/cunit/Console.h | 60 - gearsyncd/tests/cunit/MyMem.c | 585 ------ gearsyncd/tests/cunit/MyMem.cpp | 585 ------ gearsyncd/tests/cunit/MyMem.h | 104 - gearsyncd/tests/cunit/TestDB.c | 2773 ------------------------- gearsyncd/tests/cunit/TestDB.cpp | 2773 ------------------------- gearsyncd/tests/cunit/TestDB.h | 914 --------- gearsyncd/tests/cunit/TestRun.c | 2923 --------------------------- gearsyncd/tests/cunit/TestRun.cpp | 2913 -------------------------- gearsyncd/tests/cunit/TestRun.h | 444 ---- gearsyncd/tests/cunit/Util.c | 609 ------ gearsyncd/tests/cunit/Util.cpp | 609 ------ gearsyncd/tests/cunit/Util.h | 158 -- 29 files changed, 1 insertion(+), 20166 deletions(-) delete mode 100644 gearsyncd/tests/Makefile.am delete mode 100644 gearsyncd/tests/cunit/Automated.c delete mode 100644 gearsyncd/tests/cunit/Automated.cpp delete mode 100644 gearsyncd/tests/cunit/Automated.h delete mode 100644 gearsyncd/tests/cunit/Basic.c delete mode 100644 gearsyncd/tests/cunit/Basic.cpp delete mode 100644 gearsyncd/tests/cunit/Basic.h delete mode 100644 gearsyncd/tests/cunit/CUError.c delete mode 100644 gearsyncd/tests/cunit/CUError.cpp delete mode 100644 gearsyncd/tests/cunit/CUError.h delete mode 100644 gearsyncd/tests/cunit/CUnit.h delete mode 100644 gearsyncd/tests/cunit/CUnit_intl.h delete mode 100644 gearsyncd/tests/cunit/Console.c delete mode 100644 gearsyncd/tests/cunit/Console.cpp delete mode 100644 gearsyncd/tests/cunit/Console.h delete mode 100644 gearsyncd/tests/cunit/MyMem.c delete mode 100644 gearsyncd/tests/cunit/MyMem.cpp delete mode 100644 gearsyncd/tests/cunit/MyMem.h delete mode 100644 gearsyncd/tests/cunit/TestDB.c delete mode 100644 gearsyncd/tests/cunit/TestDB.cpp delete mode 100644 gearsyncd/tests/cunit/TestDB.h delete mode 100644 gearsyncd/tests/cunit/TestRun.c delete mode 100644 gearsyncd/tests/cunit/TestRun.cpp delete mode 100644 gearsyncd/tests/cunit/TestRun.h delete mode 100644 gearsyncd/tests/cunit/Util.c delete mode 100644 gearsyncd/tests/cunit/Util.cpp delete mode 100644 gearsyncd/tests/cunit/Util.h diff --git a/Makefile.am b/Makefile.am index 6fa155d678..add44177f8 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = fpmsyncd neighsyncd portsyncd mclagsyncd natsyncd orchagent swssconfig cfgmgr tests gearsyncd gearsyncd/tests +SUBDIRS = fpmsyncd neighsyncd portsyncd mclagsyncd natsyncd orchagent swssconfig cfgmgr tests gearsyncd if HAVE_LIBTEAM SUBDIRS += teamsyncd diff --git a/configure.ac b/configure.ac index 81e2752457..5753f553f1 100644 --- a/configure.ac +++ b/configure.ac @@ -90,7 +90,6 @@ AC_CONFIG_FILES([ fpmsyncd/Makefile neighsyncd/Makefile gearsyncd/Makefile - gearsyncd/tests/Makefile natsyncd/Makefile portsyncd/Makefile teamsyncd/Makefile diff --git a/gearsyncd/tests/Makefile.am b/gearsyncd/tests/Makefile.am deleted file mode 100644 index 3b9f94e6e7..0000000000 --- a/gearsyncd/tests/Makefile.am +++ /dev/null @@ -1,21 +0,0 @@ -INCLUDES = -I.. -I $(top_srcdir) -I $(top_srcdir)/warmrestart -I $(top_srcdir)/cfgmgr - -noinst_PROGRAMS = testphyparser testgearparser -noinst_LIBRARIES = libcunit.a - -if DEBUG -DBGFLAGS = -ggdb -DDEBUG -else -DBGFLAGS = -g -endif - -testphyparser_SOURCES = testphyparser.cpp ../gearparserbase.cpp ../phyparser.cpp $(top_srcdir)/cfgmgr/shellcmd.h -testgearparser_SOURCES = testgearparser.cpp ../gearparserbase.cpp ../gearboxparser.cpp ../phyparser.cpp $(top_srcdir)/cfgmgr/shellcmd.h - -libcunit_a_SOURCES = cunit/Automated.c cunit/Console.c cunit/MyMem.c cunit/TestRun.c cunit/Basic.c cunit/CUError.c cunit/TestDB.c cunit/Util.c - -testphyparser_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(ASAN_CFLAGS) -testgearparser_CPPFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(COV_CFLAGS) $(ASAN_CFLAGS) - -testphyparser_LDADD = -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS) $(ASAN_LDFLAGS) libcunit.a -testgearparser_LDADD = -lnl-3 -lnl-route-3 -lswsscommon $(COV_LDFLAGS) $(ASAN_LDFLAGS) libcunit.a diff --git a/gearsyncd/tests/cunit/Automated.c b/gearsyncd/tests/cunit/Automated.c deleted file mode 100644 index 79d5a68054..0000000000 --- a/gearsyncd/tests/cunit/Automated.c +++ /dev/null @@ -1,623 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of the Automated Test Interface. - * - * Feb 2002 Initial implementation. (AK) - * - * 13/Feb/2002 Added initial automated interface functions to generate - * HTML based Run report. (AK) - * - * 23/Jul/2002 Changed HTML to XML Format file generation for Automated Tests. (AK) - * - * 27/Jul/2003 Fixed a bug which hinders the listing of all failures. (AK) - * - * 17-Jul-2004 New interface, doxygen comments, eliminate compiler warnings, - * automated_run_tests now assigns a generic file name if - * none has been supplied. (JDS) - * - * 30-Apr-2005 Added notification of failed suite cleanup function. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Automated test interface with xml result output (implementation). - */ -/** @addtogroup Automated - @{ -*/ - -#include -#include -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "MyMem.h" -#include "Util.h" -#include "TestRun.h" -#include "Automated.h" -#include "CUnit_intl.h" - -#define MAX_FILENAME_LENGTH 1025 - -/*================================================================= - * Global / Static data definitions - *=================================================================*/ -static CU_pSuite f_pRunningSuite = NULL; /**< The running test suite. */ -static char f_szDefaultFileRoot[] = "CUnitAutomated"; /**< Default filename root for automated output files. */ -static char f_szTestListFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test listing file. */ -static char f_szTestResultFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test results file. */ -static FILE* f_pTestResultFile = NULL; /**< FILE pointer the test results file. */ - -static CU_BOOL f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; /**< Flag for keeping track of when a closing xml tag is required. */ - -/*================================================================= - * Static function forward declarations - *=================================================================*/ -static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename); - -static CU_ErrorCode initialize_result_file(const char* szFilename); -static CU_ErrorCode uninitialize_result_file(void); - -static void automated_run_all_tests(CU_pTestRegistry pRegistry); - -static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); -static void automated_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); -static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); -static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite); -static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -void CU_automated_run_tests(void) -{ - assert(NULL != CU_get_registry()); - - /* Ensure output makes it to screen at the moment of a SIGSEGV. */ - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - - /* if a filename root hasn't been set, use the default one */ - if (0 == strlen(f_szTestResultFileName)) { - CU_set_output_filename(f_szDefaultFileRoot); - } - - if (CUE_SUCCESS != initialize_result_file(f_szTestResultFileName)) { - fprintf(stderr, "\n%s", _("ERROR - Failed to create/initialize the result file.")); - } - else { - /* set up the message handlers for writing xml output */ - CU_set_test_start_handler(automated_test_start_message_handler); - CU_set_test_complete_handler(automated_test_complete_message_handler); - CU_set_all_test_complete_handler(automated_all_tests_complete_message_handler); - CU_set_suite_init_failure_handler(automated_suite_init_failure_message_handler); - CU_set_suite_cleanup_failure_handler(automated_suite_cleanup_failure_message_handler); - - f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; - - automated_run_all_tests(NULL); - - if (CUE_SUCCESS != uninitialize_result_file()) { - fprintf(stderr, "\n%s", _("ERROR - Failed to close/uninitialize the result files.")); - } - } -} - -/*------------------------------------------------------------------------*/ -void CU_set_output_filename(const char* szFilenameRoot) -{ - const char* szListEnding = "-Listing.xml"; - const char* szResultEnding = "-Results.xml"; - - /* Construct the name for the listing file */ - if (NULL != szFilenameRoot) { - strncpy(f_szTestListFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); - } - else { - strncpy(f_szTestListFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); - } - - f_szTestListFileName[MAX_FILENAME_LENGTH - strlen(szListEnding) - 1] = '\0'; - strcat(f_szTestListFileName, szListEnding); - - /* Construct the name for the result file */ - if (NULL != szFilenameRoot) { - strncpy(f_szTestResultFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); - } - else { - strncpy(f_szTestResultFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); - } - - f_szTestResultFileName[MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1] = '\0'; - strcat(f_szTestResultFileName, szResultEnding); -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_list_tests_to_file() -{ - /* if a filename root hasn't been set, use the default one */ - if (0 == strlen(f_szTestListFileName)) { - CU_set_output_filename(f_szDefaultFileRoot); - } - - return automated_list_all_tests(CU_get_registry(), f_szTestListFileName); -} - -/*================================================================= - * Static function implementation - *=================================================================*/ -/** Runs the registered tests using the automated interface. - * If non-NULL. the specified registry is set as the active - * registry for running the tests. If NULL, then the default - * CUnit test registry is used. The actual test running is - * performed by CU_run_all_tests(). - * @param pRegistry The test registry to run. - */ -static void automated_run_all_tests(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = NULL; - - assert(NULL != f_pTestResultFile); - - f_pRunningSuite = NULL; - - if (NULL != pRegistry) { - pOldRegistry = CU_set_registry(pRegistry); - } - fprintf(f_pTestResultFile," \n"); - CU_run_all_tests(); - if (NULL != pRegistry) { - CU_set_registry(pOldRegistry); - } -} - -/*------------------------------------------------------------------------*/ -/** Initializes the test results file generated by the automated interface. - * A file stream is opened and header information is written. - */ -static CU_ErrorCode initialize_result_file(const char* szFilename) -{ - char* szTime; - time_t tTime = 0; - - CU_set_error(CUE_SUCCESS); - - if ((NULL == szFilename) || (strlen(szFilename) == 0)) { - CU_set_error(CUE_BAD_FILENAME); - } - else if (NULL == (f_pTestResultFile = fopen(szFilename, "w"))) { - CU_set_error(CUE_FOPEN_FAILED); - } - else { - setvbuf(f_pTestResultFile, NULL, _IONBF, 0); - - time(&tTime); - szTime = ctime(&tTime); - fprintf(f_pTestResultFile, - " \n" - " \n" - " Test run start - %s \n", - (NULL != szTime) ? szTime : ""); - } - - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at start of each test. - * The test result file must have been opened before this - * function is called (i.e. f_pTestResultFile non-NULL). - * @param pTest The test being run (non-NULL). - * @param pSuite The suite containing the test (non-NULL). - */ -static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - CU_UNREFERENCED_PARAMETER(pTest); /* not currently used */ - - assert(NULL != pTest); - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - /* write suite close/open tags if this is the 1st test for this szSuite */ - if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { - if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n", - pSuite->pName); - - f_bWriting_CUNIT_RUN_SUITE = CU_TRUE; - f_pRunningSuite = pSuite; - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of each test. - * @param pTest The test being run (non-NULL). - * @param pSuite The suite containing the test (non-NULL). - * @param pFailure Pointer to the 1st failure record for this test. - */ -static void automated_test_complete_message_handler(const CU_pTest pTest, - const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - char *szTemp = NULL; - size_t szTemp_len = 0; - size_t cur_len = 0; - CU_pFailureRecord pTempFailure = pFailure; - - CU_UNREFERENCED_PARAMETER(pSuite); /* pSuite is not used except in assertion */ - - assert(NULL != pTest); - assert(NULL != pTest->pName); - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - if (NULL != pTempFailure) { - - while (NULL != pTempFailure) { - - assert((NULL != pTempFailure->pSuite) && (pTempFailure->pSuite == pSuite)); - assert((NULL != pTempFailure->pTest) && (pTempFailure->pTest == pTest)); - - /* expand temporary char buffer if need more room */ - if (NULL != pTempFailure->strCondition) { - cur_len = CU_translated_strlen(pTempFailure->strCondition) + 1; - } - else { - cur_len = 1; - } - if (cur_len > szTemp_len) { - szTemp_len = cur_len; - if (NULL != szTemp) { - CU_FREE(szTemp); - } - szTemp = (char *)CU_MALLOC(szTemp_len); - } - - /* convert xml entities in strCondition (if present) */ - if (NULL != pTempFailure->strCondition) { - CU_translate_special_characters(pTempFailure->strCondition, szTemp, szTemp_len); - } - else { - szTemp[0] = '\0'; - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " %s \n" - " %u \n" - " %s \n" - " \n" - " \n", - pTest->pName, - (NULL != pTempFailure->strFileName) ? pTempFailure->strFileName : "", - pTempFailure->uiLineNumber, - szTemp); - pTempFailure = pTempFailure->pNext; - } - } - else { - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " \n" - " \n", - pTest->pName); - } - - if (NULL != szTemp) { - CU_FREE(szTemp); - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of all tests in a suite. - * @param pFailure Pointer to the test failure record list. - */ -static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) -{ - CU_pTestRegistry pRegistry = CU_get_registry(); - CU_pRunSummary pRunSummary = CU_get_run_summary(); - - CU_UNREFERENCED_PARAMETER(pFailure); /* not used */ - - assert(NULL != pRegistry); - assert(NULL != pRunSummary); - assert(NULL != f_pTestResultFile); - - if ((NULL != f_pRunningSuite) && (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE)) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - } - - fprintf(f_pTestResultFile, - " \n" - " \n"); - - fprintf(f_pTestResultFile, - " \n" - " %s \n" - " %u \n" - " %u \n" - " - NA - \n" - " %u \n" - " %u \n" - " \n", - _("Suites"), - pRegistry->uiNumberOfSuites, - pRunSummary->nSuitesRun, - pRunSummary->nSuitesFailed, - pRunSummary->nSuitesInactive - ); - - fprintf(f_pTestResultFile, - " \n" - " %s \n" - " %u \n" - " %u \n" - " %u \n" - " %u \n" - " %u \n" - " \n", - _("Test Cases"), - pRegistry->uiNumberOfTests, - pRunSummary->nTestsRun, - pRunSummary->nTestsRun - pRunSummary->nTestsFailed, - pRunSummary->nTestsFailed, - pRunSummary->nTestsInactive - ); - - fprintf(f_pTestResultFile, - " \n" - " %s \n" - " %u \n" - " %u \n" - " %u \n" - " %u \n" - " %s \n" - " \n" - " \n", - _("Assertions"), - pRunSummary->nAsserts, - pRunSummary->nAsserts, - pRunSummary->nAsserts - pRunSummary->nAssertsFailed, - pRunSummary->nAssertsFailed, - _("n/a") - ); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite initialization fails. - * @param pSuite The suite for which initialization failed. - */ -static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " %s \n" - " \n" - " \n", - pSuite->pName, - _("Suite Initialization Failed")); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite cleanup fails. - * @param pSuite The suite for which cleanup failed. - */ -static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " %s \n" - " \n" - " \n", - pSuite->pName, - _("Suite Cleanup Failed")); -} - -/*------------------------------------------------------------------------*/ -/** Finalizes and closes the results output file generated - * by the automated interface. - */ -static CU_ErrorCode uninitialize_result_file(void) -{ - - assert(NULL != f_pTestResultFile); - - CU_set_error(CUE_SUCCESS); - - fprintf(f_pTestResultFile,""); - - if (0 != fflush(f_pTestResultFile)) { - CU_set_error(CUE_FCLOSE_FAILED); - } - - if (0 != fclose(f_pTestResultFile)) { - CU_set_error(CUE_FCLOSE_FAILED); - } - - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -/** Generates an xml listing of all tests in all suites for the - * specified test registry. The output is directed to a file - * having the specified name. - * @param pRegistry Test registry for which to generate list (non-NULL). - * @param szFilename Non-NULL, non-empty string containing name for - * listing file. - * @return A CU_ErrorCode indicating the error status. - */ -static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename) -{ - CU_pSuite pSuite = NULL; - CU_pTest pTest = NULL; - FILE* pTestListFile = NULL; - char* szTime; - time_t tTime = 0; - - CU_set_error(CUE_SUCCESS); - - if (NULL == pRegistry) { - CU_set_error(CUE_NOREGISTRY); - } - else if ((NULL == szFilename) || (0 == strlen(szFilename))) { - CU_set_error(CUE_BAD_FILENAME); - } - else if (NULL == (pTestListFile = fopen(f_szTestListFileName, "w"))) { - CU_set_error(CUE_FOPEN_FAILED); - } - else { - setvbuf(pTestListFile, NULL, _IONBF, 0); - - fprintf(pTestListFile, - " \n" - " \n" - " \n" - " \n"); - - fprintf(pTestListFile, - " \n" - " %s \n" - " %u \n" - " \n", - _("Total Number of Suites"), - pRegistry->uiNumberOfSuites); - - fprintf(pTestListFile, - " \n" - " %s \n" - " %u \n" - " \n" - " \n", - _("Total Number of Test Cases"), - pRegistry->uiNumberOfTests); - - fprintf(pTestListFile, - " \n"); - - pSuite = pRegistry->pSuite; - while (NULL != pSuite) { - assert(NULL != pSuite->pName); - pTest = pSuite->pTest; - - fprintf(pTestListFile, - " \n" - " \n" - " %s \n" - " %s \n" - " %s \n" - " %s \n" - " %u \n" - " \n", - pSuite->pName, - (NULL != pSuite->pInitializeFunc) ? _("Yes") : _("No"), - (NULL != pSuite->pCleanupFunc) ? _("Yes") : _("No"), - (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No"), - pSuite->uiNumberOfTests); - - fprintf(pTestListFile, - " \n"); - while (NULL != pTest) { - assert(NULL != pTest->pName); - fprintf(pTestListFile, - " \n" - " %s \n" - " %s \n" - " \n", - pTest->pName, - (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No")); - pTest = pTest->pNext; - } - - fprintf(pTestListFile, - " \n" - " \n"); - - pSuite = pSuite->pNext; - } - - fprintf(pTestListFile, " \n"); - - time(&tTime); - szTime = ctime(&tTime); - fprintf(pTestListFile, - " %s" CU_VERSION " - %s \n" - "", - _("File Generated By CUnit v"), - (NULL != szTime) ? szTime : ""); - - if (0 != fclose(pTestListFile)) { - CU_set_error(CUE_FCLOSE_FAILED); - } - } - - return CU_get_error(); -} - -/** @} */ diff --git a/gearsyncd/tests/cunit/Automated.cpp b/gearsyncd/tests/cunit/Automated.cpp deleted file mode 100644 index 79d5a68054..0000000000 --- a/gearsyncd/tests/cunit/Automated.cpp +++ /dev/null @@ -1,623 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of the Automated Test Interface. - * - * Feb 2002 Initial implementation. (AK) - * - * 13/Feb/2002 Added initial automated interface functions to generate - * HTML based Run report. (AK) - * - * 23/Jul/2002 Changed HTML to XML Format file generation for Automated Tests. (AK) - * - * 27/Jul/2003 Fixed a bug which hinders the listing of all failures. (AK) - * - * 17-Jul-2004 New interface, doxygen comments, eliminate compiler warnings, - * automated_run_tests now assigns a generic file name if - * none has been supplied. (JDS) - * - * 30-Apr-2005 Added notification of failed suite cleanup function. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Automated test interface with xml result output (implementation). - */ -/** @addtogroup Automated - @{ -*/ - -#include -#include -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "MyMem.h" -#include "Util.h" -#include "TestRun.h" -#include "Automated.h" -#include "CUnit_intl.h" - -#define MAX_FILENAME_LENGTH 1025 - -/*================================================================= - * Global / Static data definitions - *=================================================================*/ -static CU_pSuite f_pRunningSuite = NULL; /**< The running test suite. */ -static char f_szDefaultFileRoot[] = "CUnitAutomated"; /**< Default filename root for automated output files. */ -static char f_szTestListFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test listing file. */ -static char f_szTestResultFileName[MAX_FILENAME_LENGTH] = ""; /**< Current output file name for the test results file. */ -static FILE* f_pTestResultFile = NULL; /**< FILE pointer the test results file. */ - -static CU_BOOL f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; /**< Flag for keeping track of when a closing xml tag is required. */ - -/*================================================================= - * Static function forward declarations - *=================================================================*/ -static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename); - -static CU_ErrorCode initialize_result_file(const char* szFilename); -static CU_ErrorCode uninitialize_result_file(void); - -static void automated_run_all_tests(CU_pTestRegistry pRegistry); - -static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); -static void automated_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); -static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); -static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite); -static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -void CU_automated_run_tests(void) -{ - assert(NULL != CU_get_registry()); - - /* Ensure output makes it to screen at the moment of a SIGSEGV. */ - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - - /* if a filename root hasn't been set, use the default one */ - if (0 == strlen(f_szTestResultFileName)) { - CU_set_output_filename(f_szDefaultFileRoot); - } - - if (CUE_SUCCESS != initialize_result_file(f_szTestResultFileName)) { - fprintf(stderr, "\n%s", _("ERROR - Failed to create/initialize the result file.")); - } - else { - /* set up the message handlers for writing xml output */ - CU_set_test_start_handler(automated_test_start_message_handler); - CU_set_test_complete_handler(automated_test_complete_message_handler); - CU_set_all_test_complete_handler(automated_all_tests_complete_message_handler); - CU_set_suite_init_failure_handler(automated_suite_init_failure_message_handler); - CU_set_suite_cleanup_failure_handler(automated_suite_cleanup_failure_message_handler); - - f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; - - automated_run_all_tests(NULL); - - if (CUE_SUCCESS != uninitialize_result_file()) { - fprintf(stderr, "\n%s", _("ERROR - Failed to close/uninitialize the result files.")); - } - } -} - -/*------------------------------------------------------------------------*/ -void CU_set_output_filename(const char* szFilenameRoot) -{ - const char* szListEnding = "-Listing.xml"; - const char* szResultEnding = "-Results.xml"; - - /* Construct the name for the listing file */ - if (NULL != szFilenameRoot) { - strncpy(f_szTestListFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); - } - else { - strncpy(f_szTestListFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szListEnding) - 1); - } - - f_szTestListFileName[MAX_FILENAME_LENGTH - strlen(szListEnding) - 1] = '\0'; - strcat(f_szTestListFileName, szListEnding); - - /* Construct the name for the result file */ - if (NULL != szFilenameRoot) { - strncpy(f_szTestResultFileName, szFilenameRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); - } - else { - strncpy(f_szTestResultFileName, f_szDefaultFileRoot, MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1); - } - - f_szTestResultFileName[MAX_FILENAME_LENGTH - strlen(szResultEnding) - 1] = '\0'; - strcat(f_szTestResultFileName, szResultEnding); -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_list_tests_to_file() -{ - /* if a filename root hasn't been set, use the default one */ - if (0 == strlen(f_szTestListFileName)) { - CU_set_output_filename(f_szDefaultFileRoot); - } - - return automated_list_all_tests(CU_get_registry(), f_szTestListFileName); -} - -/*================================================================= - * Static function implementation - *=================================================================*/ -/** Runs the registered tests using the automated interface. - * If non-NULL. the specified registry is set as the active - * registry for running the tests. If NULL, then the default - * CUnit test registry is used. The actual test running is - * performed by CU_run_all_tests(). - * @param pRegistry The test registry to run. - */ -static void automated_run_all_tests(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = NULL; - - assert(NULL != f_pTestResultFile); - - f_pRunningSuite = NULL; - - if (NULL != pRegistry) { - pOldRegistry = CU_set_registry(pRegistry); - } - fprintf(f_pTestResultFile," \n"); - CU_run_all_tests(); - if (NULL != pRegistry) { - CU_set_registry(pOldRegistry); - } -} - -/*------------------------------------------------------------------------*/ -/** Initializes the test results file generated by the automated interface. - * A file stream is opened and header information is written. - */ -static CU_ErrorCode initialize_result_file(const char* szFilename) -{ - char* szTime; - time_t tTime = 0; - - CU_set_error(CUE_SUCCESS); - - if ((NULL == szFilename) || (strlen(szFilename) == 0)) { - CU_set_error(CUE_BAD_FILENAME); - } - else if (NULL == (f_pTestResultFile = fopen(szFilename, "w"))) { - CU_set_error(CUE_FOPEN_FAILED); - } - else { - setvbuf(f_pTestResultFile, NULL, _IONBF, 0); - - time(&tTime); - szTime = ctime(&tTime); - fprintf(f_pTestResultFile, - " \n" - " \n" - " Test run start - %s \n", - (NULL != szTime) ? szTime : ""); - } - - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at start of each test. - * The test result file must have been opened before this - * function is called (i.e. f_pTestResultFile non-NULL). - * @param pTest The test being run (non-NULL). - * @param pSuite The suite containing the test (non-NULL). - */ -static void automated_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - CU_UNREFERENCED_PARAMETER(pTest); /* not currently used */ - - assert(NULL != pTest); - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - /* write suite close/open tags if this is the 1st test for this szSuite */ - if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { - if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n", - pSuite->pName); - - f_bWriting_CUNIT_RUN_SUITE = CU_TRUE; - f_pRunningSuite = pSuite; - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of each test. - * @param pTest The test being run (non-NULL). - * @param pSuite The suite containing the test (non-NULL). - * @param pFailure Pointer to the 1st failure record for this test. - */ -static void automated_test_complete_message_handler(const CU_pTest pTest, - const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - char *szTemp = NULL; - size_t szTemp_len = 0; - size_t cur_len = 0; - CU_pFailureRecord pTempFailure = pFailure; - - CU_UNREFERENCED_PARAMETER(pSuite); /* pSuite is not used except in assertion */ - - assert(NULL != pTest); - assert(NULL != pTest->pName); - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - if (NULL != pTempFailure) { - - while (NULL != pTempFailure) { - - assert((NULL != pTempFailure->pSuite) && (pTempFailure->pSuite == pSuite)); - assert((NULL != pTempFailure->pTest) && (pTempFailure->pTest == pTest)); - - /* expand temporary char buffer if need more room */ - if (NULL != pTempFailure->strCondition) { - cur_len = CU_translated_strlen(pTempFailure->strCondition) + 1; - } - else { - cur_len = 1; - } - if (cur_len > szTemp_len) { - szTemp_len = cur_len; - if (NULL != szTemp) { - CU_FREE(szTemp); - } - szTemp = (char *)CU_MALLOC(szTemp_len); - } - - /* convert xml entities in strCondition (if present) */ - if (NULL != pTempFailure->strCondition) { - CU_translate_special_characters(pTempFailure->strCondition, szTemp, szTemp_len); - } - else { - szTemp[0] = '\0'; - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " %s \n" - " %u \n" - " %s \n" - " \n" - " \n", - pTest->pName, - (NULL != pTempFailure->strFileName) ? pTempFailure->strFileName : "", - pTempFailure->uiLineNumber, - szTemp); - pTempFailure = pTempFailure->pNext; - } - } - else { - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " \n" - " \n", - pTest->pName); - } - - if (NULL != szTemp) { - CU_FREE(szTemp); - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of all tests in a suite. - * @param pFailure Pointer to the test failure record list. - */ -static void automated_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) -{ - CU_pTestRegistry pRegistry = CU_get_registry(); - CU_pRunSummary pRunSummary = CU_get_run_summary(); - - CU_UNREFERENCED_PARAMETER(pFailure); /* not used */ - - assert(NULL != pRegistry); - assert(NULL != pRunSummary); - assert(NULL != f_pTestResultFile); - - if ((NULL != f_pRunningSuite) && (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE)) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - } - - fprintf(f_pTestResultFile, - " \n" - " \n"); - - fprintf(f_pTestResultFile, - " \n" - " %s \n" - " %u \n" - " %u \n" - " - NA - \n" - " %u \n" - " %u \n" - " \n", - _("Suites"), - pRegistry->uiNumberOfSuites, - pRunSummary->nSuitesRun, - pRunSummary->nSuitesFailed, - pRunSummary->nSuitesInactive - ); - - fprintf(f_pTestResultFile, - " \n" - " %s \n" - " %u \n" - " %u \n" - " %u \n" - " %u \n" - " %u \n" - " \n", - _("Test Cases"), - pRegistry->uiNumberOfTests, - pRunSummary->nTestsRun, - pRunSummary->nTestsRun - pRunSummary->nTestsFailed, - pRunSummary->nTestsFailed, - pRunSummary->nTestsInactive - ); - - fprintf(f_pTestResultFile, - " \n" - " %s \n" - " %u \n" - " %u \n" - " %u \n" - " %u \n" - " %s \n" - " \n" - " \n", - _("Assertions"), - pRunSummary->nAsserts, - pRunSummary->nAsserts, - pRunSummary->nAsserts - pRunSummary->nAssertsFailed, - pRunSummary->nAssertsFailed, - _("n/a") - ); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite initialization fails. - * @param pSuite The suite for which initialization failed. - */ -static void automated_suite_init_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " %s \n" - " \n" - " \n", - pSuite->pName, - _("Suite Initialization Failed")); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite cleanup fails. - * @param pSuite The suite for which cleanup failed. - */ -static void automated_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - assert(NULL != f_pTestResultFile); - - if (CU_TRUE == f_bWriting_CUNIT_RUN_SUITE) { - fprintf(f_pTestResultFile, - " \n" - " \n"); - f_bWriting_CUNIT_RUN_SUITE = CU_FALSE; - } - - fprintf(f_pTestResultFile, - " \n" - " \n" - " %s \n" - " %s \n" - " \n" - " \n", - pSuite->pName, - _("Suite Cleanup Failed")); -} - -/*------------------------------------------------------------------------*/ -/** Finalizes and closes the results output file generated - * by the automated interface. - */ -static CU_ErrorCode uninitialize_result_file(void) -{ - - assert(NULL != f_pTestResultFile); - - CU_set_error(CUE_SUCCESS); - - fprintf(f_pTestResultFile,""); - - if (0 != fflush(f_pTestResultFile)) { - CU_set_error(CUE_FCLOSE_FAILED); - } - - if (0 != fclose(f_pTestResultFile)) { - CU_set_error(CUE_FCLOSE_FAILED); - } - - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -/** Generates an xml listing of all tests in all suites for the - * specified test registry. The output is directed to a file - * having the specified name. - * @param pRegistry Test registry for which to generate list (non-NULL). - * @param szFilename Non-NULL, non-empty string containing name for - * listing file. - * @return A CU_ErrorCode indicating the error status. - */ -static CU_ErrorCode automated_list_all_tests(CU_pTestRegistry pRegistry, const char* szFilename) -{ - CU_pSuite pSuite = NULL; - CU_pTest pTest = NULL; - FILE* pTestListFile = NULL; - char* szTime; - time_t tTime = 0; - - CU_set_error(CUE_SUCCESS); - - if (NULL == pRegistry) { - CU_set_error(CUE_NOREGISTRY); - } - else if ((NULL == szFilename) || (0 == strlen(szFilename))) { - CU_set_error(CUE_BAD_FILENAME); - } - else if (NULL == (pTestListFile = fopen(f_szTestListFileName, "w"))) { - CU_set_error(CUE_FOPEN_FAILED); - } - else { - setvbuf(pTestListFile, NULL, _IONBF, 0); - - fprintf(pTestListFile, - " \n" - " \n" - " \n" - " \n"); - - fprintf(pTestListFile, - " \n" - " %s \n" - " %u \n" - " \n", - _("Total Number of Suites"), - pRegistry->uiNumberOfSuites); - - fprintf(pTestListFile, - " \n" - " %s \n" - " %u \n" - " \n" - " \n", - _("Total Number of Test Cases"), - pRegistry->uiNumberOfTests); - - fprintf(pTestListFile, - " \n"); - - pSuite = pRegistry->pSuite; - while (NULL != pSuite) { - assert(NULL != pSuite->pName); - pTest = pSuite->pTest; - - fprintf(pTestListFile, - " \n" - " \n" - " %s \n" - " %s \n" - " %s \n" - " %s \n" - " %u \n" - " \n", - pSuite->pName, - (NULL != pSuite->pInitializeFunc) ? _("Yes") : _("No"), - (NULL != pSuite->pCleanupFunc) ? _("Yes") : _("No"), - (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No"), - pSuite->uiNumberOfTests); - - fprintf(pTestListFile, - " \n"); - while (NULL != pTest) { - assert(NULL != pTest->pName); - fprintf(pTestListFile, - " \n" - " %s \n" - " %s \n" - " \n", - pTest->pName, - (CU_FALSE != pSuite->fActive) ? _("Yes") : _("No")); - pTest = pTest->pNext; - } - - fprintf(pTestListFile, - " \n" - " \n"); - - pSuite = pSuite->pNext; - } - - fprintf(pTestListFile, " \n"); - - time(&tTime); - szTime = ctime(&tTime); - fprintf(pTestListFile, - " %s" CU_VERSION " - %s \n" - "", - _("File Generated By CUnit v"), - (NULL != szTime) ? szTime : ""); - - if (0 != fclose(pTestListFile)) { - CU_set_error(CUE_FCLOSE_FAILED); - } - } - - return CU_get_error(); -} - -/** @} */ diff --git a/gearsyncd/tests/cunit/Automated.h b/gearsyncd/tests/cunit/Automated.h deleted file mode 100644 index 2acc694684..0000000000 --- a/gearsyncd/tests/cunit/Automated.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Automated Interface (generates HTML Report Files). - * - * Feb 2002 Initial implementation (AK) - * - * 13-Feb-2002 Single interface to automated_run_tests. (AK) - * - * 20-Jul-2004 New interface, doxygen comments. (JDS) - */ - -/** @file - * Automated testing interface with xml output (user interface). - */ -/** @addtogroup Automated - * @{ - */ - -#ifndef CUNIT_AUTOMATED_H_SEEN -#define CUNIT_AUTOMATED_H_SEEN - -#include "CUnit.h" -#include "TestDB.h" - -#ifdef __cplusplus -extern "C" { -#endif - -CU_EXPORT void CU_automated_run_tests(void); -/**< - * Runs CUnit tests using the automated interface. - * This function sets appropriate callback functions, initializes the - * test output files, and calls the appropriate functions to list the - * tests and run them. If an output file name root has not been - * specified using CU_set_output_filename(), a generic root will be - * applied. It is an error to call this function before the CUnit - * test registry has been initialized (check by assertion). - */ - -CU_EXPORT CU_ErrorCode CU_list_tests_to_file(void); -/**< - * Generates an xml file containing a list of all tests in all suites - * in the active registry. The output file will be named according to - * the most recent call to CU_set_output_filename(), or a default if - * not previously set. - * - * @return An error code indicating the error status. - */ - -CU_EXPORT void CU_set_output_filename(const char* szFilenameRoot); -/**< - * Sets the root file name for automated test output files. - * The strings "-Listing.xml" and "-Results.xml" are appended to the - * specified root to generate the filenames. If szFilenameRoot is - * empty, the default root ("CUnitAutomated") is used. - * - * @param szFilenameRoot String containing root to use for file names. - */ - -#ifdef USE_DEPRECATED_CUNIT_NAMES -/** Deprecated (version 1). @deprecated Use CU_automated_run_tests(). */ -#define automated_run_tests() CU_automated_run_tests() -/** Deprecated (version 1). @deprecated Use CU_set_output_filename(). */ -#define set_output_filename(x) CU_set_output_filename((x)) -#endif /* USE_DEPRECATED_CUNIT_NAMES */ - -#ifdef __cplusplus -} -#endif -#endif /* CUNIT_AUTOMATED_H_SEEN */ -/** @} */ diff --git a/gearsyncd/tests/cunit/Basic.c b/gearsyncd/tests/cunit/Basic.c deleted file mode 100644 index 181f3b9ef7..0000000000 --- a/gearsyncd/tests/cunit/Basic.c +++ /dev/null @@ -1,334 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2004-2006 Jerry St.Clair, Anil Kumar - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation for basic test runner interface. - * - * 11-Aug-2004 Initial implementation of basic test runner interface. (JDS) - * - * 8-Jan-2005 Fixed reporting bug (bug report cunit-Bugs-1093861). (JDS) - * - * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Basic interface with output to stdout. - */ -/** @addtogroup Basic - * @{ - */ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "Util.h" -#include "TestRun.h" -#include "Basic.h" -#include "CUnit_intl.h" - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -/** Pointer to the currently running suite. */ -CU_pSuite f_pRunningSuite = NULL; -/** Current run mode. */ -static CU_BasicRunMode f_run_mode = CU_BRM_NORMAL; - -/*================================================================= - * Forward declaration of module functions * - *=================================================================*/ -CU_ErrorCode basic_initialize(void); -static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry); -static CU_ErrorCode basic_run_suite(CU_pSuite pSuite); -static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest); - -static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); -static void basic_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailureList); -static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); -static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite); -static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -CU_ErrorCode CU_basic_run_tests(void) -{ - CU_ErrorCode error; - - if (NULL == CU_get_registry()) { - if (CU_BRM_SILENT != f_run_mode) - fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); - error = CUE_NOREGISTRY; - } - else if (CUE_SUCCESS == (error = basic_initialize())) - error = basic_run_all_tests(NULL); - - return error; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_basic_run_suite(CU_pSuite pSuite) -{ - CU_ErrorCode error; - - if (NULL == pSuite) - error = CUE_NOSUITE; - else if (CUE_SUCCESS == (error = basic_initialize())) - error = basic_run_suite(pSuite); - - return error; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest) -{ - CU_ErrorCode error; - - if (NULL == pSuite) - error = CUE_NOSUITE; - else if (NULL == pTest) - error = CUE_NOTEST; - else if (CUE_SUCCESS == (error = basic_initialize())) - error = basic_run_single_test(pSuite, pTest); - - return error; -} - -/*------------------------------------------------------------------------*/ -void CU_basic_set_mode(CU_BasicRunMode mode) -{ - f_run_mode = mode; -} - -/*------------------------------------------------------------------------*/ -CU_BasicRunMode CU_basic_get_mode(void) -{ - return f_run_mode; -} - -/*------------------------------------------------------------------------*/ -void CU_basic_show_failures(CU_pFailureRecord pFailure) -{ - int i; - - for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { - fprintf(stdout, "\n %d. %s:%u - %s", i, - (NULL != pFailure->strFileName) ? pFailure->strFileName : "", - pFailure->uiLineNumber, - (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); - } -} - -/*================================================================= - * Static module functions - *=================================================================*/ -/** Performs inialization actions for the basic interface. - * This includes setting output to unbuffered, printing a - * welcome message, and setting the test run handlers. - * @return An error code indicating the framework error condition. - */ -CU_ErrorCode basic_initialize(void) -{ - /* Unbuffered output so everything reaches the screen */ - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - - CU_set_error(CUE_SUCCESS); - - if (CU_BRM_SILENT != f_run_mode) - fprintf(stdout, "\n\n %s" CU_VERSION - "\n %s\n\n", - _("CUnit - A unit testing framework for C - Version "), - _("http://cunit.sourceforge.net/")); - - CU_set_test_start_handler(basic_test_start_message_handler); - CU_set_test_complete_handler(basic_test_complete_message_handler); - CU_set_all_test_complete_handler(basic_all_tests_complete_message_handler); - CU_set_suite_init_failure_handler(basic_suite_init_failure_message_handler); - CU_set_suite_cleanup_failure_handler(basic_suite_cleanup_failure_message_handler); - - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -/** Runs all tests within the basic interface. - * If non-NULL, the test registry is changed to the specified registry - * before running the tests, and reset to the original registry when - * done. If NULL, the default CUnit test registry will be used. - * @param pRegistry The CU_pTestRegistry containing the tests - * to be run. If NULL, use the default registry. - * @return An error code indicating the error status - * during the test run. - */ -static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = NULL; - CU_ErrorCode result; - - f_pRunningSuite = NULL; - - if (NULL != pRegistry) - pOldRegistry = CU_set_registry(pRegistry); - result = CU_run_all_tests(); - if (NULL != pRegistry) - CU_set_registry(pOldRegistry); - return result; -} - -/*------------------------------------------------------------------------*/ -/** Runs a specified suite within the basic interface. - * @param pSuite The suite to be run (non-NULL). - * @return An error code indicating the error status - * during the test run. - */ -static CU_ErrorCode basic_run_suite(CU_pSuite pSuite) -{ - f_pRunningSuite = NULL; - return CU_run_suite(pSuite); -} - -/*------------------------------------------------------------------------*/ -/** Runs a single test for the specified suite within - * the console interface. - * @param pSuite The suite containing the test to be run (non-NULL). - * @param pTest The test to be run (non-NULL). - * @return An error code indicating the error status - * during the test run. - */ -static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest) -{ - f_pRunningSuite = NULL; - return CU_run_test(pSuite, pTest); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at start of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - */ -static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pTest); - - if (CU_BRM_VERBOSE == f_run_mode) { - assert(NULL != pTest->pName); - if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { - assert(NULL != pSuite->pName); - fprintf(stdout, "\n%s: %s", _("Suite"), pSuite->pName); - fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); - f_pRunningSuite = pSuite; - } - else { - fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); - } - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - * @param pFailure Pointer to the 1st failure record for this test. - */ -static void basic_test_complete_message_handler(const CU_pTest pTest, - const CU_pSuite pSuite, - const CU_pFailureRecord pFailureList) -{ - CU_pFailureRecord pFailure = pFailureList; - int i; - - assert(NULL != pSuite); - assert(NULL != pTest); - - if (NULL == pFailure) { - if (CU_BRM_VERBOSE == f_run_mode) { - fprintf(stdout, _("passed")); - } - } - else { - switch (f_run_mode) { - case CU_BRM_VERBOSE: - fprintf(stdout, _("FAILED")); - break; - case CU_BRM_NORMAL: - assert(NULL != pSuite->pName); - assert(NULL != pTest->pName); - fprintf(stdout, _("\nSuite %s, Test %s had failures:"), pSuite->pName, pTest->pName); - break; - default: /* gcc wants all enums covered. ok. */ - break; - } - if (CU_BRM_SILENT != f_run_mode) { - for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { - fprintf(stdout, "\n %d. %s:%u - %s", i, - (NULL != pFailure->strFileName) ? pFailure->strFileName : "", - pFailure->uiLineNumber, - (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); - } - } - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of all tests in a suite. - * @param pFailure Pointer to the test failure record list. - */ -static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) -{ - CU_UNREFERENCED_PARAMETER(pFailure); /* not used in basic interface */ - printf("\n\n"); - CU_print_run_results(stdout); - printf("\n"); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite initialization fails. - * @param pSuite The suite for which initialization failed. - */ -static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - if (CU_BRM_SILENT != f_run_mode) - fprintf(stdout, _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite cleanup fails. - * @param pSuite The suite for which cleanup failed. - */ -static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - if (CU_BRM_SILENT != f_run_mode) - fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); -} - -/** @} */ diff --git a/gearsyncd/tests/cunit/Basic.cpp b/gearsyncd/tests/cunit/Basic.cpp deleted file mode 100644 index 181f3b9ef7..0000000000 --- a/gearsyncd/tests/cunit/Basic.cpp +++ /dev/null @@ -1,334 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2004-2006 Jerry St.Clair, Anil Kumar - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation for basic test runner interface. - * - * 11-Aug-2004 Initial implementation of basic test runner interface. (JDS) - * - * 8-Jan-2005 Fixed reporting bug (bug report cunit-Bugs-1093861). (JDS) - * - * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Basic interface with output to stdout. - */ -/** @addtogroup Basic - * @{ - */ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "Util.h" -#include "TestRun.h" -#include "Basic.h" -#include "CUnit_intl.h" - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -/** Pointer to the currently running suite. */ -CU_pSuite f_pRunningSuite = NULL; -/** Current run mode. */ -static CU_BasicRunMode f_run_mode = CU_BRM_NORMAL; - -/*================================================================= - * Forward declaration of module functions * - *=================================================================*/ -CU_ErrorCode basic_initialize(void); -static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry); -static CU_ErrorCode basic_run_suite(CU_pSuite pSuite); -static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest); - -static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); -static void basic_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailureList); -static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); -static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite); -static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -CU_ErrorCode CU_basic_run_tests(void) -{ - CU_ErrorCode error; - - if (NULL == CU_get_registry()) { - if (CU_BRM_SILENT != f_run_mode) - fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); - error = CUE_NOREGISTRY; - } - else if (CUE_SUCCESS == (error = basic_initialize())) - error = basic_run_all_tests(NULL); - - return error; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_basic_run_suite(CU_pSuite pSuite) -{ - CU_ErrorCode error; - - if (NULL == pSuite) - error = CUE_NOSUITE; - else if (CUE_SUCCESS == (error = basic_initialize())) - error = basic_run_suite(pSuite); - - return error; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest) -{ - CU_ErrorCode error; - - if (NULL == pSuite) - error = CUE_NOSUITE; - else if (NULL == pTest) - error = CUE_NOTEST; - else if (CUE_SUCCESS == (error = basic_initialize())) - error = basic_run_single_test(pSuite, pTest); - - return error; -} - -/*------------------------------------------------------------------------*/ -void CU_basic_set_mode(CU_BasicRunMode mode) -{ - f_run_mode = mode; -} - -/*------------------------------------------------------------------------*/ -CU_BasicRunMode CU_basic_get_mode(void) -{ - return f_run_mode; -} - -/*------------------------------------------------------------------------*/ -void CU_basic_show_failures(CU_pFailureRecord pFailure) -{ - int i; - - for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { - fprintf(stdout, "\n %d. %s:%u - %s", i, - (NULL != pFailure->strFileName) ? pFailure->strFileName : "", - pFailure->uiLineNumber, - (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); - } -} - -/*================================================================= - * Static module functions - *=================================================================*/ -/** Performs inialization actions for the basic interface. - * This includes setting output to unbuffered, printing a - * welcome message, and setting the test run handlers. - * @return An error code indicating the framework error condition. - */ -CU_ErrorCode basic_initialize(void) -{ - /* Unbuffered output so everything reaches the screen */ - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - - CU_set_error(CUE_SUCCESS); - - if (CU_BRM_SILENT != f_run_mode) - fprintf(stdout, "\n\n %s" CU_VERSION - "\n %s\n\n", - _("CUnit - A unit testing framework for C - Version "), - _("http://cunit.sourceforge.net/")); - - CU_set_test_start_handler(basic_test_start_message_handler); - CU_set_test_complete_handler(basic_test_complete_message_handler); - CU_set_all_test_complete_handler(basic_all_tests_complete_message_handler); - CU_set_suite_init_failure_handler(basic_suite_init_failure_message_handler); - CU_set_suite_cleanup_failure_handler(basic_suite_cleanup_failure_message_handler); - - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -/** Runs all tests within the basic interface. - * If non-NULL, the test registry is changed to the specified registry - * before running the tests, and reset to the original registry when - * done. If NULL, the default CUnit test registry will be used. - * @param pRegistry The CU_pTestRegistry containing the tests - * to be run. If NULL, use the default registry. - * @return An error code indicating the error status - * during the test run. - */ -static CU_ErrorCode basic_run_all_tests(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = NULL; - CU_ErrorCode result; - - f_pRunningSuite = NULL; - - if (NULL != pRegistry) - pOldRegistry = CU_set_registry(pRegistry); - result = CU_run_all_tests(); - if (NULL != pRegistry) - CU_set_registry(pOldRegistry); - return result; -} - -/*------------------------------------------------------------------------*/ -/** Runs a specified suite within the basic interface. - * @param pSuite The suite to be run (non-NULL). - * @return An error code indicating the error status - * during the test run. - */ -static CU_ErrorCode basic_run_suite(CU_pSuite pSuite) -{ - f_pRunningSuite = NULL; - return CU_run_suite(pSuite); -} - -/*------------------------------------------------------------------------*/ -/** Runs a single test for the specified suite within - * the console interface. - * @param pSuite The suite containing the test to be run (non-NULL). - * @param pTest The test to be run (non-NULL). - * @return An error code indicating the error status - * during the test run. - */ -static CU_ErrorCode basic_run_single_test(CU_pSuite pSuite, CU_pTest pTest) -{ - f_pRunningSuite = NULL; - return CU_run_test(pSuite, pTest); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at start of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - */ -static void basic_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pTest); - - if (CU_BRM_VERBOSE == f_run_mode) { - assert(NULL != pTest->pName); - if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { - assert(NULL != pSuite->pName); - fprintf(stdout, "\n%s: %s", _("Suite"), pSuite->pName); - fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); - f_pRunningSuite = pSuite; - } - else { - fprintf(stdout, "\n %s: %s ...", _("Test"), pTest->pName); - } - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - * @param pFailure Pointer to the 1st failure record for this test. - */ -static void basic_test_complete_message_handler(const CU_pTest pTest, - const CU_pSuite pSuite, - const CU_pFailureRecord pFailureList) -{ - CU_pFailureRecord pFailure = pFailureList; - int i; - - assert(NULL != pSuite); - assert(NULL != pTest); - - if (NULL == pFailure) { - if (CU_BRM_VERBOSE == f_run_mode) { - fprintf(stdout, _("passed")); - } - } - else { - switch (f_run_mode) { - case CU_BRM_VERBOSE: - fprintf(stdout, _("FAILED")); - break; - case CU_BRM_NORMAL: - assert(NULL != pSuite->pName); - assert(NULL != pTest->pName); - fprintf(stdout, _("\nSuite %s, Test %s had failures:"), pSuite->pName, pTest->pName); - break; - default: /* gcc wants all enums covered. ok. */ - break; - } - if (CU_BRM_SILENT != f_run_mode) { - for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { - fprintf(stdout, "\n %d. %s:%u - %s", i, - (NULL != pFailure->strFileName) ? pFailure->strFileName : "", - pFailure->uiLineNumber, - (NULL != pFailure->strCondition) ? pFailure->strCondition : ""); - } - } - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of all tests in a suite. - * @param pFailure Pointer to the test failure record list. - */ -static void basic_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) -{ - CU_UNREFERENCED_PARAMETER(pFailure); /* not used in basic interface */ - printf("\n\n"); - CU_print_run_results(stdout); - printf("\n"); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite initialization fails. - * @param pSuite The suite for which initialization failed. - */ -static void basic_suite_init_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - if (CU_BRM_SILENT != f_run_mode) - fprintf(stdout, _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite cleanup fails. - * @param pSuite The suite for which cleanup failed. - */ -static void basic_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - if (CU_BRM_SILENT != f_run_mode) - fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); -} - -/** @} */ diff --git a/gearsyncd/tests/cunit/Basic.h b/gearsyncd/tests/cunit/Basic.h deleted file mode 100644 index d8a638de3e..0000000000 --- a/gearsyncd/tests/cunit/Basic.h +++ /dev/null @@ -1,113 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2004-2006 Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Interface for simple test runner. - * - * 11-Aug-2004 Initial implementation of basic test runner interface. (JDS) - */ - -/** @file - * Basic interface with output to stdout. - */ -/** @addtogroup Basic - * @{ - */ - -#ifndef CUNIT_BASIC_H_SEEN -#define CUNIT_BASIC_H_SEEN - -#include "CUnit.h" -#include "TestDB.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** Run modes for the basic interface. */ -typedef enum { - CU_BRM_NORMAL = 0, /**< Normal mode - failures and run summary are printed [default]. */ - CU_BRM_SILENT, /**< Silent mode - no output is printed except framework error messages. */ - CU_BRM_VERBOSE /**< Verbose mode - maximum output of run details. */ -} CU_BasicRunMode; - -CU_EXPORT CU_ErrorCode CU_basic_run_tests(void); -/**< - * Runs all registered CUnit tests using the basic interface. - * The default CU_BasicRunMode is used unless it has been - * previously changed using CU_basic_set_mode(). The CUnit test - * registry must have been initialized before calling this function. - * - * @return A CU_ErrorCode indicating the framework error condition, including - * CUE_NOREGISTRY - Registry has not been initialized. - */ - -CU_EXPORT CU_ErrorCode CU_basic_run_suite(CU_pSuite pSuite); -/**< - * Runs all tests for a specific suite in the basic interface. - * If pSuite is NULL, the function returns without taking any - * action. The default CU_BasicRunMode is used unless it has - * been changed using CU_basic_set_mode(). - * - * @param pSuite The CU_Suite to run. - * @return A CU_ErrorCode indicating the framework error condition, including - * CUE_NOSUITE - pSuite was NULL. - */ - -CU_EXPORT CU_ErrorCode CU_basic_run_test(CU_pSuite pSuite, CU_pTest pTest); -/**< - * Runs a single test in a specific suite in the basic interface. - * If pSuite or pTest is NULL, the function returns without - * taking any action. The default CU_BasicRunMode is used unless - * it has been changed using CU_basic_set_mode. - * - * @param pSuite The CU_Suite holding the CU_Test to run. - * @param pTest The CU_Test to run. - * @return A CU_ErrorCode indicating the framework error condition, including - * CUE_NOSUITE - pSuite was NULL. - * CUE_NOTEST - pTest was NULL. - */ - -CU_EXPORT void CU_basic_set_mode(CU_BasicRunMode mode); -/**< Sets the run mode for the basic interface. - * @param mode The new CU_BasicRunMode for subsequent test - * runs using the basic interface. - */ - -CU_EXPORT CU_BasicRunMode CU_basic_get_mode(void); -/**< Retrieves the current run mode for the basic interface. - * @return The current CU_BasicRunMode setting for test - * runs using the basic interface. - */ - -CU_EXPORT void CU_basic_show_failures(CU_pFailureRecord pFailure); -/**< - * Prints a summary of run failures to stdout. - * This is provided for user convenience upon request, and does - * not take into account the current run mode. The failures are - * printed to stdout independent of the most recent run mode. - * - * @param pFailure List of CU_pFailureRecord's to output. - */ - -#ifdef __cplusplus -} -#endif -#endif /* CUNIT_BASIC_H_SEEN */ -/** @} */ diff --git a/gearsyncd/tests/cunit/CUError.c b/gearsyncd/tests/cunit/CUError.c deleted file mode 100644 index 41f9bc3207..0000000000 --- a/gearsyncd/tests/cunit/CUError.c +++ /dev/null @@ -1,231 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Error handling code used by CUnit - * - * 16-Jul-2004 Created access functions for error code, error action - * functions, messages for new error codes. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Error handling functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include - -#include "CUnit_intl.h" -#include "CUError.h" - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -/** Local variable holding the current error code. */ -static CU_ErrorCode g_error_number = CUE_SUCCESS; -/** Local variable holding the current error action code. */ -static CU_ErrorAction g_error_action = CUEA_IGNORE; - -/*================================================================= - * Private function forward declarations - *=================================================================*/ -static const char* get_error_desc(CU_ErrorCode error); - -#ifdef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS -void test_exit(int status); -#endif - -/*================================================================= - * Public API functions - *=================================================================*/ -void CU_set_error(CU_ErrorCode error) -{ - if ((error != CUE_SUCCESS) && (g_error_action == CUEA_ABORT)) { -#ifndef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS - fprintf(stderr, _("\nAborting due to error #%d: %s\n"), - (int)error, - get_error_desc(error)); - exit((int)error); -#else - test_exit(error); -#endif - } - - g_error_number = error; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_get_error(void) -{ - return g_error_number; -} - -/*------------------------------------------------------------------------*/ -const char* CU_get_error_msg(void) -{ - return get_error_desc(g_error_number); -} - -/*------------------------------------------------------------------------*/ -void CU_set_error_action(CU_ErrorAction action) -{ - g_error_action = action; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorAction CU_get_error_action(void) -{ - return g_error_action; -} - -/*================================================================= - * Private static function definitions - *=================================================================*/ -/** Internal function to look up the error message for a specified - * error code. An empty string is returned if iError is not a member - * of CU_ErrorCode. If you add an error code to enum CU_ErrorCode, - * be sure to add a corresponding error message here. - * - * @param iError CU_ErrorCode to look up. - * @return Pointer to a string containing the error message. - * @see CU_get_error_msg() - */ -static const char* get_error_desc(CU_ErrorCode iError) -{ - int iMaxIndex; - - static const char* ErrorDescription[] = { - N_("No Error."), /* CUE_SUCCESS - 0 */ - N_("Memory allocation failed."), /* CUE_NOMEMORY - 1 */ - "", - "", - "", - "", - "", - "", - "", - "", - N_("Test registry does not exist."), /* CUE_NOREGISTRY - 10 */ - N_("Registry already exists."), /* CUE_REGISTRY_EXISTS - 11 */ - "", - "", - "", - "", - "", - "", - "", - "", - N_("NULL suite not allowed."), /* CUE_NOSUITE - 20 */ - N_("Suite name cannot be NULL."), /* CUE_NO_SUITENAME - 21 */ - N_("Suite initialization function failed."), /* CUE_SINIT_FAILED - 22 */ - N_("Suite cleanup function failed."), /* CUE_SCLEAN_FAILED - 23 */ - N_("Suite having name already registered."), /* CUE_DUP_SUITE - 24 */ - N_("Requested suite is not active."), /* CUE_SUITE_INACTIVE - 25 */ - "", - "", - "", - "", - N_("NULL test or test function not allowed."),/* CUE_NOTEST - 30 */ - N_("Test name cannot be NULL."), /* CUE_NO_TESTNAME - 31 */ - N_("Test having this name already in suite."),/* CUE_DUP_TEST - 32 */ - N_("Test not registered in specified suite."),/* CUE_TEST_NOT_IN_SUITE - 33 */ - N_("Requested test is not active"), /* CUE_TEST_INACTIVE - 34 */ - "", - "", - "", - "", - "", - N_("Error opening file."), /* CUE_FOPEN_FAILED - 40 */ - N_("Error closing file."), /* CUE_FCLOSE_FAILED - 41 */ - N_("Bad file name."), /* CUE_BAD_FILENAME - 42 */ - N_("Error during write to file."), /* CUE_WRITE_ERROR - 43 */ - N_("Undefined Error") - }; - - iMaxIndex = (int)(sizeof(ErrorDescription)/sizeof(char *) - 1); - if ((int)iError < 0) { - return _(ErrorDescription[0]); - } - else if ((int)iError > iMaxIndex) { - return _(ErrorDescription[iMaxIndex]); - } - else { - return _(ErrorDescription[(int)iError]); - } -} - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -void test_cunit_CUError(void) -{ - CU_ErrorCode old_err = CU_get_error(); - CU_ErrorAction old_action = CU_get_error_action(); - - test_cunit_start_tests("CUError.c"); - - /* CU_set_error() & CU_get_error() */ - CU_set_error(CUE_NOMEMORY); - TEST(CU_get_error() != CUE_SUCCESS); - TEST(CU_get_error() == CUE_NOMEMORY); - - CU_set_error(CUE_NOREGISTRY); - TEST(CU_get_error() != CUE_SUCCESS); - TEST(CU_get_error() == CUE_NOREGISTRY); - - /* CU_get_error_msg() */ - CU_set_error(CUE_SUCCESS); - TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_SUCCESS))); - - CU_set_error(CUE_NOTEST); - TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOTEST))); - - CU_set_error(CUE_NOMEMORY); - TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOMEMORY))); - TEST(strcmp(CU_get_error_msg(), get_error_desc(CUE_SCLEAN_FAILED))); - - TEST(!strcmp(get_error_desc(100), "Undefined Error")); - - /* CU_set_error_action() & CU_get_error_action() */ - CU_set_error_action(CUEA_FAIL); - TEST(CU_get_error_action() != CUEA_IGNORE); - TEST(CU_get_error_action() == CUEA_FAIL); - TEST(CU_get_error_action() != CUEA_ABORT); - - CU_set_error_action(CUEA_ABORT); - TEST(CU_get_error_action() != CUEA_IGNORE); - TEST(CU_get_error_action() != CUEA_FAIL); - TEST(CU_get_error_action() == CUEA_ABORT); - - /* reset values */ - CU_set_error(old_err); - CU_set_error_action(old_action); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/CUError.cpp b/gearsyncd/tests/cunit/CUError.cpp deleted file mode 100644 index 41f9bc3207..0000000000 --- a/gearsyncd/tests/cunit/CUError.cpp +++ /dev/null @@ -1,231 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Error handling code used by CUnit - * - * 16-Jul-2004 Created access functions for error code, error action - * functions, messages for new error codes. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Error handling functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include - -#include "CUnit_intl.h" -#include "CUError.h" - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -/** Local variable holding the current error code. */ -static CU_ErrorCode g_error_number = CUE_SUCCESS; -/** Local variable holding the current error action code. */ -static CU_ErrorAction g_error_action = CUEA_IGNORE; - -/*================================================================= - * Private function forward declarations - *=================================================================*/ -static const char* get_error_desc(CU_ErrorCode error); - -#ifdef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS -void test_exit(int status); -#endif - -/*================================================================= - * Public API functions - *=================================================================*/ -void CU_set_error(CU_ErrorCode error) -{ - if ((error != CUE_SUCCESS) && (g_error_action == CUEA_ABORT)) { -#ifndef CUNIT_DO_NOT_DEFINE_UNLESS_BUILDING_TESTS - fprintf(stderr, _("\nAborting due to error #%d: %s\n"), - (int)error, - get_error_desc(error)); - exit((int)error); -#else - test_exit(error); -#endif - } - - g_error_number = error; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_get_error(void) -{ - return g_error_number; -} - -/*------------------------------------------------------------------------*/ -const char* CU_get_error_msg(void) -{ - return get_error_desc(g_error_number); -} - -/*------------------------------------------------------------------------*/ -void CU_set_error_action(CU_ErrorAction action) -{ - g_error_action = action; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorAction CU_get_error_action(void) -{ - return g_error_action; -} - -/*================================================================= - * Private static function definitions - *=================================================================*/ -/** Internal function to look up the error message for a specified - * error code. An empty string is returned if iError is not a member - * of CU_ErrorCode. If you add an error code to enum CU_ErrorCode, - * be sure to add a corresponding error message here. - * - * @param iError CU_ErrorCode to look up. - * @return Pointer to a string containing the error message. - * @see CU_get_error_msg() - */ -static const char* get_error_desc(CU_ErrorCode iError) -{ - int iMaxIndex; - - static const char* ErrorDescription[] = { - N_("No Error."), /* CUE_SUCCESS - 0 */ - N_("Memory allocation failed."), /* CUE_NOMEMORY - 1 */ - "", - "", - "", - "", - "", - "", - "", - "", - N_("Test registry does not exist."), /* CUE_NOREGISTRY - 10 */ - N_("Registry already exists."), /* CUE_REGISTRY_EXISTS - 11 */ - "", - "", - "", - "", - "", - "", - "", - "", - N_("NULL suite not allowed."), /* CUE_NOSUITE - 20 */ - N_("Suite name cannot be NULL."), /* CUE_NO_SUITENAME - 21 */ - N_("Suite initialization function failed."), /* CUE_SINIT_FAILED - 22 */ - N_("Suite cleanup function failed."), /* CUE_SCLEAN_FAILED - 23 */ - N_("Suite having name already registered."), /* CUE_DUP_SUITE - 24 */ - N_("Requested suite is not active."), /* CUE_SUITE_INACTIVE - 25 */ - "", - "", - "", - "", - N_("NULL test or test function not allowed."),/* CUE_NOTEST - 30 */ - N_("Test name cannot be NULL."), /* CUE_NO_TESTNAME - 31 */ - N_("Test having this name already in suite."),/* CUE_DUP_TEST - 32 */ - N_("Test not registered in specified suite."),/* CUE_TEST_NOT_IN_SUITE - 33 */ - N_("Requested test is not active"), /* CUE_TEST_INACTIVE - 34 */ - "", - "", - "", - "", - "", - N_("Error opening file."), /* CUE_FOPEN_FAILED - 40 */ - N_("Error closing file."), /* CUE_FCLOSE_FAILED - 41 */ - N_("Bad file name."), /* CUE_BAD_FILENAME - 42 */ - N_("Error during write to file."), /* CUE_WRITE_ERROR - 43 */ - N_("Undefined Error") - }; - - iMaxIndex = (int)(sizeof(ErrorDescription)/sizeof(char *) - 1); - if ((int)iError < 0) { - return _(ErrorDescription[0]); - } - else if ((int)iError > iMaxIndex) { - return _(ErrorDescription[iMaxIndex]); - } - else { - return _(ErrorDescription[(int)iError]); - } -} - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -void test_cunit_CUError(void) -{ - CU_ErrorCode old_err = CU_get_error(); - CU_ErrorAction old_action = CU_get_error_action(); - - test_cunit_start_tests("CUError.c"); - - /* CU_set_error() & CU_get_error() */ - CU_set_error(CUE_NOMEMORY); - TEST(CU_get_error() != CUE_SUCCESS); - TEST(CU_get_error() == CUE_NOMEMORY); - - CU_set_error(CUE_NOREGISTRY); - TEST(CU_get_error() != CUE_SUCCESS); - TEST(CU_get_error() == CUE_NOREGISTRY); - - /* CU_get_error_msg() */ - CU_set_error(CUE_SUCCESS); - TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_SUCCESS))); - - CU_set_error(CUE_NOTEST); - TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOTEST))); - - CU_set_error(CUE_NOMEMORY); - TEST(!strcmp(CU_get_error_msg(), get_error_desc(CUE_NOMEMORY))); - TEST(strcmp(CU_get_error_msg(), get_error_desc(CUE_SCLEAN_FAILED))); - - TEST(!strcmp(get_error_desc(100), "Undefined Error")); - - /* CU_set_error_action() & CU_get_error_action() */ - CU_set_error_action(CUEA_FAIL); - TEST(CU_get_error_action() != CUEA_IGNORE); - TEST(CU_get_error_action() == CUEA_FAIL); - TEST(CU_get_error_action() != CUEA_ABORT); - - CU_set_error_action(CUEA_ABORT); - TEST(CU_get_error_action() != CUEA_IGNORE); - TEST(CU_get_error_action() != CUEA_FAIL); - TEST(CU_get_error_action() == CUEA_ABORT); - - /* reset values */ - CU_set_error(old_err); - CU_set_error_action(old_action); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/CUError.h b/gearsyncd/tests/cunit/CUError.h deleted file mode 100644 index c9943c1245..0000000000 --- a/gearsyncd/tests/cunit/CUError.h +++ /dev/null @@ -1,199 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Contains CUnit error codes which can be used externally. - * - * Aug 2001 Initial implementation. (AK) - * - * 02/Oct/2001 Added proper Eror Codes. (AK) - * - * 13-Oct-2001 Added Error Codes for Duplicate TestGroup and Test. (AK) - * - * 03-Aug-2004 Converted error code macros to an enum, doxygen comments, moved - * error handing code here, changed file name from Errno.h, added - * error codes for file open errors, added error action selection. (JDS) - * - * 05-Sep-2004 Added internal test interface. (JDS) - */ - -/** @file - * Error handling functions (user interface). - * CUnit uses a simple (and conventional) error handling strategy. - * Functions that can generate errors set (and usually return) an - * error code to indicate the run status. The error code can be - * inspected using the CU_get_error() function. A descriptive - * error message can be retrieved using CU_get_error_msg(). - */ -/** @addtogroup Framework - * @{ - */ - -#ifndef CUNIT_CUERROR_H_SEEN -#define CUNIT_CUERROR_H_SEEN - -#include - -/*------------------------------------------------------------------------*/ -/** CUnit error codes. - * If codes are added or removed, be sure to make a change to the - * error messages in CUError.c/get_error_desc(). - * @see CU_set_error() - * @see CU_get_error() - * @see CU_get_error_msg() - */ -typedef enum { - /* basic errors */ - CUE_SUCCESS = 0, /**< No error condition. */ - CUE_NOMEMORY = 1, /**< Memory allocation failed. */ - - /* Test Registry Level Errors */ - CUE_NOREGISTRY = 10, /**< Test registry not initialized. */ - CUE_REGISTRY_EXISTS = 11, /**< Attempt to CU_set_registry() without CU_cleanup_registry(). */ - - /* Test Suite Level Errors */ - CUE_NOSUITE = 20, /**< A required CU_pSuite pointer was NULL. */ - CUE_NO_SUITENAME = 21, /**< Required CU_Suite name not provided. */ - CUE_SINIT_FAILED = 22, /**< Suite initialization failed. */ - CUE_SCLEAN_FAILED = 23, /**< Suite cleanup failed. */ - CUE_DUP_SUITE = 24, /**< Duplicate suite name not allowed. */ - CUE_SUITE_INACTIVE = 25, /**< Test run initiated for an inactive suite. */ - - /* Test Case Level Errors */ - CUE_NOTEST = 30, /**< A required CU_pTest or CU_TestFunc pointer was NULL. */ - CUE_NO_TESTNAME = 31, /**< Required CU_Test name not provided. */ - CUE_DUP_TEST = 32, /**< Duplicate test case name not allowed. */ - CUE_TEST_NOT_IN_SUITE = 33, /**< Test not registered in specified suite. */ - CUE_TEST_INACTIVE = 34, /**< Test run initiated for an inactive test. */ - - /* File handling errors */ - CUE_FOPEN_FAILED = 40, /**< An error occurred opening a file. */ - CUE_FCLOSE_FAILED = 41, /**< An error occurred closing a file. */ - CUE_BAD_FILENAME = 42, /**< A bad filename was requested (NULL, empty, nonexistent, etc.). */ - CUE_WRITE_ERROR = 43 /**< An error occurred during a write to a file. */ -} CU_ErrorCode; - -/*------------------------------------------------------------------------*/ -/** CUnit error action codes. - * These are used to set the action desired when an error - * condition is detected in the CUnit framework. - * @see CU_set_error_action() - * @see CU_get_error_action() - */ -typedef enum CU_ErrorAction { - CUEA_IGNORE, /**< Runs should be continued when an error condition occurs (if possible). */ - CUEA_FAIL, /**< Runs should be stopped when an error condition occurs. */ - CUEA_ABORT /**< The application should exit() when an error conditions occurs. */ -} CU_ErrorAction; - -/* Error handling & reporting functions. */ - -#include "CUnit.h" - -#ifdef __cplusplus -extern "C" { -#endif - -CU_EXPORT CU_ErrorCode CU_get_error(void); -/**< - * Retrieves the current CUnit framework error code. - * CUnit implementation functions set the error code to indicate the - * status of the most recent operation. In general, the CUnit functions - * will clear the code to CUE_SUCCESS, then reset it to a specific error - * code if an exception condition is encountered. Some functions - * return the code, others leave it to the user to inspect if desired. - * - * @return The current error condition code. - * @see CU_get_error_msg() - * @see CU_ErrorCode - */ - -CU_EXPORT const char* CU_get_error_msg(void); -/**< - * Retrieves a message corresponding to the current framework error code. - * CUnit implementation functions set the error code to indicate the - * of the most recent operation. In general, the CUnit functions will - * clear the code to CUE_SUCCESS, then reset it to a specific error - * code if an exception condition is encountered. This function allows - * the user to retrieve a descriptive error message corresponding to the - * error code set by the last operation. - * - * @return A message corresponding to the current error condition. - * @see CU_get_error() - * @see CU_ErrorCode - */ - -CU_EXPORT void CU_set_error_action(CU_ErrorAction action); -/**< - * Sets the action to take when a framework error condition occurs. - * This function should be used to specify the action to take - * when an error condition is encountered. The default action is - * CUEA_IGNORE, which results in errors being ignored and test runs - * being continued (if possible). A value of CUEA_FAIL causes test - * runs to stop as soon as an error condition occurs, while - * CU_ABORT causes the application to exit on any error. - * - * @param action CU_ErrorAction indicating the new error action. - * @see CU_get_error_action() - * @see CU_set_error() - * @see CU_ErrorAction - */ - -CU_EXPORT CU_ErrorAction CU_get_error_action(void); -/**< - * Retrieves the current framework error action code. - * - * @return The current error action code. - * @see CU_set_error_action() - * @see CU_set_error() - * @see CU_ErrorAction - */ - -#ifdef CUNIT_BUILD_TESTS -void test_cunit_CUError(void); -#endif - -/* Internal function - users should not generally call this function */ -CU_EXPORT void CU_set_error(CU_ErrorCode error); -/**< - * Sets the CUnit framework error code. - * This function is used internally by CUnit implementation functions - * when an error condition occurs within the framework. It should - * not generally be called by user code. NOTE that if the current - * error action is CUEA_ABORT, then calling this function will - * result in exit() being called for the current application. - * - * @param error CU_ErrorCode indicating the current error condition. - * @see CU_get_error() - * @see CU_get_error_msg() - * @see CU_ErrorCode - */ - -#ifdef __cplusplus -} -#endif - -#ifdef USE_DEPRECATED_CUNIT_NAMES -/** Deprecated (version 1). @deprecated Use CU_get_error_msg(). */ -#define get_error() CU_get_error_msg() -#endif /* USE_DEPRECATED_CUNIT_NAMES */ - -#endif /* CUNIT_CUERROR_H_SEEN */ -/** @} */ diff --git a/gearsyncd/tests/cunit/CUnit.h b/gearsyncd/tests/cunit/CUnit.h deleted file mode 100644 index 9592eda50c..0000000000 --- a/gearsyncd/tests/cunit/CUnit.h +++ /dev/null @@ -1,383 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * ASSERT Macro definitions and general CUnit configuration definitions. - * - * 09/Aug/2001 ASSERT definitions. (AK) - * - * 12/Mar/2003 New Assert definitions. (AK) - * - * 27/Jul/2003 Modified ASSERT_XXX Macro definitions. (AK) - * - * 15-Jul-2004 New interface, changed action on assert failure to not - * return, provided _FATAL versions of assertions to return - * from test function on failure. (JDS) - * - * 01-Sep-2004 Modified assertions for setjmp/longjmp mechanism of - * aborting test runs, added CU_FAIL and CU_PASS macros. (JDS) - * - * 07-May-2005 Added CU_ prefix to remaining CUnit defines (BOOL, TRUE, - * FALSE, MAX_...). Added CU_UNREFERENCED_PARAMETER() define. (JDS) - */ - -/** @file - * Basic CUnit include file for user and system code. - * Defines macros for assertions for use in user test cases. - * Basic system macro definitions also appear here. - */ -/** @addtogroup Framework - * @{ - */ - -#ifndef CUNIT_CUNIT_H_SEEN -#define CUNIT_CUNIT_H_SEEN - -#include -#include - -/** CUnit version number. */ -#define CU_VERSION "2.1-2" - -/* Max string lengths for names (includes terminating NULL. */ -/** Maximum length of a test name string. */ -#define CU_MAX_TEST_NAME_LENGTH 256 -/** Maximim length of a suite name string. */ -#define CU_MAX_SUITE_NAME_LENGTH 256 - -/* Global type Definitions to be used for boolean operators. */ -#ifndef CU_BOOL - /** Boolean type for CUnit use. */ - #define CU_BOOL int -#endif - -#ifndef CU_TRUE - /** Boolean TRUE for CUnit use. */ - #define CU_TRUE 1 -#endif - -#ifndef CU_FALSE - /** Boolean FALSE for CUnit use. */ - #define CU_FALSE 0 -#endif - -#ifndef CU_UNREFERENCED_PARAMETER - /** Consistent approach to referencing unused parameters. */ - #define CU_UNREFERENCED_PARAMETER(x) (void)x -#endif - -#ifndef CU_MAX -# define CU_MAX(a,b) (((a) >= (b)) ? (a) : (b)) -#endif - -#ifndef CU_MIN -# define CU_MIN(a,b) (((a) >= (b)) ? (b) : (a)) -#endif - -#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) -# ifdef CU_DLL -# ifdef CU_BUILD_DLL -# define CU_EXPORT __declspec(dllexport) -# else -# define CU_EXPORT __declspec(dllimport) -# endif -# else -# define CU_EXPORT -# endif -# ifdef _MSC_VER -# define snprintf _snprintf -# endif -#else -# define CU_EXPORT -#endif /* WIN32 */ - -#include "CUError.h" -#include "TestDB.h" /* not needed here - included for user convenience */ -#include "TestRun.h" /* not needed here - include (after BOOL define) for user convenience */ - -/** Record a pass condition without performing a logical test. */ -#define CU_PASS(msg) \ - { CU_assertImplementation(CU_TRUE, __LINE__, ("CU_PASS(" #msg ")"), __FILE__, "", CU_FALSE); } - -/** Simple assertion. - * Reports failure with no other action. - */ -#define CU_ASSERT(value) \ - { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); } - -/** Simple assertion. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_FATAL(value) \ - { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); } - -/** Simple assertion. - * Reports failure with no other action. - */ -#define CU_TEST(value) \ - { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_FALSE); } - -/** Simple assertion. - * Reports failure and causes test to abort. - */ -#define CU_TEST_FATAL(value) \ - { CU_assertImplementation((value), __LINE__, #value, __FILE__, "", CU_TRUE); } - -/** Record a failure without performing a logical test. */ -#define CU_FAIL(msg) \ - { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL(" #msg ")"), __FILE__, "", CU_FALSE); } - -/** Record a failure without performing a logical test, and abort test. */ -#define CU_FAIL_FATAL(msg) \ - { CU_assertImplementation(CU_FALSE, __LINE__, ("CU_FAIL_FATAL(" #msg ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that value is CU_TRUE. - * Reports failure with no other action. - */ -#define CU_ASSERT_TRUE(value) \ - { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE(" #value ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that value is CU_TRUE. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_TRUE_FATAL(value) \ - { CU_assertImplementation((value), __LINE__, ("CU_ASSERT_TRUE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that value is CU_FALSE. - * Reports failure with no other action. - */ -#define CU_ASSERT_FALSE(value) \ - { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE(" #value ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that value is CU_FALSE. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_FALSE_FATAL(value) \ - { CU_assertImplementation(!(value), __LINE__, ("CU_ASSERT_FALSE_FATAL(" #value ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that actual == expected. - * Reports failure with no other action. - */ -#define CU_ASSERT_EQUAL(actual, expected) \ - { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that actual == expected. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_EQUAL_FATAL(actual, expected) \ - { CU_assertImplementation(((actual) == (expected)), __LINE__, ("CU_ASSERT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that actual != expected. - * Reports failure with no other action. - */ -#define CU_ASSERT_NOT_EQUAL(actual, expected) \ - { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that actual != expected. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_NOT_EQUAL_FATAL(actual, expected) \ - { CU_assertImplementation(((actual) != (expected)), __LINE__, ("CU_ASSERT_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that pointers actual == expected. - * Reports failure with no other action. - */ -#define CU_ASSERT_PTR_EQUAL(actual, expected) \ - { CU_assertImplementation(((void*)(actual) == (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that pointers actual == expected. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_PTR_EQUAL_FATAL(actual, expected) \ - { CU_assertImplementation(((void*)(actual) == (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that pointers actual != expected. - * Reports failure with no other action. - */ -#define CU_ASSERT_PTR_NOT_EQUAL(actual, expected) \ - { CU_assertImplementation(((void*)(actual) != (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that pointers actual != expected. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_PTR_NOT_EQUAL_FATAL(actual, expected) \ - { CU_assertImplementation(((void*)(actual) != (void*)(expected)), __LINE__, ("CU_ASSERT_PTR_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that pointer value is NULL. - * Reports failure with no other action. - */ -#define CU_ASSERT_PTR_NULL(value) \ - { CU_assertImplementation((NULL == (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL(" #value")"), __FILE__, "", CU_FALSE); } - -/** Asserts that pointer value is NULL. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_PTR_NULL_FATAL(value) \ - { CU_assertImplementation((NULL == (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); } - -/** Asserts that pointer value is not NULL. - * Reports failure with no other action. - */ -#define CU_ASSERT_PTR_NOT_NULL(value) \ - { CU_assertImplementation((NULL != (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", CU_FALSE); } - -/** Asserts that pointer value is not NULL. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_PTR_NOT_NULL_FATAL(value) \ - { CU_assertImplementation((NULL != (void*)(value)), __LINE__, ("CU_ASSERT_PTR_NOT_NULL_FATAL(" #value")"), __FILE__, "", CU_TRUE); } - -/** Asserts that string actual == expected. - * Reports failure with no other action. - */ -#define CU_ASSERT_STRING_EQUAL(actual, expected) \ - { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that string actual == expected. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_STRING_EQUAL_FATAL(actual, expected) \ - { CU_assertImplementation(!(strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that string actual != expected. - * Reports failure with no other action. - */ -#define CU_ASSERT_STRING_NOT_EQUAL(actual, expected) \ - { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that string actual != expected. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_STRING_NOT_EQUAL_FATAL(actual, expected) \ - { CU_assertImplementation((strcmp((const char*)(actual), (const char*)(expected))), __LINE__, ("CU_ASSERT_STRING_NOT_EQUAL_FATAL(" #actual "," #expected ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that string actual == expected with length specified. - * The comparison is limited to count characters. - * Reports failure with no other action. - */ -#define CU_ASSERT_NSTRING_EQUAL(actual, expected, count) \ - { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that string actual == expected with length specified. - * The comparison is limited to count characters. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_NSTRING_EQUAL_FATAL(actual, expected, count) \ - { CU_assertImplementation(!(strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_EQUAL_FATAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that string actual != expected with length specified. - * The comparison is limited to count characters. - * Reports failure with no other action. - */ -#define CU_ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) \ - { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that string actual != expected with length specified. - * The comparison is limited to count characters. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(actual, expected, count) \ - { CU_assertImplementation((strncmp((const char*)(actual), (const char*)(expected), (size_t)(count))), __LINE__, ("CU_ASSERT_NSTRING_NOT_EQUAL_FATAL(" #actual "," #expected "," #count ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that double actual == expected within the specified tolerance. - * If actual is within granularity of expected, the assertion passes. - * Reports failure with no other action. - */ -#define CU_ASSERT_DOUBLE_EQUAL(actual, expected, granularity) \ - { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that double actual == expected within the specified tolerance. - * If actual is within granularity of expected, the assertion passes. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_DOUBLE_EQUAL_FATAL(actual, expected, granularity) \ - { CU_assertImplementation(((fabs((double)(actual) - (expected)) <= fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE); } - -/** Asserts that double actual != expected within the specified tolerance. - * If actual is within granularity of expected, the assertion fails. - * Reports failure with no other action. - */ -#define CU_ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) \ - { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_FALSE); } - -/** Asserts that double actual != expected within the specified tolerance. - * If actual is within granularity of expected, the assertion fails. - * Reports failure and causes test to abort. - */ -#define CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(actual, expected, granularity) \ - { CU_assertImplementation(((fabs((double)(actual) - (expected)) > fabs((double)(granularity)))), __LINE__, ("CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", CU_TRUE); } - -#ifdef USE_DEPRECATED_CUNIT_NAMES - -#ifndef BOOL - /** Deprecated (version 2.0-2). @deprecated Use CU_BOOL. */ - #define BOOL int -#endif - -#ifndef TRUE - /** Deprecated (version 2.0-2). @deprecated Use CU_TRUE. */ - #define TRUE 1 -#endif - -#ifndef FALSE - /** Deprecated (version 2.0-2). @deprecated Use CU_FALSE. */ - #define FALSE 0 -#endif - -/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_TEST_NAME_LENGTH. */ -#define MAX_TEST_NAME_LENGTH 256 -/** Deprecated (version 2.0-2). @deprecated Use CU_MAX_SUITE_NAME_LENGTH. */ -#define MAX_SUITE_NAME_LENGTH 256 - -/** Deprecated (version 1). @deprecated Use CU_ASSERT_FATAL. */ -#define ASSERT(value) { if (FALSE == (int)(value)) { CU_assertImplementation((BOOL)value, __LINE__, #value, __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_TRUE_FATAL. */ -#define ASSERT_TRUE(value) { if (FALSE == (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_TRUE(" #value ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_FALSE_FATAL. */ -#define ASSERT_FALSE(value) { if (FALSE != (value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_FALSE(" #value ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_EQUAL_FATAL. */ -#define ASSERT_EQUAL(actual, expected) { if ((actual) != (expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_NOT_EQUAL_FATAL. */ -#define ASSERT_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_EQUAL_FATAL. */ -#define ASSERT_PTR_EQUAL(actual, expected) { if ((void*)(actual) != (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_EQUAL_FATAL. */ -#define ASSERT_PTR_NOT_EQUAL(actual, expected) { if ((void*)(actual) == (void*)(expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NULL_FATAL. */ -#define ASSERT_PTR_NULL(value) { if (NULL != (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NULL(" #value")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_PTR_NOT_NULL_FATAL. */ -#define ASSERT_PTR_NOT_NULL(value) { if (NULL == (void*)(value)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_PTR_NOT_NULL(" #value")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_EQUAL_FATAL. */ -#define ASSERT_STRING_EQUAL(actual, expected) { if (strcmp((const char*)actual, (const char*)expected)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_STRING_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_STRING_NOT_EQUAL_FATAL. */ -#define ASSERT_STRING_NOT_EQUAL(actual, expected) { if (!strcmp((const char*)actual, (const char*)expected)) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_STRING_NOT_EQUAL(" #actual "," #expected ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_EQUAL_FATAL. */ -#define ASSERT_NSTRING_EQUAL(actual, expected, count) { if (strncmp((const char*)actual, (const char*)expected, (size_t)count)) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_NSTRING_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_NSTRING_NOT_EQUAL_FATAL. */ -#define ASSERT_NSTRING_NOT_EQUAL(actual, expected, count) { if (!strncmp((const char*)actual, (const char*)expected, (size_t)count)) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_NSTRING_NOT_EQUAL(" #actual "," #expected "," #count ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_EQUAL_FATAL. */ -#define ASSERT_DOUBLE_EQUAL(actual, expected, granularity) { if ((fabs((double)actual - expected) > fabs((double)granularity))) { CU_assertImplementation(FALSE, __LINE__, ("ASSERT_DOUBLE_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", FALSE); return; }} -/** Deprecated (version 1). @deprecated Use CU_ASSERT_DOUBLE_NOT_EQUAL_FATAL. */ -#define ASSERT_DOUBLE_NOT_EQUAL(actual, expected, granularity) { if ((fabs((double)actual - expected) <= fabs((double)granularity))) { CU_assertImplementation(TRUE, __LINE__, ("ASSERT_DOUBLE_NOT_EQUAL(" #actual "," #expected "," #granularity ")"), __FILE__, "", FALSE); return; }} -#endif /* USE_DEPRECATED_CUNIT_NAMES */ - -#endif /* CUNIT_CUNIT_H_SEEN */ - -/** @} */ diff --git a/gearsyncd/tests/cunit/CUnit_intl.h b/gearsyncd/tests/cunit/CUnit_intl.h deleted file mode 100644 index 813917b25c..0000000000 --- a/gearsyncd/tests/cunit/CUnit_intl.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2006 Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Internationalization support - * - * 05-May-2006 Initial implementation. (JDS) - */ - -/** @file - * Internal CUnit header supporting internationalization of - * CUnit framework & interfaces. - */ -/** @addtogroup Framework - * @{ - */ - -#ifndef CUNIT_CUNIT_INTL_H_SEEN -#define CUNIT_CUNIT_INTL_H_SEEN - -/* activate these when source preparation is complete -#include -#ifndef _ -# define _(String) gettext (String) -#endif -#ifndef gettext_noop -# define gettext_noop(String) String -#endif -#ifndef N_ -# define N_(String) gettext_noop (String) -#endif -*/ - -/* deactivate these when source preparation is complete */ -#undef _ -#define _(String) (String) -#undef N_ -#define N_(String) String -#undef textdomain -#define textdomain(Domain) -#undef bindtextdomain -#define bindtextdomain(Package, Directory) - -#endif /* CUNIT_CUNIT_INTL_H_SEEN */ - -/** @} */ diff --git a/gearsyncd/tests/cunit/Console.c b/gearsyncd/tests/cunit/Console.c deleted file mode 100644 index 512e3e6987..0000000000 --- a/gearsyncd/tests/cunit/Console.c +++ /dev/null @@ -1,735 +0,0 @@ - /* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of the Console Test Interface. - * - * Aug 2001 Initial implementation (AK) - * - * 19/Aug/2001 Added initial console interface functions without - * any run functionality. (AK) - * - * 24/Aug/2001 Added compare_strings, show_errors, list_suites, - * list_tests function declarations. (AK) - * - * 17-Jul-2004 New interface, doxygen comments, reformat console output. (JDS) - * - * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) - * - * 24-Apr-2006 Suite/test selection is now by number rather than name. - * Inactive suites/tests now reported. - * Interface supports (de)activation of tests/suites. - * Help function added for both menu levels. - * Option menu added. Immediate action on hotkeys - * without needing to , like curses. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Console test interface with interactive output (implementation). - */ -/** @addtogroup Console - @{ -*/ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "Util.h" -#include "TestRun.h" -#include "Console.h" -#include "CUnit_intl.h" - -extern int sai_driver_shell(); -extern int warmboot_enable, collect_syslog; - -/** Console interface status flag. */ -typedef enum -{ - CU_STATUS_CONTINUE = 1, /**< Continue processing commands in current menu. */ - CU_STATUS_MOVE_UP, /**< Move up to the previous menu. */ - CU_STATUS_STOP /**< Stop processing (user selected 'Quit'). */ -} CU_STATUS; - -/*================================================================= - * Global / Static data definitions - *=================================================================*/ -/** Pointer to the currently running suite. */ -static CU_pSuite f_pRunningSuite = NULL; - -/** Common width measurements for output formatting. */ -static size_t f_yes_width = 0; -static size_t f_no_width = 0; - -/*================================================================= - * Static function forward declarations - *=================================================================*/ -void console_registry_level_run(CU_pTestRegistry pRegistry); -static CU_STATUS console_suite_level_run(CU_pSuite pSuite); -static CU_STATUS console_set_options_run(void); - -static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry); -static CU_ErrorCode console_run_suite(CU_pSuite pSuite); -static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest); - -static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); -static void console_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); -static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); -static void console_suite_init_failure_message_handler(const CU_pSuite pSuite); -static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); - -static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest); -static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite); - -static void list_suites(CU_pTestRegistry pRegistry); -static void list_tests(CU_pSuite pSuite); -static void show_failures(void); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -void CU_console_run_tests(void) -{ - /* - * To avoid user from cribbing about the output not coming onto - * screen at the moment of SIGSEGV. - */ - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - - fprintf(stdout, "\n\n %s" CU_VERSION - "\n %s\n", - _("CUnit - A Unit testing framework for C - Version "), - _("http://cunit.sourceforge.net/")); - - if (NULL == CU_get_registry()) { - fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); - CU_set_error(CUE_NOREGISTRY); - } - else { - f_yes_width = strlen(_("Yes")); - f_no_width = strlen(_("No")); - - CU_set_test_start_handler(console_test_start_message_handler); - CU_set_test_complete_handler(console_test_complete_message_handler); - CU_set_all_test_complete_handler(console_all_tests_complete_message_handler); - CU_set_suite_init_failure_handler(console_suite_init_failure_message_handler); - CU_set_suite_cleanup_failure_handler(console_suite_cleanup_failure_message_handler); - - console_registry_level_run(NULL); - } -} - -/*================================================================= - * Static function implementation - *=================================================================*/ -/** - * Main loop for console interface. - * Displays actions and responds based on user imput. If pRegistry - * is NULL, will use the default internal CUnit test registry. - * - * @param pRegistry The CU_pTestRegistry to use for testing. - */ -void console_registry_level_run(CU_pTestRegistry pRegistry) -{ - int chChoice; - CU_pSuite pSuite = NULL; - CU_STATUS eStatus = CU_STATUS_CONTINUE; - char szTemp[256]; - - while (CU_STATUS_CONTINUE == eStatus) - { - fprintf(stdout, "\n\n%s\n%s\n%s", - _("***************************** CUNIT CONSOLE - MAIN MENU *******************************************"), - _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), - _("Enter command: ")); - chChoice = toupper(getchar()); - if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ - - if (chChoice == _("R")[0]) { - console_run_all_tests(pRegistry); - } - - else if (chChoice == _("S")[0]) { - if (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { - assert(NULL != pSuite->pName); - fprintf(stdout, _("Suite '%s' selected."), pSuite->pName); - fprintf(stdout, "\n"); - if (CU_STATUS_STOP == console_suite_level_run(pSuite)) { - eStatus = CU_STATUS_STOP; - } - } - else { - fprintf(stdout, "\n%s\n", _("Suite not found.")); - } - } - - else if (chChoice == _("L")[0]) { - list_suites(pRegistry); - } - - else if (chChoice == _("A")[0]) { - while (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { - CU_set_suite_active(pSuite, (CU_FALSE == pSuite->fActive) ? CU_TRUE : CU_FALSE); - } - } - - else if (chChoice == _("F")[0]) { - show_failures(); - } - - else if (chChoice == _("O")[0]) { - console_set_options_run(); - } - - else if (chChoice == _("Q")[0]) { - eStatus = CU_STATUS_STOP; - } - - else if (chChoice == _("D")[0]) { - fprintf(stdout, "Hit enter to get drivshell prompt..\n"); - sai_driver_shell(); - } - - else if (chChoice == _("W")[0]) { - warmboot_enable = warmboot_enable ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", - warmboot_enable ? "enable" : "disable"); - } - - else if (chChoice == _("C")[0]) { - collect_syslog = collect_syslog ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", - collect_syslog ? "enable" : "disable"); - } - - else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { - fprintf(stdout, "\n%s\n", _("Commands: R - run all tests in all suites")); - fprintf(stdout, "%s\n", _(" S - Select a suite to run or modify")); - fprintf(stdout, "%s\n", _(" L - List all registered suites")); - fprintf(stdout, "%s\n", _(" A - Activate or deactivate a suite (toggle)")); - fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); - fprintf(stdout, "%s\n", _(" O - Set CUnit options")); - fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); - fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); - fprintf(stdout, "%s\n", _(" H - Show this help message")); - fprintf(stdout, "%s\n", _(" Q - Quit the application")); - } - } -} - -/*------------------------------------------------------------------------*/ -/** - * Runs a selected suite within the console interface. - * Displays actions and responds based on user imput. - * - * @param pSuite The suite to use for testing (non-NULL). - */ -static CU_STATUS console_suite_level_run(CU_pSuite pSuite) -{ - int chChoice; - CU_pTest pTest = NULL; - CU_STATUS eStatus = CU_STATUS_CONTINUE; - char szTemp[256]; - - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - while (CU_STATUS_CONTINUE == eStatus) { - - fprintf(stdout, "\n%s\n%s\n%s", - _("**************************** CUNIT CONSOLE - SUITE MENU ************************************************"), - _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), - _("Enter command: ")); - chChoice = toupper(getchar()); - if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ - - if (chChoice == _("R")[0]) { - console_run_suite(pSuite); - } - - else if (chChoice == _("S")[0]) { - if (CUE_SUCCESS == select_test(pSuite, &pTest)) { - console_run_single_test(pSuite, pTest); - } - else { - fprintf(stdout, "\n%s\n", _("Test not found.")); - } - } - - else if (chChoice == _("L")[0]) { - list_tests(pSuite); - } - - else if (chChoice == _("A")[0]) { - while (CUE_SUCCESS == select_test(pSuite, &pTest)) { - CU_set_test_active(pTest, (CU_FALSE == pTest->fActive) ? CU_TRUE : CU_FALSE); - } - } - - else if (chChoice == _("F")[0]) { - show_failures(); - } - - else if ((chChoice == _("M")[0]) || (chChoice == _("U")[0])) { - eStatus = CU_STATUS_MOVE_UP; - } - - else if (chChoice == _("O")[0]) { - console_set_options_run(); - } - - else if (chChoice == _("Q")[0]) { - eStatus = CU_STATUS_STOP; - } - - else if (chChoice == _("D")[0]) { - fprintf(stdout, "Hit enter to get drivshell prompt..\n"); - sai_driver_shell(); - } - - else if (chChoice == _("W")[0]) { - warmboot_enable = warmboot_enable ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", - warmboot_enable ? "enable" : "disable"); - } - - else if (chChoice == _("C")[0]) { - collect_syslog = collect_syslog ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", - collect_syslog ? "enable" : "disable"); - } - - else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { - fprintf(stdout, "\n"); - fprintf(stdout, _("Commands: R - run all tests in suite %s"), pSuite->pName); - fprintf(stdout, "\n"); - fprintf(stdout, "%s\n", _(" S - Select and run a test")); - fprintf(stdout, _(" L - List all tests registered in suite %s"), pSuite->pName); - fprintf(stdout, "\n"); - fprintf(stdout, "%s\n", _(" A - Activate or deactivate a test (toggle)")); - fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); - fprintf(stdout, "%s\n", _(" M - Move up to main menu")); - fprintf(stdout, "%s\n", _(" O - Set CUnit options")); - fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); - fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); - fprintf(stdout, "%s\n", _(" H - Show this help message")); - fprintf(stdout, "%s\n", _(" Q - Quit the application")); - } - } - return eStatus; -} - -/*------------------------------------------------------------------------*/ -/** - * Sets CUnit options interactively using console interface. - * Displays actions and responds based on user imput. - */ -static CU_STATUS console_set_options_run(void) -{ - int chChoice; - CU_STATUS eStatus = CU_STATUS_CONTINUE; - char szTemp[256]; - - while (CU_STATUS_CONTINUE == eStatus) { - fprintf(stdout, "\n%s\n", - _("***************** CUNIT CONSOLE - OPTIONS **************************")); - fprintf(stdout, _(" 1 - Inactive suites/tests treated as runtime failures %s"), - (CU_FALSE != CU_get_fail_on_inactive()) ? _("Yes") : _("No")); - fprintf(stdout, "\n********************************************************************\n"); - fprintf(stdout, "%s", - _("Enter number of option to change : ")); - chChoice = getchar(); - if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ - - switch (tolower(chChoice)) { - case '1': - CU_set_fail_on_inactive((CU_FALSE == CU_get_fail_on_inactive()) ? CU_TRUE : CU_FALSE); - break; - - default: - eStatus = CU_STATUS_MOVE_UP; - break; - } - } - return eStatus; -} - -/*------------------------------------------------------------------------*/ -/** - * Runs all tests within the console interface. - * The test registry is changed to the specified registry before running - * the tests, and reset to the original registry when done. If pRegistry - * is NULL, the default internal CUnit test registry is used. - * - * @param pRegistry The CU_pTestRegistry containing the tests to be run. - * @return An error code indicating the error status during the test run. - */ -static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = NULL; - CU_ErrorCode result; - - f_pRunningSuite = NULL; - - if (NULL != pRegistry) { - pOldRegistry = CU_set_registry(pRegistry); - } - result = CU_run_all_tests(); - if (NULL != pRegistry) { - CU_set_registry(pOldRegistry); - } - return result; -} - -/*------------------------------------------------------------------------*/ -/** - * Runs a specified suite within the console interface. - * - * @param pSuite The suite to be run (non-NULL). - * @return An error code indicating the error status during the test run. - */ -static CU_ErrorCode console_run_suite(CU_pSuite pSuite) -{ - f_pRunningSuite = NULL; - return CU_run_suite(pSuite); -} - -/*------------------------------------------------------------------------*/ -/** - ( Runs a specific test for the specified suite within the console interface. - * - * @param pSuite The suite containing the test to be run (non-NULL). - * @param pTest The test to be run (non-NULL). - * @return An error code indicating the error status during the test run. - */ -static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest) -{ - f_pRunningSuite = NULL; - return CU_run_test(pSuite, pTest); -} - -/*------------------------------------------------------------------------*/ -/** - * Reads the number of a test from standard input and locates the test - * at that position. A pointer to the located test is stored - * in ppTest upon return. - * - * @param pSuite The suite to be queried. - * @param ppTest Pointer to location to store the selected test. - * @return CUE_SUCCESS if a test was successfully selected, CUE_NOTEST - * otherwise. On return, ppTest points to the test selected, - * or NULL if none. - */ -static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest) -{ - char buffer[100]; - - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - *ppTest = NULL; - - if (0 == pSuite->uiNumberOfTests) { - fprintf(stdout, "\n"); - fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); - } - else { - list_tests(pSuite); - fprintf(stdout, "\n"); - fprintf(stdout, _("Enter number of test to select (1-%u) : "), - pSuite->uiNumberOfTests); - if (fgets(buffer, 100, stdin)); - - *ppTest = CU_get_test_by_index(atol(buffer), pSuite); - } - - return (NULL != *ppTest) ? CUE_SUCCESS : CUE_NOTEST; -} - -/*------------------------------------------------------------------------*/ -/** - * Reads the number of a suite from standard input and locates the suite - * at that position. If pRegistry is NULL, the default CUnit registry - * will be used. The located pSuite is returned in ppSuite. ppSuite - * will be NULL if there is no suite in the registry having the input name. - * Returns NULL if the suite is successfully located, non-NULL otherwise. - * - * @param pRegistry The CU_pTestRegistry to query. If NULL, use the - * default internal CUnit test registry. - * @param ppSuite Pointer to location to store the selected suite. - * @return CUE_SUCCESS if a suite was successfully selected, CUE_NOSUITE - * otherwise. On return, ppSuite points to the suite selected. - */ -static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite) -{ - char buffer[100]; - - if (NULL == pRegistry) { - pRegistry = CU_get_registry(); - } - - if (0 == pRegistry->uiNumberOfSuites) { - fprintf(stdout, "\n%s", _("No suites are registered.")); - *ppSuite = NULL; - } - else { - list_suites(pRegistry); - fprintf(stdout, "\n"); - fprintf(stdout, _("Enter number of suite to select (1-%u) : "), - pRegistry->uiNumberOfSuites); - if (fgets(buffer, 100, stdin)); - - *ppSuite = CU_get_suite_by_index(atol(buffer), pRegistry); - } - - return (NULL != *ppSuite) ? CUE_SUCCESS : CUE_NOSUITE; -} - -/*------------------------------------------------------------------------*/ -/** - * Lists the suites in a registry to standard output. - * @param pRegistry The CU_pTestRegistry to query (non-NULL). - */ -static void list_suites(CU_pTestRegistry pRegistry) -{ - CU_pSuite pCurSuite = NULL; - int i; - static int width[6]; - - if (NULL == pRegistry) { - pRegistry = CU_get_registry(); - } - - assert(NULL != pRegistry); - if (0 == pRegistry->uiNumberOfSuites) { - fprintf(stdout, "\n%s\n", _("No suites are registered.")); - return; - } - - assert(NULL != pRegistry->pSuite); - - /* only need to calculate formatting widths once */ - if (0 == width[0]) { - width[0] = CU_number_width(pRegistry->uiNumberOfSuites) + 1; - width[1] = 34; - width[2] = CU_MAX(strlen(_("Init?")), CU_MAX(f_yes_width, f_no_width)) + 1; - width[3] = CU_MAX(strlen(_("Cleanup?")), CU_MAX(f_yes_width, f_no_width)) + 1; - width[4] = CU_MAX(strlen(_("#Tests")), CU_number_width(pRegistry->uiNumberOfTests) + 1) + 1; - width[5] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; - } - - fprintf(stdout, "\n%s", _("--------------------- Registered Suites -----------------------------")); - fprintf(stdout, "\n%*s %-*s%*s%*s%*s%*s\n", - width[0], _("#"), - width[1], _("Suite Name"), - width[2], _("Init?"), - width[3], _("Cleanup?"), - width[4], _("#Tests"), - width[5], _("Active?")); - - for (i = 1, pCurSuite = pRegistry->pSuite; (NULL != pCurSuite); pCurSuite = pCurSuite->pNext, ++i) { - assert(NULL != pCurSuite->pName); - fprintf(stdout, "\n%*d. %-*.*s%*s%*s%*u%*s", - width[0], i, - width[1], width[1] - 1, pCurSuite->pName, - width[2]-1, (NULL != pCurSuite->pInitializeFunc) ? _("Yes") : _("No"), - width[3], (NULL != pCurSuite->pCleanupFunc) ? _("Yes") : _("No"), - width[4], pCurSuite->uiNumberOfTests, - width[5], (CU_FALSE != pCurSuite->fActive) ? _("Yes") : _("No")); - } - fprintf(stdout, "\n---------------------------------------------------------------------\n"); - fprintf(stdout, _("Total Number of Suites : %-u"), pRegistry->uiNumberOfSuites); - fprintf(stdout, "\n"); -} - -/*------------------------------------------------------------------------*/ -/** - * Lists the tests in a suite to standard output. - * @param pSuite The suite to query (non-NULL). - */ -static void list_tests(CU_pSuite pSuite) -{ - CU_pTest pCurTest = NULL; - unsigned int uiCount; - static int width[3]; - - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - if (0 == pSuite->uiNumberOfTests) { - fprintf(stdout, "\n"); - fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); - fprintf(stdout, "\n"); - return; - } - - assert(NULL != pSuite->pTest); - - /* only number of tests can change between calls */ - width[0] = CU_number_width(pSuite->uiNumberOfTests) + 1; - if (0 == width[1]) { - width[1] = 34; - width[2] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; - } - - fprintf(stdout, "\n%s", - _("----------------- Test List ------------------------------")); - fprintf(stdout, "\n%s%s\n", _("Suite: "), pSuite->pName); - fprintf(stdout, "\n%*s %-*s%*s\n", - width[0], _("#"), - width[1], _("Test Name"), - width[2], _("Active?")); - - for (uiCount = 1, pCurTest = pSuite->pTest ; - NULL != pCurTest ; - uiCount++, pCurTest = pCurTest->pNext) { - assert(NULL != pCurTest->pName); - fprintf(stdout, "\n%*u. %-*.*s%*s", - width[0], uiCount, - width[1], width[1]-1, pCurTest->pName, - width[2]-1, (CU_FALSE != pCurTest->fActive) ? _("Yes") : _("No")); - } - fprintf(stdout, "\n----------------------------------------------------------\n"); - fprintf(stdout, _("Total Number of Tests : %-u"), pSuite->uiNumberOfTests); - fprintf(stdout, "\n"); -} - -/*------------------------------------------------------------------------*/ -/** Displays the record of test failures on standard output. */ -static void show_failures(void) -{ - unsigned int i; - CU_pFailureRecord pFailure = CU_get_failure_list(); - - if (NULL == pFailure) { - fprintf(stdout, "\n%s\n", _("No failures.")); - } - else { - - fprintf(stdout, "\n%s", - _("--------------- Test Run Failures -------------------------")); - fprintf(stdout, "\n%s\n", - _(" src_file:line# : (suite:test) : failure_condition")); - - for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { - fprintf(stdout, "\n%d. %s:%u : (%s : %s) : %s", i, - (NULL != pFailure->strFileName) - ? pFailure->strFileName : "", - pFailure->uiLineNumber, - ((NULL != pFailure->pSuite) && (NULL != pFailure->pSuite->pName)) - ? pFailure->pSuite->pName : "", - ((NULL != pFailure->pTest) && (NULL != pFailure->pTest->pName)) - ? pFailure->pTest->pName : "", - (NULL != pFailure->strCondition) - ? pFailure->strCondition : ""); - } - fprintf(stdout, "\n-----------------------------------------------------------"); - fprintf(stdout, "\n"); - fprintf(stdout, _("Total Number of Failures : %-u"), i - 1); - fprintf(stdout, "\n"); - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at start of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - */ -static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - assert(NULL != pTest); - assert(NULL != pTest->pName); - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - /* Comparing the Addresses rather than the Group Names. */ - if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { - fprintf(stdout, _("\nRunning Suite : %s"), pSuite->pName); - fprintf(stdout, _("\n Running Test : %s"), pTest->pName); - f_pRunningSuite = pSuite; - } - else { - fprintf(stdout, _("\n Running Test : %s"), pTest->pName); - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - * @param pFailure Pointer to the 1st failure record for this test. - */ -static void console_test_complete_message_handler(const CU_pTest pTest, - const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - /* - * For console interface do nothing. This is useful only for the test - * interface where UI is involved. Just silence compiler warnings. - */ - CU_UNREFERENCED_PARAMETER(pTest); - CU_UNREFERENCED_PARAMETER(pSuite); - CU_UNREFERENCED_PARAMETER(pFailure); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of all tests in a suite. - * @param pFailure Pointer to the test failure record list. - */ -static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) -{ - CU_UNREFERENCED_PARAMETER(pFailure); /* not used in console interface */ - printf("\n\n"); - CU_print_run_results(stdout); - printf("\n"); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite initialization fails. - * @param pSuite The suite for which initialization failed. - */ -static void console_suite_init_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - fprintf(stdout, - _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite cleanup fails. - * @param pSuite The suite for which cleanup failed. - */ -static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); -} - -/** @} */ diff --git a/gearsyncd/tests/cunit/Console.cpp b/gearsyncd/tests/cunit/Console.cpp deleted file mode 100644 index 512e3e6987..0000000000 --- a/gearsyncd/tests/cunit/Console.cpp +++ /dev/null @@ -1,735 +0,0 @@ - /* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of the Console Test Interface. - * - * Aug 2001 Initial implementation (AK) - * - * 19/Aug/2001 Added initial console interface functions without - * any run functionality. (AK) - * - * 24/Aug/2001 Added compare_strings, show_errors, list_suites, - * list_tests function declarations. (AK) - * - * 17-Jul-2004 New interface, doxygen comments, reformat console output. (JDS) - * - * 30-Apr-2005 Added notification of suite cleanup failure. (JDS) - * - * 24-Apr-2006 Suite/test selection is now by number rather than name. - * Inactive suites/tests now reported. - * Interface supports (de)activation of tests/suites. - * Help function added for both menu levels. - * Option menu added. Immediate action on hotkeys - * without needing to , like curses. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Console test interface with interactive output (implementation). - */ -/** @addtogroup Console - @{ -*/ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "Util.h" -#include "TestRun.h" -#include "Console.h" -#include "CUnit_intl.h" - -extern int sai_driver_shell(); -extern int warmboot_enable, collect_syslog; - -/** Console interface status flag. */ -typedef enum -{ - CU_STATUS_CONTINUE = 1, /**< Continue processing commands in current menu. */ - CU_STATUS_MOVE_UP, /**< Move up to the previous menu. */ - CU_STATUS_STOP /**< Stop processing (user selected 'Quit'). */ -} CU_STATUS; - -/*================================================================= - * Global / Static data definitions - *=================================================================*/ -/** Pointer to the currently running suite. */ -static CU_pSuite f_pRunningSuite = NULL; - -/** Common width measurements for output formatting. */ -static size_t f_yes_width = 0; -static size_t f_no_width = 0; - -/*================================================================= - * Static function forward declarations - *=================================================================*/ -void console_registry_level_run(CU_pTestRegistry pRegistry); -static CU_STATUS console_suite_level_run(CU_pSuite pSuite); -static CU_STATUS console_set_options_run(void); - -static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry); -static CU_ErrorCode console_run_suite(CU_pSuite pSuite); -static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest); - -static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite); -static void console_test_complete_message_handler(const CU_pTest pTest, const CU_pSuite pSuite, const CU_pFailureRecord pFailure); -static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure); -static void console_suite_init_failure_message_handler(const CU_pSuite pSuite); -static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite); - -static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest); -static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite); - -static void list_suites(CU_pTestRegistry pRegistry); -static void list_tests(CU_pSuite pSuite); -static void show_failures(void); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -void CU_console_run_tests(void) -{ - /* - * To avoid user from cribbing about the output not coming onto - * screen at the moment of SIGSEGV. - */ - setvbuf(stdout, NULL, _IONBF, 0); - setvbuf(stderr, NULL, _IONBF, 0); - - fprintf(stdout, "\n\n %s" CU_VERSION - "\n %s\n", - _("CUnit - A Unit testing framework for C - Version "), - _("http://cunit.sourceforge.net/")); - - if (NULL == CU_get_registry()) { - fprintf(stderr, "\n\n%s\n", _("FATAL ERROR - Test registry is not initialized.")); - CU_set_error(CUE_NOREGISTRY); - } - else { - f_yes_width = strlen(_("Yes")); - f_no_width = strlen(_("No")); - - CU_set_test_start_handler(console_test_start_message_handler); - CU_set_test_complete_handler(console_test_complete_message_handler); - CU_set_all_test_complete_handler(console_all_tests_complete_message_handler); - CU_set_suite_init_failure_handler(console_suite_init_failure_message_handler); - CU_set_suite_cleanup_failure_handler(console_suite_cleanup_failure_message_handler); - - console_registry_level_run(NULL); - } -} - -/*================================================================= - * Static function implementation - *=================================================================*/ -/** - * Main loop for console interface. - * Displays actions and responds based on user imput. If pRegistry - * is NULL, will use the default internal CUnit test registry. - * - * @param pRegistry The CU_pTestRegistry to use for testing. - */ -void console_registry_level_run(CU_pTestRegistry pRegistry) -{ - int chChoice; - CU_pSuite pSuite = NULL; - CU_STATUS eStatus = CU_STATUS_CONTINUE; - char szTemp[256]; - - while (CU_STATUS_CONTINUE == eStatus) - { - fprintf(stdout, "\n\n%s\n%s\n%s", - _("***************************** CUNIT CONSOLE - MAIN MENU *******************************************"), - _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), - _("Enter command: ")); - chChoice = toupper(getchar()); - if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ - - if (chChoice == _("R")[0]) { - console_run_all_tests(pRegistry); - } - - else if (chChoice == _("S")[0]) { - if (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { - assert(NULL != pSuite->pName); - fprintf(stdout, _("Suite '%s' selected."), pSuite->pName); - fprintf(stdout, "\n"); - if (CU_STATUS_STOP == console_suite_level_run(pSuite)) { - eStatus = CU_STATUS_STOP; - } - } - else { - fprintf(stdout, "\n%s\n", _("Suite not found.")); - } - } - - else if (chChoice == _("L")[0]) { - list_suites(pRegistry); - } - - else if (chChoice == _("A")[0]) { - while (CUE_SUCCESS == select_suite(pRegistry, &pSuite)) { - CU_set_suite_active(pSuite, (CU_FALSE == pSuite->fActive) ? CU_TRUE : CU_FALSE); - } - } - - else if (chChoice == _("F")[0]) { - show_failures(); - } - - else if (chChoice == _("O")[0]) { - console_set_options_run(); - } - - else if (chChoice == _("Q")[0]) { - eStatus = CU_STATUS_STOP; - } - - else if (chChoice == _("D")[0]) { - fprintf(stdout, "Hit enter to get drivshell prompt..\n"); - sai_driver_shell(); - } - - else if (chChoice == _("W")[0]) { - warmboot_enable = warmboot_enable ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", - warmboot_enable ? "enable" : "disable"); - } - - else if (chChoice == _("C")[0]) { - collect_syslog = collect_syslog ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", - collect_syslog ? "enable" : "disable"); - } - - else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { - fprintf(stdout, "\n%s\n", _("Commands: R - run all tests in all suites")); - fprintf(stdout, "%s\n", _(" S - Select a suite to run or modify")); - fprintf(stdout, "%s\n", _(" L - List all registered suites")); - fprintf(stdout, "%s\n", _(" A - Activate or deactivate a suite (toggle)")); - fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); - fprintf(stdout, "%s\n", _(" O - Set CUnit options")); - fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); - fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); - fprintf(stdout, "%s\n", _(" H - Show this help message")); - fprintf(stdout, "%s\n", _(" Q - Quit the application")); - } - } -} - -/*------------------------------------------------------------------------*/ -/** - * Runs a selected suite within the console interface. - * Displays actions and responds based on user imput. - * - * @param pSuite The suite to use for testing (non-NULL). - */ -static CU_STATUS console_suite_level_run(CU_pSuite pSuite) -{ - int chChoice; - CU_pTest pTest = NULL; - CU_STATUS eStatus = CU_STATUS_CONTINUE; - char szTemp[256]; - - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - while (CU_STATUS_CONTINUE == eStatus) { - - fprintf(stdout, "\n%s\n%s\n%s", - _("**************************** CUNIT CONSOLE - SUITE MENU ************************************************"), - _("(R)un (S)elect (L)ist (A)ctivate (F)ailures (U)p (O)ptions (H)elp (D)rvShell (W)armboot (C)syslog (Q)uit"), - _("Enter command: ")); - chChoice = toupper(getchar()); - if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ - - if (chChoice == _("R")[0]) { - console_run_suite(pSuite); - } - - else if (chChoice == _("S")[0]) { - if (CUE_SUCCESS == select_test(pSuite, &pTest)) { - console_run_single_test(pSuite, pTest); - } - else { - fprintf(stdout, "\n%s\n", _("Test not found.")); - } - } - - else if (chChoice == _("L")[0]) { - list_tests(pSuite); - } - - else if (chChoice == _("A")[0]) { - while (CUE_SUCCESS == select_test(pSuite, &pTest)) { - CU_set_test_active(pTest, (CU_FALSE == pTest->fActive) ? CU_TRUE : CU_FALSE); - } - } - - else if (chChoice == _("F")[0]) { - show_failures(); - } - - else if ((chChoice == _("M")[0]) || (chChoice == _("U")[0])) { - eStatus = CU_STATUS_MOVE_UP; - } - - else if (chChoice == _("O")[0]) { - console_set_options_run(); - } - - else if (chChoice == _("Q")[0]) { - eStatus = CU_STATUS_STOP; - } - - else if (chChoice == _("D")[0]) { - fprintf(stdout, "Hit enter to get drivshell prompt..\n"); - sai_driver_shell(); - } - - else if (chChoice == _("W")[0]) { - warmboot_enable = warmboot_enable ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases in warmboot %s mode\n", - warmboot_enable ? "enable" : "disable"); - } - - else if (chChoice == _("C")[0]) { - collect_syslog = collect_syslog ? 0 : 1; - fprintf(stdout, "Framework changed to run use cases with syslog collection %s\n", - collect_syslog ? "enable" : "disable"); - } - - else if ((chChoice == _("H")[0]) || (chChoice == _("?")[0])) { - fprintf(stdout, "\n"); - fprintf(stdout, _("Commands: R - run all tests in suite %s"), pSuite->pName); - fprintf(stdout, "\n"); - fprintf(stdout, "%s\n", _(" S - Select and run a test")); - fprintf(stdout, _(" L - List all tests registered in suite %s"), pSuite->pName); - fprintf(stdout, "\n"); - fprintf(stdout, "%s\n", _(" A - Activate or deactivate a test (toggle)")); - fprintf(stdout, "%s\n", _(" F - Show failures from last test run")); - fprintf(stdout, "%s\n", _(" M - Move up to main menu")); - fprintf(stdout, "%s\n", _(" O - Set CUnit options")); - fprintf(stdout, "%s\n", _(" W - Toggle warmboot mode.")); - fprintf(stdout, "%s\n", _(" C - Toggle collect syslog mode")); - fprintf(stdout, "%s\n", _(" H - Show this help message")); - fprintf(stdout, "%s\n", _(" Q - Quit the application")); - } - } - return eStatus; -} - -/*------------------------------------------------------------------------*/ -/** - * Sets CUnit options interactively using console interface. - * Displays actions and responds based on user imput. - */ -static CU_STATUS console_set_options_run(void) -{ - int chChoice; - CU_STATUS eStatus = CU_STATUS_CONTINUE; - char szTemp[256]; - - while (CU_STATUS_CONTINUE == eStatus) { - fprintf(stdout, "\n%s\n", - _("***************** CUNIT CONSOLE - OPTIONS **************************")); - fprintf(stdout, _(" 1 - Inactive suites/tests treated as runtime failures %s"), - (CU_FALSE != CU_get_fail_on_inactive()) ? _("Yes") : _("No")); - fprintf(stdout, "\n********************************************************************\n"); - fprintf(stdout, "%s", - _("Enter number of option to change : ")); - chChoice = getchar(); - if (fgets(szTemp, 256, stdin)); /* flush any chars out of the read buffer */ - - switch (tolower(chChoice)) { - case '1': - CU_set_fail_on_inactive((CU_FALSE == CU_get_fail_on_inactive()) ? CU_TRUE : CU_FALSE); - break; - - default: - eStatus = CU_STATUS_MOVE_UP; - break; - } - } - return eStatus; -} - -/*------------------------------------------------------------------------*/ -/** - * Runs all tests within the console interface. - * The test registry is changed to the specified registry before running - * the tests, and reset to the original registry when done. If pRegistry - * is NULL, the default internal CUnit test registry is used. - * - * @param pRegistry The CU_pTestRegistry containing the tests to be run. - * @return An error code indicating the error status during the test run. - */ -static CU_ErrorCode console_run_all_tests(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = NULL; - CU_ErrorCode result; - - f_pRunningSuite = NULL; - - if (NULL != pRegistry) { - pOldRegistry = CU_set_registry(pRegistry); - } - result = CU_run_all_tests(); - if (NULL != pRegistry) { - CU_set_registry(pOldRegistry); - } - return result; -} - -/*------------------------------------------------------------------------*/ -/** - * Runs a specified suite within the console interface. - * - * @param pSuite The suite to be run (non-NULL). - * @return An error code indicating the error status during the test run. - */ -static CU_ErrorCode console_run_suite(CU_pSuite pSuite) -{ - f_pRunningSuite = NULL; - return CU_run_suite(pSuite); -} - -/*------------------------------------------------------------------------*/ -/** - ( Runs a specific test for the specified suite within the console interface. - * - * @param pSuite The suite containing the test to be run (non-NULL). - * @param pTest The test to be run (non-NULL). - * @return An error code indicating the error status during the test run. - */ -static CU_ErrorCode console_run_single_test(CU_pSuite pSuite, CU_pTest pTest) -{ - f_pRunningSuite = NULL; - return CU_run_test(pSuite, pTest); -} - -/*------------------------------------------------------------------------*/ -/** - * Reads the number of a test from standard input and locates the test - * at that position. A pointer to the located test is stored - * in ppTest upon return. - * - * @param pSuite The suite to be queried. - * @param ppTest Pointer to location to store the selected test. - * @return CUE_SUCCESS if a test was successfully selected, CUE_NOTEST - * otherwise. On return, ppTest points to the test selected, - * or NULL if none. - */ -static CU_ErrorCode select_test(CU_pSuite pSuite, CU_pTest* ppTest) -{ - char buffer[100]; - - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - *ppTest = NULL; - - if (0 == pSuite->uiNumberOfTests) { - fprintf(stdout, "\n"); - fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); - } - else { - list_tests(pSuite); - fprintf(stdout, "\n"); - fprintf(stdout, _("Enter number of test to select (1-%u) : "), - pSuite->uiNumberOfTests); - if (fgets(buffer, 100, stdin)); - - *ppTest = CU_get_test_by_index(atol(buffer), pSuite); - } - - return (NULL != *ppTest) ? CUE_SUCCESS : CUE_NOTEST; -} - -/*------------------------------------------------------------------------*/ -/** - * Reads the number of a suite from standard input and locates the suite - * at that position. If pRegistry is NULL, the default CUnit registry - * will be used. The located pSuite is returned in ppSuite. ppSuite - * will be NULL if there is no suite in the registry having the input name. - * Returns NULL if the suite is successfully located, non-NULL otherwise. - * - * @param pRegistry The CU_pTestRegistry to query. If NULL, use the - * default internal CUnit test registry. - * @param ppSuite Pointer to location to store the selected suite. - * @return CUE_SUCCESS if a suite was successfully selected, CUE_NOSUITE - * otherwise. On return, ppSuite points to the suite selected. - */ -static CU_ErrorCode select_suite(CU_pTestRegistry pRegistry, CU_pSuite* ppSuite) -{ - char buffer[100]; - - if (NULL == pRegistry) { - pRegistry = CU_get_registry(); - } - - if (0 == pRegistry->uiNumberOfSuites) { - fprintf(stdout, "\n%s", _("No suites are registered.")); - *ppSuite = NULL; - } - else { - list_suites(pRegistry); - fprintf(stdout, "\n"); - fprintf(stdout, _("Enter number of suite to select (1-%u) : "), - pRegistry->uiNumberOfSuites); - if (fgets(buffer, 100, stdin)); - - *ppSuite = CU_get_suite_by_index(atol(buffer), pRegistry); - } - - return (NULL != *ppSuite) ? CUE_SUCCESS : CUE_NOSUITE; -} - -/*------------------------------------------------------------------------*/ -/** - * Lists the suites in a registry to standard output. - * @param pRegistry The CU_pTestRegistry to query (non-NULL). - */ -static void list_suites(CU_pTestRegistry pRegistry) -{ - CU_pSuite pCurSuite = NULL; - int i; - static int width[6]; - - if (NULL == pRegistry) { - pRegistry = CU_get_registry(); - } - - assert(NULL != pRegistry); - if (0 == pRegistry->uiNumberOfSuites) { - fprintf(stdout, "\n%s\n", _("No suites are registered.")); - return; - } - - assert(NULL != pRegistry->pSuite); - - /* only need to calculate formatting widths once */ - if (0 == width[0]) { - width[0] = CU_number_width(pRegistry->uiNumberOfSuites) + 1; - width[1] = 34; - width[2] = CU_MAX(strlen(_("Init?")), CU_MAX(f_yes_width, f_no_width)) + 1; - width[3] = CU_MAX(strlen(_("Cleanup?")), CU_MAX(f_yes_width, f_no_width)) + 1; - width[4] = CU_MAX(strlen(_("#Tests")), CU_number_width(pRegistry->uiNumberOfTests) + 1) + 1; - width[5] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; - } - - fprintf(stdout, "\n%s", _("--------------------- Registered Suites -----------------------------")); - fprintf(stdout, "\n%*s %-*s%*s%*s%*s%*s\n", - width[0], _("#"), - width[1], _("Suite Name"), - width[2], _("Init?"), - width[3], _("Cleanup?"), - width[4], _("#Tests"), - width[5], _("Active?")); - - for (i = 1, pCurSuite = pRegistry->pSuite; (NULL != pCurSuite); pCurSuite = pCurSuite->pNext, ++i) { - assert(NULL != pCurSuite->pName); - fprintf(stdout, "\n%*d. %-*.*s%*s%*s%*u%*s", - width[0], i, - width[1], width[1] - 1, pCurSuite->pName, - width[2]-1, (NULL != pCurSuite->pInitializeFunc) ? _("Yes") : _("No"), - width[3], (NULL != pCurSuite->pCleanupFunc) ? _("Yes") : _("No"), - width[4], pCurSuite->uiNumberOfTests, - width[5], (CU_FALSE != pCurSuite->fActive) ? _("Yes") : _("No")); - } - fprintf(stdout, "\n---------------------------------------------------------------------\n"); - fprintf(stdout, _("Total Number of Suites : %-u"), pRegistry->uiNumberOfSuites); - fprintf(stdout, "\n"); -} - -/*------------------------------------------------------------------------*/ -/** - * Lists the tests in a suite to standard output. - * @param pSuite The suite to query (non-NULL). - */ -static void list_tests(CU_pSuite pSuite) -{ - CU_pTest pCurTest = NULL; - unsigned int uiCount; - static int width[3]; - - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - if (0 == pSuite->uiNumberOfTests) { - fprintf(stdout, "\n"); - fprintf(stdout, _("Suite %s contains no tests."), pSuite->pName); - fprintf(stdout, "\n"); - return; - } - - assert(NULL != pSuite->pTest); - - /* only number of tests can change between calls */ - width[0] = CU_number_width(pSuite->uiNumberOfTests) + 1; - if (0 == width[1]) { - width[1] = 34; - width[2] = CU_MAX(strlen(_("Active?")), CU_MAX(f_yes_width, f_no_width)) + 1; - } - - fprintf(stdout, "\n%s", - _("----------------- Test List ------------------------------")); - fprintf(stdout, "\n%s%s\n", _("Suite: "), pSuite->pName); - fprintf(stdout, "\n%*s %-*s%*s\n", - width[0], _("#"), - width[1], _("Test Name"), - width[2], _("Active?")); - - for (uiCount = 1, pCurTest = pSuite->pTest ; - NULL != pCurTest ; - uiCount++, pCurTest = pCurTest->pNext) { - assert(NULL != pCurTest->pName); - fprintf(stdout, "\n%*u. %-*.*s%*s", - width[0], uiCount, - width[1], width[1]-1, pCurTest->pName, - width[2]-1, (CU_FALSE != pCurTest->fActive) ? _("Yes") : _("No")); - } - fprintf(stdout, "\n----------------------------------------------------------\n"); - fprintf(stdout, _("Total Number of Tests : %-u"), pSuite->uiNumberOfTests); - fprintf(stdout, "\n"); -} - -/*------------------------------------------------------------------------*/ -/** Displays the record of test failures on standard output. */ -static void show_failures(void) -{ - unsigned int i; - CU_pFailureRecord pFailure = CU_get_failure_list(); - - if (NULL == pFailure) { - fprintf(stdout, "\n%s\n", _("No failures.")); - } - else { - - fprintf(stdout, "\n%s", - _("--------------- Test Run Failures -------------------------")); - fprintf(stdout, "\n%s\n", - _(" src_file:line# : (suite:test) : failure_condition")); - - for (i = 1 ; (NULL != pFailure) ; pFailure = pFailure->pNext, i++) { - fprintf(stdout, "\n%d. %s:%u : (%s : %s) : %s", i, - (NULL != pFailure->strFileName) - ? pFailure->strFileName : "", - pFailure->uiLineNumber, - ((NULL != pFailure->pSuite) && (NULL != pFailure->pSuite->pName)) - ? pFailure->pSuite->pName : "", - ((NULL != pFailure->pTest) && (NULL != pFailure->pTest->pName)) - ? pFailure->pTest->pName : "", - (NULL != pFailure->strCondition) - ? pFailure->strCondition : ""); - } - fprintf(stdout, "\n-----------------------------------------------------------"); - fprintf(stdout, "\n"); - fprintf(stdout, _("Total Number of Failures : %-u"), i - 1); - fprintf(stdout, "\n"); - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at start of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - */ -static void console_test_start_message_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - assert(NULL != pTest); - assert(NULL != pTest->pName); - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - /* Comparing the Addresses rather than the Group Names. */ - if ((NULL == f_pRunningSuite) || (f_pRunningSuite != pSuite)) { - fprintf(stdout, _("\nRunning Suite : %s"), pSuite->pName); - fprintf(stdout, _("\n Running Test : %s"), pTest->pName); - f_pRunningSuite = pSuite; - } - else { - fprintf(stdout, _("\n Running Test : %s"), pTest->pName); - } -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of each test. - * @param pTest The test being run. - * @param pSuite The suite containing the test. - * @param pFailure Pointer to the 1st failure record for this test. - */ -static void console_test_complete_message_handler(const CU_pTest pTest, - const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - /* - * For console interface do nothing. This is useful only for the test - * interface where UI is involved. Just silence compiler warnings. - */ - CU_UNREFERENCED_PARAMETER(pTest); - CU_UNREFERENCED_PARAMETER(pSuite); - CU_UNREFERENCED_PARAMETER(pFailure); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called at completion of all tests in a suite. - * @param pFailure Pointer to the test failure record list. - */ -static void console_all_tests_complete_message_handler(const CU_pFailureRecord pFailure) -{ - CU_UNREFERENCED_PARAMETER(pFailure); /* not used in console interface */ - printf("\n\n"); - CU_print_run_results(stdout); - printf("\n"); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite initialization fails. - * @param pSuite The suite for which initialization failed. - */ -static void console_suite_init_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - fprintf(stdout, - _("\nWARNING - Suite initialization failed for '%s'."), pSuite->pName); -} - -/*------------------------------------------------------------------------*/ -/** Handler function called when suite cleanup fails. - * @param pSuite The suite for which cleanup failed. - */ -static void console_suite_cleanup_failure_message_handler(const CU_pSuite pSuite) -{ - assert(NULL != pSuite); - assert(NULL != pSuite->pName); - - fprintf(stdout, _("\nWARNING - Suite cleanup failed for '%s'."), pSuite->pName); -} - -/** @} */ diff --git a/gearsyncd/tests/cunit/Console.h b/gearsyncd/tests/cunit/Console.h deleted file mode 100644 index f5b37695f3..0000000000 --- a/gearsyncd/tests/cunit/Console.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Contains Interface for console Run tests. - * - * Aug 2001 Initial implementation. (AK) - * - * 09/Aug/2001 Single interface to Console_run_tests. (AK) - * - * 20-Jul-2004 New interface, doxygen comments. (JDS) - */ - -/** @file - * Console interface with interactive output (user interface). - */ -/** @addtogroup Console - * @{ - */ - -#ifndef CUNIT_CONSOLE_H_SEEN -#define CUNIT_CONSOLE_H_SEEN - -#include "CUnit.h" -#include "TestDB.h" - -#ifdef __cplusplus -extern "C" { -#endif - -CU_EXPORT void CU_console_run_tests(void); -/**< Run registered CUnit tests using the console interface. */ - -#ifdef USE_DEPRECATED_CUNIT_NAMES -/** Deprecated (version 1). @deprecated Use CU_console_run_tests(). */ -#define console_run_tests() CU_console_run_tests() -#endif /* USE_DEPRECATED_CUNIT_NAMES */ - -#ifdef __cplusplus -} -#endif -#endif /* CUNIT_CONSOLE_H_SEEN */ -/** @} */ diff --git a/gearsyncd/tests/cunit/MyMem.c b/gearsyncd/tests/cunit/MyMem.c deleted file mode 100644 index 7ff8a3af9b..0000000000 --- a/gearsyncd/tests/cunit/MyMem.c +++ /dev/null @@ -1,585 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Memory management functions used throughout CUnit. - * - * 13/Oct/2001 Moved some of the generic functions definitions from other - * files to this one so as to use the functions consitently. - * This file is not included in the distribution headers because - * it is used internally by CUnit. (AK) - * - * 18-Jul-2004 New interface, doxygen comments, made local functions & - * constants static, fixed reporting of memory tracking (valid - * vs invalid cycles), restructured memory tracking to detect - * reallocations & multiple deletions. (JDS) - * - * 24-Apr-2005 Changed type of allocated sizes to size_t to avoid - * signed-unsigned mismatch. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Memory management & reporting functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "MyMem.h" -#include "CUnit_intl.h" - -#ifdef MEMTRACE - -#define MAX_FILE_NAME_LENGTH 256 - -/** Default name for memory dump file. */ -static const char* f_szDefaultDumpFileName = "CUnit-Memory-Dump.xml"; -/**< Default name for memory dump file. */ - -#ifdef CUNIT_BUILD_TESTS -/** For testing use (only) to simulate memory exhaustion - - * if CU_FALSE, allocation requests will always fail and return NULL. - */ -static CU_BOOL f_bTestCunitMallocActive = CU_TRUE; -#endif - -/** Structure holding the details of a memory allocation/deallocation event. */ -typedef struct mem_event { - size_t Size; - unsigned int AllocLine; - char AllocFilename[MAX_FILE_NAME_LENGTH]; - unsigned int DeallocLine; - char DeallocFilename[MAX_FILE_NAME_LENGTH]; - struct mem_event* pNext; -} MEMORY_EVENT; -typedef MEMORY_EVENT* PMEMORY_EVENT; - -#define NOT_ALLOCATED 0 -#define NOT_DELETED 0 - -/** Structure holding the details of a memory node having allocation/deallocation events. */ -typedef struct mem_node { - void* pLocation; - unsigned int EventCount; - PMEMORY_EVENT pFirstEvent; - struct mem_node* pNext; -} MEMORY_NODE; -typedef MEMORY_NODE* PMEMORY_NODE; - -static PMEMORY_NODE f_pMemoryTrackerHead = NULL; /**< Head of double-linked list of memory nodes. */ -static unsigned int f_nMemoryNodes = 0; /**< Counter for memory nodes created. */ -/*------------------------------------------------------------------------*/ -/** Locate the memory node for the specified memory location (returns NULL if none). */ -static PMEMORY_NODE find_memory_node(void* pLocation) -{ - PMEMORY_NODE pMemoryNode = f_pMemoryTrackerHead; - while (NULL != pMemoryNode) { - if (pLocation == pMemoryNode->pLocation) { - break; - } - pMemoryNode = pMemoryNode->pNext; - } - - return pMemoryNode; -} -/*------------------------------------------------------------------------*/ -/** Create a new memory node for the specified memory location. */ -static PMEMORY_NODE create_memory_node(void* pLocation) -{ - PMEMORY_NODE pTempNode = NULL; - PMEMORY_NODE pMemoryNode = find_memory_node(pLocation); - - /* a memory node for pLocation should not exist yet */ - if (NULL == pMemoryNode) { - - pMemoryNode = (PMEMORY_NODE)malloc(sizeof(MEMORY_NODE)); - assert(NULL != pMemoryNode); - - pMemoryNode->pLocation = pLocation; - pMemoryNode->EventCount = 0; - pMemoryNode->pFirstEvent = NULL; - pMemoryNode->pNext = NULL; - - /* add new node to linked list */ - pTempNode = f_pMemoryTrackerHead; - if (NULL == pTempNode) { - f_pMemoryTrackerHead = pMemoryNode; - } - else { - while (NULL != pTempNode->pNext) { - pTempNode = pTempNode->pNext; - } - pTempNode->pNext = pMemoryNode; - } - - ++f_nMemoryNodes; - } - return pMemoryNode; -} -/*------------------------------------------------------------------------*/ -/** Add a new memory event having the specified parameters. */ -static PMEMORY_EVENT add_memory_event(PMEMORY_NODE pMemoryNode, - size_t size, - unsigned int alloc_line, - const char* alloc_filename) -{ - PMEMORY_EVENT pMemoryEvent = NULL; - PMEMORY_EVENT pTempEvent = NULL; - - assert (NULL != pMemoryNode); - - /* create and set up the new event */ - pMemoryEvent = (PMEMORY_EVENT)malloc(sizeof(MEMORY_EVENT)); - assert(NULL != pMemoryEvent); - - pMemoryEvent->Size = size; - pMemoryEvent->AllocLine = alloc_line; - strncpy(pMemoryEvent->AllocFilename, alloc_filename, (size_t) MAX_FILE_NAME_LENGTH-1); - pMemoryEvent->AllocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; - pMemoryEvent->DeallocLine = NOT_DELETED; - pMemoryEvent->DeallocFilename[0] = (char)0; - pMemoryEvent->pNext = NULL; - - /* add the new event to the end of the linked list */ - pTempEvent = pMemoryNode->pFirstEvent; - if (NULL == pTempEvent) { - pMemoryNode->pFirstEvent = pMemoryEvent; - } - else { - while (NULL != pTempEvent->pNext) { - pTempEvent = pTempEvent->pNext; - } - pTempEvent->pNext = pMemoryEvent; - } - - ++pMemoryNode->EventCount; - - return pMemoryEvent; -} -/*------------------------------------------------------------------------*/ -/** Record memory allocation event. */ -static PMEMORY_NODE allocate_memory(size_t nSize, - void* pLocation, - unsigned int uiAllocationLine, - const char* szAllocationFile) -{ - PMEMORY_NODE pMemoryNode = NULL; - - /* attempt to locate an existing record for this pLocation */ - pMemoryNode = find_memory_node(pLocation); - - /* pLocation not found - create a new event record */ - if (NULL == pMemoryNode) { - pMemoryNode = create_memory_node(pLocation); - } - - /* add the new event record */ - add_memory_event(pMemoryNode, nSize, uiAllocationLine, szAllocationFile); - - return pMemoryNode; -} - -/*------------------------------------------------------------------------*/ -/** Record memory deallocation event. */ -static void deallocate_memory(void* pLocation, unsigned int uiDeletionLine, const char* szDeletionFileName) -{ - PMEMORY_NODE pMemoryNode = NULL; - PMEMORY_EVENT pTempEvent = NULL; - - assert(0 != uiDeletionLine); - assert(NULL != szDeletionFileName); - - /* attempt to locate an existing record for this pLocation */ - pMemoryNode = find_memory_node(pLocation); - - /* if no entry, then an unallocated pointer was freed */ - if (NULL == pMemoryNode) { - pMemoryNode = create_memory_node(pLocation); - pTempEvent = add_memory_event(pMemoryNode, 0, NOT_ALLOCATED, ""); - } - else { - /* there should always be at least 1 event for an existing memory node */ - assert(NULL != pMemoryNode->pFirstEvent); - - /* locate last memory event for this pLocation */ - pTempEvent = pMemoryNode->pFirstEvent; - while (NULL != pTempEvent->pNext) { - pTempEvent = pTempEvent->pNext; - } - - /* if pointer has already been freed, create a new event for double deletion */ - if (NOT_DELETED != pTempEvent->DeallocLine) { - pTempEvent = add_memory_event(pMemoryNode, pTempEvent->Size, NOT_ALLOCATED, ""); - } - } - - pTempEvent->DeallocLine = uiDeletionLine; - strncpy(pTempEvent->DeallocFilename, szDeletionFileName, MAX_FILE_NAME_LENGTH-1); - pTempEvent->DeallocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; -} - -/*------------------------------------------------------------------------*/ -/** Custom calloc function with memory event recording. */ -void* CU_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName) -{ - void* pVoid = NULL; - -#ifdef CUNIT_BUILD_TESTS - if (CU_FALSE == f_bTestCunitMallocActive) { - return NULL; - } -#endif - - pVoid = calloc(nmemb, size); - if (NULL != pVoid) { - allocate_memory(nmemb * size, pVoid, uiLine, szFileName); - } - - return pVoid; -} - -/*------------------------------------------------------------------------*/ -/** Custom malloc function with memory event recording. */ -void* CU_malloc(size_t size, unsigned int uiLine, const char* szFileName) -{ - void* pVoid = NULL; - -#ifdef CUNIT_BUILD_TESTS - if (CU_FALSE == f_bTestCunitMallocActive) { - return NULL; - } -#endif - - pVoid = malloc(size); - if (NULL != pVoid) { - allocate_memory(size, pVoid, uiLine, szFileName); - } - - return pVoid; -} - -/*------------------------------------------------------------------------*/ -/** Custom free function with memory event recording. */ -void CU_free(void *ptr, unsigned int uiLine, const char* szFileName) -{ - deallocate_memory(ptr, uiLine, szFileName); - free(ptr); -} - -/*------------------------------------------------------------------------*/ -/** Custom realloc function with memory event recording. */ -void* CU_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName) -{ - void* pVoid = NULL; - - deallocate_memory(ptr, uiLine, szFileName); - -#ifdef CUNIT_BUILD_TESTS - if (CU_FALSE == f_bTestCunitMallocActive) { - free(ptr); - return NULL; - } -#endif - - pVoid = realloc(ptr, size); - - if (NULL != pVoid) { - allocate_memory(size, pVoid, uiLine, szFileName); - } - - return pVoid; -} - -/*------------------------------------------------------------------------*/ -/** Print a report of memory events to file. */ -void CU_dump_memory_usage(const char* szFilename) -{ - char* szDumpFileName = (char*)f_szDefaultDumpFileName; - unsigned int nValid; - unsigned int nInvalid; - PMEMORY_NODE pTempNode = NULL; - PMEMORY_EVENT pTempEvent = NULL; - FILE* pFile = NULL; - time_t tTime = 0; - - /* use the specified file name, if supplied) */ - if ((NULL != szFilename) && strlen(szFilename) > 0) { - szDumpFileName = (char*)szFilename; - } - - if (NULL == (pFile = fopen(szDumpFileName, "w"))) { - fprintf(stderr, _("Failed to open file \"%s\" : %s"), szDumpFileName, strerror(errno)); - return; - } - - setvbuf(pFile, NULL, _IONBF, 0); - - fprintf(pFile, "<\?xml version=\"1.0\" \?>"); - fprintf(pFile, "\n<\?xml-stylesheet type=\"text/xsl\" href=\"Memory-Dump.xsl\" \?>"); - fprintf(pFile, "\n"); - fprintf(pFile, "\n"); - fprintf(pFile, "\n "); - fprintf(pFile, "\n "); - - nValid = 0; - nInvalid = 0; - pTempNode = f_pMemoryTrackerHead; - while (NULL != pTempNode) { - fprintf(pFile, "\n "); - fprintf(pFile, "\n %p ", pTempNode->pLocation); - fprintf(pFile, "\n %u ", pTempNode->EventCount); - - pTempEvent = pTempNode->pFirstEvent; - while (NULL != pTempEvent) { - fprintf(pFile, "\n "); - fprintf(pFile, "\n %u ", pTempEvent->Size); - fprintf(pFile, "\n %s ", pTempEvent->AllocFilename); - fprintf(pFile, "\n %u ", pTempEvent->AllocLine); - fprintf(pFile, "\n %s ", pTempEvent->DeallocFilename); - fprintf(pFile, "\n %u ", pTempEvent->DeallocLine); - fprintf(pFile, "\n "); - - if ((0 != pTempEvent->AllocLine) && (0 != pTempEvent->DeallocLine)) { - ++nValid; - } - else { - ++nInvalid; - } - - pTempEvent = pTempEvent->pNext; - } - - fprintf(pFile, "\n "); - pTempNode = pTempNode->pNext; - } - - fprintf(pFile, "\n "); - - fprintf(pFile, "\n "); - fprintf(pFile, "\n %u ", nValid); - fprintf(pFile, "\n %u ", nInvalid); - fprintf(pFile, "\n %u ", nValid + nInvalid); - fprintf(pFile, "\n "); - - time(&tTime); - fprintf(pFile, "\n Memory Trace for CUnit Run at %s ", ctime(&tTime)); - fprintf(pFile, ""); - - fclose(pFile); -} - -#endif /* MEMTRACE */ - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -/** Deactivate CUnit memory allocation - * After calling this function, all Cunit memory - * allocation routines will fail and return NULL. - */ -void test_cunit_deactivate_malloc(void) -{ - f_bTestCunitMallocActive = CU_FALSE; -} - -/** Activate CUnit memory allocation - * After calling this function, all Cunit memory - * allocation routines will behave normally (allocating - * memory if it is available). - */ -void test_cunit_activate_malloc(void) -{ - f_bTestCunitMallocActive = CU_TRUE; -} - -/** Retrieve the number of memory events recorded for a given pointer. */ -unsigned int test_cunit_get_n_memevents(void* pLocation) -{ - PMEMORY_NODE pNode = find_memory_node(pLocation); - return (pNode) ? pNode->EventCount : 0; -} - -/** Retrieve the number of memory allocations recorded for a given pointer. */ -unsigned int test_cunit_get_n_allocations(void* pLocation) -{ - PMEMORY_NODE pNode = find_memory_node(pLocation); - PMEMORY_EVENT pEvent = NULL; - int result = 0; - - if (NULL != pNode) { - pEvent = pNode->pFirstEvent; - while (NULL != pEvent) { - if (pEvent->AllocLine != NOT_ALLOCATED) - ++result; - pEvent = pEvent->pNext; - } - } - - return result; -} - -/** Retrieve the number of memory deallocations recorded for a given pointer. */ -unsigned int test_cunit_get_n_deallocations(void* pLocation) -{ - PMEMORY_NODE pNode = find_memory_node(pLocation); - PMEMORY_EVENT pEvent = NULL; - int result = 0; - - if (NULL != pNode) { - pEvent = pNode->pFirstEvent; - while (NULL != pEvent) { - if (pEvent->DeallocLine != NOT_DELETED) - ++result; - pEvent = pEvent->pNext; - } - } - - return result; -} - -void test_CU_calloc(void) -{ - void* ptr1 = NULL; - void* ptr2 = calloc(2, sizeof(int)); - unsigned int n2 = test_cunit_get_n_memevents(ptr2); - - /* test allocation failure */ - test_cunit_deactivate_malloc(); - ptr1 = CU_CALLOC(2, sizeof(int)); - TEST(NULL == ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - test_cunit_activate_malloc(); - - /* normal allocation */ - ptr1 = CU_CALLOC(2, sizeof(int)); - TEST_FATAL(NULL != ptr1); - TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - - CU_FREE(ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(n2 == test_cunit_get_n_memevents(ptr2)); - - free(ptr2); -} - -void test_CU_malloc(void) -{ - void* ptr1 = NULL; - void* ptr2 = malloc(sizeof(int)); - unsigned int n2 = test_cunit_get_n_memevents(ptr2); - - /* test allocation failure */ - test_cunit_deactivate_malloc(); - ptr1 = CU_MALLOC(sizeof(int)); - TEST(NULL == ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - test_cunit_activate_malloc(); - - /* normal allocation */ - ptr1 = CU_MALLOC(sizeof(int)); - TEST_FATAL(NULL != ptr1); - TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - - CU_FREE(ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(n2 == test_cunit_get_n_memevents(ptr2)); - - free(ptr2); -} - -void test_CU_free(void) -{ - /* covered by other test functions */ -} - -void test_CU_realloc(void) -{ - void* ptr1 = CU_MALLOC(sizeof(int)); - void* ptr2 = malloc(sizeof(int)); - void* ptr3; - void* ptr4; - unsigned int n2 = test_cunit_get_n_memevents(ptr2); - - /* test allocation failure */ - test_cunit_deactivate_malloc(); - ptr1 = CU_REALLOC(ptr1, sizeof(long int)); - TEST(NULL == ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - test_cunit_activate_malloc(); - - /* normal allocation */ - ptr3 = CU_MALLOC(sizeof(int)); - TEST_FATAL(NULL != ptr3); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(test_cunit_get_n_allocations(ptr3) != test_cunit_get_n_deallocations(ptr3)); - - ptr4 = CU_REALLOC(ptr3, sizeof(long int)); - TEST_FATAL(NULL != ptr4); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - if (ptr3 != ptr4) - TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); - TEST(test_cunit_get_n_allocations(ptr4) != test_cunit_get_n_deallocations(ptr4)); - - CU_FREE(ptr4); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); - TEST(test_cunit_get_n_allocations(ptr4) == test_cunit_get_n_deallocations(ptr4)); - TEST(n2 == test_cunit_get_n_memevents(ptr2)); - - free(ptr2); -} - -/** The main internal testing function for MyMem.c. */ -void test_cunit_MyMem(void) -{ - test_cunit_start_tests("MyMem.c"); - - test_CU_calloc(); - test_CU_malloc(); - test_CU_free(); - test_CU_realloc(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/MyMem.cpp b/gearsyncd/tests/cunit/MyMem.cpp deleted file mode 100644 index 7ff8a3af9b..0000000000 --- a/gearsyncd/tests/cunit/MyMem.cpp +++ /dev/null @@ -1,585 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Memory management functions used throughout CUnit. - * - * 13/Oct/2001 Moved some of the generic functions definitions from other - * files to this one so as to use the functions consitently. - * This file is not included in the distribution headers because - * it is used internally by CUnit. (AK) - * - * 18-Jul-2004 New interface, doxygen comments, made local functions & - * constants static, fixed reporting of memory tracking (valid - * vs invalid cycles), restructured memory tracking to detect - * reallocations & multiple deletions. (JDS) - * - * 24-Apr-2005 Changed type of allocated sizes to size_t to avoid - * signed-unsigned mismatch. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Memory management & reporting functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "MyMem.h" -#include "CUnit_intl.h" - -#ifdef MEMTRACE - -#define MAX_FILE_NAME_LENGTH 256 - -/** Default name for memory dump file. */ -static const char* f_szDefaultDumpFileName = "CUnit-Memory-Dump.xml"; -/**< Default name for memory dump file. */ - -#ifdef CUNIT_BUILD_TESTS -/** For testing use (only) to simulate memory exhaustion - - * if CU_FALSE, allocation requests will always fail and return NULL. - */ -static CU_BOOL f_bTestCunitMallocActive = CU_TRUE; -#endif - -/** Structure holding the details of a memory allocation/deallocation event. */ -typedef struct mem_event { - size_t Size; - unsigned int AllocLine; - char AllocFilename[MAX_FILE_NAME_LENGTH]; - unsigned int DeallocLine; - char DeallocFilename[MAX_FILE_NAME_LENGTH]; - struct mem_event* pNext; -} MEMORY_EVENT; -typedef MEMORY_EVENT* PMEMORY_EVENT; - -#define NOT_ALLOCATED 0 -#define NOT_DELETED 0 - -/** Structure holding the details of a memory node having allocation/deallocation events. */ -typedef struct mem_node { - void* pLocation; - unsigned int EventCount; - PMEMORY_EVENT pFirstEvent; - struct mem_node* pNext; -} MEMORY_NODE; -typedef MEMORY_NODE* PMEMORY_NODE; - -static PMEMORY_NODE f_pMemoryTrackerHead = NULL; /**< Head of double-linked list of memory nodes. */ -static unsigned int f_nMemoryNodes = 0; /**< Counter for memory nodes created. */ -/*------------------------------------------------------------------------*/ -/** Locate the memory node for the specified memory location (returns NULL if none). */ -static PMEMORY_NODE find_memory_node(void* pLocation) -{ - PMEMORY_NODE pMemoryNode = f_pMemoryTrackerHead; - while (NULL != pMemoryNode) { - if (pLocation == pMemoryNode->pLocation) { - break; - } - pMemoryNode = pMemoryNode->pNext; - } - - return pMemoryNode; -} -/*------------------------------------------------------------------------*/ -/** Create a new memory node for the specified memory location. */ -static PMEMORY_NODE create_memory_node(void* pLocation) -{ - PMEMORY_NODE pTempNode = NULL; - PMEMORY_NODE pMemoryNode = find_memory_node(pLocation); - - /* a memory node for pLocation should not exist yet */ - if (NULL == pMemoryNode) { - - pMemoryNode = (PMEMORY_NODE)malloc(sizeof(MEMORY_NODE)); - assert(NULL != pMemoryNode); - - pMemoryNode->pLocation = pLocation; - pMemoryNode->EventCount = 0; - pMemoryNode->pFirstEvent = NULL; - pMemoryNode->pNext = NULL; - - /* add new node to linked list */ - pTempNode = f_pMemoryTrackerHead; - if (NULL == pTempNode) { - f_pMemoryTrackerHead = pMemoryNode; - } - else { - while (NULL != pTempNode->pNext) { - pTempNode = pTempNode->pNext; - } - pTempNode->pNext = pMemoryNode; - } - - ++f_nMemoryNodes; - } - return pMemoryNode; -} -/*------------------------------------------------------------------------*/ -/** Add a new memory event having the specified parameters. */ -static PMEMORY_EVENT add_memory_event(PMEMORY_NODE pMemoryNode, - size_t size, - unsigned int alloc_line, - const char* alloc_filename) -{ - PMEMORY_EVENT pMemoryEvent = NULL; - PMEMORY_EVENT pTempEvent = NULL; - - assert (NULL != pMemoryNode); - - /* create and set up the new event */ - pMemoryEvent = (PMEMORY_EVENT)malloc(sizeof(MEMORY_EVENT)); - assert(NULL != pMemoryEvent); - - pMemoryEvent->Size = size; - pMemoryEvent->AllocLine = alloc_line; - strncpy(pMemoryEvent->AllocFilename, alloc_filename, (size_t) MAX_FILE_NAME_LENGTH-1); - pMemoryEvent->AllocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; - pMemoryEvent->DeallocLine = NOT_DELETED; - pMemoryEvent->DeallocFilename[0] = (char)0; - pMemoryEvent->pNext = NULL; - - /* add the new event to the end of the linked list */ - pTempEvent = pMemoryNode->pFirstEvent; - if (NULL == pTempEvent) { - pMemoryNode->pFirstEvent = pMemoryEvent; - } - else { - while (NULL != pTempEvent->pNext) { - pTempEvent = pTempEvent->pNext; - } - pTempEvent->pNext = pMemoryEvent; - } - - ++pMemoryNode->EventCount; - - return pMemoryEvent; -} -/*------------------------------------------------------------------------*/ -/** Record memory allocation event. */ -static PMEMORY_NODE allocate_memory(size_t nSize, - void* pLocation, - unsigned int uiAllocationLine, - const char* szAllocationFile) -{ - PMEMORY_NODE pMemoryNode = NULL; - - /* attempt to locate an existing record for this pLocation */ - pMemoryNode = find_memory_node(pLocation); - - /* pLocation not found - create a new event record */ - if (NULL == pMemoryNode) { - pMemoryNode = create_memory_node(pLocation); - } - - /* add the new event record */ - add_memory_event(pMemoryNode, nSize, uiAllocationLine, szAllocationFile); - - return pMemoryNode; -} - -/*------------------------------------------------------------------------*/ -/** Record memory deallocation event. */ -static void deallocate_memory(void* pLocation, unsigned int uiDeletionLine, const char* szDeletionFileName) -{ - PMEMORY_NODE pMemoryNode = NULL; - PMEMORY_EVENT pTempEvent = NULL; - - assert(0 != uiDeletionLine); - assert(NULL != szDeletionFileName); - - /* attempt to locate an existing record for this pLocation */ - pMemoryNode = find_memory_node(pLocation); - - /* if no entry, then an unallocated pointer was freed */ - if (NULL == pMemoryNode) { - pMemoryNode = create_memory_node(pLocation); - pTempEvent = add_memory_event(pMemoryNode, 0, NOT_ALLOCATED, ""); - } - else { - /* there should always be at least 1 event for an existing memory node */ - assert(NULL != pMemoryNode->pFirstEvent); - - /* locate last memory event for this pLocation */ - pTempEvent = pMemoryNode->pFirstEvent; - while (NULL != pTempEvent->pNext) { - pTempEvent = pTempEvent->pNext; - } - - /* if pointer has already been freed, create a new event for double deletion */ - if (NOT_DELETED != pTempEvent->DeallocLine) { - pTempEvent = add_memory_event(pMemoryNode, pTempEvent->Size, NOT_ALLOCATED, ""); - } - } - - pTempEvent->DeallocLine = uiDeletionLine; - strncpy(pTempEvent->DeallocFilename, szDeletionFileName, MAX_FILE_NAME_LENGTH-1); - pTempEvent->DeallocFilename[MAX_FILE_NAME_LENGTH-1] = (char)0; -} - -/*------------------------------------------------------------------------*/ -/** Custom calloc function with memory event recording. */ -void* CU_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName) -{ - void* pVoid = NULL; - -#ifdef CUNIT_BUILD_TESTS - if (CU_FALSE == f_bTestCunitMallocActive) { - return NULL; - } -#endif - - pVoid = calloc(nmemb, size); - if (NULL != pVoid) { - allocate_memory(nmemb * size, pVoid, uiLine, szFileName); - } - - return pVoid; -} - -/*------------------------------------------------------------------------*/ -/** Custom malloc function with memory event recording. */ -void* CU_malloc(size_t size, unsigned int uiLine, const char* szFileName) -{ - void* pVoid = NULL; - -#ifdef CUNIT_BUILD_TESTS - if (CU_FALSE == f_bTestCunitMallocActive) { - return NULL; - } -#endif - - pVoid = malloc(size); - if (NULL != pVoid) { - allocate_memory(size, pVoid, uiLine, szFileName); - } - - return pVoid; -} - -/*------------------------------------------------------------------------*/ -/** Custom free function with memory event recording. */ -void CU_free(void *ptr, unsigned int uiLine, const char* szFileName) -{ - deallocate_memory(ptr, uiLine, szFileName); - free(ptr); -} - -/*------------------------------------------------------------------------*/ -/** Custom realloc function with memory event recording. */ -void* CU_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName) -{ - void* pVoid = NULL; - - deallocate_memory(ptr, uiLine, szFileName); - -#ifdef CUNIT_BUILD_TESTS - if (CU_FALSE == f_bTestCunitMallocActive) { - free(ptr); - return NULL; - } -#endif - - pVoid = realloc(ptr, size); - - if (NULL != pVoid) { - allocate_memory(size, pVoid, uiLine, szFileName); - } - - return pVoid; -} - -/*------------------------------------------------------------------------*/ -/** Print a report of memory events to file. */ -void CU_dump_memory_usage(const char* szFilename) -{ - char* szDumpFileName = (char*)f_szDefaultDumpFileName; - unsigned int nValid; - unsigned int nInvalid; - PMEMORY_NODE pTempNode = NULL; - PMEMORY_EVENT pTempEvent = NULL; - FILE* pFile = NULL; - time_t tTime = 0; - - /* use the specified file name, if supplied) */ - if ((NULL != szFilename) && strlen(szFilename) > 0) { - szDumpFileName = (char*)szFilename; - } - - if (NULL == (pFile = fopen(szDumpFileName, "w"))) { - fprintf(stderr, _("Failed to open file \"%s\" : %s"), szDumpFileName, strerror(errno)); - return; - } - - setvbuf(pFile, NULL, _IONBF, 0); - - fprintf(pFile, "<\?xml version=\"1.0\" \?>"); - fprintf(pFile, "\n<\?xml-stylesheet type=\"text/xsl\" href=\"Memory-Dump.xsl\" \?>"); - fprintf(pFile, "\n"); - fprintf(pFile, "\n"); - fprintf(pFile, "\n "); - fprintf(pFile, "\n "); - - nValid = 0; - nInvalid = 0; - pTempNode = f_pMemoryTrackerHead; - while (NULL != pTempNode) { - fprintf(pFile, "\n "); - fprintf(pFile, "\n %p ", pTempNode->pLocation); - fprintf(pFile, "\n %u ", pTempNode->EventCount); - - pTempEvent = pTempNode->pFirstEvent; - while (NULL != pTempEvent) { - fprintf(pFile, "\n "); - fprintf(pFile, "\n %u ", pTempEvent->Size); - fprintf(pFile, "\n %s ", pTempEvent->AllocFilename); - fprintf(pFile, "\n %u ", pTempEvent->AllocLine); - fprintf(pFile, "\n %s ", pTempEvent->DeallocFilename); - fprintf(pFile, "\n %u ", pTempEvent->DeallocLine); - fprintf(pFile, "\n "); - - if ((0 != pTempEvent->AllocLine) && (0 != pTempEvent->DeallocLine)) { - ++nValid; - } - else { - ++nInvalid; - } - - pTempEvent = pTempEvent->pNext; - } - - fprintf(pFile, "\n "); - pTempNode = pTempNode->pNext; - } - - fprintf(pFile, "\n "); - - fprintf(pFile, "\n "); - fprintf(pFile, "\n %u ", nValid); - fprintf(pFile, "\n %u ", nInvalid); - fprintf(pFile, "\n %u ", nValid + nInvalid); - fprintf(pFile, "\n "); - - time(&tTime); - fprintf(pFile, "\n Memory Trace for CUnit Run at %s ", ctime(&tTime)); - fprintf(pFile, ""); - - fclose(pFile); -} - -#endif /* MEMTRACE */ - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -/** Deactivate CUnit memory allocation - * After calling this function, all Cunit memory - * allocation routines will fail and return NULL. - */ -void test_cunit_deactivate_malloc(void) -{ - f_bTestCunitMallocActive = CU_FALSE; -} - -/** Activate CUnit memory allocation - * After calling this function, all Cunit memory - * allocation routines will behave normally (allocating - * memory if it is available). - */ -void test_cunit_activate_malloc(void) -{ - f_bTestCunitMallocActive = CU_TRUE; -} - -/** Retrieve the number of memory events recorded for a given pointer. */ -unsigned int test_cunit_get_n_memevents(void* pLocation) -{ - PMEMORY_NODE pNode = find_memory_node(pLocation); - return (pNode) ? pNode->EventCount : 0; -} - -/** Retrieve the number of memory allocations recorded for a given pointer. */ -unsigned int test_cunit_get_n_allocations(void* pLocation) -{ - PMEMORY_NODE pNode = find_memory_node(pLocation); - PMEMORY_EVENT pEvent = NULL; - int result = 0; - - if (NULL != pNode) { - pEvent = pNode->pFirstEvent; - while (NULL != pEvent) { - if (pEvent->AllocLine != NOT_ALLOCATED) - ++result; - pEvent = pEvent->pNext; - } - } - - return result; -} - -/** Retrieve the number of memory deallocations recorded for a given pointer. */ -unsigned int test_cunit_get_n_deallocations(void* pLocation) -{ - PMEMORY_NODE pNode = find_memory_node(pLocation); - PMEMORY_EVENT pEvent = NULL; - int result = 0; - - if (NULL != pNode) { - pEvent = pNode->pFirstEvent; - while (NULL != pEvent) { - if (pEvent->DeallocLine != NOT_DELETED) - ++result; - pEvent = pEvent->pNext; - } - } - - return result; -} - -void test_CU_calloc(void) -{ - void* ptr1 = NULL; - void* ptr2 = calloc(2, sizeof(int)); - unsigned int n2 = test_cunit_get_n_memevents(ptr2); - - /* test allocation failure */ - test_cunit_deactivate_malloc(); - ptr1 = CU_CALLOC(2, sizeof(int)); - TEST(NULL == ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - test_cunit_activate_malloc(); - - /* normal allocation */ - ptr1 = CU_CALLOC(2, sizeof(int)); - TEST_FATAL(NULL != ptr1); - TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - - CU_FREE(ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(n2 == test_cunit_get_n_memevents(ptr2)); - - free(ptr2); -} - -void test_CU_malloc(void) -{ - void* ptr1 = NULL; - void* ptr2 = malloc(sizeof(int)); - unsigned int n2 = test_cunit_get_n_memevents(ptr2); - - /* test allocation failure */ - test_cunit_deactivate_malloc(); - ptr1 = CU_MALLOC(sizeof(int)); - TEST(NULL == ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - test_cunit_activate_malloc(); - - /* normal allocation */ - ptr1 = CU_MALLOC(sizeof(int)); - TEST_FATAL(NULL != ptr1); - TEST(test_cunit_get_n_allocations(ptr1) != test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - - CU_FREE(ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(n2 == test_cunit_get_n_memevents(ptr2)); - - free(ptr2); -} - -void test_CU_free(void) -{ - /* covered by other test functions */ -} - -void test_CU_realloc(void) -{ - void* ptr1 = CU_MALLOC(sizeof(int)); - void* ptr2 = malloc(sizeof(int)); - void* ptr3; - void* ptr4; - unsigned int n2 = test_cunit_get_n_memevents(ptr2); - - /* test allocation failure */ - test_cunit_deactivate_malloc(); - ptr1 = CU_REALLOC(ptr1, sizeof(long int)); - TEST(NULL == ptr1); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - test_cunit_activate_malloc(); - - /* normal allocation */ - ptr3 = CU_MALLOC(sizeof(int)); - TEST_FATAL(NULL != ptr3); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(test_cunit_get_n_allocations(ptr3) != test_cunit_get_n_deallocations(ptr3)); - - ptr4 = CU_REALLOC(ptr3, sizeof(long int)); - TEST_FATAL(NULL != ptr4); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - if (ptr3 != ptr4) - TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); - TEST(test_cunit_get_n_allocations(ptr4) != test_cunit_get_n_deallocations(ptr4)); - - CU_FREE(ptr4); - TEST(test_cunit_get_n_allocations(ptr1) == test_cunit_get_n_deallocations(ptr1)); - TEST(test_cunit_get_n_allocations(ptr2) == test_cunit_get_n_deallocations(ptr2)); - TEST(test_cunit_get_n_allocations(ptr3) == test_cunit_get_n_deallocations(ptr3)); - TEST(test_cunit_get_n_allocations(ptr4) == test_cunit_get_n_deallocations(ptr4)); - TEST(n2 == test_cunit_get_n_memevents(ptr2)); - - free(ptr2); -} - -/** The main internal testing function for MyMem.c. */ -void test_cunit_MyMem(void) -{ - test_cunit_start_tests("MyMem.c"); - - test_CU_calloc(); - test_CU_malloc(); - test_CU_free(); - test_CU_realloc(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/MyMem.h b/gearsyncd/tests/cunit/MyMem.h deleted file mode 100644 index 88a7e625e4..0000000000 --- a/gearsyncd/tests/cunit/MyMem.h +++ /dev/null @@ -1,104 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Contains Memory Related Defines to use internal routines to detect Memory Leak - * in Debug Versions - * - * 18/Jun/2002 Memory Debug Functions. (AK) - * - * 17-Jul-2004 New interface for global function names. (JDS) - * - * 05-Sep-2004 Added internal test interface. (JDS) - */ - -/** @file - * Memory management functions (user interface). - * Two versions of memory allocation/deallocation are available. - * If compiled with MEMTRACE defined, CUnit keeps track of all - * system allocations & deallocations. The memory record can - * then be reported using CU_CREATE_MEMORY_REPORT. Otherwise, - * standard system memory allocation is used without tracing. - */ -/** @addtogroup Framework - * @{ - */ - -#ifndef CUNIT_MYMEM_H_SEEN -#define CUNIT_MYMEM_H_SEEN - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef MEMTRACE - void* CU_calloc(size_t nmemb, size_t size, unsigned int uiLine, const char* szFileName); - void* CU_malloc(size_t size, unsigned int uiLine, const char* szFileName); - void CU_free(void *ptr, unsigned int uiLine, const char* szFileName); - void* CU_realloc(void *ptr, size_t size, unsigned int uiLine, const char* szFileName); - CU_EXPORT void CU_dump_memory_usage(const char*); - - /** c-allocate with memory tracking. */ - #define CU_CALLOC(x, y) CU_calloc((x), (y), __LINE__, __FILE__) - /** m-allocate with memory tracking. */ - #define CU_MALLOC(x) CU_malloc((x), __LINE__, __FILE__) - /** Free with memory tracking. */ - #define CU_FREE(x) CU_free((x), __LINE__, __FILE__) - /** Reallocate with memory tracking. */ - #define CU_REALLOC(x, y) CU_realloc((x), (y), __LINE__, __FILE__) - /** Generate report on tracked memory. */ - #define CU_CREATE_MEMORY_REPORT(x) CU_dump_memory_usage((x)) - /** Generate report on tracked memory (old macro). */ - #define CU_DUMP_MEMORY_USAGE(x) CU_dump_memory_usage((x)) -#else /* MEMTRACE */ - /** Standard calloc() if MEMTRACE not defined. */ - #define CU_CALLOC(x, y) calloc((x), (y)) - /** Standard malloc() if MEMTRACE not defined. */ - #define CU_MALLOC(x) malloc((x)) - /** Standard free() if MEMTRACE not defined. */ - #define CU_FREE(x) free((x)) - /** Standard realloc() if MEMTRACE not defined. */ - #define CU_REALLOC(x, y) realloc((x), (y)) - /** No-op if MEMTRACE not defined. */ - #define CU_CREATE_MEMORY_REPORT(x) - /** No-op if MEMTRACE not defined. */ - #define CU_DUMP_MEMORY_USAGE(x) -#endif /* MEMTRACE */ - -#ifdef CUNIT_BUILD_TESTS -/** Disable memory allocation for testing purposes. */ -void test_cunit_deactivate_malloc(void); -/** Enable memory allocation for testing purposes. */ -void test_cunit_activate_malloc(void); -/** Retrieve number of memory events for a given pointer */ -unsigned int test_cunit_get_n_memevents(void* pLocation); -/** Retrieve number of allocations for a given pointer */ -unsigned int test_cunit_get_n_allocations(void* pLocation); -/** Retrieve number of deallocations for a given pointer */ -unsigned int test_cunit_get_n_deallocations(void* pLocation); - -void test_cunit_MyMem(void); -#endif /* CUNIT_BUILD_TESTS */ - -#ifdef __cplusplus -} -#endif -#endif /* CUNIT_MYMEM_H_SEEN */ -/** @} */ diff --git a/gearsyncd/tests/cunit/TestDB.c b/gearsyncd/tests/cunit/TestDB.c deleted file mode 100644 index d609f50549..0000000000 --- a/gearsyncd/tests/cunit/TestDB.c +++ /dev/null @@ -1,2773 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004,2005,2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of Registry/TestGroup/Testcase management Routines. - * - * Aug 2001 Initial implementation (AK) - * - * 09/Aug/2001 Added startup initialize/cleanup registry functions. (AK) - * - * 29/Aug/2001 Added Test and Group Add functions. (AK) - * - * 02/Oct/2001 Added Proper Error codes and Messages on the failure conditions. (AK) - * - * 13/Oct/2001 Added Code to Check for the Duplicate Group name and test name. (AK) - * - * 15-Jul-2004 Added doxygen comments, new interface, added assertions to - * internal functions, moved error handling code to CUError.c, - * added assertions to make sure no modification of registry - * during a run, bug fixes, changed CU_set_registry() so that it - * doesn't require cleaning the existing registry. (JDS) - * - * 24-Apr-2006 Removed constraint that suites/tests be uniquely named. - * Added ability to turn individual tests/suites on or off. - * Added lookup functions for suites/tests based on index. - * Moved doxygen comments for public API here to header. - * Modified internal unit tests to include these changes. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Management functions for tests, suites, and the test registry (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "MyMem.h" -#include "TestDB.h" -#include "TestRun.h" -#include "Util.h" -#include "CUnit_intl.h" - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -static CU_pTestRegistry f_pTestRegistry = NULL; /**< The active internal Test Registry. */ - -/*================================================================= - * Private function forward declarations - *=================================================================*/ -static void cleanup_test_registry(CU_pTestRegistry pRegistry); -static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean); -static void cleanup_suite(CU_pSuite pSuite); -static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite); -static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc); -static void cleanup_test(CU_pTest pTest); -static void insert_test(CU_pSuite pSuite, CU_pTest pTest); - -static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName); -static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -CU_ErrorCode CU_initialize_registry(void) -{ - CU_ErrorCode result; - - assert(CU_FALSE == CU_is_test_running()); - - CU_set_error(result = CUE_SUCCESS); - - if (NULL != f_pTestRegistry) { - CU_cleanup_registry(); - } - - f_pTestRegistry = CU_create_new_registry(); - if (NULL == f_pTestRegistry) { - CU_set_error(result = CUE_NOMEMORY); - } - - return result; -} - -/*------------------------------------------------------------------------*/ -CU_BOOL CU_registry_initialized(void) -{ - return (NULL == f_pTestRegistry) ? CU_FALSE : CU_TRUE; -} - -/*------------------------------------------------------------------------*/ -void CU_cleanup_registry(void) -{ - assert(CU_FALSE == CU_is_test_running()); - - CU_set_error(CUE_SUCCESS); - CU_destroy_existing_registry(&f_pTestRegistry); /* supposed to handle NULL ok */ - CU_clear_previous_results(); - CU_CREATE_MEMORY_REPORT(NULL); -} - -/*------------------------------------------------------------------------*/ -CU_pTestRegistry CU_get_registry(void) -{ - return f_pTestRegistry; -} - -/*------------------------------------------------------------------------*/ -CU_pTestRegistry CU_set_registry(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = f_pTestRegistry; - - assert(CU_FALSE == CU_is_test_running()); - - CU_set_error(CUE_SUCCESS); - f_pTestRegistry = pRegistry; - return pOldRegistry; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) -{ - CU_pSuite pRetValue = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - assert(CU_FALSE == CU_is_test_running()); - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - pRetValue = create_suite(strName, pInit, pClean); - if (NULL == pRetValue) { - error = CUE_NOMEMORY; - } - else { - if (CU_TRUE == suite_exists(f_pTestRegistry, strName)) { - error = CUE_DUP_SUITE; - } - insert_suite(f_pTestRegistry, pRetValue); - } - } - - CU_set_error(error); - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - pSuite->fActive = fNewActive; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else if (NULL == strNewName) { - result = CUE_NO_SUITENAME; - } - else { - CU_FREE(pSuite->pName); - pSuite->pName = (char *)CU_MALLOC(strlen(strNewName)+1); - strcpy(pSuite->pName, strNewName); - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - pSuite->pInitializeFunc = pNewInit; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - pSuite->pCleanupFunc = pNewClean; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite(const char *strName) -{ - CU_pSuite result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - result = CU_get_suite_by_name(strName, f_pTestRegistry); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite_at_pos(unsigned int pos) -{ - CU_pSuite result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else { - result = CU_get_suite_by_index(pos, f_pTestRegistry); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_suite_pos(CU_pSuite pSuite) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pSuite pCurrentSuite = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else { - pCurrentSuite = f_pTestRegistry->pSuite; - result = 1; - while ((NULL != pCurrentSuite) && (pCurrentSuite != pSuite)) { - ++result; - pCurrentSuite = pCurrentSuite->pNext; - } - if (pCurrentSuite == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_suite_pos_by_name(const char *strName) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pSuite pCurrentSuite = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - pCurrentSuite = f_pTestRegistry->pSuite; - result = 1; - while ((NULL != pCurrentSuite) && (0 != strcmp(pCurrentSuite->pName, strName))) { - ++result; - pCurrentSuite = pCurrentSuite->pNext; - } - if (pCurrentSuite == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc) -{ - CU_pTest pRetValue = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - assert(CU_FALSE == CU_is_test_running()); - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == strName) { - error = CUE_NO_TESTNAME; - } - else if(NULL == pTestFunc) { - error = CUE_NOTEST; - } - else { - pRetValue = create_test(strName, pTestFunc); - if (NULL == pRetValue) { - error = CUE_NOMEMORY; - } - else { - f_pTestRegistry->uiNumberOfTests++; - if (CU_TRUE == test_exists(pSuite, strName)) { - error = CUE_DUP_TEST; - } - insert_test(pSuite, pRetValue); - } - } - - CU_set_error(error); - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_test_active(CU_pTest pTest, CU_BOOL fNewActive) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pTest) { - result = CUE_NOTEST; - } - else { - pTest->fActive = fNewActive; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pTest) { - result = CUE_NOTEST; - } - else if (NULL == strNewName) { - result = CUE_NO_TESTNAME; - } - else { - CU_FREE(pTest->pName); - pTest->pName = (char *)CU_MALLOC(strlen(strNewName)+1); - strcpy(pTest->pName, strNewName); - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if ((NULL == pTest) || (NULL == pNewFunc)) { - result = CUE_NOTEST; - } - else { - pTest->pTestFunc = pNewFunc; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName) -{ - CU_pTest result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - result = CU_get_test_by_name(strName, pSuite); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos) -{ - CU_pTest result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else { - result = CU_get_test_by_index(pos, pSuite); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pTest pCurrentTest = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == pTest) { - error = CUE_NOTEST; - } - else { - pCurrentTest = pSuite->pTest; - result = 1; - while ((NULL != pCurrentTest) && (pCurrentTest != pTest)) { - ++result; - pCurrentTest = pCurrentTest->pNext; - } - if (pCurrentTest == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pTest pCurrentTest = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == strName) { - error = CUE_NO_TESTNAME; - } - else { - pCurrentTest = pSuite->pTest; - result = 1; - while ((NULL != pCurrentTest) && (0 != strcmp(pCurrentTest->pName, strName))) { - ++result; - pCurrentTest = pCurrentTest->pNext; - } - if (pCurrentTest == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ -/* This section is based conceptually on code - * Copyright (C) 2004 Aurema Pty Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Derived from code contributed by K. Cheung and Aurema Pty Ltd. (thanks!) - * int test_group_register(test_group_t *tg) - * int test_suite_register(test_suite_t *ts) - */ -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_register_nsuites(int suite_count, ...) -{ - CU_SuiteInfo *pSuiteItem = NULL; - CU_TestInfo *pTestItem = NULL; - CU_pSuite pSuite = NULL; - - va_list argptr; - int i; - - va_start(argptr, suite_count); - - for (i=0 ; ipName; pSuiteItem++) { - if (NULL != (pSuite = CU_add_suite(pSuiteItem->pName, pSuiteItem->pInitFunc, pSuiteItem->pCleanupFunc))) { - for (pTestItem = pSuiteItem->pTests; NULL != pTestItem->pName; pTestItem++) { - if (NULL == CU_add_test(pSuite, pTestItem->pName, pTestItem->pTestFunc)) { - return CU_get_error(); - } - } - } - else { - return CU_get_error(); - } - } - } - } - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]) -{ - return CU_register_nsuites(1, suite_info); -} -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ - -/*================================================================= - * Private static function definitions - *=================================================================*/ -/*------------------------------------------------------------------------*/ -/** - * Internal function to clean up the specified test registry. - * cleanup_suite() will be called for each registered suite to perform - * cleanup of the associated test cases. Then, the suite's memory will - * be freed. Note that any pointers to tests or suites in pRegistry - * held by the user will be invalidated by this function. Severe problems - * can occur if this function is called during a test run involving pRegistry. - * Note that memory held for data members in the registry (e.g. pName) and - * the registry itself are not freed by this function. - * - * @see cleanup_suite() - * @see cleanup_test() - * @param pRegistry CU_pTestRegistry to clean up (non-NULL). - */ -static void cleanup_test_registry(CU_pTestRegistry pRegistry) -{ - CU_pSuite pCurSuite = NULL; - CU_pSuite pNextSuite = NULL; - - assert(NULL != pRegistry); - - pCurSuite = pRegistry->pSuite; - while (NULL != pCurSuite) { - pNextSuite = pCurSuite->pNext; - cleanup_suite(pCurSuite); - - CU_FREE(pCurSuite); - pCurSuite = pNextSuite; - } - pRegistry->pSuite = NULL; - pRegistry->uiNumberOfSuites = 0; - pRegistry->uiNumberOfTests = 0; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to create a new test suite having the specified parameters. - * This function creates a new test suite having the specified name and - * initialization/cleanup functions. The new suite is active for execution during - * test runs. The strName cannot be NULL (checked by assertion), but either or - * both function pointers can be. A pointer to the newly-created suite is returned, - * or NULL if there was an error allocating memory for the new suite. It is the - * responsibility of the caller to destroy the returned suite (use cleanup_suite() - * before freeing the returned pointer). - * - * @param strName Name for the new test suite (non-NULL). - * @param pInit Initialization function to call before running suite. - * @param pClean Cleanup function to call after running suite. - * @return A pointer to the newly-created suite (NULL if creation failed) - */ -static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) -{ - CU_pSuite pRetValue = (CU_pSuite)CU_MALLOC(sizeof(CU_Suite)); - - assert(NULL != strName); - - if (NULL != pRetValue) { - pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); - if (NULL != pRetValue->pName) { - strcpy(pRetValue->pName, strName); - pRetValue->fActive = CU_TRUE; - pRetValue->pInitializeFunc = pInit; - pRetValue->pCleanupFunc = pClean; - pRetValue->pTest = NULL; - pRetValue->pNext = NULL; - pRetValue->pPrev = NULL; - pRetValue->uiNumberOfTests = 0; - } - else { - CU_FREE(pRetValue); - pRetValue = NULL; - } - } - - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to clean up the specified test suite. - * Each test case registered with pSuite will be freed. Allocated memory held - * by the suite (i.e. the name) will also be deallocated. Severe problems can - * occur if this function is called during a test run involving pSuite. - * - * @param pSuite CU_pSuite to clean up (non-NULL). - * @see cleanup_test_registry() - * @see cleanup_test() - */ -static void cleanup_suite(CU_pSuite pSuite) -{ - CU_pTest pCurTest = NULL; - CU_pTest pNextTest = NULL; - - assert(NULL != pSuite); - - pCurTest = pSuite->pTest; - while (NULL != pCurTest) { - pNextTest = pCurTest->pNext; - - cleanup_test(pCurTest); - - CU_FREE(pCurTest); - pCurTest = pNextTest; - } - if (NULL != pSuite->pName) { - CU_FREE(pSuite->pName); - } - - pSuite->pName = NULL; - pSuite->pTest = NULL; - pSuite->uiNumberOfTests = 0; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to insert a suite into a registry. - * The suite name is assumed to be unique. Internally, the list of suites - * is a double-linked list, which this function manages. Insertion of NULL - * pSuites is not allowed (checked by assertion). Severe problems can occur - * if this function is called during a test run involving pRegistry. - * - * @param pRegistry CU_pTestRegistry to insert into (non-NULL). - * @param pSuite CU_pSuite to insert (non-NULL). - * @see insert_test() - */ -static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite) -{ - CU_pSuite pCurSuite = NULL; - - assert(NULL != pRegistry); - assert(NULL != pSuite); - - pCurSuite = pRegistry->pSuite; - - assert(pCurSuite != pSuite); - - pSuite->pNext = NULL; - pRegistry->uiNumberOfSuites++; - - /* if this is the 1st suite to be added... */ - if (NULL == pCurSuite) { - pRegistry->pSuite = pSuite; - pSuite->pPrev = NULL; - } - /* otherwise, add it to the end of the linked list... */ - else { - while (NULL != pCurSuite->pNext) { - pCurSuite = pCurSuite->pNext; - assert(pCurSuite != pSuite); - } - - pCurSuite->pNext = pSuite; - pSuite->pPrev = pCurSuite; - } -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to create a new test case having the specified parameters. - * This function creates a new test having the specified name and test function. - * The strName cannot be NULL (checked by assertion), but the function pointer - * may be. A pointer to the newly-created test is returned, or NULL if there - * was an error allocating memory for the new test. It is the responsibility - * of the caller to destroy the returned test (use cleanup_test() before freeing - * the returned pointer). - * - * @param strName Name for the new test. - * @param pTestFunc Test function to call when running this test. - * @return A pointer to the newly-created test (NULL if creation failed) - */ -static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc) -{ - CU_pTest pRetValue = (CU_pTest)CU_MALLOC(sizeof(CU_Test)); - - assert(NULL != strName); - - if (NULL != pRetValue) { - pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); - if (NULL != pRetValue->pName) { - strcpy(pRetValue->pName, strName); - pRetValue->fActive = CU_TRUE; - pRetValue->pTestFunc = pTestFunc; - pRetValue->pJumpBuf = NULL; - pRetValue->pNext = NULL; - pRetValue->pPrev = NULL; - } - else { - CU_FREE(pRetValue); - pRetValue = NULL; - } - } - - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to clean up the specified test. - * All memory associated with the test will be freed. Severe problems can - * occur if this function is called during a test run involving pTest. - * - * @param pTest CU_pTest to clean up (non-NULL). - * @see cleanup_test_registry() - * @see cleanup_suite() - */ -static void cleanup_test(CU_pTest pTest) -{ - assert(NULL != pTest); - - if (NULL != pTest->pName) { - CU_FREE(pTest->pName); - } - - pTest->pName = NULL; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to insert a test into a suite. - * The test name is assumed to be unique. Internally, the list of tests in - * a suite is a double-linked list, which this function manages. Neither - * pSuite nor pTest may be NULL (checked by assertion). Further, pTest must - * be an independent test (i.e. both pTest->pNext and pTest->pPrev == NULL), - * which is also checked by assertion. Severe problems can occur if this - * function is called during a test run involving pSuite. - * - * @param pSuite CU_pSuite to insert into (non-NULL). - * @param pTest CU_pTest to insert (non-NULL). - * @see insert_suite() - */ -static void insert_test(CU_pSuite pSuite, CU_pTest pTest) -{ - CU_pTest pCurTest = NULL; - - assert(NULL != pSuite); - assert(NULL != pTest); - assert(NULL == pTest->pNext); - assert(NULL == pTest->pPrev); - - pCurTest = pSuite->pTest; - - assert(pCurTest != pTest); - - pSuite->uiNumberOfTests++; - /* if this is the 1st suite to be added... */ - if (NULL == pCurTest) { - pSuite->pTest = pTest; - pTest->pPrev = NULL; - } - else { - while (NULL != pCurTest->pNext) { - pCurTest = pCurTest->pNext; - assert(pCurTest != pTest); - } - - pCurTest->pNext = pTest; - pTest->pPrev = pCurTest; - } -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to check whether a suite having a specified - * name already exists. - * - * @param pRegistry CU_pTestRegistry to check (non-NULL). - * @param szSuiteName Suite name to check (non-NULL). - * @return CU_TRUE if suite exists in the registry, CU_FALSE otherwise. - */ -static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName) -{ - CU_pSuite pSuite = NULL; - - assert(NULL != pRegistry); - assert(NULL != szSuiteName); - - pSuite = pRegistry->pSuite; - while (NULL != pSuite) { - if ((NULL != pSuite->pName) && (0 == CU_compare_strings(szSuiteName, pSuite->pName))) { - return CU_TRUE; - } - pSuite = pSuite->pNext; - } - - return CU_FALSE; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to check whether a test having a specified - * name is already registered in a given suite. - * - * @param pSuite CU_pSuite to check (non-NULL). - * @param szTestName Test case name to check (non-NULL). - * @return CU_TRUE if test exists in the suite, CU_FALSE otherwise. - */ -static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName) -{ - CU_pTest pTest = NULL; - - assert(NULL != pSuite); - assert(NULL != szTestName); - - pTest = pSuite->pTest; - while (NULL != pTest) { - if ((NULL != pTest->pName) && (0 == CU_compare_strings(szTestName, pTest->pName))) { - return CU_TRUE; - } - pTest = pTest->pNext; - } - - return CU_FALSE; -} - -/*================================================================= - * Public but primarily internal function definitions - *=================================================================*/ -CU_pTestRegistry CU_create_new_registry(void) -{ - CU_pTestRegistry pRegistry = (CU_pTestRegistry)CU_MALLOC(sizeof(CU_TestRegistry)); - if (NULL != pRegistry) { - pRegistry->pSuite = NULL; - pRegistry->uiNumberOfSuites = 0; - pRegistry->uiNumberOfTests = 0; - } - - return pRegistry; -} - -/*------------------------------------------------------------------------*/ -void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry) -{ - assert(NULL != ppRegistry); - - /* Note - CU_cleanup_registry counts on being able to pass NULL */ - - if (NULL != *ppRegistry) { - cleanup_test_registry(*ppRegistry); - } - CU_FREE(*ppRegistry); - *ppRegistry = NULL; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite_by_name(const char* szSuiteName, CU_pTestRegistry pRegistry) -{ - CU_pSuite pSuite = NULL; - CU_pSuite pCur = NULL; - - assert(NULL != pRegistry); - assert(NULL != szSuiteName); - - pCur = pRegistry->pSuite; - while (NULL != pCur) { - if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szSuiteName))) { - pSuite = pCur; - break; - } - pCur = pCur->pNext; - } - - return pSuite; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite_by_index(unsigned int index, CU_pTestRegistry pRegistry) -{ - CU_pSuite result = NULL; - unsigned int i; - - assert(NULL != pRegistry); - - if ((index > 0) && (index <= f_pTestRegistry->uiNumberOfSuites)) { - result = f_pTestRegistry->pSuite; - for (i=1 ; ipNext; - } - } - - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test_by_name(const char* szTestName, CU_pSuite pSuite) -{ - CU_pTest pTest = NULL; - CU_pTest pCur = NULL; - - assert(NULL != pSuite); - assert(NULL != szTestName); - - pCur = pSuite->pTest; - while (NULL != pCur) { - if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szTestName))) { - pTest = pCur; - break; - } - pCur = pCur->pNext; - } - - return pTest; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test_by_index(unsigned int index, CU_pSuite pSuite) -{ - CU_pTest result = NULL; - unsigned int i; - - assert(NULL != pSuite); - - if ((index > 0) && (index <= pSuite->uiNumberOfTests)) { - result = pSuite->pTest; - for (i=1 ; ipNext; - } - } - - return result; -} -/** @} */ - -/*------------------------------------------------------------------------*/ -/*------------------------------------------------------------------------*/ -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -static int sfunc1(void) -{ return 0; } - -static void test1(void) -{} - -static void test2(void) -{} - -/*--------------------------------------------------*/ -static void test_CU_initialize_registry(void) -{ - CU_pTestRegistry pReg = NULL; - unsigned int ndeallocs_before; - - /* initial state */ - TEST(NULL == CU_get_registry()); - TEST(CU_FALSE == CU_registry_initialized()); - - /* after normal initialization */ - TEST(CUE_SUCCESS == CU_initialize_registry()); - pReg = CU_get_registry(); - TEST_FATAL(NULL != pReg); - TEST(CU_TRUE == CU_registry_initialized()); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(NULL == pReg->pSuite); - - /* after reinitialization */ - TEST(0 < test_cunit_get_n_memevents(pReg)); - ndeallocs_before = test_cunit_get_n_deallocations(pReg); - TEST(CUE_SUCCESS == CU_initialize_registry()); - TEST((ndeallocs_before + 1) == test_cunit_get_n_deallocations(pReg)); - pReg = CU_get_registry(); - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(NULL == pReg->pSuite); - - /* after cleanup */ - CU_cleanup_registry(); - TEST(NULL == CU_get_registry()); - TEST(CU_FALSE == CU_registry_initialized()); - - /* if malloc fails */ - test_cunit_deactivate_malloc(); - TEST(CUE_NOMEMORY == CU_initialize_registry()); - TEST(NULL == CU_get_registry()); - TEST(CU_FALSE == CU_registry_initialized()); - test_cunit_activate_malloc(); -} - -/*--------------------------------------------------*/ -static void test_CU_cleanup_registry(void) -{ - /* make sure calling with uninitialized registry does not crash */ - CU_cleanup_registry(); - CU_cleanup_registry(); - CU_cleanup_registry(); - CU_cleanup_registry(); - CU_cleanup_registry(); - - /* nothing more to do over test_CU_initialize_registry() */ -} - -/*--------------------------------------------------*/ -/* test CU_add_suite() - * CU_get_suite_by_name() - * CU_get_suite_by_index() - */ -static void test_CU_add_suite(void) -{ - CU_pSuite pSuite = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTestRegistry pReg = NULL; - - CU_cleanup_registry(); /* make sure registry not initialized */ - - /* error condition - registry not initialized */ - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_NOREGISTRY == CU_get_error()); - TEST(NULL == pSuite); - - /* error condition - no name */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite = CU_add_suite(NULL, NULL, NULL); - TEST(CUE_NO_SUITENAME == CU_get_error()); - TEST(NULL == pSuite); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* warning condition - duplicate name */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - pSuite2 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_DUP_SUITE == CU_get_error()); - TEST(NULL != pSuite2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - TEST(!strcmp("suite1", pSuite->pName)); - TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite->pTest == NULL); /* no tests added yet */ - TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite->pNext == pSuite2); /* now have another suite */ - - TEST(!strcmp("suite1", pSuite2->pName)); - TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite2->pTest == NULL); /* no tests added yet */ - TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite2->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite2->pNext == NULL); /* end of the list */ - - /* error condition - memory allocation failure */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - test_cunit_deactivate_malloc(); - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_NOMEMORY == CU_get_error()); - TEST(NULL == pSuite); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - test_cunit_activate_malloc(); - - /* normal creation & cleanup */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == NULL); - - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite1", pReg) == pSuite); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == NULL); - TEST(pReg->pSuite == pSuite); - - TEST(!strcmp("suite1", pSuite->pName)); - TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite->pTest == NULL); /* no tests added yet */ - TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite->pNext == NULL); /* no more suites added yet */ - - pSuite2 = CU_add_suite("suite2", sfunc1, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite2", pReg) == pSuite2); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == pSuite2); - TEST(CU_get_suite_by_index(3, pReg) == NULL); - - pSuite3 = CU_add_suite("suite3", NULL, sfunc1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite3); - TEST(3 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite3", pReg) == pSuite3); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == pSuite2); - TEST(CU_get_suite_by_index(3, pReg) == pSuite3); - TEST(CU_get_suite_by_index(4, pReg) == NULL); - - pSuite4 = CU_add_suite("suite4", sfunc1, sfunc1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite4); - TEST(4 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite4", pReg) == pSuite4); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == pSuite2); - TEST(CU_get_suite_by_index(3, pReg) == pSuite3); - TEST(CU_get_suite_by_index(4, pReg) == pSuite4); - TEST(CU_get_suite_by_index(5, pReg) == NULL); - - /* test registry suite structures */ - TEST(pReg->pSuite == pSuite); - - TEST(!strcmp("suite1", pSuite->pName)); - TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite->pTest == NULL); /* no tests added yet */ - TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite->pNext == pSuite2); /* now have another suite */ - - TEST(!strcmp("suite2", pSuite2->pName)); - TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite2->pTest == NULL); /* no tests added yet */ - TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite2->pInitializeFunc == sfunc1); /* no init function */ - TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite2->pNext == pSuite3); /* next suite in list */ - - TEST(!strcmp("suite3", pSuite3->pName)); - TEST(pSuite3->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite3->pTest == NULL); /* no tests added yet */ - TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite3->pCleanupFunc == sfunc1); /* no cleanup function */ - TEST(pSuite3->pNext == pSuite4); /* next suite in list */ - - TEST(!strcmp("suite4", pSuite4->pName)); - TEST(pSuite4->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite4->pTest == NULL); /* no tests added yet */ - TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ - TEST(pSuite4->pCleanupFunc == sfunc1); /* no cleanup function */ - TEST(pSuite4->pNext == NULL); /* end of suite list */ - - TEST(0 != test_cunit_get_n_memevents(pSuite)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pSuite3)); - TEST(0 != test_cunit_get_n_memevents(pSuite4)); - - TEST(test_cunit_get_n_allocations(pSuite) != test_cunit_get_n_deallocations(pSuite)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); - - /* clean up everything and confirm deallocation */ - CU_cleanup_registry(); - - TEST(test_cunit_get_n_allocations(pSuite) == test_cunit_get_n_deallocations(pSuite)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); -} - -/*--------------------------------------------------*/ -/* test CU_set_suite_active() - * CU_set_suite_name() - * CU_set_suite_initfunc() - * CU_set_suite_cleanupfunc() - */ -static void test_CU_set_suite_attributes(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - - /* initialize system */ - CU_initialize_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST_FATAL(NULL != pSuite1); - TEST_FATAL(NULL != CU_add_test(pSuite1, "test1", test1)); - TEST_FATAL(NULL != CU_add_test(pSuite1, "test2", test1)); - - pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ - TEST_FATAL(NULL != pSuite2); - TEST_FATAL(NULL != CU_add_test(pSuite2, "test1", test1)); - TEST_FATAL(NULL != CU_add_test(pSuite2, "test2", test1)); - - /* test CU_set_suite_active() */ - TEST(CUE_NOSUITE == CU_set_suite_active(NULL, CU_FALSE)); /* error - NULL suite */ - - TEST(pSuite2->fActive == CU_TRUE); /* suites active on creation */ - TEST(pSuite1->fActive == CU_TRUE); - - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); - TEST(pSuite1->fActive == CU_TRUE); - TEST(pSuite2->fActive == CU_FALSE); - - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); - TEST(pSuite1->fActive == CU_FALSE); - TEST(pSuite2->fActive == CU_FALSE); - - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_TRUE)); - TEST(pSuite1->fActive == CU_FALSE); - TEST(pSuite2->fActive == CU_TRUE); - - /* test CU_set_suite_name() */ - TEST(CUE_NOSUITE == CU_set_suite_name(NULL, "null suite")); /* error - NULL suite */ - TEST(CUE_NO_SUITENAME == CU_set_suite_name(pSuite1, NULL)); /* error - NULL name */ - - TEST(!strcmp(pSuite1->pName, "suite1")); - TEST(!strcmp(pSuite2->pName, "suite2")); - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "This is my new name.")); - TEST(!strcmp(pSuite1->pName, "This is my new name.")); - TEST(!strcmp(pSuite2->pName, "suite2")); - - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "Never mind.")); - TEST(!strcmp(pSuite1->pName, "This is my new name.")); - TEST(!strcmp(pSuite2->pName, "Never mind.")); - - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "suite1")); - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "suite2")); - TEST(!strcmp(pSuite1->pName, "suite1")); - TEST(!strcmp(pSuite2->pName, "suite2")); - - /* test CU_set_suite_initfunc() */ - TEST(CUE_NOSUITE == CU_set_suite_initfunc(NULL, &sfunc1)); /* error - NULL suite */ - - TEST(pSuite1->pInitializeFunc == NULL); - TEST(pSuite2->pInitializeFunc == &sfunc1); - TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite1, &sfunc1)); - TEST(pSuite1->pInitializeFunc == &sfunc1); - TEST(pSuite2->pInitializeFunc == &sfunc1); - - TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite2, NULL)); - TEST(pSuite1->pInitializeFunc == &sfunc1); - TEST(pSuite2->pInitializeFunc == NULL); - - /* test CU_set_suite_cleanupfunc() */ - TEST(CUE_NOSUITE == CU_set_suite_cleanupfunc(NULL, &sfunc1)); - - TEST(pSuite1->pCleanupFunc == NULL); - TEST(pSuite2->pCleanupFunc == NULL); - TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite1, &sfunc1)); - TEST(pSuite1->pCleanupFunc == &sfunc1); - TEST(pSuite2->pCleanupFunc == NULL); - - TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite2, &sfunc1)); - TEST(pSuite1->pCleanupFunc == &sfunc1); - TEST(pSuite2->pCleanupFunc == &sfunc1); - - /* clean up */ - CU_cleanup_registry(); -} - -static void test_succeed(void) { CU_TEST(CU_TRUE); } -static void test_fail(void) { CU_TEST(CU_FALSE); } -static int suite_succeed(void) { return 0; } -static int suite_fail(void) { return 1; } - -/*--------------------------------------------------*/ -/* test CU_get_suite() - * CU_get_suite_at_pos() - * CU_get_suite_pos() - * CU_get_suite_pos_by_name() - */ -static void test_get_suite_functions(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pSuite pSuite5 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - CU_pTest pTest10 = NULL; - - /* error condition - registry not initialized */ - CU_cleanup_registry(); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_suite("suite1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_suite_at_pos(0)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos(pSuite1)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos_by_name("suite1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test9", test_succeed); - pSuite4 = CU_add_suite("suite4", NULL, suite_fail); - pTest10 = CU_add_test(pSuite4, "test10", test_succeed); - - /* error condition - invalid parameters */ - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_suite(NULL)); - TEST(CUE_NO_SUITENAME == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos(NULL)); - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos_by_name(NULL)); - TEST(CUE_NO_SUITENAME == CU_get_error()); - - /* normal operation - CU_get_suite() */ - - TEST(NULL == CU_get_suite("")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_suite("bad name")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_suite("suite3")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pSuite1 == CU_get_suite("suite1")); /* valid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite2 == CU_get_suite("suite2")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite4 == CU_get_suite("suite4")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_suite_at_pos() */ - - TEST(NULL == CU_get_suite_at_pos(0)); /* invalid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_suite_at_pos(5)); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pSuite1 == CU_get_suite_at_pos(1)); /* valid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite2 == CU_get_suite_at_pos(2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite3 == CU_get_suite_at_pos(3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite4 == CU_get_suite_at_pos(4)); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_suite_pos() */ - - pSuite5 = (CU_pSuite)malloc(sizeof(CU_Suite)); - TEST_FATAL(NULL != pSuite5); - - TEST(0 == CU_get_suite_pos(pSuite5)); /* invalid suite */ - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_suite_pos(pSuite1)); /* valid suites */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_suite_pos(pSuite2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(3 == CU_get_suite_pos(pSuite3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_suite_pos(pSuite4)); - TEST(CUE_SUCCESS == CU_get_error()); - - free(pSuite5); - - /* normal operation - CU_get_suite_pos_by_name() */ - - TEST(0 == CU_get_suite_pos_by_name("")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_suite_pos_by_name("suite3")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_suite_pos_by_name("suite1")); /* valid suites */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_suite_pos_by_name("suite2")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_suite_pos_by_name("suite4")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* clean up */ - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -/* test CU_add_test() - * CU_get_test_by_name() - * CU_get_test_by_index() - */ -static void test_CU_add_test(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTestRegistry pReg = NULL; - - CU_cleanup_registry(); - - /* error condition - registry not initialized */ - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_NOREGISTRY == CU_get_error()); - TEST(NULL == pTest1); - - CU_initialize_registry(); - pReg = CU_get_registry(); - - /* error condition - no suite */ - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_NOSUITE == CU_get_error()); - TEST(NULL == pTest1); - - /* error condition - no name */ - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - pTest1 = CU_add_test(pSuite1, NULL, test1); - TEST(CUE_NO_TESTNAME == CU_get_error()); - TEST(NULL == pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(0 == pSuite1->uiNumberOfTests); - - /* error condition - no test function */ - pTest1 = CU_add_test(pSuite1, "test1", NULL); - TEST(CUE_NOTEST == CU_get_error()); - TEST(NULL == pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(0 == pSuite1->uiNumberOfTests); - - /* warning condition - duplicate name */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(1 == pReg->uiNumberOfTests); - TEST(1 == pSuite1->uiNumberOfTests); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == NULL); - - pTest2 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_DUP_TEST == CU_get_error()); - TEST(NULL != pTest2); - TEST(1 == pReg->uiNumberOfSuites); - TEST(2 == pReg->uiNumberOfTests); - TEST(2 == pSuite1->uiNumberOfTests); - - TEST(!strcmp("test1", pTest1->pName)); - TEST(pTest1->fActive == CU_TRUE); - TEST(pTest1->pNext == pTest2); - TEST(pTest1->pJumpBuf == NULL); - TEST(pTest1->pTestFunc == test1); - TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == pTest2); - TEST(CU_get_test_by_index(3, pSuite1) == NULL); - - TEST(!strcmp("test1", pTest2->pName)); - TEST(pTest2->fActive == CU_TRUE); - TEST(pTest2->pNext == NULL); - TEST(pTest2->pJumpBuf == NULL); - TEST(pTest2->pTestFunc == test1); - TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); - - /* error condition - memory allocation failure */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - test_cunit_deactivate_malloc(); - pTest1 = CU_add_test(pSuite1, "test1", test1); - test_cunit_activate_malloc(); - TEST(CUE_NOMEMORY == CU_get_error()); - TEST(NULL == pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(0 == pSuite1->uiNumberOfTests); - - /* normal creation & cleanup */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - pSuite2 = CU_add_suite("suite2", sfunc1, sfunc1); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == NULL); - - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest1); - TEST(2 == pReg->uiNumberOfSuites); - TEST(1 == pReg->uiNumberOfTests); - TEST(1 == pSuite1->uiNumberOfTests); - TEST(0 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == NULL); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == NULL); - - pTest2 = CU_add_test(pSuite2, "test2", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(2 == pReg->uiNumberOfTests); - TEST(1 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest2); - TEST(CU_get_test_by_index(0, pSuite2) == NULL); - TEST(CU_get_test_by_index(1, pSuite2) == pTest2); - TEST(CU_get_test_by_index(2, pSuite2) == NULL); - - pTest3 = CU_add_test(pSuite1, "test3", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest3); - TEST(2 == pReg->uiNumberOfSuites); - TEST(3 == pReg->uiNumberOfTests); - TEST(2 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest2); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == pTest3); - TEST(CU_get_test_by_index(3, pSuite1) == NULL); - - pTest4 = CU_add_test(pSuite1, "test4", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest4); - TEST(2 == pReg->uiNumberOfSuites); - TEST(4 == pReg->uiNumberOfTests); - TEST(3 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest2); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == pTest3); - TEST(CU_get_test_by_index(3, pSuite1) == pTest4); - TEST(CU_get_test_by_index(4, pSuite1) == NULL); - - TEST(!strcmp("test1", pTest1->pName)); - TEST(pTest1->pNext == pTest3); - TEST(pTest1->pJumpBuf == NULL); - TEST(pTest1->pTestFunc == test1); - TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); - TEST(CU_get_test_by_name("test1", pSuite2) == NULL); - - TEST(!strcmp("test2", pTest2->pName)); - TEST(pTest2->pNext == NULL); - TEST(pTest2->pJumpBuf == NULL); - TEST(pTest2->pTestFunc == test1); - TEST(CU_get_test_by_name("test2", pSuite1) == NULL); - TEST(CU_get_test_by_name("test2", pSuite2) == pTest2); - - TEST(!strcmp("test3", pTest3->pName)); - TEST(pTest3->pNext == pTest4); - TEST(pTest3->pJumpBuf == NULL); - TEST(pTest3->pTestFunc == test1); - TEST(CU_get_test_by_name("test3", pSuite1) == pTest3); - TEST(CU_get_test_by_name("test3", pSuite2) == NULL); - - TEST(!strcmp("test4", pTest4->pName)); - TEST(pTest4->pNext == NULL); - TEST(pTest4->pJumpBuf == NULL); - TEST(pTest4->pTestFunc == test1); - TEST(CU_get_test_by_name("test4", pSuite1) == pTest4); - TEST(CU_get_test_by_name("test4", pSuite2) == NULL); - - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - TEST(0 != test_cunit_get_n_memevents(pTest3)); - TEST(0 != test_cunit_get_n_memevents(pTest4)); - - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); - - CU_cleanup_registry(); - - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); -} - -/*--------------------------------------------------*/ -/* test CU_set_test_active() - * CU_set_test_name() - * CU_set_test_func() - */ -static void test_CU_set_test_attributes(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - - /* initialize system */ - CU_initialize_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST_FATAL(NULL != pSuite1); - pTest1 = CU_add_test(pSuite1, "test1", test2); - TEST_FATAL(NULL != pTest1); - pTest2 = CU_add_test(pSuite1, "test2", test1); - TEST_FATAL(NULL != pTest2); - - pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ - TEST_FATAL(NULL != pSuite2); - pTest3 = CU_add_test(pSuite2, "test3", test2); - TEST_FATAL(NULL != pTest3); - pTest4 = CU_add_test(pSuite2, "test4", test1); - TEST_FATAL(NULL != pTest4); - - /* test CU_set_test_active() */ - TEST(CUE_NOTEST == CU_set_test_active(NULL, CU_FALSE)); /* error - NULL test */ - - TEST(CU_TRUE == pTest1->fActive); /* tests active on creation */ - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_TRUE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); - TEST(CU_TRUE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_FALSE == pTest3->fActive); - TEST(CU_FALSE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); - TEST(CU_FALSE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_FALSE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); - TEST(CU_FALSE == pTest1->fActive); - TEST(CU_FALSE == pTest2->fActive); - TEST(CU_FALSE == pTest3->fActive); - TEST(CU_FALSE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_TRUE)); - TEST(CU_TRUE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_TRUE == pTest4->fActive); - - /* test CU_set_test_name() */ - TEST(CUE_NOTEST == CU_set_test_name(NULL, "null test")); /* error - NULL test */ - TEST(CUE_NO_TESTNAME == CU_set_test_name(pTest1, NULL)); /* error - NULL name */ - - TEST(!strcmp("test1", pTest1->pName)); - TEST(!strcmp("test2", pTest2->pName)); - TEST(!strcmp("test3", pTest3->pName)); - TEST(!strcmp("test4", pTest4->pName)); - - TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "Aren't I a pretty girl?")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "Polly want a cracker.")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "@This is utter nonsense@")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "Yep!")); - - TEST(!strcmp("Aren't I a pretty girl?", pTest1->pName)); - TEST(!strcmp("Polly want a cracker.", pTest2->pName)); - TEST(!strcmp("@This is utter nonsense@", pTest3->pName)); - TEST(!strcmp("Yep!", pTest4->pName)); - - TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "test1")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "test2")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "test3")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "test4")); - - TEST(!strcmp("test1", pTest1->pName)); - TEST(!strcmp("test2", pTest2->pName)); - TEST(!strcmp("test3", pTest3->pName)); - TEST(!strcmp("test4", pTest4->pName)); - - /* test CU_set_test_func() */ - TEST(CUE_NOTEST == CU_set_test_func(NULL, &test1)); /* error - NULL test */ - TEST(CUE_NOTEST == CU_set_test_func(pTest1, NULL)); /* error - NULL test function */ - - TEST(&test2 == pTest1->pTestFunc); - TEST(&test1 == pTest2->pTestFunc); - TEST(&test2 == pTest3->pTestFunc); - TEST(&test1 == pTest4->pTestFunc); - - TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test1)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test2)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test1)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test2)); - - TEST(&test1 == pTest1->pTestFunc); - TEST(&test2 == pTest2->pTestFunc); - TEST(&test1 == pTest3->pTestFunc); - TEST(&test2 == pTest4->pTestFunc); - - TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test2)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test1)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test2)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test1)); - - /* clean up */ - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -/* test CU_get_test() - * CU_get_test_at_pos() - * CU_get_test_pos() - * CU_get_test_pos_by_name() - */ -static void test_get_test_functions(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - CU_pTest pTest10 = NULL; - - /* error condition - registry not initialized */ - CU_cleanup_registry(); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test(pSuite1, "test1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos(pSuite1, pTest1)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos_by_name(pSuite1, "test1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test9", test_succeed); - pSuite4 = CU_add_suite("suite4", NULL, suite_fail); - pTest10 = CU_add_test(pSuite4, "test10", test_succeed); - - /* error condition - invalid parameters */ - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test(NULL, "test1")); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test(pSuite1, NULL)); /* name NULL */ - TEST(CUE_NO_SUITENAME == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_at_pos(NULL, 1)); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos(NULL, pTest1)); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos(pSuite1, NULL)); /* test NULL */ - TEST(CUE_NOTEST == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos_by_name(NULL, "test1")); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos_by_name(pSuite1, NULL)); /* name NULL */ - TEST(CUE_NO_TESTNAME == CU_get_error()); - - /* normal operation - CU_get_test() */ - - TEST(NULL == CU_get_test(pSuite1, "")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test(pSuite2, "bad name")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test(pSuite1, "test3")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pTest1 == CU_get_test(pSuite1, "test1")); /* valid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest6 == CU_get_test(pSuite2, "test6")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest8 == CU_get_test(pSuite3, "test8")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_test_at_pos() */ - - TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); /* invalid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test_at_pos(pSuite1, 6)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test_at_pos(pSuite4, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pTest1 == CU_get_test_at_pos(pSuite1, 1)); /* valid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest2 == CU_get_test_at_pos(pSuite1, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest3 == CU_get_test_at_pos(pSuite1, 3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest4 == CU_get_test_at_pos(pSuite1, 4)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest5 == CU_get_test_at_pos(pSuite1, 5)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest6 == CU_get_test_at_pos(pSuite2, 1)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest7 == CU_get_test_at_pos(pSuite2, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest8 == CU_get_test_at_pos(pSuite3, 1)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest9 == CU_get_test_at_pos(pSuite3, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest10 == CU_get_test_at_pos(pSuite4, 1)); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_test_pos() */ - - TEST(0 == CU_get_test_pos(pSuite1, pTest6)); /* invalid tests */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_test_pos(pSuite4, pTest6)); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_test_pos(pSuite1, pTest1)); /* valid tests */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos(pSuite1, pTest2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(3 == CU_get_test_pos(pSuite1, pTest3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_test_pos(pSuite1, pTest4)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(5 == CU_get_test_pos(pSuite1, pTest5)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos(pSuite2, pTest6)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos(pSuite2, pTest7)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos(pSuite3, pTest8)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos(pSuite3, pTest9)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos(pSuite4, pTest10)); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_test_pos_by_name() */ - - TEST(0 == CU_get_test_pos_by_name(pSuite1, "")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_test_pos_by_name(pSuite1, "test9")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_test_pos_by_name(pSuite2, "test1")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_test_pos_by_name(pSuite1, "test1")); /* valid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos_by_name(pSuite1, "test2")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_test_pos_by_name(pSuite1, "test4")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos_by_name(pSuite2, "test6")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos_by_name(pSuite2, "test7")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos_by_name(pSuite3, "test8")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos_by_name(pSuite3, "test9")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos_by_name(pSuite4, "test10")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* clean up */ - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -static void test_CU_get_registry(void) -{ - CU_cleanup_registry(); - TEST(NULL == CU_get_registry()); - - CU_initialize_registry(); - TEST(NULL != CU_get_registry()); - TEST(f_pTestRegistry == CU_get_registry()); - - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -static void test_CU_set_registry(void) -{ - CU_pTestRegistry pReg1 = NULL; - CU_pTestRegistry pReg2 = NULL; - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pSuite2 = CU_add_suite("suite2", NULL, NULL); - - CU_add_test(pSuite1, "test1", test1); - CU_add_test(pSuite1, "test2", test1); - CU_add_test(pSuite2, "test1", test1); - CU_add_test(pSuite2, "test2", test1); - - pReg1 = CU_get_registry(); - - TEST(pReg1->pSuite == pSuite1); - TEST(pReg1->uiNumberOfSuites == 2); - TEST(pReg1->uiNumberOfTests == 4); - TEST(0 < test_cunit_get_n_memevents(pReg1)); - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - CU_set_registry(NULL); - - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - CU_cleanup_registry(); - - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - pReg2 = CU_create_new_registry(); - CU_set_registry(pReg2); - - TEST(pReg1->pSuite == pSuite1); - TEST(pReg1->uiNumberOfSuites == 2); - TEST(pReg1->uiNumberOfTests == 4); - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - TEST(CU_get_registry()->pSuite == NULL); - TEST(CU_get_registry()->uiNumberOfSuites == 0); - TEST(CU_get_registry()->uiNumberOfTests == 0); - TEST(0 < test_cunit_get_n_memevents(pReg2)); - TEST(test_cunit_get_n_allocations(pReg2) != test_cunit_get_n_deallocations(pReg2)); - - CU_cleanup_registry(); - - TEST(pReg1->pSuite == pSuite1); - TEST(pReg1->uiNumberOfSuites == 2); - TEST(pReg1->uiNumberOfTests == 4); - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - TEST(test_cunit_get_n_allocations(pReg2) == test_cunit_get_n_deallocations(pReg2)); - - CU_set_registry(pReg1); - CU_cleanup_registry(); - TEST(test_cunit_get_n_allocations(pReg1) == test_cunit_get_n_deallocations(pReg1)); -} - -/*--------------------------------------------------*/ -/* test CU_create_new_registry() - * CU_destroy_existing_registry() - */ -static void test_CU_create_new_registry(void) -{ - CU_pTestRegistry pReg = NULL; - CU_pTestRegistry pRegOld = NULL; - - CU_cleanup_registry(); - pReg = CU_create_new_registry(); - - TEST(NULL != pReg); - TEST(0 < test_cunit_get_n_memevents(pReg)); - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - - TEST(pReg->pSuite == NULL); - TEST(pReg->uiNumberOfSuites == 0); - TEST(pReg->uiNumberOfTests == 0); - - CU_cleanup_registry(); - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - - pRegOld = pReg; - CU_destroy_existing_registry(&pReg); - TEST(test_cunit_get_n_allocations(pRegOld) == test_cunit_get_n_deallocations(pRegOld)); - TEST(NULL == pReg); -} - -/*--------------------------------------------------*/ -static void test_cleanup_test_registry(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTestRegistry pReg = CU_create_new_registry(); - - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* create tests to register */ - pTest1 = create_test("test1", test1); - pTest2 = create_test("test2", NULL); - pTest3 = create_test("test3", test1); - pTest4 = create_test("", NULL); - - /* create suites to hold tests */ - pSuite1 = create_suite("suite1", NULL, NULL); - pSuite2 = create_suite("suite2", sfunc1, sfunc1); - insert_suite(pReg, pSuite1); - insert_suite(pReg, pSuite2); - - insert_test(pSuite1, pTest1); - insert_test(pSuite1, pTest2); - insert_test(pSuite1, pTest3); - insert_test(pSuite2, pTest4); - - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); /* not managed in primitive functions */ - TEST(3 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest4); - TEST(pTest1->pNext == pTest2); - TEST(pTest1->pPrev == NULL); - TEST(pTest2->pNext == pTest3); - TEST(pTest2->pPrev == pTest1); - TEST(pTest3->pNext == NULL); - TEST(pTest3->pPrev == pTest2); - TEST(pTest4->pNext == NULL); - TEST(pTest4->pPrev == NULL); - - TEST(0 != test_cunit_get_n_memevents(pReg)); - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - TEST(0 != test_cunit_get_n_memevents(pTest3)); - TEST(0 != test_cunit_get_n_memevents(pTest4)); - - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); - - cleanup_test_registry(pReg); - CU_FREE(pReg); - - TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); -} - -/*--------------------------------------------------*/ -/* test create_suite() - * cleanup_suite() - */ -static void test_create_suite(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - - /* error condition - memory allocation failure */ - test_cunit_deactivate_malloc(); - pSuite1 = create_suite("suite1", NULL, NULL); - TEST(NULL == pSuite1); - test_cunit_activate_malloc(); - - /* normal creation & cleanup */ - pSuite1 = create_suite("suite1", NULL, NULL); - TEST(NULL != pSuite1); - TEST(!strcmp("suite1", pSuite1->pName)); - TEST(pSuite1->pTest == NULL); /* no tests added yet */ - TEST(pSuite1->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite1->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite1->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite1->pNext == NULL); /* no more suites added yet */ - - pSuite2 = create_suite("suite2", sfunc1, NULL); - TEST(NULL != pSuite2); - TEST(!strcmp("suite2", pSuite2->pName)); - TEST(pSuite2->pTest == NULL); /* no tests added yet */ - TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite2->pInitializeFunc == sfunc1); /* init function */ - TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite2->pNext == NULL); /* no more suites added yet */ - - pSuite3 = create_suite("suite3", NULL, sfunc1); - TEST(NULL != pSuite3); - TEST(!strcmp("suite3", pSuite3->pName)); - TEST(pSuite3->pTest == NULL); /* no tests added yet */ - TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite3->pCleanupFunc == sfunc1); /* cleanup function */ - TEST(pSuite3->pNext == NULL); /* no more suites added yet */ - - pSuite4 = create_suite("suite4", sfunc1, sfunc1); - TEST(NULL != pSuite4); - TEST(!strcmp("suite4", pSuite4->pName)); - TEST(pSuite4->pTest == NULL); /* no tests added yet */ - TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ - TEST(pSuite4->pCleanupFunc == sfunc1); /* cleanup function */ - TEST(pSuite4->pNext == NULL); /* no more suites added yet */ - - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - cleanup_suite(pSuite1); - CU_FREE(pSuite1); - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - cleanup_suite(pSuite2); - CU_FREE(pSuite2); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - - TEST(0 != test_cunit_get_n_memevents(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); - cleanup_suite(pSuite3); - CU_FREE(pSuite3); - TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); - - TEST(0 != test_cunit_get_n_memevents(pSuite4)); - TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); - cleanup_suite(pSuite4); - CU_FREE(pSuite4); - TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); -} - -/*--------------------------------------------------*/ -/* test insert_suite() - * suite_exists() - */ -static void test_insert_suite(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTestRegistry pReg = CU_create_new_registry(); - - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(NULL == pReg->pSuite); - TEST(CU_FALSE == suite_exists(pReg, "suite1")); - TEST(CU_FALSE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - /* normal creation & cleanup */ - pSuite1 = create_suite("suite1", NULL, NULL); - insert_suite(pReg, pSuite1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_FALSE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - pSuite2 = create_suite("suite2", sfunc1, NULL); - insert_suite(pReg, pSuite2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == pSuite2); - TEST(pSuite2->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_TRUE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - pSuite3 = create_suite("suite3", NULL, sfunc1); - insert_suite(pReg, pSuite3); - TEST(3 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == pSuite2); - TEST(pSuite2->pNext == pSuite3); - TEST(pSuite3->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_TRUE == suite_exists(pReg, "suite2")); - TEST(CU_TRUE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - pSuite4 = create_suite("suite4", sfunc1, sfunc1); - insert_suite(pReg, pSuite4); - TEST(4 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == pSuite2); - TEST(pSuite2->pNext == pSuite3); - TEST(pSuite3->pNext == pSuite4); - TEST(pSuite4->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_TRUE == suite_exists(pReg, "suite2")); - TEST(CU_TRUE == suite_exists(pReg, "suite3")); - TEST(CU_TRUE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - TEST(0 != test_cunit_get_n_memevents(pReg)); - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pSuite3)); - TEST(0 != test_cunit_get_n_memevents(pSuite4)); - - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); - - cleanup_test_registry(pReg); - TEST(CU_FALSE == suite_exists(pReg, "suite1")); - TEST(CU_FALSE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - CU_FREE(pReg); - - TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); -} - -/*--------------------------------------------------*/ -static void test_create_test(void) -{ - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - - /* error condition - memory allocation failure */ - test_cunit_deactivate_malloc(); - pTest1 = create_test("test1", test1); - test_cunit_activate_malloc(); - TEST(NULL == pTest1); - - /* normal creation & cleanup */ - pTest1 = create_test("test1", test1); - TEST(NULL != pTest1); - TEST(pTest1->pTestFunc == test1); - TEST(!strcmp("test1", pTest1->pName)); - TEST(pTest1->pNext == NULL); - TEST(pTest1->pPrev == NULL); - TEST(pTest1->pJumpBuf == NULL); - - pTest2= create_test("test2", NULL); - TEST(NULL != pTest2); - TEST(pTest2->pTestFunc == NULL); - TEST(!strcmp("test2", pTest2->pName)); - TEST(pTest2->pNext == NULL); - TEST(pTest2->pPrev == NULL); - TEST(pTest2->pJumpBuf == NULL); - - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - - cleanup_test(pTest1); - CU_FREE(pTest1); - cleanup_test(pTest2); - CU_FREE(pTest2); - - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); -} - -/*--------------------------------------------------*/ -/* test insert_test() - * test_exists() - */ -static void test_insert_test(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - - /* create tests to register */ - pTest1 = create_test("test1", test1); - pTest2 = create_test("test2", NULL); - pTest3 = create_test("test3", test1); - pTest4 = create_test("", NULL); - - /* create suites to hold tests */ - pSuite1 = create_suite("suite1", NULL, NULL); - pSuite2 = create_suite("suite2", sfunc1, sfunc1); - - TEST(CU_FALSE == test_exists(pSuite1, "test1")); - TEST(CU_FALSE == test_exists(pSuite1, "test2")); - TEST(CU_FALSE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_FALSE == test_exists(pSuite2, "")); - - insert_test(pSuite1, pTest1); - insert_test(pSuite1, pTest2); - insert_test(pSuite1, pTest3); - insert_test(pSuite2, pTest4); - - TEST(CU_TRUE == test_exists(pSuite1, "test1")); - TEST(CU_TRUE == test_exists(pSuite1, "test2")); - TEST(CU_TRUE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_TRUE == test_exists(pSuite2, "")); - - TEST(3 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest4); - TEST(pTest1->pNext == pTest2); - TEST(pTest1->pPrev == NULL); - TEST(pTest2->pNext == pTest3); - TEST(pTest2->pPrev == pTest1); - TEST(pTest3->pNext == NULL); - TEST(pTest3->pPrev == pTest2); - TEST(pTest4->pNext == NULL); - TEST(pTest4->pPrev == NULL); - - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - TEST(0 != test_cunit_get_n_memevents(pTest3)); - TEST(0 != test_cunit_get_n_memevents(pTest4)); - - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); - - cleanup_suite(pSuite1); - - TEST(CU_FALSE == test_exists(pSuite1, "test1")); - TEST(CU_FALSE == test_exists(pSuite1, "test2")); - TEST(CU_FALSE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_TRUE == test_exists(pSuite2, "")); - - cleanup_suite(pSuite2); - - TEST(CU_FALSE == test_exists(pSuite1, "test1")); - TEST(CU_FALSE == test_exists(pSuite1, "test2")); - TEST(CU_FALSE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_FALSE == test_exists(pSuite2, "")); - - CU_FREE(pSuite1); - CU_FREE(pSuite2); - - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); -} - -/*--------------------------------------------------*/ -static void test_cleanup_test(void) -{ - char* pName; - CU_pTest pTest1 = create_test("test1", NULL); - - TEST_FATAL(NULL != pTest1); - - pName = pTest1->pName; - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pName)); - - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pName) != test_cunit_get_n_deallocations(pName)); - - cleanup_test(pTest1); - CU_FREE(pTest1); - - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pName) == test_cunit_get_n_deallocations(pName)); -} - -/*--------------------------------------------------*/ -static void group_A_case_1(void) -{ - CU_ASSERT_TRUE(1); -} - -static void group_A_case_2(void) -{ - CU_ASSERT_TRUE(2); -} - -static void group_B_case_1(void) -{ - CU_ASSERT_FALSE(1); -} - -static void group_B_case_2(void) -{ - CU_ASSERT_FALSE(2); -} - -static CU_TestInfo group_A_test_cases[] = { - { "1", group_A_case_1 }, - { "2", group_A_case_2 }, - CU_TEST_INFO_NULL, -}; - -static CU_TestInfo group_B_test_cases[] = { - { "1", group_B_case_1 }, - { "2", group_B_case_2 }, - CU_TEST_INFO_NULL, -}; - -static CU_TestInfo group_C_test_cases[] = { - { "1", group_B_case_1 }, - { "1", group_B_case_2 }, /* duplicate test name */ - CU_TEST_INFO_NULL, -}; - -static CU_SuiteInfo suites0[] = { - CU_SUITE_INFO_NULL, -}; - -static CU_SuiteInfo suites1[] = { - { "A1", NULL, NULL, group_A_test_cases }, - { "B1", NULL, NULL, group_B_test_cases }, - CU_SUITE_INFO_NULL, -}; - -static CU_SuiteInfo suites2[] = { - { "A2", NULL, NULL, group_A_test_cases }, - { "B2", NULL, NULL, group_B_test_cases }, - CU_SUITE_INFO_NULL, -}; - -static CU_SuiteInfo suites3[] = { - { "A3", NULL, NULL, group_A_test_cases }, - { "A3", NULL, NULL, group_C_test_cases }, /* duplicate suite name */ - CU_SUITE_INFO_NULL, -}; - -static void test_register_suite(void) -{ - CU_pTestRegistry pReg = NULL; - CU_ErrorCode status; - - if (CU_initialize_registry()) { - fprintf(stderr, "\nError initializing registry in test_register_suite()."); - return; - } - - pReg = CU_get_registry(); - - /* test initial condition */ - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with NULL */ - status = CU_register_suites(NULL); - TEST(CUE_SUCCESS == status); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with empty array */ - status = CU_register_suites(suites0); - TEST(CUE_SUCCESS == status); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with ok array */ - status = CU_register_suites(suites1); - TEST(CUE_SUCCESS == status); - TEST(2 == pReg->uiNumberOfSuites); - TEST(4 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with duplicate suite name */ - status = CU_register_suites(suites1); - TEST(CUE_SUCCESS == status); /* shaky - depends on order of operation in CU_register_suites() */ - TEST(4 == pReg->uiNumberOfSuites); - TEST(8 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with duplicate test name */ - status = CU_register_suites(suites3); - TEST(CUE_DUP_TEST == status); /* shaky - depends on order of operation in CU_register_suites() */ - TEST(6 == pReg->uiNumberOfSuites); - TEST(12 == pReg->uiNumberOfTests); - - CU_cleanup_registry(); - - if (CU_initialize_registry()) { - fprintf(stderr, "\nError initializing registry in test_register_suite()."); - return; - } - - pReg = CU_get_registry(); - - /* test CU_register_nsuites() with ok arrays */ - status = CU_register_nsuites(2, suites1, suites2); - TEST(CUE_SUCCESS == status); - TEST(4 == pReg->uiNumberOfSuites); - TEST(8 == pReg->uiNumberOfTests); -} - -/*--------------------------------------------------*/ -void test_cunit_TestDB(void) -{ - test_cunit_start_tests("TestDB.c"); - - test_CU_initialize_registry(); - test_CU_cleanup_registry(); - test_CU_add_suite(); - test_CU_set_suite_attributes(); - test_get_suite_functions(); - test_CU_add_test(); - test_CU_set_test_attributes(); - test_get_test_functions(); - test_CU_get_registry(); - test_CU_set_registry(); - test_CU_create_new_registry(); - test_cleanup_test_registry(); - test_create_suite(); - test_insert_suite(); - test_create_test(); - test_cleanup_test(); - test_insert_test(); - test_register_suite(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestDB.cpp b/gearsyncd/tests/cunit/TestDB.cpp deleted file mode 100644 index d609f50549..0000000000 --- a/gearsyncd/tests/cunit/TestDB.cpp +++ /dev/null @@ -1,2773 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004,2005,2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of Registry/TestGroup/Testcase management Routines. - * - * Aug 2001 Initial implementation (AK) - * - * 09/Aug/2001 Added startup initialize/cleanup registry functions. (AK) - * - * 29/Aug/2001 Added Test and Group Add functions. (AK) - * - * 02/Oct/2001 Added Proper Error codes and Messages on the failure conditions. (AK) - * - * 13/Oct/2001 Added Code to Check for the Duplicate Group name and test name. (AK) - * - * 15-Jul-2004 Added doxygen comments, new interface, added assertions to - * internal functions, moved error handling code to CUError.c, - * added assertions to make sure no modification of registry - * during a run, bug fixes, changed CU_set_registry() so that it - * doesn't require cleaning the existing registry. (JDS) - * - * 24-Apr-2006 Removed constraint that suites/tests be uniquely named. - * Added ability to turn individual tests/suites on or off. - * Added lookup functions for suites/tests based on index. - * Moved doxygen comments for public API here to header. - * Modified internal unit tests to include these changes. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - */ - -/** @file - * Management functions for tests, suites, and the test registry (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "MyMem.h" -#include "TestDB.h" -#include "TestRun.h" -#include "Util.h" -#include "CUnit_intl.h" - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -static CU_pTestRegistry f_pTestRegistry = NULL; /**< The active internal Test Registry. */ - -/*================================================================= - * Private function forward declarations - *=================================================================*/ -static void cleanup_test_registry(CU_pTestRegistry pRegistry); -static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean); -static void cleanup_suite(CU_pSuite pSuite); -static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite); -static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc); -static void cleanup_test(CU_pTest pTest); -static void insert_test(CU_pSuite pSuite, CU_pTest pTest); - -static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName); -static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -CU_ErrorCode CU_initialize_registry(void) -{ - CU_ErrorCode result; - - assert(CU_FALSE == CU_is_test_running()); - - CU_set_error(result = CUE_SUCCESS); - - if (NULL != f_pTestRegistry) { - CU_cleanup_registry(); - } - - f_pTestRegistry = CU_create_new_registry(); - if (NULL == f_pTestRegistry) { - CU_set_error(result = CUE_NOMEMORY); - } - - return result; -} - -/*------------------------------------------------------------------------*/ -CU_BOOL CU_registry_initialized(void) -{ - return (NULL == f_pTestRegistry) ? CU_FALSE : CU_TRUE; -} - -/*------------------------------------------------------------------------*/ -void CU_cleanup_registry(void) -{ - assert(CU_FALSE == CU_is_test_running()); - - CU_set_error(CUE_SUCCESS); - CU_destroy_existing_registry(&f_pTestRegistry); /* supposed to handle NULL ok */ - CU_clear_previous_results(); - CU_CREATE_MEMORY_REPORT(NULL); -} - -/*------------------------------------------------------------------------*/ -CU_pTestRegistry CU_get_registry(void) -{ - return f_pTestRegistry; -} - -/*------------------------------------------------------------------------*/ -CU_pTestRegistry CU_set_registry(CU_pTestRegistry pRegistry) -{ - CU_pTestRegistry pOldRegistry = f_pTestRegistry; - - assert(CU_FALSE == CU_is_test_running()); - - CU_set_error(CUE_SUCCESS); - f_pTestRegistry = pRegistry; - return pOldRegistry; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_add_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) -{ - CU_pSuite pRetValue = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - assert(CU_FALSE == CU_is_test_running()); - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - pRetValue = create_suite(strName, pInit, pClean); - if (NULL == pRetValue) { - error = CUE_NOMEMORY; - } - else { - if (CU_TRUE == suite_exists(f_pTestRegistry, strName)) { - error = CUE_DUP_SUITE; - } - insert_suite(f_pTestRegistry, pRetValue); - } - } - - CU_set_error(error); - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - pSuite->fActive = fNewActive; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else if (NULL == strNewName) { - result = CUE_NO_SUITENAME; - } - else { - CU_FREE(pSuite->pName); - pSuite->pName = (char *)CU_MALLOC(strlen(strNewName)+1); - strcpy(pSuite->pName, strNewName); - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - pSuite->pInitializeFunc = pNewInit; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - pSuite->pCleanupFunc = pNewClean; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite(const char *strName) -{ - CU_pSuite result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - result = CU_get_suite_by_name(strName, f_pTestRegistry); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite_at_pos(unsigned int pos) -{ - CU_pSuite result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else { - result = CU_get_suite_by_index(pos, f_pTestRegistry); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_suite_pos(CU_pSuite pSuite) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pSuite pCurrentSuite = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else { - pCurrentSuite = f_pTestRegistry->pSuite; - result = 1; - while ((NULL != pCurrentSuite) && (pCurrentSuite != pSuite)) { - ++result; - pCurrentSuite = pCurrentSuite->pNext; - } - if (pCurrentSuite == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_suite_pos_by_name(const char *strName) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pSuite pCurrentSuite = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - pCurrentSuite = f_pTestRegistry->pSuite; - result = 1; - while ((NULL != pCurrentSuite) && (0 != strcmp(pCurrentSuite->pName, strName))) { - ++result; - pCurrentSuite = pCurrentSuite->pNext; - } - if (pCurrentSuite == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc) -{ - CU_pTest pRetValue = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - assert(CU_FALSE == CU_is_test_running()); - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == strName) { - error = CUE_NO_TESTNAME; - } - else if(NULL == pTestFunc) { - error = CUE_NOTEST; - } - else { - pRetValue = create_test(strName, pTestFunc); - if (NULL == pRetValue) { - error = CUE_NOMEMORY; - } - else { - f_pTestRegistry->uiNumberOfTests++; - if (CU_TRUE == test_exists(pSuite, strName)) { - error = CUE_DUP_TEST; - } - insert_test(pSuite, pRetValue); - } - } - - CU_set_error(error); - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_test_active(CU_pTest pTest, CU_BOOL fNewActive) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pTest) { - result = CUE_NOTEST; - } - else { - pTest->fActive = fNewActive; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if (NULL == pTest) { - result = CUE_NOTEST; - } - else if (NULL == strNewName) { - result = CUE_NO_TESTNAME; - } - else { - CU_FREE(pTest->pName); - pTest->pName = (char *)CU_MALLOC(strlen(strNewName)+1); - strcpy(pTest->pName, strNewName); - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc) -{ - CU_ErrorCode result = CUE_SUCCESS; - - if ((NULL == pTest) || (NULL == pNewFunc)) { - result = CUE_NOTEST; - } - else { - pTest->pTestFunc = pNewFunc; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName) -{ - CU_pTest result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == strName) { - error = CUE_NO_SUITENAME; - } - else { - result = CU_get_test_by_name(strName, pSuite); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos) -{ - CU_pTest result = NULL; - CU_ErrorCode error = CUE_SUCCESS; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else { - result = CU_get_test_by_index(pos, pSuite); - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pTest pCurrentTest = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == pTest) { - error = CUE_NOTEST; - } - else { - pCurrentTest = pSuite->pTest; - result = 1; - while ((NULL != pCurrentTest) && (pCurrentTest != pTest)) { - ++result; - pCurrentTest = pCurrentTest->pNext; - } - if (pCurrentTest == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName) -{ - unsigned int result = 0; - CU_ErrorCode error = CUE_SUCCESS; - CU_pTest pCurrentTest = NULL; - - if (NULL == f_pTestRegistry) { - error = CUE_NOREGISTRY; - } - else if (NULL == pSuite) { - error = CUE_NOSUITE; - } - else if (NULL == strName) { - error = CUE_NO_TESTNAME; - } - else { - pCurrentTest = pSuite->pTest; - result = 1; - while ((NULL != pCurrentTest) && (0 != strcmp(pCurrentTest->pName, strName))) { - ++result; - pCurrentTest = pCurrentTest->pNext; - } - if (pCurrentTest == NULL) { - result = 0; - } - } - - CU_set_error(error); - return result; -} - -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ -/* This section is based conceptually on code - * Copyright (C) 2004 Aurema Pty Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Derived from code contributed by K. Cheung and Aurema Pty Ltd. (thanks!) - * int test_group_register(test_group_t *tg) - * int test_suite_register(test_suite_t *ts) - */ -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_register_nsuites(int suite_count, ...) -{ - CU_SuiteInfo *pSuiteItem = NULL; - CU_TestInfo *pTestItem = NULL; - CU_pSuite pSuite = NULL; - - va_list argptr; - int i; - - va_start(argptr, suite_count); - - for (i=0 ; ipName; pSuiteItem++) { - if (NULL != (pSuite = CU_add_suite(pSuiteItem->pName, pSuiteItem->pInitFunc, pSuiteItem->pCleanupFunc))) { - for (pTestItem = pSuiteItem->pTests; NULL != pTestItem->pName; pTestItem++) { - if (NULL == CU_add_test(pSuite, pTestItem->pName, pTestItem->pTestFunc)) { - return CU_get_error(); - } - } - } - else { - return CU_get_error(); - } - } - } - } - return CU_get_error(); -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]) -{ - return CU_register_nsuites(1, suite_info); -} -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ - -/*================================================================= - * Private static function definitions - *=================================================================*/ -/*------------------------------------------------------------------------*/ -/** - * Internal function to clean up the specified test registry. - * cleanup_suite() will be called for each registered suite to perform - * cleanup of the associated test cases. Then, the suite's memory will - * be freed. Note that any pointers to tests or suites in pRegistry - * held by the user will be invalidated by this function. Severe problems - * can occur if this function is called during a test run involving pRegistry. - * Note that memory held for data members in the registry (e.g. pName) and - * the registry itself are not freed by this function. - * - * @see cleanup_suite() - * @see cleanup_test() - * @param pRegistry CU_pTestRegistry to clean up (non-NULL). - */ -static void cleanup_test_registry(CU_pTestRegistry pRegistry) -{ - CU_pSuite pCurSuite = NULL; - CU_pSuite pNextSuite = NULL; - - assert(NULL != pRegistry); - - pCurSuite = pRegistry->pSuite; - while (NULL != pCurSuite) { - pNextSuite = pCurSuite->pNext; - cleanup_suite(pCurSuite); - - CU_FREE(pCurSuite); - pCurSuite = pNextSuite; - } - pRegistry->pSuite = NULL; - pRegistry->uiNumberOfSuites = 0; - pRegistry->uiNumberOfTests = 0; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to create a new test suite having the specified parameters. - * This function creates a new test suite having the specified name and - * initialization/cleanup functions. The new suite is active for execution during - * test runs. The strName cannot be NULL (checked by assertion), but either or - * both function pointers can be. A pointer to the newly-created suite is returned, - * or NULL if there was an error allocating memory for the new suite. It is the - * responsibility of the caller to destroy the returned suite (use cleanup_suite() - * before freeing the returned pointer). - * - * @param strName Name for the new test suite (non-NULL). - * @param pInit Initialization function to call before running suite. - * @param pClean Cleanup function to call after running suite. - * @return A pointer to the newly-created suite (NULL if creation failed) - */ -static CU_pSuite create_suite(const char* strName, CU_InitializeFunc pInit, CU_CleanupFunc pClean) -{ - CU_pSuite pRetValue = (CU_pSuite)CU_MALLOC(sizeof(CU_Suite)); - - assert(NULL != strName); - - if (NULL != pRetValue) { - pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); - if (NULL != pRetValue->pName) { - strcpy(pRetValue->pName, strName); - pRetValue->fActive = CU_TRUE; - pRetValue->pInitializeFunc = pInit; - pRetValue->pCleanupFunc = pClean; - pRetValue->pTest = NULL; - pRetValue->pNext = NULL; - pRetValue->pPrev = NULL; - pRetValue->uiNumberOfTests = 0; - } - else { - CU_FREE(pRetValue); - pRetValue = NULL; - } - } - - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to clean up the specified test suite. - * Each test case registered with pSuite will be freed. Allocated memory held - * by the suite (i.e. the name) will also be deallocated. Severe problems can - * occur if this function is called during a test run involving pSuite. - * - * @param pSuite CU_pSuite to clean up (non-NULL). - * @see cleanup_test_registry() - * @see cleanup_test() - */ -static void cleanup_suite(CU_pSuite pSuite) -{ - CU_pTest pCurTest = NULL; - CU_pTest pNextTest = NULL; - - assert(NULL != pSuite); - - pCurTest = pSuite->pTest; - while (NULL != pCurTest) { - pNextTest = pCurTest->pNext; - - cleanup_test(pCurTest); - - CU_FREE(pCurTest); - pCurTest = pNextTest; - } - if (NULL != pSuite->pName) { - CU_FREE(pSuite->pName); - } - - pSuite->pName = NULL; - pSuite->pTest = NULL; - pSuite->uiNumberOfTests = 0; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to insert a suite into a registry. - * The suite name is assumed to be unique. Internally, the list of suites - * is a double-linked list, which this function manages. Insertion of NULL - * pSuites is not allowed (checked by assertion). Severe problems can occur - * if this function is called during a test run involving pRegistry. - * - * @param pRegistry CU_pTestRegistry to insert into (non-NULL). - * @param pSuite CU_pSuite to insert (non-NULL). - * @see insert_test() - */ -static void insert_suite(CU_pTestRegistry pRegistry, CU_pSuite pSuite) -{ - CU_pSuite pCurSuite = NULL; - - assert(NULL != pRegistry); - assert(NULL != pSuite); - - pCurSuite = pRegistry->pSuite; - - assert(pCurSuite != pSuite); - - pSuite->pNext = NULL; - pRegistry->uiNumberOfSuites++; - - /* if this is the 1st suite to be added... */ - if (NULL == pCurSuite) { - pRegistry->pSuite = pSuite; - pSuite->pPrev = NULL; - } - /* otherwise, add it to the end of the linked list... */ - else { - while (NULL != pCurSuite->pNext) { - pCurSuite = pCurSuite->pNext; - assert(pCurSuite != pSuite); - } - - pCurSuite->pNext = pSuite; - pSuite->pPrev = pCurSuite; - } -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to create a new test case having the specified parameters. - * This function creates a new test having the specified name and test function. - * The strName cannot be NULL (checked by assertion), but the function pointer - * may be. A pointer to the newly-created test is returned, or NULL if there - * was an error allocating memory for the new test. It is the responsibility - * of the caller to destroy the returned test (use cleanup_test() before freeing - * the returned pointer). - * - * @param strName Name for the new test. - * @param pTestFunc Test function to call when running this test. - * @return A pointer to the newly-created test (NULL if creation failed) - */ -static CU_pTest create_test(const char* strName, CU_TestFunc pTestFunc) -{ - CU_pTest pRetValue = (CU_pTest)CU_MALLOC(sizeof(CU_Test)); - - assert(NULL != strName); - - if (NULL != pRetValue) { - pRetValue->pName = (char *)CU_MALLOC(strlen(strName)+1); - if (NULL != pRetValue->pName) { - strcpy(pRetValue->pName, strName); - pRetValue->fActive = CU_TRUE; - pRetValue->pTestFunc = pTestFunc; - pRetValue->pJumpBuf = NULL; - pRetValue->pNext = NULL; - pRetValue->pPrev = NULL; - } - else { - CU_FREE(pRetValue); - pRetValue = NULL; - } - } - - return pRetValue; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to clean up the specified test. - * All memory associated with the test will be freed. Severe problems can - * occur if this function is called during a test run involving pTest. - * - * @param pTest CU_pTest to clean up (non-NULL). - * @see cleanup_test_registry() - * @see cleanup_suite() - */ -static void cleanup_test(CU_pTest pTest) -{ - assert(NULL != pTest); - - if (NULL != pTest->pName) { - CU_FREE(pTest->pName); - } - - pTest->pName = NULL; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to insert a test into a suite. - * The test name is assumed to be unique. Internally, the list of tests in - * a suite is a double-linked list, which this function manages. Neither - * pSuite nor pTest may be NULL (checked by assertion). Further, pTest must - * be an independent test (i.e. both pTest->pNext and pTest->pPrev == NULL), - * which is also checked by assertion. Severe problems can occur if this - * function is called during a test run involving pSuite. - * - * @param pSuite CU_pSuite to insert into (non-NULL). - * @param pTest CU_pTest to insert (non-NULL). - * @see insert_suite() - */ -static void insert_test(CU_pSuite pSuite, CU_pTest pTest) -{ - CU_pTest pCurTest = NULL; - - assert(NULL != pSuite); - assert(NULL != pTest); - assert(NULL == pTest->pNext); - assert(NULL == pTest->pPrev); - - pCurTest = pSuite->pTest; - - assert(pCurTest != pTest); - - pSuite->uiNumberOfTests++; - /* if this is the 1st suite to be added... */ - if (NULL == pCurTest) { - pSuite->pTest = pTest; - pTest->pPrev = NULL; - } - else { - while (NULL != pCurTest->pNext) { - pCurTest = pCurTest->pNext; - assert(pCurTest != pTest); - } - - pCurTest->pNext = pTest; - pTest->pPrev = pCurTest; - } -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to check whether a suite having a specified - * name already exists. - * - * @param pRegistry CU_pTestRegistry to check (non-NULL). - * @param szSuiteName Suite name to check (non-NULL). - * @return CU_TRUE if suite exists in the registry, CU_FALSE otherwise. - */ -static CU_BOOL suite_exists(CU_pTestRegistry pRegistry, const char* szSuiteName) -{ - CU_pSuite pSuite = NULL; - - assert(NULL != pRegistry); - assert(NULL != szSuiteName); - - pSuite = pRegistry->pSuite; - while (NULL != pSuite) { - if ((NULL != pSuite->pName) && (0 == CU_compare_strings(szSuiteName, pSuite->pName))) { - return CU_TRUE; - } - pSuite = pSuite->pNext; - } - - return CU_FALSE; -} - -/*------------------------------------------------------------------------*/ -/** - * Internal function to check whether a test having a specified - * name is already registered in a given suite. - * - * @param pSuite CU_pSuite to check (non-NULL). - * @param szTestName Test case name to check (non-NULL). - * @return CU_TRUE if test exists in the suite, CU_FALSE otherwise. - */ -static CU_BOOL test_exists(CU_pSuite pSuite, const char* szTestName) -{ - CU_pTest pTest = NULL; - - assert(NULL != pSuite); - assert(NULL != szTestName); - - pTest = pSuite->pTest; - while (NULL != pTest) { - if ((NULL != pTest->pName) && (0 == CU_compare_strings(szTestName, pTest->pName))) { - return CU_TRUE; - } - pTest = pTest->pNext; - } - - return CU_FALSE; -} - -/*================================================================= - * Public but primarily internal function definitions - *=================================================================*/ -CU_pTestRegistry CU_create_new_registry(void) -{ - CU_pTestRegistry pRegistry = (CU_pTestRegistry)CU_MALLOC(sizeof(CU_TestRegistry)); - if (NULL != pRegistry) { - pRegistry->pSuite = NULL; - pRegistry->uiNumberOfSuites = 0; - pRegistry->uiNumberOfTests = 0; - } - - return pRegistry; -} - -/*------------------------------------------------------------------------*/ -void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry) -{ - assert(NULL != ppRegistry); - - /* Note - CU_cleanup_registry counts on being able to pass NULL */ - - if (NULL != *ppRegistry) { - cleanup_test_registry(*ppRegistry); - } - CU_FREE(*ppRegistry); - *ppRegistry = NULL; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite_by_name(const char* szSuiteName, CU_pTestRegistry pRegistry) -{ - CU_pSuite pSuite = NULL; - CU_pSuite pCur = NULL; - - assert(NULL != pRegistry); - assert(NULL != szSuiteName); - - pCur = pRegistry->pSuite; - while (NULL != pCur) { - if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szSuiteName))) { - pSuite = pCur; - break; - } - pCur = pCur->pNext; - } - - return pSuite; -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_suite_by_index(unsigned int index, CU_pTestRegistry pRegistry) -{ - CU_pSuite result = NULL; - unsigned int i; - - assert(NULL != pRegistry); - - if ((index > 0) && (index <= f_pTestRegistry->uiNumberOfSuites)) { - result = f_pTestRegistry->pSuite; - for (i=1 ; ipNext; - } - } - - return result; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test_by_name(const char* szTestName, CU_pSuite pSuite) -{ - CU_pTest pTest = NULL; - CU_pTest pCur = NULL; - - assert(NULL != pSuite); - assert(NULL != szTestName); - - pCur = pSuite->pTest; - while (NULL != pCur) { - if ((NULL != pCur->pName) && (0 == CU_compare_strings(pCur->pName, szTestName))) { - pTest = pCur; - break; - } - pCur = pCur->pNext; - } - - return pTest; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_test_by_index(unsigned int index, CU_pSuite pSuite) -{ - CU_pTest result = NULL; - unsigned int i; - - assert(NULL != pSuite); - - if ((index > 0) && (index <= pSuite->uiNumberOfTests)) { - result = pSuite->pTest; - for (i=1 ; ipNext; - } - } - - return result; -} -/** @} */ - -/*------------------------------------------------------------------------*/ -/*------------------------------------------------------------------------*/ -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -static int sfunc1(void) -{ return 0; } - -static void test1(void) -{} - -static void test2(void) -{} - -/*--------------------------------------------------*/ -static void test_CU_initialize_registry(void) -{ - CU_pTestRegistry pReg = NULL; - unsigned int ndeallocs_before; - - /* initial state */ - TEST(NULL == CU_get_registry()); - TEST(CU_FALSE == CU_registry_initialized()); - - /* after normal initialization */ - TEST(CUE_SUCCESS == CU_initialize_registry()); - pReg = CU_get_registry(); - TEST_FATAL(NULL != pReg); - TEST(CU_TRUE == CU_registry_initialized()); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(NULL == pReg->pSuite); - - /* after reinitialization */ - TEST(0 < test_cunit_get_n_memevents(pReg)); - ndeallocs_before = test_cunit_get_n_deallocations(pReg); - TEST(CUE_SUCCESS == CU_initialize_registry()); - TEST((ndeallocs_before + 1) == test_cunit_get_n_deallocations(pReg)); - pReg = CU_get_registry(); - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(NULL == pReg->pSuite); - - /* after cleanup */ - CU_cleanup_registry(); - TEST(NULL == CU_get_registry()); - TEST(CU_FALSE == CU_registry_initialized()); - - /* if malloc fails */ - test_cunit_deactivate_malloc(); - TEST(CUE_NOMEMORY == CU_initialize_registry()); - TEST(NULL == CU_get_registry()); - TEST(CU_FALSE == CU_registry_initialized()); - test_cunit_activate_malloc(); -} - -/*--------------------------------------------------*/ -static void test_CU_cleanup_registry(void) -{ - /* make sure calling with uninitialized registry does not crash */ - CU_cleanup_registry(); - CU_cleanup_registry(); - CU_cleanup_registry(); - CU_cleanup_registry(); - CU_cleanup_registry(); - - /* nothing more to do over test_CU_initialize_registry() */ -} - -/*--------------------------------------------------*/ -/* test CU_add_suite() - * CU_get_suite_by_name() - * CU_get_suite_by_index() - */ -static void test_CU_add_suite(void) -{ - CU_pSuite pSuite = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTestRegistry pReg = NULL; - - CU_cleanup_registry(); /* make sure registry not initialized */ - - /* error condition - registry not initialized */ - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_NOREGISTRY == CU_get_error()); - TEST(NULL == pSuite); - - /* error condition - no name */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite = CU_add_suite(NULL, NULL, NULL); - TEST(CUE_NO_SUITENAME == CU_get_error()); - TEST(NULL == pSuite); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* warning condition - duplicate name */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - pSuite2 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_DUP_SUITE == CU_get_error()); - TEST(NULL != pSuite2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - TEST(!strcmp("suite1", pSuite->pName)); - TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite->pTest == NULL); /* no tests added yet */ - TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite->pNext == pSuite2); /* now have another suite */ - - TEST(!strcmp("suite1", pSuite2->pName)); - TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite2->pTest == NULL); /* no tests added yet */ - TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite2->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite2->pNext == NULL); /* end of the list */ - - /* error condition - memory allocation failure */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - test_cunit_deactivate_malloc(); - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_NOMEMORY == CU_get_error()); - TEST(NULL == pSuite); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - test_cunit_activate_malloc(); - - /* normal creation & cleanup */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == NULL); - - pSuite = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite1", pReg) == pSuite); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == NULL); - TEST(pReg->pSuite == pSuite); - - TEST(!strcmp("suite1", pSuite->pName)); - TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite->pTest == NULL); /* no tests added yet */ - TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite->pNext == NULL); /* no more suites added yet */ - - pSuite2 = CU_add_suite("suite2", sfunc1, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite2", pReg) == pSuite2); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == pSuite2); - TEST(CU_get_suite_by_index(3, pReg) == NULL); - - pSuite3 = CU_add_suite("suite3", NULL, sfunc1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite3); - TEST(3 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite3", pReg) == pSuite3); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == pSuite2); - TEST(CU_get_suite_by_index(3, pReg) == pSuite3); - TEST(CU_get_suite_by_index(4, pReg) == NULL); - - pSuite4 = CU_add_suite("suite4", sfunc1, sfunc1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pSuite4); - TEST(4 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(CU_get_suite_by_name("suite4", pReg) == pSuite4); - TEST(CU_get_suite_by_index(0, pReg) == NULL); - TEST(CU_get_suite_by_index(1, pReg) == pSuite); - TEST(CU_get_suite_by_index(2, pReg) == pSuite2); - TEST(CU_get_suite_by_index(3, pReg) == pSuite3); - TEST(CU_get_suite_by_index(4, pReg) == pSuite4); - TEST(CU_get_suite_by_index(5, pReg) == NULL); - - /* test registry suite structures */ - TEST(pReg->pSuite == pSuite); - - TEST(!strcmp("suite1", pSuite->pName)); - TEST(pSuite->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite->pTest == NULL); /* no tests added yet */ - TEST(pSuite->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite->pNext == pSuite2); /* now have another suite */ - - TEST(!strcmp("suite2", pSuite2->pName)); - TEST(pSuite2->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite2->pTest == NULL); /* no tests added yet */ - TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite2->pInitializeFunc == sfunc1); /* no init function */ - TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite2->pNext == pSuite3); /* next suite in list */ - - TEST(!strcmp("suite3", pSuite3->pName)); - TEST(pSuite3->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite3->pTest == NULL); /* no tests added yet */ - TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite3->pCleanupFunc == sfunc1); /* no cleanup function */ - TEST(pSuite3->pNext == pSuite4); /* next suite in list */ - - TEST(!strcmp("suite4", pSuite4->pName)); - TEST(pSuite4->fActive == CU_TRUE); /* suite active on creation */ - TEST(pSuite4->pTest == NULL); /* no tests added yet */ - TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ - TEST(pSuite4->pCleanupFunc == sfunc1); /* no cleanup function */ - TEST(pSuite4->pNext == NULL); /* end of suite list */ - - TEST(0 != test_cunit_get_n_memevents(pSuite)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pSuite3)); - TEST(0 != test_cunit_get_n_memevents(pSuite4)); - - TEST(test_cunit_get_n_allocations(pSuite) != test_cunit_get_n_deallocations(pSuite)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); - - /* clean up everything and confirm deallocation */ - CU_cleanup_registry(); - - TEST(test_cunit_get_n_allocations(pSuite) == test_cunit_get_n_deallocations(pSuite)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); -} - -/*--------------------------------------------------*/ -/* test CU_set_suite_active() - * CU_set_suite_name() - * CU_set_suite_initfunc() - * CU_set_suite_cleanupfunc() - */ -static void test_CU_set_suite_attributes(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - - /* initialize system */ - CU_initialize_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST_FATAL(NULL != pSuite1); - TEST_FATAL(NULL != CU_add_test(pSuite1, "test1", test1)); - TEST_FATAL(NULL != CU_add_test(pSuite1, "test2", test1)); - - pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ - TEST_FATAL(NULL != pSuite2); - TEST_FATAL(NULL != CU_add_test(pSuite2, "test1", test1)); - TEST_FATAL(NULL != CU_add_test(pSuite2, "test2", test1)); - - /* test CU_set_suite_active() */ - TEST(CUE_NOSUITE == CU_set_suite_active(NULL, CU_FALSE)); /* error - NULL suite */ - - TEST(pSuite2->fActive == CU_TRUE); /* suites active on creation */ - TEST(pSuite1->fActive == CU_TRUE); - - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); - TEST(pSuite1->fActive == CU_TRUE); - TEST(pSuite2->fActive == CU_FALSE); - - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_FALSE)); - TEST(pSuite1->fActive == CU_FALSE); - TEST(pSuite2->fActive == CU_FALSE); - - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_suite_active(pSuite2, CU_TRUE)); - TEST(pSuite1->fActive == CU_FALSE); - TEST(pSuite2->fActive == CU_TRUE); - - /* test CU_set_suite_name() */ - TEST(CUE_NOSUITE == CU_set_suite_name(NULL, "null suite")); /* error - NULL suite */ - TEST(CUE_NO_SUITENAME == CU_set_suite_name(pSuite1, NULL)); /* error - NULL name */ - - TEST(!strcmp(pSuite1->pName, "suite1")); - TEST(!strcmp(pSuite2->pName, "suite2")); - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "This is my new name.")); - TEST(!strcmp(pSuite1->pName, "This is my new name.")); - TEST(!strcmp(pSuite2->pName, "suite2")); - - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "Never mind.")); - TEST(!strcmp(pSuite1->pName, "This is my new name.")); - TEST(!strcmp(pSuite2->pName, "Never mind.")); - - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite1, "suite1")); - TEST(CUE_SUCCESS == CU_set_suite_name(pSuite2, "suite2")); - TEST(!strcmp(pSuite1->pName, "suite1")); - TEST(!strcmp(pSuite2->pName, "suite2")); - - /* test CU_set_suite_initfunc() */ - TEST(CUE_NOSUITE == CU_set_suite_initfunc(NULL, &sfunc1)); /* error - NULL suite */ - - TEST(pSuite1->pInitializeFunc == NULL); - TEST(pSuite2->pInitializeFunc == &sfunc1); - TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite1, &sfunc1)); - TEST(pSuite1->pInitializeFunc == &sfunc1); - TEST(pSuite2->pInitializeFunc == &sfunc1); - - TEST(CUE_SUCCESS == CU_set_suite_initfunc(pSuite2, NULL)); - TEST(pSuite1->pInitializeFunc == &sfunc1); - TEST(pSuite2->pInitializeFunc == NULL); - - /* test CU_set_suite_cleanupfunc() */ - TEST(CUE_NOSUITE == CU_set_suite_cleanupfunc(NULL, &sfunc1)); - - TEST(pSuite1->pCleanupFunc == NULL); - TEST(pSuite2->pCleanupFunc == NULL); - TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite1, &sfunc1)); - TEST(pSuite1->pCleanupFunc == &sfunc1); - TEST(pSuite2->pCleanupFunc == NULL); - - TEST(CUE_SUCCESS == CU_set_suite_cleanupfunc(pSuite2, &sfunc1)); - TEST(pSuite1->pCleanupFunc == &sfunc1); - TEST(pSuite2->pCleanupFunc == &sfunc1); - - /* clean up */ - CU_cleanup_registry(); -} - -static void test_succeed(void) { CU_TEST(CU_TRUE); } -static void test_fail(void) { CU_TEST(CU_FALSE); } -static int suite_succeed(void) { return 0; } -static int suite_fail(void) { return 1; } - -/*--------------------------------------------------*/ -/* test CU_get_suite() - * CU_get_suite_at_pos() - * CU_get_suite_pos() - * CU_get_suite_pos_by_name() - */ -static void test_get_suite_functions(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pSuite pSuite5 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - CU_pTest pTest10 = NULL; - - /* error condition - registry not initialized */ - CU_cleanup_registry(); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_suite("suite1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_suite_at_pos(0)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos(pSuite1)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos_by_name("suite1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test9", test_succeed); - pSuite4 = CU_add_suite("suite4", NULL, suite_fail); - pTest10 = CU_add_test(pSuite4, "test10", test_succeed); - - /* error condition - invalid parameters */ - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_suite(NULL)); - TEST(CUE_NO_SUITENAME == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos(NULL)); - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_suite_pos_by_name(NULL)); - TEST(CUE_NO_SUITENAME == CU_get_error()); - - /* normal operation - CU_get_suite() */ - - TEST(NULL == CU_get_suite("")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_suite("bad name")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_suite("suite3")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pSuite1 == CU_get_suite("suite1")); /* valid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite2 == CU_get_suite("suite2")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite4 == CU_get_suite("suite4")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_suite_at_pos() */ - - TEST(NULL == CU_get_suite_at_pos(0)); /* invalid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_suite_at_pos(5)); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pSuite1 == CU_get_suite_at_pos(1)); /* valid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite2 == CU_get_suite_at_pos(2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite3 == CU_get_suite_at_pos(3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pSuite4 == CU_get_suite_at_pos(4)); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_suite_pos() */ - - pSuite5 = (CU_pSuite)malloc(sizeof(CU_Suite)); - TEST_FATAL(NULL != pSuite5); - - TEST(0 == CU_get_suite_pos(pSuite5)); /* invalid suite */ - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_suite_pos(pSuite1)); /* valid suites */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_suite_pos(pSuite2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(3 == CU_get_suite_pos(pSuite3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_suite_pos(pSuite4)); - TEST(CUE_SUCCESS == CU_get_error()); - - free(pSuite5); - - /* normal operation - CU_get_suite_pos_by_name() */ - - TEST(0 == CU_get_suite_pos_by_name("")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_suite_pos_by_name("suite3")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_suite_pos_by_name("suite1")); /* valid suites */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_suite_pos_by_name("suite2")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_suite_pos_by_name("suite4")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* clean up */ - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -/* test CU_add_test() - * CU_get_test_by_name() - * CU_get_test_by_index() - */ -static void test_CU_add_test(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTestRegistry pReg = NULL; - - CU_cleanup_registry(); - - /* error condition - registry not initialized */ - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_NOREGISTRY == CU_get_error()); - TEST(NULL == pTest1); - - CU_initialize_registry(); - pReg = CU_get_registry(); - - /* error condition - no suite */ - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_NOSUITE == CU_get_error()); - TEST(NULL == pTest1); - - /* error condition - no name */ - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - pTest1 = CU_add_test(pSuite1, NULL, test1); - TEST(CUE_NO_TESTNAME == CU_get_error()); - TEST(NULL == pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(0 == pSuite1->uiNumberOfTests); - - /* error condition - no test function */ - pTest1 = CU_add_test(pSuite1, "test1", NULL); - TEST(CUE_NOTEST == CU_get_error()); - TEST(NULL == pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(0 == pSuite1->uiNumberOfTests); - - /* warning condition - duplicate name */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(1 == pReg->uiNumberOfTests); - TEST(1 == pSuite1->uiNumberOfTests); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == NULL); - - pTest2 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_DUP_TEST == CU_get_error()); - TEST(NULL != pTest2); - TEST(1 == pReg->uiNumberOfSuites); - TEST(2 == pReg->uiNumberOfTests); - TEST(2 == pSuite1->uiNumberOfTests); - - TEST(!strcmp("test1", pTest1->pName)); - TEST(pTest1->fActive == CU_TRUE); - TEST(pTest1->pNext == pTest2); - TEST(pTest1->pJumpBuf == NULL); - TEST(pTest1->pTestFunc == test1); - TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == pTest2); - TEST(CU_get_test_by_index(3, pSuite1) == NULL); - - TEST(!strcmp("test1", pTest2->pName)); - TEST(pTest2->fActive == CU_TRUE); - TEST(pTest2->pNext == NULL); - TEST(pTest2->pJumpBuf == NULL); - TEST(pTest2->pTestFunc == test1); - TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); - - /* error condition - memory allocation failure */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - test_cunit_deactivate_malloc(); - pTest1 = CU_add_test(pSuite1, "test1", test1); - test_cunit_activate_malloc(); - TEST(CUE_NOMEMORY == CU_get_error()); - TEST(NULL == pTest1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(0 == pSuite1->uiNumberOfTests); - - /* normal creation & cleanup */ - CU_initialize_registry(); - pReg = CU_get_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST(CUE_SUCCESS == CU_get_error()); - pSuite2 = CU_add_suite("suite2", sfunc1, sfunc1); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == NULL); - - pTest1 = CU_add_test(pSuite1, "test1", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest1); - TEST(2 == pReg->uiNumberOfSuites); - TEST(1 == pReg->uiNumberOfTests); - TEST(1 == pSuite1->uiNumberOfTests); - TEST(0 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == NULL); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == NULL); - - pTest2 = CU_add_test(pSuite2, "test2", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(2 == pReg->uiNumberOfTests); - TEST(1 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest2); - TEST(CU_get_test_by_index(0, pSuite2) == NULL); - TEST(CU_get_test_by_index(1, pSuite2) == pTest2); - TEST(CU_get_test_by_index(2, pSuite2) == NULL); - - pTest3 = CU_add_test(pSuite1, "test3", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest3); - TEST(2 == pReg->uiNumberOfSuites); - TEST(3 == pReg->uiNumberOfTests); - TEST(2 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest2); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == pTest3); - TEST(CU_get_test_by_index(3, pSuite1) == NULL); - - pTest4 = CU_add_test(pSuite1, "test4", test1); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL != pTest4); - TEST(2 == pReg->uiNumberOfSuites); - TEST(4 == pReg->uiNumberOfTests); - TEST(3 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest2); - TEST(CU_get_test_by_index(0, pSuite1) == NULL); - TEST(CU_get_test_by_index(1, pSuite1) == pTest1); - TEST(CU_get_test_by_index(2, pSuite1) == pTest3); - TEST(CU_get_test_by_index(3, pSuite1) == pTest4); - TEST(CU_get_test_by_index(4, pSuite1) == NULL); - - TEST(!strcmp("test1", pTest1->pName)); - TEST(pTest1->pNext == pTest3); - TEST(pTest1->pJumpBuf == NULL); - TEST(pTest1->pTestFunc == test1); - TEST(CU_get_test_by_name("test1", pSuite1) == pTest1); - TEST(CU_get_test_by_name("test1", pSuite2) == NULL); - - TEST(!strcmp("test2", pTest2->pName)); - TEST(pTest2->pNext == NULL); - TEST(pTest2->pJumpBuf == NULL); - TEST(pTest2->pTestFunc == test1); - TEST(CU_get_test_by_name("test2", pSuite1) == NULL); - TEST(CU_get_test_by_name("test2", pSuite2) == pTest2); - - TEST(!strcmp("test3", pTest3->pName)); - TEST(pTest3->pNext == pTest4); - TEST(pTest3->pJumpBuf == NULL); - TEST(pTest3->pTestFunc == test1); - TEST(CU_get_test_by_name("test3", pSuite1) == pTest3); - TEST(CU_get_test_by_name("test3", pSuite2) == NULL); - - TEST(!strcmp("test4", pTest4->pName)); - TEST(pTest4->pNext == NULL); - TEST(pTest4->pJumpBuf == NULL); - TEST(pTest4->pTestFunc == test1); - TEST(CU_get_test_by_name("test4", pSuite1) == pTest4); - TEST(CU_get_test_by_name("test4", pSuite2) == NULL); - - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - TEST(0 != test_cunit_get_n_memevents(pTest3)); - TEST(0 != test_cunit_get_n_memevents(pTest4)); - - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); - - CU_cleanup_registry(); - - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); -} - -/*--------------------------------------------------*/ -/* test CU_set_test_active() - * CU_set_test_name() - * CU_set_test_func() - */ -static void test_CU_set_test_attributes(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - - /* initialize system */ - CU_initialize_registry(); - - pSuite1 = CU_add_suite("suite1", NULL, NULL); - TEST_FATAL(NULL != pSuite1); - pTest1 = CU_add_test(pSuite1, "test1", test2); - TEST_FATAL(NULL != pTest1); - pTest2 = CU_add_test(pSuite1, "test2", test1); - TEST_FATAL(NULL != pTest2); - - pSuite2 = CU_add_suite("suite2", sfunc1, NULL); /* add another suite */ - TEST_FATAL(NULL != pSuite2); - pTest3 = CU_add_test(pSuite2, "test3", test2); - TEST_FATAL(NULL != pTest3); - pTest4 = CU_add_test(pSuite2, "test4", test1); - TEST_FATAL(NULL != pTest4); - - /* test CU_set_test_active() */ - TEST(CUE_NOTEST == CU_set_test_active(NULL, CU_FALSE)); /* error - NULL test */ - - TEST(CU_TRUE == pTest1->fActive); /* tests active on creation */ - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_TRUE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); - TEST(CU_TRUE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_FALSE == pTest3->fActive); - TEST(CU_FALSE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); - TEST(CU_FALSE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_FALSE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_FALSE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_FALSE)); - TEST(CU_FALSE == pTest1->fActive); - TEST(CU_FALSE == pTest2->fActive); - TEST(CU_FALSE == pTest3->fActive); - TEST(CU_FALSE == pTest4->fActive); - - TEST(CUE_SUCCESS == CU_set_test_active(pTest1, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest2, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest3, CU_TRUE)); - TEST(CUE_SUCCESS == CU_set_test_active(pTest4, CU_TRUE)); - TEST(CU_TRUE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_TRUE == pTest4->fActive); - - /* test CU_set_test_name() */ - TEST(CUE_NOTEST == CU_set_test_name(NULL, "null test")); /* error - NULL test */ - TEST(CUE_NO_TESTNAME == CU_set_test_name(pTest1, NULL)); /* error - NULL name */ - - TEST(!strcmp("test1", pTest1->pName)); - TEST(!strcmp("test2", pTest2->pName)); - TEST(!strcmp("test3", pTest3->pName)); - TEST(!strcmp("test4", pTest4->pName)); - - TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "Aren't I a pretty girl?")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "Polly want a cracker.")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "@This is utter nonsense@")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "Yep!")); - - TEST(!strcmp("Aren't I a pretty girl?", pTest1->pName)); - TEST(!strcmp("Polly want a cracker.", pTest2->pName)); - TEST(!strcmp("@This is utter nonsense@", pTest3->pName)); - TEST(!strcmp("Yep!", pTest4->pName)); - - TEST(CUE_SUCCESS == CU_set_test_name(pTest1, "test1")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest2, "test2")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest3, "test3")); - TEST(CUE_SUCCESS == CU_set_test_name(pTest4, "test4")); - - TEST(!strcmp("test1", pTest1->pName)); - TEST(!strcmp("test2", pTest2->pName)); - TEST(!strcmp("test3", pTest3->pName)); - TEST(!strcmp("test4", pTest4->pName)); - - /* test CU_set_test_func() */ - TEST(CUE_NOTEST == CU_set_test_func(NULL, &test1)); /* error - NULL test */ - TEST(CUE_NOTEST == CU_set_test_func(pTest1, NULL)); /* error - NULL test function */ - - TEST(&test2 == pTest1->pTestFunc); - TEST(&test1 == pTest2->pTestFunc); - TEST(&test2 == pTest3->pTestFunc); - TEST(&test1 == pTest4->pTestFunc); - - TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test1)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test2)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test1)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test2)); - - TEST(&test1 == pTest1->pTestFunc); - TEST(&test2 == pTest2->pTestFunc); - TEST(&test1 == pTest3->pTestFunc); - TEST(&test2 == pTest4->pTestFunc); - - TEST(CUE_SUCCESS == CU_set_test_func(pTest1, &test2)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest2, &test1)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest3, &test2)); - TEST(CUE_SUCCESS == CU_set_test_func(pTest4, &test1)); - - /* clean up */ - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -/* test CU_get_test() - * CU_get_test_at_pos() - * CU_get_test_pos() - * CU_get_test_pos_by_name() - */ -static void test_get_test_functions(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - CU_pTest pTest10 = NULL; - - /* error condition - registry not initialized */ - CU_cleanup_registry(); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test(pSuite1, "test1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos(pSuite1, pTest1)); - TEST(CUE_NOREGISTRY == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos_by_name(pSuite1, "test1")); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name */ - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test9", test_succeed); - pSuite4 = CU_add_suite("suite4", NULL, suite_fail); - pTest10 = CU_add_test(pSuite4, "test10", test_succeed); - - /* error condition - invalid parameters */ - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test(NULL, "test1")); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(NULL == CU_get_test(pSuite1, NULL)); /* name NULL */ - TEST(CUE_NO_SUITENAME == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_at_pos(NULL, 1)); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos(NULL, pTest1)); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos(pSuite1, NULL)); /* test NULL */ - TEST(CUE_NOTEST == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos_by_name(NULL, "test1")); /* suite NULL */ - TEST(CUE_NOSUITE == CU_get_error()); - - CU_set_error(CUE_SUCCESS); - TEST(0 == CU_get_test_pos_by_name(pSuite1, NULL)); /* name NULL */ - TEST(CUE_NO_TESTNAME == CU_get_error()); - - /* normal operation - CU_get_test() */ - - TEST(NULL == CU_get_test(pSuite1, "")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test(pSuite2, "bad name")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test(pSuite1, "test3")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pTest1 == CU_get_test(pSuite1, "test1")); /* valid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest6 == CU_get_test(pSuite2, "test6")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest8 == CU_get_test(pSuite3, "test8")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_test_at_pos() */ - - TEST(NULL == CU_get_test_at_pos(pSuite1, 0)); /* invalid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test_at_pos(pSuite1, 6)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(NULL == CU_get_test_at_pos(pSuite4, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(pTest1 == CU_get_test_at_pos(pSuite1, 1)); /* valid positions */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest2 == CU_get_test_at_pos(pSuite1, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest3 == CU_get_test_at_pos(pSuite1, 3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest4 == CU_get_test_at_pos(pSuite1, 4)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest5 == CU_get_test_at_pos(pSuite1, 5)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest6 == CU_get_test_at_pos(pSuite2, 1)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest7 == CU_get_test_at_pos(pSuite2, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest8 == CU_get_test_at_pos(pSuite3, 1)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest9 == CU_get_test_at_pos(pSuite3, 2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(pTest10 == CU_get_test_at_pos(pSuite4, 1)); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_test_pos() */ - - TEST(0 == CU_get_test_pos(pSuite1, pTest6)); /* invalid tests */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_test_pos(pSuite4, pTest6)); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_test_pos(pSuite1, pTest1)); /* valid tests */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos(pSuite1, pTest2)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(3 == CU_get_test_pos(pSuite1, pTest3)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_test_pos(pSuite1, pTest4)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(5 == CU_get_test_pos(pSuite1, pTest5)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos(pSuite2, pTest6)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos(pSuite2, pTest7)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos(pSuite3, pTest8)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos(pSuite3, pTest9)); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos(pSuite4, pTest10)); - TEST(CUE_SUCCESS == CU_get_error()); - - /* normal operation - CU_get_test_pos_by_name() */ - - TEST(0 == CU_get_test_pos_by_name(pSuite1, "")); /* invalid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_test_pos_by_name(pSuite1, "test9")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(0 == CU_get_test_pos_by_name(pSuite2, "test1")); - TEST(CUE_SUCCESS == CU_get_error()); - - TEST(1 == CU_get_test_pos_by_name(pSuite1, "test1")); /* valid names */ - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos_by_name(pSuite1, "test2")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(4 == CU_get_test_pos_by_name(pSuite1, "test4")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos_by_name(pSuite2, "test6")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos_by_name(pSuite2, "test7")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos_by_name(pSuite3, "test8")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(2 == CU_get_test_pos_by_name(pSuite3, "test9")); - TEST(CUE_SUCCESS == CU_get_error()); - TEST(1 == CU_get_test_pos_by_name(pSuite4, "test10")); - TEST(CUE_SUCCESS == CU_get_error()); - - /* clean up */ - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -static void test_CU_get_registry(void) -{ - CU_cleanup_registry(); - TEST(NULL == CU_get_registry()); - - CU_initialize_registry(); - TEST(NULL != CU_get_registry()); - TEST(f_pTestRegistry == CU_get_registry()); - - CU_cleanup_registry(); -} - -/*--------------------------------------------------*/ -static void test_CU_set_registry(void) -{ - CU_pTestRegistry pReg1 = NULL; - CU_pTestRegistry pReg2 = NULL; - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pSuite2 = CU_add_suite("suite2", NULL, NULL); - - CU_add_test(pSuite1, "test1", test1); - CU_add_test(pSuite1, "test2", test1); - CU_add_test(pSuite2, "test1", test1); - CU_add_test(pSuite2, "test2", test1); - - pReg1 = CU_get_registry(); - - TEST(pReg1->pSuite == pSuite1); - TEST(pReg1->uiNumberOfSuites == 2); - TEST(pReg1->uiNumberOfTests == 4); - TEST(0 < test_cunit_get_n_memevents(pReg1)); - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - CU_set_registry(NULL); - - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - CU_cleanup_registry(); - - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - pReg2 = CU_create_new_registry(); - CU_set_registry(pReg2); - - TEST(pReg1->pSuite == pSuite1); - TEST(pReg1->uiNumberOfSuites == 2); - TEST(pReg1->uiNumberOfTests == 4); - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - - TEST(CU_get_registry()->pSuite == NULL); - TEST(CU_get_registry()->uiNumberOfSuites == 0); - TEST(CU_get_registry()->uiNumberOfTests == 0); - TEST(0 < test_cunit_get_n_memevents(pReg2)); - TEST(test_cunit_get_n_allocations(pReg2) != test_cunit_get_n_deallocations(pReg2)); - - CU_cleanup_registry(); - - TEST(pReg1->pSuite == pSuite1); - TEST(pReg1->uiNumberOfSuites == 2); - TEST(pReg1->uiNumberOfTests == 4); - TEST(test_cunit_get_n_allocations(pReg1) != test_cunit_get_n_deallocations(pReg1)); - TEST(test_cunit_get_n_allocations(pReg2) == test_cunit_get_n_deallocations(pReg2)); - - CU_set_registry(pReg1); - CU_cleanup_registry(); - TEST(test_cunit_get_n_allocations(pReg1) == test_cunit_get_n_deallocations(pReg1)); -} - -/*--------------------------------------------------*/ -/* test CU_create_new_registry() - * CU_destroy_existing_registry() - */ -static void test_CU_create_new_registry(void) -{ - CU_pTestRegistry pReg = NULL; - CU_pTestRegistry pRegOld = NULL; - - CU_cleanup_registry(); - pReg = CU_create_new_registry(); - - TEST(NULL != pReg); - TEST(0 < test_cunit_get_n_memevents(pReg)); - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - - TEST(pReg->pSuite == NULL); - TEST(pReg->uiNumberOfSuites == 0); - TEST(pReg->uiNumberOfTests == 0); - - CU_cleanup_registry(); - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - - pRegOld = pReg; - CU_destroy_existing_registry(&pReg); - TEST(test_cunit_get_n_allocations(pRegOld) == test_cunit_get_n_deallocations(pRegOld)); - TEST(NULL == pReg); -} - -/*--------------------------------------------------*/ -static void test_cleanup_test_registry(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTestRegistry pReg = CU_create_new_registry(); - - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* create tests to register */ - pTest1 = create_test("test1", test1); - pTest2 = create_test("test2", NULL); - pTest3 = create_test("test3", test1); - pTest4 = create_test("", NULL); - - /* create suites to hold tests */ - pSuite1 = create_suite("suite1", NULL, NULL); - pSuite2 = create_suite("suite2", sfunc1, sfunc1); - insert_suite(pReg, pSuite1); - insert_suite(pReg, pSuite2); - - insert_test(pSuite1, pTest1); - insert_test(pSuite1, pTest2); - insert_test(pSuite1, pTest3); - insert_test(pSuite2, pTest4); - - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); /* not managed in primitive functions */ - TEST(3 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest4); - TEST(pTest1->pNext == pTest2); - TEST(pTest1->pPrev == NULL); - TEST(pTest2->pNext == pTest3); - TEST(pTest2->pPrev == pTest1); - TEST(pTest3->pNext == NULL); - TEST(pTest3->pPrev == pTest2); - TEST(pTest4->pNext == NULL); - TEST(pTest4->pPrev == NULL); - - TEST(0 != test_cunit_get_n_memevents(pReg)); - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - TEST(0 != test_cunit_get_n_memevents(pTest3)); - TEST(0 != test_cunit_get_n_memevents(pTest4)); - - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); - - cleanup_test_registry(pReg); - CU_FREE(pReg); - - TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); -} - -/*--------------------------------------------------*/ -/* test create_suite() - * cleanup_suite() - */ -static void test_create_suite(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - - /* error condition - memory allocation failure */ - test_cunit_deactivate_malloc(); - pSuite1 = create_suite("suite1", NULL, NULL); - TEST(NULL == pSuite1); - test_cunit_activate_malloc(); - - /* normal creation & cleanup */ - pSuite1 = create_suite("suite1", NULL, NULL); - TEST(NULL != pSuite1); - TEST(!strcmp("suite1", pSuite1->pName)); - TEST(pSuite1->pTest == NULL); /* no tests added yet */ - TEST(pSuite1->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite1->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite1->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite1->pNext == NULL); /* no more suites added yet */ - - pSuite2 = create_suite("suite2", sfunc1, NULL); - TEST(NULL != pSuite2); - TEST(!strcmp("suite2", pSuite2->pName)); - TEST(pSuite2->pTest == NULL); /* no tests added yet */ - TEST(pSuite2->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite2->pInitializeFunc == sfunc1); /* init function */ - TEST(pSuite2->pCleanupFunc == NULL); /* no cleanup function */ - TEST(pSuite2->pNext == NULL); /* no more suites added yet */ - - pSuite3 = create_suite("suite3", NULL, sfunc1); - TEST(NULL != pSuite3); - TEST(!strcmp("suite3", pSuite3->pName)); - TEST(pSuite3->pTest == NULL); /* no tests added yet */ - TEST(pSuite3->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite3->pInitializeFunc == NULL); /* no init function */ - TEST(pSuite3->pCleanupFunc == sfunc1); /* cleanup function */ - TEST(pSuite3->pNext == NULL); /* no more suites added yet */ - - pSuite4 = create_suite("suite4", sfunc1, sfunc1); - TEST(NULL != pSuite4); - TEST(!strcmp("suite4", pSuite4->pName)); - TEST(pSuite4->pTest == NULL); /* no tests added yet */ - TEST(pSuite4->uiNumberOfTests == 0); /* no tests added yet */ - TEST(pSuite4->pInitializeFunc == sfunc1); /* no init function */ - TEST(pSuite4->pCleanupFunc == sfunc1); /* cleanup function */ - TEST(pSuite4->pNext == NULL); /* no more suites added yet */ - - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - cleanup_suite(pSuite1); - CU_FREE(pSuite1); - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - cleanup_suite(pSuite2); - CU_FREE(pSuite2); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - - TEST(0 != test_cunit_get_n_memevents(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); - cleanup_suite(pSuite3); - CU_FREE(pSuite3); - TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); - - TEST(0 != test_cunit_get_n_memevents(pSuite4)); - TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); - cleanup_suite(pSuite4); - CU_FREE(pSuite4); - TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); -} - -/*--------------------------------------------------*/ -/* test insert_suite() - * suite_exists() - */ -static void test_insert_suite(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTestRegistry pReg = CU_create_new_registry(); - - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(NULL == pReg->pSuite); - TEST(CU_FALSE == suite_exists(pReg, "suite1")); - TEST(CU_FALSE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - /* normal creation & cleanup */ - pSuite1 = create_suite("suite1", NULL, NULL); - insert_suite(pReg, pSuite1); - TEST(1 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_FALSE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - pSuite2 = create_suite("suite2", sfunc1, NULL); - insert_suite(pReg, pSuite2); - TEST(2 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == pSuite2); - TEST(pSuite2->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_TRUE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - pSuite3 = create_suite("suite3", NULL, sfunc1); - insert_suite(pReg, pSuite3); - TEST(3 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == pSuite2); - TEST(pSuite2->pNext == pSuite3); - TEST(pSuite3->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_TRUE == suite_exists(pReg, "suite2")); - TEST(CU_TRUE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - pSuite4 = create_suite("suite4", sfunc1, sfunc1); - insert_suite(pReg, pSuite4); - TEST(4 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - TEST(pReg->pSuite == pSuite1); - TEST(pSuite1->pNext == pSuite2); - TEST(pSuite2->pNext == pSuite3); - TEST(pSuite3->pNext == pSuite4); - TEST(pSuite4->pNext == NULL); - TEST(CU_TRUE == suite_exists(pReg, "suite1")); - TEST(CU_TRUE == suite_exists(pReg, "suite2")); - TEST(CU_TRUE == suite_exists(pReg, "suite3")); - TEST(CU_TRUE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - - TEST(0 != test_cunit_get_n_memevents(pReg)); - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pSuite3)); - TEST(0 != test_cunit_get_n_memevents(pSuite4)); - - TEST(test_cunit_get_n_allocations(pReg) != test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) != test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) != test_cunit_get_n_deallocations(pSuite4)); - - cleanup_test_registry(pReg); - TEST(CU_FALSE == suite_exists(pReg, "suite1")); - TEST(CU_FALSE == suite_exists(pReg, "suite2")); - TEST(CU_FALSE == suite_exists(pReg, "suite3")); - TEST(CU_FALSE == suite_exists(pReg, "suite4")); - TEST(CU_FALSE == suite_exists(pReg, "suite5")); - TEST(CU_FALSE == suite_exists(pReg, "")); - CU_FREE(pReg); - - TEST(test_cunit_get_n_allocations(pReg) == test_cunit_get_n_deallocations(pReg)); - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pSuite3) == test_cunit_get_n_deallocations(pSuite3)); - TEST(test_cunit_get_n_allocations(pSuite4) == test_cunit_get_n_deallocations(pSuite4)); -} - -/*--------------------------------------------------*/ -static void test_create_test(void) -{ - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - - /* error condition - memory allocation failure */ - test_cunit_deactivate_malloc(); - pTest1 = create_test("test1", test1); - test_cunit_activate_malloc(); - TEST(NULL == pTest1); - - /* normal creation & cleanup */ - pTest1 = create_test("test1", test1); - TEST(NULL != pTest1); - TEST(pTest1->pTestFunc == test1); - TEST(!strcmp("test1", pTest1->pName)); - TEST(pTest1->pNext == NULL); - TEST(pTest1->pPrev == NULL); - TEST(pTest1->pJumpBuf == NULL); - - pTest2= create_test("test2", NULL); - TEST(NULL != pTest2); - TEST(pTest2->pTestFunc == NULL); - TEST(!strcmp("test2", pTest2->pName)); - TEST(pTest2->pNext == NULL); - TEST(pTest2->pPrev == NULL); - TEST(pTest2->pJumpBuf == NULL); - - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - - cleanup_test(pTest1); - CU_FREE(pTest1); - cleanup_test(pTest2); - CU_FREE(pTest2); - - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); -} - -/*--------------------------------------------------*/ -/* test insert_test() - * test_exists() - */ -static void test_insert_test(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - - /* create tests to register */ - pTest1 = create_test("test1", test1); - pTest2 = create_test("test2", NULL); - pTest3 = create_test("test3", test1); - pTest4 = create_test("", NULL); - - /* create suites to hold tests */ - pSuite1 = create_suite("suite1", NULL, NULL); - pSuite2 = create_suite("suite2", sfunc1, sfunc1); - - TEST(CU_FALSE == test_exists(pSuite1, "test1")); - TEST(CU_FALSE == test_exists(pSuite1, "test2")); - TEST(CU_FALSE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_FALSE == test_exists(pSuite2, "")); - - insert_test(pSuite1, pTest1); - insert_test(pSuite1, pTest2); - insert_test(pSuite1, pTest3); - insert_test(pSuite2, pTest4); - - TEST(CU_TRUE == test_exists(pSuite1, "test1")); - TEST(CU_TRUE == test_exists(pSuite1, "test2")); - TEST(CU_TRUE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_TRUE == test_exists(pSuite2, "")); - - TEST(3 == pSuite1->uiNumberOfTests); - TEST(1 == pSuite2->uiNumberOfTests); - TEST(pSuite1->pTest == pTest1); - TEST(pSuite2->pTest == pTest4); - TEST(pTest1->pNext == pTest2); - TEST(pTest1->pPrev == NULL); - TEST(pTest2->pNext == pTest3); - TEST(pTest2->pPrev == pTest1); - TEST(pTest3->pNext == NULL); - TEST(pTest3->pPrev == pTest2); - TEST(pTest4->pNext == NULL); - TEST(pTest4->pPrev == NULL); - - TEST(0 != test_cunit_get_n_memevents(pSuite1)); - TEST(0 != test_cunit_get_n_memevents(pSuite2)); - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pTest2)); - TEST(0 != test_cunit_get_n_memevents(pTest3)); - TEST(0 != test_cunit_get_n_memevents(pTest4)); - - TEST(test_cunit_get_n_allocations(pSuite1) != test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) != test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) != test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) != test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) != test_cunit_get_n_deallocations(pTest4)); - - cleanup_suite(pSuite1); - - TEST(CU_FALSE == test_exists(pSuite1, "test1")); - TEST(CU_FALSE == test_exists(pSuite1, "test2")); - TEST(CU_FALSE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_TRUE == test_exists(pSuite2, "")); - - cleanup_suite(pSuite2); - - TEST(CU_FALSE == test_exists(pSuite1, "test1")); - TEST(CU_FALSE == test_exists(pSuite1, "test2")); - TEST(CU_FALSE == test_exists(pSuite1, "test3")); - TEST(CU_FALSE == test_exists(pSuite1, "test4")); - TEST(CU_FALSE == test_exists(pSuite1, "")); - TEST(CU_FALSE == test_exists(pSuite2, "test1")); - TEST(CU_FALSE == test_exists(pSuite2, "test2")); - TEST(CU_FALSE == test_exists(pSuite2, "test3")); - TEST(CU_FALSE == test_exists(pSuite2, "test4")); - TEST(CU_FALSE == test_exists(pSuite2, "")); - - CU_FREE(pSuite1); - CU_FREE(pSuite2); - - TEST(test_cunit_get_n_allocations(pSuite1) == test_cunit_get_n_deallocations(pSuite1)); - TEST(test_cunit_get_n_allocations(pSuite2) == test_cunit_get_n_deallocations(pSuite2)); - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pTest2) == test_cunit_get_n_deallocations(pTest2)); - TEST(test_cunit_get_n_allocations(pTest3) == test_cunit_get_n_deallocations(pTest3)); - TEST(test_cunit_get_n_allocations(pTest4) == test_cunit_get_n_deallocations(pTest4)); -} - -/*--------------------------------------------------*/ -static void test_cleanup_test(void) -{ - char* pName; - CU_pTest pTest1 = create_test("test1", NULL); - - TEST_FATAL(NULL != pTest1); - - pName = pTest1->pName; - TEST(0 != test_cunit_get_n_memevents(pTest1)); - TEST(0 != test_cunit_get_n_memevents(pName)); - - TEST(test_cunit_get_n_allocations(pTest1) != test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pName) != test_cunit_get_n_deallocations(pName)); - - cleanup_test(pTest1); - CU_FREE(pTest1); - - TEST(test_cunit_get_n_allocations(pTest1) == test_cunit_get_n_deallocations(pTest1)); - TEST(test_cunit_get_n_allocations(pName) == test_cunit_get_n_deallocations(pName)); -} - -/*--------------------------------------------------*/ -static void group_A_case_1(void) -{ - CU_ASSERT_TRUE(1); -} - -static void group_A_case_2(void) -{ - CU_ASSERT_TRUE(2); -} - -static void group_B_case_1(void) -{ - CU_ASSERT_FALSE(1); -} - -static void group_B_case_2(void) -{ - CU_ASSERT_FALSE(2); -} - -static CU_TestInfo group_A_test_cases[] = { - { "1", group_A_case_1 }, - { "2", group_A_case_2 }, - CU_TEST_INFO_NULL, -}; - -static CU_TestInfo group_B_test_cases[] = { - { "1", group_B_case_1 }, - { "2", group_B_case_2 }, - CU_TEST_INFO_NULL, -}; - -static CU_TestInfo group_C_test_cases[] = { - { "1", group_B_case_1 }, - { "1", group_B_case_2 }, /* duplicate test name */ - CU_TEST_INFO_NULL, -}; - -static CU_SuiteInfo suites0[] = { - CU_SUITE_INFO_NULL, -}; - -static CU_SuiteInfo suites1[] = { - { "A1", NULL, NULL, group_A_test_cases }, - { "B1", NULL, NULL, group_B_test_cases }, - CU_SUITE_INFO_NULL, -}; - -static CU_SuiteInfo suites2[] = { - { "A2", NULL, NULL, group_A_test_cases }, - { "B2", NULL, NULL, group_B_test_cases }, - CU_SUITE_INFO_NULL, -}; - -static CU_SuiteInfo suites3[] = { - { "A3", NULL, NULL, group_A_test_cases }, - { "A3", NULL, NULL, group_C_test_cases }, /* duplicate suite name */ - CU_SUITE_INFO_NULL, -}; - -static void test_register_suite(void) -{ - CU_pTestRegistry pReg = NULL; - CU_ErrorCode status; - - if (CU_initialize_registry()) { - fprintf(stderr, "\nError initializing registry in test_register_suite()."); - return; - } - - pReg = CU_get_registry(); - - /* test initial condition */ - TEST_FATAL(NULL != pReg); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with NULL */ - status = CU_register_suites(NULL); - TEST(CUE_SUCCESS == status); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with empty array */ - status = CU_register_suites(suites0); - TEST(CUE_SUCCESS == status); - TEST(0 == pReg->uiNumberOfSuites); - TEST(0 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with ok array */ - status = CU_register_suites(suites1); - TEST(CUE_SUCCESS == status); - TEST(2 == pReg->uiNumberOfSuites); - TEST(4 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with duplicate suite name */ - status = CU_register_suites(suites1); - TEST(CUE_SUCCESS == status); /* shaky - depends on order of operation in CU_register_suites() */ - TEST(4 == pReg->uiNumberOfSuites); - TEST(8 == pReg->uiNumberOfTests); - - /* test CU_register_suites() with duplicate test name */ - status = CU_register_suites(suites3); - TEST(CUE_DUP_TEST == status); /* shaky - depends on order of operation in CU_register_suites() */ - TEST(6 == pReg->uiNumberOfSuites); - TEST(12 == pReg->uiNumberOfTests); - - CU_cleanup_registry(); - - if (CU_initialize_registry()) { - fprintf(stderr, "\nError initializing registry in test_register_suite()."); - return; - } - - pReg = CU_get_registry(); - - /* test CU_register_nsuites() with ok arrays */ - status = CU_register_nsuites(2, suites1, suites2); - TEST(CUE_SUCCESS == status); - TEST(4 == pReg->uiNumberOfSuites); - TEST(8 == pReg->uiNumberOfTests); -} - -/*--------------------------------------------------*/ -void test_cunit_TestDB(void) -{ - test_cunit_start_tests("TestDB.c"); - - test_CU_initialize_registry(); - test_CU_cleanup_registry(); - test_CU_add_suite(); - test_CU_set_suite_attributes(); - test_get_suite_functions(); - test_CU_add_test(); - test_CU_set_test_attributes(); - test_get_test_functions(); - test_CU_get_registry(); - test_CU_set_registry(); - test_CU_create_new_registry(); - test_cleanup_test_registry(); - test_create_suite(); - test_insert_suite(); - test_create_test(); - test_cleanup_test(); - test_insert_test(); - test_register_suite(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestDB.h b/gearsyncd/tests/cunit/TestDB.h deleted file mode 100644 index ec07f921c1..0000000000 --- a/gearsyncd/tests/cunit/TestDB.h +++ /dev/null @@ -1,914 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Contains all the Type Definitions and functions declarations - * for the CUnit test database maintenance. - * - * Aug 2001 Initial implementation. (AK) - * - * 09/Aug/2001 Added Preprocessor conditionals for the file. (AK) - * - * 24/aug/2001 Made the linked list from SLL to DLL(doubly linked list). (AK) - * - * 31-Aug-2004 Restructured to eliminate global variables error_number, - * g_pTestRegistry; new interface, support for deprecated - * version 1 interface, moved error handling code to - * CUError.[ch], moved test run counts and _TestResult out - * of TestRegistry to TestRun.h. (JDS) - * - * 01-Sep-2004 Added jmp_buf to CU_Test. (JDS) - * - * 05-Sep-2004 Added internal test interface. (JDS) - * - * 15-Apr-2006 Removed constraint that suites/tests be uniquely named. - * Added ability to turn individual tests/suites on or off. - * Moved doxygen comments for public API here to header. (JDS) - */ - -/** @file - * Management functions for tests, suites, and the test registry. - * Unit testing in CUnit follows the common structure of unit - * tests aggregated in suites, which are themselves aggregated - * in a test registry. This module provides functions and - * typedef's to support the creation, registration, and manipulation - * of test cases, suites, and the registry. - */ -/** @addtogroup Framework - * @{ - */ - -#ifndef CUNIT_TESTDB_H_SEEN -#define CUNIT_TESTDB_H_SEEN - -#include /* jmp_buf */ - -#include "CUnit.h" -#include "CUError.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/*================================================================= - * Typedefs and Data Structures - *=================================================================*/ - -typedef int (*CU_InitializeFunc)(void); /**< Signature for suite initialization function. */ -typedef int (*CU_CleanupFunc)(void); /**< Signature for suite cleanup function. */ -typedef void (*CU_TestFunc)(void); /**< Signature for a testing function in a test case. */ - -/*----------------------------------------------------------------- - * CU_Test, CU_pTest - *-----------------------------------------------------------------*/ -/** CUnit test case data type. - * CU_Test is a double linked list of unit tests. Each test has - * a name, a callable test function, and a flag for whether the - * test is active and thus executed during a test run. A test - * also holds links to the next and previous tests in the list, - * as well as a jmp_buf reference for use in implementing fatal - * assertions.

- * - * Generally, the linked list includes tests which are associated - * with each other in a CU_Suite. As a result, tests are run in - * the order in which they are added to a suite (see CU_add_test()). - *

- * - * It is recommended that the name of each CU_Test in a suite have - * a unique name. Otherwise, only the first-registered test having - * a given name will be accessible by that name. There are no - * restrictions on the test function. This means that the same - * function could, in principle, be called more than once from - * different tests. - * - * @see CU_Suite - * @see CU_TestRegistry - */ -typedef struct CU_Test -{ - char* pName; /**< Test name. */ - CU_BOOL fActive; /**< Flag for whether test is executed during a run. */ - CU_TestFunc pTestFunc; /**< Pointer to the test function. */ - jmp_buf* pJumpBuf; /**< Jump buffer for setjmp/longjmp test abort mechanism. */ - - struct CU_Test* pNext; /**< Pointer to the next test in linked list. */ - struct CU_Test* pPrev; /**< Pointer to the previous test in linked list. */ - -} CU_Test; -typedef CU_Test* CU_pTest; /**< Pointer to a CUnit test case. */ - -/*----------------------------------------------------------------- - * CU_Suite, CU_pSuite - *-----------------------------------------------------------------*/ -/** CUnit suite data type. - * CU_Suite is a linked list of CU_Test containers. Each suite has - * a name, a count of registered unit tests, and a flag for whether - * the suite is active during test runs. It also holds pointers to - * optional initialization and cleanup functions. If non-NULL, these - * are called before and after running the suite's tests, respectively. - * In addition, the suite holds a pointer to the head of the linked - * list of associated CU_Test objects. Finally, pointers to the next - * and previous suites in the linked list are maintained.

- * - * Generally, the linked list includes suites which are associated with - * each other in a CU_TestRegistry. As a result, suites are run in the - * order in which they are registered (see CU_add_suite()).

- * - * It is recommended that name of each CU_Suite in a test registry have - * a unique name. Otherwise, only the first-registered suite having a - * given name will be accessible by name. There are no restrictions on - * the contained tests. This means that the same CU_Test could, in - * principle, be run more than once fron different suites. - * - * @see CU_Test - * @see CU_TestRegistry - */ -typedef struct CU_Suite -{ - char* pName; /**< Suite name. */ - CU_BOOL fActive; /**< Flag for whether suite is executed during a run. */ - CU_pTest pTest; /**< Pointer to the 1st test in the suite. */ - CU_InitializeFunc pInitializeFunc; /**< Pointer to the suite initialization function. */ - CU_CleanupFunc pCleanupFunc; /**< Pointer to the suite cleanup function. */ - - unsigned int uiNumberOfTests; /**< Number of tests in the suite. */ - struct CU_Suite* pNext; /**< Pointer to the next suite in linked list. */ - struct CU_Suite* pPrev; /**< Pointer to the previous suite in linked list. */ - -} CU_Suite; -typedef CU_Suite* CU_pSuite; /**< Pointer to a CUnit suite. */ - -/*----------------------------------------------------------------- - * CU_TestRegistry, CU_pTestRegistry - *-----------------------------------------------------------------*/ -/** CUnit test registry data type. - * CU_TestRegisty is the repository for suites containing unit tests. - * The test registry maintains a count of the number of CU_Suite - * objects contained in the registry, as well as a count of the total - * number of CU_Test objects associated with those suites. It also - * holds a pointer to the head of the linked list of CU_Suite objects. - *

- * - * With this structure, the user will normally add suites implictly to - * the internal test registry using CU_add_suite(), and then add tests - * to each suite using CU_add_test(). Test runs are then initiated - * using one of the appropriate functions in TestRun.c via one of the - * user interfaces.

- * - * Automatic creation and destruction of the internal registry and its - * objects is available using CU_initialize_registry() and - * CU_cleanup_registry(), respectively. For internal and testing - * purposes, the internal registry can be retrieved and assigned. - * Functions are also provided for creating and destroying independent - * registries.

- * - * Note that earlier versions of CUnit also contained a pointer to a - * linked list of CU_FailureRecord objects (termed _TestResults). - * This has been removed from theregistry and relocated to TestRun.c. - * - * @see CU_Test - * @see CU_Suite - * @see CU_initialize_registry() - * @see CU_cleanup_registry() - * @see CU_get_registry() - * @see CU_set_registry() - * @see CU_create_new_registry() - * @see CU_destroy_existing_registry() - */ -typedef struct CU_TestRegistry -{ -#ifdef USE_DEPRECATED_CUNIT_NAMES - /** Union to support v1.1-1 member name. */ - union { - unsigned int uiNumberOfSuites; /**< Number of suites in the test registry. */ - unsigned int uiNumberOfGroups; /**< Deprecated (version 1). @deprecated Use uiNumberOfSuites. */ - }; - unsigned int uiNumberOfTests; /**< Number of tests in the test registry. */ - /** Union to support v1.1-1 member name. */ - union { - CU_pSuite pSuite; /**< Pointer to the 1st suite in the test registry. */ - CU_pSuite pGroup; /**< Deprecated (version 1). @deprecated Use pSuite. */ - }; -#else - unsigned int uiNumberOfSuites; /**< Number of registered suites in the registry. */ - unsigned int uiNumberOfTests; /**< Total number of registered tests in the registry. */ - CU_pSuite pSuite; /**< Pointer to the 1st suite in the test registry. */ -#endif -} CU_TestRegistry; -typedef CU_TestRegistry* CU_pTestRegistry; /**< Pointer to a CUnit test registry. */ - -/*================================================================= - * Public interface functions - *=================================================================*/ - -CU_EXPORT -CU_ErrorCode CU_initialize_registry(void); -/**< - * Initializes the framework test registry. - * Any existing registry is freed, including all stored suites - * and associated tests. It is not necessary to explicitly call - * CU_cleanup_registry() before reinitializing the framework. - * The most recent stored test results are also cleared.

- * - * This function must not be called during a test run (checked - * by assertion) - * - * @return CUE_NOMEMORY if memory for the new registry cannot - * be allocated, CUE_SUCCESS otherwise. - * @see CU_cleanup_registry - * @see CU_get_registry - * @see CU_set_registry - * @see CU_registry_initialized - */ - -CU_EXPORT -void CU_cleanup_registry(void); -/**< - * Clears the test registry. - * The active test registry is freed, including all stored suites - * and associated tests. The most recent stored test results are - * also cleared. After calling this function, CUnit suites cannot - * be added until CU_initialize_registry() or CU_set_registry() is - * called. Further, any pointers to suites or test cases held by - * the user will be invalidated by calling this function.

- * - * This function may be called multiple times without generating - * an error condition. However, this function must not be - * called during a test run (checked by assertion)

. - * - * @see CU_initialize_registry - * @see CU_get_registry - * @see CU_set_registry - */ - -CU_EXPORT CU_BOOL CU_registry_initialized(void); -/**< - * Checks whether the test registry has been initialized. - * - * @return CU_TRUE if the registry has been initialized, - * CU_FALSE otherwise. - * @see CU_initialize_registry - * @see CU_cleanup_registry - */ - -CU_EXPORT -CU_pSuite CU_add_suite(const char *strName, - CU_InitializeFunc pInit, - CU_CleanupFunc pClean); -/**< - * Creates a new test suite and adds it to the test registry. - * This function creates a new test suite having the specified - * name and initialization/cleanup functions and adds it to the - * test registry. The new suite will be active and able to be - * executed during a test run. The test registry must be - * initialized before calling this function (checked by assertion). - * pInit and pClean may be NULL, in which case no corresponding - * initialization of cleanup function will be called when the suite - * is run. strName may be empty ("") but may not be NULL.

- * - * The return value is a pointer to the newly-created suite, or - * NULL if there was a problem with the suite creation or addition. - * An error code is also set for the framework. Note that if the - * name specified for the new suite is a duplicate, the suite will - * be created and added but the error code will be set to CUE_DUP_SUITE. - * The duplicate suite will not be accessible by name.

- * - * NOTE - the CU_pSuite pointer returned should NOT BE FREED BY - * THE USER. The suite is freed by the CUnit system when - * CU_cleanup_registry() is called. This function must not - * be called during a test run (checked by assertion).

- * - * CU_add_suite() sets the following error codes: - * - CUE_SUCCESS if no errors occurred. - * - CUE_NOREGISTRY if the registry has not been initialized. - * - CUE_NO_SUITENAME if strName is NULL. - * - CUE_DUP_SUITE if a suite having strName is already registered. - * - CUE_NOMEMORY if a memory allocation failed. - * - * @param strName Name for the new test suite (non-NULL). - * @param pInit Initialization function to call before running suite. - * @param pClean Cleanup function to call after running suite. - * @return A pointer to the newly-created suite (NULL if creation failed) - */ - -CU_EXPORT -CU_ErrorCode CU_set_suite_active(CU_pSuite pSuite, CU_BOOL fNewActive); -/**< - * Activates or deactivates a suite. - * Only activated suites can be executed during a test run. - * By default a suite is active upon creation, but can be deactivated - * by passing it along with CU_FALSE to this function. The suite - * can be reactivated by passing it along with CU_TRUE. The current - * value of the active flag is available as pSuite->fActive. If pSuite - * is NULL then error code CUE_NOSUITE is returned. - * - * @param pSuite Pointer to the suite to modify (non-NULL). - * @param fNewActive If CU_TRUE then the suite will be activated; - * if CU_FALSE it will be deactivated. - * @return Returns CUE_NOSUITE if pSuite is NULL, CUE_SUCCESS if all is well. - */ - -CU_EXPORT -CU_ErrorCode CU_set_suite_name(CU_pSuite pSuite, const char *strNewName); -/**< - * Modifies the name of a suite. - * This function allows the name associated with a suite to - * be changed. It is not recommended that a suite name be changed, - * nor should it be necessary under most circumstances. However, - * this function is provided for those clients who need to change - * a suite's name. The current value of the suite's name - * is available as pSuite->pName. CUE_SUCCESS is returned if the - * function succeeds in changing the name. CUE_NOSUITE is returned if - * pSuite is NULL, and CUE_NO_SUITENAME if strNewName is NULL. - * - * @param pSuite Pointer to the suite to modify (non-NULL). - * @param strNewName Pointer to string containing new suite name (non-NULL). - * @return Returns CUE_NOSUITE if pSuite is NULL, CUE_NO_SUITENAME if - * strNewName is NULL, and CUE_SUCCESS if all is well. - */ - -CU_EXPORT -CU_ErrorCode CU_set_suite_initfunc(CU_pSuite pSuite, CU_InitializeFunc pNewInit); -/**< - * Modifies the initialization function of a suite. - * This function allows the initialization function associated with - * a suite to be changed. This is neither recommended nor should it - * be necessary under most circumstances. However, this function is - * provided for those clients who need to change the function. The - * current value of the function is available as pSuite->pInitializeFunc. - * CUE_SUCCESS is returned if the function succeeds, or CUE_NOSUITE if - * pSuite is NULL. pNewInit may be NULL, which indicates the suite has - * no initialization function. - * - * @param pSuite Pointer to the suite to modify (non-NULL). - * @param pNewInit Pointer to function to use to initialize suite. - * @return Returns CUE_NOSUITE if pSuite is NULL, and CUE_SUCCESS if - * all is well. - */ - -CU_EXPORT -CU_ErrorCode CU_set_suite_cleanupfunc(CU_pSuite pSuite, CU_CleanupFunc pNewClean); -/**< - * Modifies the cleanup function of a suite. - * This function allows the cleanup function associated with a suite to - * be changed. This is neither recommended nor should it be necessary - * under most circumstances. However, this function is provided for those - * clients who need to change the function. The current value of the - * function is available as pSuite->pCleanupFunc. CUE_SUCCESS is returned - * if the function succeeds, or CUE_NOSUITE if pSuite is NULL. pNewClean - * may be NULL, which indicates the suite has no cleanup function. - * - * @param pSuite Pointer to the suite to modify (non-NULL). - * @param pNewClean Pointer to function to use to clean up suite. - * @return Returns CUE_NOSUITE if pSuite is NULL, and CUE_SUCCESS if - * all is well. - */ - -CU_EXPORT -CU_pSuite CU_get_suite(const char* strName); -/**< - * Retrieves the suite having the specified name. - * Searches the active test registry and returns a pointer to the 1st - * suite found. NULL is returned if no suite having the specified name - * is found. In addition, the framework error state is set to CUE_NOREGISTRY - * if the registry is not initialized or to CUE_NO_SUITENAME if strName is NULL. - * If the return value is NULL and framework error state is CUE_SUCCESS, then - * the search simply failed to find the specified name. - * Use CU_get_suite_at_pos() to retrieve a suite by position rather than name. - * - * @param strName The name of the suite to search for (non-NULL). - * @return Returns a pointer to the suite, or NULL if not found or an error occurred. - * @see CU_get_suite_at_pos() - */ - -CU_EXPORT -CU_pSuite CU_get_suite_at_pos(unsigned int pos); -/**< - * Retrieves the suite at the specified position. - * Iterates the active test registry and returns a pointer to the suite at - * position pos. pos is a 1-based index having valid values - * [1 .. CU_get_registry()->uiNumberOfSuites] and corresponds to the order in - * which suites were registered. If pos is invalid or an error occurs, 0 is - * returned. In addition, the framework error state is set to CUE_NOREGISTRY if - * the registry is not initialized, or CUE_SUCCESS if pos was invalid. Use - * CU_get_suite() to retrieve a suite by name rather than position. - * - * @param pos The 1-based position of the suite to fetch. - * @return Returns a pointer to the suite, or 0 if not found or an error occurred. - * @see CU_get_suite() - */ - -CU_EXPORT -unsigned int CU_get_suite_pos(CU_pSuite pSuite); -/**< - * Looks up the position of the specified suite. - * The position is a 1-based index of suites in the active test registry which - * corresponds to the order in which suites were registered. If pSuite is not - * found or an error occurs, 0 is returned. In addition, the framework error - * state is set to CUE_NOREGISTRY if the registry is not initialized, or - * CUE_NOSUITE if pSuite is NULL. The returned position may be used to retrieve - * the suite using CU_get_suite_by_pos(). - * - * @param pSuite Pointer to the suite to find (non-NULL). - * @return Returns the 1-based position of pSuite in the registry, or NULL if - * not found or an error occurred. - * @see CU_get_suite_by_pos() - * @see CU_get_suite_pos_by_name() - */ - -CU_EXPORT -unsigned int CU_get_suite_pos_by_name(const char* strName); -/**< - * Looks up the position of the suite having the specified name. - * The position is a 1-based index of suites in the active test registry which - * corresponds to the order in which suites were registered. If no suite has the - * specified name or an error occurs, 0 is returned. In addition, the framework error - * state is set to CUE_NOREGISTRY if the registry is not initialized, or - * CUE_NO_SUITENAME if strName is NULL. The search ends at the 1st suite found having - * name strName. The returned position may be used to retrieve the suite using - * CU_get_suite_by_pos(). - * - * @param strName Name of the suite to find (non-NULL). - * @return Returns the 1-based position of pSuite in the registry, or NULL if - * not found or an error occurred. - * @see CU_get_suite_by_pos() - * @see CU_get_suite_pos_by_name() - */ - -CU_EXPORT -CU_pTest CU_add_test(CU_pSuite pSuite, const char* strName, CU_TestFunc pTestFunc); -/**< - * This function creates a new test having the specified name - * and function, and adds it to the specified suite. The new test - * is active and able to be executed during a test run. At present, - * there is no mechanism for creating a test case independent of a - * suite. Neither pSuite, strName, nor pTestFunc may be NULL. - * - * The return value is a pointer to the newly-created test, or - * NULL if there was a problem with the test creation or addition. - * An error code is also set for the framework. Note that if the - * name specified for the new test is a duplicate within pSuite, - * the test will be created and added but the error code will be - * set to CUE_DUP_TEST. The duplicate test will not be accessible - * by name.

- * - * NOTE - the CU_pTest pointer returned should NOT BE FREED BY - * THE USER. The test is freed by the CUnit system when - * CU_cleanup_registry() is called. This function must not - * be called during a test run (checked by assertion).

- - * CU_add_test() sets the following error codes: - * - CUE_SUCCESS if no errors occurred. - * - CUE_NOREGISTRY if the registry has not been initialized. - * - CUE_NOSUITE if pSuite is NULL. - * - CUE_NO_TESTNAME if strName is NULL. - * - CUE_NOTEST if pTestFunc is NULL. - * - CUE_DUP_TEST if a test having strName is already registered to pSuite. - * - CUE_NOMEMORY if a memory allocation failed.

- * - * @param pSuite Test suite to which to add new test (non-NULL). - * @param strName Name for the new test case (non-NULL). - * @param pTest Function to call when running the test (non-NULL). - * @return A pointer to the newly-created test (NULL if creation failed) - */ - -CU_EXPORT -CU_ErrorCode CU_set_test_active(CU_pTest pTest, CU_BOOL fNewActive); -/**< - * Activates or deactivates a specific test. - * Only activated tests can be executed during a test run. - * By default a test is active upon creation, but can be deactvated - * by passing it along with CU_FALSE to this function. The test - * can be reactivated by passing it along with CU_TRUE. - * The current value of the active flag is available as pTest->fActive. - * If pTest is NULL then error code CUE_NOTEST is returned. Otherwise - * CUE_SUCCESS is returned. - * - * @param pTest Pointer to the test to modify (non-NULL). - * @param fNewActive If CU_TRUE then test will be activated; - * if CU_FALSE it will be deactivated. - * @return Returns CUE_NOTEST if pTest is NULL, CUE_SUCCESS if all is well. -*/ - -CU_EXPORT -CU_ErrorCode CU_set_test_name(CU_pTest pTest, const char *strNewName); -/**< - * Modifies the name of a test. - * This function allows the name associated with a test to - * be changed. It is not recommended that a test name be changed, - * nor should it be necessary under most circumstances. However, - * this function is provided for those clients who need to change - * a test's name. The current value of the test's name is - * available as pTest->pName. CUE_SUCCESS is returned if the - * function succeeds in changing the name. CUE_NOTEST is returned if - * pTest is NULL, and CUE_NO_TESTNAME if strNewName is NULL. - * - * @param pTest Pointer to the test to modify (non-NULL). - * @param strNewName Pointer to string containing new test name (non-NULL). - * @return Returns CUE_NOTEST if pTest is NULL, CUE_NO_TESTNAME if - * strNewName is NULL, and CUE_SUCCESS if all is well. - */ - -CU_EXPORT -CU_ErrorCode CU_set_test_func(CU_pTest pTest, CU_TestFunc pNewFunc); -/**< - * Modifies the test function of a test. - * This function allows the test function associated with a test to be - * changed. This is neither recommended nor should it be necessary under - * most circumstances. However, this function is provided for those - * clients who need to change the test function. The current value of - * the test function is available as pTest->pTestFunc. CUE_SUCCESS is - * returned if the function succeeds, or CUE_NOTEST if either pTest or - * pNewFunc is NULL. - * - * @param pTest Pointer to the test to modify (non-NULL). - * @param pNewFunc Pointer to function to use for test function (non-NULL). - * @return Returns CUE_NOTEST if pTest or pNewFunc is NULL, and CUE_SUCCESS - * if all is well. - */ - -CU_EXPORT -CU_pTest CU_get_test(CU_pSuite pSuite, const char *strName); -/**< - * Retrieves the test having the specified name. - * Searches pSuite and returns a pointer to the 1st test found named strName. - * NULL is returned if no test having the specified name is found in pSuite. - * In addition, the framework error state is set as follows: - * - CUE_NOREGISTRY if the registry is not initialized - * - CUE_NOSUITE if pSuite is NULL - * - CUE_NO_TESTNAME if strName is NULL. - * - * If the return value is NULL and framework error state is CUE_SUCCESS, then - * the search simply failed to find the specified name. Use CU_get_test_at_pos() - * to retrieve a test by position rather than name. - * - * @param pSuite Pointer to the suite to search (non-NULL). - * @param strName The name of the test to search for (non-NULL). - * @return Returns a pointer to the test, or NULL if not found or an error occurred. - * @see CU_get_test_at_pos() - */ - -CU_EXPORT -CU_pTest CU_get_test_at_pos(CU_pSuite pSuite, unsigned int pos); -/**< - * Retrieves the test at the specified position in pSuite. - * Iterates the tests registered in pSuite and returns a pointer to the - * test at position pos. pos is a 1-based index having valid values - * [1 .. pSuite->uiNumberOfTests] and corresponds to the order in - * which tests were added to pSuite. If pos is invalid or an error occurs, 0 is - * returned. In addition, the framework error state is set as follows: - * - CUE_NOREGISTRY if the registry is not initialized - * - CUE_NOSUITE if pSuite is NULL - * Use CU_get_test() to retrieve a test by name rather than position. - * - * @param pSuite Pointer to the suite to search (non-NULL). - * @param pos The 1-based position of the test to fetch. - * @return Returns a pointer to the test, or 0 if not found or an error occurred. - * @see CU_get_test() - */ - -CU_EXPORT -unsigned int CU_get_test_pos(CU_pSuite pSuite, CU_pTest pTest); -/**< - * Looks up the position of the specified test in pSuite. - * The position is a 1-based index of tests in pSuite which corresponds to the - * order in which tests were added. If pTest is not found or an error occurs, - * 0 is returned. In addition, the framework error state is set as follows: - * - CUE_NOREGISTRY if the registry is not initialized - * - CUE_NOSUITE if pSuite is NULL - * - CUE_NOTEST if pTest is NULL - * - * The returned position may be used to retrieve the test using CU_get_test_by_pos(). - * - * @param pSuite Pointer to the suite to search (non-NULL). - * @param pTest Pointer to the test to find (non-NULL). - * @return Returns the 1-based position of pTest in pSuite, or NULL if - * not found or an error occurred. - * @see CU_get_test_by_pos() - * @see CU_get_test_pos_by_name() - */ - -CU_EXPORT -unsigned int CU_get_test_pos_by_name(CU_pSuite pSuite, const char *strName); -/**< - * Looks up the position of the test having the specified name in pSuite. - * The position is a 1-based index of tests in pSuite which corresponds to the order - * in which tests were added. If no test has the specified name or an error occurs, - * 0 is returned. In addition, the framework error state is set as follows: - * - CUE_NOREGISTRY if the registry is not initialized - * - CUE_NOSUITE if pSuite is NULL - * - CUE_NO_TESTNAME if strName is NULL - * The search ends at the 1st test found having name strName. The returned position - * may be used to retrieve the suite using CU_get_test_by_pos(). - * - * @param pSuite Pointer to the suite to search (non-NULL). - * @param strName Name of the test to find (non-NULL). - * @return Returns the 1-based position of pTest in pSuite, or NULL if - * not found or an error occurred. - * @see CU_get_test_by_pos() - * @see CU_get_test_pos_by_name() - */ - -#define CU_ADD_TEST(suite, test) (CU_add_test(suite, #test, (CU_TestFunc)test)) -/**< Shortcut macro for adding a test to a suite. */ - -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ -/* This section is based conceptually on code - * Copyright (C) 2004 Aurema Pty Ltd. - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - * Derived from code contributed by K. Cheung and Aurema Pty Ltd. (thanks!) - * test_case_t, test_group_t, test_suite_t - */ - -/** - * Test case parameters structure. - * This data type is provided to assist CUnit users manage collections of - * tests and suites. It is intended to be used to build arrays of test case - * parameters that can be then be referred to in a CU_suite_info_t variable. - */ -typedef struct CU_TestInfo { - char *pName; /**< Test name. */ - CU_TestFunc pTestFunc; /**< Test function. */ -} CU_TestInfo; -typedef CU_TestInfo* CU_pTestInfo; /**< Pointer to CU_TestInfo type. */ - -/** - * Suite parameters. - * This data type is provided to assist CUnit users manage collections of - * tests and suites. It is intended to be used to build arrays of suite - * parameters that can be passed to a bulk registration function such as - * CU_register_suite() or CU_register_suites(). - */ -typedef struct CU_SuiteInfo { - char *pName; /**< Suite name. */ - CU_InitializeFunc pInitFunc; /**< Suite initialization function. */ - CU_CleanupFunc pCleanupFunc; /**< Suite cleanup function */ - CU_TestInfo *pTests; /**< Test case array - must be NULL terminated. */ -} CU_SuiteInfo; -typedef CU_SuiteInfo* CU_pSuiteInfo; /**< Pointer to CU_SuiteInfo type. */ - -#define CU_TEST_INFO_NULL { NULL, NULL } -/**< NULL CU_test_info_t to terminate arrays of tests. */ -#define CU_SUITE_INFO_NULL { NULL, NULL, NULL, NULL } -/**< NULL CU_suite_info_t to terminate arrays of suites. */ - - -CU_EXPORT CU_ErrorCode CU_register_suites(CU_SuiteInfo suite_info[]); -/**< - * Registers the suites in a single CU_SuiteInfo array. - * Multiple arrays can be registered using CU_register_nsuites(). - * - * @param suite_info NULL-terminated array of CU_SuiteInfo items to register. - * @return A CU_ErrorCode indicating the error status. - * @see CU_register_suites() - */ -CU_EXPORT CU_ErrorCode CU_register_nsuites(int suite_count, ...); -/**< - * Registers multiple suite arrays in CU_SuiteInfo format. - * The function accepts a variable number of suite arrays to be registered. - * The number of arrays is indicated by the value of the 1st argument, - * suite_count. Each suite in each array is registered with the CUnit test - * registry, along with all of the associated tests. - * - * @param suite_count The number of CU_SuiteInfo* arguments to follow. - * @param ... suite_count number of CU_SuiteInfo* arguments. NULLs are ignored. - * @return A CU_ErrorCode indicating the error status. - * @see CU_register_suites() - */ - -#ifdef USE_DEPRECATED_CUNIT_NAMES -typedef CU_TestInfo test_case_t; /**< Deprecated (version 1). @deprecated Use CU_TestInfo. */ -typedef CU_SuiteInfo test_group_t; /**< Deprecated (version 1). @deprecated Use CU_SuiteInfo. */ - -/** Deprecated (version 1). @deprecated Use CU_SuiteInfo and CU_TestInfo. */ -typedef struct test_suite { - char *name; /**< Suite name. Currently not used. */ - test_group_t *groups; /**< Test groups. This must be a NULL terminated array. */ -} test_suite_t; - -/** Deprecated (version 1). @deprecated Use CU_TEST_INFO_NULL. */ -#define TEST_CASE_NULL { NULL, NULL } -/** Deprecated (version 1). @deprecated Use CU_TEST_GROUP_NULL. */ -#define TEST_GROUP_NULL { NULL, NULL, NULL, NULL } - -/** Deprecated (version 1). @deprecated Use CU_register_suites(). */ -#define test_group_register(tg) CU_register_suites(tg) - -/** Deprecated (version 1). @deprecated Use CU_SuiteInfo and CU_register_suites(). */ -CU_EXPORT int test_suite_register(test_suite_t *ts) -{ - test_group_t *tg; - int error; - - for (tg = ts->groups; tg->pName; tg++) - if ((error = CU_register_suites(tg)) != CUE_SUCCESS) - return error; - - return CUE_SUCCESS; -} -#endif /* USE_DEPRECATED_CUNIT_NAMES */ -/*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/ - -#ifdef USE_DEPRECATED_CUNIT_NAMES -typedef CU_InitializeFunc InitializeFunc; /**< Deprecated (version 1). @deprecated Use CU_InitializeFunc. */ -typedef CU_CleanupFunc CleanupFunc; /**< Deprecated (version 1). @deprecated Use CU_CleanupFunc. */ -typedef CU_TestFunc TestFunc; /**< Deprecated (version 1). @deprecated Use CU_TestFunc. */ - -typedef CU_Test _TestCase; /**< Deprecated (version 1). @deprecated Use CU_Test. */ -typedef CU_pTest PTestCase; /**< Deprecated (version 1). @deprecated Use CU_pTest. */ - -typedef CU_Suite _TestGroup; /**< Deprecated (version 1). @deprecated Use CU_Suite. */ -typedef CU_pSuite PTestGroup; /**< Deprecated (version 1). @deprecated Use CU_pSuite. */ - -typedef CU_TestRegistry _TestRegistry; /**< Deprecated (version 1). @deprecated Use CU_TestRegistry. */ -typedef CU_pTestRegistry PTestRegistry; /**< Deprecated (version 1). @deprecated Use CU_pTestRegistry. */ - -/* Public interface functions */ -/** Deprecated (version 1). @deprecated Use CU_initialize_registry(). */ -#define initialize_registry() CU_initialize_registry() -/** Deprecated (version 1). @deprecated Use CU_cleanup_registry(). */ -#define cleanup_registry() CU_cleanup_registry() -/** Deprecated (version 1). @deprecated Use CU_add_suite(). */ -#define add_test_group(name, init, clean) CU_add_suite(name, init, clean) -/** Deprecated (version 1). @deprecated Use CU_add_test(). */ -#define add_test_case(group, name, test) CU_add_test(group, name, test) - -/* private internal CUnit testing functions */ -/** Deprecated (version 1). @deprecated Use CU_get_registry(). */ -#define get_registry() CU_get_registry() -/** Deprecated (version 1). @deprecated Use CU_set_registry(). */ -#define set_registry(reg) CU_set_registry((reg)) - -/** Deprecated (version 1). @deprecated Use CU_get_suite_by_name(). */ -#define get_group_by_name(group, reg) CU_get_suite_by_name(group, reg) -/** Deprecated (version 1). @deprecated Use CU_get_test_by_name(). */ -#define get_test_by_name(test, group) CU_get_test_by_name(test, group) - -/** Deprecated (version 1). @deprecated Use ADD_TEST_TO_SUITE. */ -#define ADD_TEST_TO_GROUP(group, test) (CU_add_test(group, #test, (CU_TestFunc)test)) -#endif /* USE_DEPRECATED_CUNIT_NAMES */ - -/*================================================================= - * Internal CUnit system functions. - * Should not be routinely called by users. - *=================================================================*/ - -CU_EXPORT CU_pTestRegistry CU_get_registry(void); -/**< - * Retrieves a pointer to the current test registry. - * Returns NULL if the registry has not been initialized using - * CU_initialize_registry(). Directly accessing the registry - * should not be necessary for most users. This function is - * provided primarily for internal and testing purposes. - * - * @return A pointer to the current registry (NULL if uninitialized). - * @see CU_initialize_registry - * @see CU_set_registry - */ - -CU_EXPORT CU_pTestRegistry CU_set_registry(CU_pTestRegistry pTestRegistry); -/**< - * Sets the registry to an existing CU_pTestRegistry instance. - * A pointer to the original registry is returned. Note that the - * original registry is not freed, and it becomes the caller's - * responsibility to do so. Directly accessing the registry - * should not be necessary for most users. This function is - * provided primarily for internal and testing purposes.

- * - * This function must not be called during a test run (checked - * by assertion). - * - * @return A pointer to the original registry that was replaced. - * @see CU_initialize_registry - * @see CU_cleanup_registry - * @see CU_get_registry - */ - -CU_EXPORT CU_pTestRegistry CU_create_new_registry(void); -/**< - * Creates and initializes a new test registry. - * Returns a pointer to a new, initialized registry (NULL if memory could - * not be allocated). It is the caller's responsibility to destroy and free - * the new registry (unless it is made the active test registry using - * CU_set_registry()). - */ - -CU_EXPORT -void CU_destroy_existing_registry(CU_pTestRegistry* ppRegistry); -/**< - * Destroys and frees all memory for an existing test registry. - * The active test registry is destroyed by the CUnit system in - * CU_cleanup_registry(), so only call this function on registries created - * or held independently of the internal CUnit system.

- * - * Once a registry is made the active test registry using CU_set_registry(), - * its destruction will be handled by the framework. ppRegistry may not be - * NULL (checked by assertion), but *ppRegistry can be NULL (in which case the - * function has no effect). Note that *ppRegistry will be set to NULL on return. - * - * @param ppRegistry Address of a pointer to the registry to destroy (non-NULL). - */ - -CU_EXPORT -CU_pSuite CU_get_suite_by_name(const char *szSuiteName, CU_pTestRegistry pRegistry); -/**< - * Retrieves a pointer to the suite having the specified name. - * Scans the pRegistry and returns a pointer to the first suite located - * having the specified name. Neither szSuiteName nor pRegistry may be - * NULL (checked by assertion). Clients should normally use CU_get_suite() - * instead, which automatically searches the active test registry. - * - * @param szSuiteName The name of the suite to locate (non-NULL). - * @param pRegistry The registry to scan (non-NULL). - * @return Pointer to the first suite having the specified name, - * NULL if not found. - * @see CU_get_suite() - */ - -CU_EXPORT -CU_pSuite CU_get_suite_by_index(unsigned int index, CU_pTestRegistry pRegistry); -/**< - * Retrieves a pointer to the suite at the specified (1-based) index. - * Iterates pRegistry and returns a pointer to the suite located at the - * specified index. pRegistry may not be NULL (checked by assertion). - * Clients should normally use CU_get_suite_at_pos() instead, which - * automatically searches the active test registry. - * - * @param index The 1-based index of the suite to find. - * @param pRegistry The registry to scan (non-NULL). - * @return Pointer to the suite at the specified index, or - * NULL if index is invalid. - * @see CU_get_suite_at_pos() - */ - -CU_EXPORT -CU_pTest CU_get_test_by_name(const char* szTestName, CU_pSuite pSuite); -/**< - * Retrieves a pointer to the test case in pSuite having the specified name. - * The first test case in pSuite having the specified name is returned, or - * NULL if not found. Neither szSuiteName nor pSuite may be NULL (checked - * by assertion). Clients should normally use CU_get_test() instead. - * - * @param szTestName The name of the test case to locate (non-NULL). - * @param pSuite The suite to scan (non-NULL). - * @return Pointer to the first test case having the specified name, - * NULL if not found. - * @see CU_get_test() - */ - -CU_EXPORT -CU_pTest CU_get_test_by_index(unsigned int index, CU_pSuite pSuite); -/**< - * Retrieves a pointer to the test at the specified (1-based) index. - * Iterates pSuite and returns a pointer to the test located at the - * specified index. pSuite may not be NULL (checked by assertion). - * Clients should normally use CU_get_test_at_pos() instead, which - * automatically searches the active test registry. - * - * @param index The 1-based index of the test to find. - * @param pRegistry The registry to scan (non-NULL). - * @return Pointer to the test at the specified index, or - * NULL if index is invalid. - * @see CU_get_test_at_pos() - */ - -#ifdef CUNIT_BUILD_TESTS -void test_cunit_TestDB(void); -#endif - -#ifdef __cplusplus -} -#endif -#endif /* CUNIT_TESTDB_H_SEEN */ -/** @} */ diff --git a/gearsyncd/tests/cunit/TestRun.c b/gearsyncd/tests/cunit/TestRun.c deleted file mode 100644 index efd88e48fc..0000000000 --- a/gearsyncd/tests/cunit/TestRun.c +++ /dev/null @@ -1,2923 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of Test Run Interface. - * - * Aug 2001 Initial implementaion (AK) - * - * 19/Aug/2001 Added initial registry/Suite/test framework implementation. (AK) - * - * 24/Aug/2001 Changed Data structure from SLL to DLL for all linked lists. (AK) - * - * 25/Nov/2001 Added notification for Suite Initialization failure condition. (AK) - * - * 5-Aug-2004 New interface, doxygen comments, moved add_failure on suite - * initialization so called even if a callback is not registered, - * moved CU_assertImplementation into TestRun.c, consolidated - * all run summary info out of CU_TestRegistry into TestRun.c, - * revised counting and reporting of run stats to cleanly - * differentiate suite, test, and assertion failures. (JDS) - * - * 1-Sep-2004 Modified CU_assertImplementation() and run_single_test() for - * setjmp/longjmp mechanism of aborting test runs, add asserts in - * CU_assertImplementation() to trap use outside a registered - * test function during an active test run. (JDS) - * - * 22-Sep-2004 Initial implementation of internal unit tests, added nFailureRecords - * to CU_Run_Summary, added CU_get_n_failure_records(), removed - * requirement for registry to be initialized in order to run - * CU_run_suite() and CU_run_test(). (JDS) - * - * 30-Apr-2005 Added callback for suite cleanup function failure, - * updated unit tests. (JDS) - * - * 23-Apr-2006 Added testing for suite/test deactivation, changing functions. - * Moved doxygen comments for public functions into header. - * Added type marker to CU_FailureRecord. - * Added support for tracking inactive suites/tests. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - * - * 02-Jun-2006 Added support for elapsed time. Added handlers for suite - * start and complete events. Reworked test run routines to - * better support these features, suite/test activation. (JDS) - */ - -/** @file - * Test run management functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include -#include -#if NOT_YET -#include -#else -#include -#endif -#include - -#include "CUnit.h" -#include "MyMem.h" -#include "TestDB.h" -#include "TestRun.h" -#include "Util.h" -#include "CUnit_intl.h" - -#define MAX_CMD_LEN 100 - -//Extern declaration -extern int collect_syslog; - -//Callback fn declaration -typedef void (*cunit_logger_cb)(void *fn); - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -static CU_BOOL f_bTestIsRunning = CU_FALSE; /**< Flag for whether a test run is in progress */ -static CU_pSuite f_pCurSuite = NULL; /**< Pointer to the suite currently being run. */ -static CU_pTest f_pCurTest = NULL; /**< Pointer to the test currently being run. */ - -/** CU_RunSummary to hold results of each test run. */ -static CU_RunSummary f_run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - -/** CU_pFailureRecord to hold head of failure record list of each test run. */ -static CU_pFailureRecord f_failure_list = NULL; - -/** CU_pFailureRecord to hold head of failure record list of each test run. */ -static CU_pFailureRecord f_last_failure = NULL; - -/** Flag for whether inactive suites/tests are treated as failures. */ -static CU_BOOL f_failure_on_inactive = CU_TRUE; - -/** Variable for storage of start time for test run. */ -static clock_t f_start_time; - - -/** Pointer to the function to be called before running a suite. */ -static CU_SuiteStartMessageHandler f_pSuiteStartMessageHandler = NULL; - -/** Pointer to the function to be called before running a test. */ -static CU_TestStartMessageHandler f_pTestStartMessageHandler = NULL; - -/** Pointer to the function to be called after running a test. */ -static CU_TestCompleteMessageHandler f_pTestCompleteMessageHandler = NULL; - -/** Pointer to the function to be called after running a suite. */ -static CU_SuiteCompleteMessageHandler f_pSuiteCompleteMessageHandler = NULL; - -/** Pointer to the function to be called when all tests have been run. */ -static CU_AllTestsCompleteMessageHandler f_pAllTestsCompleteMessageHandler = NULL; - -/** Pointer to the function to be called if a suite initialization function returns an error. */ -static CU_SuiteInitFailureMessageHandler f_pSuiteInitFailureMessageHandler = NULL; - -/** Pointer to the function to be called if a suite cleanup function returns an error. */ -static CU_SuiteCleanupFailureMessageHandler f_pSuiteCleanupFailureMessageHandler = NULL; - -/*================================================================= - * Private function forward declarations - *=================================================================*/ -static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure); -static void cleanup_failure_list(CU_pFailureRecord* ppFailure); -static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary); -static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary); -static void add_failure(CU_pFailureRecord* ppFailure, - CU_pRunSummary pRunSummary, - CU_FailureType type, - unsigned int uiLineNumber, - const char *szCondition, - const char *szFileName, - CU_pSuite pSuite, - CU_pTest pTest); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -CU_BOOL CU_assertImplementation(CU_BOOL bValue, - unsigned int uiLine, - const char *strCondition, - const char *strFile, - const char *strFunction, - CU_BOOL bFatal) -{ - /* not used in current implementation - stop compiler warning */ - CU_UNREFERENCED_PARAMETER(strFunction); - - /* these should always be non-NULL (i.e. a test run is in progress) */ - assert(NULL != f_pCurSuite); - assert(NULL != f_pCurTest); - - ++f_run_summary.nAsserts; - if (CU_FALSE == bValue) { - ++f_run_summary.nAssertsFailed; - add_failure(&f_failure_list, &f_run_summary, CUF_AssertFailed, - uiLine, strCondition, strFile, f_pCurSuite, f_pCurTest); - - if ((CU_TRUE == bFatal) && (NULL != f_pCurTest->pJumpBuf)) { - longjmp(*(f_pCurTest->pJumpBuf), 1); - } - } - - return bValue; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartHandler) -{ - f_pSuiteStartMessageHandler = pSuiteStartHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartHandler) -{ - f_pTestStartMessageHandler = pTestStartHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteHandler) -{ - f_pTestCompleteMessageHandler = pTestCompleteHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteHandler) -{ - f_pSuiteCompleteMessageHandler = pSuiteCompleteHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteHandler) -{ - f_pAllTestsCompleteMessageHandler = pAllTestsCompleteHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureHandler) -{ - f_pSuiteInitFailureMessageHandler = pSuiteInitFailureHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureHandler) -{ - f_pSuiteCleanupFailureMessageHandler = pSuiteCleanupFailureHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteStartMessageHandler CU_get_suite_start_handler(void) -{ - return f_pSuiteStartMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_TestStartMessageHandler CU_get_test_start_handler(void) -{ - return f_pTestStartMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_TestCompleteMessageHandler CU_get_test_complete_handler(void) -{ - return f_pTestCompleteMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteCompleteMessageHandler CU_get_suite_complete_handler(void) -{ - return f_pSuiteCompleteMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_AllTestsCompleteMessageHandler CU_get_all_test_complete_handler(void) -{ - return f_pAllTestsCompleteMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteInitFailureMessageHandler CU_get_suite_init_failure_handler(void) -{ - return f_pSuiteInitFailureMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteCleanupFailureMessageHandler CU_get_suite_cleanup_failure_handler(void) -{ - return f_pSuiteCleanupFailureMessageHandler; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_suites_run(void) -{ - return f_run_summary.nSuitesRun; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_suites_failed(void) -{ - return f_run_summary.nSuitesFailed; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_suites_inactive(void) -{ - return f_run_summary.nSuitesInactive; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_tests_run(void) -{ - return f_run_summary.nTestsRun; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_tests_failed(void) -{ - return f_run_summary.nTestsFailed; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_tests_inactive(void) -{ - return f_run_summary.nTestsInactive; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_asserts(void) -{ - return f_run_summary.nAsserts; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_successes(void) -{ - return (f_run_summary.nAsserts - f_run_summary.nAssertsFailed); -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_failures(void) -{ - return f_run_summary.nAssertsFailed; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_failure_records(void) -{ - return f_run_summary.nFailureRecords; -} - -/*------------------------------------------------------------------------*/ -double CU_get_elapsed_time(void) -{ - if (CU_TRUE == f_bTestIsRunning) { - return ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - } - else { - return f_run_summary.ElapsedTime; - } -} - -/*------------------------------------------------------------------------*/ -CU_pFailureRecord CU_get_failure_list(void) -{ - return f_failure_list; -} - -/*------------------------------------------------------------------------*/ -CU_pRunSummary CU_get_run_summary(void) -{ - return &f_run_summary; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_run_all_tests(void) -{ - CU_pTestRegistry pRegistry = CU_get_registry(); - CU_pSuite pSuite = NULL; - CU_ErrorCode result = CUE_SUCCESS; - CU_ErrorCode result2; - - /* Clear results from the previous run */ - clear_previous_results(&f_run_summary, &f_failure_list); - - if (NULL == pRegistry) { - result = CUE_NOREGISTRY; - } - else { - /* test run is starting - set flag */ - f_bTestIsRunning = CU_TRUE; - f_start_time = clock(); - - pSuite = pRegistry->pSuite; - while ((NULL != pSuite) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { - result2 = run_single_suite(pSuite, &f_run_summary); - result = (CUE_SUCCESS == result) ? result2 : result; /* result = 1st error encountered */ - pSuite = pSuite->pNext; - } - - /* test run is complete - clear flag */ - f_bTestIsRunning = CU_FALSE; - f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - - if (NULL != f_pAllTestsCompleteMessageHandler) { - (*f_pAllTestsCompleteMessageHandler)(f_failure_list); - } - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_run_suite(CU_pSuite pSuite) -{ - CU_ErrorCode result = CUE_SUCCESS; - - /* Clear results from the previous run */ - clear_previous_results(&f_run_summary, &f_failure_list); - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - /* test run is starting - set flag */ - f_bTestIsRunning = CU_TRUE; - f_start_time = clock(); - - result = run_single_suite(pSuite, &f_run_summary); - - /* test run is complete - clear flag */ - f_bTestIsRunning = CU_FALSE; - f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - - /* run handler for overall completion, if any */ - if (NULL != f_pAllTestsCompleteMessageHandler) { - (*f_pAllTestsCompleteMessageHandler)(f_failure_list); - } - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest) -{ - CU_ErrorCode result = CUE_SUCCESS; - CU_ErrorCode result2; - - /* Clear results from the previous run */ - clear_previous_results(&f_run_summary, &f_failure_list); - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else if (NULL == pTest) { - result = CUE_NOTEST; - } - else if (CU_FALSE == pSuite->fActive) { - f_run_summary.nSuitesInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, - 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); - } - result = CUE_SUITE_INACTIVE; - } - else if ((NULL == pTest->pName) || (NULL == CU_get_test_by_name(pTest->pName, pSuite))) { - result = CUE_TEST_NOT_IN_SUITE; - } - else { - /* test run is starting - set flag */ - f_bTestIsRunning = CU_TRUE; - f_start_time = clock(); - - f_pCurTest = NULL; - f_pCurSuite = pSuite; - - /* run handler for suite start, if any */ - if (NULL != f_pSuiteStartMessageHandler) { - (*f_pSuiteStartMessageHandler)(pSuite); - } - - /* run the suite initialization function, if any */ - if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { - /* init function had an error - call handler, if any */ - if (NULL != f_pSuiteInitFailureMessageHandler) { - (*f_pSuiteInitFailureMessageHandler)(pSuite); - } - f_run_summary.nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, - _("Suite Initialization failed - Suite Skipped"), - _("CUnit System"), pSuite, NULL); - result = CUE_SINIT_FAILED; - } - /* reach here if no suite initialization, or if it succeeded */ - else { - result2 = run_single_test(pTest, &f_run_summary); - result = (CUE_SUCCESS == result) ? result2 : result; - - /* run the suite cleanup function, if any */ - if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { - /* cleanup function had an error - call handler, if any */ - if (NULL != f_pSuiteCleanupFailureMessageHandler) { - (*f_pSuiteCleanupFailureMessageHandler)(pSuite); - } - f_run_summary.nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, - 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); - result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; - } - } - - /* run handler for suite completion, if any */ - if (NULL != f_pSuiteCompleteMessageHandler) { - (*f_pSuiteCompleteMessageHandler)(pSuite, NULL); - } - - /* test run is complete - clear flag */ - f_bTestIsRunning = CU_FALSE; - f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - - /* run handler for overall completion, if any */ - if (NULL != f_pAllTestsCompleteMessageHandler) { - (*f_pAllTestsCompleteMessageHandler)(f_failure_list); - } - - f_pCurSuite = NULL; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -void CU_clear_previous_results(void) -{ - clear_previous_results(&f_run_summary, &f_failure_list); -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_current_suite(void) -{ - return f_pCurSuite; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_current_test(void) -{ - return f_pCurTest; -} - -/*------------------------------------------------------------------------*/ -CU_BOOL CU_is_test_running(void) -{ - return f_bTestIsRunning; -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive) -{ - f_failure_on_inactive = new_inactive; -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void) -{ - return f_failure_on_inactive; -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT void CU_print_run_results(FILE *file) -{ - char *summary_string; - - assert(NULL != file); - summary_string = CU_get_run_results_string(); - if (NULL != summary_string) { - fprintf(file, "%s", summary_string); - CU_FREE(summary_string); - } - else { - fprintf(file, _("An error occurred printing the run results.")); - } -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT char * CU_get_run_results_string(void) - -{ - CU_pRunSummary pRunSummary = &f_run_summary; - CU_pTestRegistry pRegistry = CU_get_registry(); - int width[9]; - size_t len; - char *result; - - assert(NULL != pRunSummary); - assert(NULL != pRegistry); - - width[0] = strlen(_("Run Summary:")); - width[1] = CU_MAX(6, - CU_MAX(strlen(_("Type")), - CU_MAX(strlen(_("suites")), - CU_MAX(strlen(_("tests")), - strlen(_("asserts")))))) + 1; - width[2] = CU_MAX(6, - CU_MAX(strlen(_("Total")), - CU_MAX(CU_number_width(pRegistry->uiNumberOfSuites), - CU_MAX(CU_number_width(pRegistry->uiNumberOfTests), - CU_number_width(pRunSummary->nAsserts))))) + 1; - width[3] = CU_MAX(6, - CU_MAX(strlen(_("Ran")), - CU_MAX(CU_number_width(pRunSummary->nSuitesRun), - CU_MAX(CU_number_width(pRunSummary->nTestsRun), - CU_number_width(pRunSummary->nAsserts))))) + 1; - width[4] = CU_MAX(6, - CU_MAX(strlen(_("Passed")), - CU_MAX(strlen(_("n/a")), - CU_MAX(CU_number_width(pRunSummary->nTestsRun - pRunSummary->nTestsFailed), - CU_number_width(pRunSummary->nAsserts - pRunSummary->nAssertsFailed))))) + 1; - width[5] = CU_MAX(6, - CU_MAX(strlen(_("Failed")), - CU_MAX(CU_number_width(pRunSummary->nSuitesFailed), - CU_MAX(CU_number_width(pRunSummary->nTestsFailed), - CU_number_width(pRunSummary->nAssertsFailed))))) + 1; - width[6] = CU_MAX(6, - CU_MAX(strlen(_("Inactive")), - CU_MAX(CU_number_width(pRunSummary->nSuitesInactive), - CU_MAX(CU_number_width(pRunSummary->nTestsInactive), - strlen(_("n/a")))))) + 1; - - width[7] = strlen(_("Elapsed time = ")); - width[8] = strlen(_(" seconds")); - - len = 13 + 4*(width[0] + width[1] + width[2] + width[3] + width[4] + width[5] + width[6]) + width[7] + width[8] + 1; - result = (char *)CU_MALLOC(len); - - if (NULL != result) { - snprintf(result, len, "%*s%*s%*s%*s%*s%*s%*s\n" /* if you change this, be sure */ - "%*s%*s%*u%*u%*s%*u%*u\n" /* to change the calculation of */ - "%*s%*s%*u%*u%*u%*u%*u\n" /* len above! */ - "%*s%*s%*u%*u%*u%*u%*s\n\n" - "%*s%8.3f%*s", - width[0], _("Run Summary:"), - width[1], _("Type"), - width[2], _("Total"), - width[3], _("Ran"), - width[4], _("Passed"), - width[5], _("Failed"), - width[6], _("Inactive"), - width[0], " ", - width[1], _("suites"), - width[2], pRegistry->uiNumberOfSuites, - width[3], pRunSummary->nSuitesRun, - width[4], _("n/a"), - width[5], pRunSummary->nSuitesFailed, - width[6], pRunSummary->nSuitesInactive, - width[0], " ", - width[1], _("tests"), - width[2], pRegistry->uiNumberOfTests, - width[3], pRunSummary->nTestsRun, - width[4], pRunSummary->nTestsRun - pRunSummary->nTestsFailed, - width[5], pRunSummary->nTestsFailed, - width[6], pRunSummary->nTestsInactive, - width[0], " ", - width[1], _("asserts"), - width[2], pRunSummary->nAsserts, - width[3], pRunSummary->nAsserts, - width[4], pRunSummary->nAsserts - pRunSummary->nAssertsFailed, - width[5], pRunSummary->nAssertsFailed, - width[6], _("n/a"), - width[7], _("Elapsed time = "), CU_get_elapsed_time(), /* makes sure time is updated */ - width[8], _(" seconds") - ); - result[len-1] = '\0'; - } - return result; -} - -/*================================================================= - * Static Function Definitions - *=================================================================*/ -/** - * Records a runtime failure. - * This function is called whenever a runtime failure occurs. - * This includes user assertion failures, suite initialization and - * cleanup failures, and inactive suites/tests when set as failures. - * This function records the details of the failure in a new - * failure record in the linked list of runtime failures. - * - * @param ppFailure Pointer to head of linked list of failure - * records to append with new failure record. - * If it points to a NULL pointer, it will be set - * to point to the new failure record. - * @param pRunSummary Pointer to CU_RunSummary keeping track of failure records - * (ignored if NULL). - * @param type Type of failure. - * @param uiLineNumber Line number of the failure, if applicable. - * @param szCondition Description of failure condition - * @param szFileName Name of file, if applicable - * @param pSuite The suite being run at time of failure - * @param pTest The test being run at time of failure - */ -static void add_failure(CU_pFailureRecord* ppFailure, - CU_pRunSummary pRunSummary, - CU_FailureType type, - unsigned int uiLineNumber, - const char *szCondition, - const char *szFileName, - CU_pSuite pSuite, - CU_pTest pTest) -{ - CU_pFailureRecord pFailureNew = NULL; - CU_pFailureRecord pTemp = NULL; - - assert(NULL != ppFailure); - - pFailureNew = (CU_pFailureRecord)CU_MALLOC(sizeof(CU_FailureRecord)); - - if (NULL == pFailureNew) { - return; - } - - pFailureNew->strFileName = NULL; - pFailureNew->strCondition = NULL; - if (NULL != szFileName) { - pFailureNew->strFileName = (char*)CU_MALLOC(strlen(szFileName) + 1); - if(NULL == pFailureNew->strFileName) { - CU_FREE(pFailureNew); - return; - } - strcpy(pFailureNew->strFileName, szFileName); - } - - if (NULL != szCondition) { - pFailureNew->strCondition = (char*)CU_MALLOC(strlen(szCondition) + 1); - if (NULL == pFailureNew->strCondition) { - if(NULL != pFailureNew->strFileName) { - CU_FREE(pFailureNew->strFileName); - } - CU_FREE(pFailureNew); - return; - } - strcpy(pFailureNew->strCondition, szCondition); - } - - pFailureNew->type = type; - pFailureNew->uiLineNumber = uiLineNumber; - pFailureNew->pTest = pTest; - pFailureNew->pSuite = pSuite; - pFailureNew->pNext = NULL; - pFailureNew->pPrev = NULL; - - pTemp = *ppFailure; - if (NULL != pTemp) { - while (NULL != pTemp->pNext) { - pTemp = pTemp->pNext; - } - pTemp->pNext = pFailureNew; - pFailureNew->pPrev = pTemp; - } - else { - *ppFailure = pFailureNew; - } - - if (NULL != pRunSummary) { - ++(pRunSummary->nFailureRecords); - } - f_last_failure = pFailureNew; -} - -/* - * Local function for result set initialization/cleanup. - */ -/*------------------------------------------------------------------------*/ -/** - * Initializes the run summary information in the specified structure. - * Resets the run counts to zero, and calls cleanup_failure_list() if - * failures were recorded by the last test run. Calling this function - * multiple times, while inefficient, will not cause an error condition. - * - * @param pRunSummary CU_RunSummary to initialize (non-NULL). - * @param ppFailure The failure record to clean (non-NULL). - * @see CU_clear_previous_results() - */ -static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure) -{ - assert(NULL != pRunSummary); - assert(NULL != ppFailure); - - pRunSummary->nSuitesRun = 0; - pRunSummary->nSuitesFailed = 0; - pRunSummary->nSuitesInactive = 0; - pRunSummary->nTestsRun = 0; - pRunSummary->nTestsFailed = 0; - pRunSummary->nTestsInactive = 0; - pRunSummary->nAsserts = 0; - pRunSummary->nAssertsFailed = 0; - pRunSummary->nFailureRecords = 0; - pRunSummary->ElapsedTime = 0.0; - - if (NULL != *ppFailure) { - cleanup_failure_list(ppFailure); - } - - f_last_failure = NULL; -} - -/*------------------------------------------------------------------------*/ -/** - * Frees all memory allocated for the linked list of test failure - * records. pFailure is reset to NULL after its list is cleaned up. - * - * @param ppFailure Pointer to head of linked list of - * CU_pFailureRecords to clean. - * @see CU_clear_previous_results() - */ -static void cleanup_failure_list(CU_pFailureRecord* ppFailure) -{ - CU_pFailureRecord pCurFailure = NULL; - CU_pFailureRecord pNextFailure = NULL; - - pCurFailure = *ppFailure; - - while (NULL != pCurFailure) { - - if (NULL != pCurFailure->strCondition) { - CU_FREE(pCurFailure->strCondition); - } - - if (NULL != pCurFailure->strFileName) { - CU_FREE(pCurFailure->strFileName); - } - - pNextFailure = pCurFailure->pNext; - CU_FREE(pCurFailure); - pCurFailure = pNextFailure; - } - - *ppFailure = NULL; -} - - -/* Function to collect syslog for a test */ -//FIXME find another bettwer way to do this, It is currentlly leaving tail procs in ps -void cunit_logger_fn(void *fn){ - char sys_cmd[MAX_CMD_LEN] = {'\0'}; - sprintf(sys_cmd, "tail -f /var/log/syslog > %s.txt", (char *)fn); - system("service syslog restart"); - system(sys_cmd); -} - -/*------------------------------------------------------------------------*/ -/** - * Runs all tests in a specified suite. - * Internal function to run all tests in a suite. The suite need - * not be registered in the test registry to be run. Only - * suites having their fActive flags set CU_TRUE will actually be - * run. If the CUnit framework is in an error condition after - * running a test, no additional tests are run. - * - * @param pSuite The suite containing the test (non-NULL). - * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). - * @return A CU_ErrorCode indicating the status of the run. - * @see CU_run_suite() for public interface function. - * @see CU_run_all_tests() for running all suites. - */ -static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary) -{ - CU_pTest pTest = NULL; - unsigned int nStartFailures; - /* keep track of the last failure BEFORE running the test */ - CU_pFailureRecord pLastFailure = f_last_failure; - CU_ErrorCode result = CUE_SUCCESS; - CU_ErrorCode result2; - - assert(NULL != pSuite); - assert(NULL != pRunSummary); - - nStartFailures = pRunSummary->nFailureRecords; - - f_pCurTest = NULL; - f_pCurSuite = pSuite; - - /* run handler for suite start, if any */ - if (NULL != f_pSuiteStartMessageHandler) { - (*f_pSuiteStartMessageHandler)(pSuite); - } - - /* run suite if it's active */ - if (CU_FALSE != pSuite->fActive) { - - /* run the suite initialization function, if any */ - if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { - /* init function had an error - call handler, if any */ - if (NULL != f_pSuiteInitFailureMessageHandler) { - (*f_pSuiteInitFailureMessageHandler)(pSuite); - } - pRunSummary->nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, - _("Suite Initialization failed - Suite Skipped"), - _("CUnit System"), pSuite, NULL); - result = CUE_SINIT_FAILED; - } - - /* reach here if no suite initialization, or if it succeeded */ - else { - pTest = pSuite->pTest; - while ((NULL != pTest) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { - if (CU_FALSE != pTest->fActive) { - result2 = run_single_test(pTest, pRunSummary); - result = (CUE_SUCCESS == result) ? result2 : result; - } - else { - f_run_summary.nTestsInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, - 0, _("Test inactive"), _("CUnit System"), pSuite, pTest); - result = CUE_TEST_INACTIVE; - } - } - pTest = pTest->pNext; - } - pRunSummary->nSuitesRun++; - - /* call the suite cleanup function, if any */ - if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { - if (NULL != f_pSuiteCleanupFailureMessageHandler) { - (*f_pSuiteCleanupFailureMessageHandler)(pSuite); - } - pRunSummary->nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, - 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); - result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; - } - } - } - - /* otherwise record inactive suite and failure if appropriate */ - else { - f_run_summary.nSuitesInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, - 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); - result = CUE_SUITE_INACTIVE; - } - } - - /* if additional failures have occurred... */ - if (pRunSummary->nFailureRecords > nStartFailures) { - if (NULL != pLastFailure) { - pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ - } - else { - pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ - } - } - else { - pLastFailure = NULL; /* no additional failure - set to NULL */ - } - - /* run handler for suite completion, if any */ - if (NULL != f_pSuiteCompleteMessageHandler) { - (*f_pSuiteCompleteMessageHandler)(pSuite, pLastFailure); - } - - f_pCurSuite = NULL; - return result; -} - -/*------------------------------------------------------------------------*/ -/** - * Runs a specific test. - * Internal function to run a test case. This includes calling - * any handler to be run before executing the test, running the - * test's function (if any), and calling any handler to be run - * after executing a test. Suite initialization and cleanup functions - * are not called by this function. A current suite must be set and - * active (checked by assertion). - * - * @param pTest The test to be run (non-NULL). - * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). - * @return A CU_ErrorCode indicating the status of the run. - * @see CU_run_test() for public interface function. - * @see CU_run_all_tests() for running all suites. - */ -static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary) -{ - volatile unsigned int nStartFailures; -#if NOT_YET - pthread_t tid; -#endif - int rv = 0; - cunit_logger_cb cb = cunit_logger_fn; - /* keep track of the last failure BEFORE running the test */ - volatile CU_pFailureRecord pLastFailure = f_last_failure; - jmp_buf buf; - CU_ErrorCode result = CUE_SUCCESS; - - assert(NULL != f_pCurSuite); - assert(CU_FALSE != f_pCurSuite->fActive); - assert(NULL != pTest); - assert(NULL != pRunSummary); - - nStartFailures = pRunSummary->nFailureRecords; - - f_pCurTest = pTest; - - if (NULL != f_pTestStartMessageHandler) { - (*f_pTestStartMessageHandler)(f_pCurTest, f_pCurSuite); - } - - /* run test if it is active */ - if (CU_FALSE != pTest->fActive) { - - /* set jmp_buf and run test */ - pTest->pJumpBuf = &buf; - if (0 == setjmp(buf)) { - if (NULL != pTest->pTestFunc) { -#if NOT_YET - if (collect_syslog) {//FIXME Currently WIP, It is not reliable for huge batch runs now - rv = pthread_create(&tid, NULL, (void *)cb, (void *)pTest->pName); - if (rv) { - fprintf(stdout, "thread create failed for collecting syslog for %s\n",(char *)pTest->pName); - } - } -#endif - (*pTest->pTestFunc)(); -#if NOT_YET - if (collect_syslog){ - sleep(1); - pthread_cancel(tid); - } -#endif - } - } - - pRunSummary->nTestsRun++; - } - else { - f_run_summary.nTestsInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, - 0, _("Test inactive"), _("CUnit System"), f_pCurSuite, f_pCurTest); - } - result = CUE_TEST_INACTIVE; - } - - /* if additional failures have occurred... */ - if (pRunSummary->nFailureRecords > nStartFailures) { - pRunSummary->nTestsFailed++; - if (NULL != pLastFailure) { - pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ - } - else { - pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ - } - } - else { - pLastFailure = NULL; /* no additional failure - set to NULL */ - } - - if (NULL != f_pTestCompleteMessageHandler) { - (*f_pTestCompleteMessageHandler)(f_pCurTest, f_pCurSuite, pLastFailure); - } - - pTest->pJumpBuf = NULL; - f_pCurTest = NULL; - - return result; -} - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -/** Types of framework events tracked by test system. */ -typedef enum TET { - SUITE_START = 1, - TEST_START, - TEST_COMPLETE, - SUITE_COMPLETE, - ALL_TESTS_COMPLETE, - SUITE_INIT_FAILED, - SUITE_CLEANUP_FAILED -} TestEventType; - -/** Test event structure for recording details of a framework event. */ -typedef struct TE { - TestEventType type; - CU_pSuite pSuite; - CU_pTest pTest; - CU_pFailureRecord pFailure; - struct TE * pNext; -} TestEvent, * pTestEvent; - -static int f_nTestEvents = 0; -static pTestEvent f_pFirstEvent = NULL; - -/** Creates & stores a test event record having the specified details. */ -static void add_test_event(TestEventType type, CU_pSuite psuite, - CU_pTest ptest, CU_pFailureRecord pfailure) -{ - pTestEvent pNewEvent = (pTestEvent)malloc(sizeof(TestEvent)); - pTestEvent pNextEvent = f_pFirstEvent; - - if (NULL == pNewEvent) { - fprintf(stderr, "Memory allocation failed in add_test_event()."); - exit(1); - } - - pNewEvent->type = type; - pNewEvent->pSuite = psuite; - pNewEvent->pTest = ptest; - pNewEvent->pFailure = pfailure; - pNewEvent->pNext = NULL; - - if (pNextEvent) { - while (pNextEvent->pNext) { - pNextEvent = pNextEvent->pNext; - } - pNextEvent->pNext = pNewEvent; - } - else { - f_pFirstEvent = pNewEvent; - } - ++f_nTestEvents; -} - -/** Deallocates all test event data. */ -static void clear_test_events(void) -{ - pTestEvent pCurrentEvent = f_pFirstEvent; - pTestEvent pNextEvent = NULL; - - while (pCurrentEvent) { - pNextEvent = pCurrentEvent->pNext; - free(pCurrentEvent); - pCurrentEvent = pNextEvent; - } - - f_pFirstEvent = NULL; - f_nTestEvents = 0; -} - -static void suite_start_handler(const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(NULL == CU_get_current_test()); - - add_test_event(SUITE_START, pSuite, NULL, NULL); -} - -static void test_start_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(pTest == CU_get_current_test()); - - add_test_event(TEST_START, pSuite, pTest, NULL); -} - -static void test_complete_handler(const CU_pTest pTest, const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(pTest == CU_get_current_test()); - - add_test_event(TEST_COMPLETE, pSuite, pTest, pFailure); -} - -static void suite_complete_handler(const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(NULL == CU_get_current_test()); - - add_test_event(SUITE_COMPLETE, pSuite, NULL, pFailure); -} - -static void test_all_complete_handler(const CU_pFailureRecord pFailure) -{ - TEST(!CU_is_test_running()); - - add_test_event(ALL_TESTS_COMPLETE, NULL, NULL, pFailure); -} - -static void suite_init_failure_handler(const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - - add_test_event(SUITE_INIT_FAILED, pSuite, NULL, NULL); -} - -static void suite_cleanup_failure_handler(const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - - add_test_event(SUITE_CLEANUP_FAILED, pSuite, NULL, NULL); -} - -/** - * Centralize test result testing - we're going to do it a lot! - * This is messy since we want to report the calling location upon failure. - * - * Via calling test functions tests: - * CU_get_number_of_suites_run() - * CU_get_number_of_suites_failed() - * CU_get_number_of_tests_run() - * CU_get_number_of_tests_failed() - * CU_get_number_of_asserts() - * CU_get_number_of_successes() - * CU_get_number_of_failures() - * CU_get_number_of_failure_records() - * CU_get_run_summary() - */ -static void do_test_results(unsigned int nSuitesRun, - unsigned int nSuitesFailed, - unsigned int nSuitesInactive, - unsigned int nTestsRun, - unsigned int nTestsFailed, - unsigned int nTestsInactive, - unsigned int nAsserts, - unsigned int nSuccesses, - unsigned int nFailures, - unsigned int nFailureRecords, - const char *file, - unsigned int line) -{ - char msg[500]; - CU_pRunSummary pRunSummary = NULL; - - if (nSuitesRun == CU_get_number_of_suites_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", - nSuitesRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nSuitesInactive == CU_get_number_of_suites_inactive()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_inactive() (called from %s:%u)", - nSuitesInactive, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nSuitesFailed == CU_get_number_of_suites_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", - nSuitesFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nTestsRun == CU_get_number_of_tests_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", - nTestsRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nTestsFailed == CU_get_number_of_tests_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", - nTestsFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nTestsInactive == CU_get_number_of_tests_inactive()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_inactive() (called from %s:%u)", - nTestsInactive, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nAsserts == CU_get_number_of_asserts()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", - nAsserts, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nSuccesses == CU_get_number_of_successes()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_successes() (called from %s:%u)", - nSuccesses, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nFailures == CU_get_number_of_failures()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", - nFailures, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nFailureRecords == CU_get_number_of_failure_records()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", - nFailureRecords, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - pRunSummary = CU_get_run_summary(); - - if (pRunSummary->nSuitesRun == CU_get_number_of_suites_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", - pRunSummary->nSuitesRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nSuitesFailed == CU_get_number_of_suites_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", - pRunSummary->nSuitesFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nTestsRun == CU_get_number_of_tests_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", - pRunSummary->nTestsRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nTestsFailed == CU_get_number_of_tests_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", - pRunSummary->nTestsFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nAsserts == CU_get_number_of_asserts()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", - pRunSummary->nAsserts, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nAssertsFailed == CU_get_number_of_failures()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", - pRunSummary->nAssertsFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nFailureRecords == CU_get_number_of_failure_records()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", - pRunSummary->nFailureRecords, file, line); - msg[499] = '\0'; - FAIL(msg); - } -} - -#define test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ - nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords) \ - do_test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ - nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords, \ - __FILE__, __LINE__) - -static void test_succeed(void) { CU_TEST(CU_TRUE); } -static void test_fail(void) { CU_TEST(CU_FALSE); } -static int suite_succeed(void) { return 0; } -static int suite_fail(void) { return 1; } - -/*-------------------------------------------------*/ -/* tests: - * CU_set_suite_start_handler() - * CU_set_test_start_handler() - * CU_set_test_complete_handler() - * CU_set_suite_complete_handler() - * CU_set_all_test_complete_handler() - * CU_set_suite_init_failure_handler() - * CU_set_suite_cleanup_failure_handler() - * CU_get_suite_start_handler() - * CU_get_test_start_handler() - * CU_get_test_complete_handler() - * CU_get_suite_complete_handler() - * CU_get_all_test_complete_handler() - * CU_get_suite_init_failure_handler() - * CU_get_suite_cleanup_failure_handler() - * CU_is_test_running() - * via handlers tests: - * CU_get_current_suite() - * CU_get_current_test() - */ -static void test_message_handlers(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - pTestEvent pEvent = NULL; - - TEST(!CU_is_test_running()); - - /* handlers should be NULL on startup */ - TEST(NULL == CU_get_suite_start_handler()); - TEST(NULL == CU_get_test_start_handler()); - TEST(NULL == CU_get_test_complete_handler()); - TEST(NULL == CU_get_suite_complete_handler()); - TEST(NULL == CU_get_all_test_complete_handler()); - TEST(NULL == CU_get_suite_init_failure_handler()); - TEST(NULL == CU_get_suite_cleanup_failure_handler()); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test3", test_succeed); - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest4 = CU_add_test(pSuite2, "test4", test_succeed); - pSuite3 = CU_add_suite("suite3", suite_succeed, suite_fail); - pTest5 = CU_add_test(pSuite3, "test5", test_fail); - - TEST_FATAL(CUE_SUCCESS == CU_get_error()); - - /* first run tests without handlers set */ - clear_test_events(); - CU_run_all_tests(); - - TEST(0 == f_nTestEvents); - TEST(NULL == f_pFirstEvent); - test_results(2,2,0,4,2,0,4,2,2,4); - - /* set handlers to local functions */ - CU_set_suite_start_handler(&suite_start_handler); - CU_set_test_start_handler(&test_start_handler); - CU_set_test_complete_handler(&test_complete_handler); - CU_set_suite_complete_handler(&suite_complete_handler); - CU_set_all_test_complete_handler(&test_all_complete_handler); - CU_set_suite_init_failure_handler(&suite_init_failure_handler); - CU_set_suite_cleanup_failure_handler(&suite_cleanup_failure_handler); - - /* confirm handlers set properly */ - TEST(suite_start_handler == CU_get_suite_start_handler()); - TEST(test_start_handler == CU_get_test_start_handler()); - TEST(test_complete_handler == CU_get_test_complete_handler()); - TEST(suite_complete_handler == CU_get_suite_complete_handler()); - TEST(test_all_complete_handler == CU_get_all_test_complete_handler()); - TEST(suite_init_failure_handler == CU_get_suite_init_failure_handler()); - TEST(suite_cleanup_failure_handler == CU_get_suite_cleanup_failure_handler()); - - /* run tests again with handlers set */ - clear_test_events(); - CU_run_all_tests(); - - TEST(17 == f_nTestEvents); - if (17 == f_nTestEvents) { - pEvent = f_pFirstEvent; - TEST(SUITE_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest1 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest1 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest2 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest2 == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest3 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest3 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_START == pEvent->type); - TEST(pSuite2 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_INIT_FAILED == pEvent->type); - TEST(pSuite2 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_COMPLETE == pEvent->type); - TEST(pSuite2 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_START == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(pTest5 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(pTest5 == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_CLEANUP_FAILED == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_COMPLETE == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(ALL_TESTS_COMPLETE == pEvent->type); - TEST(NULL == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - if (4 == CU_get_number_of_failure_records()) { - TEST(NULL != pEvent->pFailure->pNext); - TEST(NULL != pEvent->pFailure->pNext->pNext); - TEST(NULL != pEvent->pFailure->pNext->pNext->pNext); - TEST(NULL == pEvent->pFailure->pNext->pNext->pNext->pNext); - } - TEST(pEvent->pFailure == CU_get_failure_list()); - } - - test_results(2,2,0,4,2,0,4,2,2,4); - - /* clear handlers and run again */ - CU_set_suite_start_handler(NULL); - CU_set_test_start_handler(NULL); - CU_set_test_complete_handler(NULL); - CU_set_suite_complete_handler(NULL); - CU_set_all_test_complete_handler(NULL); - CU_set_suite_init_failure_handler(NULL); - CU_set_suite_cleanup_failure_handler(NULL); - - TEST(NULL == CU_get_suite_start_handler()); - TEST(NULL == CU_get_test_start_handler()); - TEST(NULL == CU_get_test_complete_handler()); - TEST(NULL == CU_get_suite_complete_handler()); - TEST(NULL == CU_get_all_test_complete_handler()); - TEST(NULL == CU_get_suite_init_failure_handler()); - TEST(NULL == CU_get_suite_cleanup_failure_handler()); - - clear_test_events(); - CU_run_all_tests(); - - TEST(0 == f_nTestEvents); - TEST(NULL == f_pFirstEvent); - test_results(2,2,0,4,2,0,4,2,2,4); - - CU_cleanup_registry(); - clear_test_events(); -} - -static CU_BOOL f_exit_called = CU_FALSE; - -/* intercept exit for testing of CUEA_ABORT action */ -void test_exit(int status) -{ - CU_UNREFERENCED_PARAMETER(status); /* not used */ - f_exit_called = CU_TRUE; -} - - -/*-------------------------------------------------*/ -static void test_CU_fail_on_inactive(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - - CU_set_error_action(CUEA_IGNORE); - CU_initialize_registry(); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest3 = CU_add_test(pSuite2, "test3", test_succeed); - pTest4 = CU_add_test(pSuite2, "test4", test_succeed); - - /* test initial conditions */ - TEST(CU_TRUE == CU_get_fail_on_inactive()); - TEST(CU_TRUE == pSuite1->fActive); - TEST(CU_TRUE == pSuite2->fActive); - TEST(CU_TRUE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_TRUE == pTest4->fActive); - - CU_set_fail_on_inactive(CU_TRUE); - TEST(CU_TRUE == CU_get_fail_on_inactive()); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ - test_results(1,1,0,2,1,0,2,1,1,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CU_FALSE == CU_get_fail_on_inactive()); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); - test_results(1,1,0,2,1,0,2,1,1,2); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* all suites inactive */ - test_results(0,0,2,0,0,0,0,0,0,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(0,0,2,0,0,0,0,0,0,0); - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* some suites inactive */ - test_results(1,0,1,2,1,0,2,1,1,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(1,0,1,2,1,0,2,1,1,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* all tests inactive */ - test_results(1,1,0,0,0,2,0,0,0,3); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); - test_results(1,1,0,0,0,2,0,0,0,1); - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some tests inactive */ - test_results(1,1,0,1,0,1,1,1,0,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); - test_results(1,1,0,1,0,1,1,1,0,1); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_test_active(pTest1, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some suites & tests inactive */ - test_results(1,0,1,1,1,1,1,0,1,3); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(1,0,1,1,1,1,1,0,1,1); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_test_active(pTest1, CU_TRUE); - - /* clean up */ - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -static void test_CU_run_all_tests(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - CU_pTest pTest10 = NULL; - - /* error - uninitialized registry (CUEA_IGNORE) */ - CU_cleanup_registry(); - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOREGISTRY == CU_run_all_tests()); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* error - uninitialized registry (CUEA_FAIL) */ - CU_cleanup_registry(); - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOREGISTRY == CU_run_all_tests()); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* error - uninitialized registry (CUEA_ABORT) */ - CU_cleanup_registry(); - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_all_tests(); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* run with no suites or tests registered */ - CU_initialize_registry(); - - CU_set_error_action(CUEA_IGNORE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(0,0,0,0,0,0,0,0,0,0); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name OK */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test9", test_succeed); - pSuite4 = CU_add_suite("suite4", NULL, suite_fail); - pTest10 = CU_add_test(pSuite4, "test10", test_succeed); - - TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); - TEST_FATAL(10 == CU_get_registry()->uiNumberOfTests); - - /* run all tests (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ - test_results(3,2,0,8,3,0,8,5,3,5); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_suite_active(pSuite4, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); /* suites inactive */ - test_results(0,0,4,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - test_results(0,0,4,0,0,0,0,0,0,4); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some suites inactive */ - test_results(1,1,2,2,1,0,2,1,1,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - test_results(1,1,2,2,1,0,2,1,1,4); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ - test_results(3,2,0,0,0,8,0,0,0,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(3,2,0,0,0,8,0,0,0,10); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ - test_results(3,2,0,4,0,4,4,4,0,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(3,2,0,4,0,4,4,4,0,6); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_TRUE); - - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - test_results(2,3,0,3,1,0,3,2,1,4); - - CU_set_suite_cleanupfunc(pSuite4, NULL); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite4, &suite_fail); - test_results(3,1,0,8,3,0,8,5,3,4); - - CU_set_test_func(pTest2, &test_succeed); - CU_set_test_func(pTest4, &test_succeed); - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ - CU_set_test_func(pTest2, &test_fail); - CU_set_test_func(pTest4, &test_fail); - CU_set_test_func(pTest8, &test_fail); - test_results(3,2,0,8,0,0,8,8,0,2); - - /* run all tests (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_suite_active(pSuite4, CU_TRUE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites inactive */ - test_results(2,1,2,6,2,0,6,4,2,3); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - test_results(1,0,1,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ - test_results(1,1,0,0,0,5,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_TRUE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ - test_results(1,1,0,2,2,3,2,0,2,3); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_TRUE); - - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - test_results(4,1,0,10,3,0,10,7,3,4); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_test_func(pTest1, &test_fail); - CU_set_test_func(pTest3, &test_fail); - CU_set_test_func(pTest5, &test_fail); - CU_set_test_func(pTest9, &test_fail); - CU_set_test_func(pTest10, &test_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ - CU_set_test_func(pTest1, &test_succeed); - CU_set_test_func(pTest3, &test_succeed); - CU_set_test_func(pTest5, &test_succeed); - CU_set_test_func(pTest9, &test_succeed); - CU_set_test_func(pTest10, &test_succeed); - test_results(1,1,0,5,5,0,5,0,5,6); - - /* run all tests (CUEA_ABORT) */ - f_exit_called = CU_FALSE; - CU_set_error_action(CUEA_ABORT); - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_suite_active(pSuite4, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); /* no suites active, so no abort() */ - TEST(CU_FALSE == f_exit_called); - test_results(0,0,4,0,0,0,0,0,0,0); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,1); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites active */ - TEST(CU_TRUE == f_exit_called); - test_results(3,1,1,8,3,0,8,5,3,4); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(1,0,1,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,5,0,0,0,1); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_TRUE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,2,2,3,2,0,2,3); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_TRUE); - - f_exit_called = CU_FALSE; - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,2,0,5,3,2,3); - - f_exit_called = CU_FALSE; - CU_set_test_func(pTest1, &test_fail); - CU_set_test_func(pTest3, &test_fail); - CU_set_test_func(pTest5, &test_fail); - CU_set_test_func(pTest9, &test_fail); - CU_set_test_func(pTest10, &test_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ - CU_set_test_func(pTest1, &test_succeed); - CU_set_test_func(pTest3, &test_succeed); - CU_set_test_func(pTest5, &test_succeed); - CU_set_test_func(pTest9, &test_succeed); - CU_set_test_func(pTest10, &test_succeed); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,5,0,5,0,5,6); - - /* clean up after testing */ - CU_set_error_action(CUEA_IGNORE); - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -static void test_CU_run_suite(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - - /* error - NULL suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOSUITE == CU_run_suite(NULL)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL suite (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOSUITE == CU_run_suite(NULL)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL suite (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_suite(NULL); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test3", test_succeed); - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test5", test_succeed); - pSuite2 = CU_add_suite("suite1", suite_fail, NULL); /* duplicate suite name OK */ - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite3", NULL, suite_fail); - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ - pSuite4 = CU_add_suite("suite4", NULL, NULL); - - TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); - TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); - - /* run each suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suites/tests active */ - test_results(1,0,0,5,2,0,5,3,2,2); - - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); - test_results(1,1,0,2,1,0,2,1,1,2); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); - test_results(1,0,0,0,0,0,0,0,0,0); - - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite3)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite3)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite3, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* all tests inactive */ - test_results(1,0,0,0,0,5,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); - test_results(1,0,0,0,0,5,0,0,0,5); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* some tests inactive */ - test_results(1,0,0,3,0,2,3,3,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); - test_results(1,0,0,3,0,2,3,3,0,2); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - test_results(0,1,0,0,0,0,0,0,0,1); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_test_func(pTest1, &test_fail); - CU_set_test_func(pTest3, &test_fail); - CU_set_test_func(pTest5, &test_fail); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ - CU_set_test_func(pTest1, &test_succeed); - CU_set_test_func(pTest3, &test_succeed); - CU_set_test_func(pTest5, &test_succeed); - test_results(1,0,0,5,5,0,5,0,5,5); - - /* run each suite (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ - test_results(1,0,0,5,2,0,5,3,2,2); - - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); - test_results(1,1,0,2,1,0,2,1,1,2); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); - test_results(1,0,0,0,0,0,0,0,0,0); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite1)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite1, CU_TRUE); - - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ - test_results(1,1,0,0,0,2,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - test_results(1,1,0,0,0,1,0,0,0,2); - - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ - test_results(1,1,0,1,1,1,1,0,1,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - test_results(1,1,0,1,1,1,1,0,1,3); - CU_set_test_active(pTest9, CU_TRUE); - - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - test_results(1,0,0,2,0,0,2,2,0,0); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_test_func(pTest2, &test_succeed); - CU_set_test_func(pTest4, &test_succeed); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ - CU_set_test_func(pTest2, &test_fail); - CU_set_test_func(pTest4, &test_fail); - test_results(1,0,0,5,0,0,5,5,0,0); - - /* run each suite (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ - TEST(CU_FALSE == f_exit_called); - test_results(1,0,0,5,2,0,5,3,2,2); - - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,2,1,0,2,1,1,2); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); - TEST(CU_FALSE == f_exit_called); - test_results(1,0,0,0,0,0,0,0,0,0); - - CU_set_suite_active(pSuite2, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* suite inactive, but not a failure */ - TEST(CU_FALSE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,0); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite2)); - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,2,0,0,0,1); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,1,0,0,0,2); - - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_TRUE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,1,0,1,1,1,0,1); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,1,0,0,0,2); - CU_set_test_active(pTest8, CU_TRUE); - - f_exit_called = CU_FALSE; - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,2,0,5,3,2,3); - - f_exit_called = CU_FALSE; - CU_set_test_func(pTest8, &test_succeed); - CU_set_test_func(pTest9, &test_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - CU_set_test_func(pTest9, &test_succeed); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,2,1,0,2,1,1,2); - - /* clean up after testing */ - CU_set_error_action(CUEA_IGNORE); - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -static void test_CU_run_test(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test3", test_succeed); - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test5", test_succeed); - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite2", NULL, suite_fail); /* duplicate suite name OK */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ - - TEST_FATAL(3 == CU_get_registry()->uiNumberOfSuites); - TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); - - /* error - NULL suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL suite (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_test(NULL, pTest1); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* error - NULL test (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); - TEST(CUE_NOTEST == CU_get_error()); - - /* error - NULL test (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); - TEST(CUE_NOTEST == CU_get_error()); - - /* error - NULL test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_test(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* error - test not in suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); - TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); - - /* error - NULL test (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); - TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); - - /* error - NULL test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_test(pSuite3, pTest1); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* run each test (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* all suite/tests active */ - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); - test_results(0,1,0,1,1,0,1,0,1,2); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite1, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); /* test inactive */ - test_results(0,0,0,0,0,1,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); - test_results(0,0,0,0,1,1,0,0,0,1); - CU_set_test_active(pTest1, CU_TRUE); - - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - test_results(0,1,0,0,0,0,0,0,0,1); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - test_results(0,1,0,1,0,0,1,1,0,1); - - /* run each test (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* suite/test active */ - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); - test_results(0,1,0,1,1,0,1,0,1,2); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest7, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); /* test inactive */ - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_test_active(pTest7, CU_TRUE); - - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - test_results(0,0,0,1,0,0,1,1,0,0); - - CU_set_suite_cleanupfunc(pSuite3, NULL); - TEST(CUE_SUCCESS == CU_run_test(pSuite3, pTest8)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite3, &suite_fail); - test_results(0,0,0,1,1,0,1,0,1,1); - - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - test_results(0,1,0,1,0,0,1,1,0,1); - - /* run each test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,1,0,1,0,1,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,1,0,1,0,1,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,1,0,1,0,1,2); - - f_exit_called = CU_FALSE; - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - f_exit_called = CU_FALSE; - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); /* suite inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - f_exit_called = CU_FALSE; - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest6, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); /* test inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_test_active(pTest6, CU_TRUE); - - f_exit_called = CU_FALSE; - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,0,0,1,1,0,1); - - f_exit_called = CU_FALSE; - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,0,0,1,1,0,1); - - /* clean up after testing */ - CU_set_error_action(CUEA_IGNORE); - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -/* tests CU_assertImplementation() - * CU_get_failure_list() - * CU_clear_previous_results() - */ -static void test_CU_assertImplementation(void) -{ - CU_Test dummy_test; - CU_Suite dummy_suite; - CU_pFailureRecord pFailure1 = NULL; - CU_pFailureRecord pFailure2 = NULL; - CU_pFailureRecord pFailure3 = NULL; - CU_pFailureRecord pFailure4 = NULL; - CU_pFailureRecord pFailure5 = NULL; - CU_pFailureRecord pFailure6 = NULL; - - CU_clear_previous_results(); - - TEST(NULL == CU_get_failure_list()); - TEST(0 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - /* fool CU_assertImplementation into thinking test run is in progress */ - f_pCurTest = &dummy_test; - f_pCurSuite = &dummy_suite; - - /* asserted value is CU_TRUE*/ - TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 100, "Nothing happened 0.", "dummy0.c", "dummy_func0", CU_FALSE)); - - TEST(NULL == CU_get_failure_list()); - TEST(1 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 101, "Nothing happened 1.", "dummy1.c", "dummy_func1", CU_FALSE)); - - TEST(NULL == CU_get_failure_list()); - TEST(2 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - /* asserted value is CU_FALSE */ - TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 102, "Something happened 2.", "dummy2.c", "dummy_func2", CU_FALSE)); - - TEST(NULL != CU_get_failure_list()); - TEST(3 == CU_get_number_of_asserts()); - TEST(1 == CU_get_number_of_failures()); - TEST(1 == CU_get_number_of_failure_records()); - - TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 103, "Something happened 3.", "dummy3.c", "dummy_func3", CU_FALSE)); - - TEST(NULL != CU_get_failure_list()); - TEST(4 == CU_get_number_of_asserts()); - TEST(2 == CU_get_number_of_failures()); - TEST(2 == CU_get_number_of_failure_records()); - - TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 104, "Something happened 4.", "dummy4.c", "dummy_func4", CU_FALSE)); - - TEST(NULL != CU_get_failure_list()); - TEST(5 == CU_get_number_of_asserts()); - TEST(3 == CU_get_number_of_failures()); - TEST(3 == CU_get_number_of_failure_records()); - - if (3 == CU_get_number_of_failure_records()) { - pFailure1 = CU_get_failure_list(); - TEST(102 == pFailure1->uiLineNumber); - TEST(!strcmp("dummy2.c", pFailure1->strFileName)); - TEST(!strcmp("Something happened 2.", pFailure1->strCondition)); - TEST(&dummy_test == pFailure1->pTest); - TEST(&dummy_suite == pFailure1->pSuite); - TEST(NULL != pFailure1->pNext); - TEST(NULL == pFailure1->pPrev); - - pFailure2 = pFailure1->pNext; - TEST(103 == pFailure2->uiLineNumber); - TEST(!strcmp("dummy3.c", pFailure2->strFileName)); - TEST(!strcmp("Something happened 3.", pFailure2->strCondition)); - TEST(&dummy_test == pFailure2->pTest); - TEST(&dummy_suite == pFailure2->pSuite); - TEST(NULL != pFailure2->pNext); - TEST(pFailure1 == pFailure2->pPrev); - - pFailure3 = pFailure2->pNext; - TEST(104 == pFailure3->uiLineNumber); - TEST(!strcmp("dummy4.c", pFailure3->strFileName)); - TEST(!strcmp("Something happened 4.", pFailure3->strCondition)); - TEST(&dummy_test == pFailure3->pTest); - TEST(&dummy_suite == pFailure3->pSuite); - TEST(NULL == pFailure3->pNext); - TEST(pFailure2 == pFailure3->pPrev); - } - else - FAIL("Unexpected number of failure records."); - - /* confirm destruction of failure records */ - pFailure4 = pFailure1; - pFailure5 = pFailure2; - pFailure6 = pFailure3; - TEST(0 != test_cunit_get_n_memevents(pFailure4)); - TEST(test_cunit_get_n_allocations(pFailure4) != test_cunit_get_n_deallocations(pFailure4)); - TEST(0 != test_cunit_get_n_memevents(pFailure5)); - TEST(test_cunit_get_n_allocations(pFailure5) != test_cunit_get_n_deallocations(pFailure5)); - TEST(0 != test_cunit_get_n_memevents(pFailure6)); - TEST(test_cunit_get_n_allocations(pFailure6) != test_cunit_get_n_deallocations(pFailure6)); - - CU_clear_previous_results(); - TEST(0 != test_cunit_get_n_memevents(pFailure4)); - TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); - TEST(0 != test_cunit_get_n_memevents(pFailure5)); - TEST(test_cunit_get_n_allocations(pFailure5) == test_cunit_get_n_deallocations(pFailure5)); - TEST(0 != test_cunit_get_n_memevents(pFailure6)); - TEST(test_cunit_get_n_allocations(pFailure6) == test_cunit_get_n_deallocations(pFailure6)); - TEST(0 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_successes()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - f_pCurTest = NULL; - f_pCurSuite = NULL; -} - -/*-------------------------------------------------*/ -static void test_add_failure(void) -{ - CU_Test test1; - CU_Suite suite1; - CU_pFailureRecord pFailure1 = NULL; - CU_pFailureRecord pFailure2 = NULL; - CU_pFailureRecord pFailure3 = NULL; - CU_pFailureRecord pFailure4 = NULL; - CU_RunSummary run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - /* test under memory exhaustion */ - test_cunit_deactivate_malloc(); - add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 100, "condition 0", "file0.c", &suite1, &test1); - TEST(NULL == pFailure1); - TEST(0 == run_summary.nFailureRecords); - test_cunit_activate_malloc(); - - /* normal operation */ - add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 101, "condition 1", "file1.c", &suite1, &test1); - TEST(1 == run_summary.nFailureRecords); - if (TEST(NULL != pFailure1)) { - TEST(101 == pFailure1->uiLineNumber); - TEST(!strcmp("condition 1", pFailure1->strCondition)); - TEST(!strcmp("file1.c", pFailure1->strFileName)); - TEST(&test1 == pFailure1->pTest); - TEST(&suite1 == pFailure1->pSuite); - TEST(NULL == pFailure1->pNext); - TEST(NULL == pFailure1->pPrev); - TEST(pFailure1 == f_last_failure); - TEST(0 != test_cunit_get_n_memevents(pFailure1)); - TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); - } - - add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 102, "condition 2", "file2.c", NULL, &test1); - TEST(2 == run_summary.nFailureRecords); - if (TEST(NULL != pFailure1)) { - TEST(101 == pFailure1->uiLineNumber); - TEST(!strcmp("condition 1", pFailure1->strCondition)); - TEST(!strcmp("file1.c", pFailure1->strFileName)); - TEST(&test1 == pFailure1->pTest); - TEST(&suite1 == pFailure1->pSuite); - TEST(NULL != pFailure1->pNext); - TEST(NULL == pFailure1->pPrev); - TEST(pFailure1 != f_last_failure); - TEST(0 != test_cunit_get_n_memevents(pFailure1)); - TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); - - if (TEST(NULL != (pFailure2 = pFailure1->pNext))) { - TEST(102 == pFailure2->uiLineNumber); - TEST(!strcmp("condition 2", pFailure2->strCondition)); - TEST(!strcmp("file2.c", pFailure2->strFileName)); - TEST(&test1 == pFailure2->pTest); - TEST(NULL == pFailure2->pSuite); - TEST(NULL == pFailure2->pNext); - TEST(pFailure1 == pFailure2->pPrev); - TEST(pFailure2 == f_last_failure); - TEST(0 != test_cunit_get_n_memevents(pFailure2)); - TEST(test_cunit_get_n_allocations(pFailure2) != test_cunit_get_n_deallocations(pFailure2)); - } - } - - pFailure3 = pFailure1; - pFailure4 = pFailure2; - clear_previous_results(&run_summary, &pFailure1); - - TEST(0 == run_summary.nFailureRecords); - TEST(0 != test_cunit_get_n_memevents(pFailure3)); - TEST(test_cunit_get_n_allocations(pFailure3) == test_cunit_get_n_deallocations(pFailure3)); - TEST(0 != test_cunit_get_n_memevents(pFailure4)); - TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); -} - -/*-------------------------------------------------*/ -void test_cunit_TestRun(void) -{ - test_cunit_start_tests("TestRun.c"); - - test_message_handlers(); - test_CU_fail_on_inactive(); - test_CU_run_all_tests(); - test_CU_run_suite(); - test_CU_run_test(); - test_CU_assertImplementation(); - test_add_failure(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestRun.cpp b/gearsyncd/tests/cunit/TestRun.cpp deleted file mode 100644 index ac6a735b01..0000000000 --- a/gearsyncd/tests/cunit/TestRun.cpp +++ /dev/null @@ -1,2913 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Implementation of Test Run Interface. - * - * Aug 2001 Initial implementaion (AK) - * - * 19/Aug/2001 Added initial registry/Suite/test framework implementation. (AK) - * - * 24/Aug/2001 Changed Data structure from SLL to DLL for all linked lists. (AK) - * - * 25/Nov/2001 Added notification for Suite Initialization failure condition. (AK) - * - * 5-Aug-2004 New interface, doxygen comments, moved add_failure on suite - * initialization so called even if a callback is not registered, - * moved CU_assertImplementation into TestRun.c, consolidated - * all run summary info out of CU_TestRegistry into TestRun.c, - * revised counting and reporting of run stats to cleanly - * differentiate suite, test, and assertion failures. (JDS) - * - * 1-Sep-2004 Modified CU_assertImplementation() and run_single_test() for - * setjmp/longjmp mechanism of aborting test runs, add asserts in - * CU_assertImplementation() to trap use outside a registered - * test function during an active test run. (JDS) - * - * 22-Sep-2004 Initial implementation of internal unit tests, added nFailureRecords - * to CU_Run_Summary, added CU_get_n_failure_records(), removed - * requirement for registry to be initialized in order to run - * CU_run_suite() and CU_run_test(). (JDS) - * - * 30-Apr-2005 Added callback for suite cleanup function failure, - * updated unit tests. (JDS) - * - * 23-Apr-2006 Added testing for suite/test deactivation, changing functions. - * Moved doxygen comments for public functions into header. - * Added type marker to CU_FailureRecord. - * Added support for tracking inactive suites/tests. (JDS) - * - * 02-May-2006 Added internationalization hooks. (JDS) - * - * 02-Jun-2006 Added support for elapsed time. Added handlers for suite - * start and complete events. Reworked test run routines to - * better support these features, suite/test activation. (JDS) - */ - -/** @file - * Test run management functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "MyMem.h" -#include "TestDB.h" -#include "TestRun.h" -#include "Util.h" -#include "CUnit_intl.h" - -#define MAX_CMD_LEN 100 - -//Extern declaration -extern int collect_syslog; - -//Callback fn declaration -typedef void (*cunit_logger_cb)(void *fn); - -/*================================================================= - * Global/Static Definitions - *=================================================================*/ -static CU_BOOL f_bTestIsRunning = CU_FALSE; /**< Flag for whether a test run is in progress */ -static CU_pSuite f_pCurSuite = NULL; /**< Pointer to the suite currently being run. */ -static CU_pTest f_pCurTest = NULL; /**< Pointer to the test currently being run. */ - -/** CU_RunSummary to hold results of each test run. */ -static CU_RunSummary f_run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - -/** CU_pFailureRecord to hold head of failure record list of each test run. */ -static CU_pFailureRecord f_failure_list = NULL; - -/** CU_pFailureRecord to hold head of failure record list of each test run. */ -static CU_pFailureRecord f_last_failure = NULL; - -/** Flag for whether inactive suites/tests are treated as failures. */ -static CU_BOOL f_failure_on_inactive = CU_TRUE; - -/** Variable for storage of start time for test run. */ -static clock_t f_start_time; - - -/** Pointer to the function to be called before running a suite. */ -static CU_SuiteStartMessageHandler f_pSuiteStartMessageHandler = NULL; - -/** Pointer to the function to be called before running a test. */ -static CU_TestStartMessageHandler f_pTestStartMessageHandler = NULL; - -/** Pointer to the function to be called after running a test. */ -static CU_TestCompleteMessageHandler f_pTestCompleteMessageHandler = NULL; - -/** Pointer to the function to be called after running a suite. */ -static CU_SuiteCompleteMessageHandler f_pSuiteCompleteMessageHandler = NULL; - -/** Pointer to the function to be called when all tests have been run. */ -static CU_AllTestsCompleteMessageHandler f_pAllTestsCompleteMessageHandler = NULL; - -/** Pointer to the function to be called if a suite initialization function returns an error. */ -static CU_SuiteInitFailureMessageHandler f_pSuiteInitFailureMessageHandler = NULL; - -/** Pointer to the function to be called if a suite cleanup function returns an error. */ -static CU_SuiteCleanupFailureMessageHandler f_pSuiteCleanupFailureMessageHandler = NULL; - -/*================================================================= - * Private function forward declarations - *=================================================================*/ -static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure); -static void cleanup_failure_list(CU_pFailureRecord* ppFailure); -static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary); -static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary); -static void add_failure(CU_pFailureRecord* ppFailure, - CU_pRunSummary pRunSummary, - CU_FailureType type, - unsigned int uiLineNumber, - const char *szCondition, - const char *szFileName, - CU_pSuite pSuite, - CU_pTest pTest); - -/*================================================================= - * Public Interface functions - *=================================================================*/ -CU_BOOL CU_assertImplementation(CU_BOOL bValue, - unsigned int uiLine, - const char *strCondition, - const char *strFile, - const char *strFunction, - CU_BOOL bFatal) -{ - /* not used in current implementation - stop compiler warning */ - CU_UNREFERENCED_PARAMETER(strFunction); - - /* these should always be non-NULL (i.e. a test run is in progress) */ - assert(NULL != f_pCurSuite); - assert(NULL != f_pCurTest); - - ++f_run_summary.nAsserts; - if (CU_FALSE == bValue) { - ++f_run_summary.nAssertsFailed; - add_failure(&f_failure_list, &f_run_summary, CUF_AssertFailed, - uiLine, strCondition, strFile, f_pCurSuite, f_pCurTest); - - if ((CU_TRUE == bFatal) && (NULL != f_pCurTest->pJumpBuf)) { - longjmp(*(f_pCurTest->pJumpBuf), 1); - } - } - - return bValue; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartHandler) -{ - f_pSuiteStartMessageHandler = pSuiteStartHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartHandler) -{ - f_pTestStartMessageHandler = pTestStartHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteHandler) -{ - f_pTestCompleteMessageHandler = pTestCompleteHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteHandler) -{ - f_pSuiteCompleteMessageHandler = pSuiteCompleteHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteHandler) -{ - f_pAllTestsCompleteMessageHandler = pAllTestsCompleteHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureHandler) -{ - f_pSuiteInitFailureMessageHandler = pSuiteInitFailureHandler; -} - -/*------------------------------------------------------------------------*/ -void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureHandler) -{ - f_pSuiteCleanupFailureMessageHandler = pSuiteCleanupFailureHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteStartMessageHandler CU_get_suite_start_handler(void) -{ - return f_pSuiteStartMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_TestStartMessageHandler CU_get_test_start_handler(void) -{ - return f_pTestStartMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_TestCompleteMessageHandler CU_get_test_complete_handler(void) -{ - return f_pTestCompleteMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteCompleteMessageHandler CU_get_suite_complete_handler(void) -{ - return f_pSuiteCompleteMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_AllTestsCompleteMessageHandler CU_get_all_test_complete_handler(void) -{ - return f_pAllTestsCompleteMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteInitFailureMessageHandler CU_get_suite_init_failure_handler(void) -{ - return f_pSuiteInitFailureMessageHandler; -} - -/*------------------------------------------------------------------------*/ -CU_SuiteCleanupFailureMessageHandler CU_get_suite_cleanup_failure_handler(void) -{ - return f_pSuiteCleanupFailureMessageHandler; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_suites_run(void) -{ - return f_run_summary.nSuitesRun; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_suites_failed(void) -{ - return f_run_summary.nSuitesFailed; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_suites_inactive(void) -{ - return f_run_summary.nSuitesInactive; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_tests_run(void) -{ - return f_run_summary.nTestsRun; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_tests_failed(void) -{ - return f_run_summary.nTestsFailed; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_tests_inactive(void) -{ - return f_run_summary.nTestsInactive; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_asserts(void) -{ - return f_run_summary.nAsserts; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_successes(void) -{ - return (f_run_summary.nAsserts - f_run_summary.nAssertsFailed); -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_failures(void) -{ - return f_run_summary.nAssertsFailed; -} - -/*------------------------------------------------------------------------*/ -unsigned int CU_get_number_of_failure_records(void) -{ - return f_run_summary.nFailureRecords; -} - -/*------------------------------------------------------------------------*/ -double CU_get_elapsed_time(void) -{ - if (CU_TRUE == f_bTestIsRunning) { - return ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - } - else { - return f_run_summary.ElapsedTime; - } -} - -/*------------------------------------------------------------------------*/ -CU_pFailureRecord CU_get_failure_list(void) -{ - return f_failure_list; -} - -/*------------------------------------------------------------------------*/ -CU_pRunSummary CU_get_run_summary(void) -{ - return &f_run_summary; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_run_all_tests(void) -{ - CU_pTestRegistry pRegistry = CU_get_registry(); - CU_pSuite pSuite = NULL; - CU_ErrorCode result = CUE_SUCCESS; - CU_ErrorCode result2; - - /* Clear results from the previous run */ - clear_previous_results(&f_run_summary, &f_failure_list); - - if (NULL == pRegistry) { - result = CUE_NOREGISTRY; - } - else { - /* test run is starting - set flag */ - f_bTestIsRunning = CU_TRUE; - f_start_time = clock(); - - pSuite = pRegistry->pSuite; - while ((NULL != pSuite) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { - result2 = run_single_suite(pSuite, &f_run_summary); - result = (CUE_SUCCESS == result) ? result2 : result; /* result = 1st error encountered */ - pSuite = pSuite->pNext; - } - - /* test run is complete - clear flag */ - f_bTestIsRunning = CU_FALSE; - f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - - if (NULL != f_pAllTestsCompleteMessageHandler) { - (*f_pAllTestsCompleteMessageHandler)(f_failure_list); - } - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_run_suite(CU_pSuite pSuite) -{ - CU_ErrorCode result = CUE_SUCCESS; - - /* Clear results from the previous run */ - clear_previous_results(&f_run_summary, &f_failure_list); - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else { - /* test run is starting - set flag */ - f_bTestIsRunning = CU_TRUE; - f_start_time = clock(); - - result = run_single_suite(pSuite, &f_run_summary); - - /* test run is complete - clear flag */ - f_bTestIsRunning = CU_FALSE; - f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - - /* run handler for overall completion, if any */ - if (NULL != f_pAllTestsCompleteMessageHandler) { - (*f_pAllTestsCompleteMessageHandler)(f_failure_list); - } - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest) -{ - CU_ErrorCode result = CUE_SUCCESS; - CU_ErrorCode result2; - - /* Clear results from the previous run */ - clear_previous_results(&f_run_summary, &f_failure_list); - - if (NULL == pSuite) { - result = CUE_NOSUITE; - } - else if (NULL == pTest) { - result = CUE_NOTEST; - } - else if (CU_FALSE == pSuite->fActive) { - f_run_summary.nSuitesInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, - 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); - } - result = CUE_SUITE_INACTIVE; - } - else if ((NULL == pTest->pName) || (NULL == CU_get_test_by_name(pTest->pName, pSuite))) { - result = CUE_TEST_NOT_IN_SUITE; - } - else { - /* test run is starting - set flag */ - f_bTestIsRunning = CU_TRUE; - f_start_time = clock(); - - f_pCurTest = NULL; - f_pCurSuite = pSuite; - - /* run handler for suite start, if any */ - if (NULL != f_pSuiteStartMessageHandler) { - (*f_pSuiteStartMessageHandler)(pSuite); - } - - /* run the suite initialization function, if any */ - if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { - /* init function had an error - call handler, if any */ - if (NULL != f_pSuiteInitFailureMessageHandler) { - (*f_pSuiteInitFailureMessageHandler)(pSuite); - } - f_run_summary.nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, - _("Suite Initialization failed - Suite Skipped"), - _("CUnit System"), pSuite, NULL); - result = CUE_SINIT_FAILED; - } - /* reach here if no suite initialization, or if it succeeded */ - else { - result2 = run_single_test(pTest, &f_run_summary); - result = (CUE_SUCCESS == result) ? result2 : result; - - /* run the suite cleanup function, if any */ - if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { - /* cleanup function had an error - call handler, if any */ - if (NULL != f_pSuiteCleanupFailureMessageHandler) { - (*f_pSuiteCleanupFailureMessageHandler)(pSuite); - } - f_run_summary.nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, - 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); - result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; - } - } - - /* run handler for suite completion, if any */ - if (NULL != f_pSuiteCompleteMessageHandler) { - (*f_pSuiteCompleteMessageHandler)(pSuite, NULL); - } - - /* test run is complete - clear flag */ - f_bTestIsRunning = CU_FALSE; - f_run_summary.ElapsedTime = ((double)clock() - (double)f_start_time)/(double)CLOCKS_PER_SEC; - - /* run handler for overall completion, if any */ - if (NULL != f_pAllTestsCompleteMessageHandler) { - (*f_pAllTestsCompleteMessageHandler)(f_failure_list); - } - - f_pCurSuite = NULL; - } - - CU_set_error(result); - return result; -} - -/*------------------------------------------------------------------------*/ -void CU_clear_previous_results(void) -{ - clear_previous_results(&f_run_summary, &f_failure_list); -} - -/*------------------------------------------------------------------------*/ -CU_pSuite CU_get_current_suite(void) -{ - return f_pCurSuite; -} - -/*------------------------------------------------------------------------*/ -CU_pTest CU_get_current_test(void) -{ - return f_pCurTest; -} - -/*------------------------------------------------------------------------*/ -CU_BOOL CU_is_test_running(void) -{ - return f_bTestIsRunning; -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive) -{ - f_failure_on_inactive = new_inactive; -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void) -{ - return f_failure_on_inactive; -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT void CU_print_run_results(FILE *file) -{ - char *summary_string; - - assert(NULL != file); - summary_string = CU_get_run_results_string(); - if (NULL != summary_string) { - fprintf(file, "%s", summary_string); - CU_FREE(summary_string); - } - else { - fprintf(file, _("An error occurred printing the run results.")); - } -} - -/*------------------------------------------------------------------------*/ -CU_EXPORT char * CU_get_run_results_string(void) - -{ - CU_pRunSummary pRunSummary = &f_run_summary; - CU_pTestRegistry pRegistry = CU_get_registry(); - int width[9]; - size_t len; - char *result; - - assert(NULL != pRunSummary); - assert(NULL != pRegistry); - - width[0] = strlen(_("Run Summary:")); - width[1] = CU_MAX(6, - CU_MAX(strlen(_("Type")), - CU_MAX(strlen(_("suites")), - CU_MAX(strlen(_("tests")), - strlen(_("asserts")))))) + 1; - width[2] = CU_MAX(6, - CU_MAX(strlen(_("Total")), - CU_MAX(CU_number_width(pRegistry->uiNumberOfSuites), - CU_MAX(CU_number_width(pRegistry->uiNumberOfTests), - CU_number_width(pRunSummary->nAsserts))))) + 1; - width[3] = CU_MAX(6, - CU_MAX(strlen(_("Ran")), - CU_MAX(CU_number_width(pRunSummary->nSuitesRun), - CU_MAX(CU_number_width(pRunSummary->nTestsRun), - CU_number_width(pRunSummary->nAsserts))))) + 1; - width[4] = CU_MAX(6, - CU_MAX(strlen(_("Passed")), - CU_MAX(strlen(_("n/a")), - CU_MAX(CU_number_width(pRunSummary->nTestsRun - pRunSummary->nTestsFailed), - CU_number_width(pRunSummary->nAsserts - pRunSummary->nAssertsFailed))))) + 1; - width[5] = CU_MAX(6, - CU_MAX(strlen(_("Failed")), - CU_MAX(CU_number_width(pRunSummary->nSuitesFailed), - CU_MAX(CU_number_width(pRunSummary->nTestsFailed), - CU_number_width(pRunSummary->nAssertsFailed))))) + 1; - width[6] = CU_MAX(6, - CU_MAX(strlen(_("Inactive")), - CU_MAX(CU_number_width(pRunSummary->nSuitesInactive), - CU_MAX(CU_number_width(pRunSummary->nTestsInactive), - strlen(_("n/a")))))) + 1; - - width[7] = strlen(_("Elapsed time = ")); - width[8] = strlen(_(" seconds")); - - len = 13 + 4*(width[0] + width[1] + width[2] + width[3] + width[4] + width[5] + width[6]) + width[7] + width[8] + 1; - result = (char *)CU_MALLOC(len); - - if (NULL != result) { - snprintf(result, len, "%*s%*s%*s%*s%*s%*s%*s\n" /* if you change this, be sure */ - "%*s%*s%*u%*u%*s%*u%*u\n" /* to change the calculation of */ - "%*s%*s%*u%*u%*u%*u%*u\n" /* len above! */ - "%*s%*s%*u%*u%*u%*u%*s\n\n" - "%*s%8.3f%*s", - width[0], _("Run Summary:"), - width[1], _("Type"), - width[2], _("Total"), - width[3], _("Ran"), - width[4], _("Passed"), - width[5], _("Failed"), - width[6], _("Inactive"), - width[0], " ", - width[1], _("suites"), - width[2], pRegistry->uiNumberOfSuites, - width[3], pRunSummary->nSuitesRun, - width[4], _("n/a"), - width[5], pRunSummary->nSuitesFailed, - width[6], pRunSummary->nSuitesInactive, - width[0], " ", - width[1], _("tests"), - width[2], pRegistry->uiNumberOfTests, - width[3], pRunSummary->nTestsRun, - width[4], pRunSummary->nTestsRun - pRunSummary->nTestsFailed, - width[5], pRunSummary->nTestsFailed, - width[6], pRunSummary->nTestsInactive, - width[0], " ", - width[1], _("asserts"), - width[2], pRunSummary->nAsserts, - width[3], pRunSummary->nAsserts, - width[4], pRunSummary->nAsserts - pRunSummary->nAssertsFailed, - width[5], pRunSummary->nAssertsFailed, - width[6], _("n/a"), - width[7], _("Elapsed time = "), CU_get_elapsed_time(), /* makes sure time is updated */ - width[8], _(" seconds") - ); - result[len-1] = '\0'; - } - return result; -} - -/*================================================================= - * Static Function Definitions - *=================================================================*/ -/** - * Records a runtime failure. - * This function is called whenever a runtime failure occurs. - * This includes user assertion failures, suite initialization and - * cleanup failures, and inactive suites/tests when set as failures. - * This function records the details of the failure in a new - * failure record in the linked list of runtime failures. - * - * @param ppFailure Pointer to head of linked list of failure - * records to append with new failure record. - * If it points to a NULL pointer, it will be set - * to point to the new failure record. - * @param pRunSummary Pointer to CU_RunSummary keeping track of failure records - * (ignored if NULL). - * @param type Type of failure. - * @param uiLineNumber Line number of the failure, if applicable. - * @param szCondition Description of failure condition - * @param szFileName Name of file, if applicable - * @param pSuite The suite being run at time of failure - * @param pTest The test being run at time of failure - */ -static void add_failure(CU_pFailureRecord* ppFailure, - CU_pRunSummary pRunSummary, - CU_FailureType type, - unsigned int uiLineNumber, - const char *szCondition, - const char *szFileName, - CU_pSuite pSuite, - CU_pTest pTest) -{ - CU_pFailureRecord pFailureNew = NULL; - CU_pFailureRecord pTemp = NULL; - - assert(NULL != ppFailure); - - pFailureNew = (CU_pFailureRecord)CU_MALLOC(sizeof(CU_FailureRecord)); - - if (NULL == pFailureNew) { - return; - } - - pFailureNew->strFileName = NULL; - pFailureNew->strCondition = NULL; - if (NULL != szFileName) { - pFailureNew->strFileName = (char*)CU_MALLOC(strlen(szFileName) + 1); - if(NULL == pFailureNew->strFileName) { - CU_FREE(pFailureNew); - return; - } - strcpy(pFailureNew->strFileName, szFileName); - } - - if (NULL != szCondition) { - pFailureNew->strCondition = (char*)CU_MALLOC(strlen(szCondition) + 1); - if (NULL == pFailureNew->strCondition) { - if(NULL != pFailureNew->strFileName) { - CU_FREE(pFailureNew->strFileName); - } - CU_FREE(pFailureNew); - return; - } - strcpy(pFailureNew->strCondition, szCondition); - } - - pFailureNew->type = type; - pFailureNew->uiLineNumber = uiLineNumber; - pFailureNew->pTest = pTest; - pFailureNew->pSuite = pSuite; - pFailureNew->pNext = NULL; - pFailureNew->pPrev = NULL; - - pTemp = *ppFailure; - if (NULL != pTemp) { - while (NULL != pTemp->pNext) { - pTemp = pTemp->pNext; - } - pTemp->pNext = pFailureNew; - pFailureNew->pPrev = pTemp; - } - else { - *ppFailure = pFailureNew; - } - - if (NULL != pRunSummary) { - ++(pRunSummary->nFailureRecords); - } - f_last_failure = pFailureNew; -} - -/* - * Local function for result set initialization/cleanup. - */ -/*------------------------------------------------------------------------*/ -/** - * Initializes the run summary information in the specified structure. - * Resets the run counts to zero, and calls cleanup_failure_list() if - * failures were recorded by the last test run. Calling this function - * multiple times, while inefficient, will not cause an error condition. - * - * @param pRunSummary CU_RunSummary to initialize (non-NULL). - * @param ppFailure The failure record to clean (non-NULL). - * @see CU_clear_previous_results() - */ -static void clear_previous_results(CU_pRunSummary pRunSummary, CU_pFailureRecord* ppFailure) -{ - assert(NULL != pRunSummary); - assert(NULL != ppFailure); - - pRunSummary->nSuitesRun = 0; - pRunSummary->nSuitesFailed = 0; - pRunSummary->nSuitesInactive = 0; - pRunSummary->nTestsRun = 0; - pRunSummary->nTestsFailed = 0; - pRunSummary->nTestsInactive = 0; - pRunSummary->nAsserts = 0; - pRunSummary->nAssertsFailed = 0; - pRunSummary->nFailureRecords = 0; - pRunSummary->ElapsedTime = 0.0; - - if (NULL != *ppFailure) { - cleanup_failure_list(ppFailure); - } - - f_last_failure = NULL; -} - -/*------------------------------------------------------------------------*/ -/** - * Frees all memory allocated for the linked list of test failure - * records. pFailure is reset to NULL after its list is cleaned up. - * - * @param ppFailure Pointer to head of linked list of - * CU_pFailureRecords to clean. - * @see CU_clear_previous_results() - */ -static void cleanup_failure_list(CU_pFailureRecord* ppFailure) -{ - CU_pFailureRecord pCurFailure = NULL; - CU_pFailureRecord pNextFailure = NULL; - - pCurFailure = *ppFailure; - - while (NULL != pCurFailure) { - - if (NULL != pCurFailure->strCondition) { - CU_FREE(pCurFailure->strCondition); - } - - if (NULL != pCurFailure->strFileName) { - CU_FREE(pCurFailure->strFileName); - } - - pNextFailure = pCurFailure->pNext; - CU_FREE(pCurFailure); - pCurFailure = pNextFailure; - } - - *ppFailure = NULL; -} - - -/* Function to collect syslog for a test */ -//FIXME find another bettwer way to do this, It is currentlly leaving tail procs in ps -void cunit_logger_fn(void *fn){ - char sys_cmd[MAX_CMD_LEN] = {'\0'}; - sprintf(sys_cmd, "tail -f /var/log/syslog > %s.txt", (char *)fn); - system("service syslog restart"); - system(sys_cmd); -} - -/*------------------------------------------------------------------------*/ -/** - * Runs all tests in a specified suite. - * Internal function to run all tests in a suite. The suite need - * not be registered in the test registry to be run. Only - * suites having their fActive flags set CU_TRUE will actually be - * run. If the CUnit framework is in an error condition after - * running a test, no additional tests are run. - * - * @param pSuite The suite containing the test (non-NULL). - * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). - * @return A CU_ErrorCode indicating the status of the run. - * @see CU_run_suite() for public interface function. - * @see CU_run_all_tests() for running all suites. - */ -static CU_ErrorCode run_single_suite(CU_pSuite pSuite, CU_pRunSummary pRunSummary) -{ - CU_pTest pTest = NULL; - unsigned int nStartFailures; - /* keep track of the last failure BEFORE running the test */ - CU_pFailureRecord pLastFailure = f_last_failure; - CU_ErrorCode result = CUE_SUCCESS; - CU_ErrorCode result2; - - assert(NULL != pSuite); - assert(NULL != pRunSummary); - - nStartFailures = pRunSummary->nFailureRecords; - - f_pCurTest = NULL; - f_pCurSuite = pSuite; - - /* run handler for suite start, if any */ - if (NULL != f_pSuiteStartMessageHandler) { - (*f_pSuiteStartMessageHandler)(pSuite); - } - - /* run suite if it's active */ - if (CU_FALSE != pSuite->fActive) { - - /* run the suite initialization function, if any */ - if ((NULL != pSuite->pInitializeFunc) && (0 != (*pSuite->pInitializeFunc)())) { - /* init function had an error - call handler, if any */ - if (NULL != f_pSuiteInitFailureMessageHandler) { - (*f_pSuiteInitFailureMessageHandler)(pSuite); - } - pRunSummary->nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInitFailed, 0, - _("Suite Initialization failed - Suite Skipped"), - _("CUnit System"), pSuite, NULL); - result = CUE_SINIT_FAILED; - } - - /* reach here if no suite initialization, or if it succeeded */ - else { - pTest = pSuite->pTest; - while ((NULL != pTest) && ((CUE_SUCCESS == result) || (CU_get_error_action() == CUEA_IGNORE))) { - if (CU_FALSE != pTest->fActive) { - result2 = run_single_test(pTest, pRunSummary); - result = (CUE_SUCCESS == result) ? result2 : result; - } - else { - f_run_summary.nTestsInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, - 0, _("Test inactive"), _("CUnit System"), pSuite, pTest); - result = CUE_TEST_INACTIVE; - } - } - pTest = pTest->pNext; - } - pRunSummary->nSuitesRun++; - - /* call the suite cleanup function, if any */ - if ((NULL != pSuite->pCleanupFunc) && (0 != (*pSuite->pCleanupFunc)())) { - if (NULL != f_pSuiteCleanupFailureMessageHandler) { - (*f_pSuiteCleanupFailureMessageHandler)(pSuite); - } - pRunSummary->nSuitesFailed++; - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteCleanupFailed, - 0, _("Suite cleanup failed."), _("CUnit System"), pSuite, NULL); - result = (CUE_SUCCESS == result) ? CUE_SCLEAN_FAILED : result; - } - } - } - - /* otherwise record inactive suite and failure if appropriate */ - else { - f_run_summary.nSuitesInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_SuiteInactive, - 0, _("Suite inactive"), _("CUnit System"), pSuite, NULL); - result = CUE_SUITE_INACTIVE; - } - } - - /* if additional failures have occurred... */ - if (pRunSummary->nFailureRecords > nStartFailures) { - if (NULL != pLastFailure) { - pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ - } - else { - pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ - } - } - else { - pLastFailure = NULL; /* no additional failure - set to NULL */ - } - - /* run handler for suite completion, if any */ - if (NULL != f_pSuiteCompleteMessageHandler) { - (*f_pSuiteCompleteMessageHandler)(pSuite, pLastFailure); - } - - f_pCurSuite = NULL; - return result; -} - -/*------------------------------------------------------------------------*/ -/** - * Runs a specific test. - * Internal function to run a test case. This includes calling - * any handler to be run before executing the test, running the - * test's function (if any), and calling any handler to be run - * after executing a test. Suite initialization and cleanup functions - * are not called by this function. A current suite must be set and - * active (checked by assertion). - * - * @param pTest The test to be run (non-NULL). - * @param pRunSummary The CU_RunSummary to receive the results (non-NULL). - * @return A CU_ErrorCode indicating the status of the run. - * @see CU_run_test() for public interface function. - * @see CU_run_all_tests() for running all suites. - */ -static CU_ErrorCode run_single_test(CU_pTest pTest, CU_pRunSummary pRunSummary) -{ - volatile unsigned int nStartFailures; - pthread_t tid; - int rv = 0; - cunit_logger_cb cb = cunit_logger_fn; - /* keep track of the last failure BEFORE running the test */ - volatile CU_pFailureRecord pLastFailure = f_last_failure; - jmp_buf buf; - CU_ErrorCode result = CUE_SUCCESS; - - assert(NULL != f_pCurSuite); - assert(CU_FALSE != f_pCurSuite->fActive); - assert(NULL != pTest); - assert(NULL != pRunSummary); - - nStartFailures = pRunSummary->nFailureRecords; - - f_pCurTest = pTest; - - if (NULL != f_pTestStartMessageHandler) { - (*f_pTestStartMessageHandler)(f_pCurTest, f_pCurSuite); - } - - /* run test if it is active */ - if (CU_FALSE != pTest->fActive) { - - /* set jmp_buf and run test */ - pTest->pJumpBuf = &buf; - if (0 == setjmp(buf)) { - if (NULL != pTest->pTestFunc) { - if (collect_syslog) {//FIXME Currently WIP, It is not reliable for huge batch runs now - rv = pthread_create(&tid, NULL, (void *)cb, (void *)pTest->pName); - if (rv) { - fprintf(stdout, "thread create failed for collecting syslog for %s\n",(char *)pTest->pName); - } - } - (*pTest->pTestFunc)(); - if (collect_syslog){ - sleep(1); - pthread_cancel(tid); - } - } - } - - pRunSummary->nTestsRun++; - } - else { - f_run_summary.nTestsInactive++; - if (CU_FALSE != f_failure_on_inactive) { - add_failure(&f_failure_list, &f_run_summary, CUF_TestInactive, - 0, _("Test inactive"), _("CUnit System"), f_pCurSuite, f_pCurTest); - } - result = CUE_TEST_INACTIVE; - } - - /* if additional failures have occurred... */ - if (pRunSummary->nFailureRecords > nStartFailures) { - pRunSummary->nTestsFailed++; - if (NULL != pLastFailure) { - pLastFailure = pLastFailure->pNext; /* was a previous failure, so go to next one */ - } - else { - pLastFailure = f_failure_list; /* no previous failure - go to 1st one */ - } - } - else { - pLastFailure = NULL; /* no additional failure - set to NULL */ - } - - if (NULL != f_pTestCompleteMessageHandler) { - (*f_pTestCompleteMessageHandler)(f_pCurTest, f_pCurSuite, pLastFailure); - } - - pTest->pJumpBuf = NULL; - f_pCurTest = NULL; - - return result; -} - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -/** Types of framework events tracked by test system. */ -typedef enum TET { - SUITE_START = 1, - TEST_START, - TEST_COMPLETE, - SUITE_COMPLETE, - ALL_TESTS_COMPLETE, - SUITE_INIT_FAILED, - SUITE_CLEANUP_FAILED -} TestEventType; - -/** Test event structure for recording details of a framework event. */ -typedef struct TE { - TestEventType type; - CU_pSuite pSuite; - CU_pTest pTest; - CU_pFailureRecord pFailure; - struct TE * pNext; -} TestEvent, * pTestEvent; - -static int f_nTestEvents = 0; -static pTestEvent f_pFirstEvent = NULL; - -/** Creates & stores a test event record having the specified details. */ -static void add_test_event(TestEventType type, CU_pSuite psuite, - CU_pTest ptest, CU_pFailureRecord pfailure) -{ - pTestEvent pNewEvent = (pTestEvent)malloc(sizeof(TestEvent)); - pTestEvent pNextEvent = f_pFirstEvent; - - if (NULL == pNewEvent) { - fprintf(stderr, "Memory allocation failed in add_test_event()."); - exit(1); - } - - pNewEvent->type = type; - pNewEvent->pSuite = psuite; - pNewEvent->pTest = ptest; - pNewEvent->pFailure = pfailure; - pNewEvent->pNext = NULL; - - if (pNextEvent) { - while (pNextEvent->pNext) { - pNextEvent = pNextEvent->pNext; - } - pNextEvent->pNext = pNewEvent; - } - else { - f_pFirstEvent = pNewEvent; - } - ++f_nTestEvents; -} - -/** Deallocates all test event data. */ -static void clear_test_events(void) -{ - pTestEvent pCurrentEvent = f_pFirstEvent; - pTestEvent pNextEvent = NULL; - - while (pCurrentEvent) { - pNextEvent = pCurrentEvent->pNext; - free(pCurrentEvent); - pCurrentEvent = pNextEvent; - } - - f_pFirstEvent = NULL; - f_nTestEvents = 0; -} - -static void suite_start_handler(const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(NULL == CU_get_current_test()); - - add_test_event(SUITE_START, pSuite, NULL, NULL); -} - -static void test_start_handler(const CU_pTest pTest, const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(pTest == CU_get_current_test()); - - add_test_event(TEST_START, pSuite, pTest, NULL); -} - -static void test_complete_handler(const CU_pTest pTest, const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(pTest == CU_get_current_test()); - - add_test_event(TEST_COMPLETE, pSuite, pTest, pFailure); -} - -static void suite_complete_handler(const CU_pSuite pSuite, - const CU_pFailureRecord pFailure) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - TEST(NULL == CU_get_current_test()); - - add_test_event(SUITE_COMPLETE, pSuite, NULL, pFailure); -} - -static void test_all_complete_handler(const CU_pFailureRecord pFailure) -{ - TEST(!CU_is_test_running()); - - add_test_event(ALL_TESTS_COMPLETE, NULL, NULL, pFailure); -} - -static void suite_init_failure_handler(const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - - add_test_event(SUITE_INIT_FAILED, pSuite, NULL, NULL); -} - -static void suite_cleanup_failure_handler(const CU_pSuite pSuite) -{ - TEST(CU_is_test_running()); - TEST(pSuite == CU_get_current_suite()); - - add_test_event(SUITE_CLEANUP_FAILED, pSuite, NULL, NULL); -} - -/** - * Centralize test result testing - we're going to do it a lot! - * This is messy since we want to report the calling location upon failure. - * - * Via calling test functions tests: - * CU_get_number_of_suites_run() - * CU_get_number_of_suites_failed() - * CU_get_number_of_tests_run() - * CU_get_number_of_tests_failed() - * CU_get_number_of_asserts() - * CU_get_number_of_successes() - * CU_get_number_of_failures() - * CU_get_number_of_failure_records() - * CU_get_run_summary() - */ -static void do_test_results(unsigned int nSuitesRun, - unsigned int nSuitesFailed, - unsigned int nSuitesInactive, - unsigned int nTestsRun, - unsigned int nTestsFailed, - unsigned int nTestsInactive, - unsigned int nAsserts, - unsigned int nSuccesses, - unsigned int nFailures, - unsigned int nFailureRecords, - const char *file, - unsigned int line) -{ - char msg[500]; - CU_pRunSummary pRunSummary = NULL; - - if (nSuitesRun == CU_get_number_of_suites_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", - nSuitesRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nSuitesInactive == CU_get_number_of_suites_inactive()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_inactive() (called from %s:%u)", - nSuitesInactive, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nSuitesFailed == CU_get_number_of_suites_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", - nSuitesFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nTestsRun == CU_get_number_of_tests_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", - nTestsRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nTestsFailed == CU_get_number_of_tests_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", - nTestsFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nTestsInactive == CU_get_number_of_tests_inactive()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_inactive() (called from %s:%u)", - nTestsInactive, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nAsserts == CU_get_number_of_asserts()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", - nAsserts, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nSuccesses == CU_get_number_of_successes()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_successes() (called from %s:%u)", - nSuccesses, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nFailures == CU_get_number_of_failures()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", - nFailures, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (nFailureRecords == CU_get_number_of_failure_records()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", - nFailureRecords, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - pRunSummary = CU_get_run_summary(); - - if (pRunSummary->nSuitesRun == CU_get_number_of_suites_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_run() (called from %s:%u)", - pRunSummary->nSuitesRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nSuitesFailed == CU_get_number_of_suites_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_suites_failed() (called from %s:%u)", - pRunSummary->nSuitesFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nTestsRun == CU_get_number_of_tests_run()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_run() (called from %s:%u)", - pRunSummary->nTestsRun, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nTestsFailed == CU_get_number_of_tests_failed()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_tests_failed() (called from %s:%u)", - pRunSummary->nTestsFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nAsserts == CU_get_number_of_asserts()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_asserts() (called from %s:%u)", - pRunSummary->nAsserts, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nAssertsFailed == CU_get_number_of_failures()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failures() (called from %s:%u)", - pRunSummary->nAssertsFailed, file, line); - msg[499] = '\0'; - FAIL(msg); - } - - if (pRunSummary->nFailureRecords == CU_get_number_of_failure_records()) { - PASS(); - } else { - snprintf(msg, 499, "%u == CU_get_number_of_failure_records() (called from %s:%u)", - pRunSummary->nFailureRecords, file, line); - msg[499] = '\0'; - FAIL(msg); - } -} - -#define test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ - nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords) \ - do_test_results(nSuitesRun, nSuitesFailed, nSuitesInactive, nTestsRun, nTestsFailed, \ - nTestsInactive, nAsserts, nSuccesses, nFailures, nFailureRecords, \ - __FILE__, __LINE__) - -static void test_succeed(void) { CU_TEST(CU_TRUE); } -static void test_fail(void) { CU_TEST(CU_FALSE); } -static int suite_succeed(void) { return 0; } -static int suite_fail(void) { return 1; } - -/*-------------------------------------------------*/ -/* tests: - * CU_set_suite_start_handler() - * CU_set_test_start_handler() - * CU_set_test_complete_handler() - * CU_set_suite_complete_handler() - * CU_set_all_test_complete_handler() - * CU_set_suite_init_failure_handler() - * CU_set_suite_cleanup_failure_handler() - * CU_get_suite_start_handler() - * CU_get_test_start_handler() - * CU_get_test_complete_handler() - * CU_get_suite_complete_handler() - * CU_get_all_test_complete_handler() - * CU_get_suite_init_failure_handler() - * CU_get_suite_cleanup_failure_handler() - * CU_is_test_running() - * via handlers tests: - * CU_get_current_suite() - * CU_get_current_test() - */ -static void test_message_handlers(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - pTestEvent pEvent = NULL; - - TEST(!CU_is_test_running()); - - /* handlers should be NULL on startup */ - TEST(NULL == CU_get_suite_start_handler()); - TEST(NULL == CU_get_test_start_handler()); - TEST(NULL == CU_get_test_complete_handler()); - TEST(NULL == CU_get_suite_complete_handler()); - TEST(NULL == CU_get_all_test_complete_handler()); - TEST(NULL == CU_get_suite_init_failure_handler()); - TEST(NULL == CU_get_suite_cleanup_failure_handler()); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test3", test_succeed); - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest4 = CU_add_test(pSuite2, "test4", test_succeed); - pSuite3 = CU_add_suite("suite3", suite_succeed, suite_fail); - pTest5 = CU_add_test(pSuite3, "test5", test_fail); - - TEST_FATAL(CUE_SUCCESS == CU_get_error()); - - /* first run tests without handlers set */ - clear_test_events(); - CU_run_all_tests(); - - TEST(0 == f_nTestEvents); - TEST(NULL == f_pFirstEvent); - test_results(2,2,0,4,2,0,4,2,2,4); - - /* set handlers to local functions */ - CU_set_suite_start_handler(&suite_start_handler); - CU_set_test_start_handler(&test_start_handler); - CU_set_test_complete_handler(&test_complete_handler); - CU_set_suite_complete_handler(&suite_complete_handler); - CU_set_all_test_complete_handler(&test_all_complete_handler); - CU_set_suite_init_failure_handler(&suite_init_failure_handler); - CU_set_suite_cleanup_failure_handler(&suite_cleanup_failure_handler); - - /* confirm handlers set properly */ - TEST(suite_start_handler == CU_get_suite_start_handler()); - TEST(test_start_handler == CU_get_test_start_handler()); - TEST(test_complete_handler == CU_get_test_complete_handler()); - TEST(suite_complete_handler == CU_get_suite_complete_handler()); - TEST(test_all_complete_handler == CU_get_all_test_complete_handler()); - TEST(suite_init_failure_handler == CU_get_suite_init_failure_handler()); - TEST(suite_cleanup_failure_handler == CU_get_suite_cleanup_failure_handler()); - - /* run tests again with handlers set */ - clear_test_events(); - CU_run_all_tests(); - - TEST(17 == f_nTestEvents); - if (17 == f_nTestEvents) { - pEvent = f_pFirstEvent; - TEST(SUITE_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest1 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest1 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest2 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest2 == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest3 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(pTest3 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_COMPLETE == pEvent->type); - TEST(pSuite1 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_START == pEvent->type); - TEST(pSuite2 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_INIT_FAILED == pEvent->type); - TEST(pSuite2 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_COMPLETE == pEvent->type); - TEST(pSuite2 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_START == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_START == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(pTest5 == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(TEST_COMPLETE == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(pTest5 == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_CLEANUP_FAILED == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL == pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(SUITE_COMPLETE == pEvent->type); - TEST(pSuite3 == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - - pEvent = pEvent->pNext; - TEST(ALL_TESTS_COMPLETE == pEvent->type); - TEST(NULL == pEvent->pSuite); - TEST(NULL == pEvent->pTest); - TEST(NULL != pEvent->pFailure); - if (4 == CU_get_number_of_failure_records()) { - TEST(NULL != pEvent->pFailure->pNext); - TEST(NULL != pEvent->pFailure->pNext->pNext); - TEST(NULL != pEvent->pFailure->pNext->pNext->pNext); - TEST(NULL == pEvent->pFailure->pNext->pNext->pNext->pNext); - } - TEST(pEvent->pFailure == CU_get_failure_list()); - } - - test_results(2,2,0,4,2,0,4,2,2,4); - - /* clear handlers and run again */ - CU_set_suite_start_handler(NULL); - CU_set_test_start_handler(NULL); - CU_set_test_complete_handler(NULL); - CU_set_suite_complete_handler(NULL); - CU_set_all_test_complete_handler(NULL); - CU_set_suite_init_failure_handler(NULL); - CU_set_suite_cleanup_failure_handler(NULL); - - TEST(NULL == CU_get_suite_start_handler()); - TEST(NULL == CU_get_test_start_handler()); - TEST(NULL == CU_get_test_complete_handler()); - TEST(NULL == CU_get_suite_complete_handler()); - TEST(NULL == CU_get_all_test_complete_handler()); - TEST(NULL == CU_get_suite_init_failure_handler()); - TEST(NULL == CU_get_suite_cleanup_failure_handler()); - - clear_test_events(); - CU_run_all_tests(); - - TEST(0 == f_nTestEvents); - TEST(NULL == f_pFirstEvent); - test_results(2,2,0,4,2,0,4,2,2,4); - - CU_cleanup_registry(); - clear_test_events(); -} - -static CU_BOOL f_exit_called = CU_FALSE; - -/* intercept exit for testing of CUEA_ABORT action */ -void test_exit(int status) -{ - CU_UNREFERENCED_PARAMETER(status); /* not used */ - f_exit_called = CU_TRUE; -} - - -/*-------------------------------------------------*/ -static void test_CU_fail_on_inactive(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - - CU_set_error_action(CUEA_IGNORE); - CU_initialize_registry(); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest3 = CU_add_test(pSuite2, "test3", test_succeed); - pTest4 = CU_add_test(pSuite2, "test4", test_succeed); - - /* test initial conditions */ - TEST(CU_TRUE == CU_get_fail_on_inactive()); - TEST(CU_TRUE == pSuite1->fActive); - TEST(CU_TRUE == pSuite2->fActive); - TEST(CU_TRUE == pTest1->fActive); - TEST(CU_TRUE == pTest2->fActive); - TEST(CU_TRUE == pTest3->fActive); - TEST(CU_TRUE == pTest4->fActive); - - CU_set_fail_on_inactive(CU_TRUE); - TEST(CU_TRUE == CU_get_fail_on_inactive()); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ - test_results(1,1,0,2,1,0,2,1,1,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CU_FALSE == CU_get_fail_on_inactive()); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); - test_results(1,1,0,2,1,0,2,1,1,2); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* all suites inactive */ - test_results(0,0,2,0,0,0,0,0,0,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(0,0,2,0,0,0,0,0,0,0); - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); /* some suites inactive */ - test_results(1,0,1,2,1,0,2,1,1,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(1,0,1,2,1,0,2,1,1,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* all tests inactive */ - test_results(1,1,0,0,0,2,0,0,0,3); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); - test_results(1,1,0,0,0,2,0,0,0,1); - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some tests inactive */ - test_results(1,1,0,1,0,1,1,1,0,2); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); - test_results(1,1,0,1,0,1,1,1,0,1); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_test_active(pTest1, CU_FALSE); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); /* some suites & tests inactive */ - test_results(1,0,1,1,1,1,1,0,1,3); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(1,0,1,1,1,1,1,0,1,1); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_test_active(pTest1, CU_TRUE); - - /* clean up */ - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -static void test_CU_run_all_tests(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - CU_pTest pTest10 = NULL; - - /* error - uninitialized registry (CUEA_IGNORE) */ - CU_cleanup_registry(); - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOREGISTRY == CU_run_all_tests()); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* error - uninitialized registry (CUEA_FAIL) */ - CU_cleanup_registry(); - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOREGISTRY == CU_run_all_tests()); - TEST(CUE_NOREGISTRY == CU_get_error()); - - /* error - uninitialized registry (CUEA_ABORT) */ - CU_cleanup_registry(); - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_all_tests(); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* run with no suites or tests registered */ - CU_initialize_registry(); - - CU_set_error_action(CUEA_IGNORE); - TEST(CUE_SUCCESS == CU_run_all_tests()); - test_results(0,0,0,0,0,0,0,0,0,0); - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test1", test_succeed); /* duplicate test name OK */ - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite1", NULL, NULL); /* duplicate suite name OK */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test9", test_succeed); - pSuite4 = CU_add_suite("suite4", NULL, suite_fail); - pTest10 = CU_add_test(pSuite4, "test10", test_succeed); - - TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); - TEST_FATAL(10 == CU_get_registry()->uiNumberOfTests); - - /* run all tests (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites/tests active */ - test_results(3,2,0,8,3,0,8,5,3,5); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_suite_active(pSuite4, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); /* suites inactive */ - test_results(0,0,4,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - test_results(0,0,4,0,0,0,0,0,0,4); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some suites inactive */ - test_results(1,1,2,2,1,0,2,1,1,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - test_results(1,1,2,2,1,0,2,1,1,4); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ - test_results(3,2,0,0,0,8,0,0,0,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(3,2,0,0,0,8,0,0,0,10); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ - test_results(3,2,0,4,0,4,4,4,0,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(3,2,0,4,0,4,4,4,0,6); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_TRUE); - - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - test_results(2,3,0,3,1,0,3,2,1,4); - - CU_set_suite_cleanupfunc(pSuite4, NULL); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite4, &suite_fail); - test_results(3,1,0,8,3,0,8,5,3,4); - - CU_set_test_func(pTest2, &test_succeed); - CU_set_test_func(pTest4, &test_succeed); - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ - CU_set_test_func(pTest2, &test_fail); - CU_set_test_func(pTest4, &test_fail); - CU_set_test_func(pTest8, &test_fail); - test_results(3,2,0,8,0,0,8,8,0,2); - - /* run all tests (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_suite_active(pSuite4, CU_TRUE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites inactive */ - test_results(2,1,2,6,2,0,6,4,2,3); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - test_results(1,0,1,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ - test_results(1,1,0,0,0,5,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_TRUE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ - test_results(1,1,0,2,2,3,2,0,2,3); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_TRUE); - - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - test_results(4,1,0,10,3,0,10,7,3,4); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_test_func(pTest1, &test_fail); - CU_set_test_func(pTest3, &test_fail); - CU_set_test_func(pTest5, &test_fail); - CU_set_test_func(pTest9, &test_fail); - CU_set_test_func(pTest10, &test_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ - CU_set_test_func(pTest1, &test_succeed); - CU_set_test_func(pTest3, &test_succeed); - CU_set_test_func(pTest5, &test_succeed); - CU_set_test_func(pTest9, &test_succeed); - CU_set_test_func(pTest10, &test_succeed); - test_results(1,1,0,5,5,0,5,0,5,6); - - /* run all tests (CUEA_ABORT) */ - f_exit_called = CU_FALSE; - CU_set_error_action(CUEA_ABORT); - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* all suites active */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_suite_active(pSuite4, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_all_tests()); /* no suites active, so no abort() */ - TEST(CU_FALSE == f_exit_called); - test_results(0,0,4,0,0,0,0,0,0,0); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,1); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* some suites active */ - TEST(CU_TRUE == f_exit_called); - test_results(3,1,1,8,3,0,8,5,3,4); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(1,0,1,5,2,0,5,3,2,3); - - CU_set_suite_active(pSuite1, CU_TRUE); - CU_set_suite_active(pSuite2, CU_TRUE); - CU_set_suite_active(pSuite3, CU_TRUE); - CU_set_suite_active(pSuite4, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_FALSE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* no tests active */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,5,0,0,0,1); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_FALSE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_test_active(pTest10, CU_TRUE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* some tests active */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,2,2,3,2,0,2,3); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_all_tests()); - TEST(CU_TRUE == f_exit_called); - test_results(1,0,0,0,0,1,0,0,0,1); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_test_active(pTest6, CU_TRUE); - CU_set_test_active(pTest7, CU_TRUE); - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_TRUE); - CU_set_test_active(pTest10, CU_TRUE); - - f_exit_called = CU_FALSE; - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_all_tests()); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,2,0,5,3,2,3); - - f_exit_called = CU_FALSE; - CU_set_test_func(pTest1, &test_fail); - CU_set_test_func(pTest3, &test_fail); - CU_set_test_func(pTest5, &test_fail); - CU_set_test_func(pTest9, &test_fail); - CU_set_test_func(pTest10, &test_fail); - TEST(CUE_SINIT_FAILED == CU_run_all_tests()); /* change a test function */ - CU_set_test_func(pTest1, &test_succeed); - CU_set_test_func(pTest3, &test_succeed); - CU_set_test_func(pTest5, &test_succeed); - CU_set_test_func(pTest9, &test_succeed); - CU_set_test_func(pTest10, &test_succeed); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,5,0,5,0,5,6); - - /* clean up after testing */ - CU_set_error_action(CUEA_IGNORE); - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -static void test_CU_run_suite(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pSuite pSuite4 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - - /* error - NULL suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOSUITE == CU_run_suite(NULL)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL suite (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOSUITE == CU_run_suite(NULL)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL suite (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_suite(NULL); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test3", test_succeed); - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test5", test_succeed); - pSuite2 = CU_add_suite("suite1", suite_fail, NULL); /* duplicate suite name OK */ - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite3", NULL, suite_fail); - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ - pSuite4 = CU_add_suite("suite4", NULL, NULL); - - TEST_FATAL(4 == CU_get_registry()->uiNumberOfSuites); - TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); - - /* run each suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suites/tests active */ - test_results(1,0,0,5,2,0,5,3,2,2); - - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); - test_results(1,1,0,2,1,0,2,1,1,2); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); - test_results(1,0,0,0,0,0,0,0,0,0); - - CU_set_suite_active(pSuite3, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite3)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite3)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite3, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_FALSE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* all tests inactive */ - test_results(1,0,0,0,0,5,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); - test_results(1,0,0,0,0,5,0,0,0,5); - - CU_set_test_active(pTest1, CU_TRUE); - CU_set_test_active(pTest2, CU_FALSE); - CU_set_test_active(pTest3, CU_TRUE); - CU_set_test_active(pTest4, CU_FALSE); - CU_set_test_active(pTest5, CU_TRUE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* some tests inactive */ - test_results(1,0,0,3,0,2,3,3,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite1)); - test_results(1,0,0,3,0,2,3,3,0,2); - CU_set_test_active(pTest2, CU_TRUE); - CU_set_test_active(pTest4, CU_TRUE); - - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - test_results(0,1,0,0,0,0,0,0,0,1); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_test_func(pTest1, &test_fail); - CU_set_test_func(pTest3, &test_fail); - CU_set_test_func(pTest5, &test_fail); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ - CU_set_test_func(pTest1, &test_succeed); - CU_set_test_func(pTest3, &test_succeed); - CU_set_test_func(pTest5, &test_succeed); - test_results(1,0,0,5,5,0,5,0,5,5); - - /* run each suite (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ - test_results(1,0,0,5,2,0,5,3,2,2); - - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); - test_results(1,1,0,2,1,0,2,1,1,2); - - TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); - test_results(1,0,0,0,0,0,0,0,0,0); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite1)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite1, CU_TRUE); - - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ - test_results(1,1,0,0,0,2,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - test_results(1,1,0,0,0,1,0,0,0,2); - - CU_set_test_active(pTest8, CU_TRUE); - CU_set_test_active(pTest9, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ - test_results(1,1,0,1,1,1,1,0,1,2); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - test_results(1,1,0,1,1,1,1,0,1,3); - CU_set_test_active(pTest9, CU_TRUE); - - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - test_results(1,0,0,2,0,0,2,2,0,0); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(1,1,0,5,2,0,5,3,2,3); - - CU_set_test_func(pTest2, &test_succeed); - CU_set_test_func(pTest4, &test_succeed); - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* change a test function */ - CU_set_test_func(pTest2, &test_fail); - CU_set_test_func(pTest4, &test_fail); - test_results(1,0,0,5,0,0,5,5,0,0); - - /* run each suite (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_suite(pSuite1)); /* suite active */ - TEST(CU_FALSE == f_exit_called); - test_results(1,0,0,5,2,0,5,3,2,2); - - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite2)); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,2,1,0,2,1,1,2); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_suite(pSuite4)); - TEST(CU_FALSE == f_exit_called); - test_results(1,0,0,0,0,0,0,0,0,0); - - CU_set_suite_active(pSuite2, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUCCESS == CU_run_suite(pSuite2)); /* suite inactive, but not a failure */ - TEST(CU_FALSE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,0); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_suite(pSuite2)); - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_FALSE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* all tests inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,2,0,0,0,1); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,1,0,0,0,2); - - CU_set_test_active(pTest8, CU_FALSE); - CU_set_test_active(pTest9, CU_TRUE); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* some tests inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,1,0,1,1,1,0,1); - f_exit_called = CU_FALSE; - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_suite(pSuite3)); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,0,0,1,0,0,0,2); - CU_set_test_active(pTest8, CU_TRUE); - - f_exit_called = CU_FALSE; - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_suite(pSuite1)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,5,2,0,5,3,2,3); - - f_exit_called = CU_FALSE; - CU_set_test_func(pTest8, &test_succeed); - CU_set_test_func(pTest9, &test_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_suite(pSuite3)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - CU_set_test_func(pTest9, &test_succeed); - TEST(CU_TRUE == f_exit_called); - test_results(1,1,0,2,1,0,2,1,1,2); - - /* clean up after testing */ - CU_set_error_action(CUEA_IGNORE); - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -static void test_CU_run_test(void) -{ - CU_pSuite pSuite1 = NULL; - CU_pSuite pSuite2 = NULL; - CU_pSuite pSuite3 = NULL; - CU_pTest pTest1 = NULL; - CU_pTest pTest2 = NULL; - CU_pTest pTest3 = NULL; - CU_pTest pTest4 = NULL; - CU_pTest pTest5 = NULL; - CU_pTest pTest6 = NULL; - CU_pTest pTest7 = NULL; - CU_pTest pTest8 = NULL; - CU_pTest pTest9 = NULL; - - /* register some suites and tests */ - CU_initialize_registry(); - pSuite1 = CU_add_suite("suite1", NULL, NULL); - pTest1 = CU_add_test(pSuite1, "test1", test_succeed); - pTest2 = CU_add_test(pSuite1, "test2", test_fail); - pTest3 = CU_add_test(pSuite1, "test3", test_succeed); - pTest4 = CU_add_test(pSuite1, "test4", test_fail); - pTest5 = CU_add_test(pSuite1, "test5", test_succeed); - pSuite2 = CU_add_suite("suite2", suite_fail, NULL); - pTest6 = CU_add_test(pSuite2, "test6", test_succeed); - pTest7 = CU_add_test(pSuite2, "test7", test_succeed); - pSuite3 = CU_add_suite("suite2", NULL, suite_fail); /* duplicate suite name OK */ - pTest8 = CU_add_test(pSuite3, "test8", test_fail); - pTest9 = CU_add_test(pSuite3, "test8", test_succeed); /* duplicate test name OK */ - - TEST_FATAL(3 == CU_get_registry()->uiNumberOfSuites); - TEST_FATAL(9 == CU_get_registry()->uiNumberOfTests); - - /* error - NULL suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL suite (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOSUITE == CU_run_test(NULL, pTest1)); - TEST(CUE_NOSUITE == CU_get_error()); - - /* error - NULL test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_test(NULL, pTest1); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* error - NULL test (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); - TEST(CUE_NOTEST == CU_get_error()); - - /* error - NULL test (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_NOTEST == CU_run_test(pSuite1, NULL)); - TEST(CUE_NOTEST == CU_get_error()); - - /* error - NULL test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_test(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* error - test not in suite (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); - TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); - - /* error - NULL test (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_TEST_NOT_IN_SUITE == CU_run_test(pSuite3, pTest1)); - TEST(CUE_TEST_NOT_IN_SUITE == CU_get_error()); - - /* error - NULL test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - CU_run_test(pSuite3, pTest1); - TEST(CU_TRUE == f_exit_called); - f_exit_called = CU_FALSE; - - /* run each test (CUEA_IGNORE) */ - CU_set_error_action(CUEA_IGNORE); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* all suite/tests active */ - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); - test_results(0,1,0,1,1,0,1,0,1,2); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_suite_active(pSuite1, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite1, pTest1)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite1, CU_TRUE); - - CU_set_test_active(pTest1, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); /* test inactive */ - test_results(0,0,0,0,0,1,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_TEST_INACTIVE == CU_run_test(pSuite1, pTest1)); - test_results(0,0,0,0,1,1,0,0,0,1); - CU_set_test_active(pTest1, CU_TRUE); - - CU_set_suite_initfunc(pSuite1, &suite_fail); - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite1, NULL); - test_results(0,1,0,0,0,0,0,0,0,1); - - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - test_results(0,1,0,1,0,0,1,1,0,1); - - /* run each test (CUEA_FAIL) */ - CU_set_error_action(CUEA_FAIL); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); /* suite/test active */ - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); - test_results(0,0,0,1,1,0,1,0,1,1); - - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); - test_results(0,0,0,1,0,0,1,1,0,0); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - test_results(0,1,0,0,0,0,0,0,0,1); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); - test_results(0,1,0,1,1,0,1,0,1,2); - - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); /* suite inactive */ - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest7)); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest7, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); /* test inactive */ - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_test_active(pTest7, CU_TRUE); - - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - test_results(0,0,0,1,0,0,1,1,0,0); - - CU_set_suite_cleanupfunc(pSuite3, NULL); - TEST(CUE_SUCCESS == CU_run_test(pSuite3, pTest8)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite3, &suite_fail); - test_results(0,0,0,1,1,0,1,0,1,1); - - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - test_results(0,1,0,1,0,0,1,1,0,1); - - /* run each test (CUEA_ABORT) */ - CU_set_error_action(CUEA_ABORT); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest1)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest2)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,1,0,1,0,1,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest3)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest4)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,1,0,1,0,1,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SUCCESS == CU_run_test(pSuite1, pTest5)); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest7)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - - f_exit_called = CU_FALSE; - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,1,0,1,0,1,2); - - f_exit_called = CU_FALSE; - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest9)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,0,0,1,1,0,1); - - CU_set_suite_active(pSuite2, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - f_exit_called = CU_FALSE; - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); /* suite inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,0); - CU_set_fail_on_inactive(CU_TRUE); - f_exit_called = CU_FALSE; - TEST(CUE_SUITE_INACTIVE == CU_run_test(pSuite2, pTest6)); - TEST(CU_TRUE == f_exit_called); - test_results(0,0,1,0,0,0,0,0,0,1); - CU_set_suite_active(pSuite2, CU_TRUE); - - CU_set_test_active(pTest6, CU_FALSE); - CU_set_fail_on_inactive(CU_FALSE); - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); /* test inactive */ - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_fail_on_inactive(CU_TRUE); - f_exit_called = CU_FALSE; - TEST(CUE_SINIT_FAILED == CU_run_test(pSuite2, pTest6)); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,0,0,0,0,0,0,1); - CU_set_test_active(pTest6, CU_TRUE); - - f_exit_called = CU_FALSE; - CU_set_suite_initfunc(pSuite2, NULL); - TEST(CUE_SUCCESS == CU_run_test(pSuite2, pTest6)); /* change a suite init function */ - CU_set_suite_initfunc(pSuite2, &suite_fail); - TEST(CU_FALSE == f_exit_called); - test_results(0,0,0,1,0,0,1,1,0,0); - - f_exit_called = CU_FALSE; - CU_set_suite_cleanupfunc(pSuite1, &suite_fail); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite1, pTest1)); /* change a suite cleanup function */ - CU_set_suite_cleanupfunc(pSuite1, NULL); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,0,0,1,1,0,1); - - f_exit_called = CU_FALSE; - CU_set_test_func(pTest8, &test_succeed); - TEST(CUE_SCLEAN_FAILED == CU_run_test(pSuite3, pTest8)); /* change a test function */ - CU_set_test_func(pTest8, &test_fail); - TEST(CU_TRUE == f_exit_called); - test_results(0,1,0,1,0,0,1,1,0,1); - - /* clean up after testing */ - CU_set_error_action(CUEA_IGNORE); - CU_cleanup_registry(); -} - -/*-------------------------------------------------*/ -/* tests CU_assertImplementation() - * CU_get_failure_list() - * CU_clear_previous_results() - */ -static void test_CU_assertImplementation(void) -{ - CU_Test dummy_test; - CU_Suite dummy_suite; - CU_pFailureRecord pFailure1 = NULL; - CU_pFailureRecord pFailure2 = NULL; - CU_pFailureRecord pFailure3 = NULL; - CU_pFailureRecord pFailure4 = NULL; - CU_pFailureRecord pFailure5 = NULL; - CU_pFailureRecord pFailure6 = NULL; - - CU_clear_previous_results(); - - TEST(NULL == CU_get_failure_list()); - TEST(0 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - /* fool CU_assertImplementation into thinking test run is in progress */ - f_pCurTest = &dummy_test; - f_pCurSuite = &dummy_suite; - - /* asserted value is CU_TRUE*/ - TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 100, "Nothing happened 0.", "dummy0.c", "dummy_func0", CU_FALSE)); - - TEST(NULL == CU_get_failure_list()); - TEST(1 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - TEST(CU_TRUE == CU_assertImplementation(CU_TRUE, 101, "Nothing happened 1.", "dummy1.c", "dummy_func1", CU_FALSE)); - - TEST(NULL == CU_get_failure_list()); - TEST(2 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - /* asserted value is CU_FALSE */ - TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 102, "Something happened 2.", "dummy2.c", "dummy_func2", CU_FALSE)); - - TEST(NULL != CU_get_failure_list()); - TEST(3 == CU_get_number_of_asserts()); - TEST(1 == CU_get_number_of_failures()); - TEST(1 == CU_get_number_of_failure_records()); - - TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 103, "Something happened 3.", "dummy3.c", "dummy_func3", CU_FALSE)); - - TEST(NULL != CU_get_failure_list()); - TEST(4 == CU_get_number_of_asserts()); - TEST(2 == CU_get_number_of_failures()); - TEST(2 == CU_get_number_of_failure_records()); - - TEST(CU_FALSE == CU_assertImplementation(CU_FALSE, 104, "Something happened 4.", "dummy4.c", "dummy_func4", CU_FALSE)); - - TEST(NULL != CU_get_failure_list()); - TEST(5 == CU_get_number_of_asserts()); - TEST(3 == CU_get_number_of_failures()); - TEST(3 == CU_get_number_of_failure_records()); - - if (3 == CU_get_number_of_failure_records()) { - pFailure1 = CU_get_failure_list(); - TEST(102 == pFailure1->uiLineNumber); - TEST(!strcmp("dummy2.c", pFailure1->strFileName)); - TEST(!strcmp("Something happened 2.", pFailure1->strCondition)); - TEST(&dummy_test == pFailure1->pTest); - TEST(&dummy_suite == pFailure1->pSuite); - TEST(NULL != pFailure1->pNext); - TEST(NULL == pFailure1->pPrev); - - pFailure2 = pFailure1->pNext; - TEST(103 == pFailure2->uiLineNumber); - TEST(!strcmp("dummy3.c", pFailure2->strFileName)); - TEST(!strcmp("Something happened 3.", pFailure2->strCondition)); - TEST(&dummy_test == pFailure2->pTest); - TEST(&dummy_suite == pFailure2->pSuite); - TEST(NULL != pFailure2->pNext); - TEST(pFailure1 == pFailure2->pPrev); - - pFailure3 = pFailure2->pNext; - TEST(104 == pFailure3->uiLineNumber); - TEST(!strcmp("dummy4.c", pFailure3->strFileName)); - TEST(!strcmp("Something happened 4.", pFailure3->strCondition)); - TEST(&dummy_test == pFailure3->pTest); - TEST(&dummy_suite == pFailure3->pSuite); - TEST(NULL == pFailure3->pNext); - TEST(pFailure2 == pFailure3->pPrev); - } - else - FAIL("Unexpected number of failure records."); - - /* confirm destruction of failure records */ - pFailure4 = pFailure1; - pFailure5 = pFailure2; - pFailure6 = pFailure3; - TEST(0 != test_cunit_get_n_memevents(pFailure4)); - TEST(test_cunit_get_n_allocations(pFailure4) != test_cunit_get_n_deallocations(pFailure4)); - TEST(0 != test_cunit_get_n_memevents(pFailure5)); - TEST(test_cunit_get_n_allocations(pFailure5) != test_cunit_get_n_deallocations(pFailure5)); - TEST(0 != test_cunit_get_n_memevents(pFailure6)); - TEST(test_cunit_get_n_allocations(pFailure6) != test_cunit_get_n_deallocations(pFailure6)); - - CU_clear_previous_results(); - TEST(0 != test_cunit_get_n_memevents(pFailure4)); - TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); - TEST(0 != test_cunit_get_n_memevents(pFailure5)); - TEST(test_cunit_get_n_allocations(pFailure5) == test_cunit_get_n_deallocations(pFailure5)); - TEST(0 != test_cunit_get_n_memevents(pFailure6)); - TEST(test_cunit_get_n_allocations(pFailure6) == test_cunit_get_n_deallocations(pFailure6)); - TEST(0 == CU_get_number_of_asserts()); - TEST(0 == CU_get_number_of_successes()); - TEST(0 == CU_get_number_of_failures()); - TEST(0 == CU_get_number_of_failure_records()); - - f_pCurTest = NULL; - f_pCurSuite = NULL; -} - -/*-------------------------------------------------*/ -static void test_add_failure(void) -{ - CU_Test test1; - CU_Suite suite1; - CU_pFailureRecord pFailure1 = NULL; - CU_pFailureRecord pFailure2 = NULL; - CU_pFailureRecord pFailure3 = NULL; - CU_pFailureRecord pFailure4 = NULL; - CU_RunSummary run_summary = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - - /* test under memory exhaustion */ - test_cunit_deactivate_malloc(); - add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 100, "condition 0", "file0.c", &suite1, &test1); - TEST(NULL == pFailure1); - TEST(0 == run_summary.nFailureRecords); - test_cunit_activate_malloc(); - - /* normal operation */ - add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 101, "condition 1", "file1.c", &suite1, &test1); - TEST(1 == run_summary.nFailureRecords); - if (TEST(NULL != pFailure1)) { - TEST(101 == pFailure1->uiLineNumber); - TEST(!strcmp("condition 1", pFailure1->strCondition)); - TEST(!strcmp("file1.c", pFailure1->strFileName)); - TEST(&test1 == pFailure1->pTest); - TEST(&suite1 == pFailure1->pSuite); - TEST(NULL == pFailure1->pNext); - TEST(NULL == pFailure1->pPrev); - TEST(pFailure1 == f_last_failure); - TEST(0 != test_cunit_get_n_memevents(pFailure1)); - TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); - } - - add_failure(&pFailure1, &run_summary, CUF_AssertFailed, 102, "condition 2", "file2.c", NULL, &test1); - TEST(2 == run_summary.nFailureRecords); - if (TEST(NULL != pFailure1)) { - TEST(101 == pFailure1->uiLineNumber); - TEST(!strcmp("condition 1", pFailure1->strCondition)); - TEST(!strcmp("file1.c", pFailure1->strFileName)); - TEST(&test1 == pFailure1->pTest); - TEST(&suite1 == pFailure1->pSuite); - TEST(NULL != pFailure1->pNext); - TEST(NULL == pFailure1->pPrev); - TEST(pFailure1 != f_last_failure); - TEST(0 != test_cunit_get_n_memevents(pFailure1)); - TEST(test_cunit_get_n_allocations(pFailure1) != test_cunit_get_n_deallocations(pFailure1)); - - if (TEST(NULL != (pFailure2 = pFailure1->pNext))) { - TEST(102 == pFailure2->uiLineNumber); - TEST(!strcmp("condition 2", pFailure2->strCondition)); - TEST(!strcmp("file2.c", pFailure2->strFileName)); - TEST(&test1 == pFailure2->pTest); - TEST(NULL == pFailure2->pSuite); - TEST(NULL == pFailure2->pNext); - TEST(pFailure1 == pFailure2->pPrev); - TEST(pFailure2 == f_last_failure); - TEST(0 != test_cunit_get_n_memevents(pFailure2)); - TEST(test_cunit_get_n_allocations(pFailure2) != test_cunit_get_n_deallocations(pFailure2)); - } - } - - pFailure3 = pFailure1; - pFailure4 = pFailure2; - clear_previous_results(&run_summary, &pFailure1); - - TEST(0 == run_summary.nFailureRecords); - TEST(0 != test_cunit_get_n_memevents(pFailure3)); - TEST(test_cunit_get_n_allocations(pFailure3) == test_cunit_get_n_deallocations(pFailure3)); - TEST(0 != test_cunit_get_n_memevents(pFailure4)); - TEST(test_cunit_get_n_allocations(pFailure4) == test_cunit_get_n_deallocations(pFailure4)); -} - -/*-------------------------------------------------*/ -void test_cunit_TestRun(void) -{ - test_cunit_start_tests("TestRun.c"); - - test_message_handlers(); - test_CU_fail_on_inactive(); - test_CU_run_all_tests(); - test_CU_run_suite(); - test_CU_run_test(); - test_CU_assertImplementation(); - test_add_failure(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/TestRun.h b/gearsyncd/tests/cunit/TestRun.h deleted file mode 100644 index 51072f492a..0000000000 --- a/gearsyncd/tests/cunit/TestRun.h +++ /dev/null @@ -1,444 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Contains Interface to Run tests. - * - * Aug 2001 Initial implementation. (AK) - * - * 09/Aug/2001 Contains generic run tests interface which can be used - * for any type of frontend interface framework. (AK) - * - * 24/Nov/2001 Added Handler for Group Initialization failure condition. (AK) - * - * 05-Aug-2004 New interface. Since these should be internal functions, - * no support for deprecated version 1 names provided now, - * eliminated global variables for current test & suite, - * moved (renamed) _TestResult here from TestDB.h. (JDS) - * - * 05-Sep-2004 Added internal test interface. (JDS) - * - * 23-Apr-2006 Moved doxygen comments into header. - * Added type marker to CU_FailureRecord. - * Added support for tracking inactive suites/tests. (JDS) - * - * 08-May-2006 Moved CU_print_run_results() functionality from - * console/basic test complete handler. (JDS) - * - * 24-May-2006 Added callbacks for suite start and complete events. - * Added tracking/reported of elapsed time. (JDS) - */ - -/** @file - * Test run management functions (user interface). - * The TestRun module implements functions supporting the running - * of tests elements (suites and tests). This includes functions for - * running suites and tests, retrieving the number of tests/suites run, - * and managing callbacks during the run process.

- * - * The callback mechanism works as follows. The CUnit runtime system - * supports the registering and calling of functions at the start and end - * of each test, when all tests are complete, and when a suite - * initialialization function returns an error. This allows clients to - * perform actions associated with these events such as output formatting - * and reporting. - */ -/** @addtogroup Framework - * @{ - */ - -#ifndef CUNIT_TESTRUN_H_SEEN -#define CUNIT_TESTRUN_H_SEEN - -#include "CUnit.h" -#include "CUError.h" -#include "TestDB.h" -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/** Types of failures occurring during test runs. */ -typedef enum CU_FailureTypes -{ - CUF_SuiteInactive = 1, /**< Inactive suite was run. */ - CUF_SuiteInitFailed, /**< Suite initialization function failed. */ - CUF_SuiteCleanupFailed, /**< Suite cleanup function failed. */ - CUF_TestInactive, /**< Inactive test was run. */ - CUF_AssertFailed /**< CUnit assertion failed during test run. */ -} CU_FailureType; /**< Failure type. */ - -/* CU_FailureRecord type definition. */ -/** Data type for holding assertion failure information (linked list). */ -typedef struct CU_FailureRecord -{ - CU_FailureType type; /**< Failure type. */ - unsigned int uiLineNumber; /**< Line number of failure. */ - char* strFileName; /**< Name of file where failure occurred. */ - char* strCondition; /**< Test condition which failed. */ - CU_pTest pTest; /**< Test containing failure. */ - CU_pSuite pSuite; /**< Suite containing test having failure. */ - - struct CU_FailureRecord* pNext; /**< Pointer to next record in linked list. */ - struct CU_FailureRecord* pPrev; /**< Pointer to previous record in linked list. */ - -} CU_FailureRecord; -typedef CU_FailureRecord* CU_pFailureRecord; /**< Pointer to CU_FailureRecord. */ - -/* CU_RunSummary type definition. */ -/** Data type for holding statistics and assertion failures for a test run. */ -typedef struct CU_RunSummary -{ - unsigned int nSuitesRun; /**< Number of suites completed during run. */ - unsigned int nSuitesFailed; /**< Number of suites for which initialization failed. */ - unsigned int nSuitesInactive; /**< Number of suites which were inactive. */ - unsigned int nTestsRun; /**< Number of tests completed during run. */ - unsigned int nTestsFailed; /**< Number of tests containing failed assertions. */ - unsigned int nTestsInactive; /**< Number of tests which were inactive (in active suites). */ - unsigned int nAsserts; /**< Number of assertions tested during run. */ - unsigned int nAssertsFailed; /**< Number of failed assertions. */ - unsigned int nFailureRecords; /**< Number of failure records generated. */ - double ElapsedTime; /**< Elapsed time for run in seconds. */ -} CU_RunSummary; -typedef CU_RunSummary* CU_pRunSummary; /**< Pointer to CU_RunSummary. */ - -/*-------------------------------------------------------------------- - * Type Definitions for Message Handlers. - *--------------------------------------------------------------------*/ -typedef void (*CU_SuiteStartMessageHandler)(const CU_pSuite pSuite); -/**< Message handler called at the start of a suite. pSuite will not be null. */ - -typedef void (*CU_TestStartMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite); -/**< Message handler called at the start of a test. - * The parameters are the test and suite being run. The test run is - * considered in progress when the message handler is called. - * Neither pTest nor pSuite may be null. - */ - -typedef void (*CU_TestCompleteMessageHandler)(const CU_pTest pTest, const CU_pSuite pSuite, - const CU_pFailureRecord pFailure); -/**< Message handler called at the completion of a test. - * The parameters are the test and suite being run, plus a pointer to - * the first failure record applicable to this test. If the test did - * not have any assertion failures, pFailure will be NULL. The test run - * is considered in progress when the message handler is called. - */ - -typedef void (*CU_SuiteCompleteMessageHandler)(const CU_pSuite pSuite, - const CU_pFailureRecord pFailure); -/**< Message handler called at the completion of a suite. - * The parameters are suite being run, plus a pointer to the first failure - * record applicable to this suite. If the suite and it's tests did not - * have any failures, pFailure will be NULL. The test run is considered - * in progress when the message handler is called. - */ - -typedef void (*CU_AllTestsCompleteMessageHandler)(const CU_pFailureRecord pFailure); -/**< Message handler called at the completion of a test run. - * The parameter is a pointer to the linked list holding the failure - * records for the test run. The test run is considered completed - * when the message handler is called. - */ - -typedef void (*CU_SuiteInitFailureMessageHandler)(const CU_pSuite pSuite); -/**< Message handler called when a suite initializer fails. - * The test run is considered in progress when the message handler is called. - */ - -typedef void (*CU_SuiteCleanupFailureMessageHandler)(const CU_pSuite pSuite); -/**< Message handler called when a suite cleanup function fails. - * The test run is considered in progress when the message handler is called. - */ - -/*-------------------------------------------------------------------- - * Get/Set functions for Message Handlers - *--------------------------------------------------------------------*/ -CU_EXPORT void CU_set_suite_start_handler(CU_SuiteStartMessageHandler pSuiteStartMessage); -/**< Sets the message handler to call before each suite is run. */ -CU_EXPORT void CU_set_test_start_handler(CU_TestStartMessageHandler pTestStartMessage); -/**< Sets the message handler to call before each test is run. */ -CU_EXPORT void CU_set_test_complete_handler(CU_TestCompleteMessageHandler pTestCompleteMessage); -/**< Sets the message handler to call after each test is run. */ -CU_EXPORT void CU_set_suite_complete_handler(CU_SuiteCompleteMessageHandler pSuiteCompleteMessage); -/**< Sets the message handler to call after each suite is run. */ -CU_EXPORT void CU_set_all_test_complete_handler(CU_AllTestsCompleteMessageHandler pAllTestsCompleteMessage); -/**< Sets the message handler to call after all tests have been run. */ -CU_EXPORT void CU_set_suite_init_failure_handler(CU_SuiteInitFailureMessageHandler pSuiteInitFailureMessage); -/**< Sets the message handler to call when a suite initialization function returns an error. */ -CU_EXPORT void CU_set_suite_cleanup_failure_handler(CU_SuiteCleanupFailureMessageHandler pSuiteCleanupFailureMessage); -/**< Sets the message handler to call when a suite cleanup function returns an error. */ - -CU_EXPORT CU_SuiteStartMessageHandler CU_get_suite_start_handler(void); -/**< Retrieves the message handler called before each suite is run. */ -CU_EXPORT CU_TestStartMessageHandler CU_get_test_start_handler(void); -/**< Retrieves the message handler called before each test is run. */ -CU_EXPORT CU_TestCompleteMessageHandler CU_get_test_complete_handler(void); -/**< Retrieves the message handler called after each test is run. */ -CU_EXPORT CU_SuiteCompleteMessageHandler CU_get_suite_complete_handler(void); -/**< Retrieves the message handler called after each suite is run. */ -CU_EXPORT CU_AllTestsCompleteMessageHandler CU_get_all_test_complete_handler(void); -/**< Retrieves the message handler called after all tests are run. */ -CU_EXPORT CU_SuiteInitFailureMessageHandler CU_get_suite_init_failure_handler(void); -/**< Retrieves the message handler called when a suite initialization error occurs. */ -CU_EXPORT CU_SuiteCleanupFailureMessageHandler CU_get_suite_cleanup_failure_handler(void); -/**< Retrieves the message handler called when a suite cleanup error occurs. */ - -/*-------------------------------------------------------------------- - * Functions for running registered tests and suites. - *--------------------------------------------------------------------*/ -CU_EXPORT CU_ErrorCode CU_run_all_tests(void); -/**< - * Runs all tests in all suites registered in the test registry. - * The suites are run in the order registered in the test registry. - * For each suite, it is first checked to make sure it is active. - * Any initialization function is then called, the suite is run - * using run_single_suite(), and finally any suite cleanup function - * is called. If an error condition (other than CUE_NOREGISTRY) - * occurs during the run, the action depends on the current error - * action (see CU_set_error_action()). An inactive suite is not - * considered an error for this function. Note that the run - * statistics (counts of tests, successes, failures) are cleared - * each time this function is run, even if it is unsuccessful. - * - * @return A CU_ErrorCode indicating the first error condition - * encountered while running the tests. - * @see CU_run_suite() to run the tests in a specific suite. - * @see CU_run_test() for run a specific test only. - */ - -CU_EXPORT CU_ErrorCode CU_run_suite(CU_pSuite pSuite); -/**< - * Runs all tests in a specified suite. - * The suite need not be registered in the test registry to be - * run. It does, however, need to have its fActive flag set to - * CU_TRUE.

- * - * Any initialization function for the suite is first called, - * then the suite is run using run_single_suite(), and any suite - * cleanup function is called. Note that the run statistics - * (counts of tests, successes, failures) are initialized each - * time this function is called even if it is unsuccessful. If - * an error condition occurs during the run, the action depends - * on the current error action (see CU_set_error_action()). - * - * @param pSuite The suite containing the test (non-NULL) - * @return A CU_ErrorCode indicating the first error condition - * encountered while running the suite. CU_run_suite() - * sets and returns CUE_NOSUITE if pSuite is NULL, or - * CUE_SUITE_INACTIVE if the requested suite is not - * activated. Other error codes can be set during suite - * initialization or cleanup or during test runs. - * @see CU_run_all_tests() to run all suites. - * @see CU_run_test() to run a single test in a specific suite. - */ - -CU_EXPORT CU_ErrorCode CU_run_test(CU_pSuite pSuite, CU_pTest pTest); -/**< - * Runs a specific test in a specified suite. - * The suite need not be registered in the test registry to be run, - * although the test must be registered in the specified suite. - * Any initialization function for the suite is first - * called, then the test is run using run_single_test(), and - * any suite cleanup function is called. Note that the - * run statistics (counts of tests, successes, failures) - * will be initialized each time this function is called even - * if it is not successful. Both the suite and test specified - * must be active for the test to be run. The suite is not - * considered to be run, although it may be counted as a failed - * suite if the intialization or cleanup functions fail. - * - * @param pSuite The suite containing the test (non-NULL) - * @param pTest The test to run (non-NULL) - * @return A CU_ErrorCode indicating the first error condition - * encountered while running the suite. CU_run_test() - * sets and returns CUE_NOSUITE if pSuite is NULL, - * CUE_NOTEST if pTest is NULL, CUE_SUITE_INACTIVE if - * pSuite is not active, CUE_TEST_NOT_IN_SUITE - * if pTest is not registered in pSuite, and CU_TEST_INACTIVE - * if pTest is not active. Other error codes can be set during - * suite initialization or cleanup or during the test run. - * @see CU_run_all_tests() to run all tests/suites. - * @see CU_run_suite() to run all tests in a specific suite. - */ - -/*-------------------------------------------------------------------- - * Functions for setting runtime behavior. - *--------------------------------------------------------------------*/ -CU_EXPORT void CU_set_fail_on_inactive(CU_BOOL new_inactive); -/**< - * Sets whether an inactive suite or test is treated as a failure. - * If CU_TRUE, then failure records will be generated for inactive - * suites or tests encountered during a test run. The default is - * CU_TRUE so that the client is reminded that the framewrork - * contains inactive suites/tests. Set to CU_FALSE to turn off - * this behavior. - * - * @param new_inactive New setting for whether to treat inactive - * suites and tests as failures during a test - * run (CU_TRUE) or not (CU_FALSE). - * @see CU_get_fail_on_failure() - */ - -CU_EXPORT CU_BOOL CU_get_fail_on_inactive(void); -/**< - * Retrieves the current setting for whether inactive suites/tests - * are treated as failures. If CU_TRUE then failure records will - * be generated for inactive suites encountered during a test run. - * - * @return CU_TRUE if inactive suites/tests are failures, CU_FALSE if not. - * @see CU_set_fail_on_inactive() - */ - -/*-------------------------------------------------------------------- - * Functions for getting information about the previous test run. - *--------------------------------------------------------------------*/ -CU_EXPORT unsigned int CU_get_number_of_suites_run(void); -/**< Retrieves the number of suites completed during the previous run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_suites_failed(void); -/**< Retrieves the number of suites which failed to initialize during the previous run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_suites_inactive(void); -/**< Retrieves the number of inactive suites found during the previous run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_tests_run(void); -/**< Retrieves the number of tests completed during the previous run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_tests_failed(void); -/**< Retrieves the number of tests containing failed assertions during the previous run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_tests_inactive(void); -/**< Retrieves the number of inactive tests found during the previous run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_asserts(void); -/**< Retrieves the number of assertions processed during the last run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_successes(void); -/**< Retrieves the number of successful assertions during the last run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_failures(void); -/**< Retrieves the number of failed assertions during the last run (reset each run). */ -CU_EXPORT unsigned int CU_get_number_of_failure_records(void); -/**< - * Retrieves the number failure records created during the previous run (reset each run). - * Note that this may be more than the number of failed assertions, since failure - * records may also be created for failed suite initialization and cleanup. - */ -CU_EXPORT double CU_get_elapsed_time(void); -/**< - * Retrieves the elapsed time for the last run in seconds (reset each run). - * This function will calculate the current elapsed time if the test run has not - * yet completed. This is in contrast to the run summary returned by - * CU_get_run_summary(), for which the elapsed time is not updated until the - * end of the run. - */ -CU_EXPORT CU_pFailureRecord CU_get_failure_list(void); -/**< - * Retrieves the head of the linked list of failures which occurred during the - * last run (reset each run). Note that the pointer returned is invalidated - * when the client initiates a run using CU_run_all_tests(), CU_run_suite(), - * or CU_run_test(). - */ -CU_EXPORT CU_pRunSummary CU_get_run_summary(void); -/**< - * Retrieves the entire run summary for the last test run (reset each run). - * The run counts and stats contained in the run summary are updated - * throughout a test run. Note, however, that the elapsed time is not - * updated until after all suites/tests are run but before the "all tests - * complete" message handler is called (if any). To get the elapsed - * time during a test run, use CU_get_elapsed_time() instead. - */ - -CU_EXPORT char * CU_get_run_results_string(void); -/**< - * Creates a string and fills it with a summary of the current run results. - * The run summary presents data for the suites, tests, and assertions - * encountered during the run, as well as the elapsed time. The data - * presented include the number of registered, run, passed, failed, and - * inactive entities for each, as well as the elapsed time. This function - * can be called at any time, although the test registry must have been - * initialized (checked by assertion). The returned string is owned by - * the caller and should be deallocated using CU_FREE(). NULL is returned - * if there is an error allocating the new string. - * - * @return A new string containing the run summary (owned by caller). - */ - -CU_EXPORT void CU_print_run_results(FILE *file); -/**< - * Prints a summary of the current run results to file. - * The run summary is the same as returned by CU_get_run_results_string(). - * Note that no newlines are printed before or after the report, so any - * positioning must be performed before/after calling this function. The - * report itself extends over several lines broken by '\n' characters. - * file may not be NULL (checked by assertion). - * - * @param file Pointer to stream to receive the printed summary (non-NULL). - */ - -/*-------------------------------------------------------------------- - * Functions for internal & testing use. - *--------------------------------------------------------------------*/ -CU_EXPORT CU_pSuite CU_get_current_suite(void); -/**< Retrieves a pointer to the currently-running suite (NULL if none). */ -CU_EXPORT CU_pTest CU_get_current_test(void); -/**< Retrievea a pointer to the currently-running test (NULL if none). */ -CU_EXPORT CU_BOOL CU_is_test_running(void); -/**< Returns CU_TRUE if a test run is in progress, - * CU_TRUE otherwise. - */ - -CU_EXPORT void CU_clear_previous_results(void); -/**< - * Initializes the run summary information stored from the previous test run. - * Resets the run counts to zero, and frees any memory associated with - * failure records. Calling this function multiple times, while inefficient, - * will not cause an error condition. - * @see clear_previous_results() - */ - -CU_EXPORT CU_BOOL CU_assertImplementation(CU_BOOL bValue, - unsigned int uiLine, - const char *strCondition, - const char *strFile, - const char *strFunction, - CU_BOOL bFatal); -/**< - * Assertion implementation function. - * All CUnit assertions reduce to a call to this function. It should only be - * called during an active test run (checked by assertion). This means that CUnit - * assertions should only be used in registered test functions during a test run. - * - * @param bValue Value of the assertion (CU_TRUE or CU_FALSE). - * @param uiLine Line number of failed test statement. - * @param strCondition String containing logical test that failed. - * @param strFile Source file where test statement failed. - * @param strFunction Function where test statement failed. - * @param bFatal CU_TRUE to abort test (via longjmp()), CU_FALSE to continue test. - * @return As a convenience, returns the value of the assertion (i.e. bValue). - */ - -#ifdef USE_DEPRECATED_CUNIT_NAMES -typedef CU_FailureRecord _TestResult; /**< @deprecated Use CU_FailureRecord. */ -typedef CU_pFailureRecord PTestResult; /**< @deprecated Use CU_pFailureRecord. */ -#endif /* USE_DEPRECATED_CUNIT_NAMES */ - -#ifdef CUNIT_BUILD_TESTS -void test_cunit_TestRun(void); -#endif - -#ifdef __cplusplus -} -#endif -#endif /* CUNIT_TESTRUN_H_SEEN */ -/** @} */ diff --git a/gearsyncd/tests/cunit/Util.c b/gearsyncd/tests/cunit/Util.c deleted file mode 100644 index 8e21922b9e..0000000000 --- a/gearsyncd/tests/cunit/Util.c +++ /dev/null @@ -1,609 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Generic (internal) utility functions used across CUnit. - * These were originally distributed across the other CUnit - * source files, but were consolidated here for consistency. - * - * 13/Oct/2001 Initial implementation (AK) - * - * 26/Jul/2003 Added a function to convert a string containing special - * characters into escaped character for XML/HTML usage. (AK) - * - * 16-Jul-2004 New interface, doxygen comments. (JDS) - * - * 17-Apr-2006 Added CU_translated_strlen() and CU_number_width(). - * Fixed off-by-1 error in CU_translate_special_characters(), - * modifying implementation & results in some cases. User can - * now tell if conversion failed. (JDS) - */ - -/** @file - * Utility functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "Util.h" - - -/*------------------------------------------------------------------------*/ -/** - * Structure containing mappings of special characters to xml entity codes. - * special_char's in the CU_bindings array will be translated during calls - * to CU_translate_special_characters(). Add additional chars/replacements - * or modify existing ones to change the behavior upon translation. - */ -static const struct bindings { - const char special_char; /**< Special character. */ - const char *replacement; /**< Entity code for special character. */ -} CU_bindings [] = { - {'&', "&"}, - {'>', ">"}, - {'<', "<"} -}; - -/*------------------------------------------------------------------------*/ -/** - * Checks whether a character is a special xml character. - * This function performs a lookup of the specified character in - * the CU_bindings structure. If it is a special character, its - * index into the CU_bindings array is returned. If not, -1 is returned. - * - * @param ch The character to check - * @return Index into CU_bindings if a special character, -1 otherwise. - */ -static int get_index(const char ch) -{ - int length = sizeof(CU_bindings)/sizeof(CU_bindings[0]); - int counter; - - for (counter = 0; counter < length && CU_bindings[counter].special_char != ch; ++counter) { - ; - } - - return (counter < length ? counter : -1); -} - -size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen) -{ -/* old implementation - size_t count = 0; - size_t src = 0; - size_t dest = 0; - size_t length = 0; - int conv_index; - - assert(NULL != szSrc); - assert(NULL != szDest); - - length = strlen(szSrc); - memset(szDest, 0, maxlen); - while ((dest < maxlen) && (src < length)) { - - if ((-1 != (conv_index = get_index(szSrc[src]))) && - ((dest + strlen(CU_bindings[conv_index].replacement)) < maxlen)) { - strcat(szDest, CU_bindings[conv_index].replacement); - dest += strlen(CU_bindings[conv_index].replacement); - ++count; - } else { - szDest[dest++] = szSrc[src]; - } - - ++src; - } - - return count; -*/ - size_t count = 0; - size_t repl_len; - int conv_index; - char *dest_start = szDest; - - assert(NULL != szSrc); - assert(NULL != szDest); - - /* only process if destination buffer not 0-length */ - if (maxlen > 0) { - - while ((maxlen > 0) && (*szSrc != '\0')) { - conv_index = get_index(*szSrc); - if (-1 != conv_index) { - if (maxlen > (repl_len = strlen(CU_bindings[conv_index].replacement))) { - memcpy(szDest, CU_bindings[conv_index].replacement, repl_len); - szDest += repl_len; - maxlen -= repl_len; - ++count; - } else { - maxlen = 0; /* ran out of room - abort conversion */ - break; - } - } else { - *szDest++ = *szSrc; - --maxlen; - } - ++szSrc; - } - - if (0 == maxlen) { - *dest_start = '\0'; /* ran out of room - return empty string in szDest */ - count = 0; - } else { - *szDest = '\0'; /* had room - make sure szDest has a terminating \0 */ - } - } - return count; -} - -/*------------------------------------------------------------------------*/ -size_t CU_translated_strlen(const char* szSrc) -{ - size_t count = 0; - int conv_index; - - assert(NULL != szSrc); - - while (*szSrc != '\0') { - if (-1 != (conv_index = get_index(*szSrc))) { - count += strlen(CU_bindings[conv_index].replacement); - } else { - ++count; - } - ++szSrc; - } - return count; -} - -/*------------------------------------------------------------------------*/ -int CU_compare_strings(const char* szSrc, const char* szDest) -{ - assert(NULL != szSrc); - assert(NULL != szDest); - - while (('\0' != *szSrc) && ('\0' != *szDest) && (toupper(*szSrc) == toupper(*szDest))) { - szSrc++; - szDest++; - } - - return (int)(*szSrc - *szDest); -} - -/*------------------------------------------------------------------------*/ -void CU_trim(char* szString) -{ - CU_trim_left(szString); - CU_trim_right(szString); -} - -/*------------------------------------------------------------------------*/ -void CU_trim_left(char* szString) -{ - int nOffset = 0; - char* szSrc = szString; - char* szDest = szString; - - assert(NULL != szString); - - /* Scan for the spaces in the starting of string. */ - for (; '\0' != *szSrc; szSrc++, nOffset++) { - if (!isspace(*szSrc)) { - break; - } - } - - for(; (0 != nOffset) && ('\0' != (*szDest = *szSrc)); szSrc++, szDest++) { - ; - } -} - -/*------------------------------------------------------------------------*/ -void CU_trim_right(char* szString) -{ - size_t nLength; - char* szSrc = szString; - - assert(NULL != szString); - nLength = strlen(szString); - /* - * Scan for specs in the end of string. - */ - for (; (0 != nLength) && isspace(*(szSrc + nLength - 1)); nLength--) { - ; - } - - *(szSrc + nLength) = '\0'; -} - -/*------------------------------------------------------------------------*/ -size_t CU_number_width(int number) -{ - char buf[33]; - - snprintf(buf, 33, "%d", number); - buf[32] = '\0'; - return (strlen(buf)); -} - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -/* Keep BUF_LEN even or trouble ensues below... */ -#define BUF_LEN 1000 -#define MAX_LEN BUF_LEN/2 - -static void test_CU_translate_special_characters(void) -{ - char dest_buf[BUF_LEN]; - char *dest = dest_buf + MAX_LEN; - char ref_buf[BUF_LEN]; - const int mask_char = 0x01; /* char written to buffer */ - - /* set up reference buffer for testing of translated strings */ - memset(ref_buf, mask_char, BUF_LEN); - - /* empty src */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); - - /* 1 char src */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("#", dest, 0)); - TEST(!strncmp(dest_buf, ref_buf, BUF_LEN)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("#", dest, 1)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("&", dest, 2)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("&", dest, 4)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+4), ref_buf, MAX_LEN-4)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("&", dest, 5)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(1 == CU_translate_special_characters("&", dest, 6)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "&\0", 6)); - TEST(!strncmp((dest+6), ref_buf, MAX_LEN-6)); - - /* maxlen=0 */ - memset(dest_buf, mask_char, BUF_LEN); - strcpy(dest, "random initialized string"); - TEST(0 == CU_translate_special_characters("some <", dest, 0)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strcmp(dest, "random initialized string")); - TEST(!strncmp(dest+strlen(dest)+1, ref_buf, MAX_LEN-strlen(dest)-1)); - - /* maxlen < len(converted szSrc) */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 1)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 2)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 5)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 10)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+10), ref_buf, MAX_LEN-10)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 20)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+20), ref_buf, MAX_LEN-20)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 24)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+24), ref_buf, MAX_LEN-24)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 25)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+25), ref_buf, MAX_LEN-25)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 37)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+37), ref_buf, MAX_LEN-37)); - - /* maxlen > len(converted szSrc) */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(4 == CU_translate_special_characters("some <", dest, 38)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "some <<string & another>\0", 38)); - TEST(!strncmp((dest+38), ref_buf, MAX_LEN-38)); - - /* maxlen > len(converted szSrc) */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(4 == CU_translate_special_characters("some <", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "some <<string & another>\0", 38)); - - /* no special characters */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some string or another", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "some string or another\0", 23)); - - /* only special characters */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(11 == CU_translate_special_characters("<><><<>>&&&", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strcmp(dest, "<><><<>>&&&")); -} - -static void test_CU_translated_strlen(void) -{ - /* empty src */ - TEST(0 == CU_translated_strlen("")); - - /* 1 char src */ - TEST(1 == CU_translated_strlen("#")); - TEST(5 == CU_translated_strlen("&")); - TEST(4 == CU_translated_strlen("<")); - TEST(4 == CU_translated_strlen(">")); - TEST(1 == CU_translated_strlen("?")); - - /* 2 char src */ - TEST(2 == CU_translated_strlen("#@")); - TEST(10 == CU_translated_strlen("&&")); - TEST(9 == CU_translated_strlen(">&")); - - /* longer src */ - TEST(37 == CU_translated_strlen("some <")); - TEST(22 == CU_translated_strlen("some string or another")); - TEST(47 == CU_translated_strlen("<><><<>>&&&")); -} - -static void test_CU_compare_strings(void) -{ - TEST(0 == CU_compare_strings("","")); - TEST(0 == CU_compare_strings("@","@")); - TEST(0 == CU_compare_strings("D","d")); - TEST(0 == CU_compare_strings("s1","s1")); - TEST(0 == CU_compare_strings("s1","S1")); - TEST(0 != CU_compare_strings("s1","s12")); - TEST(0 == CU_compare_strings("this is string 1","tHIS iS sTRING 1")); - TEST(0 == CU_compare_strings("i have \t a tab!","I have \t a tab!")); - TEST(0 != CU_compare_strings("not the same"," not the same")); -} - -static void test_CU_trim(void) -{ - char string[MAX_LEN]; - - strcpy(string, ""); - CU_trim(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim(string); - TEST(!strcmp("", string)); - - strcpy(string, " b"); - CU_trim(string); - TEST(!strcmp("b", string)); - - strcpy(string, " B"); - CU_trim(string); - TEST(!strcmp("B", string)); - - strcpy(string, "s "); - CU_trim(string); - TEST(!strcmp("s", string)); - - strcpy(string, "S "); - CU_trim(string); - TEST(!strcmp("S", string)); - - strcpy(string, " 5 "); - CU_trim(string); - TEST(!strcmp("5", string)); - - strcpy(string, "~ & ^ ( ^ "); - CU_trim(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^"); - CU_trim(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^ "); - CU_trim(string); - TEST(!strcmp("~ & ^ ( ^", string)); -} - -static void test_CU_trim_left(void) -{ - char string[MAX_LEN]; - - strcpy(string, ""); - CU_trim_left(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_left(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_left(string); - TEST(!strcmp("", string)); - - strcpy(string, " b"); - CU_trim_left(string); - TEST(!strcmp("b", string)); - - strcpy(string, " B"); - CU_trim_left(string); - TEST(!strcmp("B", string)); - - strcpy(string, "s "); - CU_trim_left(string); - TEST(!strcmp("s ", string)); - - strcpy(string, "S "); - CU_trim_left(string); - TEST(!strcmp("S ", string)); - - strcpy(string, " 5 "); - CU_trim_left(string); - TEST(!strcmp("5 ", string)); - - strcpy(string, "~ & ^ ( ^ "); - CU_trim_left(string); - TEST(!strcmp("~ & ^ ( ^ ", string)); - - strcpy(string, " ~ & ^ ( ^"); - CU_trim_left(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^ "); - CU_trim_left(string); - TEST(!strcmp("~ & ^ ( ^ ", string)); -} - -static void test_CU_trim_right(void) -{ - char string[MAX_LEN]; - - strcpy(string, ""); - CU_trim_right(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_right(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_right(string); - TEST(!strcmp("", string)); - - strcpy(string, " b"); - CU_trim_right(string); - TEST(!strcmp(" b", string)); - - strcpy(string, " B"); - CU_trim_right(string); - TEST(!strcmp(" B", string)); - - strcpy(string, "s "); - CU_trim_right(string); - TEST(!strcmp("s", string)); - - strcpy(string, "S "); - CU_trim_right(string); - TEST(!strcmp("S", string)); - - strcpy(string, " 5 "); - CU_trim_right(string); - TEST(!strcmp(" 5", string)); - - strcpy(string, "~ & ^ ( ^ "); - CU_trim_right(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^"); - CU_trim_right(string); - TEST(!strcmp(" ~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^ "); - CU_trim_right(string); - TEST(!strcmp(" ~ & ^ ( ^", string)); -} - -static void test_CU_number_width(void) -{ - TEST(1 == CU_number_width(0)); - TEST(1 == CU_number_width(1)); - TEST(2 == CU_number_width(-1)); - TEST(4 == CU_number_width(2346)); - TEST(7 == CU_number_width(-257265)); - TEST(9 == CU_number_width(245723572)); - TEST(9 == CU_number_width(-45622572)); -} - -void test_cunit_Util(void) -{ - - test_cunit_start_tests("Util.c"); - - test_CU_translate_special_characters(); - test_CU_translated_strlen(); - test_CU_compare_strings(); - test_CU_trim(); - test_CU_trim_left(); - test_CU_trim_right(); - test_CU_number_width(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/Util.cpp b/gearsyncd/tests/cunit/Util.cpp deleted file mode 100644 index 8e21922b9e..0000000000 --- a/gearsyncd/tests/cunit/Util.cpp +++ /dev/null @@ -1,609 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Generic (internal) utility functions used across CUnit. - * These were originally distributed across the other CUnit - * source files, but were consolidated here for consistency. - * - * 13/Oct/2001 Initial implementation (AK) - * - * 26/Jul/2003 Added a function to convert a string containing special - * characters into escaped character for XML/HTML usage. (AK) - * - * 16-Jul-2004 New interface, doxygen comments. (JDS) - * - * 17-Apr-2006 Added CU_translated_strlen() and CU_number_width(). - * Fixed off-by-1 error in CU_translate_special_characters(), - * modifying implementation & results in some cases. User can - * now tell if conversion failed. (JDS) - */ - -/** @file - * Utility functions (implementation). - */ -/** @addtogroup Framework - @{ -*/ - -#include -#include -#include -#include -#include - -#include "CUnit.h" -#include "TestDB.h" -#include "Util.h" - - -/*------------------------------------------------------------------------*/ -/** - * Structure containing mappings of special characters to xml entity codes. - * special_char's in the CU_bindings array will be translated during calls - * to CU_translate_special_characters(). Add additional chars/replacements - * or modify existing ones to change the behavior upon translation. - */ -static const struct bindings { - const char special_char; /**< Special character. */ - const char *replacement; /**< Entity code for special character. */ -} CU_bindings [] = { - {'&', "&"}, - {'>', ">"}, - {'<', "<"} -}; - -/*------------------------------------------------------------------------*/ -/** - * Checks whether a character is a special xml character. - * This function performs a lookup of the specified character in - * the CU_bindings structure. If it is a special character, its - * index into the CU_bindings array is returned. If not, -1 is returned. - * - * @param ch The character to check - * @return Index into CU_bindings if a special character, -1 otherwise. - */ -static int get_index(const char ch) -{ - int length = sizeof(CU_bindings)/sizeof(CU_bindings[0]); - int counter; - - for (counter = 0; counter < length && CU_bindings[counter].special_char != ch; ++counter) { - ; - } - - return (counter < length ? counter : -1); -} - -size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen) -{ -/* old implementation - size_t count = 0; - size_t src = 0; - size_t dest = 0; - size_t length = 0; - int conv_index; - - assert(NULL != szSrc); - assert(NULL != szDest); - - length = strlen(szSrc); - memset(szDest, 0, maxlen); - while ((dest < maxlen) && (src < length)) { - - if ((-1 != (conv_index = get_index(szSrc[src]))) && - ((dest + strlen(CU_bindings[conv_index].replacement)) < maxlen)) { - strcat(szDest, CU_bindings[conv_index].replacement); - dest += strlen(CU_bindings[conv_index].replacement); - ++count; - } else { - szDest[dest++] = szSrc[src]; - } - - ++src; - } - - return count; -*/ - size_t count = 0; - size_t repl_len; - int conv_index; - char *dest_start = szDest; - - assert(NULL != szSrc); - assert(NULL != szDest); - - /* only process if destination buffer not 0-length */ - if (maxlen > 0) { - - while ((maxlen > 0) && (*szSrc != '\0')) { - conv_index = get_index(*szSrc); - if (-1 != conv_index) { - if (maxlen > (repl_len = strlen(CU_bindings[conv_index].replacement))) { - memcpy(szDest, CU_bindings[conv_index].replacement, repl_len); - szDest += repl_len; - maxlen -= repl_len; - ++count; - } else { - maxlen = 0; /* ran out of room - abort conversion */ - break; - } - } else { - *szDest++ = *szSrc; - --maxlen; - } - ++szSrc; - } - - if (0 == maxlen) { - *dest_start = '\0'; /* ran out of room - return empty string in szDest */ - count = 0; - } else { - *szDest = '\0'; /* had room - make sure szDest has a terminating \0 */ - } - } - return count; -} - -/*------------------------------------------------------------------------*/ -size_t CU_translated_strlen(const char* szSrc) -{ - size_t count = 0; - int conv_index; - - assert(NULL != szSrc); - - while (*szSrc != '\0') { - if (-1 != (conv_index = get_index(*szSrc))) { - count += strlen(CU_bindings[conv_index].replacement); - } else { - ++count; - } - ++szSrc; - } - return count; -} - -/*------------------------------------------------------------------------*/ -int CU_compare_strings(const char* szSrc, const char* szDest) -{ - assert(NULL != szSrc); - assert(NULL != szDest); - - while (('\0' != *szSrc) && ('\0' != *szDest) && (toupper(*szSrc) == toupper(*szDest))) { - szSrc++; - szDest++; - } - - return (int)(*szSrc - *szDest); -} - -/*------------------------------------------------------------------------*/ -void CU_trim(char* szString) -{ - CU_trim_left(szString); - CU_trim_right(szString); -} - -/*------------------------------------------------------------------------*/ -void CU_trim_left(char* szString) -{ - int nOffset = 0; - char* szSrc = szString; - char* szDest = szString; - - assert(NULL != szString); - - /* Scan for the spaces in the starting of string. */ - for (; '\0' != *szSrc; szSrc++, nOffset++) { - if (!isspace(*szSrc)) { - break; - } - } - - for(; (0 != nOffset) && ('\0' != (*szDest = *szSrc)); szSrc++, szDest++) { - ; - } -} - -/*------------------------------------------------------------------------*/ -void CU_trim_right(char* szString) -{ - size_t nLength; - char* szSrc = szString; - - assert(NULL != szString); - nLength = strlen(szString); - /* - * Scan for specs in the end of string. - */ - for (; (0 != nLength) && isspace(*(szSrc + nLength - 1)); nLength--) { - ; - } - - *(szSrc + nLength) = '\0'; -} - -/*------------------------------------------------------------------------*/ -size_t CU_number_width(int number) -{ - char buf[33]; - - snprintf(buf, 33, "%d", number); - buf[32] = '\0'; - return (strlen(buf)); -} - -/** @} */ - -#ifdef CUNIT_BUILD_TESTS -#include "test_cunit.h" - -/* Keep BUF_LEN even or trouble ensues below... */ -#define BUF_LEN 1000 -#define MAX_LEN BUF_LEN/2 - -static void test_CU_translate_special_characters(void) -{ - char dest_buf[BUF_LEN]; - char *dest = dest_buf + MAX_LEN; - char ref_buf[BUF_LEN]; - const int mask_char = 0x01; /* char written to buffer */ - - /* set up reference buffer for testing of translated strings */ - memset(ref_buf, mask_char, BUF_LEN); - - /* empty src */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); - - /* 1 char src */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("#", dest, 0)); - TEST(!strncmp(dest_buf, ref_buf, BUF_LEN)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("#", dest, 1)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("&", dest, 2)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("&", dest, 4)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+4), ref_buf, MAX_LEN-4)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("&", dest, 5)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(1 == CU_translate_special_characters("&", dest, 6)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "&\0", 6)); - TEST(!strncmp((dest+6), ref_buf, MAX_LEN-6)); - - /* maxlen=0 */ - memset(dest_buf, mask_char, BUF_LEN); - strcpy(dest, "random initialized string"); - TEST(0 == CU_translate_special_characters("some <", dest, 0)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strcmp(dest, "random initialized string")); - TEST(!strncmp(dest+strlen(dest)+1, ref_buf, MAX_LEN-strlen(dest)-1)); - - /* maxlen < len(converted szSrc) */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 1)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+1), ref_buf, MAX_LEN-1)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 2)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+2), ref_buf, MAX_LEN-2)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 5)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+5), ref_buf, MAX_LEN-5)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 10)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+10), ref_buf, MAX_LEN-10)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 20)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+20), ref_buf, MAX_LEN-20)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 24)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+24), ref_buf, MAX_LEN-24)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 25)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+25), ref_buf, MAX_LEN-25)); - - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some <", dest, 37)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "\0", 1)); - TEST(!strncmp((dest+37), ref_buf, MAX_LEN-37)); - - /* maxlen > len(converted szSrc) */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(4 == CU_translate_special_characters("some <", dest, 38)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "some <<string & another>\0", 38)); - TEST(!strncmp((dest+38), ref_buf, MAX_LEN-38)); - - /* maxlen > len(converted szSrc) */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(4 == CU_translate_special_characters("some <", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "some <<string & another>\0", 38)); - - /* no special characters */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(0 == CU_translate_special_characters("some string or another", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strncmp(dest, "some string or another\0", 23)); - - /* only special characters */ - memset(dest_buf, mask_char, BUF_LEN); - TEST(11 == CU_translate_special_characters("<><><<>>&&&", dest, MAX_LEN)); - TEST(!strncmp(dest_buf, ref_buf, MAX_LEN)); - TEST(!strcmp(dest, "<><><<>>&&&")); -} - -static void test_CU_translated_strlen(void) -{ - /* empty src */ - TEST(0 == CU_translated_strlen("")); - - /* 1 char src */ - TEST(1 == CU_translated_strlen("#")); - TEST(5 == CU_translated_strlen("&")); - TEST(4 == CU_translated_strlen("<")); - TEST(4 == CU_translated_strlen(">")); - TEST(1 == CU_translated_strlen("?")); - - /* 2 char src */ - TEST(2 == CU_translated_strlen("#@")); - TEST(10 == CU_translated_strlen("&&")); - TEST(9 == CU_translated_strlen(">&")); - - /* longer src */ - TEST(37 == CU_translated_strlen("some <")); - TEST(22 == CU_translated_strlen("some string or another")); - TEST(47 == CU_translated_strlen("<><><<>>&&&")); -} - -static void test_CU_compare_strings(void) -{ - TEST(0 == CU_compare_strings("","")); - TEST(0 == CU_compare_strings("@","@")); - TEST(0 == CU_compare_strings("D","d")); - TEST(0 == CU_compare_strings("s1","s1")); - TEST(0 == CU_compare_strings("s1","S1")); - TEST(0 != CU_compare_strings("s1","s12")); - TEST(0 == CU_compare_strings("this is string 1","tHIS iS sTRING 1")); - TEST(0 == CU_compare_strings("i have \t a tab!","I have \t a tab!")); - TEST(0 != CU_compare_strings("not the same"," not the same")); -} - -static void test_CU_trim(void) -{ - char string[MAX_LEN]; - - strcpy(string, ""); - CU_trim(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim(string); - TEST(!strcmp("", string)); - - strcpy(string, " b"); - CU_trim(string); - TEST(!strcmp("b", string)); - - strcpy(string, " B"); - CU_trim(string); - TEST(!strcmp("B", string)); - - strcpy(string, "s "); - CU_trim(string); - TEST(!strcmp("s", string)); - - strcpy(string, "S "); - CU_trim(string); - TEST(!strcmp("S", string)); - - strcpy(string, " 5 "); - CU_trim(string); - TEST(!strcmp("5", string)); - - strcpy(string, "~ & ^ ( ^ "); - CU_trim(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^"); - CU_trim(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^ "); - CU_trim(string); - TEST(!strcmp("~ & ^ ( ^", string)); -} - -static void test_CU_trim_left(void) -{ - char string[MAX_LEN]; - - strcpy(string, ""); - CU_trim_left(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_left(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_left(string); - TEST(!strcmp("", string)); - - strcpy(string, " b"); - CU_trim_left(string); - TEST(!strcmp("b", string)); - - strcpy(string, " B"); - CU_trim_left(string); - TEST(!strcmp("B", string)); - - strcpy(string, "s "); - CU_trim_left(string); - TEST(!strcmp("s ", string)); - - strcpy(string, "S "); - CU_trim_left(string); - TEST(!strcmp("S ", string)); - - strcpy(string, " 5 "); - CU_trim_left(string); - TEST(!strcmp("5 ", string)); - - strcpy(string, "~ & ^ ( ^ "); - CU_trim_left(string); - TEST(!strcmp("~ & ^ ( ^ ", string)); - - strcpy(string, " ~ & ^ ( ^"); - CU_trim_left(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^ "); - CU_trim_left(string); - TEST(!strcmp("~ & ^ ( ^ ", string)); -} - -static void test_CU_trim_right(void) -{ - char string[MAX_LEN]; - - strcpy(string, ""); - CU_trim_right(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_right(string); - TEST(!strcmp("", string)); - - strcpy(string, " "); - CU_trim_right(string); - TEST(!strcmp("", string)); - - strcpy(string, " b"); - CU_trim_right(string); - TEST(!strcmp(" b", string)); - - strcpy(string, " B"); - CU_trim_right(string); - TEST(!strcmp(" B", string)); - - strcpy(string, "s "); - CU_trim_right(string); - TEST(!strcmp("s", string)); - - strcpy(string, "S "); - CU_trim_right(string); - TEST(!strcmp("S", string)); - - strcpy(string, " 5 "); - CU_trim_right(string); - TEST(!strcmp(" 5", string)); - - strcpy(string, "~ & ^ ( ^ "); - CU_trim_right(string); - TEST(!strcmp("~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^"); - CU_trim_right(string); - TEST(!strcmp(" ~ & ^ ( ^", string)); - - strcpy(string, " ~ & ^ ( ^ "); - CU_trim_right(string); - TEST(!strcmp(" ~ & ^ ( ^", string)); -} - -static void test_CU_number_width(void) -{ - TEST(1 == CU_number_width(0)); - TEST(1 == CU_number_width(1)); - TEST(2 == CU_number_width(-1)); - TEST(4 == CU_number_width(2346)); - TEST(7 == CU_number_width(-257265)); - TEST(9 == CU_number_width(245723572)); - TEST(9 == CU_number_width(-45622572)); -} - -void test_cunit_Util(void) -{ - - test_cunit_start_tests("Util.c"); - - test_CU_translate_special_characters(); - test_CU_translated_strlen(); - test_CU_compare_strings(); - test_CU_trim(); - test_CU_trim_left(); - test_CU_trim_right(); - test_CU_number_width(); - - test_cunit_end_tests(); -} - -#endif /* CUNIT_BUILD_TESTS */ diff --git a/gearsyncd/tests/cunit/Util.h b/gearsyncd/tests/cunit/Util.h deleted file mode 100644 index c03085b45b..0000000000 --- a/gearsyncd/tests/cunit/Util.h +++ /dev/null @@ -1,158 +0,0 @@ -/* - * CUnit - A Unit testing framework library for C. - * Copyright (C) 2001 Anil Kumar - * Copyright (C) 2004-2006 Anil Kumar, Jerry St.Clair - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Library General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Contains Type Definitions for some generic functions used across - * CUnit project files. - * - * 13/Oct/2001 Moved some of the generic functions declarations from - * other files to this one so as to use the functions - * consitently. This file is not included in the distribution - * headers because it is used internally by CUnit. (AK) - * - * 20-Jul-2004 New interface, support for deprecated version 1 names. (JDS) - * - * 5-Sep-2004 Added internal test interface. (JDS) - * - * 17-Apr-2006 Added CU_translated_strlen() and CU_number_width(). - * Removed CUNIT_MAX_STRING_LENGTH - dangerous since not enforced. - * Fixed off-by-1 error in CU_translate_special_characters(), - * modifying implementation & results in some cases. User can - * now tell if conversion failed. (JDS) - */ - -/** @file - * Utility functions (user interface). - */ -/** @addtogroup Framework - * @{ - */ - -#ifndef CUNIT_UTIL_H_SEEN -#define CUNIT_UTIL_H_SEEN - -#include "CUnit.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define CUNIT_MAX_ENTITY_LEN 5 -/**< Maximum number of characters in a translated xml entity. */ - -CU_EXPORT size_t CU_translate_special_characters(const char *szSrc, char *szDest, size_t maxlen); -/**< - * Converts special characters in szSrc to xml entity codes and stores - * result in szDest. Currently conversion of '&', '<', and '>' is supported. - * Note that conversion to entities increases the length of the converted - * string. The greatest conversion size increase would be a string - * consisting entirely of entity characters of converted length - * CUNIT_MAX_ENTITY_LEN. Neither szSrc nor szDest may be NULL - * (checked by assertion).

- * - * maxlen gives the maximum number of characters in the translated string. - * If szDest does not have enough room to hold the converted string, the - * return value will be zero and szDest will contain an empty string. - * If this occurs, the remaining characters in szDest may be overwritten - * in an unspecified manner. It is the caller's responsibility to make - * sure there is sufficient room in szDest to hold the converted string. - * CU_translated_strlen() may be used to calculate the length of buffer - * required (remember to add 1 for the terminating \0). - * - * @param szSrc Source string to convert (non-NULL). - * @param szDest Location to hold the converted string (non-NULL). - * @param maxlen Maximum number of characters szDest can hold. - * @return The number of special characters converted (always 0 if - * szDest did not have enough room to hold converted string). - */ - -CU_EXPORT size_t CU_translated_strlen(const char *szSrc); -/**< - * Calculates the length of a translated string. - * This function calculates the buffer length required to hold a string - * after processing with CU_translate_special_characters(). The returned - * length does not include space for the terminating '\0' character. - * szSrc may not be NULL (checked by assertion). - * - * @param szSrc Source string to analyze (non-NULL). - * @return The number of characters szSrc will expand to when converted. - */ - -CU_EXPORT int CU_compare_strings(const char *szSrc, const char *szDest); -/**< - * Case-insensitive string comparison. Neither string pointer - * can be NULL (checked by assertion). - * - * @param szSrc 1st string to compare (non-NULL). - * @param szDest 2nd string to compare (non-NULL). - * @return 0 if the strings are equal, non-zero otherwise. - */ - -CU_EXPORT void CU_trim_left(char *szString); -/**< - * Trims leading whitespace from the specified string. - * @param szString The string to trim. - */ - -CU_EXPORT void CU_trim_right(char *szString); -/**< - * Trims trailing whitespace from the specified string. - * @param szString The string to trim. - */ - -CU_EXPORT void CU_trim(char *szString); -/**< - * Trims leading and trailing whitespace from the specified string. - * @param szString The string to trim. - */ - -CU_EXPORT size_t CU_number_width(int number); -/**< - * Calulates the number of places required to display - * number in decimal. - */ - -#ifdef CUNIT_BUILD_TESTS -void test_cunit_Util(void); -#endif - -#ifdef __cplusplus -} -#endif - -#ifdef USE_DEPRECATED_CUNIT_NAMES -#define CUNIT_MAX_STRING_LENGTH 1024 -/**< Maximum string length. */ -#define translate_special_characters(src, dest, len) CU_translate_special_characters(src, dest, len) -/**< Deprecated (version 1). @deprecated Use CU_translate_special_characters(). */ -#define compare_strings(src, dest) CU_compare_strings(src, dest) -/**< Deprecated (version 1). @deprecated Use CU_compare_strings(). */ - -#define trim_left(str) CU_trim_left(str) -/**< Deprecated (version 1). @deprecated Use CU_trim_left(). */ -#define trim_right(str) CU_trim_right(str) -/**< Deprecated (version 1). @deprecated Use CU_trim_right(). */ -#define trim(str) CU_trim(str) -/**< Deprecated (version 1). @deprecated Use CU_trim(). */ - -#endif /* USE_DEPRECATED_CUNIT_NAMES */ - -#endif /* CUNIT_UTIL_H_SEEN */ -/** @} */ From 7fa6cb02dff9d9dd83d4f08e2dd81f4dd4da05ef Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Wed, 1 Jul 2020 12:17:57 -0700 Subject: [PATCH 8/9] clean up tabs in a few files --- gearsyncd/phyparser.cpp | 40 ++++++++++++++++++++-------------------- lib/gearboxutils.h | 22 +++++++++++----------- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/gearsyncd/phyparser.cpp b/gearsyncd/phyparser.cpp index eb93bd6584..69ddcc0264 100644 --- a/gearsyncd/phyparser.cpp +++ b/gearsyncd/phyparser.cpp @@ -232,8 +232,8 @@ bool PhyParser::parse() val = port["system_auto_neg"]; if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; + SWSS_LOG_ERROR("not a boolean: 'system_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; } std::string systemAutoNegStr = val.get() == true ? "true" : "false"; attr = std::make_pair("system_auto_neg", systemAutoNegStr); @@ -258,8 +258,8 @@ bool PhyParser::parse() val = port["system_training"]; if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'system_training' field of item %d of 'ports' field in phy configuration", iter); - return false; + SWSS_LOG_ERROR("not a boolean: 'system_training' field of item %d of 'ports' field in phy configuration", iter); + return false; } std::string systemTrainingStr = val.get() == true ? "true" : "false"; attr = std::make_pair("system_training", systemTrainingStr); @@ -294,8 +294,8 @@ bool PhyParser::parse() val = port["line_auto_neg"]; if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; + SWSS_LOG_ERROR("not a boolean: 'line_auto_neg' field of item %d of 'ports' field in phy configuration", iter); + return false; } std::string lineAutoNegStr = val.get() == true ? "true" : "false"; attr = std::make_pair("line_auto_neg", lineAutoNegStr); @@ -340,8 +340,8 @@ bool PhyParser::parse() val = port["line_training"]; if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'line_training' field of item %d of 'ports' field in phy configuration", iter); - return false; + SWSS_LOG_ERROR("not a boolean: 'line_training' field of item %d of 'ports' field in phy configuration", iter); + return false; } std::string lineTrainingStr = val.get() == true ? "true" : "false"; attr = std::make_pair("line_training", lineTrainingStr); @@ -357,12 +357,12 @@ bool PhyParser::parse() valsStr = ""; for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) { - val = vals[iter1]; - if (valsStr.length() > 0) + val = vals[iter1]; + if (valsStr.length() > 0) { - valsStr += ","; + valsStr += ","; } - valsStr += std::to_string(val.get()); + valsStr += std::to_string(val.get()); } attr = std::make_pair("line_adver_speed", valsStr); attrs.push_back(attr); @@ -377,12 +377,12 @@ bool PhyParser::parse() valsStr = ""; for (uint32_t iter1 = 0; iter1 < vals.size(); iter1++) { - val = vals[iter1]; - if (valsStr.length() > 0) + val = vals[iter1]; + if (valsStr.length() > 0) { - valsStr += ","; - } - valsStr += std::to_string(val.get()); + valsStr += ","; + } + valsStr += std::to_string(val.get()); } attr = std::make_pair("line_adver_fec", valsStr); attrs.push_back(attr); @@ -397,7 +397,7 @@ bool PhyParser::parse() if (val.get() != false && val.get() != true) { SWSS_LOG_ERROR("not a boolean: 'line_adver_auto_neg' field of item %d of 'ports' field in phy configuration", iter); - return false; + return false; } std::string lineAdverAutoNegStr = val.get() == true ? "true" : "false"; attr = std::make_pair("line_adver_auto_neg", lineAdverAutoNegStr); @@ -412,8 +412,8 @@ bool PhyParser::parse() val = port["line_adver_asym_pause"]; if (val.get() != false && val.get() != true) { - SWSS_LOG_ERROR("not a boolean: 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); - return false; + SWSS_LOG_ERROR("not a boolean: 'line_adver_asym_pause' field of item %d of 'ports' field in phy configuration", iter); + return false; } std::string lineAdverAsymPauseStr = val.get() == true ? "true" : "false"; attr = std::make_pair("line_adver_asym_pause", lineAdverAsymPauseStr); diff --git a/lib/gearboxutils.h b/lib/gearboxutils.h index e8ffc85c96..95e362d20e 100644 --- a/lib/gearboxutils.h +++ b/lib/gearboxutils.h @@ -70,22 +70,22 @@ typedef struct int index; std::string mdio_addr; int system_speed; - std::string system_fec; + std::string system_fec; bool system_auto_neg; - std::string system_loopback; - bool system_training; + std::string system_loopback; + bool system_training; int line_speed; - std::string line_fec; + std::string line_fec; bool line_auto_neg; - std::string line_media_type; - std::string line_intf_type; - std::string line_loopback; - bool line_training; + std::string line_media_type; + std::string line_intf_type; + std::string line_loopback; + bool line_training; std::set line_adver_speed; std::set line_adver_fec; - bool line_adver_auto_neg; - bool line_adver_asym_pause; - std::string line_adver_media_type; + bool line_adver_auto_neg; + bool line_adver_asym_pause; + std::string line_adver_media_type; } gearbox_port_t; class GearboxUtils From 44da1155abca9b50dd2a021faa53cbb3953b3604 Mon Sep 17 00:00:00 2001 From: Syd Logan Date: Thu, 2 Jul 2020 14:15:54 -0700 Subject: [PATCH 9/9] return SAI_STATUS_NOT_IMPLEMENTED for mdio_read/write --- orchagent/saihelper.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/orchagent/saihelper.cpp b/orchagent/saihelper.cpp index 67a67223cc..a776ca701d 100644 --- a/orchagent/saihelper.cpp +++ b/orchagent/saihelper.cpp @@ -71,14 +71,14 @@ sai_status_t mdio_read(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, uint32_t number_of_registers, uint32_t *data) { - return SAI_STATUS_FAILURE; + return SAI_STATUS_NOT_IMPLEMENTED; } sai_status_t mdio_write(uint64_t platform_context, uint32_t mdio_addr, uint32_t reg_addr, uint32_t number_of_registers, uint32_t *data) { - return SAI_STATUS_FAILURE; + return SAI_STATUS_NOT_IMPLEMENTED; } const char *test_profile_get_value (