From e03287f5d1c31a89fa0bc5bacfecf060f7dfa0fb Mon Sep 17 00:00:00 2001 From: Mario Fetka Date: Wed, 22 Jul 2026 13:42:26 +0200 Subject: [PATCH] Validate storetool document types --- src/apps/storetool/CMakeLists.txt | 9 ++++ .../bongo/storetool/StoreCommands.py | 17 +++++- .../storetool/tests/test_store_commands.py | 52 +++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 src/apps/storetool/tests/test_store_commands.py diff --git a/src/apps/storetool/CMakeLists.txt b/src/apps/storetool/CMakeLists.txt index 08e20c5..99b621b 100644 --- a/src/apps/storetool/CMakeLists.txt +++ b/src/apps/storetool/CMakeLists.txt @@ -9,3 +9,12 @@ install(DIRECTORY ./bongo configure_file(${CMAKE_CURRENT_SOURCE_DIR}/bongo-storetool.py ${CMAKE_CURRENT_BINARY_DIR}/bongo-storetool) install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/bongo-storetool DESTINATION ${SBIN_INSTALL_DIR}) + +if(BUILD_TESTING) + add_test(NAME storetool-document-type + COMMAND ${CMAKE_COMMAND} -E env + "PYTHONPATH=${CMAKE_SOURCE_DIR}/src/libs/python:${CMAKE_CURRENT_SOURCE_DIR}" + "PYTHONPYCACHEPREFIX=${CMAKE_CURRENT_BINARY_DIR}/pycache" + ${Python3_EXECUTABLE} -m unittest discover + -s ${CMAKE_CURRENT_SOURCE_DIR}/tests -v) +endif() diff --git a/src/apps/storetool/bongo/storetool/StoreCommands.py b/src/apps/storetool/bongo/storetool/StoreCommands.py index 669ffe0..ae9f49e 100644 --- a/src/apps/storetool/bongo/storetool/StoreCommands.py +++ b/src/apps/storetool/bongo/storetool/StoreCommands.py @@ -9,6 +9,16 @@ from bongo.cmdparse import Command from bongo.BongoError import BongoError from bongo.store.StoreClient import DocTypes, StoreClient + +def ParseDocumentType(value): + try: + document_type = int(value, 0) + except (TypeError, ValueError): + raise ValueError("document type must be an integer") + if document_type < 0: + raise ValueError("document type must not be negative") + return document_type + class DocumentListCommand(Command): log = logging.getLogger("Bongo.StoreTool") @@ -83,9 +93,12 @@ class DocumentPutCommand(Command): document = args[0] filename = args[1] - doc_type = 1 + doc_type = DocTypes.Unknown if len(args) > 2: - doc_type = args[2] + try: + doc_type = ParseDocumentType(args[2]) + except ValueError as error: + self.error(str(error)) docbits = document.split('/') if len(docbits) < 2: diff --git a/src/apps/storetool/tests/test_store_commands.py b/src/apps/storetool/tests/test_store_commands.py new file mode 100644 index 0000000..5f3b9c8 --- /dev/null +++ b/src/apps/storetool/tests/test_store_commands.py @@ -0,0 +1,52 @@ +# /**************************************************************************** +# * +# * Copyright (c) 2001 Novell, Inc. All Rights Reserved. +# * +# * This program is free software; you can redistribute it and/or +# * modify it under the terms of version 2 of the GNU General Public License +# * as published by the Free Software Foundation. +# * +# * This program is distributed in the hope that it will be useful, +# * but WITHOUT ANY WARRANTY; without even the implied warranty of +# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# * GNU General Public License for more details. +# * +# * You should have received a copy of the GNU General Public License +# * along with this program; if not, contact Novell, Inc. +# * +# * To contact Novell about this file by physical or electronic mail, you +# * may find current contact information at www.novell.com. +# * +# ****************************************************************************/ + +import importlib.util +from pathlib import Path +import unittest + + +MODULE_PATH = ( + Path(__file__).resolve().parents[1] + / "bongo" + / "storetool" + / "StoreCommands.py" +) +SPEC = importlib.util.spec_from_file_location("store_commands_under_test", MODULE_PATH) +STORE_COMMANDS = importlib.util.module_from_spec(SPEC) +SPEC.loader.exec_module(STORE_COMMANDS) +ParseDocumentType = STORE_COMMANDS.ParseDocumentType + + +class DocumentTypeTests(unittest.TestCase): + def test_decimal_and_prefixed_types(self): + self.assertEqual(ParseDocumentType("2"), 2) + self.assertEqual(ParseDocumentType("0x1002"), 0x1002) + + def test_invalid_type_is_rejected(self): + with self.assertRaisesRegex(ValueError, "must be an integer"): + ParseDocumentType("mail") + with self.assertRaisesRegex(ValueError, "must not be negative"): + ParseDocumentType("-1") + + +if __name__ == "__main__": + unittest.main()