ยปCore Development>Code coverage>Lib/test/test_uu.py

Python code coverage for Lib/test/test_uu.py

#countcontent
1n/a"""
2n/aTests for uu module.
3n/aNick Mathewson
4n/a"""
5n/a
6n/aimport unittest
7n/afrom test import support
8n/a
9n/aimport sys, os
10n/aimport uu
11n/aimport io
12n/a
13n/aplaintext = b"The smooth-scaled python crept over the sleeping dog\n"
14n/a
15n/aencodedtext = b"""\
16n/aM5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
17n/a(:6YG(&1O9PH """
18n/a
19n/a# Stolen from io.py
20n/aclass FakeIO(io.TextIOWrapper):
21n/a """Text I/O implementation using an in-memory buffer.
22n/a
23n/a Can be a used as a drop-in replacement for sys.stdin and sys.stdout.
24n/a """
25n/a
26n/a # XXX This is really slow, but fully functional
27n/a
28n/a def __init__(self, initial_value="", encoding="utf-8",
29n/a errors="strict", newline="\n"):
30n/a super(FakeIO, self).__init__(io.BytesIO(),
31n/a encoding=encoding,
32n/a errors=errors,
33n/a newline=newline)
34n/a self._encoding = encoding
35n/a self._errors = errors
36n/a if initial_value:
37n/a if not isinstance(initial_value, str):
38n/a initial_value = str(initial_value)
39n/a self.write(initial_value)
40n/a self.seek(0)
41n/a
42n/a def getvalue(self):
43n/a self.flush()
44n/a return self.buffer.getvalue().decode(self._encoding, self._errors)
45n/a
46n/a
47n/adef encodedtextwrapped(mode, filename):
48n/a return (bytes("begin %03o %s\n" % (mode, filename), "ascii") +
49n/a encodedtext + b"\n \nend\n")
50n/a
51n/aclass UUTest(unittest.TestCase):
52n/a
53n/a def test_encode(self):
54n/a inp = io.BytesIO(plaintext)
55n/a out = io.BytesIO()
56n/a uu.encode(inp, out, "t1")
57n/a self.assertEqual(out.getvalue(), encodedtextwrapped(0o666, "t1"))
58n/a inp = io.BytesIO(plaintext)
59n/a out = io.BytesIO()
60n/a uu.encode(inp, out, "t1", 0o644)
61n/a self.assertEqual(out.getvalue(), encodedtextwrapped(0o644, "t1"))
62n/a
63n/a def test_decode(self):
64n/a inp = io.BytesIO(encodedtextwrapped(0o666, "t1"))
65n/a out = io.BytesIO()
66n/a uu.decode(inp, out)
67n/a self.assertEqual(out.getvalue(), plaintext)
68n/a inp = io.BytesIO(
69n/a b"UUencoded files may contain many lines,\n" +
70n/a b"even some that have 'begin' in them.\n" +
71n/a encodedtextwrapped(0o666, "t1")
72n/a )
73n/a out = io.BytesIO()
74n/a uu.decode(inp, out)
75n/a self.assertEqual(out.getvalue(), plaintext)
76n/a
77n/a def test_truncatedinput(self):
78n/a inp = io.BytesIO(b"begin 644 t1\n" + encodedtext)
79n/a out = io.BytesIO()
80n/a try:
81n/a uu.decode(inp, out)
82n/a self.fail("No exception raised")
83n/a except uu.Error as e:
84n/a self.assertEqual(str(e), "Truncated input file")
85n/a
86n/a def test_missingbegin(self):
87n/a inp = io.BytesIO(b"")
88n/a out = io.BytesIO()
89n/a try:
90n/a uu.decode(inp, out)
91n/a self.fail("No exception raised")
92n/a except uu.Error as e:
93n/a self.assertEqual(str(e), "No valid begin line found in input file")
94n/a
95n/a def test_garbage_padding(self):
96n/a # Issue #22406
97n/a encodedtext = (
98n/a b"begin 644 file\n"
99n/a # length 1; bits 001100 111111 111111 111111
100n/a b"\x21\x2C\x5F\x5F\x5F\n"
101n/a b"\x20\n"
102n/a b"end\n"
103n/a )
104n/a plaintext = b"\x33" # 00110011
105n/a
106n/a with self.subTest("uu.decode()"):
107n/a inp = io.BytesIO(encodedtext)
108n/a out = io.BytesIO()
109n/a uu.decode(inp, out, quiet=True)
110n/a self.assertEqual(out.getvalue(), plaintext)
111n/a
112n/a with self.subTest("uu_codec"):
113n/a import codecs
114n/a decoded = codecs.decode(encodedtext, "uu_codec")
115n/a self.assertEqual(decoded, plaintext)
116n/a
117n/aclass UUStdIOTest(unittest.TestCase):
118n/a
119n/a def setUp(self):
120n/a self.stdin = sys.stdin
121n/a self.stdout = sys.stdout
122n/a
123n/a def tearDown(self):
124n/a sys.stdin = self.stdin
125n/a sys.stdout = self.stdout
126n/a
127n/a def test_encode(self):
128n/a sys.stdin = FakeIO(plaintext.decode("ascii"))
129n/a sys.stdout = FakeIO()
130n/a uu.encode("-", "-", "t1", 0o666)
131n/a self.assertEqual(sys.stdout.getvalue(),
132n/a encodedtextwrapped(0o666, "t1").decode("ascii"))
133n/a
134n/a def test_decode(self):
135n/a sys.stdin = FakeIO(encodedtextwrapped(0o666, "t1").decode("ascii"))
136n/a sys.stdout = FakeIO()
137n/a uu.decode("-", "-")
138n/a stdout = sys.stdout
139n/a sys.stdout = self.stdout
140n/a sys.stdin = self.stdin
141n/a self.assertEqual(stdout.getvalue(), plaintext.decode("ascii"))
142n/a
143n/aclass UUFileTest(unittest.TestCase):
144n/a
145n/a def _kill(self, f):
146n/a # close and remove file
147n/a if f is None:
148n/a return
149n/a try:
150n/a f.close()
151n/a except (SystemExit, KeyboardInterrupt):
152n/a raise
153n/a except:
154n/a pass
155n/a try:
156n/a os.unlink(f.name)
157n/a except (SystemExit, KeyboardInterrupt):
158n/a raise
159n/a except:
160n/a pass
161n/a
162n/a def setUp(self):
163n/a self.tmpin = support.TESTFN + "i"
164n/a self.tmpout = support.TESTFN + "o"
165n/a
166n/a def tearDown(self):
167n/a del self.tmpin
168n/a del self.tmpout
169n/a
170n/a def test_encode(self):
171n/a fin = fout = None
172n/a try:
173n/a support.unlink(self.tmpin)
174n/a fin = open(self.tmpin, 'wb')
175n/a fin.write(plaintext)
176n/a fin.close()
177n/a
178n/a fin = open(self.tmpin, 'rb')
179n/a fout = open(self.tmpout, 'wb')
180n/a uu.encode(fin, fout, self.tmpin, mode=0o644)
181n/a fin.close()
182n/a fout.close()
183n/a
184n/a fout = open(self.tmpout, 'rb')
185n/a s = fout.read()
186n/a fout.close()
187n/a self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
188n/a
189n/a # in_file and out_file as filenames
190n/a uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
191n/a fout = open(self.tmpout, 'rb')
192n/a s = fout.read()
193n/a fout.close()
194n/a self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
195n/a
196n/a finally:
197n/a self._kill(fin)
198n/a self._kill(fout)
199n/a
200n/a def test_decode(self):
201n/a f = None
202n/a try:
203n/a support.unlink(self.tmpin)
204n/a f = open(self.tmpin, 'wb')
205n/a f.write(encodedtextwrapped(0o644, self.tmpout))
206n/a f.close()
207n/a
208n/a f = open(self.tmpin, 'rb')
209n/a uu.decode(f)
210n/a f.close()
211n/a
212n/a f = open(self.tmpout, 'rb')
213n/a s = f.read()
214n/a f.close()
215n/a self.assertEqual(s, plaintext)
216n/a # XXX is there an xp way to verify the mode?
217n/a finally:
218n/a self._kill(f)
219n/a
220n/a def test_decode_filename(self):
221n/a f = None
222n/a try:
223n/a support.unlink(self.tmpin)
224n/a f = open(self.tmpin, 'wb')
225n/a f.write(encodedtextwrapped(0o644, self.tmpout))
226n/a f.close()
227n/a
228n/a uu.decode(self.tmpin)
229n/a
230n/a f = open(self.tmpout, 'rb')
231n/a s = f.read()
232n/a f.close()
233n/a self.assertEqual(s, plaintext)
234n/a finally:
235n/a self._kill(f)
236n/a
237n/a def test_decodetwice(self):
238n/a # Verify that decode() will refuse to overwrite an existing file
239n/a f = None
240n/a try:
241n/a f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout))
242n/a
243n/a f = open(self.tmpin, 'rb')
244n/a uu.decode(f)
245n/a f.close()
246n/a
247n/a f = open(self.tmpin, 'rb')
248n/a self.assertRaises(uu.Error, uu.decode, f)
249n/a f.close()
250n/a finally:
251n/a self._kill(f)
252n/a
253n/adef test_main():
254n/a support.run_unittest(UUTest,
255n/a UUStdIOTest,
256n/a UUFileTest,
257n/a )
258n/a
259n/aif __name__=="__main__":
260n/a test_main()