mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 18:43:34 +00:00
rebase: bump k8s.io/kubernetes in the k8s-dependencies group
Bumps the k8s-dependencies group with 1 update: [k8s.io/kubernetes](https://github.com/kubernetes/kubernetes). Updates `k8s.io/kubernetes` from 1.32.3 to 1.33.0 - [Release notes](https://github.com/kubernetes/kubernetes/releases) - [Commits](https://github.com/kubernetes/kubernetes/compare/v1.32.3...v1.33.0) --- updated-dependencies: - dependency-name: k8s.io/kubernetes dependency-version: 1.33.0 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: k8s-dependencies ... Signed-off-by: dependabot[bot] <support@github.com> Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
committed by
mergify[bot]
parent
4147d5d15a
commit
51895f8619
43
vendor/sigs.k8s.io/randfill/CONTRIBUTING.md
generated
vendored
Normal file
43
vendor/sigs.k8s.io/randfill/CONTRIBUTING.md
generated
vendored
Normal file
@ -0,0 +1,43 @@
|
||||
# Contributing Guidelines
|
||||
|
||||
Welcome to Kubernetes. We are excited about the prospect of you joining our [community](https://git.k8s.io/community)! The Kubernetes community abides by the CNCF [code of conduct](code-of-conduct.md). Here is an excerpt:
|
||||
|
||||
_As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities._
|
||||
|
||||
## Getting Started
|
||||
|
||||
We have full documentation on how to get started contributing here:
|
||||
|
||||
<!---
|
||||
If your repo has certain guidelines for contribution, put them here ahead of the general k8s resources
|
||||
-->
|
||||
|
||||
- [Contributor License Agreement](https://git.k8s.io/community/CLA.md) Kubernetes projects require that you sign a Contributor License Agreement (CLA) before we can accept your pull requests
|
||||
- [Kubernetes Contributor Guide](https://git.k8s.io/community/contributors/guide) - Main contributor documentation, or you can just jump directly to the [contributing section](https://git.k8s.io/community/contributors/guide#contributing)
|
||||
- [Contributor Cheat Sheet](https://git.k8s.io/community/contributors/guide/contributor-cheatsheet) - Common resources for existing developers
|
||||
|
||||
## Mentorship
|
||||
|
||||
- [Mentoring Initiatives](https://git.k8s.io/community/mentoring) - We have a diverse set of mentorship programs available that are always looking for volunteers!
|
||||
|
||||
<!---
|
||||
Custom Information - if you're copying this template for the first time you can add custom content here, for example:
|
||||
|
||||
## Contact Information
|
||||
|
||||
- [Slack channel](https://kubernetes.slack.com/messages/kubernetes-users) - Replace `kubernetes-users` with your slack channel string, this will send users directly to your channel.
|
||||
- [Mailing list](URL)
|
||||
|
||||
-->
|
||||
|
||||
## Project Management
|
||||
|
||||
The [maintainers](https://github.com/kubernetes-sigs/randfill/blob/main/OWNERS_ALIASES#L12) of this project (and often others who have official positions on the [contributor ladder](https://github.com/kubernetes-sigs/randfill/blob/main/OWNERS_ALIASES)) are responsible for performing project management which oversees development and maintenance of the API, tests, tools, e.t.c. While we try to be generally flexible when it comes to the management of individual pieces (such as Issues or PRs), we have some rules and guidelines which help us plan, coordinate and reduce waste. In this section you'll find some rules/guidelines for contributors related to project management which may extend or go beyond what you would find in the standard [Kubernetes Contributor Guide](https://git.k8s.io/community/contributors/guide).
|
||||
|
||||
### Bumping stale and closed Issues & PRs
|
||||
|
||||
Maintainers are ultimately responsible for triaging new issues and PRs, accepting or declining them, deciding priority and fitting them into milestones intended for future releases. Bots are responsible for marking issues and PRs which stagnate as stale, or closing them if progress does not continue for a long period of time. Due to the nature of this community-driven development effort (we do not have dedicated engineering resources, we rely on the community which is effectively "volunteer time") **not all issues can be accepted, prioritized or completed**.
|
||||
|
||||
You may find times when an issue you're subscribed to and interested in seems to stagnate, or perhaps gets auto-closed. Prior to bumping or directly re-opening issues yourself, we generally ask that you bring these up for discussion on the agenda for one of our community syncs if possible, or bring them up for discussion in Slack or the mailing list as this gives us a better opportunity to discuss the issue and determine viability and logistics. If feasible we **highly recommend being ready to contribute directly** to any stale or unprioritized effort that you want to see move forward, as **the best way to ensure progress is to engage with the community and personally invest time**.
|
||||
|
||||
We (the community) aren't opposed to making exceptions in some cases, but when in doubt please follow the above guidelines before bumping closed or stale issues if you're not ready to personally invest time in them. We are responsible for managing these and without further context or engagement we may set these back to how they were previously organized.
|
202
vendor/sigs.k8s.io/randfill/LICENSE
generated
vendored
Normal file
202
vendor/sigs.k8s.io/randfill/LICENSE
generated
vendored
Normal file
@ -0,0 +1,202 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright 2014 The gofuzz Authors
|
||||
Copyright 2025 The Kubernetes Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
24
vendor/sigs.k8s.io/randfill/NOTICE
generated
vendored
Normal file
24
vendor/sigs.k8s.io/randfill/NOTICE
generated
vendored
Normal file
@ -0,0 +1,24 @@
|
||||
When donating the randfill project to the CNCF, we could not reach all the
|
||||
gofuzz contributors to sign the CNCF CLA. As such, according to the CNCF rules
|
||||
to donate a repository, we must add a NOTICE referencing section 7 of the CLA
|
||||
with a list of developers who could not be reached.
|
||||
|
||||
`7. Should You wish to submit work that is not Your original creation, You may
|
||||
submit it to the Foundation separately from any Contribution, identifying the
|
||||
complete details of its source and of any license or other restriction
|
||||
(including, but not limited to, related patents, trademarks, and license
|
||||
agreements) of which you are personally aware, and conspicuously marking the
|
||||
work as "Submitted on behalf of a third-party: [named here]".`
|
||||
|
||||
Submitted on behalf of a third-party: @dnephin (Daniel Nephin)
|
||||
Submitted on behalf of a third-party: @AlekSi (Alexey Palazhchenko)
|
||||
Submitted on behalf of a third-party: @bbigras (Bruno Bigras)
|
||||
Submitted on behalf of a third-party: @samirkut (Samir)
|
||||
Submitted on behalf of a third-party: @posener (Eyal Posener)
|
||||
Submitted on behalf of a third-party: @Ashikpaul (Ashik Paul)
|
||||
Submitted on behalf of a third-party: @kwongtailau (Kwongtai)
|
||||
Submitted on behalf of a third-party: @ericcornelissen (Eric Cornelissen)
|
||||
Submitted on behalf of a third-party: @eclipseo (Robert-André Mauchin)
|
||||
Submitted on behalf of a third-party: @yanzhoupan (Andrew Pan)
|
||||
Submitted on behalf of a third-party: @STRRL (Zhiqiang ZHOU)
|
||||
Submitted on behalf of a third-party: @disconnect3d (Disconnect3d)
|
8
vendor/sigs.k8s.io/randfill/OWNERS
generated
vendored
Normal file
8
vendor/sigs.k8s.io/randfill/OWNERS
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
# See the OWNERS docs at https://go.k8s.io/owners
|
||||
# See the OWNERS_ALIASES file at https://github.com/kubernetes-sigs/randfill/blob/main/OWNERS_ALIASES for a list of members for each alias.
|
||||
|
||||
approvers:
|
||||
- sig-testing-leads
|
||||
- thockin
|
||||
|
||||
reviewers: []
|
14
vendor/sigs.k8s.io/randfill/OWNERS_ALIASES
generated
vendored
Normal file
14
vendor/sigs.k8s.io/randfill/OWNERS_ALIASES
generated
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
# See the OWNERS docs: https://git.k8s.io/community/contributors/guide/owners.md
|
||||
# This file should be kept in sync with k/org.
|
||||
|
||||
aliases:
|
||||
# Reference: https://github.com/kubernetes/org/blob/main/OWNERS_ALIASES
|
||||
sig-testing-leads:
|
||||
- BenTheElder
|
||||
- alvaroaleman
|
||||
- aojea
|
||||
- cjwagner
|
||||
- jbpratt
|
||||
- michelle192837
|
||||
- pohly
|
||||
- xmcqueen
|
98
vendor/sigs.k8s.io/randfill/README.md
generated
vendored
Normal file
98
vendor/sigs.k8s.io/randfill/README.md
generated
vendored
Normal file
@ -0,0 +1,98 @@
|
||||
randfill
|
||||
======
|
||||
|
||||
randfill is a library for populating go objects with random values.
|
||||
|
||||
This is a fork of github.com/google/gofuzz, which was archived.
|
||||
|
||||
NOTE: This repo is supported only for use within Kubernetes. It is not our
|
||||
intention to support general use. That said, if it works for you, that's
|
||||
great! If you have a problem, please feel free to file an issue, but be aware
|
||||
that it may not be a priority for us to fix it unless it is affecting
|
||||
Kubernetes. PRs are welcome, within reason.
|
||||
|
||||
[](https://godoc.org/sigs.k8s.io/randfill)
|
||||
|
||||
This is useful for testing:
|
||||
|
||||
* Do your project's objects really serialize/unserialize correctly in all cases?
|
||||
* Is there an incorrectly formatted object that will cause your project to panic?
|
||||
|
||||
Import with ```import "sigs.k8s.io/randfill"```
|
||||
|
||||
You can use it on single variables:
|
||||
```go
|
||||
f := randfill.New()
|
||||
var myInt int
|
||||
f.Fill(&myInt) // myInt gets a random value.
|
||||
```
|
||||
|
||||
You can use it on maps:
|
||||
```go
|
||||
f := randfill.New().NilChance(0).NumElements(1, 1)
|
||||
var myMap map[ComplexKeyType]string
|
||||
f.Fill(&myMap) // myMap will have exactly one element.
|
||||
```
|
||||
|
||||
Customize the chance of getting a nil pointer:
|
||||
```go
|
||||
f := randfill.New().NilChance(.5)
|
||||
var fancyStruct struct {
|
||||
A, B, C, D *string
|
||||
}
|
||||
f.Fill(&fancyStruct) // About half the pointers should be set.
|
||||
```
|
||||
|
||||
You can even customize the randomization completely if needed:
|
||||
```go
|
||||
type MyEnum string
|
||||
const (
|
||||
A MyEnum = "A"
|
||||
B MyEnum = "B"
|
||||
)
|
||||
type MyInfo struct {
|
||||
Type MyEnum
|
||||
AInfo *string
|
||||
BInfo *string
|
||||
}
|
||||
|
||||
f := randfill.New().NilChance(0).Funcs(
|
||||
func(e *MyInfo, c randfill.Continue) {
|
||||
switch c.Intn(2) {
|
||||
case 0:
|
||||
e.Type = A
|
||||
c.Fill(&e.AInfo)
|
||||
case 1:
|
||||
e.Type = B
|
||||
c.Fill(&e.BInfo)
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
var myObject MyInfo
|
||||
f.Fill(&myObject) // Type will correspond to whether A or B info is set.
|
||||
```
|
||||
|
||||
See more examples in ```example_test.go```.
|
||||
|
||||
## dvyukov/go-fuzz integration
|
||||
|
||||
You can use this library for easier [go-fuzz](https://github.com/dvyukov/go-fuzz)ing.
|
||||
go-fuzz provides the user a byte-slice, which should be converted to different inputs
|
||||
for the tested function. This library can help convert the byte slice. Consider for
|
||||
example a fuzz test for a the function `mypackage.MyFunc` that takes an int arguments:
|
||||
```go
|
||||
// +build gofuzz
|
||||
package mypackage
|
||||
|
||||
import "sigs.k8s.io/randfill"
|
||||
|
||||
func Fuzz(data []byte) int {
|
||||
var i int
|
||||
randfill.NewFromGoFuzz(data).Fill(&i)
|
||||
MyFunc(i)
|
||||
return 0
|
||||
}
|
||||
```
|
||||
|
||||
Happy testing!
|
16
vendor/sigs.k8s.io/randfill/SECURITY_CONTACTS
generated
vendored
Normal file
16
vendor/sigs.k8s.io/randfill/SECURITY_CONTACTS
generated
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
# Defined below are the security contacts for this repo.
|
||||
#
|
||||
# They are the contact point for the Product Security Committee to reach out
|
||||
# to for triaging and handling of incoming issues.
|
||||
#
|
||||
# The below names agree to abide by the
|
||||
# [Embargo Policy](https://git.k8s.io/security/private-distributors-list.md#embargo-policy)
|
||||
# and will be removed and replaced if they violate that agreement.
|
||||
#
|
||||
# DO NOT REPORT SECURITY VULNERABILITIES DIRECTLY TO THESE NAMES, FOLLOW THE
|
||||
# INSTRUCTIONS AT https://kubernetes.io/security/
|
||||
|
||||
thockin
|
||||
BenTheElder
|
||||
aojea
|
||||
pohly
|
81
vendor/sigs.k8s.io/randfill/bytesource/bytesource.go
generated
vendored
Normal file
81
vendor/sigs.k8s.io/randfill/bytesource/bytesource.go
generated
vendored
Normal file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Package bytesource provides a rand.Source64 that is determined by a slice of bytes.
|
||||
package bytesource
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
"math/rand"
|
||||
)
|
||||
|
||||
// ByteSource implements rand.Source64 determined by a slice of bytes. The random numbers are
|
||||
// generated from each 8 bytes in the slice, until the last bytes are consumed, from which a
|
||||
// fallback pseudo random source is created in case more random numbers are required.
|
||||
// It also exposes a `bytes.Reader` API, which lets callers consume the bytes directly.
|
||||
type ByteSource struct {
|
||||
*bytes.Reader
|
||||
fallback rand.Source
|
||||
}
|
||||
|
||||
// New returns a new ByteSource from a given slice of bytes.
|
||||
func New(input []byte) *ByteSource {
|
||||
s := &ByteSource{
|
||||
Reader: bytes.NewReader(input),
|
||||
fallback: rand.NewSource(0),
|
||||
}
|
||||
if len(input) > 0 {
|
||||
s.fallback = rand.NewSource(int64(s.consumeUint64()))
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *ByteSource) Uint64() uint64 {
|
||||
// Return from input if it was not exhausted.
|
||||
if s.Len() > 0 {
|
||||
return s.consumeUint64()
|
||||
}
|
||||
|
||||
// Input was exhausted, return random number from fallback (in this case fallback should not be
|
||||
// nil). Try first having a Uint64 output (Should work in current rand implementation),
|
||||
// otherwise return a conversion of Int63.
|
||||
if s64, ok := s.fallback.(rand.Source64); ok {
|
||||
return s64.Uint64()
|
||||
}
|
||||
return uint64(s.fallback.Int63())
|
||||
}
|
||||
|
||||
func (s *ByteSource) Int63() int64 {
|
||||
return int64(s.Uint64() >> 1)
|
||||
}
|
||||
|
||||
func (s *ByteSource) Seed(seed int64) {
|
||||
s.fallback = rand.NewSource(seed)
|
||||
s.Reader = bytes.NewReader(nil)
|
||||
}
|
||||
|
||||
// consumeUint64 reads 8 bytes from the input and convert them to a uint64. It assumes that the the
|
||||
// bytes reader is not empty.
|
||||
func (s *ByteSource) consumeUint64() uint64 {
|
||||
var bytes [8]byte
|
||||
_, err := s.Read(bytes[:])
|
||||
if err != nil && err != io.EOF {
|
||||
panic("failed reading source") // Should not happen.
|
||||
}
|
||||
return binary.BigEndian.Uint64(bytes[:])
|
||||
}
|
3
vendor/sigs.k8s.io/randfill/code-of-conduct.md
generated
vendored
Normal file
3
vendor/sigs.k8s.io/randfill/code-of-conduct.md
generated
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
# Kubernetes Community Code of Conduct
|
||||
|
||||
Please refer to our [Kubernetes Community Code of Conduct](https://git.k8s.io/community/code-of-conduct.md)
|
682
vendor/sigs.k8s.io/randfill/randfill.go
generated
vendored
Normal file
682
vendor/sigs.k8s.io/randfill/randfill.go
generated
vendored
Normal file
@ -0,0 +1,682 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
Copyright 2014 The gofuzz Authors.
|
||||
Copyright 2025 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
// Package randfill is a library for populating go objects with random values.
|
||||
package randfill
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/randfill/bytesource"
|
||||
)
|
||||
|
||||
// funcMap is a map from a type to a function that randfills that type. The
|
||||
// function is a reflect.Value because the type being filled is different for
|
||||
// each func.
|
||||
type funcMap map[reflect.Type]reflect.Value
|
||||
|
||||
// Filler knows how to fill any object with random fields.
|
||||
type Filler struct {
|
||||
customFuncs funcMap
|
||||
defaultFuncs funcMap
|
||||
r *rand.Rand
|
||||
nilChance float64
|
||||
minElements int
|
||||
maxElements int
|
||||
maxDepth int
|
||||
allowUnexportedFields bool
|
||||
skipFieldPatterns []*regexp.Regexp
|
||||
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
// New returns a new Filler. Customize your Filler further by calling Funcs,
|
||||
// RandSource, NilChance, or NumElements in any order.
|
||||
func New() *Filler {
|
||||
return NewWithSeed(time.Now().UnixNano())
|
||||
}
|
||||
|
||||
func NewWithSeed(seed int64) *Filler {
|
||||
f := &Filler{
|
||||
defaultFuncs: funcMap{
|
||||
reflect.TypeOf(&time.Time{}): reflect.ValueOf(randfillTime),
|
||||
},
|
||||
|
||||
customFuncs: funcMap{},
|
||||
r: rand.New(rand.NewSource(seed)),
|
||||
nilChance: .2,
|
||||
minElements: 1,
|
||||
maxElements: 10,
|
||||
maxDepth: 100,
|
||||
allowUnexportedFields: false,
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// NewFromGoFuzz is a helper function that enables using randfill (this
|
||||
// project) with go-fuzz (https://github.com/dvyukov/go-fuzz) for continuous
|
||||
// fuzzing. Essentially, it enables translating the fuzzing bytes from
|
||||
// go-fuzz to any Go object using this library.
|
||||
//
|
||||
// This implementation promises a constant translation from a given slice of
|
||||
// bytes to the fuzzed objects. This promise will remain over future
|
||||
// versions of Go and of this library.
|
||||
//
|
||||
// Note: the returned Filler should not be shared between multiple goroutines,
|
||||
// as its deterministic output will no longer be available.
|
||||
//
|
||||
// Example: use go-fuzz to test the function `MyFunc(int)` in the package
|
||||
// `mypackage`. Add the file: "mypackage_fuzz.go" with the content:
|
||||
//
|
||||
// // +build gofuzz
|
||||
// package mypackage
|
||||
// import "sigs.k8s.io/randfill"
|
||||
//
|
||||
// func Fuzz(data []byte) int {
|
||||
// var i int
|
||||
// randfill.NewFromGoFuzz(data).Fill(&i)
|
||||
// MyFunc(i)
|
||||
// return 0
|
||||
// }
|
||||
func NewFromGoFuzz(data []byte) *Filler {
|
||||
return New().RandSource(bytesource.New(data))
|
||||
}
|
||||
|
||||
// Funcs registers custom fill functions for this Filler.
|
||||
//
|
||||
// Each entry in customFuncs must be a function taking two parameters.
|
||||
// The first parameter must be a pointer or map. It is the variable that
|
||||
// function will fill with random data. The second parameter must be a
|
||||
// randfill.Continue, which will provide a source of randomness and a way
|
||||
// to automatically continue filling smaller pieces of the first parameter.
|
||||
//
|
||||
// These functions are called sensibly, e.g., if you wanted custom string
|
||||
// filling, the function `func(s *string, c randfill.Continue)` would get
|
||||
// called and passed the address of strings. Maps and pointers will always
|
||||
// be made/new'd for you, ignoring the NilChance option. For slices, it
|
||||
// doesn't make much sense to pre-create them--Filler doesn't know how
|
||||
// long you want your slice--so take a pointer to a slice, and make it
|
||||
// yourself. (If you don't want your map/pointer type pre-made, take a
|
||||
// pointer to it, and make it yourself.) See the examples for a range of
|
||||
// custom functions.
|
||||
//
|
||||
// If a function is already registered for a type, and a new function is
|
||||
// provided, the previous function will be replaced with the new one.
|
||||
func (f *Filler) Funcs(customFuncs ...interface{}) *Filler {
|
||||
for i := range customFuncs {
|
||||
v := reflect.ValueOf(customFuncs[i])
|
||||
if v.Kind() != reflect.Func {
|
||||
panic("Filler.Funcs: all arguments must be functions")
|
||||
}
|
||||
t := v.Type()
|
||||
if t.NumIn() != 2 || t.NumOut() != 0 {
|
||||
panic("Filler.Funcs: all customFuncs must have 2 arguments and 0 returns")
|
||||
}
|
||||
argT := t.In(0)
|
||||
switch argT.Kind() {
|
||||
case reflect.Ptr, reflect.Map:
|
||||
default:
|
||||
panic("Filler.Funcs: customFuncs' first argument must be a pointer or map type")
|
||||
}
|
||||
if t.In(1) != reflect.TypeOf(Continue{}) {
|
||||
panic("Filler.Funcs: customFuncs' second argument must be a randfill.Continue")
|
||||
}
|
||||
f.customFuncs[argT] = v
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
// RandSource causes this Filler to get values from the given source of
|
||||
// randomness. Use this if you want deterministic filling.
|
||||
func (f *Filler) RandSource(s rand.Source) *Filler {
|
||||
f.r = rand.New(s)
|
||||
return f
|
||||
}
|
||||
|
||||
// NilChance sets the probability of creating a nil pointer, map, or slice to
|
||||
// 'p'. 'p' should be between 0 (no nils) and 1 (all nils), inclusive.
|
||||
func (f *Filler) NilChance(p float64) *Filler {
|
||||
if p < 0 || p > 1 {
|
||||
panic("Filler.NilChance: p must be between 0 and 1, inclusive")
|
||||
}
|
||||
f.nilChance = p
|
||||
return f
|
||||
}
|
||||
|
||||
// NumElements sets the minimum and maximum number of elements that will be
|
||||
// added to a non-nil map or slice.
|
||||
func (f *Filler) NumElements(min, max int) *Filler {
|
||||
if min < 0 {
|
||||
panic("Filler.NumElements: min must be >= 0")
|
||||
}
|
||||
if min > max {
|
||||
panic("Filler.NumElements: min must be <= max")
|
||||
}
|
||||
f.minElements = min
|
||||
f.maxElements = max
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *Filler) genElementCount() int {
|
||||
if f.minElements == f.maxElements {
|
||||
return f.minElements
|
||||
}
|
||||
return f.minElements + f.r.Intn(f.maxElements-f.minElements+1)
|
||||
}
|
||||
|
||||
func (f *Filler) genShouldFill() bool {
|
||||
return f.r.Float64() >= f.nilChance
|
||||
}
|
||||
|
||||
// MaxDepth sets the maximum number of recursive fill calls that will be made
|
||||
// before stopping. This includes struct members, pointers, and map and slice
|
||||
// elements.
|
||||
func (f *Filler) MaxDepth(d int) *Filler {
|
||||
f.maxDepth = d
|
||||
return f
|
||||
}
|
||||
|
||||
// AllowUnexportedFields defines whether to fill unexported fields.
|
||||
func (f *Filler) AllowUnexportedFields(flag bool) *Filler {
|
||||
f.allowUnexportedFields = flag
|
||||
return f
|
||||
}
|
||||
|
||||
// SkipFieldsWithPattern tells this Filler to skip any field whose name matches
|
||||
// the supplied pattern. Call this multiple times if needed. This is useful to
|
||||
// skip XXX_ fields generated by protobuf.
|
||||
func (f *Filler) SkipFieldsWithPattern(pattern *regexp.Regexp) *Filler {
|
||||
f.skipFieldPatterns = append(f.skipFieldPatterns, pattern)
|
||||
return f
|
||||
}
|
||||
|
||||
// SimpleSelfFiller represents an object that knows how to randfill itself.
|
||||
//
|
||||
// Unlike NativeSelfFiller, this interface does not cause the type in question
|
||||
// to depend on the randfill package. This is most useful for simple types. For
|
||||
// more complex types, consider using NativeSelfFiller.
|
||||
type SimpleSelfFiller interface {
|
||||
// RandFill fills the current object with random data.
|
||||
RandFill(r *rand.Rand)
|
||||
}
|
||||
|
||||
// NativeSelfFiller represents an object that knows how to randfill itself.
|
||||
//
|
||||
// Unlike SimpleSelfFiller, this interface allows for recursive filling of
|
||||
// child objects with the same rules as the parent Filler.
|
||||
type NativeSelfFiller interface {
|
||||
// RandFill fills the current object with random data.
|
||||
RandFill(c Continue)
|
||||
}
|
||||
|
||||
// Fill recursively fills all of obj's fields with something random. First
|
||||
// this tries to find a custom fill function (see Funcs). If there is no
|
||||
// custom function, this tests whether the object implements SimpleSelfFiller
|
||||
// or NativeSelfFiller and if so, calls RandFill on it to fill itself. If that
|
||||
// fails, this will see if there is a default fill function provided by this
|
||||
// package. If all of that fails, this will generate random values for all
|
||||
// primitive fields and then recurse for all non-primitives.
|
||||
//
|
||||
// This is safe for cyclic or tree-like structs, up to a limit. Use the
|
||||
// MaxDepth method to adjust how deep you need it to recurse.
|
||||
//
|
||||
// obj must be a pointer. Exported (public) fields can always be set, and if
|
||||
// the AllowUnexportedFields() modifier was called it can try to set unexported
|
||||
// (private) fields, too.
|
||||
//
|
||||
// This is intended for tests, so will panic on bad input or unimplemented
|
||||
// types. This method takes a lock for the whole Filler, so it is not
|
||||
// reentrant. See Continue.
|
||||
func (f *Filler) Fill(obj interface{}) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
v := reflect.ValueOf(obj)
|
||||
if v.Kind() != reflect.Ptr {
|
||||
panic("Filler.Fill: obj must be a pointer")
|
||||
}
|
||||
v = v.Elem()
|
||||
f.fillWithContext(v, 0)
|
||||
}
|
||||
|
||||
// FillNoCustom is just like Fill, except that any custom fill function for
|
||||
// obj's type will not be called and obj will not be tested for
|
||||
// SimpleSelfFiller or NativeSelfFiller. This applies only to obj and not other
|
||||
// instances of obj's type or to obj's child fields.
|
||||
//
|
||||
// obj must be a pointer. Exported (public) fields can always be set, and if
|
||||
// the AllowUnexportedFields() modifier was called it can try to set unexported
|
||||
// (private) fields, too.
|
||||
//
|
||||
// This is intended for tests, so will panic on bad input or unimplemented
|
||||
// types. This method takes a lock for the whole Filler, so it is not
|
||||
// reentrant. See Continue.
|
||||
func (f *Filler) FillNoCustom(obj interface{}) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
|
||||
v := reflect.ValueOf(obj)
|
||||
if v.Kind() != reflect.Ptr {
|
||||
panic("Filler.FillNoCustom: obj must be a pointer")
|
||||
}
|
||||
v = v.Elem()
|
||||
f.fillWithContext(v, flagNoCustomFill)
|
||||
}
|
||||
|
||||
const (
|
||||
// Do not try to find a custom fill function. Does not apply recursively.
|
||||
flagNoCustomFill uint64 = 1 << iota
|
||||
)
|
||||
|
||||
func (f *Filler) fillWithContext(v reflect.Value, flags uint64) {
|
||||
fc := &fillerContext{filler: f}
|
||||
fc.doFill(v, flags)
|
||||
}
|
||||
|
||||
// fillerContext carries context about a single filling run, which lets Filler
|
||||
// be thread-safe.
|
||||
type fillerContext struct {
|
||||
filler *Filler
|
||||
curDepth int
|
||||
}
|
||||
|
||||
func (fc *fillerContext) doFill(v reflect.Value, flags uint64) {
|
||||
if fc.curDepth >= fc.filler.maxDepth {
|
||||
return
|
||||
}
|
||||
fc.curDepth++
|
||||
defer func() { fc.curDepth-- }()
|
||||
|
||||
if !v.CanSet() {
|
||||
if !fc.filler.allowUnexportedFields || !v.CanAddr() {
|
||||
return
|
||||
}
|
||||
v = reflect.NewAt(v.Type(), unsafe.Pointer(v.UnsafeAddr())).Elem()
|
||||
}
|
||||
|
||||
if flags&flagNoCustomFill == 0 {
|
||||
// Check for both pointer and non-pointer custom functions.
|
||||
if v.CanAddr() && fc.tryCustom(v.Addr()) {
|
||||
return
|
||||
}
|
||||
if fc.tryCustom(v) {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if fn, ok := fillFuncMap[v.Kind()]; ok {
|
||||
fn(v, fc.filler.r)
|
||||
return
|
||||
}
|
||||
|
||||
switch v.Kind() {
|
||||
case reflect.Map:
|
||||
if fc.filler.genShouldFill() {
|
||||
v.Set(reflect.MakeMap(v.Type()))
|
||||
n := fc.filler.genElementCount()
|
||||
for i := 0; i < n; i++ {
|
||||
key := reflect.New(v.Type().Key()).Elem()
|
||||
fc.doFill(key, 0)
|
||||
val := reflect.New(v.Type().Elem()).Elem()
|
||||
fc.doFill(val, 0)
|
||||
v.SetMapIndex(key, val)
|
||||
}
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Ptr:
|
||||
if fc.filler.genShouldFill() {
|
||||
v.Set(reflect.New(v.Type().Elem()))
|
||||
fc.doFill(v.Elem(), 0)
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Slice:
|
||||
if fc.filler.genShouldFill() {
|
||||
n := fc.filler.genElementCount()
|
||||
v.Set(reflect.MakeSlice(v.Type(), n, n))
|
||||
for i := 0; i < n; i++ {
|
||||
fc.doFill(v.Index(i), 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Array:
|
||||
if fc.filler.genShouldFill() {
|
||||
n := v.Len()
|
||||
for i := 0; i < n; i++ {
|
||||
fc.doFill(v.Index(i), 0)
|
||||
}
|
||||
return
|
||||
}
|
||||
v.Set(reflect.Zero(v.Type()))
|
||||
case reflect.Struct:
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
skipField := false
|
||||
fieldName := v.Type().Field(i).Name
|
||||
for _, pattern := range fc.filler.skipFieldPatterns {
|
||||
if pattern.MatchString(fieldName) {
|
||||
skipField = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !skipField {
|
||||
fc.doFill(v.Field(i), 0)
|
||||
}
|
||||
}
|
||||
case reflect.Chan:
|
||||
fallthrough
|
||||
case reflect.Func:
|
||||
fallthrough
|
||||
case reflect.Interface:
|
||||
fallthrough
|
||||
default:
|
||||
panic(fmt.Sprintf("can't fill type %v, kind %v", v.Type(), v.Kind()))
|
||||
}
|
||||
}
|
||||
|
||||
// tryCustom searches for custom handlers, and returns true iff it finds a match
|
||||
// and successfully randomizes v.
|
||||
func (fc *fillerContext) tryCustom(v reflect.Value) bool {
|
||||
// First: see if we have a fill function for it.
|
||||
doCustom, ok := fc.filler.customFuncs[v.Type()]
|
||||
if !ok {
|
||||
// Second: see if it can fill itself.
|
||||
if v.CanInterface() {
|
||||
intf := v.Interface()
|
||||
if fillable, ok := intf.(SimpleSelfFiller); ok {
|
||||
fillable.RandFill(fc.filler.r)
|
||||
return true
|
||||
}
|
||||
if fillable, ok := intf.(NativeSelfFiller); ok {
|
||||
fillable.RandFill(Continue{fc: fc, Rand: fc.filler.r})
|
||||
return true
|
||||
}
|
||||
}
|
||||
// Finally: see if there is a default fill function.
|
||||
doCustom, ok = fc.filler.defaultFuncs[v.Type()]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
switch v.Kind() {
|
||||
case reflect.Ptr:
|
||||
if v.IsNil() {
|
||||
if !v.CanSet() {
|
||||
return false
|
||||
}
|
||||
v.Set(reflect.New(v.Type().Elem()))
|
||||
}
|
||||
case reflect.Map:
|
||||
if v.IsNil() {
|
||||
if !v.CanSet() {
|
||||
return false
|
||||
}
|
||||
v.Set(reflect.MakeMap(v.Type()))
|
||||
}
|
||||
default:
|
||||
return false
|
||||
}
|
||||
|
||||
doCustom.Call([]reflect.Value{
|
||||
v,
|
||||
reflect.ValueOf(Continue{
|
||||
fc: fc,
|
||||
Rand: fc.filler.r,
|
||||
}),
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
// Continue can be passed to custom fill functions to allow them to use
|
||||
// the correct source of randomness and to continue filling their members.
|
||||
type Continue struct {
|
||||
fc *fillerContext
|
||||
|
||||
// For convenience, Continue implements rand.Rand via embedding.
|
||||
// Use this for generating any randomness if you want your filling
|
||||
// to be repeatable for a given seed.
|
||||
*rand.Rand
|
||||
}
|
||||
|
||||
// Fill continues filling obj. obj must be a pointer or a reflect.Value of a
|
||||
// pointer. See Filler.Fill.
|
||||
func (c Continue) Fill(obj interface{}) {
|
||||
v, ok := obj.(reflect.Value)
|
||||
if !ok {
|
||||
v = reflect.ValueOf(obj)
|
||||
}
|
||||
if v.Kind() != reflect.Ptr {
|
||||
panic("Continue.Fill: obj must be a pointer")
|
||||
}
|
||||
v = v.Elem()
|
||||
c.fc.doFill(v, 0)
|
||||
}
|
||||
|
||||
// FillNoCustom continues filling obj, except that any custom fill function for
|
||||
// obj's type will not be called and obj will not be tested for
|
||||
// SimpleSelfFiller or NativeSelfFiller. See Filler.FillNoCustom.
|
||||
func (c Continue) FillNoCustom(obj interface{}) {
|
||||
v, ok := obj.(reflect.Value)
|
||||
if !ok {
|
||||
v = reflect.ValueOf(obj)
|
||||
}
|
||||
if v.Kind() != reflect.Ptr {
|
||||
panic("Continue.FillNoCustom: obj must be a pointer")
|
||||
}
|
||||
v = v.Elem()
|
||||
c.fc.doFill(v, flagNoCustomFill)
|
||||
}
|
||||
|
||||
const defaultStringMaxLen = 20
|
||||
|
||||
// String makes a random string up to n characters long. If n is 0, the default
|
||||
// size range is [0-20). The returned string may include a variety of (valid)
|
||||
// UTF-8 encodings.
|
||||
func (c Continue) String(n int) string {
|
||||
return randString(c.Rand, n)
|
||||
}
|
||||
|
||||
// Uint64 makes random 64 bit numbers.
|
||||
// Weirdly, rand doesn't have a function that gives you 64 random bits.
|
||||
func (c Continue) Uint64() uint64 {
|
||||
return randUint64(c.Rand)
|
||||
}
|
||||
|
||||
// Bool returns true or false randomly.
|
||||
func (c Continue) Bool() bool {
|
||||
return randBool(c.Rand)
|
||||
}
|
||||
|
||||
func fillInt(v reflect.Value, r *rand.Rand) {
|
||||
v.SetInt(int64(randUint64(r)))
|
||||
}
|
||||
|
||||
func fillUint(v reflect.Value, r *rand.Rand) {
|
||||
v.SetUint(randUint64(r))
|
||||
}
|
||||
|
||||
func randfillTime(t *time.Time, c Continue) {
|
||||
var sec, nsec int64
|
||||
// Allow for about 1000 years of random time values, which keeps things
|
||||
// like JSON parsing reasonably happy.
|
||||
sec = c.Rand.Int63n(1000 * 365 * 24 * 60 * 60)
|
||||
// Nanosecond values greater than 1Bn are technically allowed but result in
|
||||
// time.Time values with invalid timezone offsets.
|
||||
nsec = c.Rand.Int63n(999999999)
|
||||
*t = time.Unix(sec, nsec)
|
||||
}
|
||||
|
||||
var fillFuncMap = map[reflect.Kind]func(reflect.Value, *rand.Rand){
|
||||
reflect.Bool: func(v reflect.Value, r *rand.Rand) {
|
||||
v.SetBool(randBool(r))
|
||||
},
|
||||
reflect.Int: fillInt,
|
||||
reflect.Int8: fillInt,
|
||||
reflect.Int16: fillInt,
|
||||
reflect.Int32: fillInt,
|
||||
reflect.Int64: fillInt,
|
||||
reflect.Uint: fillUint,
|
||||
reflect.Uint8: fillUint,
|
||||
reflect.Uint16: fillUint,
|
||||
reflect.Uint32: fillUint,
|
||||
reflect.Uint64: fillUint,
|
||||
reflect.Uintptr: fillUint,
|
||||
reflect.Float32: func(v reflect.Value, r *rand.Rand) {
|
||||
v.SetFloat(float64(r.Float32()))
|
||||
},
|
||||
reflect.Float64: func(v reflect.Value, r *rand.Rand) {
|
||||
v.SetFloat(r.Float64())
|
||||
},
|
||||
reflect.Complex64: func(v reflect.Value, r *rand.Rand) {
|
||||
v.SetComplex(complex128(complex(r.Float32(), r.Float32())))
|
||||
},
|
||||
reflect.Complex128: func(v reflect.Value, r *rand.Rand) {
|
||||
v.SetComplex(complex(r.Float64(), r.Float64()))
|
||||
},
|
||||
reflect.String: func(v reflect.Value, r *rand.Rand) {
|
||||
v.SetString(randString(r, 0))
|
||||
},
|
||||
reflect.UnsafePointer: func(v reflect.Value, r *rand.Rand) {
|
||||
panic("filling of UnsafePointers is not implemented")
|
||||
},
|
||||
}
|
||||
|
||||
// randBool returns true or false randomly.
|
||||
func randBool(r *rand.Rand) bool {
|
||||
return r.Int31()&(1<<30) == 0
|
||||
}
|
||||
|
||||
type int63nPicker interface {
|
||||
Int63n(int64) int64
|
||||
}
|
||||
|
||||
// UnicodeRange describes a sequential range of unicode characters.
|
||||
// Last must be numerically greater than First.
|
||||
type UnicodeRange struct {
|
||||
First, Last rune
|
||||
}
|
||||
|
||||
// UnicodeRanges describes an arbitrary number of sequential ranges of unicode characters.
|
||||
// To be useful, each range must have at least one character (First <= Last) and
|
||||
// there must be at least one range.
|
||||
type UnicodeRanges []UnicodeRange
|
||||
|
||||
// choose returns a random unicode character from the given range, using the
|
||||
// given randomness source.
|
||||
func (ur UnicodeRange) choose(r int63nPicker) rune {
|
||||
count := int64(ur.Last - ur.First + 1)
|
||||
return ur.First + rune(r.Int63n(count))
|
||||
}
|
||||
|
||||
// CustomStringFillFunc constructs a FillFunc which produces random strings.
|
||||
// Each character is selected from the range ur. If there are no characters
|
||||
// in the range (cr.Last < cr.First), this will panic.
|
||||
func (ur UnicodeRange) CustomStringFillFunc(n int) func(s *string, c Continue) {
|
||||
ur.check()
|
||||
return func(s *string, c Continue) {
|
||||
*s = ur.randString(c.Rand, n)
|
||||
}
|
||||
}
|
||||
|
||||
// check is a function that used to check whether the first of ur(UnicodeRange)
|
||||
// is greater than the last one.
|
||||
func (ur UnicodeRange) check() {
|
||||
if ur.Last < ur.First {
|
||||
panic("UnicodeRange.check: the last encoding must be greater than the first")
|
||||
}
|
||||
}
|
||||
|
||||
// randString of UnicodeRange makes a random string up to 20 characters long.
|
||||
// Each character is selected form ur(UnicodeRange).
|
||||
func (ur UnicodeRange) randString(r *rand.Rand, max int) string {
|
||||
if max == 0 {
|
||||
max = defaultStringMaxLen
|
||||
}
|
||||
n := r.Intn(max)
|
||||
sb := strings.Builder{}
|
||||
sb.Grow(n)
|
||||
for i := 0; i < n; i++ {
|
||||
sb.WriteRune(ur.choose(r))
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// defaultUnicodeRanges sets a default unicode range when users do not set
|
||||
// CustomStringFillFunc() but want to fill strings.
|
||||
var defaultUnicodeRanges = UnicodeRanges{
|
||||
{' ', '~'}, // ASCII characters
|
||||
{'\u00a0', '\u02af'}, // Multi-byte encoded characters
|
||||
{'\u4e00', '\u9fff'}, // Common CJK (even longer encodings)
|
||||
}
|
||||
|
||||
// CustomStringFillFunc constructs a FillFunc which produces random strings.
|
||||
// Each character is selected from one of the ranges of ur(UnicodeRanges).
|
||||
// Each range has an equal probability of being chosen. If there are no ranges,
|
||||
// or a selected range has no characters (.Last < .First), this will panic.
|
||||
// Do not modify any of the ranges in ur after calling this function.
|
||||
func (ur UnicodeRanges) CustomStringFillFunc(n int) func(s *string, c Continue) {
|
||||
// Check unicode ranges slice is empty.
|
||||
if len(ur) == 0 {
|
||||
panic("UnicodeRanges is empty")
|
||||
}
|
||||
// if not empty, each range should be checked.
|
||||
for i := range ur {
|
||||
ur[i].check()
|
||||
}
|
||||
return func(s *string, c Continue) {
|
||||
*s = ur.randString(c.Rand, n)
|
||||
}
|
||||
}
|
||||
|
||||
// randString of UnicodeRanges makes a random string up to 20 characters long.
|
||||
// Each character is selected form one of the ranges of ur(UnicodeRanges),
|
||||
// and each range has an equal probability of being chosen.
|
||||
func (ur UnicodeRanges) randString(r *rand.Rand, max int) string {
|
||||
if max == 0 {
|
||||
max = defaultStringMaxLen
|
||||
}
|
||||
n := r.Intn(max)
|
||||
sb := strings.Builder{}
|
||||
sb.Grow(n)
|
||||
for i := 0; i < n; i++ {
|
||||
sb.WriteRune(ur[r.Intn(len(ur))].choose(r))
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// randString makes a random string up to 20 characters long. The returned string
|
||||
// may include a variety of (valid) UTF-8 encodings.
|
||||
func randString(r *rand.Rand, max int) string {
|
||||
return defaultUnicodeRanges.randString(r, max)
|
||||
}
|
||||
|
||||
// randUint64 makes random 64 bit numbers.
|
||||
// Weirdly, rand doesn't have a function that gives you 64 random bits.
|
||||
func randUint64(r *rand.Rand) uint64 {
|
||||
return uint64(r.Uint32())<<32 | uint64(r.Uint32())
|
||||
}
|
Reference in New Issue
Block a user