1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozdevice/tests/sut_push.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,84 @@ 1.4 +from sut import MockAgent 1.5 +import mozdevice 1.6 +import mozlog 1.7 +import unittest 1.8 +import hashlib 1.9 +import tempfile 1.10 +import os 1.11 + 1.12 +class PushTest(unittest.TestCase): 1.13 + 1.14 + def test_push(self): 1.15 + pushfile = "1234ABCD" 1.16 + mdsum = hashlib.md5() 1.17 + mdsum.update(pushfile) 1.18 + expectedResponse = mdsum.hexdigest() 1.19 + 1.20 + # (good response, no exception), (bad response, exception) 1.21 + for response in [ (expectedResponse, False), ("BADHASH", True) ]: 1.22 + cmd = "push /mnt/sdcard/foobar %s\r\n%s" % (len(pushfile), pushfile) 1.23 + a = MockAgent(self, commands = [("isdir /mnt/sdcard", "TRUE"), 1.24 + (cmd, response[0])]) 1.25 + exceptionThrown = False 1.26 + with tempfile.NamedTemporaryFile() as f: 1.27 + try: 1.28 + f.write(pushfile) 1.29 + f.flush() 1.30 + d = mozdevice.DroidSUT("127.0.0.1", port=a.port) 1.31 + d.pushFile(f.name, '/mnt/sdcard/foobar') 1.32 + except mozdevice.DMError, e: 1.33 + exceptionThrown = True 1.34 + self.assertEqual(exceptionThrown, response[1]) 1.35 + a.wait() 1.36 + 1.37 + def test_push_dir(self): 1.38 + pushfile = "1234ABCD" 1.39 + mdsum = hashlib.md5() 1.40 + mdsum.update(pushfile) 1.41 + expectedFileResponse = mdsum.hexdigest() 1.42 + 1.43 + tempdir = tempfile.mkdtemp() 1.44 + complex_path = os.path.join(tempdir, "baz") 1.45 + os.mkdir(complex_path) 1.46 + f = tempfile.NamedTemporaryFile(dir=complex_path) 1.47 + f.write(pushfile) 1.48 + f.flush() 1.49 + 1.50 + subTests = [ { 'cmds': [ ("isdir /mnt/sdcard/baz", "TRUE"), 1.51 + ("push /mnt/sdcard/baz/%s %s\r\n%s" % 1.52 + (os.path.basename(f.name), len(pushfile), 1.53 + pushfile), 1.54 + expectedFileResponse) ], 1.55 + 'expectException': False }, 1.56 + { 'cmds': [ ("isdir /mnt/sdcard/baz", "TRUE"), 1.57 + ("push /mnt/sdcard/baz/%s %s\r\n%s" % 1.58 + (os.path.basename(f.name), len(pushfile), 1.59 + pushfile), 1.60 + "BADHASH") ], 1.61 + 'expectException': True }, 1.62 + { 'cmds': [ ("isdir /mnt/sdcard/baz", "FALSE"), 1.63 + ("isdir /mnt", "FALSE"), 1.64 + ("mkdr /mnt", 1.65 + "##AGENT-WARNING## Could not create the directory /mnt") ], 1.66 + 'expectException': True }, 1.67 + 1.68 + ] 1.69 + 1.70 + for subTest in subTests: 1.71 + a = MockAgent(self, commands = subTest['cmds']) 1.72 + 1.73 + exceptionThrown = False 1.74 + try: 1.75 + d = mozdevice.DroidSUT("127.0.0.1", port=a.port, 1.76 + logLevel=mozlog.DEBUG) 1.77 + d.pushDir(tempdir, "/mnt/sdcard") 1.78 + except mozdevice.DMError, e: 1.79 + exceptionThrown = True 1.80 + self.assertEqual(exceptionThrown, subTest['expectException']) 1.81 + 1.82 + a.wait() 1.83 + 1.84 + # FIXME: delete directory when done 1.85 + 1.86 +if __name__ == '__main__': 1.87 + unittest.main()