summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozdevice/tests/sut_push.py
blob: 023d5315cca7d2b0b56e135169feee2466ba89ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
from sut import MockAgent
import mozfile
import mozdevice
import logging
import unittest
import hashlib
import tempfile
import os


class PushTest(unittest.TestCase):

    def test_push(self):
        pushfile = "1234ABCD"
        mdsum = hashlib.md5()
        mdsum.update(pushfile)
        expectedResponse = mdsum.hexdigest()

        # (good response, no exception), (bad response, exception)
        for response in [(expectedResponse, False), ("BADHASH", True)]:
            cmd = "push /mnt/sdcard/foobar %s\r\n%s" % (len(pushfile), pushfile)
            a = MockAgent(self, commands=[("isdir /mnt/sdcard", "TRUE"),
                                          (cmd, response[0])])
            exceptionThrown = False
            with tempfile.NamedTemporaryFile() as f:
                try:
                    f.write(pushfile)
                    f.flush()
                    d = mozdevice.DroidSUT("127.0.0.1", port=a.port)
                    d.pushFile(f.name, '/mnt/sdcard/foobar')
                except mozdevice.DMError:
                    exceptionThrown = True
                self.assertEqual(exceptionThrown, response[1])
            a.wait()

    def test_push_dir(self):
        pushfile = "1234ABCD"
        mdsum = hashlib.md5()
        mdsum.update(pushfile)
        expectedFileResponse = mdsum.hexdigest()

        tempdir = tempfile.mkdtemp()
        self.addCleanup(mozfile.remove, tempdir)
        complex_path = os.path.join(tempdir, "baz")
        os.mkdir(complex_path)
        f = tempfile.NamedTemporaryFile(dir=complex_path)
        f.write(pushfile)
        f.flush()

        subTests = [{'cmds': [("isdir /mnt/sdcard/baz", "TRUE"),
                              ("push /mnt/sdcard/baz/%s %s\r\n%s" %
                               (os.path.basename(f.name), len(pushfile),
                                pushfile),
                               expectedFileResponse)],
                     'expectException': False},
                    {'cmds': [("isdir /mnt/sdcard/baz", "TRUE"),
                              ("push /mnt/sdcard/baz/%s %s\r\n%s" %
                               (os.path.basename(f.name), len(pushfile),
                                pushfile),
                               "BADHASH")],
                     'expectException': True},
                    {'cmds': [("isdir /mnt/sdcard/baz", "FALSE"),
                              ('info os', 'android'),
                              ("isdir /mnt", "FALSE"),
                              ("mkdr /mnt",
                               "##AGENT-WARNING## Could not create the directory /mnt")],
                     'expectException': True},

                    ]

        for subTest in subTests:
            a = MockAgent(self, commands=subTest['cmds'])

            exceptionThrown = False
            try:
                d = mozdevice.DroidSUT("127.0.0.1", port=a.port,
                                       logLevel=logging.DEBUG)
                d.pushDir(tempdir, "/mnt/sdcard")
            except mozdevice.DMError:
                exceptionThrown = True
            self.assertEqual(exceptionThrown, subTest['expectException'])

            a.wait()

        # FIXME: delete directory when done

if __name__ == '__main__':
    unittest.main()