import glob
import os
import unittest

from lora.payload import LoRaPayload

FIXTURES_PATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'fixtures')


def read(filename):
    '''Read a file and strip spaces'''
    with open(filename) as f:
        return f.read().strip()


class TestLoraPayload(unittest.TestCase):
    def test_xmlparsing(self):
        xmlfilename = os.path.join(FIXTURES_PATH, '000015E4', 'payload_1.xml')

        payload = LoRaPayload(read(xmlfilename))
        self.assertEquals(payload.DevLrrCnt, '1')

    def test_decrypting_payload(self):
        for device_path in glob.glob(os.path.join(FIXTURES_PATH, '*')):
            if device_path.endswith('README.md'):
                continue

            # infer dev_addr from fixture path
            dev_addr = os.path.split(device_path)[1]
            # key is in a file called key.hex
            key = read(os.path.join(device_path, 'key.hex'))

            # text all the files ending in xml in the path we just discovered
            for fixture_filename in glob.glob(os.path.join(device_path, 'payload_*.xml')):

                payload = LoRaPayload(read(fixture_filename).encode('UTF-8'))
                plaintext_ints = payload.decrypt(key, dev_addr)

                expected_filename = fixture_filename.replace('.xml', '.txt')
                expected_plaintext = read(expected_filename)

                self.assertEquals(
                    ''.join('{:02x}'.format(x) for x in plaintext_ints),
                    expected_plaintext
                )


if __name__ == '__main__':
    unittest.main()

@@ -285,308 +285,28 @@
 )%0A%0A%0A
-class TestLoraPayload(unittest.TestCase):%0A    def test_xmlparsing(self):%0A        xmlfilename = os.path.join(FIXTURES_PATH, '000015E4', 'payload_1.xml')%0A%0A        payload = LoRaPayload(read(xmlfilename))%0A        self.assertEquals(payload.DevLrrCnt, '1')%0A%0A    def test_decrypting_payload(self):%0A   
+def fixtures():%0A
     
- 
 for 
@@ -373,20 +373,16 @@
         
-    
 if devic
@@ -423,20 +423,16 @@
         
-   
- 
 continue
@@ -433,20 +433,16 @@
 ntinue%0A%0A
-    
         
@@ -484,20 +484,16 @@
         
-   
- 
 dev_addr
@@ -529,58 +529,8 @@
 %5B1%5D%0A
-            # key is in a file called key.hex%0A    
     
@@ -583,20 +583,16 @@
 hex'))%0A%0A
-    
         
@@ -665,20 +665,16 @@
         
-   
- 
 for fixt
@@ -761,231 +761,579 @@
     
-    payload = LoRaPayload(read(fixture_filename).encode('UTF-8'))%0A                plaintext_ints = payload.decrypt(key, dev_addr)%0A%0A                expected_filename = fixture_filename.replace('.xml', '.txt')%0A               
+yield (%0A                dev_addr,%0A                key,%0A                read(fixture_filename),%0A                read(fixture_filename.replace('.xml', '.txt'))%0A            )%0A%0A%0Aclass TestLoraPayload(unittest.TestCase):%0A    def test_xmlparsing(self):%0A        xmlfilename = os.path.join(FIXTURES_PATH, '000015E4', 'payload_1.xml')%0A%0A        payload = LoRaPayload(read(xmlfilename))%0A        self.assertEquals(payload.DevLrrCnt, '1')%0A        self.assertEquals(payload.FCntUp, '2')%0A%0A    def test_decrypting_payload(self):%0A        '''Check the decrypted plaintext against a list of
  exp
@@ -1329,33 +1329,33 @@
 list of expected
-_
+ 
 plaintext = read
@@ -1351,40 +1351,185 @@
 text
- = read(expected_filename)%0A%0A    
+s'''%0A        for dev_addr, key, xml, expected in fixtures():%0A            payload = LoRaPayload(xml.encode('UTF-8'))%0A            plaintext_ints = payload.decrypt(key, dev_addr)%0A%0A
     
@@ -1555,20 +1555,16 @@
 Equals(%0A
-    
         
@@ -1644,20 +1644,16 @@
     
-    
 expected
 _pla
@@ -1652,23 +1652,9 @@
 cted
-_plaintext%0A    
+%0A
     

Clean up tests a bit