aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-android/app/src/test/kotlin/org/meshbay/client/ByteRangeTest.kt
blob: 29d8528462f43a5331073df8badc7554220aec5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package org.meshbay.client

import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.meshbay.client.photos.ByteRange

class ByteRangeTest {
    @Test fun `a range is read as the upload asks for it`() {
        assertEquals(0L..1023L, ByteRange.parse("bytes=0-1023", 5000))
        assertEquals(4096L..4999L, ByteRange.parse("bytes=4096-", 5000))
        assertEquals("the last chunk may ask past the end", 4096L..4999L, ByteRange.parse("bytes=4096-8191", 5000))
    }

    @Test fun `what cannot be served is refused`() {
        assertNull(ByteRange.parse(null, 10))
        assertNull(ByteRange.parse("bytes=10-20", 10))
        assertNull(ByteRange.parse("bytes=5-2", 10))
        assertNull(ByteRange.parse("bytes=0-1,4-5", 10))
        assertNull(ByteRange.parse("bytes=-5", 10))
        assertNull(ByteRange.parse("items=0-1", 10))
    }

    @Test fun `a slice holds exactly those bytes`() {
        val data = ByteArray(100_000) { (it % 251).toByte() }
        val got = ByteRange.slice(data.inputStream(), 70_000L..99_999L).readBytes()
        assertArrayEquals(data.copyOfRange(70_000, 100_000), got)
        assertArrayEquals(data.copyOfRange(10, 12), ByteRange.slice(data.inputStream(), 10L..11L).readBytes())
    }
}