File: test__ped_geometry.py

package info (click to toggle)
pyparted 3.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 952 kB
  • sloc: ansic: 7,453; python: 4,579; makefile: 91; sh: 4
file content (379 lines) | stat: -rwxr-xr-x 13,336 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#
# Copyright The pyparted Project Authors
# SPDX-License-Identifier: GPL-2.0-or-later
#

import _ped
from tests.baseclass import RequiresDevice

# One class per method, multiple tests per class.  For these simple methods,
# that seems like good organization.  More complicated methods may require
# multiple classes and their own test suite.
class GeometryNewTestCase(RequiresDevice):
    def runTest(self):
        # Check that not passing args to _ped.Geometry.__init__ is caught.
        self.assertRaises(TypeError, _ped.Geometry)

        # Or passing in the parameters in the wrong order.
        self.assertRaises(TypeError, _ped.Geometry, 0, self._device, 100)

        # And then the correct ways of creating a _ped.Geometry.
        self.assertIsInstance(_ped.Geometry(self._device, 0, 100), _ped.Geometry)
        self.assertIsInstance(_ped.Geometry(self._device, 0, 100, 101), _ped.Geometry)


class GeometryGetSetTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        # Test that passing the kwargs to __init__ works.
        self.assertIsInstance(self.g, _ped.Geometry)
        self.assertEqual(self.g.start, 0)
        self.assertEqual(self.g.length, 100)
        self.assertEqual(self.g.end, 99)

        # Test that setting directly and getting with getattr works.
        self.g.start = 10
        self.g.length = 90
        self.g.end = 99

        self.assertEqual(getattr(self.g, "start"), 10)
        self.assertEqual(getattr(self.g, "length"), 90)
        self.assertEqual(getattr(self.g, "end"), 99)

        # Check that setting with setattr and getting directly works.
        setattr(self.g, "start", 20)
        setattr(self.g, "length", 80)
        setattr(self.g, "end", 99)

        self.assertEqual(self.g.start, 20)
        self.assertEqual(self.g.length, 80)
        self.assertEqual(self.g.end, 99)

        # Check that values have the right type.
        self.assertRaises(TypeError, setattr, self.g, "start", "string")

        # Check that looking for invalid attributes fails properly.
        self.assertRaises(AttributeError, getattr, self.g, "blah")


class GeometryDuplicateTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=0, length=100)
        self.dup = self.g.duplicate()

    def runTest(self):
        self.assertEqual(self.g.start, self.dup.start)
        self.assertEqual(self.g.length, self.dup.length)
        self.assertEqual(self.g.end, self.dup.end)


class GeometryIntersectTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g1 = _ped.Geometry(self._device, start=0, length=100)
        self.g2 = _ped.Geometry(self._device, start=0, length=100)
        self.i = None

    def runTest(self):
        # g1 and g2 are the same, so their intersection is the same
        self.i = self.g1.intersect(self.g2)
        self.assertEqual(self.i.start, self.g1.start)
        self.assertEqual(self.i.end, self.g1.end)
        self.assertEqual(self.i.length, self.g1.length)

        # g2 is the second half of g1, so their intersection is the same as g2.
        self.g2.set_start(50)
        self.i = self.g1.intersect(self.g2)
        self.assertEqual(self.i.start, self.g2.start)
        self.assertEqual(self.i.end, self.g2.end)
        self.assertEqual(self.i.length, self.g2.length)

        # g2 only partially overlaps the end of g1, so they have a more
        # interesting intersection.
        self.g1.set_end(75)
        self.i = self.g1.intersect(self.g2)
        self.assertEqual(self.i.start, self.g2.start)
        self.assertEqual(self.i.end, self.g1.end)
        self.assertEqual(self.i.length, 26)

        # g1 and g2 do not overlap at all, so they have no intersection.
        self.g1.set(0, 25)
        self.g2.set(50, 100)
        self.assertRaises(ArithmeticError, self.g1.intersect, self.g2)


class GeometrySetTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        self.assertEqual(self.g.start, 0)
        self.assertEqual(self.g.length, 100)

        # Setting a negative for either value, or a length past the end of
        # the device should fail.
        self.assertRaises(_ped.CreateException, self.g.set, 100, -1000)


class GeometrySetStartTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        self.g.set_start(10)
        self.assertEqual(self.g.start, 10)
        self.assertEqual(self.g.length, 90)
        self.assertEqual(self.g.end, 99)


class GeometrySetEndTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        self.g.set_end(50)
        self.assertEqual(self.g.start, 0)
        self.assertEqual(self.g.length, 51)
        self.assertEqual(self.g.end, 50)

        # Setting a negative end or or before the start should fail.
        self.assertRaises(_ped.CreateException, self.g.set_end, -1)
        self.g.set_start(10)
        self.assertRaises(_ped.CreateException, self.g.set_end, 5)


class GeometryTestOverlapTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g1 = _ped.Geometry(self._device, start=0, length=100)
        self.g2 = _ped.Geometry(self._device, start=50, length=100)

    def runTest(self):
        # g2 occupies the second half of g1, so they overlap.
        self.assertTrue(self.g1.test_overlap(self.g2))

        # g2 is entirely contained within g1, so they overlap.
        self.g2.set_end(75)
        self.assertTrue(self.g1.test_overlap(self.g2))

        # g1 goes from inside g2 to the end, so they overlap.
        self.g1.set_start(60)
        self.assertTrue(self.g1.test_overlap(self.g2))

        # g2 exists entirely before g1, so they do not overlap.
        self.g2.set(10, 10)
        self.assertFalse(self.g1.test_overlap(self.g2))


class GeometryTestInsideTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g1 = _ped.Geometry(self._device, start=0, length=100)
        self.g2 = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        # g1 and g2 are the same, so they exist inside each other.
        self.assertTrue(self.g1.test_inside(self.g2))
        self.assertTrue(self.g2.test_inside(self.g1))

        # g2 is entirely contained within g1, so it's inside.
        self.g2.set_end(75)
        self.assertTrue(self.g1.test_inside(self.g2))
        self.assertFalse(self.g2.test_inside(self.g1))

        # g1 goes from inside g2 to the end, so it's not inside.
        self.g1.set_start(60)
        self.assertFalse(self.g1.test_inside(self.g2))
        self.assertFalse(self.g2.test_inside(self.g1))

        # g2 exists entirely before g1, so it's not inside.
        self.g2.set(10, 10)
        self.assertFalse(self.g1.test_inside(self.g2))
        self.assertFalse(self.g2.test_inside(self.g1))


class GeometryTestEqualTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g1 = _ped.Geometry(self._device, start=0, length=100)
        self.g2 = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        # g1 and g2 have the same start and end.
        self.assertTrue(self.g1.test_equal(self.g2))

        # g1 and g2 have the same end, but different starts.
        self.g2.set_start(5)
        self.assertFalse(self.g1.test_equal(self.g2))

        # g1 and g2 have the same start, but different ends.
        self.g2.set_start(5)
        self.g2.set_end(50)
        self.assertFalse(self.g1.test_equal(self.g2))


class GeometryTestSectorInsideTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=10, length=100)

    def runTest(self):
        # First check the boundary conditions.
        self.assertTrue(self.g.test_sector_inside(10))
        self.assertTrue(self.g.test_sector_inside(109))
        self.assertFalse(self.g.test_sector_inside(110))

        # Then some sectors that are obviously out.
        self.assertFalse(self.g.test_sector_inside(0))
        self.assertFalse(self.g.test_sector_inside(1000))
        self.assertFalse(self.g.test_sector_inside(-1))


class GeometryReadTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=10, length=100)

    def runTest(self):
        # First try to read from a device that isn't open yet.
        self.assertRaises(_ped.IOException, self.g.read, 0, 10)

        # Our initial device is just full of zeros, so this should read a
        # whole lot of nothing.
        self._device.open()
        self.assertEqual(self.g.read(0, 10), "")

        # Test bad parameter passing.
        self.assertRaises(_ped.IOException, self.g.read, -10, 10)
        self.assertRaises(_ped.IOException, self.g.read, 0, -10)
        self.assertRaises(TypeError, self.g.read, None, None)

        # Can't read past the end of the geometry.
        self.assertRaises(_ped.IOException, self.g.read, 200, 1)
        self.assertRaises(_ped.IOException, self.g.read, 0, 200)

        # Now try writing something to the device, then reading to see if
        # we get the same thing back.
        self.g.write("1111111111", 0, 1)
        self.assertEqual(self.g.read(0, 10), "1111111111")

        # Write five bytes from the string to the geometry, so there's only
        # one byte present.  So, only one "2" should be there when we read.
        self.g.write("2", 20, 5)
        self.assertEqual(self.g.read(20, 5), "2")
        self.assertEqual(self.g.read(20, 1), "2")

        self._device.close()


class GeometrySyncTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        self._device.open()

        # XXX: I don't know of a better way to test this method.
        self.g.write("1111111111", 0, 1)
        self.assertEqual(self.g.sync(), 1)

        self._device.close()


class GeometrySyncFastTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=0, length=100)

    def runTest(self):
        self._device.open()

        # XXX: I don't know of a better way to test this method.
        self.g.write("1111111111", 0, 1)
        self.assertEqual(self.g.sync_fast(), 1)

        self._device.close()


class GeometryWriteTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=10, length=100)

    def runTest(self):
        # First try to write to a device that isn't open yet.
        self.assertRaises(_ped.IOException, self.g.write, "X", 0, 10)

        # Now try a real write and make sure we (1) don't get an error code
        # and (2) the data actually ends up on the device.
        self._device.open()
        self.assertNotEqual(self.g.write("X", 0, 10), 0)
        self.assertEqual(self.g.read(0, 10), "X")
        self.assertNotEqual(self.g.write("XXXXXXXXXX", 0, 10), 0)
        self.assertEqual(self.g.read(0, 10), "XXXXXXXXXX")

        # Test bad parameter passing.
        self.assertRaises(_ped.IOException, self.g.write, "X", -10, 10)
        self.assertRaises(_ped.IOException, self.g.write, "X", 0, -10)
        self.assertRaises(TypeError, self.g.write, None, None, None)

        # Can't write past the end of the geometry.
        self.assertRaises(_ped.IOException, self.g.write, "X", 200, 1)
        self.assertRaises(_ped.IOException, self.g.write, "X", 0, 200)

        self._device.close()


class GeometryCheckTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=10, length=100)

    def runTest(self):
        # trivial test case first
        self.assertRaises(_ped.IOException, self.g.check, 0, 0, 0)

        self._device.open()

        self.assertEqual(self.g.check(0, 0, 10), 0)
        self.assertEqual(self.g.check(0, 0, 50), 0)

        self._device.close()


class GeometryMapTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g1 = _ped.Geometry(self._device, start=10, length=100)
        self.g2 = _ped.Geometry(self._device, start=10, length=90)

    def runTest(self):
        # write a word to the device starting at sector 25
        self._device.open()
        self.g1.write("UNITTEST", 25, 8)

        val1 = self.g2.read(self.g2.map(self.g1, 25), 8)
        val2 = self.g1.read(25, 8)
        self.assertEqual(val1, val2)

        self._device.close()


class GeometryStrTestCase(RequiresDevice):
    def setUp(self):
        super().setUp()
        self.g = _ped.Geometry(self._device, start=10, length=100)

    def runTest(self):
        lines = str(self.g).split("\n")
        self.assertEqual(lines[0], "_ped.Geometry instance --")
        self.assertEqual(lines[1], "  start: 10  end: 109  length: 100")
        self.assertRegex(lines[2], "^  device: <_ped.Device object at .*")