Esempio n. 1
0
def test_syscheck_files(socket_mock, agent_id, select, filters, distinct):
    """Test function `files` from syscheck module.

    Parameters
    ----------
    agent_id : list
        Agent ID.
    select :
        List of parameters to show from the query.
    filters : dict
        Dict to filter out the result.
    distinct : bool
        True if all response items must be unique
    """
    select_list = [
        'date', 'mtime', 'file', 'size', 'perm', 'uname', 'gname', 'md5',
        'sha1', 'sha256', 'inode', 'gid', 'uid', 'type', 'changes',
        'attributes'
    ]
    with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb:
        mock_wdb.return_value = InitWDBSocketMock(
            sql_schema_file='schema_syscheck_test.sql')
        result = files(agent_id, select=select, filters=filters)
        assert isinstance(result, AffectedItemsWazuhResult)
        assert isinstance(result.affected_items, list)
        select = select if select else select_list
        for item in result.affected_items:
            assert len(select) == len(item.keys())
            assert (param in select for param in item.keys())
        assert not any(
            result.affected_items.count(item) > 1
            for item in result.affected_items) if distinct else True
        if filters:
            for key, value in filters.items():
                assert (item[key] == value for item in result.affected_items)
Esempio n. 2
0
def test_files(select, filters):
    """
    Test files function
    """
    with patch("wazuh.syscheck.Agent._load_info_from_agent_db", return_value=[[{'date':0, 'mtime':0}],1]):
        result = files(select=select, filters=filters)

        assert isinstance(result, dict)
        assert set(result.keys()) == {'totalItems', 'items'}
Esempio n. 3
0
def test_files(mock_socket, select, filters):
    """Test files function."""
    with patch("wazuh.syscheck.WazuhDBBackend.execute",
               return_value=[{
                   'items': [{
                       'date': 0,
                       'mtime': 0
                   }],
                   'totalItems': 1
               }]):
        result = files(select=select, filters=filters)

        assert isinstance(result, dict)
        assert set(result.keys()) == {'totalItems', 'items'}
Esempio n. 4
0
def test_syscheck_files(socket_mock, agent_id, select, filters, distinct):
    """Test function `files` from syscheck module.

    Parameters
    ----------
    agent_id : list
        Agent ID.
    select :
        List of parameters to show from the query.
    filters : dict
        Dict to filter out the result.
    distinct : bool
        True if all response items must be unique
    """
    select_list = [
        'date', 'mtime', 'file', 'size', 'perm', 'uname', 'gname', 'md5',
        'sha1', 'sha256', 'inode', 'gid', 'uid', 'type', 'changes',
        'attributes', 'arch', 'value.name', 'value.type'
    ]
    nested_fields = ['value']

    with patch('wazuh.core.utils.WazuhDBConnection') as mock_wdb:
        mock_wdb.return_value = InitWDBSocketMock(
            sql_schema_file='schema_syscheck_test.sql')
        select = select if select else select_list
        result = files(agent_id, select=select, filters=filters)
        assert isinstance(result, AffectedItemsWazuhResult)
        assert isinstance(result.affected_items, list)
        # Use flag for min_select_field, if file not in select, len(item.keys()) = len(select) + 1
        flag_select_min = 1 if 'file' not in select else 0
        for item in result.affected_items:
            # Use flag for nested_fields in order to compare select and item.keys() lengths
            flag_nested = 0
            for nested_field in nested_fields:
                if nested_field in item.keys():
                    flag_nested += sum(
                        1 for i in select if i.startswith(nested_field)) - 1
            assert len(select) + flag_select_min == len(
                item.keys()) + flag_nested
            assert (param in select for param in item.keys())
        assert not any(
            result.affected_items.count(item) > 1
            for item in result.affected_items) if distinct else True
        if filters:
            for key, value in filters.items():
                assert (item[key] == value for item in result.affected_items)
Esempio n. 5
0
def test_failed_files(mock_socket):
    """Test failed files function when select field isn't valid."""
    with pytest.raises(exception.WazuhException, match=".* 1724 .*"):
        files(select={'fields': ['bad_select']})