import unittest, tempfile, sys, os.path

datadir = os.environ.get('APPORT_DATA_DIR', '/usr/share/apport')
sys.path.insert(0, os.path.join(datadir, 'general-hooks'))

import parse_segv

# Default global registers, maps, and disassembly for testing
regs = '''eax            0xffffffff -1
ecx            0xbfc6af40   -1077498048
edx            0x1  1
ebx            0x26eff4 2551796
esp            0xbfc6af24   0xbfc6af24
ebp            0xbfc6af28   0xbfc6af28
esi            0x826bb60    136756064
edi            0x8083480    134755456
eip            0x808354e    0x808354e <main+14>
eflags         0x200286 [ PF SF IF ID ]
cs             0x73 115
ss 
            0x7b 123
ds          
   0x7b 123
es             0x7b 123
fs             0x4  4
gs             0x33 51
'''
regs64 = '''rax            0xffffffffffffffff   -1
rbx            0x26eff4 2551796
rcx            0xffffffffffffffff   -1
rdx            0xffffffffff600180   -10485376
rsi            0x0  0
rdi            0x7fffffffe3b0   140737488348080
rbp            0x0  0x0
rsp            0x0000bfc6af24   0x0000bfc6af24
r8             0x0  0
r9             0x0  0
r10            0x7fffffffe140   140737488347456
r11            0x246    582
r12            0x7fffffffe400   140737488348160
r13            0x7fffffffe468   140737488348264
r14            0x1  1
r15            0x7fffffffe460   140737488348256
rip            0x7ffff790be10   0x7ffff790be10 <nanosleep+16>
eflags         0x246    [ PF ZF IF ]
cs             0x33 51
ss             0x2b 43
ds             0x0  0
es             0x0  0
fs             0x0  0
gs             0x0  0
fctrl          0x37f    895
fstat          0x0  0
ftag           0xffff   65535
fiseg          0x0  0
fioff          0x40303a 4206650
foseg          0x0  0
fooff          0x0  0
fop            0x5d8    1496
mxcsr          0x1f80   [ IM DM ZM OM UM PM ]
'''
maps = '''00110000-0026c000 r-xp 00000000 08:06 375131     /lib/tls/i686/cmov/libc-2.9.so
0026c000-0026d000 ---p 0015c000 08:06 375131     /lib/tls/i686/cmov/libc-2.9.so
0026d000-0026f000 r--p 0015c000 08:06 375131     /lib/tls/i686/cmov/libc-2.9.so
0026f000-00270000 rw-p 0015e000 08:06 375131     /lib/tls/i686/cmov/libc-2.9.so
00270000-00273000 rw-p 00000000 00:00 0
002c1000-002e5000 r-xp 00000000 08:06 375135     /lib/tls/i686/cmov/libm-2.9.so
002e5000-002e6000 r--p 00023000 08:06 375135     /lib/tls/i686/cmov/libm-2.9.so
002e6000-002e7000 rw-p 00024000 08:06 375135     /lib/tls/i686/cmov/libm-2.9.so
00318000-00334000 r-xp 00000000 08:06 977846     /lib/ld-2.9.so
00334000-00335000 r--p 0001b000 08:06 977846     /lib/ld-2.9.so
00335000-00336000 rw-p 0001c000 08:06 977846     /lib/ld-2.9.so
0056e000-005a1000 r-xp 00000000 08:06 65575      /lib/libncurses.so.5.7
005a1000-005a3000 r--p 00033000 08:06 65575      /lib/libncurses.so.5.7
005a3000-005a4000 rw-p 00035000 08:06 65575      /lib/libncurses.so.5.7
00b67000-00b68000 r-xp 00000000 00:00 0          [vdso]
00bb6000-00bcb000 r-xp 00000000 08:06 375202     /lib/tls/i686/cmov/libpthread-2.9.so
00bcb000-00bcc000 r--p 00014000 08:06 375202     /lib/tls/i686/cmov/libpthread-2.9.so
00bcc000-00bcd000 rw-p 00015000 08:06 375202     /lib/tls/i686/cmov/libpthread-2.9.so
00bcd000-00bcf000 rw-p 00000000 00:00 0
00beb000-00bed000 r-xp 00000000 08:06 375134     /lib/tls/i686/cmov/libdl-2.9.so
00bed000-00bee000 r--p 00001000 08:06 375134     /lib/tls/i686/cmov/libdl-2.9.so
00bee000-00bef000 rw-p 00002000 08:06 375134     /lib/tls/i686/cmov/libdl-2.9.so
00c56000-00c7a000 r-xp 00000000 08:06 1140420    /usr/lib/libexpat.so.1.5.2
00c7a000-00c7c000 r--p 00023000 08:06 1140420    /usr/lib/libexpat.so.1.5.2
00c7c000-00c7d000 rw-p 00025000 08:06 1140420    /usr/lib/libexpat.so.1.5.2
00dce000-00dfa000 r-xp 00000000 08:06 65612      /lib/libreadline.so.5.2
00dfa000-00dfb000 ---p 0002c000 08:06 65612      /lib/libreadline.so.5.2
00dfb000-00dfc000 r--p 0002c000 08:06 65612      /lib/libreadline.so.5.2
00dfc000-00dff000 rw-p 0002d000 08:06 65612      /lib/libreadline.so.5.2
00dff000-00e00000 rw-p 00000000 00:00 0
08048000-0831c000 r-xp 00000000 08:06 1140349    /usr/bin/gdb
0831c000-0831d000 r--p 002d3000 08:06 1140349    /usr/bin/gdb
0831d000-08325000 rw-p 002d4000 08:06 1140349    /usr/bin/gdb
08325000-0833f000 rw-p 00000000 00:00 0
b8077000-b807a000 rw-p 00000000 00:00 0
b8096000-b8098000 rw-p 00000000 00:00 0
bfc57000-bfc6c000 rw-p 00000000 00:00 0          [stack]
'''
disasm = '''0x08083540 <main+0>:    lea    0x4(%esp),%ecx
0x08083544 <main+4>:    and    $0xfffffff0,%esp
0x08083547 <main+7>:    pushl  -0x4(%ecx)
0x0808354a <main+10>:   push   %ebp
0x0808354b <main+11>:   mov    %esp,%ebp
0x0808354d <main+13>:   push   %ecx
0x0808354e <main+14>:   sub    $0x14,%esp
0x08083551 <main+17>:   mov    (%ecx),%eax
0x08083553 <main+19>:   mov    0x4(%ecx),%edx
0x08083556 <main+22>:   lea    -0x14(%ebp),%ecx
0x08083559 <main+25>:   movl   $0x0,-0xc(%ebp)
0x08083560 <main+32>:   movl   $0x826bc68,-0x8(%ebp)
0x08083567 <main+39>:   mov    %eax,-0x14(%ebp)
0x0808356a <main+42>:   mov    %edx,-0x10(%ebp)
0x0808356d <main+45>:   mov    %ecx,(%esp)
0x08083570 <main+48>:   call   0x8083580 <gdb_main>
0x08083575 <main+53>:   add    $0x14,%esp
0x08083578 <main+56>:   pop    %ecx
0x08083579 <main+57>:   pop    %ebp
0x0808357a <main+58>:   lea    -0x4(%ecx),%esp
0x0808357d <main+61>:   ret
'''


