TestBytesToDataUpload tests the BytesToDataUpload function and the DataBuilder with large random data uploads.
(t *testing.T)
| 150 | // TestBytesToDataUpload tests the BytesToDataUpload function and the DataBuilder |
| 151 | // with large random data uploads. |
| 152 | func TestBytesToDataUpload(t *testing.T) { |
| 153 | t.Parallel() |
| 154 | |
| 155 | for i := 0; i < 20; i++ { |
| 156 | // Generate random data |
| 157 | //nolint:gosec // Just a unit test |
| 158 | chunkCount := 1 + rand.Intn(3) |
| 159 | //nolint:gosec // Just a unit test |
| 160 | size := (chunkCount * proto.ChunkSize) + (rand.Int() % proto.ChunkSize) |
| 161 | data := make([]byte, size) |
| 162 | _, err := crand.Read(data) |
| 163 | require.NoError(t, err) |
| 164 | |
| 165 | first, chunks, err := proto.BytesToDataUpload(proto.DataUploadType_UPLOAD_TYPE_MODULE_FILES, data) |
| 166 | require.NoError(t, err) |
| 167 | |
| 168 | builder, err := proto.NewDataBuilder(first) |
| 169 | require.NoError(t, err) |
| 170 | |
| 171 | // Try to add some bad chunks |
| 172 | _, err = builder.Add(&proto.ChunkPiece{Data: []byte{}, FullDataHash: make([]byte, 32)}) |
| 173 | require.ErrorContains(t, err, "data hash does not match") |
| 174 | |
| 175 | // Verify 'Complete' fails before adding any chunks |
| 176 | _, err = builder.Complete() |
| 177 | require.ErrorContains(t, err, "data upload is not complete") |
| 178 | |
| 179 | // Add the chunks |
| 180 | var done bool |
| 181 | for _, chunk := range chunks { |
| 182 | require.False(t, done, "data upload should not be complete before adding all chunks") |
| 183 | |
| 184 | done, err = builder.Add(chunk) |
| 185 | require.NoError(t, err, "chunk %d should be added successfully", chunk.PieceIndex) |
| 186 | } |
| 187 | require.True(t, done, "data upload should be complete after adding all chunks") |
| 188 | |
| 189 | // Try to add another chunk after completion |
| 190 | done, err = builder.Add(chunks[0]) |
| 191 | require.ErrorContains(t, err, "data upload is already complete") |
| 192 | require.True(t, done, "still complete") |
| 193 | |
| 194 | // Verify the final data matches the original |
| 195 | got, err := builder.Complete() |
| 196 | require.NoError(t, err) |
| 197 | |
| 198 | require.Equal(t, data, got, "final data should match the original data") |
| 199 | } |
| 200 | } |
nothing calls this directly
no test coverage detected