Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/om/codec/test/gen/oracle/main.go # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / om / codec / test / gen / oracle / main.go` 469 lines ยท 15123 bytes Raw Blame History 1 // SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause 2 // 3 // Test referee for om/codec, never part of the implementation. Built with go1.26.0, standard 4 // library only. 5 // 6 // oracle read driver case lines "OP ARG" on stdin, print Go's result for each 7 // oracle corpus print the hand-made malformed-input corpus as case lines (# lines name groups) 8 // oracle rand SEED print the seeded random differential corpus as case lines 9 // 10 // Result lines use the driver's format: encoders "ok TEXT", decoders "ok HEX" or "err", "-" for 11 // empty. 12 // 13 // Third-party code: base32StdNoPad, aumUnmarshalText, parseHex and fromHexChar below are derived 14 // from tailscale/tailscale tag v1.102.0 (commit `35283c95445fb198af1ce708dac05714bdfaa037`), files 15 // tka/aum.go and types/key/util.go. Copyright (c) 2020 Tailscale Inc & contributors, BSD 3-Clause 16 // License; the full license text is in TAILSCALE.LICENSE beside this file. Each derived block is 17 // marked "Derived from tailscale" and ends at "End of tailscale-derived code". Changes from 18 // upstream: AUMHash is a *[32]byte, parseHex takes strings instead of mem.RO, and parseHex's error 19 // messages do not format the prefix or sizes. test/gen/tsoracle checks this transcription against 20 // the real package. Everything else in this file is omnet's own, Apache-2.0. 21 package main 22 23 import ( 24 "bufio" 25 "encoding/base32" 26 "encoding/base64" 27 "encoding/hex" 28 "errors" 29 "fmt" 30 "math/rand/v2" 31 "os" 32 "strconv" 33 "strings" 34 ) 35 36 // Derived from tailscale v1.102.0 tka/aum.go 24 and 33-41 (BSD-3-Clause, see TAILSCALE.LICENSE). 37 var base32StdNoPad = base32.StdEncoding.WithPadding(base32.NoPadding) 38 39 func aumUnmarshalText(h *[32]byte, text []byte) error { 40 if ln := base32StdNoPad.DecodedLen(len(text)); ln != len(h) { 41 return fmt.Errorf("tka.AUMHash.UnmarshalText: text wrong length: %d, want %d", ln, len(text)) 42 } 43 if _, err := base32StdNoPad.Decode(h[:], text); err != nil { 44 return fmt.Errorf("tka.AUMHash.UnmarshalText: %w", err) 45 } 46 return nil 47 } 48 49 // End of tailscale-derived code. 50 51 // Derived from tailscale v1.102.0 types/key/util.go 70-101 (parseHex, fromHexChar), called with the 52 // prefix "nodekey:" as types/key/node.go 36 and 336-338 do (BSD-3-Clause, see TAILSCALE.LICENSE). 53 func parseHex(out []byte, in, prefix string) error { 54 if !strings.HasPrefix(in, prefix) { 55 return errors.New("key hex string doesn't have expected type prefix") 56 } 57 in = in[len(prefix):] 58 if want := len(out) * 2; len(in) != want { 59 return errors.New("key hex has the wrong size") 60 } 61 for i := range out { 62 a, ok1 := fromHexChar(in[i*2+0]) 63 b, ok2 := fromHexChar(in[i*2+1]) 64 if !ok1 || !ok2 { 65 return errors.New("invalid hex character in key") 66 } 67 out[i] = (a << 4) | b 68 } 69 return nil 70 } 71 72 func fromHexChar(c byte) (byte, bool) { 73 switch { 74 case '0' <= c && c <= '9': 75 return c - '0', true 76 case 'a' <= c && c <= 'f': 77 return c - 'a' + 10, true 78 case 'A' <= c && c <= 'F': 79 return c - 'A' + 10, true 80 } 81 return 0, false 82 } 83 84 // End of tailscale-derived code. 85 86 func text(b []byte) string { 87 if len(b) == 0 { 88 return "-" 89 } 90 return string(b) 91 } 92 93 func hx(b []byte) string { 94 if len(b) == 0 { 95 return "-" 96 } 97 return hex.EncodeToString(b) 98 } 99 100 func dec(b []byte, err error) string { 101 if err != nil { 102 return "err" 103 } 104 return "ok " + hx(b) 105 } 106 107 func result(op string, a []byte) string { 108 switch op { 109 case "b64e": 110 return "ok " + text([]byte(base64.StdEncoding.EncodeToString(a))) 111 case "b64d": 112 // encoding/json decode.go 950-951: the []byte path of literalStore 113 b := make([]byte, base64.StdEncoding.DecodedLen(len(a))) 114 n, err := base64.StdEncoding.Decode(b, a) 115 return dec(b[:n], err) 116 case "b32e": 117 return "ok " + text([]byte(base32StdNoPad.EncodeToString(a))) 118 case "b32d": 119 return dec(base32StdNoPad.DecodeString(string(a))) 120 case "hexe": 121 return "ok " + text([]byte(hex.EncodeToString(a))) 122 case "hexd": 123 return dec(hex.DecodeString(string(a))) 124 case "aume", "nkye": 125 if len(a) != 32 { 126 fail("aume/nkye need 32 bytes") 127 } 128 if op == "aume" { 129 return "ok " + base32StdNoPad.EncodeToString(a) 130 } 131 return "ok nodekey:" + hex.EncodeToString(a) 132 case "aumd": 133 var h [32]byte 134 if aumUnmarshalText(&h, a) != nil { 135 return "err" 136 } 137 return "ok " + hx(h[:]) 138 case "nkyd": 139 var k [32]byte 140 if parseHex(k[:], string(a), "nodekey:") != nil { 141 return "err" 142 } 143 return "ok " + hx(k[:]) 144 } 145 fail("unknown op " + op) 146 return "" 147 } 148 149 func fail(msg string) { 150 fmt.Fprintln(os.Stderr, "oracle:", msg) 151 os.Exit(2) 152 } 153 154 func referee() { 155 in := bufio.NewScanner(os.Stdin) 156 in.Buffer(make([]byte, 1<<20), 1<<20) 157 out := bufio.NewWriter(os.Stdout) 158 defer out.Flush() 159 for in.Scan() { 160 f := strings.Split(in.Text(), " ") 161 if len(f) != 2 { 162 fail("bad line") 163 } 164 var a []byte 165 if f[1] != "-" { 166 var err error 167 if a, err = hex.DecodeString(f[1]); err != nil { 168 fail("bad hex") 169 } 170 } 171 fmt.Fprintln(out, result(f[0], a)) 172 } 173 } 174 175 var out = bufio.NewWriter(os.Stdout) 176 177 func emit(op string, s []byte) { fmt.Fprintf(out, "%s %s\n", op, hx(s)) } 178 func emitS(op, s string) { emit(op, []byte(s)) } 179 func group(name string) { fmt.Fprintf(out, "# %s\n", name) } 180 181 const ( 182 b64alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" 183 b32alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" 184 hexalpha = "0123456789abcdefABCDEF" 185 ) 186 187 // corpus: hand-made cases, one group per malformed-input class of the acceptance list 188 func corpus() { 189 h32 := make([]byte, 32) 190 for i := range h32 { 191 h32[i] = byte(i*37 + 11) 192 } 193 aum := base32StdNoPad.EncodeToString(h32) 194 nk := "nodekey:" + hex.EncodeToString(h32) 195 196 group("empty input") 197 for _, op := range []string{"b64d", "b32d", "hexd", "aumd", "nkyd", "b64e", "b32e", "hexe"} { 198 emitS(op, "") 199 } 200 201 group("base64: bad characters") 202 for _, s := range []string{"Zm9v!", "Zm9v Zm9v", "Zm 9v", "Zm9v\t", "Zm-v", "Zm_v", "Zm9\x00", "Zm9\xff", 203 "Zm9\x80", "Zm9v\"", "Zm9v\\", "Zm8=Zm8=", "\x00", " ", "Zm9vYmFy\xffZm9v", "Zm9vYmFyZ!9v", 204 "Zm9vYmFyZm9vYmFy-", "====", "Z===", "Zm9vYm=y"} { 205 emitS("b64d", s) 206 } 207 group("base64: wrong padding") 208 for _, s := range []string{"Zg", "Zg=", "Zg===", "Zg====", "Zm8", "Zm8==", "Zm9", "Zm9v=", "Zm9v==", "=Zm9v", 209 "Z=g=", "Zg=x", "Zm8=x", "Zg==Zg==", "Zg==\n=", "Zg=\n=", "Zg=\r\n=\r\n", "Zm8=\n", "Zm8\n=", "Zg==\nA", 210 "Zm9vYg", "Zm9vYg=", "Zm9vYmE", "Zm9vYmE==", "Zm9vYmFy="} { 211 emitS("b64d", s) 212 } 213 group("base64: non-zero trailing bits") 214 for _, s := range []string{"Zh==", "Zv==", "Z/==", "Zm9=", "Zm+=", "Zm//", "Zm9vYh==", "Zm9vYmF=", "AB==", "AAB=", "//==", "///="} { 215 emitS("b64d", s) 216 } 217 group("base64: wrong lengths") 218 for _, s := range []string{"Z", "Zm", "Zm9", "Zm9vY", "Zm9vYm", "Zm9vYmF", "Zm9vYmFyZ", "A", "AAAAA"} { 219 emitS("b64d", s) 220 } 221 group("base64: mixed case and other alphabets") 222 for _, s := range []string{"zM9V", "ZM9V", "Zm9V", "Zm9vYmFy", "ZM9VYMFY", "Zm9v-_8=", "Zm9v+/8="} { 223 emitS("b64d", s) 224 } 225 group("base64: embedded CR/LF") 226 for _, s := range []string{"\n", "\r", "\r\n", "\n\n\n\n", "Zm9v\n", "\nZm9v", "Zm\n9v", "Z\rm\n9\r\nv", 227 "Zm9v\r\nYmFy", "Zm9vYmFy\n", "Zg=\n=", "Zg\n==", "Zm8\r=", "Zm9v\nYg", "Zm9vYg\n=\n=\n", "Zm9v\n=", 228 "\nZ\nm\n9\nv\nY\nm\nF\ny\n", "Zm9vYmFy\r\nZm9vYmFy\r\n"} { 229 emitS("b64d", s) 230 } 231 232 group("base32: bad characters") 233 for _, s := range []string{"MZXW6!", "MZXW 6", "MZXW6\t", "MZXW0", "MZXW1", "MZXW8", "MZXW9", "MZ-W6", "MZXW6\x00", 234 "MZXW6\x80", "MZXW6YTB\"", "MZXW6===", "MY======", "MZXQ====", "MZXW6YQ=", "MZXW6YTBOI======", "="} { 235 emitS("b32d", s) 236 } 237 group("base32: padding bytes (= and the NoPadding pad byte 0xff)") 238 for _, s := range []string{"MY\xff\xff\xff\xff\xff\xff", "MZXQ\xff\xff\xff\xff", "MZXW6\xff\xff\xff", "MZXW6YQ\xff", 239 "MY\xff\xff\xff\xff\xff\xffMY", "MY\xff\xff\xff\xff\xffA", "MY\xff\xff\xff\xff\xff", "M\xff\xff\xff\xff\xff\xff\xff", 240 "MZX\xff\xff\xff\xff\xff", "MZXW6Y\xff\xff", "MZ\xff", "MZXW\xff", "\xff", "\xff\xff", "MZXW6YTB\xff", 241 "MZXW6YTBMY\xff\xff\xff\xff\xff\xff", "MY\xff\xff\xff\xff\xff\xffAB", "MY\xff\xff\xff\xff\xff\xffABCDEFG", 242 "MZXQ\xff\xff\xff\xffZ", "MZXW6\xff\xff\xffQQ", "MY\xff\r\n\xff\xff\xff\xff\xff", "MY\xff\xff\xff\xff\xff\xff\n"} { 243 emitS("b32d", s) 244 } 245 group("base32: non-zero trailing bits") 246 for _, s := range []string{"MZ", "M7", "MZXR", "MZX7", "MZXW7", "MZXW6YR", "MZXW6YTBOJ", "MZXW6YTBO7", "77", "`7777777`"} { 247 emitS("b32d", s) 248 } 249 group("base32: wrong lengths (1, 3 and 6 trailing symbols)") 250 for _, s := range []string{"M", "MZX", "MZXW6Y", "MZXW6YTBO", "MZXW6YTBOIM", "MZXW6YTBOIMZXW", "A", "AAA", "AAAAAA", 251 "MZXW6YTB", "MZXW6YTBOI"} { 252 emitS("b32d", s) 253 } 254 group("base32: mixed case") 255 for _, s := range []string{"mzxw6", "Mzxw6", "MZXw6", "mzxw6ytboi", "MZXW6YTBoi"} { 256 emitS("b32d", s) 257 } 258 group("base32: embedded CR/LF") 259 for _, s := range []string{"\n", "\r\n", "MZXW6\n", "\nMZXW6", "MZ\nXW6", "M\rZ\nX\r\nW6", "MZXW6YTB\r\nOI", 260 "MZX\nW", "M\nZ", "MZXW6Y\nTBOI\n"} { 261 emitS("b32d", s) 262 } 263 264 group("hex: bad characters, odd lengths, mixed case") 265 for _, s := range []string{"0", "000", "0g", "g0", "zz", "0x00", " 00", "00 ", "0\n", "\n00", "00\r\n", "ABCDEF", 266 "abcdef", "aBcDeF", "0123456789abcdefABCDEF", "\xff\xff", "0\x00", "-1", "fG"} { 267 emitS("hexd", s) 268 } 269 270 group("tka.AUMHash: valid, case, length") 271 emitS("aumd", aum) 272 emitS("aumd", strings.ToLower(aum)) 273 emitS("aumd", aum[:51]) 274 emitS("aumd", aum+"A") 275 emitS("aumd", aum[:51]+"=") 276 emitS("aumd", aum+"====") 277 emitS("aumd", aum[:50]+"Q") 278 emitS("aumd", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") 279 emitS("aumd", "`7777777777777777777777777777777777777777777777777777`") 280 emitS("aumd", aum[:51]+"7") 281 emitS("aumd", aum[:51]+"1") 282 emitS("aumd", aum[:20]+"!"+aum[21:]) 283 emitS("aumd", base32.StdEncoding.EncodeToString(h32)) 284 group("tka.AUMHash: CR/LF counted in the length, then stripped") 285 emitS("aumd", aum[:51]+"\n") 286 emitS("aumd", aum[:50]+"\r\n") 287 emitS("aumd", "\n"+aum[:51]) 288 emitS("aumd", aum[:26]+"\n"+aum[26:51]) 289 emitS("aumd", aum[:48]+"\n\n\n\n") 290 emitS("aumd", aum[:49]+"\n\n\n") 291 emitS("aumd", aum[:47]+"\n\n\n\n\n") 292 emitS("aumd", aum[:46]+"\n\n\n\n\n\n") 293 emitS("aumd", aum[:8]+strings.Repeat("\n", 44)) 294 emitS("aumd", strings.Repeat("\r\n", 26)) 295 emitS("aumd", aum[:52]+"\n") 296 group("tka.AUMHash: the NoPadding pad byte 0xff") 297 emitS("aumd", "MY\xff\xff\xff\xff\xff\xff"+strings.Repeat("\n", 44)) 298 emitS("aumd", aum[:48]+"MY\xff\xff") 299 emitS("aumd", aum[:50]+"\xff\xff") 300 emitS("aumd", aum[:40]+"MZXQ\xff\xff\xff\xff"+strings.Repeat("\n", 4)) 301 302 group("key.NodePublic: valid, case, prefix, length") 303 emitS("nkyd", nk) 304 emitS("nkyd", "nodekey:"+strings.ToUpper(hex.EncodeToString(h32))) 305 mixed := []byte(nk) 306 for i := 8; i < len(mixed); i += 3 { 307 if mixed[i] >= 'a' && mixed[i] <= 'f' { 308 mixed[i] -= 32 309 } 310 } 311 emit("nkyd", mixed) 312 emitS("nkyd", "NODEKEY:"+nk[8:]) 313 emitS("nkyd", "Nodekey:"+nk[8:]) 314 emitS("nkyd", "nodekey"+nk[8:]) 315 emitS("nkyd", "nodekey;"+nk[8:]) 316 emitS("nkyd", "nodekey: "+nk[9:]) 317 emitS("nkyd", "discokey:"+nk[8:]) 318 emitS("nkyd", "mkey:"+nk[8:]) 319 emitS("nkyd", "privkey:"+nk[8:]) 320 emitS("nkyd", nk[8:]) 321 emitS("nkyd", nk[:71]) 322 emitS("nkyd", nk+"0") 323 emitS("nkyd", nk+"00") 324 emitS("nkyd", nk+"\n") 325 emitS("nkyd", nk[:70]) 326 emitS("nkyd", "nodekey:") 327 emitS("nkyd", "nodekey") 328 emitS("nkyd", nk[:71]+"g") 329 emitS("nkyd", nk[:8]+"x"+nk[9:]) 330 emitS("nkyd", nk[:40]+"\n"+nk[41:]) 331 emitS("nkyd", nk[:40]+" "+nk[41:]) 332 emitS("nkyd", nk[:71]+"\xff") 333 emitS("nkyd", "nodekey:"+strings.Repeat("0", 64)) 334 emitS("nkyd", "nodekey:"+strings.Repeat("f", 64)) 335 emitS("nkyd", "nodekey:"+strings.Repeat("F", 64)) 336 emitS("nkyd", "nodekey:0x"+nk[10:]) 337 } 338 339 // interesting bytes for mutations: alphabet characters are added per codec 340 var noise = []byte("=\r\n \t\x00\x80\xff-_\"\\!.:gGzZ0189+/aA") 341 342 func mutate(r *rand.Rand, s []byte, alpha string) []byte { 343 pick := func() byte { 344 if r.IntN(2) == 0 { 345 return alpha[r.IntN(len(alpha))] 346 } 347 return noise[r.IntN(len(noise))] 348 } 349 t := append([]byte(nil), s...) 350 for range 1 + r.IntN(3) { 351 switch k := r.IntN(7); { 352 case k == 0 && len(t) > 0: 353 t[r.IntN(len(t))] = pick() 354 case k == 1: 355 i := r.IntN(len(t) + 1) 356 t = append(t[:i], append([]byte{pick()}, t[i:]...)...) 357 case k == 2 && len(t) > 0: 358 i := r.IntN(len(t)) 359 t = append(t[:i], t[i+1:]...) 360 case k == 3 && len(t) > 0: 361 t = t[:r.IntN(len(t))] 362 case k == 4: 363 t = append(t, pick()) 364 case k == 5 && len(t) > 0: 365 // flip the case of one byte 366 i := r.IntN(len(t)) 367 t[i] ^= 32 368 case k == 6: 369 // a CR or LF somewhere 370 i := r.IntN(len(t) + 1) 371 t = append(t[:i], append([]byte{"\r\n"[r.IntN(2)]}, t[i:]...)...) 372 } 373 } 374 return t 375 } 376 377 func randBytes(r *rand.Rand, n int) []byte { 378 b := make([]byte, n) 379 for i := range b { 380 b[i] = byte(r.Uint32()) 381 } 382 return b 383 } 384 385 // capacity: the largest input each coder takes (encoders 49152, 40960, 32768 bytes; decoders 386 // 65536 characters), valid and with one bad last character, and 65536 random bytes per decoder 387 func capacity(r *rand.Rand) { 388 big := randBytes(r, 49152) 389 emit("b64e", big) 390 emit("b32e", big[:40960]) 391 emit("hexe", big[:32768]) 392 enc := map[string]string{ 393 "b64d": base64.StdEncoding.EncodeToString(big), 394 "b32d": base32StdNoPad.EncodeToString(big[:40960]), 395 "hexd": hex.EncodeToString(big[:32768]), 396 } 397 for _, op := range []string{"b64d", "b32d", "hexd"} { 398 e := []byte(enc[op]) 399 emit(op, e) 400 e[len(e)-1] = '!' 401 emit(op, e) 402 emit(op, randBytes(r, 65536)) 403 } 404 } 405 406 // rand: every length 0..2048 for each encoder and the round-trip decode, mutated decoder inputs, 407 // random garbage, then the capacity edges 408 func randCorpus(seed uint64) { 409 r := rand.New(rand.NewPCG(seed, seed^0x6f6d6e6574)) 410 for range 300 { 411 for _, op := range []string{"b64d", "b32d", "hexd", "aumd", "nkyd"} { 412 emit(op, randBytes(r, r.IntN(4097))) 413 } 414 } 415 for n := 0; n <= 2048; n++ { 416 b := randBytes(r, n) 417 e64 := base64.StdEncoding.EncodeToString(b) 418 e32 := base32StdNoPad.EncodeToString(b) 419 e16 := hex.EncodeToString(b) 420 emit("b64e", b) 421 emit("b32e", b) 422 emit("hexe", b) 423 emitS("b64d", e64) 424 emitS("b32d", e32) 425 emitS("hexd", e16) 426 } 427 for range 4000 { 428 b := randBytes(r, r.IntN(40)) 429 emit("b64d", mutate(r, []byte(base64.StdEncoding.EncodeToString(b)), b64alpha)) 430 emit("b32d", mutate(r, []byte(base32StdNoPad.EncodeToString(b)), b32alpha)) 431 emit("hexd", mutate(r, []byte(hex.EncodeToString(b)), hexalpha)) 432 } 433 for range 2000 { 434 h := randBytes(r, 32) 435 a := base32StdNoPad.EncodeToString(h) 436 k := "nodekey:" + hex.EncodeToString(h) 437 emit("aume", h) 438 emit("nkye", h) 439 emitS("aumd", a) 440 emitS("nkyd", k) 441 emit("aumd", mutate(r, []byte(a), b32alpha)) 442 emit("nkyd", mutate(r, []byte(k), hexalpha)) 443 // same length, k symbols replaced by CR/LF or by the 0xff pad byte 444 m := []byte(a) 445 for range 1 + r.IntN(8) { 446 m[r.IntN(len(m))] = "\r\n\xff"[r.IntN(3)] 447 } 448 emit("aumd", m) 449 } 450 capacity(r) 451 } 452 453 func main() { 454 defer out.Flush() 455 switch { 456 case len(os.Args) == 1: 457 referee() 458 case len(os.Args) == 2 && os.Args[1] == "corpus": 459 corpus() 460 case len(os.Args) == 3 && os.Args[1] == "rand": 461 seed, err := strconv.ParseUint(os.Args[2], 10, 64) 462 if err != nil { 463 fail("bad seed") 464 } 465 randCorpus(seed) 466 default: 467 fail("usage: oracle [corpus | rand SEED]") 468 } 469 }