| 1 |
# This is a randomized test that generates different pathnames every |
| 2 |
# time it is invoked, and tests the encoding of those pathnames. |
| 3 |
# |
| 4 |
# It uses a simple probabilistic model to generate valid pathnames |
| 5 |
# that have proven likely to expose bugs and divergent behavior in |
| 6 |
# different encoding implementations. |
| 7 |
|
| 8 |
|
| 9 |
import binascii |
| 10 |
import collections |
| 11 |
import itertools |
| 12 |
import math |
| 13 |
import os |
| 14 |
import random |
| 15 |
import sys |
| 16 |
import time |
| 17 |
from mercurial import ( |
| 18 |
pycompat, |
| 19 |
store, |
| 20 |
) |
| 21 |
|
| 22 |
validchars = set(map(pycompat.bytechr, range(0, 256))) |
| 23 |
alphanum = range(ord('A'), ord('Z')) |
| 24 |
|
| 25 |
for c in (b'\0', b'/'): |
| 26 |
validchars.remove(c) |
| 27 |
|
| 28 |
winreserved = ( |
| 29 |
b'aux con prn nul'.split() |
| 30 |
+ [b'com%d' % i for i in range(1, 10)] |
| 31 |
+ [b'lpt%d' % i for i in range(1, 10)] |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
def casecombinations(names): |
| 36 |
'''Build all case-diddled combinations of names.''' |
| 37 |
|
| 38 |
combos = set() |
| 39 |
|
| 40 |
for r in names: |
| 41 |
for i in range(len(r) + 1): |
| 42 |
for c in itertools.combinations(range(len(r)), i): |
| 43 |
d = r |
| 44 |
for j in c: |
| 45 |
d = b''.join((d[:j], d[j : j + 1].upper(), d[j + 1 :])) |
| 46 |
combos.add(d) |
| 47 |
return sorted(combos) |
| 48 |
|
| 49 |
|
| 50 |
def buildprobtable(fp, cmd='hg manifest tip'): |
| 51 |
"""Construct and print a table of probabilities for path name |
| 52 |
components. The numbers are percentages.""" |
| 53 |
|
| 54 |
counts = collections.defaultdict(int) |
| 55 |
for line in os.popen(cmd).read().splitlines(): |
| 56 |
if line[-2:] in ('.i', '.d'): |
| 57 |
line = line[:-2] |
| 58 |
if line.startswith('data/'): |
| 59 |
line = line[5:] |
| 60 |
for c in line: |
| 61 |
counts[c] += 1 |
| 62 |
for c in '\r/\n': |
| 63 |
counts.pop(c, None) |
| 64 |
t = sum(counts.values()) / 100.0 |
| 65 |
fp.write('probtable = (') |
| 66 |
for i, (k, v) in enumerate( |
| 67 |
sorted(counts.items(), key=lambda x: x[1], reverse=True) |
| 68 |
): |
| 69 |
if (i % 5) == 0: |
| 70 |
fp.write('\n ') |
| 71 |
vt = v / t |
| 72 |
if vt < 0.0005: |
| 73 |
break |
| 74 |
fp.write('(%r, %.03f), ' % (k, vt)) |
| 75 |
fp.write('\n )\n') |
| 76 |
|
| 77 |
|
| 78 |
# A table of character frequencies (as percentages), gleaned by |
| 79 |
# looking at filelog names from a real-world, very large repo. |
| 80 |
|
| 81 |
probtable = ( |
| 82 |
(b't', 9.828), |
| 83 |
(b'e', 9.042), |
| 84 |
(b's', 8.011), |
| 85 |
(b'a', 6.801), |
| 86 |
(b'i', 6.618), |
| 87 |
(b'g', 5.053), |
| 88 |
(b'r', 5.030), |
| 89 |
(b'o', 4.887), |
| 90 |
(b'p', 4.363), |
| 91 |
(b'n', 4.258), |
| 92 |
(b'l', 3.830), |
| 93 |
(b'h', 3.693), |
| 94 |
(b'_', 3.659), |
| 95 |
(b'.', 3.377), |
| 96 |
(b'm', 3.194), |
| 97 |
(b'u', 2.364), |
| 98 |
(b'd', 2.296), |
| 99 |
(b'c', 2.163), |
| 100 |
(b'b', 1.739), |
| 101 |
(b'f', 1.625), |
| 102 |
(b'6', 0.666), |
| 103 |
(b'j', 0.610), |
| 104 |
(b'y', 0.554), |
| 105 |
(b'x', 0.487), |
| 106 |
(b'w', 0.477), |
| 107 |
(b'k', 0.476), |
| 108 |
(b'v', 0.473), |
| 109 |
(b'3', 0.336), |
| 110 |
(b'1', 0.335), |
| 111 |
(b'2', 0.326), |
| 112 |
(b'4', 0.310), |
| 113 |
(b'5', 0.305), |
| 114 |
(b'9', 0.302), |
| 115 |
(b'8', 0.300), |
| 116 |
(b'7', 0.299), |
| 117 |
(b'q', 0.298), |
| 118 |
(b'0', 0.250), |
| 119 |
(b'z', 0.223), |
| 120 |
(b'-', 0.118), |
| 121 |
(b'C', 0.095), |
| 122 |
(b'T', 0.087), |
| 123 |
(b'F', 0.085), |
| 124 |
(b'B', 0.077), |
| 125 |
(b'S', 0.076), |
| 126 |
(b'P', 0.076), |
| 127 |
(b'L', 0.059), |
| 128 |
(b'A', 0.058), |
| 129 |
(b'N', 0.051), |
| 130 |
(b'D', 0.049), |
| 131 |
(b'M', 0.046), |
| 132 |
(b'E', 0.039), |
| 133 |
(b'I', 0.035), |
| 134 |
(b'R', 0.035), |
| 135 |
(b'G', 0.028), |
| 136 |
(b'U', 0.026), |
| 137 |
(b'W', 0.025), |
| 138 |
(b'O', 0.017), |
| 139 |
(b'V', 0.015), |
| 140 |
(b'H', 0.013), |
| 141 |
(b'Q', 0.011), |
| 142 |
(b'J', 0.007), |
| 143 |
(b'K', 0.005), |
| 144 |
(b'+', 0.004), |
| 145 |
(b'X', 0.003), |
| 146 |
(b'Y', 0.001), |
| 147 |
) |
| 148 |
|
| 149 |
for c, _ in probtable: |
| 150 |
validchars.remove(c) |
| 151 |
validchars = list(validchars) |
| 152 |
|
| 153 |
|
| 154 |
def pickfrom(rng, table): |
| 155 |
c = 0 |
| 156 |
r = rng.random() * sum(i[1] for i in table) |
| 157 |
for i, p in table: |
| 158 |
c += p |
| 159 |
if c >= r: |
| 160 |
return i |
| 161 |
|
| 162 |
|
| 163 |
reservedcombos = casecombinations(winreserved) |
| 164 |
|
| 165 |
# The first component of a name following a slash. |
| 166 |
|
| 167 |
firsttable = ( |
| 168 |
(lambda rng: pickfrom(rng, probtable), 90), |
| 169 |
(lambda rng: rng.choice(validchars), 5), |
| 170 |
(lambda rng: rng.choice(reservedcombos), 5), |
| 171 |
) |
| 172 |
|
| 173 |
# Components of a name following the first. |
| 174 |
|
| 175 |
resttable = firsttable[:-1] |
| 176 |
|
| 177 |
# Special suffixes. |
| 178 |
|
| 179 |
internalsuffixcombos = casecombinations(b'.hg .i .d'.split()) |
| 180 |
|
| 181 |
# The last component of a path, before a slash or at the end of a name. |
| 182 |
|
| 183 |
lasttable = resttable + ( |
| 184 |
(lambda rng: b'', 95), |
| 185 |
(lambda rng: rng.choice(internalsuffixcombos), 5), |
| 186 |
) |
| 187 |
|
| 188 |
|
| 189 |
def makepart(rng, k): |
| 190 |
'''Construct a part of a pathname, without slashes.''' |
| 191 |
|
| 192 |
p = pickfrom(rng, firsttable)(rng) |
| 193 |
l = len(p) |
| 194 |
ps = [p] |
| 195 |
maxl = rng.randint(1, k) |
| 196 |
while l < maxl: |
| 197 |
p = pickfrom(rng, resttable)(rng) |
| 198 |
l += len(p) |
| 199 |
ps.append(p) |
| 200 |
ps.append(pickfrom(rng, lasttable)(rng)) |
| 201 |
return b''.join(ps) |
| 202 |
|
| 203 |
|
| 204 |
def makepath(rng, j, k): |
| 205 |
'''Construct a complete pathname.''' |
| 206 |
|
| 207 |
return ( |
| 208 |
b'data/' |
| 209 |
+ b'/'.join(makepart(rng, k) for _ in range(j)) |
| 210 |
+ rng.choice([b'.d', b'.i']) |
| 211 |
) |
| 212 |
|
| 213 |
|
| 214 |
def genpath(rng, count): |
| 215 |
'''Generate random pathnames with gradually increasing lengths.''' |
| 216 |
|
| 217 |
mink, maxk = 1, 4096 |
| 218 |
|
| 219 |
def steps(): |
| 220 |
for i in range(count): |
| 221 |
yield mink + int(round(math.sqrt((maxk - mink) * float(i) / count))) |
| 222 |
|
| 223 |
for k in steps(): |
| 224 |
x = rng.randint(1, k) |
| 225 |
y = rng.randint(1, k) |
| 226 |
yield makepath(rng, x, y) |
| 227 |
|
| 228 |
|
| 229 |
def runtests(rng, seed, count): |
| 230 |
nerrs = 0 |
| 231 |
for p in genpath(rng, count): |
| 232 |
h = store._pathencode(p) # uses C implementation, if available |
| 233 |
r = store._hybridencode(p, True) # reference implementation in Python |
| 234 |
if h != r: |
| 235 |
if nerrs == 0: |
| 236 |
print('seed:', hex(seed)[:-1], file=sys.stderr) |
| 237 |
print("\np: '%s'" % p.encode("string_escape"), file=sys.stderr) |
| 238 |
print("h: '%s'" % h.encode("string_escape"), file=sys.stderr) |
| 239 |
print("r: '%s'" % r.encode("string_escape"), file=sys.stderr) |
| 240 |
nerrs += 1 |
| 241 |
return nerrs |
| 242 |
|
| 243 |
|
| 244 |
def main(): |
| 245 |
import getopt |
| 246 |
|
| 247 |
# Empirically observed to take about a second to run |
| 248 |
count = 100 |
| 249 |
seed = None |
| 250 |
opts, args = getopt.getopt( |
| 251 |
sys.argv[1:], 'c:s:', ['build', 'count=', 'seed='] |
| 252 |
) |
| 253 |
for o, a in opts: |
| 254 |
if o in ('-c', '--count'): |
| 255 |
count = int(a) |
| 256 |
elif o in ('-s', '--seed'): |
| 257 |
seed = int(a, base=0) # accepts base 10 or 16 strings |
| 258 |
elif o == '--build': |
| 259 |
buildprobtable( |
| 260 |
sys.stdout, |
| 261 |
'find .hg/store/data -type f && ' |
| 262 |
'cat .hg/store/fncache 2>/dev/null', |
| 263 |
) |
| 264 |
sys.exit(0) |
| 265 |
|
| 266 |
if seed is None: |
| 267 |
try: |
| 268 |
seed = int(binascii.hexlify(os.urandom(16)), 16) |
| 269 |
except AttributeError: |
| 270 |
seed = int(time.time() * 1000) |
| 271 |
|
| 272 |
rng = random.Random(seed) |
| 273 |
if runtests(rng, seed, count): |
| 274 |
sys.exit(1) |
| 275 |
|
| 276 |
|
| 277 |
if __name__ == '__main__': |
| 278 |
main() |