From 252601302d45036817546c533743e5918b6b86e8 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 5 May 2024 19:38:31 +0200 Subject: Adding upstream version 1.21.3. Signed-off-by: Daniel Baumann --- testenv/misc/__init__.py | 0 testenv/misc/colour_terminal.py | 46 ++++++ testenv/misc/metalinkv3_xml.py | 305 ++++++++++++++++++++++++++++++++++++++++ testenv/misc/wget_file.py | 16 +++ 4 files changed, 367 insertions(+) create mode 100644 testenv/misc/__init__.py create mode 100644 testenv/misc/colour_terminal.py create mode 100644 testenv/misc/metalinkv3_xml.py create mode 100644 testenv/misc/wget_file.py (limited to 'testenv/misc') diff --git a/testenv/misc/__init__.py b/testenv/misc/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/testenv/misc/colour_terminal.py b/testenv/misc/colour_terminal.py new file mode 100644 index 0000000..bc549a2 --- /dev/null +++ b/testenv/misc/colour_terminal.py @@ -0,0 +1,46 @@ +from functools import partial +import platform +from os import getenv +import sys + +""" This module allows printing coloured output to the terminal when running a +Wget Test under certain conditions. +The output is coloured only on Linux systems. This is because coloured output +in the terminal on Windows requires too much effort for what is simply a +convenience. This might work on OSX terminals, but without a confirmation, it +remains unsupported. + +Another important aspect is that the coloured output is printed only if the +environment variable MAKE_CHECK is not set. This variable is set when running +the test suite through, `make check`. In that case, the output is not only +printed to the terminal but also copied to a log file where the ANSI escape +codes on;y add clutter. """ + + +T_COLORS = { + 'PURPLE': '\033[95m', + 'BLUE': '\033[94m', + 'GREEN': '\033[92m', + 'YELLOW': '\033[93m', + 'RED': '\033[91m', + 'ENDC': '\033[0m' +} + +system = True if platform.system() in ('Linux', 'Darwin') else False +check = False if getenv("MAKE_CHECK") == 'True' else True + + +def printer(color, string): + if sys.stdout.isatty() and system and check: + print(T_COLORS.get(color) + string + T_COLORS.get('ENDC')) + else: + print(string) + + +print_blue = partial(printer, 'BLUE') +print_red = partial(printer, 'RED') +print_green = partial(printer, 'GREEN') +print_purple = partial(printer, 'PURPLE') +print_yellow = partial(printer, 'YELLOW') + +# vim: set ts=8 sw=3 tw=80 et : diff --git a/testenv/misc/metalinkv3_xml.py b/testenv/misc/metalinkv3_xml.py new file mode 100644 index 0000000..794bf87 --- /dev/null +++ b/testenv/misc/metalinkv3_xml.py @@ -0,0 +1,305 @@ +from test.http_test import HTTPTest +from misc.wget_file import WgetFile +import hashlib + +class Metalinkv3_XML: + + """ Metalink/XML v3 object """ + + # Initialize the Metalink object + def __init__ (self): + self.reset () + + # Reset the Metalink object + def reset (self): + self.LocalFiles = [] # list of WgetFile objects + self.ServerFiles = [[]] # list of WgetFile objects + self.ExpectedFiles = [] # list of WgetFile objects + self.LocalFiles_Set = [] # used as `list (set (var))` + self.ServerFiles_Set = [[]] # used as `list (set (var))` + self.ExpectedFiles_Set = [] # used as `list (set (var))` + self.Xml = '' # Metalink/XML content + self.XmlName = '' # Metalink/XML file name + self.XmlFile = None # Metalink/XML WgetFile object + self.Xml_Header = '\n' + \ + '\n' + \ + ' \n' + \ + ' GNU Wget\n' + \ + ' \n' + \ + ' \n' + \ + ' GNU GPL\n' + \ + ' http://www.gnu.org/licenses/gpl.html\n' + \ + ' \n' + \ + ' Wget Test Files\n' + \ + ' 1.2.3\n' + \ + ' Wget Test Files description\n' + \ + ' \n' + self.Xml_Footer = ' \n' + \ + '\n' + + # Print the Metalink object. + def print_meta (self): + + print (self.Xml) + print ("LocalFiles = " + str (self.LocalFiles_Set)) + print ("ServerFiles = " + str (self.ServerFiles_Set)) + print ("ExpectedFiles = " + str (self.ExpectedFiles_Set)) + + # Add LocalFiles as WgetFile objects + # + # ["file_name", "content"], + # ["file_name", "content"] + def add_LocalFiles (self, *local_files): + + for (file_name, content) in local_files: + if not file_name in self.LocalFiles_Set: + self.LocalFiles_Set.append (file_name) + self.LocalFiles.append (WgetFile (file_name, content)) + + # Add ServerFiles as WgetFile objects + # + # ["file_name", "content"], + # ["file_name", "content"] + def add_ServerFiles (self, *server_files): + + for (file_name, content) in server_files: + if not file_name in self.ServerFiles_Set[0]: + self.ServerFiles_Set[0].append (file_name) + self.ServerFiles[0].append (WgetFile (file_name, content)) + + # Add ExpectedFiles as WgetFile objects + # + # ["file_name", "content"], + # ["file_name", "content"] + def add_ExpectedFiles (self, *expected_files): + + for (file_name, content) in expected_files: + if not file_name in self.ExpectedFiles_Set: + self.ExpectedFiles_Set.append (file_name) + self.ExpectedFiles.append (WgetFile (file_name, content)) + + # Run a Wget HTTP test for the Metalink object. + def http_test (self, command_line, expected_retcode): + + pre_test = { + "ServerFiles" : self.ServerFiles, # list of WgetFile objects as [[]] + "LocalFiles" : self.LocalFiles, # list of WgetFile objects as [] + } + + test_options = { + "WgetCommands" : command_line, # Wget cli + "Urls" : [[]], # Wget urls + } + + post_test = { + "ExpectedFiles" : self.ExpectedFiles, # list of WgetFile objects as [] + "ExpectedRetcode" : expected_retcode, # Wget return status code + } + + http_test = HTTPTest ( + pre_hook=pre_test, + test_params=test_options, + post_hook=post_test, + ) + + http_test.server_setup() + # Get and use dynamic server sockname + srv_host, srv_port = http_test.servers[0].server_inst.socket.getsockname () + + self.set_srv (srv_host, srv_port) + + err = http_test.begin () + + return err + + # Set the Wget server host and port in the Metalink/XML content. + def set_srv (self, srv_host, srv_port): + + self.Xml = self.Xml.replace('{{SRV_HOST}}', srv_host) + self.Xml = self.Xml.replace('{{SRV_PORT}}', str (srv_port)) + + if self.XmlFile is not None: + self.XmlFile.content = self.Xml + + # Create the Metalink/XML file. + # + # Add the Metalink/XML file to the list of ExpectedFiles. + # + # size: + # True auto-compute size + # None no + # any use this size + # + # hash_sha256: + # False no + # True auto-compute sha256 + # None no + # any use this hash + # + # ARGUMENTS: + # + # "xml_name", # Metalink/XML file name + # ["file_name", "save_name", "content", size, hash_sha256, # metalink:file + # ["srv_file", "srv_content", utype, location, preference], # resource + # ["srv_file", "srv_content", utype, location, preference]], # resource + # ["file_name", "save_name", "content", size, hash_sha256, + # ["srv_file", "srv_content", utype, location, preference], + # ["srv_file", "srv_content", utype, location, preference]] + def xml (self, xml_name, *xml_data): + + self.Xml = self.Xml_Header + + for (file_name, save_name, content, size, hash_sha256, *resources) in xml_data: + self.Xml += self.file_tag (file_name, save_name, content, size, hash_sha256, resources) + '\n' + + self.Xml += self.Xml_Footer + + self.XmlName = xml_name + self.XmlFile = WgetFile (xml_name, self.Xml) + + if not xml_name in self.LocalFiles_Set: + self.LocalFiles_Set.append (xml_name) + self.LocalFiles.append (self.XmlFile) + + if not xml_name in self.ExpectedFiles_Set: + self.ExpectedFiles_Set.append (xml_name) + self.ExpectedFiles.append (self.XmlFile) + + # Create the file tag. + # + # Add the file to be saved to the list of ExpectedFiles. + # + # size: + # True auto-compute size + # None no + # any use this size + # + # hash_sha256: + # False no + # True auto-compute sha256 + # None no + # any use this hash + # + # ARGUMENTS: + # + # ["file_name", "save_name", "content", size, hash_sha256, # metalink:file + # ["srv_file", "srv_content", utype, location, preference], # resource + # ["srv_file", "srv_content", utype, location, preference]] # resource + def file_tag (self, file_name, save_name, content, size, hash_sha256, resources): + + Tag = ' \n' + + if save_name is not None: + self.add_ExpectedFiles ([save_name, content]) + + size_Tag = self.size_tag (content, size) + + if size_Tag is not None: + Tag += size_Tag + '\n' + + verification_Tag = self.verification_tag (content, hash_sha256) + + if verification_Tag is not None: + Tag += verification_Tag + '\n' + + Tag += self.resources_tag (resources) + '\n' + + Tag += ' ' + + return Tag + + # Create the size tag. + # + # size: + # True auto-compute size + # None no + # any use this size + # + # ARGUMENTS: + # + # "content", size + def size_tag (self, content = None, size = None): + + Tag = None + + if content is not None and size is True: + size = len (content) + + if size is not None: + Tag = ' ' + str (size) + '' + + return Tag + + # Create the verification tag. + # + # hash_sha256: + # False no + # True auto-compute sha256 + # None no + # any use this hash + # + # ARGUMENTS: + # + # "content", hash_sha256 + def verification_tag (self, content = None, hash_sha256 = None): + + Tag = None + + if hash_sha256 is not False: + + if content is not None and hash_sha256 is True: + hash_sha256 = hashlib.sha256 (content.encode ('UTF-8')).hexdigest () + + if hash_sha256 is None: + Tag = ' \n' + \ + ' ' + else: + Tag = ' \n' + \ + ' ' + str (hash_sha256) + '\n' + \ + ' ' + + return Tag + + # Create the resources tag. + # + # ARGUMENTS: + # + # ["srv_file", "srv_content", utype, location, preference], # resource + # ["srv_file", "srv_content", utype, location, preference] # resource + def resources_tag (self, resources): + + Tag = ' \n' + + for (srv_file, srv_content, utype, location, preference) in resources: + Tag += self.url_tag (srv_file, srv_content, utype, location, preference) + '\n' + + Tag += ' ' + + return Tag + + # Create the url tag. + # + # Add the file to the list of Files when there is a content. + # + # ARGUMENTS: + # + # "srv_file", "srv_content", utype, location, preference # resource + def url_tag (self, srv_file, srv_content = None, utype = "http", location = None, preference = 999999): + + Loc = '' + + if location is not None: + Loc = 'location="' + location + '" ' + + Tag = ' ' + \ + '' + \ + 'http://{{SRV_HOST}}:{{SRV_PORT}}/' + srv_file + \ + '' + + if srv_content is not None: + self.add_ServerFiles ([srv_file, srv_content]) + + return Tag diff --git a/testenv/misc/wget_file.py b/testenv/misc/wget_file.py new file mode 100644 index 0000000..c2a7239 --- /dev/null +++ b/testenv/misc/wget_file.py @@ -0,0 +1,16 @@ + +class WgetFile: + + """ WgetFile is a File Data Container object """ + + def __init__( + self, + name, + content="Test Contents", + timestamp=None, + rules=None + ): + self.name = name + self.content = content + self.timestamp = timestamp + self.rules = rules or {} -- cgit v1.2.3