|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +package aws.smithy.kotlin.runtime.content |
| 7 | + |
| 8 | +import aws.smithy.kotlin.runtime.testing.RandomTempFile |
| 9 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 10 | +import kotlinx.coroutines.test.runTest |
| 11 | +import kotlin.test.* |
| 12 | + |
| 13 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 14 | +class ByteStreamJVMTest { |
| 15 | + @Test |
| 16 | + fun `file as byte stream validates start`() = runTest { |
| 17 | + val file = RandomTempFile(1024) |
| 18 | + val e = assertFailsWith<Throwable> { |
| 19 | + file.asByteStream(-1) |
| 20 | + } |
| 21 | + assertEquals("start index -1 cannot be negative", e.message) |
| 22 | + } |
| 23 | + |
| 24 | + @Test |
| 25 | + fun `file as byte stream validates end`() = runTest { |
| 26 | + val file = RandomTempFile(1024) |
| 27 | + val e = assertFailsWith<Throwable> { |
| 28 | + file.asByteStream(endInclusive = 1024) |
| 29 | + } |
| 30 | + assertEquals("end index 1024 must be less than file size 1024", e.message) |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `file as byte stream validates start and end`() = runTest { |
| 35 | + val file = RandomTempFile(1024) |
| 36 | + val e = assertFailsWith<Throwable> { |
| 37 | + file.asByteStream(5, 1) |
| 38 | + } |
| 39 | + assertEquals("end index 1 must be greater than or equal to start index 5", e.message) |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + fun `file as byte stream has contentLength`() = runTest { |
| 44 | + val file = RandomTempFile(1024) |
| 45 | + val stream = file.asByteStream() |
| 46 | + |
| 47 | + assertEquals(1024, stream.contentLength) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `partial file as byte stream has contentLength`() = runTest { |
| 52 | + val file = RandomTempFile(1024) |
| 53 | + val stream = file.asByteStream(1, 1023) |
| 54 | + |
| 55 | + assertEquals(1023, stream.contentLength) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + fun `partial file as byte stream has contentLength with implicit end`() = runTest { |
| 60 | + val file = RandomTempFile(1024) |
| 61 | + val stream = file.asByteStream(1) |
| 62 | + |
| 63 | + assertEquals(1023, stream.contentLength) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `file as byte stream matches read`() = runTest { |
| 68 | + val file = RandomTempFile(1024) |
| 69 | + |
| 70 | + val expected = file.readBytes() |
| 71 | + val actual = file.asByteStream().toByteArray() |
| 72 | + |
| 73 | + assertContentEquals(expected, actual) |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + fun `partial file as byte stream matches read`() = runTest { |
| 78 | + val file = RandomTempFile(1024) |
| 79 | + |
| 80 | + val expected = file.readBytes() |
| 81 | + val part0 = file.asByteStream(endInclusive = 255).toByteArray() |
| 82 | + val part1 = file.asByteStream(256, 511).toByteArray() |
| 83 | + val part2 = file.asByteStream(512).toByteArray() |
| 84 | + |
| 85 | + assertContentEquals(expected, part0 + part1 + part2) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `partial file as byte stream using range`() = runTest { |
| 90 | + val file = RandomTempFile(1024) |
| 91 | + |
| 92 | + val expected = file.readBytes() |
| 93 | + val part0 = file.asByteStream(0L..255L).toByteArray() |
| 94 | + val part1 = file.asByteStream(256L..511L).toByteArray() |
| 95 | + val part2 = file.asByteStream(512L until file.length()).toByteArray() |
| 96 | + |
| 97 | + assertContentEquals(expected, part0 + part1 + part2) |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + fun `partial path as byte stream`() = runTest { |
| 102 | + val file = RandomTempFile(1024) |
| 103 | + val path = file.toPath() |
| 104 | + |
| 105 | + val expected = file.readBytes() |
| 106 | + val part0 = path.asByteStream(endInclusive = 255).toByteArray() |
| 107 | + val part1 = path.asByteStream(256, 511).toByteArray() |
| 108 | + val part2 = path.asByteStream(512).toByteArray() |
| 109 | + |
| 110 | + assertContentEquals(expected, part0 + part1 + part2) |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun `partial path as byte stream using range`() = runTest { |
| 115 | + val file = RandomTempFile(1024) |
| 116 | + val path = file.toPath() |
| 117 | + |
| 118 | + val expected = file.readBytes() |
| 119 | + val part0 = path.asByteStream(0L..255L).toByteArray() |
| 120 | + val part1 = path.asByteStream(256L..511L).toByteArray() |
| 121 | + val part2 = path.asByteStream(512L until file.length()).toByteArray() |
| 122 | + |
| 123 | + assertContentEquals(expected, part0 + part1 + part2) |
| 124 | + } |
| 125 | +} |
0 commit comments