testing/mozbase/mozdevice/tests/sut_fileMethods.py

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mozbase/mozdevice/tests/sut_fileMethods.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +import hashlib
     1.7 +import mock
     1.8 +import mozdevice
     1.9 +import mozlog
    1.10 +import shutil
    1.11 +import tempfile
    1.12 +import os
    1.13 +import unittest
    1.14 +from sut import MockAgent
    1.15 +
    1.16 +
    1.17 +class TestFileMethods(unittest.TestCase):
    1.18 +    """ Class to test misc file methods """
    1.19 +
    1.20 +    content = "What is the answer to the life, universe and everything? 42"
    1.21 +    h = hashlib.md5()
    1.22 +    h.update(content)
    1.23 +    temp_hash = h.hexdigest()
    1.24 +
    1.25 +    def test_validateFile(self):
    1.26 +
    1.27 +        with tempfile.NamedTemporaryFile() as f:
    1.28 +            f.write(self.content)
    1.29 +            f.flush()
    1.30 +
    1.31 +            # Test Valid Hashes
    1.32 +            commands_valid = [("hash /sdcard/test/file", self.temp_hash)]
    1.33 +
    1.34 +            m = MockAgent(self, commands=commands_valid)
    1.35 +            d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    1.36 +            self.assertTrue(d.validateFile('/sdcard/test/file', f.name))
    1.37 +
    1.38 +            # Test invalid hashes
    1.39 +            commands_invalid = [("hash /sdcard/test/file", "0this0hash0is0completely0invalid")]
    1.40 +
    1.41 +            m = MockAgent(self, commands=commands_invalid)
    1.42 +            d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    1.43 +            self.assertFalse(d.validateFile('/sdcard/test/file', f.name))
    1.44 +
    1.45 +    def test_getFile(self):
    1.46 +
    1.47 +        fname = "/mnt/sdcard/file"
    1.48 +        commands = [("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)),
    1.49 +                    ("hash %s" % fname, self.temp_hash)]
    1.50 +
    1.51 +        with tempfile.NamedTemporaryFile() as f:
    1.52 +            m = MockAgent(self, commands=commands)
    1.53 +            d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    1.54 +            # No error means success
    1.55 +            self.assertEqual(None, d.getFile(fname, f.name))
    1.56 +
    1.57 +    def test_getDirectory(self):
    1.58 +
    1.59 +        fname = "/mnt/sdcard/file"
    1.60 +        commands = [("isdir /mnt/sdcard", "TRUE"),
    1.61 +                    ("isdir /mnt/sdcard", "TRUE"),
    1.62 +                    ("cd /mnt/sdcard", ""),
    1.63 +                    ("ls", "file"),
    1.64 +                    ("isdir %s" % fname, "FALSE"),
    1.65 +                    ("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)),
    1.66 +                    ("hash %s" % fname, self.temp_hash)]
    1.67 +
    1.68 +        tmpdir = tempfile.mkdtemp()
    1.69 +        m = MockAgent(self, commands=commands)
    1.70 +        d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG)
    1.71 +        self.assertEqual(None, d.getDirectory("/mnt/sdcard", tmpdir))
    1.72 +
    1.73 +        # Cleanup
    1.74 +        shutil.rmtree(tmpdir)
    1.75 +
    1.76 +if __name__ == '__main__':
    1.77 +    unittest.main()

mercurial