testing/mozbase/mozdevice/tests/sut_fileMethods.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.

     1 #!/usr/bin/env python
     3 import hashlib
     4 import mock
     5 import mozdevice
     6 import mozlog
     7 import shutil
     8 import tempfile
     9 import os
    10 import unittest
    11 from sut import MockAgent
    14 class TestFileMethods(unittest.TestCase):
    15     """ Class to test misc file methods """
    17     content = "What is the answer to the life, universe and everything? 42"
    18     h = hashlib.md5()
    19     h.update(content)
    20     temp_hash = h.hexdigest()
    22     def test_validateFile(self):
    24         with tempfile.NamedTemporaryFile() as f:
    25             f.write(self.content)
    26             f.flush()
    28             # Test Valid Hashes
    29             commands_valid = [("hash /sdcard/test/file", self.temp_hash)]
    31             m = MockAgent(self, commands=commands_valid)
    32             d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    33             self.assertTrue(d.validateFile('/sdcard/test/file', f.name))
    35             # Test invalid hashes
    36             commands_invalid = [("hash /sdcard/test/file", "0this0hash0is0completely0invalid")]
    38             m = MockAgent(self, commands=commands_invalid)
    39             d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    40             self.assertFalse(d.validateFile('/sdcard/test/file', f.name))
    42     def test_getFile(self):
    44         fname = "/mnt/sdcard/file"
    45         commands = [("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)),
    46                     ("hash %s" % fname, self.temp_hash)]
    48         with tempfile.NamedTemporaryFile() as f:
    49             m = MockAgent(self, commands=commands)
    50             d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    51             # No error means success
    52             self.assertEqual(None, d.getFile(fname, f.name))
    54     def test_getDirectory(self):
    56         fname = "/mnt/sdcard/file"
    57         commands = [("isdir /mnt/sdcard", "TRUE"),
    58                     ("isdir /mnt/sdcard", "TRUE"),
    59                     ("cd /mnt/sdcard", ""),
    60                     ("ls", "file"),
    61                     ("isdir %s" % fname, "FALSE"),
    62                     ("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)),
    63                     ("hash %s" % fname, self.temp_hash)]
    65         tmpdir = tempfile.mkdtemp()
    66         m = MockAgent(self, commands=commands)
    67         d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    68         self.assertEqual(None, d.getDirectory("/mnt/sdcard", tmpdir))
    70         # Cleanup
    71         shutil.rmtree(tmpdir)
    73 if __name__ == '__main__':
    74     unittest.main()

mercurial