configurationengine/source/plugins/symbian/ConeHCRPlugin/hcrplugin/tests/unittest_read_write_repository.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     1 # *-* coding: utf-8 *-*
       
     2 #
       
     3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 # All rights reserved.
       
     5 # This component and the accompanying materials are made available
       
     6 # under the terms of "Eclipse Public License v1.0"
       
     7 # which accompanies this distribution, and is available
       
     8 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 #
       
    10 # Initial Contributors:
       
    11 # Nokia Corporation - initial contribution.
       
    12 #
       
    13 # Contributors:
       
    14 #
       
    15 # Description: 
       
    16 #
       
    17 
       
    18 """
       
    19 When editing the hex data in the test cases, this may come in handy:
       
    20 
       
    21 1. Start the Python command line interpreter
       
    22 2. Paste the following lines there:
       
    23 
       
    24 from struct import pack, unpack
       
    25 bin2hex = lambda d: ''.join("%02X" % ord(c) for c in d)
       
    26 hexpack = lambda fmt, *args: bin2hex(pack(fmt, *args))
       
    27 hex2bin = lambda h: ''.join([chr(int(h[i*2:i*2+2], 16)) for i in xrange(len(h)/2)])
       
    28 hexunpack = lambda fmt, data: unpack(fmt, hex2bin(data))
       
    29 
       
    30 Now you can get the hex representation for any format supported by
       
    31 struct.pack() easily. For example, formatting a little-endian unsigned short:
       
    32 
       
    33 >>> hexpack('<H', 1234)
       
    34 'D204'
       
    35 
       
    36 ...and conversely:
       
    37 
       
    38 >>> hexunpack('<H', 'D204')
       
    39 (1234,)
       
    40 """
       
    41 
       
    42 import unittest
       
    43 import os, shutil, random
       
    44 import sys
       
    45 import __init__
       
    46 
       
    47 ROOT_PATH = os.path.abspath(os.path.dirname(__file__))
       
    48 
       
    49 from testautomation.utils import hex_to_bindata
       
    50 
       
    51 from hcrplugin.hcrrepository import HcrRepository, HcrRecord
       
    52 from hcrplugin.hcr_writer import HcrWriter
       
    53 from hcrplugin.hcr_reader import HcrReader
       
    54 from hcrplugin import hcr_exceptions
       
    55 
       
    56 class TestReadWriteHcrRepository(unittest.TestCase):
       
    57 
       
    58     def setUp(self):
       
    59         self.writer = HcrWriter()
       
    60         self.reader = HcrReader()
       
    61 
       
    62     def write_repo_assertion_failure_files(self, actual_data, expected_data, failure_report_dir, filename):
       
    63         dir = os.path.join(ROOT_PATH, 'temp/repo_assertion_failure')
       
    64         dir = os.path.normpath(os.path.join(dir, failure_report_dir))
       
    65         if not os.path.exists(dir):
       
    66             os.makedirs(dir)
       
    67         
       
    68         f = open(os.path.join(dir, "actual_" + filename), "wb")
       
    69         try:        f.write(actual_data)
       
    70         finally:    f.close()
       
    71         
       
    72         f = open(os.path.join(dir, "expected_" + filename), "wb")
       
    73         try:        f.write(expected_data)
       
    74         finally:    f.close()
       
    75         
       
    76         return dir
       
    77     
       
    78     def _run_test_read_write_repository(self, repo, repo_data, failure_report_dir):
       
    79         """
       
    80         Test reading and writing the repository using the given data.
       
    81         
       
    82         In case of a failure, the actual and expected data are written
       
    83         to the given directory, so that they can be compared using e.g.
       
    84         Beyond Compare.
       
    85         
       
    86         @param repo: The repository object to test.
       
    87         @param repo_data: The expected binary data corresponding to the repository object.
       
    88         @param failure_report_dir: The directory where files are written in case of
       
    89             a failure. Note that this should be just e.g. 'empty_repo', the parent dirs
       
    90             are appended automatically.
       
    91         """
       
    92         # Test writing
       
    93         expected_data = repo_data
       
    94         actual_data = self.writer.get_repository_bindata(repo)
       
    95         if actual_data != expected_data:
       
    96             dir = self.write_repo_assertion_failure_files(
       
    97                 actual_data, expected_data,
       
    98                 failure_report_dir, 'repo.dat')
       
    99             self.fail("Actual and expected repository data are not equal!\n"+\
       
   100                 "See the files in '%s'" % dir)
       
   101         
       
   102         
       
   103         # Test reading
       
   104         expected_repo = repo
       
   105         actual_repo = self.reader.parse_repository_from_bindata(repo_data)
       
   106         if actual_repo != expected_repo:
       
   107             dir = self.write_repo_assertion_failure_files(
       
   108                 repr(actual_repo), repr(expected_repo),
       
   109                 failure_report_dir, 'repo.txt')
       
   110             self.fail("Actual and expected repository objects are not equal!\n"+\
       
   111                 "See the files in '%s'" % dir)
       
   112     
       
   113     # -------------------------------------------------------------------------
       
   114     #
       
   115     # -------------------------------------------------------------------------
       
   116     
       
   117     def test_read_write_empty_repository_1(self):
       
   118         repo = HcrRepository([], version=2, flags=3)
       
   119         repo_data = hex_to_bindata(
       
   120             "48435266 0200 0300 00000000 20000000"+\
       
   121             "00000000 000000000000000000000000")
       
   122         
       
   123         self._run_test_read_write_repository(repo, repo_data, 'empty_repo_1')
       
   124         
       
   125     def test_read_write_empty_repository_2(self):    
       
   126         repo = HcrRepository([], version=0xBEEF, flags=0xCAFE)
       
   127         repo_data = hex_to_bindata(
       
   128             "48435266 EFBE FECA 00000000 20000000"+\
       
   129             "00000000 000000000000000000000000")
       
   130         
       
   131         self._run_test_read_write_repository(repo, repo_data, 'empty_repo_2')
       
   132     
       
   133     # -------------------------------------------------------------------------
       
   134     #
       
   135     # -------------------------------------------------------------------------
       
   136     
       
   137     def test_read_write_repository_without_lsd(self):
       
   138         records = [
       
   139             HcrRecord(HcrRecord.VALTYPE_BOOL,       True,        1, 1, 0),
       
   140             HcrRecord(HcrRecord.VALTYPE_INT8,       -123,        1, 2, 0),
       
   141             HcrRecord(HcrRecord.VALTYPE_UINT8,      204,         1, 3, 0),
       
   142             HcrRecord(HcrRecord.VALTYPE_INT16,      -13423,      2, 1, 0),
       
   143             HcrRecord(HcrRecord.VALTYPE_UINT16,     54321,       2, 2, 0),
       
   144             HcrRecord(HcrRecord.VALTYPE_INT32,      -1000000000, 2, 3, 0),
       
   145             HcrRecord(HcrRecord.VALTYPE_UINT32,     4000000000,  3, 1, 0),
       
   146             HcrRecord(HcrRecord.VALTYPE_LIN_ADDR,   0xAABBCCDD,  3, 2, 0),
       
   147         ]
       
   148         # Shuffle the records to make sure that they are sorted properly when writing
       
   149         random.shuffle(records)
       
   150         repo = HcrRepository(records, version=2, flags=3)
       
   151         
       
   152         h = hex_to_bindata
       
   153         data = [
       
   154             # Header
       
   155             h("48435266 0200 0300 08000000 C0000000"),
       
   156             h("00000000 000000000000000000000000"),
       
   157             # Record section
       
   158             h("01000000 01000000 08000000 0000 0000 01000000"), # bool
       
   159             h("01000000 02000000 04000000 0000 0000 85FFFFFF"), # int8
       
   160             h("01000000 03000000 40000000 0000 0000 CC000000"), # uint8
       
   161             h("02000000 01000000 02000000 0000 0000 91CBFFFF"), # int16
       
   162             h("02000000 02000000 20000000 0000 0000 31D40000"), # uint16
       
   163             h("02000000 03000000 01000000 0000 0000 003665C4"), # int32
       
   164             h("03000000 01000000 10000000 0000 0000 00286BEE"), # uint32
       
   165             h("03000000 02000000 00010000 0000 0000 DDCCBBAA"), # linaddr
       
   166         ]
       
   167         repo_data = ''.join(data)
       
   168         
       
   169         self._run_test_read_write_repository(repo, repo_data, 'repo_without_lsd')
       
   170     
       
   171     # -------------------------------------------------------------------------
       
   172     #
       
   173     # -------------------------------------------------------------------------
       
   174     
       
   175     def test_read_write_repository_with_lsd(self):
       
   176         records = [
       
   177             HcrRecord(HcrRecord.VALTYPE_INT64,        9211026413402420220,  1, 1, 0),
       
   178             HcrRecord(HcrRecord.VALTYPE_UINT64,       10746170304040729876, 1, 2, 0),
       
   179             HcrRecord(HcrRecord.VALTYPE_TEXT8,        u'Cost 100€',         1, 3, 0),
       
   180             HcrRecord(HcrRecord.VALTYPE_BIN_DATA,     'test test',          2, 1, 0),
       
   181             HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32,  [-2**31, 0, 2**31-1], 2, 2, 0),
       
   182             HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [0, 100000, 2**32-1], 2, 3, 0),
       
   183         ]
       
   184         # Shuffle the records to make sure that they are sorted properly when writing
       
   185         random.shuffle(records)
       
   186         repo = HcrRepository(records, version=2, flags=3)
       
   187         
       
   188         
       
   189         # Record section size: 6 * 20 = 120
       
   190         # LSD offset: 32 + 120 = 152
       
   191         # LSD size:   8 + 8 + 12 + 12 + 12 + 12 = 64
       
   192         h = hex_to_bindata
       
   193         data = [
       
   194             # Header
       
   195             h("48435266 0200 0300 06000000 98000000"),
       
   196             h("40000000 000000000000000000000000"),
       
   197             # Record section
       
   198             h("01000000 01000000 00000001 0000 0800 00000000"), # int64, lsd pos = (0, 8)
       
   199             h("01000000 02000000 00000002 0000 0800 08000000"), # uint64, lsd pos = (8, 8)
       
   200             h("01000000 03000000 00000200 0000 0B00 10000000"), # text8, lsd pos = (8 + 8, 11)
       
   201             h("02000000 01000000 00000100 0000 0900 1C000000"), # bindata, lsd pos = (8 + 8 + 12, 9)
       
   202             h("02000000 02000000 00000400 0000 0C00 28000000"), # arrayint32, lsd pos = (8 + 8 + 12 + 12, 12)
       
   203             h("02000000 03000000 00000800 0000 0C00 34000000"), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12, 12)
       
   204             # LSD section
       
   205             h("FC73 978B B823 D47F"),          # 8 bytes
       
   206             h("14FD 32B4 F410 2295"),          # 8 bytes
       
   207             "Cost 100" + h("E2 82 AC 00"),     # 12 bytes
       
   208             "test test" + h("00 00 00"),       # 12 bytes
       
   209             h("00000080 00000000 FFFFFF7F"),   # 12 bytes
       
   210             h("00000000 A0860100 FFFFFFFF"),   # 12 bytes
       
   211         ]
       
   212         
       
   213         repo_data = ''.join(data)
       
   214         
       
   215         self._run_test_read_write_repository(repo, repo_data, 'repo_with_lsd')
       
   216     
       
   217     # -------------------------------------------------------------------------
       
   218     #
       
   219     # -------------------------------------------------------------------------
       
   220     
       
   221     def test_read_write_repository_with_all_record_types(self):
       
   222         records = [
       
   223             HcrRecord(HcrRecord.VALTYPE_BOOL,         True,                 1, 1, 0),
       
   224             HcrRecord(HcrRecord.VALTYPE_INT8,         -123,                 1, 2, 0),
       
   225             HcrRecord(HcrRecord.VALTYPE_UINT8,        204,                  1, 3, 0),
       
   226             HcrRecord(HcrRecord.VALTYPE_INT16,        -13423,               2, 1, 0),
       
   227             HcrRecord(HcrRecord.VALTYPE_UINT16,       54321,                2, 2, 0),
       
   228             HcrRecord(HcrRecord.VALTYPE_INT32,        -1000000000,          2, 3, 0),
       
   229             HcrRecord(HcrRecord.VALTYPE_UINT32,       4000000000,           3, 1, 0),
       
   230             HcrRecord(HcrRecord.VALTYPE_LIN_ADDR,     0xAABBCCDD,           3, 2, 0),
       
   231             
       
   232             HcrRecord(HcrRecord.VALTYPE_INT64,        9211026413402420220,  4, 1, 0),
       
   233             HcrRecord(HcrRecord.VALTYPE_UINT64,       10746170304040729876, 4, 2, 0),
       
   234             HcrRecord(HcrRecord.VALTYPE_TEXT8,        u'Cost 100€',         4, 3, 0),
       
   235             HcrRecord(HcrRecord.VALTYPE_TEXT8,        '',                   5, 1, 0),
       
   236             HcrRecord(HcrRecord.VALTYPE_BIN_DATA,     'test test',          5, 2, 0),
       
   237             HcrRecord(HcrRecord.VALTYPE_BIN_DATA,     '',                   5, 3, 0),
       
   238             HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32,  [-2**31, 0, 2**31-1], 6, 1, 0),
       
   239             HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32,  [],                   6, 2, 0),
       
   240             HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [0, 100000, 2**32-1], 6, 3, 0),
       
   241             HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, [],                   7, 1, 0),
       
   242             
       
   243             HcrRecord(HcrRecord.VALTYPE_BOOL,         False,                8, 1, 0),
       
   244             HcrRecord(HcrRecord.VALTYPE_TEXT8,        u'Hello world!!!',    8, 2, 0),
       
   245             HcrRecord(HcrRecord.VALTYPE_TEXT8,        u'unpadded',          8, 3, 0),
       
   246         ]
       
   247         # Shuffle the records to make sure that they are sorted properly when writing
       
   248         random.shuffle(records)
       
   249         repo = HcrRepository(records, version=2, flags=3)
       
   250         
       
   251         # Record section size: 21 * 20 = 420
       
   252         # LSD offset: 32 + 420 = 452
       
   253         # LSD size:   8 + 8 + 12 + 12 + 12 + 12 + 16 + 8 = 88
       
   254         h = hex_to_bindata
       
   255         data = [
       
   256             # Header
       
   257             h("48435266 0200 0300 15000000 C4010000"),
       
   258             h("58000000 000000000000000000000000"),
       
   259             
       
   260             # Record section
       
   261             h("01000000 01000000 08000000 0000 0000 01000000"), # bool
       
   262             h("01000000 02000000 04000000 0000 0000 85FFFFFF"), # int8
       
   263             h("01000000 03000000 40000000 0000 0000 CC000000"), # uint8
       
   264             h("02000000 01000000 02000000 0000 0000 91CBFFFF"), # int16
       
   265             h("02000000 02000000 20000000 0000 0000 31D40000"), # uint16
       
   266             h("02000000 03000000 01000000 0000 0000 003665C4"), # int32
       
   267             h("03000000 01000000 10000000 0000 0000 00286BEE"), # uint32
       
   268             h("03000000 02000000 00010000 0000 0000 DDCCBBAA"), # linaddr
       
   269             
       
   270             h("04000000 01000000 00000001 0000 0800 00000000"), # int64, lsd pos = (0, 8)
       
   271             h("04000000 02000000 00000002 0000 0800 08000000"), # uint64, lsd pos = (8, 8)
       
   272             h("04000000 03000000 00000200 0000 0B00 10000000"), # text8, lsd pos = (8 + 8, 11)
       
   273             h("05000000 01000000 00000200 0000 0000 1C000000"), # text8, lsd pos = (8 + 8 + 12, 0)
       
   274             h("05000000 02000000 00000100 0000 0900 1C000000"), # bindata, lsd pos = (8 + 8 + 12, 9)
       
   275             h("05000000 03000000 00000100 0000 0000 28000000"), # bindata, lsd pos = (8 + 8 + 12 + 12, 0)
       
   276             h("06000000 01000000 00000400 0000 0C00 28000000"), # arrayint32, lsd pos = (8 + 8 + 12 + 12, 12)
       
   277             h("06000000 02000000 00000400 0000 0000 34000000"), # arrayint32, lsd pos = (8 + 8 + 12 + 12 + 12, 0)
       
   278             h("06000000 03000000 00000800 0000 0C00 34000000"), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12, 12)
       
   279             h("07000000 01000000 00000800 0000 0000 40000000"), # arrayuint32, lsd pos = (8 + 8 + 12 + 12 + 12 + 12, 0)
       
   280             
       
   281             h("08000000 01000000 08000000 0000 0000 00000000"), # bool
       
   282             h("08000000 02000000 00000200 0000 0E00 40000000"), # text8, lsd pos = (8 + 8 + 12 + 12 + 12 + 12, 14)
       
   283             h("08000000 03000000 00000200 0000 0800 50000000"), # text8, lsd pos = (8 + 8 + 12 + 12 + 12 + 12 + 16, 8)
       
   284             
       
   285             # LSD section
       
   286             h("FC73 978B B823 D47F"),          # 8 bytes
       
   287             h("14FD 32B4 F410 2295"),          # 8 bytes
       
   288             "Cost 100" + h("E2 82 AC 00"),     # 12 bytes
       
   289             "",
       
   290             "test test" + h("00 00 00"),       # 12 bytes
       
   291             "",
       
   292             h("00000080 00000000 FFFFFF7F"),   # 12 bytes
       
   293             "",
       
   294             h("00000000 A0860100 FFFFFFFF"),   # 12 bytes
       
   295             "",
       
   296             "Hello world!!!" + h("00 00"),     # 16 bytes
       
   297             "unpadded",                        # 8 bytes
       
   298         ]
       
   299         repo_data = ''.join(data)
       
   300         
       
   301         self._run_test_read_write_repository(repo, repo_data, 'repo_with_all_record_types')