$search
00001 #! /usr/bin/python 00002 """ 00003 test_frame.py 00004 00005 Paul Malmsten, 2010 00006 pmalmsten@gmail.com 00007 00008 Tests frame module for proper behavior 00009 """ 00010 import unittest 00011 from xbee.frame import APIFrame 00012 00013 class TestAPIFrameGeneration(unittest.TestCase): 00014 """ 00015 XBee class must be able to create a valid API frame given binary 00016 data, in byte string form. 00017 """ 00018 def test_single_byte(self): 00019 """ 00020 create a frame containing a single byte 00021 """ 00022 data = '\x00' 00023 # start byte, two length bytes, data byte, checksum 00024 expected_frame = '\x7E\x00\x01\x00\xFF' 00025 00026 frame = APIFrame(data).output() 00027 self.assertEqual(frame, expected_frame) 00028 00029 class TestAPIFrameParsing(unittest.TestCase): 00030 """ 00031 XBee class must be able to read and validate the data contained 00032 by a valid API frame. 00033 """ 00034 00035 def test_remaining_bytes(self): 00036 """ 00037 remaining_bytes() should provide accurate indication 00038 of remaining bytes required before parsing a packet 00039 """ 00040 api_frame = APIFrame() 00041 00042 frame = '\x7E\x00\x04\x00\x00\x00\x00\xFF' 00043 self.assertEqual(api_frame.remaining_bytes(), 3) 00044 api_frame.fill(frame[0]) 00045 self.assertEqual(api_frame.remaining_bytes(), 2) 00046 api_frame.fill(frame[1]) 00047 self.assertEqual(api_frame.remaining_bytes(), 1) 00048 api_frame.fill(frame[2]) 00049 self.assertEqual(api_frame.remaining_bytes(), 5) 00050 api_frame.fill(frame[3]) 00051 self.assertEqual(api_frame.remaining_bytes(), 4) 00052 00053 def test_single_byte(self): 00054 """ 00055 read a frame containing a single byte 00056 """ 00057 api_frame = APIFrame() 00058 00059 frame = '\x7E\x00\x01\x00\xFF' 00060 expected_data = '\x00' 00061 00062 for byte in frame: 00063 api_frame.fill(byte) 00064 api_frame.parse() 00065 00066 self.assertEqual(api_frame.data, expected_data) 00067 00068 def test_invalid_checksum(self): 00069 """ 00070 when an invalid frame is read, an exception must be raised 00071 """ 00072 api_frame = APIFrame() 00073 frame = '\x7E\x00\x01\x00\xF6' 00074 00075 for byte in frame: 00076 api_frame.fill(byte) 00077 00078 self.assertRaises(ValueError, api_frame.parse) 00079 00080 class TestEscapedOutput(unittest.TestCase): 00081 """ 00082 APIFrame class must properly escape data 00083 """ 00084 00085 def test_escape_method(self): 00086 """ 00087 APIFrame.escape() must work as expected 00088 """ 00089 test_data = APIFrame.START_BYTE 00090 new_data = APIFrame.escape(test_data) 00091 self.assertEqual(new_data, APIFrame.ESCAPE_BYTE + '\x5e')