Validate storetool document types
Debian Trixie package bundle / packages (push) Successful in 21m56s

This commit is contained in:
Mario Fetka
2026-07-22 13:42:26 +02:00
parent e9a4c00f7c
commit e03287f5d1
3 changed files with 76 additions and 2 deletions
+9
View File
@@ -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()
@@ -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:
@@ -0,0 +1,52 @@
# /****************************************************************************
# * <Novell-copyright>
# * 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.
# * </Novell-copyright>
# ****************************************************************************/
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()