randfill.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. /*
  2. Copyright 2014 Google Inc. All rights reserved.
  3. Copyright 2014 The gofuzz Authors.
  4. Copyright 2025 The Kubernetes Authors.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. */
  15. // Package randfill is a library for populating go objects with random values.
  16. package randfill
  17. import (
  18. "fmt"
  19. "math/rand"
  20. "reflect"
  21. "regexp"
  22. "sync"
  23. "time"
  24. "unsafe"
  25. "strings"
  26. "sigs.k8s.io/randfill/bytesource"
  27. )
  28. // funcMap is a map from a type to a function that randfills that type. The
  29. // function is a reflect.Value because the type being filled is different for
  30. // each func.
  31. type funcMap map[reflect.Type]reflect.Value
  32. // Filler knows how to fill any object with random fields.
  33. type Filler struct {
  34. customFuncs funcMap
  35. defaultFuncs funcMap
  36. r *rand.Rand
  37. nilChance float64
  38. minElements int
  39. maxElements int
  40. maxDepth int
  41. allowUnexportedFields bool
  42. skipFieldPatterns []*regexp.Regexp
  43. lock sync.Mutex
  44. }
  45. // New returns a new Filler. Customize your Filler further by calling Funcs,
  46. // RandSource, NilChance, or NumElements in any order.
  47. func New() *Filler {
  48. return NewWithSeed(time.Now().UnixNano())
  49. }
  50. func NewWithSeed(seed int64) *Filler {
  51. f := &Filler{
  52. defaultFuncs: funcMap{
  53. reflect.TypeOf(&time.Time{}): reflect.ValueOf(randfillTime),
  54. },
  55. customFuncs: funcMap{},
  56. r: rand.New(rand.NewSource(seed)),
  57. nilChance: .2,
  58. minElements: 1,
  59. maxElements: 10,
  60. maxDepth: 100,
  61. allowUnexportedFields: false,
  62. }
  63. return f
  64. }
  65. // NewFromGoFuzz is a helper function that enables using randfill (this
  66. // project) with go-fuzz (https://github.com/dvyukov/go-fuzz) for continuous
  67. // fuzzing. Essentially, it enables translating the fuzzing bytes from
  68. // go-fuzz to any Go object using this library.
  69. //
  70. // This implementation promises a constant translation from a given slice of
  71. // bytes to the fuzzed objects. This promise will remain over future
  72. // versions of Go and of this library.
  73. //
  74. // Note: the returned Filler should not be shared between multiple goroutines,
  75. // as its deterministic output will no longer be available.
  76. //
  77. // Example: use go-fuzz to test the function `MyFunc(int)` in the package
  78. // `mypackage`. Add the file: "mypackage_fuzz.go" with the content:
  79. //
  80. // // +build gofuzz
  81. // package mypackage
  82. // import "sigs.k8s.io/randfill"
  83. //
  84. // func Fuzz(data []byte) int {
  85. // var i int
  86. // randfill.NewFromGoFuzz(data).Fill(&i)
  87. // MyFunc(i)
  88. // return 0
  89. // }
  90. func NewFromGoFuzz(data []byte) *Filler {
  91. return New().RandSource(bytesource.New(data))
  92. }
  93. // Funcs registers custom fill functions for this Filler.
  94. //
  95. // Each entry in customFuncs must be a function taking two parameters.
  96. // The first parameter must be a pointer or map. It is the variable that
  97. // function will fill with random data. The second parameter must be a
  98. // randfill.Continue, which will provide a source of randomness and a way
  99. // to automatically continue filling smaller pieces of the first parameter.
  100. //
  101. // These functions are called sensibly, e.g., if you wanted custom string
  102. // filling, the function `func(s *string, c randfill.Continue)` would get
  103. // called and passed the address of strings. Maps and pointers will always
  104. // be made/new'd for you, ignoring the NilChance option. For slices, it
  105. // doesn't make much sense to pre-create them--Filler doesn't know how
  106. // long you want your slice--so take a pointer to a slice, and make it
  107. // yourself. (If you don't want your map/pointer type pre-made, take a
  108. // pointer to it, and make it yourself.) See the examples for a range of
  109. // custom functions.
  110. //
  111. // If a function is already registered for a type, and a new function is
  112. // provided, the previous function will be replaced with the new one.
  113. func (f *Filler) Funcs(customFuncs ...interface{}) *Filler {
  114. for i := range customFuncs {
  115. v := reflect.ValueOf(customFuncs[i])
  116. if v.Kind() != reflect.Func {
  117. panic("Filler.Funcs: all arguments must be functions")
  118. }
  119. t := v.Type()
  120. if t.NumIn() != 2 || t.NumOut() != 0 {
  121. panic("Filler.Funcs: all customFuncs must have 2 arguments and 0 returns")
  122. }
  123. argT := t.In(0)
  124. switch argT.Kind() {
  125. case reflect.Ptr, reflect.Map:
  126. default:
  127. panic("Filler.Funcs: customFuncs' first argument must be a pointer or map type")
  128. }
  129. if t.In(1) != reflect.TypeOf(Continue{}) {
  130. panic("Filler.Funcs: customFuncs' second argument must be a randfill.Continue")
  131. }
  132. f.customFuncs[argT] = v
  133. }
  134. return f
  135. }
  136. // RandSource causes this Filler to get values from the given source of
  137. // randomness. Use this if you want deterministic filling.
  138. func (f *Filler) RandSource(s rand.Source) *Filler {
  139. f.r = rand.New(s)
  140. return f
  141. }
  142. // NilChance sets the probability of creating a nil pointer, map, or slice to
  143. // 'p'. 'p' should be between 0 (no nils) and 1 (all nils), inclusive.
  144. func (f *Filler) NilChance(p float64) *Filler {
  145. if p < 0 || p > 1 {
  146. panic("Filler.NilChance: p must be between 0 and 1, inclusive")
  147. }
  148. f.nilChance = p
  149. return f
  150. }
  151. // NumElements sets the minimum and maximum number of elements that will be
  152. // added to a non-nil map or slice.
  153. func (f *Filler) NumElements(min, max int) *Filler {
  154. if min < 0 {
  155. panic("Filler.NumElements: min must be >= 0")
  156. }
  157. if min > max {
  158. panic("Filler.NumElements: min must be <= max")
  159. }
  160. f.minElements = min
  161. f.maxElements = max
  162. return f
  163. }
  164. func (f *Filler) genElementCount() int {
  165. if f.minElements == f.maxElements {
  166. return f.minElements
  167. }
  168. return f.minElements + f.r.Intn(f.maxElements-f.minElements+1)
  169. }
  170. func (f *Filler) genShouldFill() bool {
  171. return f.r.Float64() >= f.nilChance
  172. }
  173. // MaxDepth sets the maximum number of recursive fill calls that will be made
  174. // before stopping. This includes struct members, pointers, and map and slice
  175. // elements.
  176. func (f *Filler) MaxDepth(d int) *Filler {
  177. f.maxDepth = d
  178. return f
  179. }
  180. // AllowUnexportedFields defines whether to fill unexported fields.
  181. func (f *Filler) AllowUnexportedFields(flag bool) *Filler {
  182. f.allowUnexportedFields = flag
  183. return f
  184. }
  185. // SkipFieldsWithPattern tells this Filler to skip any field whose name matches
  186. // the supplied pattern. Call this multiple times if needed. This is useful to
  187. // skip XXX_ fields generated by protobuf.
  188. func (f *Filler) SkipFieldsWithPattern(pattern *regexp.Regexp) *Filler {
  189. f.skipFieldPatterns = append(f.skipFieldPatterns, pattern)
  190. return f
  191. }
  192. // SimpleSelfFiller represents an object that knows how to randfill itself.
  193. //
  194. // Unlike NativeSelfFiller, this interface does not cause the type in question
  195. // to depend on the randfill package. This is most useful for simple types. For
  196. // more complex types, consider using NativeSelfFiller.
  197. type SimpleSelfFiller interface {
  198. // RandFill fills the current object with random data.
  199. RandFill(r *rand.Rand)
  200. }
  201. // NativeSelfFiller represents an object that knows how to randfill itself.
  202. //
  203. // Unlike SimpleSelfFiller, this interface allows for recursive filling of
  204. // child objects with the same rules as the parent Filler.
  205. type NativeSelfFiller interface {
  206. // RandFill fills the current object with random data.
  207. RandFill(c Continue)
  208. }
  209. // Fill recursively fills all of obj's fields with something random. First
  210. // this tries to find a custom fill function (see Funcs). If there is no
  211. // custom function, this tests whether the object implements SimpleSelfFiller
  212. // or NativeSelfFiller and if so, calls RandFill on it to fill itself. If that
  213. // fails, this will see if there is a default fill function provided by this
  214. // package. If all of that fails, this will generate random values for all
  215. // primitive fields and then recurse for all non-primitives.
  216. //
  217. // This is safe for cyclic or tree-like structs, up to a limit. Use the
  218. // MaxDepth method to adjust how deep you need it to recurse.
  219. //
  220. // obj must be a pointer. Exported (public) fields can always be set, and if
  221. // the AllowUnexportedFields() modifier was called it can try to set unexported
  222. // (private) fields, too.
  223. //
  224. // This is intended for tests, so will panic on bad input or unimplemented
  225. // types. This method takes a lock for the whole Filler, so it is not
  226. // reentrant. See Continue.
  227. func (f *Filler) Fill(obj interface{}) {
  228. f.lock.Lock()
  229. defer f.lock.Unlock()
  230. v := reflect.ValueOf(obj)
  231. if v.Kind() != reflect.Ptr {
  232. panic("Filler.Fill: obj must be a pointer")
  233. }
  234. v = v.Elem()
  235. f.fillWithContext(v, 0)
  236. }
  237. // FillNoCustom is just like Fill, except that any custom fill function for
  238. // obj's type will not be called and obj will not be tested for
  239. // SimpleSelfFiller or NativeSelfFiller. This applies only to obj and not other
  240. // instances of obj's type or to obj's child fields.
  241. //
  242. // obj must be a pointer. Exported (public) fields can always be set, and if
  243. // the AllowUnexportedFields() modifier was called it can try to set unexported
  244. // (private) fields, too.
  245. //
  246. // This is intended for tests, so will panic on bad input or unimplemented
  247. // types. This method takes a lock for the whole Filler, so it is not
  248. // reentrant. See Continue.
  249. func (f *Filler) FillNoCustom(obj interface{}) {
  250. f.lock.Lock()
  251. defer f.lock.Unlock()
  252. v := reflect.ValueOf(obj)
  253. if v.Kind() != reflect.Ptr {
  254. panic("Filler.FillNoCustom: obj must be a pointer")
  255. }
  256. v = v.Elem()
  257. f.fillWithContext(v, flagNoCustomFill)
  258. }
  259. const (
  260. // Do not try to find a custom fill function. Does not apply recursively.
  261. flagNoCustomFill uint64 = 1 << iota
  262. )
  263. func (f *Filler) fillWithContext(v reflect.Value, flags uint64) {
  264. fc := &fillerContext{filler: f}
  265. fc.doFill(v, flags)
  266. }
  267. // fillerContext carries context about a single filling run, which lets Filler
  268. // be thread-safe.
  269. type fillerContext struct {
  270. filler *Filler
  271. curDepth int
  272. }
  273. func (fc *fillerContext) doFill(v reflect.Value, flags uint64) {
  274. if fc.curDepth >= fc.filler.maxDepth {
  275. return
  276. }
  277. fc.curDepth++
  278. defer func() { fc.curDepth-- }()
  279. if !v.CanSet() {
  280. if !fc.filler.allowUnexportedFields || !v.CanAddr() {
  281. return
  282. }
  283. v = reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
  284. }
  285. if flags&flagNoCustomFill == 0 {
  286. // Check for both pointer and non-pointer custom functions.
  287. if v.CanAddr() && fc.tryCustom(v.Addr()) {
  288. return
  289. }
  290. if fc.tryCustom(v) {
  291. return
  292. }
  293. }
  294. if fn, ok := fillFuncMap[v.Kind()]; ok {
  295. fn(v, fc.filler.r)
  296. return
  297. }
  298. switch v.Kind() {
  299. case reflect.Map:
  300. if fc.filler.genShouldFill() {
  301. v.Set(reflect.MakeMap(v.Type()))
  302. n := fc.filler.genElementCount()
  303. for i := 0; i < n; i++ {
  304. key := reflect.New(v.Type().Key()).Elem()
  305. fc.doFill(key, 0)
  306. val := reflect.New(v.Type().Elem()).Elem()
  307. fc.doFill(val, 0)
  308. v.SetMapIndex(key, val)
  309. }
  310. return
  311. }
  312. v.Set(reflect.Zero(v.Type()))
  313. case reflect.Ptr:
  314. if fc.filler.genShouldFill() {
  315. v.Set(reflect.New(v.Type().Elem()))
  316. fc.doFill(v.Elem(), 0)
  317. return
  318. }
  319. v.Set(reflect.Zero(v.Type()))
  320. case reflect.Slice:
  321. if fc.filler.genShouldFill() {
  322. n := fc.filler.genElementCount()
  323. v.Set(reflect.MakeSlice(v.Type(), n, n))
  324. for i := 0; i < n; i++ {
  325. fc.doFill(v.Index(i), 0)
  326. }
  327. return
  328. }
  329. v.Set(reflect.Zero(v.Type()))
  330. case reflect.Array:
  331. if fc.filler.genShouldFill() {
  332. n := v.Len()
  333. for i := 0; i < n; i++ {
  334. fc.doFill(v.Index(i), 0)
  335. }
  336. return
  337. }
  338. v.Set(reflect.Zero(v.Type()))
  339. case reflect.Struct:
  340. for i := 0; i < v.NumField(); i++ {
  341. skipField := false
  342. fieldName := v.Type().Field(i).Name
  343. for _, pattern := range fc.filler.skipFieldPatterns {
  344. if pattern.MatchString(fieldName) {
  345. skipField = true
  346. break
  347. }
  348. }
  349. if !skipField {
  350. fc.doFill(v.Field(i), 0)
  351. }
  352. }
  353. case reflect.Chan:
  354. fallthrough
  355. case reflect.Func:
  356. fallthrough
  357. case reflect.Interface:
  358. fallthrough
  359. default:
  360. panic(fmt.Sprintf("can't fill type %v, kind %v", v.Type(), v.Kind()))
  361. }
  362. }
  363. // tryCustom searches for custom handlers, and returns true iff it finds a match
  364. // and successfully randomizes v.
  365. func (fc *fillerContext) tryCustom(v reflect.Value) bool {
  366. // First: see if we have a fill function for it.
  367. doCustom, ok := fc.filler.customFuncs[v.Type()]
  368. if !ok {
  369. // Second: see if it can fill itself.
  370. if v.CanInterface() {
  371. intf := v.Interface()
  372. if fillable, ok := intf.(SimpleSelfFiller); ok {
  373. fillable.RandFill(fc.filler.r)
  374. return true
  375. }
  376. if fillable, ok := intf.(NativeSelfFiller); ok {
  377. fillable.RandFill(Continue{fc: fc, Rand: fc.filler.r})
  378. return true
  379. }
  380. }
  381. // Finally: see if there is a default fill function.
  382. doCustom, ok = fc.filler.defaultFuncs[v.Type()]
  383. if !ok {
  384. return false
  385. }
  386. }
  387. switch v.Kind() {
  388. case reflect.Ptr:
  389. if v.IsNil() {
  390. if !v.CanSet() {
  391. return false
  392. }
  393. v.Set(reflect.New(v.Type().Elem()))
  394. }
  395. case reflect.Map:
  396. if v.IsNil() {
  397. if !v.CanSet() {
  398. return false
  399. }
  400. v.Set(reflect.MakeMap(v.Type()))
  401. }
  402. default:
  403. return false
  404. }
  405. doCustom.Call([]reflect.Value{
  406. v,
  407. reflect.ValueOf(Continue{
  408. fc: fc,
  409. Rand: fc.filler.r,
  410. }),
  411. })
  412. return true
  413. }
  414. // Continue can be passed to custom fill functions to allow them to use
  415. // the correct source of randomness and to continue filling their members.
  416. type Continue struct {
  417. fc *fillerContext
  418. // For convenience, Continue implements rand.Rand via embedding.
  419. // Use this for generating any randomness if you want your filling
  420. // to be repeatable for a given seed.
  421. *rand.Rand
  422. }
  423. // Fill continues filling obj. obj must be a pointer or a reflect.Value of a
  424. // pointer. See Filler.Fill.
  425. func (c Continue) Fill(obj interface{}) {
  426. v, ok := obj.(reflect.Value)
  427. if !ok {
  428. v = reflect.ValueOf(obj)
  429. }
  430. if v.Kind() != reflect.Ptr {
  431. panic("Continue.Fill: obj must be a pointer")
  432. }
  433. v = v.Elem()
  434. c.fc.doFill(v, 0)
  435. }
  436. // FillNoCustom continues filling obj, except that any custom fill function for
  437. // obj's type will not be called and obj will not be tested for
  438. // SimpleSelfFiller or NativeSelfFiller. See Filler.FillNoCustom.
  439. func (c Continue) FillNoCustom(obj interface{}) {
  440. v, ok := obj.(reflect.Value)
  441. if !ok {
  442. v = reflect.ValueOf(obj)
  443. }
  444. if v.Kind() != reflect.Ptr {
  445. panic("Continue.FillNoCustom: obj must be a pointer")
  446. }
  447. v = v.Elem()
  448. c.fc.doFill(v, flagNoCustomFill)
  449. }
  450. const defaultStringMaxLen = 20
  451. // String makes a random string up to n characters long. If n is 0, the default
  452. // size range is [0-20). The returned string may include a variety of (valid)
  453. // UTF-8 encodings.
  454. func (c Continue) String(n int) string {
  455. return randString(c.Rand, n)
  456. }
  457. // Uint64 makes random 64 bit numbers.
  458. // Weirdly, rand doesn't have a function that gives you 64 random bits.
  459. func (c Continue) Uint64() uint64 {
  460. return randUint64(c.Rand)
  461. }
  462. // Bool returns true or false randomly.
  463. func (c Continue) Bool() bool {
  464. return randBool(c.Rand)
  465. }
  466. func fillInt(v reflect.Value, r *rand.Rand) {
  467. v.SetInt(int64(randUint64(r)))
  468. }
  469. func fillUint(v reflect.Value, r *rand.Rand) {
  470. v.SetUint(randUint64(r))
  471. }
  472. func randfillTime(t *time.Time, c Continue) {
  473. var sec, nsec int64
  474. // Allow for about 1000 years of random time values, which keeps things
  475. // like JSON parsing reasonably happy.
  476. sec = c.Rand.Int63n(1000 * 365 * 24 * 60 * 60)
  477. // Nanosecond values greater than 1Bn are technically allowed but result in
  478. // time.Time values with invalid timezone offsets.
  479. nsec = c.Rand.Int63n(999999999)
  480. *t = time.Unix(sec, nsec)
  481. }
  482. var fillFuncMap = map[reflect.Kind]func(reflect.Value, *rand.Rand){
  483. reflect.Bool: func(v reflect.Value, r *rand.Rand) {
  484. v.SetBool(randBool(r))
  485. },
  486. reflect.Int: fillInt,
  487. reflect.Int8: fillInt,
  488. reflect.Int16: fillInt,
  489. reflect.Int32: fillInt,
  490. reflect.Int64: fillInt,
  491. reflect.Uint: fillUint,
  492. reflect.Uint8: fillUint,
  493. reflect.Uint16: fillUint,
  494. reflect.Uint32: fillUint,
  495. reflect.Uint64: fillUint,
  496. reflect.Uintptr: fillUint,
  497. reflect.Float32: func(v reflect.Value, r *rand.Rand) {
  498. v.SetFloat(float64(r.Float32()))
  499. },
  500. reflect.Float64: func(v reflect.Value, r *rand.Rand) {
  501. v.SetFloat(r.Float64())
  502. },
  503. reflect.Complex64: func(v reflect.Value, r *rand.Rand) {
  504. v.SetComplex(complex128(complex(r.Float32(), r.Float32())))
  505. },
  506. reflect.Complex128: func(v reflect.Value, r *rand.Rand) {
  507. v.SetComplex(complex(r.Float64(), r.Float64()))
  508. },
  509. reflect.String: func(v reflect.Value, r *rand.Rand) {
  510. v.SetString(randString(r, 0))
  511. },
  512. reflect.UnsafePointer: func(v reflect.Value, r *rand.Rand) {
  513. panic("filling of UnsafePointers is not implemented")
  514. },
  515. }
  516. // randBool returns true or false randomly.
  517. func randBool(r *rand.Rand) bool {
  518. return r.Int31()&(1<<30) == 0
  519. }
  520. type int63nPicker interface {
  521. Int63n(int64) int64
  522. }
  523. // UnicodeRange describes a sequential range of unicode characters.
  524. // Last must be numerically greater than First.
  525. type UnicodeRange struct {
  526. First, Last rune
  527. }
  528. // UnicodeRanges describes an arbitrary number of sequential ranges of unicode characters.
  529. // To be useful, each range must have at least one character (First <= Last) and
  530. // there must be at least one range.
  531. type UnicodeRanges []UnicodeRange
  532. // choose returns a random unicode character from the given range, using the
  533. // given randomness source.
  534. func (ur UnicodeRange) choose(r int63nPicker) rune {
  535. count := int64(ur.Last - ur.First + 1)
  536. return ur.First + rune(r.Int63n(count))
  537. }
  538. // CustomStringFillFunc constructs a FillFunc which produces random strings.
  539. // Each character is selected from the range ur. If there are no characters
  540. // in the range (cr.Last < cr.First), this will panic.
  541. func (ur UnicodeRange) CustomStringFillFunc(n int) func(s *string, c Continue) {
  542. ur.check()
  543. return func(s *string, c Continue) {
  544. *s = ur.randString(c.Rand, n)
  545. }
  546. }
  547. // check is a function that used to check whether the first of ur(UnicodeRange)
  548. // is greater than the last one.
  549. func (ur UnicodeRange) check() {
  550. if ur.Last < ur.First {
  551. panic("UnicodeRange.check: the last encoding must be greater than the first")
  552. }
  553. }
  554. // randString of UnicodeRange makes a random string up to 20 characters long.
  555. // Each character is selected form ur(UnicodeRange).
  556. func (ur UnicodeRange) randString(r *rand.Rand, max int) string {
  557. if max == 0 {
  558. max = defaultStringMaxLen
  559. }
  560. n := r.Intn(max)
  561. sb := strings.Builder{}
  562. sb.Grow(n)
  563. for i := 0; i < n; i++ {
  564. sb.WriteRune(ur.choose(r))
  565. }
  566. return sb.String()
  567. }
  568. // defaultUnicodeRanges sets a default unicode range when users do not set
  569. // CustomStringFillFunc() but want to fill strings.
  570. var defaultUnicodeRanges = UnicodeRanges{
  571. {' ', '~'}, // ASCII characters
  572. {'\u00a0', '\u02af'}, // Multi-byte encoded characters
  573. {'\u4e00', '\u9fff'}, // Common CJK (even longer encodings)
  574. }
  575. // CustomStringFillFunc constructs a FillFunc which produces random strings.
  576. // Each character is selected from one of the ranges of ur(UnicodeRanges).
  577. // Each range has an equal probability of being chosen. If there are no ranges,
  578. // or a selected range has no characters (.Last < .First), this will panic.
  579. // Do not modify any of the ranges in ur after calling this function.
  580. func (ur UnicodeRanges) CustomStringFillFunc(n int) func(s *string, c Continue) {
  581. // Check unicode ranges slice is empty.
  582. if len(ur) == 0 {
  583. panic("UnicodeRanges is empty")
  584. }
  585. // if not empty, each range should be checked.
  586. for i := range ur {
  587. ur[i].check()
  588. }
  589. return func(s *string, c Continue) {
  590. *s = ur.randString(c.Rand, n)
  591. }
  592. }
  593. // randString of UnicodeRanges makes a random string up to 20 characters long.
  594. // Each character is selected form one of the ranges of ur(UnicodeRanges),
  595. // and each range has an equal probability of being chosen.
  596. func (ur UnicodeRanges) randString(r *rand.Rand, max int) string {
  597. if max == 0 {
  598. max = defaultStringMaxLen
  599. }
  600. n := r.Intn(max)
  601. sb := strings.Builder{}
  602. sb.Grow(n)
  603. for i := 0; i < n; i++ {
  604. sb.WriteRune(ur[r.Intn(len(ur))].choose(r))
  605. }
  606. return sb.String()
  607. }
  608. // randString makes a random string up to 20 characters long. The returned string
  609. // may include a variety of (valid) UTF-8 encodings.
  610. func randString(r *rand.Rand, max int) string {
  611. return defaultUnicodeRanges.randString(r, max)
  612. }
  613. // randUint64 makes random 64 bit numbers.
  614. // Weirdly, rand doesn't have a function that gives you 64 random bits.
  615. func randUint64(r *rand.Rand) uint64 {
  616. return uint64(r.Uint32())<<32 | uint64(r.Uint32())
  617. }