Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | import hashlib |
michael@0 | 4 | import mock |
michael@0 | 5 | import mozdevice |
michael@0 | 6 | import mozlog |
michael@0 | 7 | import shutil |
michael@0 | 8 | import tempfile |
michael@0 | 9 | import os |
michael@0 | 10 | import unittest |
michael@0 | 11 | from sut import MockAgent |
michael@0 | 12 | |
michael@0 | 13 | |
michael@0 | 14 | class TestFileMethods(unittest.TestCase): |
michael@0 | 15 | """ Class to test misc file methods """ |
michael@0 | 16 | |
michael@0 | 17 | content = "What is the answer to the life, universe and everything? 42" |
michael@0 | 18 | h = hashlib.md5() |
michael@0 | 19 | h.update(content) |
michael@0 | 20 | temp_hash = h.hexdigest() |
michael@0 | 21 | |
michael@0 | 22 | def test_validateFile(self): |
michael@0 | 23 | |
michael@0 | 24 | with tempfile.NamedTemporaryFile() as f: |
michael@0 | 25 | f.write(self.content) |
michael@0 | 26 | f.flush() |
michael@0 | 27 | |
michael@0 | 28 | # Test Valid Hashes |
michael@0 | 29 | commands_valid = [("hash /sdcard/test/file", self.temp_hash)] |
michael@0 | 30 | |
michael@0 | 31 | m = MockAgent(self, commands=commands_valid) |
michael@0 | 32 | d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) |
michael@0 | 33 | self.assertTrue(d.validateFile('/sdcard/test/file', f.name)) |
michael@0 | 34 | |
michael@0 | 35 | # Test invalid hashes |
michael@0 | 36 | commands_invalid = [("hash /sdcard/test/file", "0this0hash0is0completely0invalid")] |
michael@0 | 37 | |
michael@0 | 38 | m = MockAgent(self, commands=commands_invalid) |
michael@0 | 39 | d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) |
michael@0 | 40 | self.assertFalse(d.validateFile('/sdcard/test/file', f.name)) |
michael@0 | 41 | |
michael@0 | 42 | def test_getFile(self): |
michael@0 | 43 | |
michael@0 | 44 | fname = "/mnt/sdcard/file" |
michael@0 | 45 | commands = [("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)), |
michael@0 | 46 | ("hash %s" % fname, self.temp_hash)] |
michael@0 | 47 | |
michael@0 | 48 | with tempfile.NamedTemporaryFile() as f: |
michael@0 | 49 | m = MockAgent(self, commands=commands) |
michael@0 | 50 | d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) |
michael@0 | 51 | # No error means success |
michael@0 | 52 | self.assertEqual(None, d.getFile(fname, f.name)) |
michael@0 | 53 | |
michael@0 | 54 | def test_getDirectory(self): |
michael@0 | 55 | |
michael@0 | 56 | fname = "/mnt/sdcard/file" |
michael@0 | 57 | commands = [("isdir /mnt/sdcard", "TRUE"), |
michael@0 | 58 | ("isdir /mnt/sdcard", "TRUE"), |
michael@0 | 59 | ("cd /mnt/sdcard", ""), |
michael@0 | 60 | ("ls", "file"), |
michael@0 | 61 | ("isdir %s" % fname, "FALSE"), |
michael@0 | 62 | ("pull %s" % fname, "%s,%s\n%s" % (fname, len(self.content), self.content)), |
michael@0 | 63 | ("hash %s" % fname, self.temp_hash)] |
michael@0 | 64 | |
michael@0 | 65 | tmpdir = tempfile.mkdtemp() |
michael@0 | 66 | m = MockAgent(self, commands=commands) |
michael@0 | 67 | d = mozdevice.DroidSUT("127.0.0.1", port=m.port, logLevel=mozlog.DEBUG) |
michael@0 | 68 | self.assertEqual(None, d.getDirectory("/mnt/sdcard", tmpdir)) |
michael@0 | 69 | |
michael@0 | 70 | # Cleanup |
michael@0 | 71 | shutil.rmtree(tmpdir) |
michael@0 | 72 | |
michael@0 | 73 | if __name__ == '__main__': |
michael@0 | 74 | unittest.main() |