diff --git a/test/libdnf5/iniparser/test_iniparser.cpp b/test/libdnf5/iniparser/test_iniparser.cpp index 4acecd94e..12f66c22d 100644 --- a/test/libdnf5/iniparser/test_iniparser.cpp +++ b/test/libdnf5/iniparser/test_iniparser.cpp @@ -162,3 +162,94 @@ key1 = value1)**"; parse_and_check_results(ini_file_content, expected_items); } + + +void IniparserTest::test_iniparser_missing_section_header() { + const std::string ini_file_content = "# Test comment1\nkey1 = value1"; + + const std::vector expected_items = { + {ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, + {ItemType::KEY_VAL, "", "key1", "value1", "key1 = value1\n"}}; + + CPPUNIT_ASSERT_THROW( + parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingSectionHeaderError); +} + + +void IniparserTest::test_iniparser_missing_bracket() { + const std::string ini_file_content = + R"**( +# Test comment1 +[section1 +key1 = value1 +)**"; + + const std::vector expected_items = { + {ItemType::EMPTY_LINE, "", "", "", "\n"}, + {ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, + {ItemType::SECTION, "section1", "", "", "[section1\n"}}; + + CPPUNIT_ASSERT_THROW( + parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingBracketError); +} + + +void IniparserTest::test_iniparser_empty_section_name() { + const std::string ini_file_content = "# Test comment1\n[] # Test\nkey1 = value1"; + + const std::vector expected_items = { + {ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, {ItemType::SECTION, "", "", "", "[] # Test\n"}}; + + CPPUNIT_ASSERT_THROW( + parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserEmptySectionNameError); +} + + +void IniparserTest::test_iniparser_text_after_section() { + const std::string ini_file_content = "# Test comment1\n[section1] Test\nkey1 = value1"; + + const std::vector expected_items = { + {ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, + {ItemType::SECTION, "section1", "", "", "[section1] Test\n"}}; + + CPPUNIT_ASSERT_THROW( + parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserTextAfterSectionError); +} + + +void IniparserTest::test_iniparser_illegal_continuation_line() { + const std::string ini_file_content = "# Test comment1\n[section1] # Test\n key1 = value1\nkey2 = value2"; + + const std::vector expected_items = { + {ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, + {ItemType::SECTION, "section1", "", "", "[section1] # Test\n"}, + {ItemType::KEY_VAL, "section1", "key1", "value1", " key1 = value1\n"}}; + + CPPUNIT_ASSERT_THROW( + parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserIllegalContinuationLineError); +} + + +void IniparserTest::test_iniparser_missing_key() { + const std::string ini_file_content = "# Test comment1\n[section1] # Test\n= value1\nkey2 = value2"; + + const std::vector expected_items = { + {ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, + {ItemType::SECTION, "section1", "", "", "[section1] # Test\n"}, + {ItemType::KEY_VAL, "section1", "", "value1", "= value1\n"}}; + + CPPUNIT_ASSERT_THROW(parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingKeyError); +} + + +void IniparserTest::test_iniparser_missing_equal() { + const std::string ini_file_content = "# Test comment1\n[section1] # Test\nkey1 \nkey2 = value2"; + + const std::vector expected_items = { + {ItemType::COMMENT_LINE, "", "", "", "# Test comment1\n"}, + {ItemType::SECTION, "section1", "", "", "[section1] # Test\n"}, + {ItemType::KEY_VAL, "section1", "key1", "", "key1 \n"}}; + + CPPUNIT_ASSERT_THROW( + parse_and_check_results(ini_file_content, expected_items), libdnf5::IniParserMissingEqualError); +} diff --git a/test/libdnf5/iniparser/test_iniparser.hpp b/test/libdnf5/iniparser/test_iniparser.hpp index c83d466b2..7bf131acd 100644 --- a/test/libdnf5/iniparser/test_iniparser.hpp +++ b/test/libdnf5/iniparser/test_iniparser.hpp @@ -32,6 +32,13 @@ class IniparserTest : public CppUnit::TestCase { #ifndef WITH_PERFORMANCE_TESTS CPPUNIT_TEST(test_iniparser); CPPUNIT_TEST(test_iniparser2); + CPPUNIT_TEST(test_iniparser_missing_section_header); + CPPUNIT_TEST(test_iniparser_missing_bracket); + CPPUNIT_TEST(test_iniparser_empty_section_name); + CPPUNIT_TEST(test_iniparser_text_after_section); + CPPUNIT_TEST(test_iniparser_illegal_continuation_line); + CPPUNIT_TEST(test_iniparser_missing_key); + CPPUNIT_TEST(test_iniparser_missing_equal); #endif CPPUNIT_TEST_SUITE_END(); @@ -42,6 +49,13 @@ class IniparserTest : public CppUnit::TestCase { void test_iniparser(); void test_iniparser2(); + void test_iniparser_missing_section_header(); + void test_iniparser_missing_bracket(); + void test_iniparser_empty_section_name(); + void test_iniparser_text_after_section(); + void test_iniparser_illegal_continuation_line(); + void test_iniparser_missing_key(); + void test_iniparser_missing_equal(); }; #endif