|
1 #!/usr/bin/env python |
|
2 |
|
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 |
|
12 |
|
13 |
|
14 class TestFileMethods(unittest.TestCase): |
|
15 """ Class to test misc file methods """ |
|
16 |
|
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() |
|
21 |
|
22 def test_validateFile(self): |
|
23 |
|
24 with tempfile.NamedTemporaryFile() as f: |
|
25 f.write(self.content) |
|
26 f.flush() |
|
27 |
|
28 # Test Valid Hashes |
|
29 commands_valid = [("hash /sdcard/test/file", self.temp_hash)] |
|
30 |
|
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)) |
|
34 |
|
35 # Test invalid hashes |
|
36 commands_invalid = [("hash /sdcard/test/file", "0this0hash0is0completely0invalid")] |
|
37 |
|
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)) |
|
41 |
|
42 def test_getFile(self): |
|
43 |
|
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)] |
|
47 |
|
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)) |
|
53 |
|
54 def test_getDirectory(self): |
|
55 |
|
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)] |
|
64 |
|
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)) |
|
69 |
|
70 # Cleanup |
|
71 shutil.rmtree(tmpdir) |
|
72 |
|
73 if __name__ == '__main__': |
|
74 unittest.main() |