本文整理汇总了Golang中bytes.Repeat函数的典型用法代码示例。如果您正苦于以下问题:Golang Repeat函数的具体用法?Golang Repeat怎么用?Golang Repeat使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Repeat函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestChaChaCorrectness
func TestChaChaCorrectness(t *testing.T) {
ec, dc := initChaCha(20)
sample = bytes.Repeat(origin, 100)
origin = bytes.Repeat(origin, 100)
randChip("dec", sample, func(chip []byte) {
ec.XORKeyStream(chip, chip)
})
dc.XORKeyStream(sample, sample)
if !bytes.Equal(origin, sample) {
dumpHex(sample, "result")
dumpHex(origin, "origin")
t.Fatalf("Incorrect result")
}
ec.XORKeyStream(sample, sample)
randChip("dec", sample, func(chip []byte) {
dc.XORKeyStream(chip, chip)
})
if !bytes.Equal(origin, sample) {
dumpHex(sample, "result")
dumpHex(origin, "origin")
t.Fatalf("Incorrect result")
}
}
示例2: TestWriteAtom
func TestWriteAtom(t *testing.T) {
c := new(Context)
test := func(in Atom, shouldFail bool) {
w := new(bytes.Buffer)
if err := c.writeAtom(w, in); err != nil {
if !shouldFail {
t.Error(in, err)
}
} else if shouldFail {
t.Error("err == nil (%v)", in)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if v != in {
t.Errorf("expected %v, got %v", in, v)
}
}
test(Atom(""), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint8)), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint8+1)), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint16)), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint16+1)), true)
}
示例3: TestUnencryptedSuffix
func TestUnencryptedSuffix(t *testing.T) {
branch := TreeBranch{
TreeItem{
Key: "foo_unencrypted",
Value: "bar",
},
}
tree := Tree{Branch: branch, Metadata: Metadata{UnencryptedSuffix: "_unencrypted"}}
expected := TreeBranch{
TreeItem{
Key: "foo_unencrypted",
Value: "bar",
},
}
cipher := aes.Cipher{}
_, err := tree.Encrypt(bytes.Repeat([]byte("f"), 32), cipher)
if err != nil {
t.Errorf("Encrypting the tree failed: %s", err)
}
if !reflect.DeepEqual(tree.Branch, expected) {
t.Errorf("Trees don't match: \ngot \t\t%+v,\n expected \t\t%+v", tree.Branch, expected)
}
_, err = tree.Decrypt(bytes.Repeat([]byte("f"), 32), cipher)
if err != nil {
t.Errorf("Decrypting the tree failed: %s", err)
}
if !reflect.DeepEqual(tree.Branch, expected) {
t.Errorf("Trees don't match: \ngot\t\t\t%+v,\nexpected\t\t%+v", tree.Branch, expected)
}
}
示例4: BenchmarkDeserializeTxLarge
// BenchmarkDeserializeTxLarge performs a benchmark on how long it takes to
// deserialize a very large transaction.
func BenchmarkDeserializeTxLarge(b *testing.B) {
bigTx := new(MsgTx)
bigTx.Version = DefaultMsgTxVersion()
inputsLen := 1000
outputsLen := 2000
bigTx.TxIn = make([]*TxIn, inputsLen)
bigTx.TxOut = make([]*TxOut, outputsLen)
for i := 0; i < inputsLen; i++ {
bigTx.TxIn[i] = &TxIn{
SignatureScript: bytes.Repeat([]byte{0x12}, 120),
}
}
for i := 0; i < outputsLen; i++ {
bigTx.TxOut[i] = &TxOut{
PkScript: bytes.Repeat([]byte{0x34}, 30),
}
}
bigTxB, err := bigTx.Bytes()
if err != nil {
b.Fatalf("%v", err.Error())
}
r := bytes.NewReader(bigTxB)
var tx MsgTx
for i := 0; i < b.N; i++ {
r.Seek(0, 0)
tx.Deserialize(r)
}
}
示例5: TestGetApproximateOffsetPlain
func TestGetApproximateOffsetPlain(t *testing.T) {
w := new(writer)
o := &opt.Options{
BlockSize: 1024,
Compression: opt.NoCompression,
}
tw := NewWriter(w, o)
tw.Append([]byte("k01"), []byte("hello"))
tw.Append([]byte("k02"), []byte("hello2"))
tw.Append([]byte("k03"), bytes.Repeat([]byte{'x'}, 10000))
tw.Append([]byte("k04"), bytes.Repeat([]byte{'x'}, 200000))
tw.Append([]byte("k05"), bytes.Repeat([]byte{'x'}, 300000))
tw.Append([]byte("k06"), []byte("hello3"))
tw.Append([]byte("k07"), bytes.Repeat([]byte{'x'}, 100000))
if err := tw.Close(); err != nil {
t.Fatal("error when finalizing table:", err.Error())
}
size := w.Len()
r := &reader{*bytes.NewReader(w.Bytes())}
tr := NewReader(r, int64(size), nil, o)
// if err != nil {
// t.Fatal("error when creating table reader instance:", err.Error())
// }
offsetBetween(t, tr, []byte("k0"), 0, 0)
offsetBetween(t, tr, []byte("k01a"), 0, 0)
offsetBetween(t, tr, []byte("k02"), 0, 0)
offsetBetween(t, tr, []byte("k03"), 0, 0)
offsetBetween(t, tr, []byte("k04"), 10000, 11000)
offsetBetween(t, tr, []byte("k04a"), 210000, 211000)
offsetBetween(t, tr, []byte("k05"), 210000, 211000)
offsetBetween(t, tr, []byte("k06"), 510000, 511000)
offsetBetween(t, tr, []byte("k07"), 510000, 511000)
offsetBetween(t, tr, []byte("xyz"), 610000, 612000)
}
示例6: TestNodeIDBase10
func TestNodeIDBase10(t *testing.T) {
tests := [...]struct {
bytes []byte
base10 *big.Int
}{
{
make([]byte, 16),
big.NewInt(0),
},
{
append(make([]byte, 15), 1),
big.NewInt(1),
},
{
bytes.Repeat([]byte{0xff}, 16),
new(big.Int).SetBytes(bytes.Repeat([]byte{0xff}, 16)),
},
}
for i, test := range tests {
id, err := NodeIDFromBytes(test.bytes)
if err != nil {
t.Errorf("test %v: unexpected error %v", i, err)
}
base10 := id.Base10()
if base10.Cmp(test.base10) != 0 {
t.Errorf("test %v: expected %v, got %v", i, test.base10, base10)
}
}
}
示例7: NewForwardingMessage
// NewForwardingMessage generates the a mix header containing the neccessary
// onion routing information required to propagate the message through the
// mixnet, eventually reaching the final node specified by 'identifier'. The
// onion encrypted message payload is then to be delivered to the specified 'dest'
// address.
func NewForwardingMessage(route []*btcec.PublicKey, dest LightningAddress,
message []byte) (*ForwardingMessage, error) {
routeLength := len(route)
// Compute the mix header, and shared secerts for each hop. We pass in
// the null destination and zero identifier in order for the final node
// in the route to be able to distinguish the payload as addressed to
// itself.
mixHeader, secrets, err := NewMixHeader([]byte{nullDest}, zeroNode, route)
if err != nil {
return nil, err
}
// Now for the body of the message. The next-node ID is set to all
// zeroes in order to notify the final hop that the message is meant for
// them. m = 0^k || dest || msg || padding.
var body [messageSize]byte
n := copy(body[:], bytes.Repeat([]byte{0}, securityParameter))
// TODO(roasbeef): destination vs identifier (node id) format.
n += copy(body[n:], []byte(dest))
n += copy(body[n:], message)
// TODO(roasbeef): make pad and unpad functions.
n += copy(body[n:], []byte{0x7f})
n += copy(body[n:], bytes.Repeat([]byte{0xff}, messageSize-len(body)))
// Now we construct the onion. Walking backwards from the last hop, we
// encrypt the message with the shared secret for each hop in the path.
onion := lionessEncode(generateKey("pi", secrets[routeLength-1]), body)
for i := routeLength - 2; i >= 0; i-- {
onion = lionessEncode(generateKey("pi", secrets[i]), onion)
}
return &ForwardingMessage{Header: mixHeader, Msg: onion}, nil
}
示例8: BenchmarkPathPacketConstruction
func BenchmarkPathPacketConstruction(b *testing.B) {
route := make([]*btcec.PublicKey, NumMaxHops)
for i := 0; i < NumMaxHops; i++ {
privKey, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
b.Fatalf("unable to generate key: %v", privKey)
}
route[i] = privKey.PubKey()
}
var (
err error
sphinxPacket *OnionPacket
)
var hopPayloads [][]byte
for i := 0; i < len(route); i++ {
payload := bytes.Repeat([]byte{byte('A' + i)}, HopPayloadSize)
hopPayloads = append(hopPayloads, payload)
}
d, _ := btcec.PrivKeyFromBytes(btcec.S256(), bytes.Repeat([]byte{'A'}, 32))
for i := 0; i < b.N; i++ {
sphinxPacket, err = NewOnionPacket(route, d, hopPayloads, nil)
if err != nil {
b.Fatalf("unable to create packet: %v", err)
}
}
s = sphinxPacket
}
示例9: TestAdvertiseEIR
func TestAdvertiseEIR(t *testing.T) {
cases := []struct {
adv []byte
scan []byte
want string
wanterr bool
}{
{adv: []byte{0x12, 0x34}, scan: []byte{0xAB, 0xCD}, want: "1234 abcd\n"},
{scan: []byte{0xAB, 0xCD}, want: " abcd\n"},
{adv: []byte{0x12, 0x34}, want: "1234 \n"},
// data too long
{adv: bytes.Repeat([]byte{0}, 32), wanterr: true},
{scan: bytes.Repeat([]byte{0}, 32), wanterr: true},
}
shim := new(testshim)
hci := newHCI(shim)
for _, tt := range cases {
shim.Buffer.Reset()
err := hci.advertiseEIR(tt.adv, tt.scan)
if tt.wanterr {
if err != ErrEIRPacketTooLong {
t.Errorf("AdvertiseEIR(%x, %x) got %v want ErrEIRPacketTooLong", tt.adv, tt.scan, err)
}
continue
}
if err != nil {
t.Errorf("AdvertiseEIR(%x, %x) unexpected error: %v", tt.adv, tt.scan, err)
}
if got := shim.Buffer.String(); got != tt.want {
t.Errorf("AdvertiseEIR(%x, %x): got %q want %q", tt.adv, tt.scan, got, tt.want)
}
}
}
示例10: TestGrow
func TestGrow(t *testing.T) {
x := []byte{'x'}
y := []byte{'y'}
tmp := make([]byte, 72)
for _, startLen := range []int{0, 100, 1000, 10000, 100000} {
xBytes := bytes.Repeat(x, startLen)
for _, growLen := range []int{0, 100, 1000, 10000, 100000} {
buf := NewBuffer(xBytes)
// If we read, this affects buf.off, which is good to test.
readBytes, _ := buf.Read(tmp)
buf.Grow(growLen)
yBytes := bytes.Repeat(y, growLen)
// Check no allocation occurs in write, as long as we're single-threaded.
var m1, m2 runtime.MemStats
runtime.ReadMemStats(&m1)
buf.Write(yBytes)
runtime.ReadMemStats(&m2)
if runtime.GOMAXPROCS(-1) == 1 && m1.Mallocs != m2.Mallocs {
t.Errorf("allocation occurred during write")
}
// Check that buffer has correct data.
if !bytes.Equal(buf.Bytes()[0:startLen-readBytes], xBytes[readBytes:]) {
t.Errorf("bad initial data at %d %d", startLen, growLen)
}
if !bytes.Equal(buf.Bytes()[startLen-readBytes:startLen-readBytes+growLen], yBytes) {
t.Errorf("bad written data at %d %d", startLen, growLen)
}
}
}
}
示例11: buildFloors
func buildFloors(b [][]byte, ln int) (ret [][]byte) {
ret = make([][]byte, len(b)*2+1)
for i := 0; i < len(b); i++ {
v := make([]byte, ln, ln)
copy(v, b[i])
v = bytes.Replace(v, []byte{0}, []byte{' '}, -1)
v = bytes.Replace(v, []byte{' '}, bytes.Repeat([]byte{' '}, 4), -1)
v = bytes.Replace(v, []byte{'*'}, []byte("+---"), -1)
v = bytes.Replace(v, []byte("- "), []byte("-+"), -1)
ret[i*2] = v
ret[i*2+1] = bytes.Repeat([]byte{' '}, ln*4)
}
ret[len(ret)-1] = make([]byte, len(ret[0]))
copy(ret[len(ret)-1], ret[len(ret)-3])
for i := len(ret) - 5; i >= 0; i -= 2 {
for j, v := range ret[i] {
if v == '-' && ret[i+2][j] == '-' {
ret[i+2][j] = ' '
}
}
ret[i+2] = bytes.Replace(ret[i+2], []byte(" + "), []byte(" "), -1)
ret[i+2] = bytes.Replace(ret[i+2], []byte("-+-"), []byte("---"), -1)
}
ret[len(ret)-1] = bytes.Replace(ret[len(ret)-1], []byte("-+-"), []byte("---"), -1)
ret[0] = bytes.Replace(ret[0], []byte("-+-"), []byte("---"), -1)
return ret
}
示例12: TestNumUnderlyingWrites
// TestNumUnderlyingWrites tests that each Writer flush only makes one or two
// Write calls on its underlying io.Writer, depending on whether or not the
// flushed buffer was compressible.
func TestNumUnderlyingWrites(t *testing.T) {
testCases := []struct {
input []byte
want int
}{
{bytes.Repeat([]byte{'x'}, 100), 1},
{bytes.Repeat([]byte{'y'}, 100), 1},
{[]byte("ABCDEFGHIJKLMNOPQRST"), 2},
}
var c writeCounter
w := NewBufferedWriter(&c)
defer w.Close()
for i, tc := range testCases {
c = 0
if _, err := w.Write(tc.input); err != nil {
t.Errorf("#%d: Write: %v", i, err)
continue
}
if err := w.Flush(); err != nil {
t.Errorf("#%d: Flush: %v", i, err)
continue
}
if int(c) != tc.want {
t.Errorf("#%d: got %d underlying writes, want %d", i, c, tc.want)
continue
}
}
}
示例13: testSize
func testSize(t *testing.T, size int) {
var b bytes.Buffer
text := string(bytes.Repeat([]byte("a"), size))
payload := bytes.Repeat([]byte{1}, size)
w := bufio.NewWriter(&b)
err := WritePacket(w, &Packet{
Text: text,
Seq: 1234,
Payload: payload,
})
assert.Nil(t, err)
// log.Println("size:", size)
// for i := 0; i < len(b.Bytes()) && i < 255; i++ {
// fmt.Printf("%02x ", b.Bytes()[i])
// }
// fmt.Println("")
var pkt Packet
r := bufio.NewReader(&b)
err = ReadPacket(r, &pkt)
assert.Nil(t, err)
assert.EqualValues(t, len([]byte(text)), pkt.LengthText)
assert.EqualValues(t, len(payload), pkt.LengthPayload)
assert.EqualValues(t, 1234, pkt.Seq)
assert.EqualValues(t, text, pkt.Text)
assert.EqualValues(t, payload, pkt.Payload)
}
示例14: TestReadStringBytes
func TestReadStringBytes(t *testing.T) {
obtained, rest, err := ReadStringBytes([]byte{0xab, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64})
expected := "Hello World"
assert.Equal(t, nil, err)
assert.Equal(t, expected, obtained)
assert.Equal(t, []byte{}, rest)
obtained, rest, err = ReadStringBytes([]byte{
0xd9, 0x21, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31})
expected = string(bytes.Repeat([]byte{0x31}, 33))
assert.Equal(t, nil, err)
assert.Equal(t, expected, obtained)
assert.Equal(t, []byte{}, rest)
obtained, rest, err = ReadStringBytes([]byte{
0xda, 0x01, 0x04, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31,
0x31, 0x31, 0x31, 0x31, 0x31, 0x31, 0x31})
expected = string(bytes.Repeat([]byte{0x31}, 260))
assert.Equal(t, nil, err)
assert.Equal(t, expected, obtained)
assert.Equal(t, []byte{}, rest)
}
示例15: initialNegotiation
func (c *Client) initialNegotiation() (tun *Conn) {
var newParams = new(tunParams)
var err error
tun, err = c.nego.negotiate(newParams)
if err != nil {
if log.V(1) == true || DEBUG {
log.Errorf("Connection failed %s, Error: %s. Retry after %s",
c.nego.RemoteName(), err, RETRY_INTERVAL)
} else {
log.Errorf("Connection failed %s. Retry after %s",
c.nego.RemoteName(), RETRY_INTERVAL)
}
if strings.Contains(err.Error(), "closed") {
log.Warningln(string(bytes.Repeat([]byte{'+'}, 30)))
log.Warningln("Maybe your clock is inaccurate, or your client credential is invalid.")
log.Warningln(string(bytes.Repeat([]byte{'+'}, 30)))
os.Exit(2)
}
return nil
}
c.params = newParams
c.token = newParams.token
tun.identifier = c.nego.RemoteName()
log.Infof("Login to the gateway %s successfully", tun.identifier)
return
}