michael@0: from sut import MockAgent michael@0: import mozdevice michael@0: import mozlog michael@0: import unittest michael@0: import hashlib michael@0: import tempfile michael@0: import os michael@0: michael@0: class PushTest(unittest.TestCase): michael@0: michael@0: def test_push(self): michael@0: pushfile = "1234ABCD" michael@0: mdsum = hashlib.md5() michael@0: mdsum.update(pushfile) michael@0: expectedResponse = mdsum.hexdigest() michael@0: michael@0: # (good response, no exception), (bad response, exception) michael@0: for response in [ (expectedResponse, False), ("BADHASH", True) ]: michael@0: cmd = "push /mnt/sdcard/foobar %s\r\n%s" % (len(pushfile), pushfile) michael@0: a = MockAgent(self, commands = [("isdir /mnt/sdcard", "TRUE"), michael@0: (cmd, response[0])]) michael@0: exceptionThrown = False michael@0: with tempfile.NamedTemporaryFile() as f: michael@0: try: michael@0: f.write(pushfile) michael@0: f.flush() michael@0: d = mozdevice.DroidSUT("127.0.0.1", port=a.port) michael@0: d.pushFile(f.name, '/mnt/sdcard/foobar') michael@0: except mozdevice.DMError, e: michael@0: exceptionThrown = True michael@0: self.assertEqual(exceptionThrown, response[1]) michael@0: a.wait() michael@0: michael@0: def test_push_dir(self): michael@0: pushfile = "1234ABCD" michael@0: mdsum = hashlib.md5() michael@0: mdsum.update(pushfile) michael@0: expectedFileResponse = mdsum.hexdigest() michael@0: michael@0: tempdir = tempfile.mkdtemp() michael@0: complex_path = os.path.join(tempdir, "baz") michael@0: os.mkdir(complex_path) michael@0: f = tempfile.NamedTemporaryFile(dir=complex_path) michael@0: f.write(pushfile) michael@0: f.flush() michael@0: michael@0: subTests = [ { 'cmds': [ ("isdir /mnt/sdcard/baz", "TRUE"), michael@0: ("push /mnt/sdcard/baz/%s %s\r\n%s" % michael@0: (os.path.basename(f.name), len(pushfile), michael@0: pushfile), michael@0: expectedFileResponse) ], michael@0: 'expectException': False }, michael@0: { 'cmds': [ ("isdir /mnt/sdcard/baz", "TRUE"), michael@0: ("push /mnt/sdcard/baz/%s %s\r\n%s" % michael@0: (os.path.basename(f.name), len(pushfile), michael@0: pushfile), michael@0: "BADHASH") ], michael@0: 'expectException': True }, michael@0: { 'cmds': [ ("isdir /mnt/sdcard/baz", "FALSE"), michael@0: ("isdir /mnt", "FALSE"), michael@0: ("mkdr /mnt", michael@0: "##AGENT-WARNING## Could not create the directory /mnt") ], michael@0: 'expectException': True }, michael@0: michael@0: ] michael@0: michael@0: for subTest in subTests: michael@0: a = MockAgent(self, commands = subTest['cmds']) michael@0: michael@0: exceptionThrown = False michael@0: try: michael@0: d = mozdevice.DroidSUT("127.0.0.1", port=a.port, michael@0: logLevel=mozlog.DEBUG) michael@0: d.pushDir(tempdir, "/mnt/sdcard") michael@0: except mozdevice.DMError, e: michael@0: exceptionThrown = True michael@0: self.assertEqual(exceptionThrown, subTest['expectException']) michael@0: michael@0: a.wait() michael@0: michael@0: # FIXME: delete directory when done michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()