testing/mozbase/mozdevice/tests/sut_push.py

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:7373d4a693bf
1 from sut import MockAgent
2 import mozdevice
3 import mozlog
4 import unittest
5 import hashlib
6 import tempfile
7 import os
8
9 class PushTest(unittest.TestCase):
10
11 def test_push(self):
12 pushfile = "1234ABCD"
13 mdsum = hashlib.md5()
14 mdsum.update(pushfile)
15 expectedResponse = mdsum.hexdigest()
16
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()
33
34 def test_push_dir(self):
35 pushfile = "1234ABCD"
36 mdsum = hashlib.md5()
37 mdsum.update(pushfile)
38 expectedFileResponse = mdsum.hexdigest()
39
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()
46
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 },
64
65 ]
66
67 for subTest in subTests:
68 a = MockAgent(self, commands = subTest['cmds'])
69
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'])
78
79 a.wait()
80
81 # FIXME: delete directory when done
82
83 if __name__ == '__main__':
84 unittest.main()

mercurial