| 1 |
import unittest |
| 2 |
|
| 3 |
from mercurial import ( |
| 4 |
util, |
| 5 |
wireprotoframing as framing, |
| 6 |
) |
| 7 |
|
| 8 |
ffs = framing.makeframefromhumanstring |
| 9 |
|
| 10 |
|
| 11 |
class FrameHumanStringTests(unittest.TestCase): |
| 12 |
def testbasic(self): |
| 13 |
self.assertEqual( |
| 14 |
ffs(b'1 1 0 1 0 '), b'\x00\x00\x00\x01\x00\x01\x00\x10' |
| 15 |
) |
| 16 |
|
| 17 |
self.assertEqual( |
| 18 |
ffs(b'2 4 0 1 0 '), b'\x00\x00\x00\x02\x00\x04\x00\x10' |
| 19 |
) |
| 20 |
|
| 21 |
self.assertEqual( |
| 22 |
ffs(b'2 4 0 1 0 foo'), b'\x03\x00\x00\x02\x00\x04\x00\x10foo' |
| 23 |
) |
| 24 |
|
| 25 |
def testcborint(self): |
| 26 |
self.assertEqual( |
| 27 |
ffs(b'1 1 0 1 0 cbor:15'), b'\x01\x00\x00\x01\x00\x01\x00\x10\x0f' |
| 28 |
) |
| 29 |
|
| 30 |
self.assertEqual( |
| 31 |
ffs(b'1 1 0 1 0 cbor:42'), b'\x02\x00\x00\x01\x00\x01\x00\x10\x18*' |
| 32 |
) |
| 33 |
|
| 34 |
self.assertEqual( |
| 35 |
ffs(b'1 1 0 1 0 cbor:1048576'), |
| 36 |
b'\x05\x00\x00\x01\x00\x01\x00\x10\x1a' b'\x00\x10\x00\x00', |
| 37 |
) |
| 38 |
|
| 39 |
self.assertEqual( |
| 40 |
ffs(b'1 1 0 1 0 cbor:0'), b'\x01\x00\x00\x01\x00\x01\x00\x10\x00' |
| 41 |
) |
| 42 |
|
| 43 |
self.assertEqual( |
| 44 |
ffs(b'1 1 0 1 0 cbor:-1'), b'\x01\x00\x00\x01\x00\x01\x00\x10 ' |
| 45 |
) |
| 46 |
|
| 47 |
self.assertEqual( |
| 48 |
ffs(b'1 1 0 1 0 cbor:-342542'), |
| 49 |
b'\x05\x00\x00\x01\x00\x01\x00\x10:\x00\x05:\r', |
| 50 |
) |
| 51 |
|
| 52 |
def testcborstrings(self): |
| 53 |
self.assertEqual( |
| 54 |
ffs(b"1 1 0 1 0 cbor:b'foo'"), |
| 55 |
b'\x04\x00\x00\x01\x00\x01\x00\x10Cfoo', |
| 56 |
) |
| 57 |
|
| 58 |
def testcborlists(self): |
| 59 |
self.assertEqual( |
| 60 |
ffs(b"1 1 0 1 0 cbor:[None, True, False, 42, b'foo']"), |
| 61 |
b'\n\x00\x00\x01\x00\x01\x00\x10\x85\xf6\xf5\xf4' b'\x18*Cfoo', |
| 62 |
) |
| 63 |
|
| 64 |
def testcbordicts(self): |
| 65 |
self.assertEqual( |
| 66 |
ffs(b"1 1 0 1 0 " b"cbor:{b'foo': b'val1', b'bar': b'val2'}"), |
| 67 |
b'\x13\x00\x00\x01\x00\x01\x00\x10\xa2' b'CbarDval2CfooDval1', |
| 68 |
) |
| 69 |
|
| 70 |
|
| 71 |
class FrameTests(unittest.TestCase): |
| 72 |
def testdataexactframesize(self): |
| 73 |
data = util.bytesio(b'x' * framing.DEFAULT_MAX_FRAME_SIZE) |
| 74 |
|
| 75 |
stream = framing.stream(1) |
| 76 |
frames = list( |
| 77 |
framing.createcommandframes(stream, 1, b'command', {}, data) |
| 78 |
) |
| 79 |
self.assertEqual( |
| 80 |
frames, |
| 81 |
[ |
| 82 |
ffs( |
| 83 |
b'1 1 stream-begin command-request new|have-data ' |
| 84 |
b"cbor:{b'name': b'command'}" |
| 85 |
), |
| 86 |
ffs(b'1 1 0 command-data continuation %s' % data.getvalue()), |
| 87 |
ffs(b'1 1 0 command-data eos '), |
| 88 |
], |
| 89 |
) |
| 90 |
|
| 91 |
def testdatamultipleframes(self): |
| 92 |
data = util.bytesio(b'x' * (framing.DEFAULT_MAX_FRAME_SIZE + 1)) |
| 93 |
|
| 94 |
stream = framing.stream(1) |
| 95 |
frames = list( |
| 96 |
framing.createcommandframes(stream, 1, b'command', {}, data) |
| 97 |
) |
| 98 |
self.assertEqual( |
| 99 |
frames, |
| 100 |
[ |
| 101 |
ffs( |
| 102 |
b'1 1 stream-begin command-request new|have-data ' |
| 103 |
b"cbor:{b'name': b'command'}" |
| 104 |
), |
| 105 |
ffs( |
| 106 |
b'1 1 0 command-data continuation %s' |
| 107 |
% (b'x' * framing.DEFAULT_MAX_FRAME_SIZE) |
| 108 |
), |
| 109 |
ffs(b'1 1 0 command-data eos x'), |
| 110 |
], |
| 111 |
) |
| 112 |
|
| 113 |
def testargsanddata(self): |
| 114 |
data = util.bytesio(b'x' * 100) |
| 115 |
|
| 116 |
stream = framing.stream(1) |
| 117 |
frames = list( |
| 118 |
framing.createcommandframes( |
| 119 |
stream, |
| 120 |
1, |
| 121 |
b'command', |
| 122 |
{ |
| 123 |
b'key1': b'key1value', |
| 124 |
b'key2': b'key2value', |
| 125 |
b'key3': b'key3value', |
| 126 |
}, |
| 127 |
data, |
| 128 |
) |
| 129 |
) |
| 130 |
|
| 131 |
self.assertEqual( |
| 132 |
frames, |
| 133 |
[ |
| 134 |
ffs( |
| 135 |
b'1 1 stream-begin command-request new|have-data ' |
| 136 |
b"cbor:{b'name': b'command', b'args': {b'key1': b'key1value', " |
| 137 |
b"b'key2': b'key2value', b'key3': b'key3value'}}" |
| 138 |
), |
| 139 |
ffs(b'1 1 0 command-data eos %s' % data.getvalue()), |
| 140 |
], |
| 141 |
) |
| 142 |
|
| 143 |
if not getattr(unittest.TestCase, 'assertRaisesRegex', False): |
| 144 |
# Python 3.7 deprecates the regex*p* version, but 2.7 lacks |
| 145 |
# the regex version. |
| 146 |
assertRaisesRegex = ( # camelcase-required |
| 147 |
unittest.TestCase.assertRaisesRegexp |
| 148 |
) |
| 149 |
|
| 150 |
def testtextoutputformattingstringtype(self): |
| 151 |
"""Formatting string must be bytes.""" |
| 152 |
with self.assertRaisesRegex(ValueError, 'must use bytes formatting '): |
| 153 |
list( |
| 154 |
framing.createtextoutputframe( |
| 155 |
None, 1, [(b'foo'.decode('ascii'), [], [])] |
| 156 |
) |
| 157 |
) |
| 158 |
|
| 159 |
def testtextoutputargumentbytes(self): |
| 160 |
with self.assertRaisesRegex(ValueError, 'must use bytes for argument'): |
| 161 |
list( |
| 162 |
framing.createtextoutputframe( |
| 163 |
None, 1, [(b'foo', [b'foo'.decode('ascii')], [])] |
| 164 |
) |
| 165 |
) |
| 166 |
|
| 167 |
def testtextoutputlabelbytes(self): |
| 168 |
with self.assertRaisesRegex(ValueError, 'must use bytes for labels'): |
| 169 |
list( |
| 170 |
framing.createtextoutputframe( |
| 171 |
None, 1, [(b'foo', [], [b'foo'.decode('ascii')])] |
| 172 |
) |
| 173 |
) |
| 174 |
|
| 175 |
def testtextoutput1simpleatom(self): |
| 176 |
stream = framing.stream(1) |
| 177 |
val = list(framing.createtextoutputframe(stream, 1, [(b'foo', [], [])])) |
| 178 |
|
| 179 |
self.assertEqual( |
| 180 |
val, |
| 181 |
[ |
| 182 |
ffs( |
| 183 |
b'1 1 stream-begin text-output 0 ' |
| 184 |
b"cbor:[{b'msg': b'foo'}]" |
| 185 |
), |
| 186 |
], |
| 187 |
) |
| 188 |
|
| 189 |
def testtextoutput2simpleatoms(self): |
| 190 |
stream = framing.stream(1) |
| 191 |
val = list( |
| 192 |
framing.createtextoutputframe( |
| 193 |
stream, |
| 194 |
1, |
| 195 |
[ |
| 196 |
(b'foo', [], []), |
| 197 |
(b'bar', [], []), |
| 198 |
], |
| 199 |
) |
| 200 |
) |
| 201 |
|
| 202 |
self.assertEqual( |
| 203 |
val, |
| 204 |
[ |
| 205 |
ffs( |
| 206 |
b'1 1 stream-begin text-output 0 ' |
| 207 |
b"cbor:[{b'msg': b'foo'}, {b'msg': b'bar'}]" |
| 208 |
) |
| 209 |
], |
| 210 |
) |
| 211 |
|
| 212 |
def testtextoutput1arg(self): |
| 213 |
stream = framing.stream(1) |
| 214 |
val = list( |
| 215 |
framing.createtextoutputframe( |
| 216 |
stream, |
| 217 |
1, |
| 218 |
[ |
| 219 |
(b'foo %s', [b'val1'], []), |
| 220 |
], |
| 221 |
) |
| 222 |
) |
| 223 |
|
| 224 |
self.assertEqual( |
| 225 |
val, |
| 226 |
[ |
| 227 |
ffs( |
| 228 |
b'1 1 stream-begin text-output 0 ' |
| 229 |
b"cbor:[{b'msg': b'foo %s', b'args': [b'val1']}]" |
| 230 |
) |
| 231 |
], |
| 232 |
) |
| 233 |
|
| 234 |
def testtextoutput2arg(self): |
| 235 |
stream = framing.stream(1) |
| 236 |
val = list( |
| 237 |
framing.createtextoutputframe( |
| 238 |
stream, |
| 239 |
1, |
| 240 |
[ |
| 241 |
(b'foo %s %s', [b'val', b'value'], []), |
| 242 |
], |
| 243 |
) |
| 244 |
) |
| 245 |
|
| 246 |
self.assertEqual( |
| 247 |
val, |
| 248 |
[ |
| 249 |
ffs( |
| 250 |
b'1 1 stream-begin text-output 0 ' |
| 251 |
b"cbor:[{b'msg': b'foo %s %s', b'args': [b'val', b'value']}]" |
| 252 |
) |
| 253 |
], |
| 254 |
) |
| 255 |
|
| 256 |
def testtextoutput1label(self): |
| 257 |
stream = framing.stream(1) |
| 258 |
val = list( |
| 259 |
framing.createtextoutputframe( |
| 260 |
stream, |
| 261 |
1, |
| 262 |
[ |
| 263 |
(b'foo', [], [b'label']), |
| 264 |
], |
| 265 |
) |
| 266 |
) |
| 267 |
|
| 268 |
self.assertEqual( |
| 269 |
val, |
| 270 |
[ |
| 271 |
ffs( |
| 272 |
b'1 1 stream-begin text-output 0 ' |
| 273 |
b"cbor:[{b'msg': b'foo', b'labels': [b'label']}]" |
| 274 |
) |
| 275 |
], |
| 276 |
) |
| 277 |
|
| 278 |
def testargandlabel(self): |
| 279 |
stream = framing.stream(1) |
| 280 |
val = list( |
| 281 |
framing.createtextoutputframe( |
| 282 |
stream, |
| 283 |
1, |
| 284 |
[ |
| 285 |
(b'foo %s', [b'arg'], [b'label']), |
| 286 |
], |
| 287 |
) |
| 288 |
) |
| 289 |
|
| 290 |
self.assertEqual( |
| 291 |
val, |
| 292 |
[ |
| 293 |
ffs( |
| 294 |
b'1 1 stream-begin text-output 0 ' |
| 295 |
b"cbor:[{b'msg': b'foo %s', b'args': [b'arg'], " |
| 296 |
b"b'labels': [b'label']}]" |
| 297 |
) |
| 298 |
], |
| 299 |
) |
| 300 |
|
| 301 |
|
| 302 |
if __name__ == '__main__': |
| 303 |
import silenttestrunner |
| 304 |
|
| 305 |
silenttestrunner.main(__name__) |