buildframework/helium/tools/common/python/lib/cpythontest/test_policy_validator.py
author wbernard
Wed, 23 Dec 2009 19:29:07 +0200
changeset 179 d8ac696cc51f
permissions -rw-r--r--
helium_7.0-r14027
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
179
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     1
#============================================================================ 
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     2
#Name        : test_policy_validator.py 
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     3
#Part of     : Helium 
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     4
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     5
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     6
#All rights reserved.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     7
#This component and the accompanying materials are made available
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     8
#under the terms of the License "Eclipse Public License v1.0"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
     9
#which accompanies this distribution, and is available
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    10
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    11
#
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    12
#Initial Contributors:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    13
#Nokia Corporation - initial contribution.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    14
#
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    15
#Contributors:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    16
#
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    17
#Description:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    18
#===============================================================================
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    19
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    20
"""Test the integration.quality module."""
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    21
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    22
import os
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    23
import unittest
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    24
import sys
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    25
import time
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    26
import shutil
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    27
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    28
import integration.quality
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    29
import logging
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    30
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    31
_logger = logging.getLogger('test.archive')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    32
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    33
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    34
_test_file_paths = [
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    35
    's60/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    36
    's60/ADO/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    37
    's60/ADO/group/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    38
    's60/ADO/internal/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    39
    's60/ADO/internal/good/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    40
    's60/ADO/internal/bad/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    41
    's60/ADO/tsrc/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    42
    's60/ADO/tsrc/group/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    43
    's60/ADO/tsrc/private/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    44
    's60/component_public/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    45
    's60/component_public/component_public_file.txt',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    46
    's60/component_private/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    47
    's60/component_private/component_private_file.txt',    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    48
    's60/missing/to_be_removed_9999.txt',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    49
    's60/missing/subdir/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    50
    's60/missing/subdir/to_be_removed_9999.txt',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    51
    's60/invalid/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    52
    's60/invalid/comment/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    53
    's60/invalid/utf16/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    54
    's60/invalid/letter/Distribution.Policy.S60',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    55
    's60/new_rules/',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    56
    's60/new_rules/subdir1/invalid.txt',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    57
    's60/new_rules/subdir2/',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    58
    ]
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    59
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    60
_test_file_content = {
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    61
    's60/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    62
    's60/missing/subdir/Distribution.Policy.S60' : '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    63
    's60/component_public/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    64
    's60/component_private/Distribution.Policy.S60': '1',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    65
    's60/ADO/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    66
    's60/ADO/group/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    67
    's60/ADO/internal/Distribution.Policy.S60': '1',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    68
    's60/ADO/internal/good/Distribution.Policy.S60': '1',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    69
    's60/ADO/internal/bad/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    70
    's60/ADO/tsrc/Distribution.Policy.S60': '950',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    71
    's60/ADO/tsrc/group/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    72
    's60/ADO/tsrc/private/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    73
    's60/invalid/Distribution.Policy.S60': '0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    74
    's60/invalid/comment/Distribution.Policy.S60': '0 ; some comment',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    75
    's60/invalid/utf16/Distribution.Policy.S60': '\xFF\xFE\x30\x00\x0D\x00\x0D\x00\x0D\x00\x0A\x00',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    76
    's60/invalid/letter/Distribution.Policy.S60': '9A0',
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    77
    }
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    78
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    79
class PolicyValidatorTest(unittest.TestCase):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    80
    EXEC_FILE = "archive_create.ant.xml"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    81
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    82
    def _testpath(self, subpath):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    83
        """ Normalised path for test paths. """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    84
        return os.path.normpath(os.path.join(self.root_test_dir, subpath))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    85
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    86
    def setUp(self):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    87
        """ Setup files test config. 
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    88
        
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    89
        This creates a number of empty files in a temporary directory structure
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    90
        for testing various file selection and archiving operations.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    91
        """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    92
        #print 'setup_module()'
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    93
        #print _test_file_content.keys()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    94
        self.root_test_dir = "_test_" + str(time.strftime("%H.%M.%S"))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    95
        for child_path in _test_file_paths:
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    96
            path = os.path.join(self.root_test_dir, child_path)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    97
            path_dir = path
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    98
            path_dir = os.path.dirname(path)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
    99
            
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   100
            if (not os.path.exists(path_dir)):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   101
                _logger.debug('Creating dir:  ' + path_dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   102
                os.makedirs(path_dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   103
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   104
            if(not path.endswith('/') and not path.endswith('\\')):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   105
                _logger.debug('Creating file: ' + path)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   106
                handle = open(path, 'w')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   107
                # Write any file content that is needed
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   108
                if _test_file_content.has_key(child_path):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   109
                    handle.write(_test_file_content[child_path])
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   110
                handle.close()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   111
    
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   112
    def tearDown(self):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   113
        """ Teardown test config. """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   114
        shutil.rmtree(self.root_test_dir)
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   115
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   116
    def test_policy_validator(self):
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   117
        """ Testing the policy validator behaviour. """
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   118
        validator = integration.quality.PolicyValidator()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   119
        errors = [] 
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   120
        errors.extend(validator.validate(self._testpath('s60')))
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   121
        errors.sort()
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   122
        print errors
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   123
        assert len(errors) == 5
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   124
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   125
        
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   126
        # Invalid encoding: contains other stuff than policy id.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   127
        assert errors[0][0] == "invalidencoding"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   128
        assert errors[0][1].lower() == self._testpath('s60' + os.sep + 'invalid' + os.sep + 'comment' + os.sep + 'distribution.policy.s60')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   129
        assert errors[0][2] == None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   130
        
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   131
        # Invalid encoding: ID contains a letter.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   132
        assert errors[1][0] == "invalidencoding"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   133
        assert errors[1][1].lower() == self._testpath('s60' + os.sep + 'invalid' + os.sep + 'letter' + os.sep + 'distribution.policy.s60')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   134
        assert errors[1][2] == None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   135
        
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   136
        # Invalid encoding: not ascii.
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   137
        assert errors[2][0] == "invalidencoding"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   138
        assert errors[2][1].lower() == self._testpath('s60' + os.sep + 'invalid' + os.sep + 'utf16' + os.sep + 'distribution.policy.s60')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   139
        assert errors[2][2] == None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   140
        
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   141
        # Policy file is missing
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   142
        assert errors[3][0] == "missing"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   143
        assert errors[3][1].lower() == self._testpath('s60' + os.sep + 'missing')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   144
        assert errors[3][2] == None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   145
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   146
        # Policy file is missing
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   147
        assert errors[4][0] == "missing"
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   148
        assert errors[4][1].lower() == self._testpath('s60' + os.sep + 'new_rules' + os.sep + 'subdir1')
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   149
        assert errors[4][2] == None
d8ac696cc51f helium_7.0-r14027
wbernard
parents:
diff changeset
   150