Skip to content

Commit

Permalink
refactor: rename caluclateCoords function
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinAhlberg committed Apr 17, 2024
1 parent 7f270dc commit d862b0c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion sda-download/api/sda/sda.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ var sendStream = func(reader io.ReadSeeker, writer http.ResponseWriter, start, e
// Calculates the start and end coordinats to use. If a range is set in HTTP headers,
// it will be used as is. If not, the functions parameters will be used.
// If in encrypted mode, the parameters will be adjusted to match the data block boundaries.
var calculateEncryptedCoords = func(start, end int64, htsget_range string, fileDetails *database.FileDownload, encryptedType string) (int64, int64, error) {
var calculateCoords = func(start, end int64, htsget_range string, fileDetails *database.FileDownload, encryptedType string) (int64, int64, error) {
log.Warnf("calculate")
if htsget_range != "" {
log.Warnf("calculate non empty")
Expand Down
16 changes: 8 additions & 8 deletions sda-download/api/sda/sda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ func TestDownload_Fail_OpenFile(t *testing.T) {

}

func TestEncrypted_Coords(t *testing.T) {
func Test_CalucalateCoords(t *testing.T) {
var to, from int64
from, to = 0, 1000
fileDetails := &database.FileDownload{
Expand All @@ -528,38 +528,38 @@ func TestEncrypted_Coords(t *testing.T) {
fullSize := headerSize + int64(fileDetails.ArchiveSize)
var endPos int64
endPos = 20
start, end, err := calculateEncryptedCoords(from, to, "bytes=10-"+strconv.FormatInt(endPos, 10), fileDetails, "default")
start, end, err := calculateCoords(from, to, "bytes=10-"+strconv.FormatInt(endPos, 10), fileDetails, "default")
assert.Equal(t, start, int64(10))
assert.Equal(t, end, endPos+1)
assert.NoError(t, err)

// end should be greater than or equal to inputted end
_, end, err = calculateEncryptedCoords(from, to, "", fileDetails, "encrypted")
_, end, err = calculateCoords(from, to, "", fileDetails, "encrypted")
assert.GreaterOrEqual(t, end, from)
assert.NoError(t, err)

// end should not be smaller than a header
_, end, err = calculateEncryptedCoords(from, headerSize-10, "", fileDetails, "encrypted")
_, end, err = calculateCoords(from, headerSize-10, "", fileDetails, "encrypted")
assert.GreaterOrEqual(t, end, headerSize)
assert.NoError(t, err)

// end should not be larger than file length + header
_, end, err = calculateEncryptedCoords(from, fullSize+1900, "", fileDetails, "encrypted")
_, end, err = calculateCoords(from, fullSize+1900, "", fileDetails, "encrypted")
assert.Equal(t, fullSize, end)
assert.NoError(t, err)

// param range 0-0 should give whole file
start, end, err = calculateEncryptedCoords(0, 0, "", fileDetails, "encrypted")
start, end, err = calculateCoords(0, 0, "", fileDetails, "encrypted")
assert.Equal(t, end-start, fullSize)
assert.NoError(t, err)

// byte range 0-1000 should return the range size, end coord inclusive
endPos = 1000
_, end, err = calculateEncryptedCoords(0, 0, "bytes=0-"+strconv.FormatInt(endPos, 10), fileDetails, "encrypted")
_, end, err = calculateCoords(0, 0, "bytes=0-"+strconv.FormatInt(endPos, 10), fileDetails, "encrypted")
assert.Equal(t, end, endPos+1)
assert.NoError(t, err)

// range in the header should return error if values are not numbers
_, _, err = calculateEncryptedCoords(0, 0, "bytes=start-end", fileDetails, "encrypted")
_, _, err = calculateCoords(0, 0, "bytes=start-end", fileDetails, "encrypted")
assert.Error(t, err)
}

0 comments on commit d862b0c

Please sign in to comment.