configurationengine/source/plugins/symbian/ConeHCRPlugin/hcrplugin/tests/unittest_read_write_record.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
       
    44 import sys
       
    45 import __init__
       
    46 
       
    47 from testautomation.utils import hex_to_bindata
       
    48 
       
    49 from hcrplugin.hcrrepository import HcrRepository, HcrRecord
       
    50 from hcrplugin.hcr_writer import HcrWriter
       
    51 from hcrplugin.hcr_reader import HcrReader
       
    52 
       
    53 class TestReadWriteHcrRecords(unittest.TestCase):
       
    54     
       
    55     def setUp(self):
       
    56         self.writer = HcrWriter()
       
    57         self.reader = HcrReader()
       
    58     
       
    59     def _run_test_read_write_record_no_lsd(self, record, record_bindata):
       
    60         self.assertEquals(self.writer.get_record_bindata(record, None), record_bindata)
       
    61         self.assertEquals(self.writer.get_record_lsd_bindata(record), None)
       
    62         
       
    63         parsed_record, parsed_lsd_pos = self.reader.parse_record_from_bindata(record_bindata)
       
    64         self.assertEquals(parsed_record.type,           record.type)
       
    65         self.assertEquals(parsed_record.value,          record.value)
       
    66         self.assertEquals(parsed_record.category_id,    record.category_id)
       
    67         self.assertEquals(parsed_record.element_id,     record.element_id)
       
    68         self.assertEquals(parsed_record.flags,          record.flags)
       
    69         self.assertEquals(parsed_lsd_pos,               None)
       
    70         
       
    71         self.assertEquals(self.reader.parse_record_value_from_lsd_bindata(parsed_record.type, None), None)
       
    72     
       
    73     def _run_test_read_write_record_with_lsd(self, record, record_bindata, lsd_pos, lsd_bindata):
       
    74         self.assertEquals(self.writer.get_record_bindata(record, lsd_pos), record_bindata)
       
    75         self.assertEquals(self.writer.get_record_lsd_bindata(record), lsd_bindata)
       
    76         
       
    77         
       
    78         parsed_record, parsed_lsd_pos = self.reader.parse_record_from_bindata(record_bindata)
       
    79         self.assertEquals(parsed_record.type,           record.type)
       
    80         self.assertEquals(parsed_record.category_id,    record.category_id)
       
    81         self.assertEquals(parsed_record.element_id,     record.element_id)
       
    82         self.assertEquals(parsed_record.flags,          record.flags)
       
    83         self.assertEquals(parsed_lsd_pos,               lsd_pos)
       
    84         
       
    85         self.assertEquals(self.reader.parse_record_value_from_lsd_bindata(record.type, lsd_bindata), record.value)
       
    86     
       
    87     # -------------------------------------------------------------------------
       
    88     #
       
    89     # -------------------------------------------------------------------------
       
    90 
       
    91     def test_read_write_bool(self):
       
    92         r = HcrRecord(HcrRecord.VALTYPE_BOOL, False, 12, 43, 5)
       
    93         d = hex_to_bindata("0C000000 2B000000 08000000 0500 0000 00000000")
       
    94         self._run_test_read_write_record_no_lsd(r, d)
       
    95         
       
    96         r = HcrRecord(HcrRecord.VALTYPE_BOOL, True, 0xDEADBEEF, 0xBAADF00D, 0xCAFE)
       
    97         d = hex_to_bindata("EFBEADDE 0DF0ADBA 08000000 FECA 0000 01000000")
       
    98         self._run_test_read_write_record_no_lsd(r, d)
       
    99     
       
   100     def test_read_write_int8(self):
       
   101         r = HcrRecord(HcrRecord.VALTYPE_INT8, -2**7, 12, 43, 5)
       
   102         d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 80FFFFFF")
       
   103         self._run_test_read_write_record_no_lsd(r, d)
       
   104         
       
   105         r = HcrRecord(HcrRecord.VALTYPE_INT8, 122, 12, 43, 5)
       
   106         d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 7A000000")
       
   107         self._run_test_read_write_record_no_lsd(r, d)
       
   108         
       
   109         r = HcrRecord(HcrRecord.VALTYPE_INT8, 2**7-1, 12, 43, 5)
       
   110         d = hex_to_bindata("0C000000 2B000000 04000000 0500 0000 7F000000")
       
   111         self._run_test_read_write_record_no_lsd(r, d)
       
   112     
       
   113     def test_read_write_uint8(self):
       
   114         r = HcrRecord(HcrRecord.VALTYPE_UINT8, 0, 12, 43, 5)
       
   115         d = hex_to_bindata("0C000000 2B000000 40000000 0500 0000 00000000")
       
   116         self._run_test_read_write_record_no_lsd(r, d)
       
   117         
       
   118         r = HcrRecord(HcrRecord.VALTYPE_UINT8, 234, 12, 43, 5)
       
   119         d = hex_to_bindata("0C000000 2B000000 40000000 0500 0000 EA000000")
       
   120         self._run_test_read_write_record_no_lsd(r, d)
       
   121         
       
   122         r = HcrRecord(HcrRecord.VALTYPE_UINT8, 2**8-1, 12, 43, 5)
       
   123         d = hex_to_bindata("0C000000 2B000000 40000000 0500 0000 FF000000")
       
   124         self._run_test_read_write_record_no_lsd(r, d)
       
   125     
       
   126     def test_read_write_int16(self):
       
   127         r = HcrRecord(HcrRecord.VALTYPE_INT16, -2**15, 12, 43, 5)
       
   128         d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 0080FFFF")
       
   129         self._run_test_read_write_record_no_lsd(r, d)
       
   130         
       
   131         r = HcrRecord(HcrRecord.VALTYPE_INT16, 12345, 12, 43, 5)
       
   132         d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 39300000")
       
   133         self._run_test_read_write_record_no_lsd(r, d)
       
   134         
       
   135         r = HcrRecord(HcrRecord.VALTYPE_INT16, 2**15-1, 12, 43, 5)
       
   136         d = hex_to_bindata("0C000000 2B000000 02000000 0500 0000 FF7F0000")
       
   137         self._run_test_read_write_record_no_lsd(r, d)
       
   138 
       
   139     def test_read_write_uint16(self):
       
   140         r = HcrRecord(HcrRecord.VALTYPE_UINT16, 0, 12, 43, 5)
       
   141         d = hex_to_bindata("0C000000 2B000000 20000000 0500 0000 00000000")
       
   142         self._run_test_read_write_record_no_lsd(r, d)
       
   143         
       
   144         r = HcrRecord(HcrRecord.VALTYPE_UINT16, 43215, 12, 43, 5)
       
   145         d = hex_to_bindata("0C000000 2B000000 20000000 0500 0000 CFA80000")
       
   146         self._run_test_read_write_record_no_lsd(r, d)
       
   147         
       
   148         r = HcrRecord(HcrRecord.VALTYPE_UINT16, 2**16-1, 12, 43, 5)
       
   149         d = hex_to_bindata("0C000000 2B000000 20000000 0500 0000 FFFF0000")
       
   150         self._run_test_read_write_record_no_lsd(r, d)
       
   151     
       
   152     def test_read_write_int32(self):
       
   153         r = HcrRecord(HcrRecord.VALTYPE_INT32, -2**31, 12, 43, 5)
       
   154         d = hex_to_bindata("0C000000 2B000000 01000000 0500 0000 00000080")
       
   155         self._run_test_read_write_record_no_lsd(r, d)
       
   156         
       
   157         r = HcrRecord(HcrRecord.VALTYPE_INT32, 1234567890, 12, 43, 5)
       
   158         d = hex_to_bindata("0C000000 2B000000 01000000 0500 0000 D2029649")
       
   159         self._run_test_read_write_record_no_lsd(r, d)
       
   160         
       
   161         r = HcrRecord(HcrRecord.VALTYPE_INT32, 2**31-1, 12, 43, 5)
       
   162         d = hex_to_bindata("0C000000 2B000000 01000000 0500 0000 FFFFFF7F")
       
   163         self._run_test_read_write_record_no_lsd(r, d)
       
   164     
       
   165     def test_read_write_uint32(self):
       
   166         r = HcrRecord(HcrRecord.VALTYPE_UINT32, 0, 12, 43, 5)
       
   167         d = hex_to_bindata("0C000000 2B000000 10000000 0500 0000 00000000")
       
   168         self._run_test_read_write_record_no_lsd(r, d)
       
   169         
       
   170         r = HcrRecord(HcrRecord.VALTYPE_UINT32, 3123456789, 12, 43, 5)
       
   171         d = hex_to_bindata("0C000000 2B000000 10000000 0500 0000 152B2CBA")
       
   172         self._run_test_read_write_record_no_lsd(r, d)
       
   173         
       
   174         r = HcrRecord(HcrRecord.VALTYPE_UINT32, 2**32-1, 12, 43, 5)
       
   175         d = hex_to_bindata("0C000000 2B000000 10000000 0500 0000 FFFFFFFF")
       
   176         self._run_test_read_write_record_no_lsd(r, d)
       
   177     
       
   178     def test_read_write_linaddr(self):
       
   179         r = HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 0, 12, 43, 5)
       
   180         d = hex_to_bindata("0C000000 2B000000 00010000 0500 0000 00000000")
       
   181         self._run_test_read_write_record_no_lsd(r, d)
       
   182         
       
   183         r = HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 3123456789, 12, 43, 5)
       
   184         d = hex_to_bindata("0C000000 2B000000 00010000 0500 0000 152B2CBA")
       
   185         self._run_test_read_write_record_no_lsd(r, d)
       
   186         
       
   187         r = HcrRecord(HcrRecord.VALTYPE_LIN_ADDR, 2**32-1, 12, 43, 5)
       
   188         d = hex_to_bindata("0C000000 2B000000 00010000 0500 0000 FFFFFFFF")
       
   189         self._run_test_read_write_record_no_lsd(r, d)
       
   190 
       
   191 
       
   192 
       
   193     def test_read_write_int64(self):
       
   194         lsd_pos = (1234, 8)
       
   195 
       
   196         record = HcrRecord(HcrRecord.VALTYPE_INT64, -2**63, 12, 43, 5)
       
   197         rec_data = hex_to_bindata("0C000000 2B000000 00000001 0500 0800 D2040000")
       
   198         lsd_data = hex_to_bindata("0000 0000 0000 0080")
       
   199         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   200 
       
   201         record = HcrRecord(HcrRecord.VALTYPE_INT64, 9211026413402420220, 12, 43, 5)
       
   202         rec_data = hex_to_bindata("0C000000 2B000000 00000001 0500 0800 D2040000")
       
   203         lsd_data = hex_to_bindata("FC73 978B B823 D47F")
       
   204         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   205 
       
   206         record = HcrRecord(HcrRecord.VALTYPE_INT64, 2**63-1, 12, 43, 5)
       
   207         rec_data = hex_to_bindata("0C000000 2B000000 00000001 0500 0800 D2040000")
       
   208         lsd_data = hex_to_bindata("FFFF FFFF FFFF FF7F")
       
   209         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   210     
       
   211     def test_read_write_uint64(self):
       
   212         lsd_pos = (1234, 8)
       
   213         record = HcrRecord(HcrRecord.VALTYPE_UINT64, 0, 12, 43, 5)
       
   214         rec_data = hex_to_bindata("0C000000 2B000000 00000002 0500 0800 D2040000")
       
   215         lsd_data = hex_to_bindata("0000 0000 0000 0000")
       
   216         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   217 
       
   218         record = HcrRecord(HcrRecord.VALTYPE_UINT64, 10746170304040729876, 12, 43, 5)
       
   219         rec_data = hex_to_bindata("0C000000 2B000000 00000002 0500 0800 D2040000")
       
   220         lsd_data = hex_to_bindata("14FD 32B4 F410 2295")
       
   221         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   222 
       
   223         record = HcrRecord(HcrRecord.VALTYPE_UINT64, 2**64-1, 12, 43, 5)
       
   224         rec_data = hex_to_bindata("0C000000 2B000000 00000002 0500 0800 D2040000")
       
   225         lsd_data = hex_to_bindata("FFFF FFFF FFFF FFFF")
       
   226         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   227 
       
   228     def test_read_write_arrayint32(self):
       
   229         arr = []
       
   230         lsd_pos = (1234, 0)
       
   231         record = HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, arr, 12, 43, 5)
       
   232         rec_data = hex_to_bindata("0C000000 2B000000 00000400 0500 0000 D2040000")
       
   233         lsd_data = ""
       
   234         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   235 
       
   236         arr = [1234]
       
   237         lsd_pos = (1234, 4)
       
   238         record = HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, arr, 12, 43, 5)
       
   239         rec_data = hex_to_bindata("0C000000 2B000000 00000400 0500 0400 D2040000")
       
   240         lsd_data = hex_to_bindata("D2040000")
       
   241         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   242 
       
   243         arr = [-2**31, 0, 1234567890, 2**31-1]
       
   244         lsd_pos = (1234, 4*3)
       
   245         record = HcrRecord(HcrRecord.VALTYPE_ARRAY_INT32, arr, 12, 43, 5)
       
   246         rec_data = hex_to_bindata("0C000000 2B000000 00000400 0500 0C00 D2040000")
       
   247         lsd_data = hex_to_bindata("00000080 00000000 D2029649 FFFFFF7F")
       
   248         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   249     
       
   250     def test_read_write_arrayuint32(self):
       
   251         arr = []
       
   252         lsd_pos = (1234, 0)
       
   253         record = HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, arr, 12, 43, 5)
       
   254         rec_data = hex_to_bindata("0C000000 2B000000 00000800 0500 0000 D2040000")
       
   255         lsd_data = ""
       
   256         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   257 
       
   258         arr = [1234]
       
   259         lsd_pos = (1234, 4)
       
   260         record = HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, arr, 12, 43, 5)
       
   261         rec_data = hex_to_bindata("0C000000 2B000000 00000800 0500 0400 D2040000")
       
   262         lsd_data = hex_to_bindata("D2040000")
       
   263         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   264 
       
   265         arr = [0, 3123456789, 2**32-1]
       
   266         lsd_pos = (1234, 4*3)
       
   267         record = HcrRecord(HcrRecord.VALTYPE_ARRAY_UINT32, arr, 12, 43, 5)
       
   268         rec_data = hex_to_bindata("0C000000 2B000000 00000800 0500 0C00 D2040000")
       
   269         lsd_data = hex_to_bindata("00000000 152B2CBA FFFFFFFF")
       
   270         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   271 
       
   272     def test_read_write_text8(self):
       
   273         string = ''
       
   274         lsd_pos = (1234, 0)
       
   275         record = HcrRecord(HcrRecord.VALTYPE_TEXT8, string, 12, 43, 5)
       
   276         rec_data = hex_to_bindata("0C000000 2B000000 00000200 0500 0000 D2040000")
       
   277         lsd_data = ""
       
   278         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   279 
       
   280         string = 'Hello world!!'
       
   281         lsd_pos = (1234, 13)
       
   282         record = HcrRecord(HcrRecord.VALTYPE_TEXT8, string, 12, 43, 5)
       
   283         rec_data = hex_to_bindata("0C000000 2B000000 00000200 0500 0D00 D2040000")
       
   284         lsd_data = "Hello world!!"
       
   285         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   286 
       
   287         string = u'Cost 100€'
       
   288         lsd_pos = (1234, 11)
       
   289         record = HcrRecord(HcrRecord.VALTYPE_TEXT8, string, 12, 43, 5)
       
   290         rec_data = hex_to_bindata("0C000000 2B000000 00000200 0500 0B00 D2040000")
       
   291         lsd_data = "Cost 100" + hex_to_bindata("E2 82 AC")
       
   292         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   293     
       
   294     def test_read_write_bindata(self):
       
   295         data = ''
       
   296         lsd_pos = (1234, 0)
       
   297         record = HcrRecord(HcrRecord.VALTYPE_BIN_DATA, data, 12, 43, 5)
       
   298         rec_data = hex_to_bindata("0C000000 2B000000 00000100 0500 0000 D2040000")
       
   299         lsd_data = ""
       
   300         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   301 
       
   302         data = hex_to_bindata('00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF')
       
   303         lsd_pos = (1234, 16)
       
   304         record = HcrRecord(HcrRecord.VALTYPE_BIN_DATA, data, 12, 43, 5)
       
   305         rec_data = hex_to_bindata("0C000000 2B000000 00000100 0500 1000 D2040000")
       
   306         lsd_data = hex_to_bindata('00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF')
       
   307         self._run_test_read_write_record_with_lsd(record, rec_data, lsd_pos, lsd_data)
       
   308