github.com/ethersphere/bee/v2@v2.2.0/pkg/storage/testing/chunk.go (about) 1 // nolint:goheader 2 // Copyright 2019 The Swarm Authors 3 // This file is part of the Swarm library. 4 // 5 // The Swarm library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The Swarm library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the Swarm library. If not, see <http://www.gnu.org/licenses/>. 17 18 package testing 19 20 import ( 21 "crypto/rand" 22 "testing" 23 24 "github.com/ethersphere/bee/v2/pkg/cac" 25 "github.com/ethersphere/bee/v2/pkg/crypto" 26 postagetesting "github.com/ethersphere/bee/v2/pkg/postage/testing" 27 "github.com/ethersphere/bee/v2/pkg/soc" 28 "github.com/ethersphere/bee/v2/pkg/swarm" 29 "github.com/ethersphere/bee/v2/pkg/util/testutil" 30 ) 31 32 // GenerateTestRandomChunk generates a valid content addressed chunk. 33 func GenerateTestRandomChunk() swarm.Chunk { 34 key, _ := crypto.GenerateSecp256k1Key() 35 signer := crypto.NewDefaultSigner(key) 36 37 data := make([]byte, swarm.ChunkSize) 38 _, _ = rand.Read(data) 39 ch, _ := cac.New(data) 40 stamp := postagetesting.MustNewValidStamp(signer, ch.Address()) 41 42 return ch.WithStamp(stamp) 43 } 44 45 // GenerateTestRandomSoChunk generates a valid single owner chunk 46 // using supplied content addressed chunk. 47 func GenerateTestRandomSoChunk(tb testing.TB, cac swarm.Chunk) swarm.Chunk { 48 tb.Helper() 49 50 key, _ := crypto.GenerateSecp256k1Key() 51 signer := crypto.NewDefaultSigner(key) 52 id := testutil.RandBytes(tb, swarm.HashSize) 53 54 ch, err := soc.New(id, cac).Sign(signer) 55 if err != nil { 56 tb.Fatal(err) 57 } 58 59 stamp := postagetesting.MustNewValidStamp(signer, ch.Address()) 60 61 return ch.WithStamp(stamp) 62 } 63 64 // GenerateTestRandomInvalidChunk generates a random, however invalid, content 65 // addressed chunk. 66 func GenerateTestRandomInvalidChunk() swarm.Chunk { 67 data := make([]byte, swarm.ChunkSize) 68 _, _ = rand.Read(data) 69 key := make([]byte, swarm.SectionSize) 70 _, _ = rand.Read(key) 71 stamp := postagetesting.MustNewStamp() 72 return swarm.NewChunk(swarm.NewAddress(key), data).WithStamp(stamp) 73 } 74 75 // GenerateTestRandomChunks generates a slice of random 76 // Chunks by using GenerateTestRandomChunk function. 77 func GenerateTestRandomChunks(count int) []swarm.Chunk { 78 chunks := make([]swarm.Chunk, count) 79 for i := 0; i < count; i++ { 80 chunks[i] = GenerateTestRandomChunk() 81 } 82 return chunks 83 } 84 85 // GenerateTestRandomChunkAt generates an invalid (!) chunk with address of proximity order po wrt target. 86 func GenerateTestRandomChunkAt(tb testing.TB, target swarm.Address, po int) swarm.Chunk { 87 tb.Helper() 88 89 data := make([]byte, swarm.ChunkSize) 90 _, _ = rand.Read(data) 91 addr := swarm.RandAddressAt(tb, target, po) 92 stamp := postagetesting.MustNewStamp() 93 return swarm.NewChunk(addr, data).WithStamp(stamp) 94 } 95 96 // GenerateTestRandomChunkAt generates an invalid (!) chunk with address of proximity order po wrt target. 97 func GenerateValidRandomChunkAt(target swarm.Address, po int) swarm.Chunk { 98 data := make([]byte, swarm.ChunkSize) 99 100 var ch swarm.Chunk 101 var err error 102 for { 103 _, _ = rand.Read(data) 104 ch, err = cac.New(data) 105 if err != nil { 106 continue 107 } 108 if swarm.Proximity(ch.Address().Bytes(), target.Bytes()) > uint8(po) { 109 break 110 } 111 } 112 113 stamp := postagetesting.MustNewStamp() 114 return ch.WithStamp(stamp) 115 } 116 117 // FixtureChunk gets a pregenerated content-addressed chunk and 118 // panics if one is not found. 119 func FixtureChunk(prefix string) swarm.Chunk { 120 c, ok := fixtureChunks()[prefix] 121 if !ok { 122 panic("no fixture found") 123 } 124 return c.WithStamp(postagetesting.MustNewStamp()) 125 } 126 127 // fixtureChunks returns pregenerated content-addressed chunks necessary for explicit 128 // test scenarios where random generated chunks are not good enough. 129 func fixtureChunks() map[string]swarm.Chunk { 130 return map[string]swarm.Chunk{ 131 "0025": swarm.NewChunk( 132 swarm.MustParseHexAddress("0025737be11979e91654dffd2be817ac1e52a2dadb08c97a7cef12f937e707bc"), 133 []byte{72, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 149, 179, 31, 244, 146, 247, 129, 123, 132, 248, 215, 77, 44, 47, 91, 248, 229, 215, 89, 156, 210, 243, 3, 110, 204, 74, 101, 119, 53, 53, 145, 188, 193, 153, 130, 197, 83, 152, 36, 140, 150, 209, 191, 214, 193, 4, 144, 121, 32, 45, 205, 220, 59, 227, 28, 43, 161, 51, 108, 14, 106, 180, 135, 2}, 134 ), 135 "0033": swarm.NewChunk( 136 swarm.MustParseHexAddress("0033153ac8cfb0c343db1795f578c15ed8ef827f3e68ed3c58329900bf0d7276"), 137 []byte{72, 0, 0, 0, 0, 0, 0, 0, 170, 117, 0, 0, 0, 0, 0, 0, 21, 157, 63, 86, 45, 17, 166, 184, 47, 126, 58, 172, 242, 77, 153, 249, 97, 5, 107, 244, 23, 153, 220, 255, 254, 47, 209, 24, 63, 58, 126, 142, 41, 79, 201, 182, 178, 227, 235, 223, 63, 11, 220, 155, 40, 181, 56, 204, 91, 44, 51, 185, 95, 155, 245, 235, 187, 250, 103, 49, 139, 184, 46, 199}, 138 ), 139 "02c2": swarm.NewChunk( 140 swarm.MustParseHexAddress("02c2bd0db71efb7d245eafcc1c126189c1f598feb80e8f14e7ecef913c6a2ef5"), 141 []byte{72, 0, 0, 0, 0, 0, 0, 0, 226, 0, 0, 0, 0, 0, 0, 0, 67, 234, 252, 231, 229, 11, 121, 163, 131, 171, 41, 107, 57, 191, 221, 32, 62, 204, 159, 124, 116, 87, 30, 244, 99, 137, 121, 248, 119, 56, 74, 102, 140, 73, 178, 7, 151, 22, 47, 126, 173, 30, 43, 7, 61, 187, 13, 236, 59, 194, 245, 18, 25, 237, 106, 125, 78, 241, 35, 34, 116, 154, 105, 205}, 142 ), 143 "7000": swarm.NewChunk( 144 swarm.MustParseHexAddress("70002115a015d40a1f5ef68c29d072f06fae58854934c1cb399fcb63cf336127"), 145 []byte{72, 0, 0, 0, 0, 0, 0, 0, 124, 59, 0, 0, 0, 0, 0, 0, 44, 67, 19, 101, 42, 213, 4, 209, 212, 189, 107, 244, 111, 22, 230, 24, 245, 103, 227, 165, 88, 74, 50, 11, 143, 197, 220, 118, 175, 24, 169, 193, 15, 40, 225, 196, 246, 151, 1, 45, 86, 7, 36, 99, 156, 86, 83, 29, 46, 207, 115, 112, 126, 88, 101, 128, 153, 113, 30, 27, 50, 232, 77, 215}, 146 ), 147 } 148 }