Skip to content

Commit

Permalink
tests: update download tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MalinAhlberg committed Apr 25, 2024
1 parent 0b36d61 commit 092cf1c
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions sda-download/api/sda/sda_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,14 @@ func TestDownload_Fail_OpenFile(t *testing.T) {
return fileDetails, nil
}

// Mock request and response holders
// Mock request and response holders and initialize headers
w := httptest.NewRecorder()
c, _ := gin.CreateTestContext(w)
req := &http.Request{
URL: &url.URL{},
Header: make(http.Header),
}
c.Request = req

// Test the outcomes of the handler
Download(c)
Expand All @@ -520,7 +525,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 @@ -529,41 +534,44 @@ func TestEncrypted_Coords(t *testing.T) {
Header: make([]byte, 124),
}

// htsget_range should be used first and as is
// htsget_range should be used first and its end position should be increased by one
headerSize := bytes.NewReader(fileDetails.Header).Size()
fullSize := headerSize + int64(fileDetails.ArchiveSize)
start, end, err := calculateEncryptedCoords(from, to, "bytes=10-20", fileDetails)
var endPos int64
endPos = 20
start, end, err := calculateCoords(from, to, "bytes=10-"+strconv.FormatInt(endPos, 10), fileDetails, "default")
assert.Equal(t, start, int64(10))
assert.Equal(t, end, int64(20))
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)
_, 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)
_, 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)
_, end, err = calculateCoords(from, fullSize+1900, "", fileDetails, "encrypted")
assert.Equal(t, fullSize, end)
assert.NoError(t, err)

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

// range 0-0 with range in the header should return the range size
_, end, err = calculateEncryptedCoords(0, 0, "bytes=0-1000", fileDetails)
assert.Equal(t, end, int64(1000))
// byte range 0-1000 should return the range size, end coord inclusive
endPos = 1000
_, 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)
_, _, err = calculateCoords(0, 0, "bytes=start-end", fileDetails, "encrypted")
assert.Error(t, err)
}

Expand Down

0 comments on commit 092cf1c

Please sign in to comment.