michael@0: #!/usr/bin/env python michael@0: michael@0: import hashlib michael@0: import mock michael@0: import mozdevice michael@0: import mozlog michael@0: import shutil michael@0: import tempfile michael@0: import os michael@0: import unittest michael@0: from sut import MockAgent michael@0: michael@0: michael@0: class TestFileMethods(unittest.TestCase): michael@0: """ Class to test misc file methods """ michael@0: michael@0: content = "What is the answer to the life, universe and everything? 42" michael@0: h = hashlib.md5() michael@0: h.update(content) michael@0: temp_hash = h.hexdigest() michael@0: michael@0: def test_validateFile(self): michael@0: michael@0: with tempfile.NamedTemporaryFile() as f: michael@0: f.write(self.content) michael@0: f.flush() michael@0: michael@0: # Test Valid Hashes michael@0: commands_valid = [("hash /sdcard/test/file", self.temp_hash)] michael@0: michael@0: m = MockAgent(self, commands=commands_valid) michael@0: d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) michael@0: self.assertTrue(d.validateFile('/sdcard/test/file', f.name)) michael@0: michael@0: # Test invalid hashes michael@0: commands_invalid = [("hash /sdcard/test/file", "0this0hash0is0completely0invalid")] michael@0: michael@0: m = MockAgent(self, commands=commands_invalid) michael@0: d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) michael@0: self.assertFalse(d.validateFile('/sdcard/test/file', f.name)) michael@0: michael@0: def test_getFile(self): michael@0: michael@0: fname = "/mnt/sdcard/file" michael@0: commands = [("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)), michael@0: ("hash %s" % fname, self.temp_hash)] michael@0: michael@0: with tempfile.NamedTemporaryFile() as f: michael@0: m = MockAgent(self, commands=commands) michael@0: d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) michael@0: # No error means success michael@0: self.assertEqual(None, d.getFile(fname, f.name)) michael@0: michael@0: def test_getDirectory(self): michael@0: michael@0: fname = "/mnt/sdcard/file" michael@0: commands = [("isdir /mnt/sdcard", "TRUE"), michael@0: ("isdir /mnt/sdcard", "TRUE"), michael@0: ("cd /mnt/sdcard", ""), michael@0: ("ls", "file"), michael@0: ("isdir %s" % fname, "FALSE"), michael@0: ("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)), michael@0: ("hash %s" % fname, self.temp_hash)] michael@0: michael@0: tmpdir = tempfile.mkdtemp() michael@0: m = MockAgent(self, commands=commands) michael@0: d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) michael@0: self.assertEqual(None, d.getDirectory("/mnt/sdcard", tmpdir)) michael@0: michael@0: # Cleanup michael@0: shutil.rmtree(tmpdir) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()