
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | // std/msgpack (issue #140): byte-exact vectors against the MessagePack |
| 2 | // specification, round trips of every `Value` shape, decode failures on |
| 3 | // untrusted input (truncated, oversize, reserved, too deep), the std |
| 4 | // impls of `ToMsgPack`/`FromMsgPack`, and a `@derive(ToMsgPack)` with a |
| 5 | // hand-written decoder (until ADR 0051 derives them). |
| 6 | |
| 7 | import std/array |
| 8 | import std/bytes |
| 9 | import std/dict |
| 10 | import std/int |
| 11 | import std/io |
| 12 | import std/list |
| 13 | import std/msgpack |
| 14 | import std/msgpack. |
| 15 | {type DecodeError, type FromMsgPack, type ToMsgPack, type Value} |
| 16 | import std/result |
| 17 | import std/string |
| 18 | |
| 19 | fn hex :: v: Value -> String |
| 20 | { |
| 21 | debug (bytes.to_list (msgpack.write v)) |
| 22 | } |
| 23 | |
| 24 | fn show_read :: data: List<Int> -> String |
| 25 | { |
| 26 | debug (msgpack.read (bytes.from_list data)) |
| 27 | } |
| 28 | |
| 29 | fn show_error :: data: Bytes -> String |
| 30 | { |
| 31 | match msgpack.read data |
| 32 | { Ok(v) -> "unexpectedly ok: " <> debug v, Error(e) -> show e } |
| 33 | } |
| 34 | |
| 35 | /// `n` arrays nested around `Int(0)`. |
| 36 | fn nest :: n: Int -> Value |
| 37 | { |
| 38 | if n == 0 |
| 39 | { |
| 40 | msgpack.Int(0) |
| 41 | } |
| 42 | else |
| 43 | { |
| 44 | msgpack.Array([nest (n - 1)]) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @derive Debug ToMsgPack |
| 49 | pub type Order { Order(id: Int, lines: List<String>) } |
| 50 | |
| 51 | @derive Debug ToMsgPack |
| 52 | pub type Shape { Dot, Circle(Int), Rect(Int, Int) } |
| 53 | |
| 54 | // A hand-written decoder (ADR 0051 derives these): a record is a map |
| 55 | // keyed by field label; unknown keys are ignored, a missing field is an |
| 56 | // error. |
| 57 | impl FromMsgPack<Order> |
| 58 | { |
| 59 | fn from_msgpack :: v |
| 60 | { |
| 61 | match v |
| 62 | { msgpack.Map(fields) -> order_of fields None None |
| 63 | , _ -> Error(msgpack.Mismatch("a map for an Order", msgpack.kind v)) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | fn order_of :: |
| 69 | fields: List<(Value, Value)> |
| 70 | id: Option<Int> |
| 71 | lines: Option<List<String>> |
| 72 | -> Result<Order, DecodeError> |
| 73 | { |
| 74 | match fields |
| 75 | { [] -> |
| 76 | { |
| 77 | match (id, lines) |
| 78 | { (Some(id), Some(lines)) -> Ok(Order(id, lines)) |
| 79 | , _ -> |
| 80 | Error( msgpack.Mismatch( "a map with \"id\" and \"lines\"" |
| 81 | , "a map without them" |
| 82 | ) |
| 83 | ) |
| 84 | } |
| 85 | } |
| 86 | , [(key, value), ..rest] -> |
| 87 | { |
| 88 | match key |
| 89 | { msgpack.Str("id") -> |
| 90 | { |
| 91 | use n <- result.try (msgpack.from_msgpack value) |
| 92 | order_of rest Some(n) lines |
| 93 | } |
| 94 | , msgpack.Str("lines") -> |
| 95 | { |
| 96 | use ls <- result.try (msgpack.from_msgpack value) |
| 97 | order_of rest id Some(ls) |
| 98 | } |
| 99 | // Unknown keys are ignored (schema evolution). |
| 100 | , _ -> order_of rest id lines |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | pub fn main :: -> () / <io> |
| 107 | { |
| 108 | // ---- byte-exact vectors: integers, smallest encoding ---------------- |
| 109 | io.println (hex msgpack.Nil) |
| 110 | io.println (hex msgpack.Bool(False) <> " " <> hex msgpack.Bool(True)) |
| 111 | io.println (hex msgpack.Int(0) <> " " <> hex msgpack.Int(127)) |
| 112 | io.println (hex msgpack.Int(128) <> " " <> hex msgpack.Int(255)) |
| 113 | io.println (hex msgpack.Int(256) <> " " <> hex msgpack.Int(65535)) |
| 114 | io.println (hex msgpack.Int(65536) <> " " <> hex msgpack.Int(4294967295)) |
| 115 | io.println (hex msgpack.Int(4294967296)) |
| 116 | io.println |
| 117 | (hex msgpack.Int(int.shift_left 1 64 - 1)) // uint64 max, a big integer |
| 118 | io.println (hex msgpack.Int(-1) <> " " <> hex msgpack.Int(-32)) |
| 119 | io.println (hex msgpack.Int(-33) <> " " <> hex msgpack.Int(-128)) |
| 120 | io.println (hex msgpack.Int(-129) <> " " <> hex msgpack.Int(-32768)) |
| 121 | io.println (hex msgpack.Int(-32769) <> " " <> hex msgpack.Int(-2147483648)) |
| 122 | io.println (hex msgpack.Int(-2147483649)) |
| 123 | io.println (hex msgpack.Int(0 - int.shift_left 1 63)) // int64 min |
| 124 | // ---- floats (always float 64; -0.0 keeps its sign bit) --------------- |
| 125 | io.println (hex msgpack.Float(1.5)) |
| 126 | io.println (hex msgpack.Float(-0.0)) |
| 127 | // ---- strings, binary, arrays, maps, ext ------------------------------- |
| 128 | io.println (hex msgpack.Str("") <> " " <> hex msgpack.Str("abc")) |
| 129 | io.println (hex msgpack.Str("héllo")) // é is two UTF-8 bytes |
| 130 | io.println (hex msgpack.Bin(bytes.from_list [0, 255])) |
| 131 | io.println |
| 132 | ( hex msgpack.Array([]) <> " " |
| 133 | <> hex msgpack.Array([msgpack.Int(1), msgpack.Int(2)]) |
| 134 | ) |
| 135 | io.println |
| 136 | ( hex msgpack.Map([]) <> " " |
| 137 | <> hex msgpack.Map([(msgpack.Str("a"), msgpack.Int(1))]) |
| 138 | ) |
| 139 | io.println (hex msgpack.Ext(1, bytes.from_list [255])) // fixext 1 |
| 140 | io.println |
| 141 | (hex msgpack.Ext(-1, bytes.from_list [0, 0, 0, 0])) // fixext 4, tag -1 |
| 142 | // The wider length headers (str 16, array 16, bin 16): header bytes and |
| 143 | // total length. |
| 144 | let long_str = msgpack.write msgpack.Str(string.repeat "ab" 150) |
| 145 | io.println |
| 146 | "{bytes.length long_str} {debug (bytes.to_list (bytes.slice long_str 0 3))}" |
| 147 | let wide_array = |
| 148 | msgpack.write |
| 149 | msgpack.Array(list.map (list.range 1 16) fn n { msgpack.Int(n) }) |
| 150 | io.println |
| 151 | "{bytes.length wide_array} {debug (bytes.to_list (bytes.slice wide_array 0 3))}" |
| 152 | let wide_bin = |
| 153 | msgpack.write |
| 154 | msgpack.Bin(bytes.from_list (list.map (list.range 1 300) fn _ { 7 })) |
| 155 | io.println |
| 156 | "{bytes.length wide_bin} {debug (bytes.to_list (bytes.slice wide_bin 0 3))}" |
| 157 | // ---- reading: every family, widening, big integers -------------------- |
| 158 | io.println (show_read [202, 63, 192, 0, 0]) // float 32, widened |
| 159 | io.println (show_read [203, 63, 248, 0, 0, 0, 0, 0, 0]) |
| 160 | io.println |
| 161 | (show_read [207, 255, 255, 255, 255, 255, 255, 255, 255]) // uint64 max |
| 162 | io.println (show_read [211, 128, 0, 0, 0, 0, 0, 0, 0]) // int64 min |
| 163 | io.println (show_read [204, 0]) // non-minimal encodings decode fine |
| 164 | io.println (show_read [217, 3, 97, 98, 99]) |
| 165 | io.println (show_read [196, 2, 0, 255]) |
| 166 | io.println (show_read [130, 161, 97, 1, 161, 98, 145, 195]) |
| 167 | io.println (show_read [199, 3, 254, 1, 2, 3]) // ext 8, tag -2 |
| 168 | io.println (show_read [220, 0, 2, 1, 2]) // array 16 |
| 169 | // ---- round trips of a corpus ------------------------------------------ |
| 170 | let corpus = |
| 171 | [ msgpack.Nil |
| 172 | , msgpack.Bool(True) |
| 173 | , msgpack.Int(0 - int.shift_left 1 63) |
| 174 | , msgpack.Int(int.shift_left 1 64 - 1) |
| 175 | , msgpack.Float(-0.0) |
| 176 | , msgpack.Float(3.14159) |
| 177 | , msgpack.Str("héllo, 世界") |
| 178 | , msgpack.Bin(bytes.from_list [0, 159, 146, 150]) |
| 179 | , msgpack.Array([msgpack.Int(1), msgpack.Array([]), msgpack.Str("x")]) |
| 180 | , msgpack.Map( [ ( msgpack.Str("k") |
| 181 | , msgpack.Array([msgpack.Int(1), msgpack.Float(2.5)]) |
| 182 | ) |
| 183 | ] |
| 184 | ) |
| 185 | , msgpack.Ext(-128, bytes.empty ()) |
| 186 | , msgpack.Ext( 127 |
| 187 | , bytes.from_list |
| 188 | [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17] |
| 189 | ) |
| 190 | , nest 100 |
| 191 | ] |
| 192 | let round_trips = |
| 193 | list.fold |
| 194 | corpus |
| 195 | True |
| 196 | fn ok v { ok && msgpack.read (msgpack.write v) == Ok(v) } |
| 197 | io.println "round trips: {round_trips}" |
| 198 | // ---- untrusted input: errors, never a panic or a huge allocation ------- |
| 199 | io.println (show_error (bytes.empty ())) |
| 200 | io.println (show_error (bytes.from_list [204])) |
| 201 | io.println (show_error (bytes.from_list [193])) |
| 202 | io.println (show_error (bytes.from_list [161, 255])) |
| 203 | io.println (show_error (bytes.from_list [217, 5, 97])) |
| 204 | io.println (show_error (bytes.from_list [221, 255, 255, 255, 255, 1])) |
| 205 | io.println (show_error (bytes.from_list [1, 1])) |
| 206 | // Nesting is bounded: 512 deep reads back, 513 is an error. |
| 207 | io.println (show (msgpack.read (msgpack.write (nest 512)) == Ok(nest 512))) |
| 208 | io.println (show_error (msgpack.write (nest 513))) |
| 209 | // ---- the traits: encode/decode over the std impls ----------------------- |
| 210 | io.println (debug (bytes.to_list (msgpack.encode [1, 2, 3]))) |
| 211 | io.println (debug (bytes.to_list (msgpack.encode "héllo"))) |
| 212 | io.println (debug (bytes.to_list (msgpack.encode True))) |
| 213 | io.println (debug (bytes.to_list (msgpack.encode 2.5))) |
| 214 | io.println |
| 215 | (debug (bytes.to_list (msgpack.encode (bytes.from_list [1, 2, 3])))) |
| 216 | io.println |
| 217 | (debug (bytes.to_list (msgpack.encode (array.from_list ["a", "b"])))) |
| 218 | io.println |
| 219 | (debug (bytes.to_list (msgpack.encode (dict.from_list [("a", 1)])))) |
| 220 | let none: Option<Int> = None |
| 221 | io.println (debug (bytes.to_list (msgpack.encode none))) |
| 222 | io.println (debug (bytes.to_list (msgpack.encode Some([1, 2])))) |
| 223 | let ok: Result<Int, String> = Ok(1) |
| 224 | let no: Result<Int, String> = Error("no") |
| 225 | io.println (debug (bytes.to_list (msgpack.encode ok))) |
| 226 | io.println (debug (bytes.to_list (msgpack.encode no))) |
| 227 | let ints: Result<List<Int>, DecodeError> = |
| 228 | msgpack.decode (bytes.from_list [147, 1, 2, 3]) |
| 229 | io.println (debug ints) |
| 230 | let no_back: Result<Result<Int, String>, DecodeError> = |
| 231 | msgpack.decode (msgpack.encode no) |
| 232 | io.println (debug no_back) |
| 233 | // A whole number decodes as a Float too (many encoders write 1.0 as 1). |
| 234 | let one: Result<Float, DecodeError> = msgpack.decode (bytes.from_list [1]) |
| 235 | io.println (debug one) |
| 236 | // A mismatch names what was expected and what was found. |
| 237 | let not_ints: Result<List<Int>, DecodeError> = |
| 238 | msgpack.decode (bytes.from_list [145, 160]) |
| 239 | io.println |
| 240 | match not_ints |
| 241 | { Ok(v) -> "unexpectedly ok: " <> debug v, Error(e) -> show e } |
| 242 | // ---- the derive: records as maps, sums tagged by name ------------------- |
| 243 | let order = Order(7, ["a", "b"]) |
| 244 | io.println (debug (bytes.to_list (msgpack.encode order))) |
| 245 | io.println |
| 246 | (debug (bytes.to_list (msgpack.encode [Dot, Circle(3), Rect(1, 2)]))) |
| 247 | let order_back: Result<Order, DecodeError> = |
| 248 | msgpack.decode (msgpack.encode order) |
| 249 | io.println (debug order_back) |
| 250 | // Unknown keys are ignored; a missing field is an error. |
| 251 | let extra_map = |
| 252 | msgpack.Map( [ (msgpack.Str("id"), msgpack.Int(7)) |
| 253 | , ( msgpack.Str("lines") |
| 254 | , msgpack.Array([msgpack.Str("a"), msgpack.Str("b")]) |
| 255 | ) |
| 256 | , (msgpack.Str("future"), msgpack.Nil) |
| 257 | ] |
| 258 | ) |
| 259 | let evolved: Result<Order, DecodeError> = |
| 260 | msgpack.decode (msgpack.write extra_map) |
| 261 | io.println (debug evolved) |
| 262 | let missing: Result<Order, DecodeError> = |
| 263 | msgpack.decode |
| 264 | (msgpack.write msgpack.Map([(msgpack.Str("id"), msgpack.Int(7))])) |
| 265 | io.println |
| 266 | match missing |
| 267 | { Ok(v) -> "unexpectedly ok: " <> debug v, Error(e) -> show e } |
| 268 | } |
| 269 | //> [192] |
| 270 | //> [194] [195] |
| 271 | //> [0] [127] |
| 272 | //> [204, 128] [204, 255] |
| 273 | //> [205, 1, 0] [205, 255, 255] |
| 274 | //> [206, 0, 1, 0, 0] [206, 255, 255, 255, 255] |
| 275 | //> [207, 0, 0, 0, 1, 0, 0, 0, 0] |
| 276 | //> [207, 255, 255, 255, 255, 255, 255, 255, 255] |
| 277 | //> [255] [224] |
| 278 | //> [208, 223] [208, 128] |
| 279 | //> [209, 255, 127] [209, 128, 0] |
| 280 | //> [210, 255, 255, 127, 255] [210, 128, 0, 0, 0] |
| 281 | //> [211, 255, 255, 255, 255, 127, 255, 255, 255] |
| 282 | //> [211, 128, 0, 0, 0, 0, 0, 0, 0] |
| 283 | //> [203, 63, 248, 0, 0, 0, 0, 0, 0] |
| 284 | //> [203, 128, 0, 0, 0, 0, 0, 0, 0] |
| 285 | //> [160] [163, 97, 98, 99] |
| 286 | //> [166, 104, 195, 169, 108, 108, 111] |
| 287 | //> [196, 2, 0, 255] |
| 288 | //> [144] [146, 1, 2] |
| 289 | //> [128] [129, 161, 97, 1] |
| 290 | //> [212, 1, 255] |
| 291 | //> [214, 255, 0, 0, 0, 0] |
| 292 | //> 303 [218, 1, 44] |
| 293 | //> 19 [220, 0, 16] |
| 294 | //> 303 [197, 1, 44] |
| 295 | //> Ok(Float(1.5)) |
| 296 | //> Ok(Float(1.5)) |
| 297 | //> Ok(Int(18446744073709551615)) |
| 298 | //> Ok(Int(-9223372036854775808)) |
| 299 | //> Ok(Int(0)) |
| 300 | //> Ok(Str("abc")) |
| 301 | //> Ok(Bin(b"\x00\xff")) |
| 302 | //> Ok(Map([(Str("a"), Int(1)), (Str("b"), Array([Bool(True)]))])) |
| 303 | //> Ok(Ext { tag: -2, data: b"\x01\x02\x03" }) |
| 304 | //> Ok(Array([Int(1), Int(2)])) |
| 305 | //> round trips: True |
| 306 | //> invalid MessagePack at byte 0: the input ends early |
| 307 | //> invalid MessagePack at byte 1: a length of 1 with 0 bytes left |
| 308 | //> invalid MessagePack at byte 0: 0xc1 is not a MessagePack byte |
| 309 | //> invalid MessagePack at byte 1: a str that is not UTF-8: invalid UTF-8 at byte 0 |
| 310 | //> invalid MessagePack at byte 2: a length of 5 with 1 bytes left |
| 311 | //> invalid MessagePack at byte 5: an array of 4294967295 items with 1 bytes left |
| 312 | //> invalid MessagePack at byte 1: 1 bytes after the value |
| 313 | //> True |
| 314 | //> invalid MessagePack at byte 513: nesting deeper than 512 levels |
| 315 | //> [147, 1, 2, 3] |
| 316 | //> [166, 104, 195, 169, 108, 108, 111] |
| 317 | //> [195] |
| 318 | //> [203, 64, 4, 0, 0, 0, 0, 0, 0] |
| 319 | //> [196, 3, 1, 2, 3] |
| 320 | //> [146, 161, 97, 161, 98] |
| 321 | //> [129, 161, 97, 1] |
| 322 | //> [192] |
| 323 | //> [146, 1, 2] |
| 324 | //> [129, 162, 79, 107, 145, 1] |
| 325 | //> [129, 165, 69, 114, 114, 111, 114, 145, 162, 110, 111] |
| 326 | //> Ok([1, 2, 3]) |
| 327 | //> Ok(Error("no")) |
| 328 | //> Ok(1.0) |
| 329 | //> MessagePack: expected an Int, found a Str |
| 330 | //> [130, 162, 105, 100, 7, 165, 108, 105, 110, 101, 115, 146, 161, 97, 161, 98] |
| 331 | //> [147, 163, 68, 111, 116, 129, 166, 67, 105, 114, 99, 108, 101, 145, 3, 129, 164, 82, 101, 99, 116, 146, 1, 2] |
| 332 | //> Ok(Order { id: 7, lines: ["a", "b"] }) |
| 333 | //> Ok(Order { id: 7, lines: ["a", "b"] }) |
| 334 | //> MessagePack: expected a map with "id" and "lines", found a map without them |