testing/mozbase/mozdevice/tests/sut_push.py

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 from sut import MockAgent
     2 import mozdevice
     3 import mozlog
     4 import unittest
     5 import hashlib
     6 import tempfile
     7 import os
     9 class PushTest(unittest.TestCase):
    11     def test_push(self):
    12         pushfile = "1234ABCD"
    13         mdsum = hashlib.md5()
    14         mdsum.update(pushfile)
    15         expectedResponse = mdsum.hexdigest()
    17         # (good response, no exception), (bad response, exception)
    18         for response in [ (expectedResponse, False), ("BADHASH", True) ]:
    19             cmd = "push /mnt/sdcard/foobar %s\r\n%s" % (len(pushfile), pushfile)
    20             a = MockAgent(self, commands = [("isdir /mnt/sdcard", "TRUE"),
    21                                             (cmd, response[0])])
    22             exceptionThrown = False
    23             with tempfile.NamedTemporaryFile() as f:
    24                 try:
    25                     f.write(pushfile)
    26                     f.flush()
    27                     d = mozdevice.DroidSUT("127.0.0.1", port=a.port)
    28                     d.pushFile(f.name, '/mnt/sdcard/foobar')
    29                 except mozdevice.DMError, e:
    30                     exceptionThrown = True
    31                 self.assertEqual(exceptionThrown, response[1])
    32             a.wait()
    34     def test_push_dir(self):
    35         pushfile = "1234ABCD"
    36         mdsum = hashlib.md5()
    37         mdsum.update(pushfile)
    38         expectedFileResponse = mdsum.hexdigest()
    40         tempdir = tempfile.mkdtemp()
    41         complex_path = os.path.join(tempdir, "baz")
    42         os.mkdir(complex_path)
    43         f = tempfile.NamedTemporaryFile(dir=complex_path)
    44         f.write(pushfile)
    45         f.flush()
    47         subTests = [ { 'cmds': [ ("isdir /mnt/sdcard/baz", "TRUE"),
    48                                  ("push /mnt/sdcard/baz/%s %s\r\n%s" %
    49                                   (os.path.basename(f.name), len(pushfile),
    50                                    pushfile),
    51                                   expectedFileResponse) ],
    52                        'expectException': False },
    53                      { 'cmds': [ ("isdir /mnt/sdcard/baz", "TRUE"),
    54                                  ("push /mnt/sdcard/baz/%s %s\r\n%s" %
    55                                   (os.path.basename(f.name), len(pushfile),
    56                                    pushfile),
    57                                   "BADHASH") ],
    58                        'expectException': True },
    59                      { 'cmds': [ ("isdir /mnt/sdcard/baz", "FALSE"),
    60                                  ("isdir /mnt", "FALSE"),
    61                                  ("mkdr /mnt",
    62                                   "##AGENT-WARNING## Could not create the directory /mnt") ],
    63                        'expectException': True },
    65                      ]
    67         for subTest in subTests:
    68             a = MockAgent(self, commands = subTest['cmds'])
    70             exceptionThrown = False
    71             try:
    72                 d = mozdevice.DroidSUT("127.0.0.1", port=a.port,
    73                                        logLevel=mozlog.DEBUG)
    74                 d.pushDir(tempdir, "/mnt/sdcard")
    75             except mozdevice.DMError, e:
    76                 exceptionThrown = True
    77             self.assertEqual(exceptionThrown, subTest['expectException'])
    79             a.wait()
    81         # FIXME: delete directory when done
    83 if __name__ == '__main__':
    84     unittest.main()

mercurial