Package lepl :: Package bin :: Package _test :: Module matchers
[hide private]
[frames] | no frames]

Source Code for Module lepl.bin._test.matchers

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Tests for the lepl.bin.matchers module. 
 32  ''' 
 33   
 34  if bytes is str: 
 35      print('Binary parsing unsupported in this Python version') 
 36  else: 
 37   
 38      #from logging import basicConfig, DEBUG 
 39      from unittest import TestCase 
 40       
 41      from lepl.bin import * 
 42      from lepl.support.node import Node 
 43      from lepl.matchers.variables import TraceVariables 
 44       
 45       
 46      # pylint: disable-msg=C0103, C0111, C0301 
 47      # (dude this is just a test) 
 48       
49 - class MatcherTest(TestCase):
50 ''' 51 Test whether we correctly match some data. 52 ''' 53
54 - def test_match(self):
55 #basicConfig(level=DEBUG) 56 57 # first, define some test data - we'll use a simple definition 58 # language, but you could also construct this directly in Python 59 # (Frame, Header etc are auto-generated subclasses of Node). 60 mac = parse(''' 61 Frame( 62 Header( 63 preamble = 0b10101010*7, 64 start = 0b10101011, 65 destn = 010203040506x0, 66 source = 0708090a0b0cx0, 67 ethertype = 0800x0 68 ), 69 Data(1/8,2/8,3/8,4/8), 70 CRC(234d0/4.) 71 ) 72 ''') 73 74 # next, define a parser for the header structure 75 # this is mainly literal values, but we make the two addresses 76 # big-endian integers, which will be read from the data 77 78 # this looks very like "normal" lepl because it is - there's 79 # nothing in lepl that forces the data being parsed to be text. 80 81 with TraceVariables(False): 82 preamble = ~Const('0b10101010')[7] 83 start = ~Const('0b10101011') 84 destn = BEnd(6.0) > 'destn' 85 source = BEnd(6.0) > 'source' 86 ethertype = ~Const('0800x0') 87 header = preamble & start & destn & source & ethertype > Node 88 89 # so, what do the test data look like? 90 # print(mac) 91 # Frame 92 # +- Header 93 # | +- preamble BitString(b'\xaa\xaa\xaa\xaa\xaa\xaa\xaa', 56, 0) 94 # | +- start BitString(b'\xab', 8, 0) 95 # | +- destn BitString(b'\x01\x02\x03\x04\x05\x06', 48, 0) 96 # | +- source BitString(b'\x07\x08\t\n\x0b\x0c', 48, 0) 97 # | `- ethertype BitString(b'\x08\x00', 16, 0) 98 # +- Data 99 # | +- BitString(b'\x01', 8, 0) 100 # | +- BitString(b'\x02', 8, 0) 101 # | +- BitString(b'\x03', 8, 0) 102 # | `- BitString(b'\x04', 8, 0) 103 # `- CRC 104 # `- BitString(b'\x00\x00\x00\xea', 32, 0) 105 106 # we can serialize that to a BitString 107 b = simple_serialiser(mac, dispatch_table()) 108 assert str(b) == 'aaaaaaaaaaaaaaab123456789abc801234000eax0/240' 109 110 # and then we can parse it 111 header.config.no_full_first_match() 112 p = header.parse(b)[0] 113 # print(p) 114 # Node 115 # +- destn Int(1108152157446,48) 116 # `- source Int(7731092785932,48) 117 118 # the destination address 119 assert hex(p.destn[0]) == '0x10203040506' 120 121 # the source address 122 assert hex(p.source[0]) == '0x708090a0b0c'
123