def __init__(self, workspace=None): """ Initialize the Validator. A workspace may be provided for an easy parameter configuration, such as location and extension of descriptors, verbosity level, etc. :param workspace: SONATA workspace object """ self._workspace = workspace self._syntax = True self._integrity = True self._topology = True # create "virtual" workspace if not provided (don't actually create # file structure) if not self._workspace: self._workspace = Workspace('.', log_level='info') # load configurations from workspace self._dext = self._workspace.default_descriptor_extension self._dpath = '.' self._log_level = self._workspace.log_level # configure logs coloredlogs.install(level=self._log_level) # descriptors storage self._storage = DescriptorStorage() # syntax validation self._schema_validator = SchemaValidator(self._workspace) # reset event logger evtlog.reset()
def test_generate_package(self, m_zipfile, m_join): """ Ensures that a package file is created with correct name and location """ # First, create a workspace to give to Packager workspace = Workspace("ws/root", ws_name="ws_test", log_level='debug') # Create project project = Project(workspace, 'prj/path') # Instantiate a Packager instance packager = Packager(workspace=workspace, project=project, generate_pd=False, dst_path="dst/path") packager._package_descriptor = True # Prepare mocks context_manager_mock = Mock() m_zipfile.ZipFile.return_value = context_manager_mock enter_mock = Mock() exit_mock = Mock() setattr(context_manager_mock, '__enter__', enter_mock) setattr(context_manager_mock, '__exit__', exit_mock) # execute packager.generate_package("package_name") # make assertions self.assertEqual(m_join.call_args_list[-1], mock.call('dst/path', 'package_name.son'))
def __init__(self, *args, **kwargs): super(UnitValidateTests, self).__init__(*args, **kwargs) # tap log functions to count for errors and warnings self._workspace = Workspace('.', log_level='debug') val.log.error = CountCalls(val.log.error) val.log.warning = CountCalls(val.log.warning)
def setUp(self): """ Creates a fresh test workspace in a temp dir to ensure we do not have conflicts with other CLI tests. """ self.tmp_ws_dir = os.path.join(tempfile.mkdtemp(), "son-workspace") ws = Workspace(self.tmp_ws_dir, ws_name="son-profile test workspace") ws.create_dirs() ws.create_files()
def test___init__(self): """ Verify variable assignments in init function :return: """ # Create a new workspace ws = Workspace("workspace/root/dir", ws_name="workspace_name", log_level='log_level') # Verify its properties self.assertEqual(ws.ws_root, "workspace/root/dir") self.assertEqual(ws.ws_name, "workspace_name") self.assertEqual(ws.log_level, "log_level")
def test_create_dirs(self, m_makedirs): """ Verify if the workspace directory structure is being well created. """ # Create a new workspace ws = Workspace("workspace/root/dir", ws_name="workspace_name", log_level='log_level') # Assure directory structure is well created m_makedirs.return_value = None ws.create_dirs() for call in m_makedirs.call_args_list: assert '\'workspace/root/dir' in str(call)
def signature(): from son.access.access import AccessClient from son.workspace.workspace import Workspace from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 ws = Workspace('~/workspace/ws1') ac = AccessClient(ws) key = RSA.generate(2048) # package_path = 'son/access/samples/sonata-demo.son' package_path = '../samples/sonata-demo.son' with open(package_path, 'rb') as fhandle: package_content = fhandle.read() package_hash = SHA256.new(package_content).digest() signature = ac.sign_package(package_path, private_key=key.exportKey().decode('utf-8')) public_key = key.publickey() print(public_key.verify(package_hash, (int(signature), )))
def test_package_gds(self): """ Test the validation of the project general description section """ # First, create a workspace to give to Packager workspace = Workspace("ws/root", ws_name="ws_test", log_level='debug') # Create project project = Project(workspace, 'prj/path') # Instantiate a Packager instance packager = Packager(workspace=workspace, project=project, generate_pd=False, dst_path="dst/path") packager._package_descriptor = True # Create fake project configuration prj_config = { 'version': '0.5', 'package': { 'version': '0.1', 'name': 'sonata-project-sample', 'vendor': 'com.sonata.project', 'maintainer': 'Name, Company, Contact', 'description': 'Project description', }, 'descriptor_extension': 'yml' } # Remove package keys, one by one... for key in prj_config['package']: value = prj_config['package'].pop(key) self.assertIsNone(packager.package_gds(prj_config)) prj_config['package'][key] = value # Make prj_config complete... prj_config['name'] = 'sonata - project - sample' self.assertTrue(packager.package_gds(prj_config))
def test_create_ws_descriptor(self, m_open, m_yaml, m_path): """ Tests the function that generates the workspace configuration file. Verify that a workspace can be created using the file generated by this function. """ # Create a new workspace ws = Workspace("workspace/root/dir", ws_name="workspace_name", log_level='log_level') # Patch file handling functions m_open.return_value = None m_open.write.return_value = None m_yaml.dump.return_value = None # Call function cfg_d = ws.create_ws_descriptor() # Assure that config file would be writen with the correct location configfile = os.path.join(ws.ws_root, Workspace.__descriptor_name__) self.assertEqual(m_open.call_args, mock.call(configfile, 'w')) # Make sure the workspace root dir and # config file exist by patching os.path m_path.isdir.return_value = True m_path.isfile.return_value = True # Patch yaml.load to return the previously obtained configuration m_yaml.load.return_value = cfg_d # Call function new_ws = Workspace.__create_from_descriptor__(ws.ws_root) # Assert returned workspace configuration is equal to the previous self.assertEqual(ws, new_ws)
def __init__(self, *args, **kwargs): super(IntPDTester, self).__init__(*args, **kwargs) ws = Workspace("") prj = Project(ws, '/') self.pck = Packager(workspace=ws, project=prj, generate_pd=False)
from son.access.access import AccessClient from son.workspace.workspace import Workspace from Crypto.PublicKey import RSA from Crypto.Hash import SHA256 ws = Workspace('~/workspace/ws1') ac = AccessClient(ws) key = RSA.generate(2048) package_path = 'son/access/samples/sonata-demo.son' with open(package_path, 'rb') as fhandle: package_content = fhandle.read() package_hash = SHA256.new(package_content).digest() signature = ac.sign_package(package_path, private_key=key.exportKey().decode('utf-8')) public_key = key.publickey() print(public_key.verify(package_hash, (int(signature), )))