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.

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

mercurial