summaryrefslogtreecommitdiff
path: root/rdiff-backup/testing/rdifftest.py
blob: 9de20a3a0d597413851cf1abd445ad04de28cb56 (plain)
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
import unittest, random
from commontest import *
from rdiff_backup.log import *
from rdiff_backup.selection import *
from rdiff_backup import Globals, Rdiff

Log.setverbosity(6)

def MakeRandomFile(path):
	"""Writes a random file of length between 10000 and 100000"""
	fp = open(path, "w")
	randseq = []
	for i in xrange(random.randrange(5000, 30000)):
		randseq.append(chr(random.randrange(256)))
	fp.write("".join(randseq))
	fp.close()


class RdiffTest(unittest.TestCase):
	"""Test rdiff"""
	lc = Globals.local_connection
	basis = RPath(lc, "testfiles/basis")
	new = RPath(lc, "testfiles/new")
	output = RPath(lc, "testfiles/output")
	delta = RPath(lc, "testfiles/delta")
	signature = RPath(lc, "testfiles/signature")

	def testRdiffSig(self):
		"""Test making rdiff signatures"""
		sig = RPath(self.lc, "testfiles/various_file_types/regular_file.sig")
		sigfp = sig.open("r")
		rfsig = Rdiff.get_signature(RPath(self.lc, "testfiles/various_file_types/regular_file"))
		assert RPath.cmpfileobj(sigfp, rfsig)
		sigfp.close()
		rfsig.close()

	def testRdiffDeltaPatch(self):
		"""Test making deltas and patching files"""
		rplist = [self.basis, self.new, self.delta,
				  self.signature, self.output]
		for rp in rplist:
			if rp.lstat(): rp.delete()
			
		for i in range(2):
			MakeRandomFile(self.basis.path)
			MakeRandomFile(self.new.path)
			map(RPath.setdata, [self.basis, self.new])
			assert self.basis.lstat() and self.new.lstat()
			self.signature.write_from_fileobj(Rdiff.get_signature(self.basis))
			assert self.signature.lstat()
			self.delta.write_from_fileobj(Rdiff.get_delta_sigrp(self.signature,
																self.new))
			assert self.delta.lstat()
			Rdiff.patch_action(self.basis, self.delta, self.output).execute()
			assert RPath.cmp(self.new, self.output)
			map(RPath.delete, rplist)

	def testRdiffDeltaPatchGzip(self):
		"""Same as above by try gzipping patches"""
		rplist = [self.basis, self.new, self.delta,
				  self.signature, self.output]
		for rp in rplist:
			if rp.lstat(): rp.delete()
			
		MakeRandomFile(self.basis.path)
		MakeRandomFile(self.new.path)
		map(RPath.setdata, [self.basis, self.new])
		assert self.basis.lstat() and self.new.lstat()
		self.signature.write_from_fileobj(Rdiff.get_signature(self.basis))
		assert self.signature.lstat()
		self.delta.write_from_fileobj(Rdiff.get_delta_sigrp(self.signature,
															self.new))
		assert self.delta.lstat()
		os.system("gzip " + self.delta.path)
		os.system("mv %s %s" % (self.delta.path + ".gz", self.delta.path))
		self.delta.setdata()

		Rdiff.patch_action(self.basis, self.delta, self.output,
						   delta_compressed = 1).execute()
		assert RPath.cmp(self.new, self.output)
		map(RPath.delete, rplist)

	def testWriteDelta(self):
		"""Test write delta feature of rdiff"""
		self.delta.delete()
		rplist = [self.basis, self.new, self.delta, self.output]
		MakeRandomFile(self.basis.path)
		MakeRandomFile(self.new.path)
		map(RPath.setdata, [self.basis, self.new])
		assert self.basis.lstat() and self.new.lstat()

		Rdiff.write_delta(self.basis, self.new, self.delta)
		assert self.delta.lstat()
		Rdiff.patch_action(self.basis, self.delta, self.output).execute()
		assert RPath.cmp(self.new, self.output)
		map(RPath.delete, rplist)		

	def testWriteDeltaGzip(self):
		"""Same as above but delta is written gzipped"""
		rplist = [self.basis, self.new, self.delta, self.output]
		MakeRandomFile(self.basis.path)
		MakeRandomFile(self.new.path)
		map(RPath.setdata, [self.basis, self.new])
		assert self.basis.lstat() and self.new.lstat()
		delta_gz = RPath(self.delta.conn, self.delta.path + ".gz")
		if delta_gz.lstat(): delta_gz.delete()

		Rdiff.write_delta(self.basis, self.new, delta_gz, 1)
		assert delta_gz.lstat()
		os.system("gunzip " + delta_gz.path)
		delta_gz.setdata()
		self.delta.setdata()
		Rdiff.patch_action(self.basis, self.delta, self.output).execute()
		assert RPath.cmp(self.new, self.output)
		map(RPath.delete, rplist)		

	def testRdiffRename(self):
		"""Rdiff replacing original file with patch outfile"""
		rplist = [self.basis, self.new, self.delta, self.signature]
		for rp in rplist:
			if rp.lstat(): rp.delete()

		MakeRandomFile(self.basis.path)
		MakeRandomFile(self.new.path)
		map(RPath.setdata, [self.basis, self.new])
		assert self.basis.lstat() and self.new.lstat()
		self.signature.write_from_fileobj(Rdiff.get_signature(self.basis))
		assert self.signature.lstat()
		self.delta.write_from_fileobj(Rdiff.get_delta_sigrp(self.signature,
															self.new))
		assert self.delta.lstat()
		Rdiff.patch_action(self.basis, self.delta).execute()
		assert RPath.cmp(self.basis, self.new)
		map(RPath.delete, rplist)

	def testCopy(self):
		"""Using rdiff to copy two files"""
		rplist = [self.basis, self.new]
		for rp in rplist:
			if rp.lstat(): rp.delete()

		MakeRandomFile(self.basis.path)
		MakeRandomFile(self.new.path)
		map(RPath.setdata, rplist)
		Rdiff.copy_action(self.basis, self.new).execute()
		assert RPath.cmp(self.basis, self.new)
		map(RPath.delete, rplist)

	def testPatchWithAttribs(self):
		"""Using rdiff to copy two files with attributes"""
		rplist = [self.basis, self.new, self.delta]
		for rp in rplist:
			if rp.lstat(): rp.delete()

		MakeRandomFile(self.basis.path)
		MakeRandomFile(self.new.path)
		self.new.chmod(0401)
		map(RPath.setdata, rplist)
		Rdiff.write_delta(self.basis, self.new, self.delta)
		RPath.copy_attribs(self.new, self.delta)
		assert self.delta.getperms() == 0401

		assert not self.basis == self.new
		Rdiff.patch_with_attribs_action(self.basis, self.delta).execute()
		if not self.basis == self.new:
			print self.basis, self.new
			assert 0
		map(RPath.delete, rplist)


if __name__ == '__main__':
	unittest.main()