| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* |
| 18 |
Copyright 2019 The Go Authors. All rights reserved. |
| 19 |
Use of this source code is governed by a BSD-style |
| 20 |
license that can be found in the LICENSE file. |
| 21 |
*/ |
| 22 |
|
| 23 |
package estargz |
| 24 |
|
| 25 |
import "testing" |
| 26 |
|
| 27 |
// Tests *Reader.ChunkEntryForOffset about offset and size calculation. |
| 28 |
func TestChunkEntryForOffset(t *testing.T) { |
| 29 |
const chunkSize = 4 |
| 30 |
tests := []struct { |
| 31 |
name string |
| 32 |
fileSize int64 |
| 33 |
reqOffset int64 |
| 34 |
wantOk bool |
| 35 |
wantChunkOffset int64 |
| 36 |
wantChunkSize int64 |
| 37 |
}{ |
| 38 |
{ |
| 39 |
name: "1st_chunk_in_1_chunk_reg", |
| 40 |
fileSize: chunkSize * 1, |
| 41 |
reqOffset: chunkSize * 0, |
| 42 |
wantChunkOffset: chunkSize * 0, |
| 43 |
wantChunkSize: chunkSize, |
| 44 |
wantOk: true, |
| 45 |
}, |
| 46 |
{ |
| 47 |
name: "2nd_chunk_in_1_chunk_reg", |
| 48 |
fileSize: chunkSize * 1, |
| 49 |
reqOffset: chunkSize * 1, |
| 50 |
wantOk: false, |
| 51 |
}, |
| 52 |
{ |
| 53 |
name: "1st_chunk_in_2_chunks_reg", |
| 54 |
fileSize: chunkSize * 2, |
| 55 |
reqOffset: chunkSize * 0, |
| 56 |
wantChunkOffset: chunkSize * 0, |
| 57 |
wantChunkSize: chunkSize, |
| 58 |
wantOk: true, |
| 59 |
}, |
| 60 |
{ |
| 61 |
name: "2nd_chunk_in_2_chunks_reg", |
| 62 |
fileSize: chunkSize * 2, |
| 63 |
reqOffset: chunkSize * 1, |
| 64 |
wantChunkOffset: chunkSize * 1, |
| 65 |
wantChunkSize: chunkSize, |
| 66 |
wantOk: true, |
| 67 |
}, |
| 68 |
{ |
| 69 |
name: "3rd_chunk_in_2_chunks_reg", |
| 70 |
fileSize: chunkSize * 2, |
| 71 |
reqOffset: chunkSize * 2, |
| 72 |
wantOk: false, |
| 73 |
}, |
| 74 |
} |
| 75 |
|
| 76 |
for _, te := range tests { |
| 77 |
t.Run(te.name, func(t *testing.T) { |
| 78 |
name := "test" |
| 79 |
_, r := regularFileReader(name, te.fileSize, chunkSize) |
| 80 |
ce, ok := r.ChunkEntryForOffset(name, te.reqOffset) |
| 81 |
if ok != te.wantOk { |
| 82 |
t.Errorf("ok = %v; want (%v)", ok, te.wantOk) |
| 83 |
} else if ok { |
| 84 |
if !(ce.ChunkOffset == te.wantChunkOffset && ce.ChunkSize == te.wantChunkSize) { |
| 85 |
t.Errorf("chunkOffset = %d, ChunkSize = %d; want (chunkOffset = %d, chunkSize = %d)", |
| 86 |
ce.ChunkOffset, ce.ChunkSize, te.wantChunkOffset, te.wantChunkSize) |
| 87 |
} |
| 88 |
} |
| 89 |
}) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// regularFileReader makes a minimal Reader of "reg" and "chunk" without tar-related information. |
| 94 |
func regularFileReader(name string, size int64, chunkSize int64) (*TOCEntry, *Reader) { |
| 95 |
ent := &TOCEntry{ |
| 96 |
Name: name, |
| 97 |
Type: "reg", |
| 98 |
} |
| 99 |
m := ent |
| 100 |
chunks := make([]*TOCEntry, 0, size/chunkSize+1) |
| 101 |
var written int64 |
| 102 |
for written < size { |
| 103 |
remain := size - written |
| 104 |
cs := chunkSize |
| 105 |
if remain < cs { |
| 106 |
cs = remain |
| 107 |
} |
| 108 |
ent.ChunkSize = cs |
| 109 |
ent.ChunkOffset = written |
| 110 |
chunks = append(chunks, ent) |
| 111 |
written += cs |
| 112 |
ent = &TOCEntry{ |
| 113 |
Name: name, |
| 114 |
Type: "chunk", |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
if len(chunks) == 1 { |
| 119 |
chunks = nil |
| 120 |
} |
| 121 |
return m, &Reader{ |
| 122 |
m: map[string]*TOCEntry{name: m}, |
| 123 |
chunks: map[string][]*TOCEntry{name: chunks}, |
| 124 |
} |
| 125 |
} |