class T(unittest.TestCase):
    '''Test Segfault Parser'''

    def test_invalid_00_registers(self):
        '''Require valid registers'''

        regs = 'a 0x10\nb !!!\n'
        self.assertRaises(ValueError, parse_segv.ParseSegv, regs, '', '')
        try:
            segv = parse_segv.ParseSegv(regs, '', '')
        except ValueError as e:
            self.assertTrue('invalid literal for int()' in str(e), str(e))

        regs = 'a 0x10'
        disasm = '0x08083540 <main+0>:    lea    0x4(%esp),%ecx\n'
        segv = parse_segv.ParseSegv(regs, disasm, '')
        self.assertEqual(segv.regs['a'], 0x10, segv)

        segv.regs = None
        self.assertRaises(ValueError, segv.parse_disassembly, '')

    def test_invalid_01_disassembly(self):
        '''Require valid disassembly'''
        regs = 'a 0x10'

        disasm = ''
        self.assertRaises(ValueError, parse_segv.ParseSegv, regs, disasm, '')

        disasm = 'Dump ...'
        self.assertRaises(ValueError, parse_segv.ParseSegv, regs, disasm, '')

        disasm = 'Dump ...\nmonkey'
        self.assertRaises(ValueError, parse_segv.ParseSegv, regs, disasm, '')

        disasm = 'monkey'
        self.assertRaises(ValueError, parse_segv.ParseSegv, regs, disasm, '')

        disasm = '0x1111111111: Cannot access memory at address 0x1111111111\n'
        segv = parse_segv.ParseSegv(regs, disasm, '')
        self.assertEqual(segv.pc, 0x1111111111, segv.pc)
        self.assertEqual(segv.insn, None, segv.insn)
        self.assertEqual(segv.src, None, segv.src)
        self.assertEqual(segv.dest, None, segv.dest)

        disasm = '0x2111111111: \n'
        segv = parse_segv.ParseSegv(regs, disasm, '')
        self.assertEqual(segv.pc, 0x2111111111, segv.pc)
        self.assertEqual(segv.insn, None, segv.insn)
        self.assertEqual(segv.src, None, segv.src)
        self.assertEqual(segv.dest, None, segv.dest)

        disasm = '0x8069ff0 <fopen@plt+132220>: cmpb   $0x0,(%eax,%ebx,1)\n'
        segv = parse_segv.ParseSegv(regs, disasm, '')
        self.assertEqual(segv.pc, 0x8069ff0, segv.pc)
        self.assertEqual(segv.insn, 'cmpb', segv.insn)
        self.assertEqual(segv.src, '$0x0', segv.src)
        self.assertEqual(segv.dest, '(%eax,%ebx,1)', segv.dest)

        disasm = '0xb765bb48 <_XSend+440>:  call   *0x40(%edi)\n'
        segv = parse_segv.ParseSegv(regs, disasm, '')
        self.assertEqual(segv.pc, 0xb765bb48, segv.pc)
        self.assertEqual(segv.insn, 'call', segv.insn)
        self.assertEqual(segv.src, '*0x40(%edi)', segv.src)
        self.assertEqual(segv.dest, None, segv.dest)

        disasm = '0xb7aae5a0:   call   0xb7a805af <_Unwind_Find_FDE@plt+111>\n'
        segv = parse_segv.ParseSegv(regs, disasm, '')
        self.assertEqual(segv.pc, 0xb7aae5a0, segv.pc)
        self.assertEqual(segv.insn, 'call', segv.insn)
        self.