|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. |
| 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 | +package org.modelix.model.server.api.v2 |
| 18 | + |
| 19 | +import io.ktor.util.cio.use |
| 20 | +import io.ktor.util.toByteArray |
| 21 | +import io.ktor.utils.io.ByteChannel |
| 22 | +import io.ktor.utils.io.writeFully |
| 23 | +import io.ktor.utils.io.writeStringUtf8 |
| 24 | +import kotlinx.coroutines.flow.emptyFlow |
| 25 | +import kotlinx.coroutines.flow.flowOf |
| 26 | +import kotlinx.coroutines.flow.toList |
| 27 | +import kotlinx.coroutines.test.runTest |
| 28 | +import org.modelix.model.server.api.v2.VersionDeltaStreamV2.Companion.IncompleteData |
| 29 | +import org.modelix.model.server.api.v2.VersionDeltaStreamV2.Companion.decodeVersionDeltaStreamV2 |
| 30 | +import org.modelix.model.server.api.v2.VersionDeltaStreamV2.Companion.encodeVersionDeltaStreamV2 |
| 31 | +import kotlin.test.Test |
| 32 | +import kotlin.test.assertEquals |
| 33 | +import kotlin.test.assertFailsWith |
| 34 | + |
| 35 | +class VersionDeltaStreamV2Test { |
| 36 | + |
| 37 | + @Test |
| 38 | + fun parsesStreamWithoutObjects() = runTest { |
| 39 | + val channel = ByteChannel() |
| 40 | + channel.use { |
| 41 | + encodeVersionDeltaStreamV2(channel, "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A", emptyFlow()) |
| 42 | + } |
| 43 | + val versionDeltaStream = decodeVersionDeltaStreamV2(channel) |
| 44 | + |
| 45 | + assertEquals("CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A", versionDeltaStream.versionHash) |
| 46 | + assertEquals(emptyList(), versionDeltaStream.hashesWithDeltaObject.toList()) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun parsesStreamWithObjects() = runTest { |
| 51 | + val channel = ByteChannel() |
| 52 | + channel.use { |
| 53 | + encodeVersionDeltaStreamV2( |
| 54 | + channel, |
| 55 | + "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A", |
| 56 | + flowOf( |
| 57 | + "r7k0y*p0mmIhhD46RvqLsmTEGuBQvAf9hw7aN0IzihLc" to "L/100000017/xioDt*mnraICBf48DpWkvvtl2KuPixWn1p7yteYQ3XSg", |
| 58 | + "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A" to "1/%00/0/%00///", |
| 59 | + ), |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + val versionDeltaStream = decodeVersionDeltaStreamV2(channel) |
| 64 | + |
| 65 | + assertEquals("CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A", versionDeltaStream.versionHash) |
| 66 | + val expectedObjects = listOf( |
| 67 | + "r7k0y*p0mmIhhD46RvqLsmTEGuBQvAf9hw7aN0IzihLc" to "L/100000017/xioDt*mnraICBf48DpWkvvtl2KuPixWn1p7yteYQ3XSg", |
| 68 | + "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A" to "1/%00/0/%00///", |
| 69 | + ) |
| 70 | + assertEquals(expectedObjects, versionDeltaStream.hashesWithDeltaObject.toList()) |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + fun failsToParseEmptyStream() = runTest { |
| 75 | + val data = "" |
| 76 | + val channel = ByteChannel() |
| 77 | + channel.use { |
| 78 | + writeStringUtf8(data) |
| 79 | + } |
| 80 | + |
| 81 | + val exception = assertFailsWith<IncompleteData> { |
| 82 | + decodeVersionDeltaStreamV2(channel).hashesWithDeltaObject.toList() |
| 83 | + } |
| 84 | + assertEquals("Missing data line", exception.message) |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + fun failsToParseIncompleteLine() = runTest { |
| 89 | + val data = "CTVRw*a6KXJ4o7uzGlp" |
| 90 | + val channel = ByteChannel() |
| 91 | + channel.use { |
| 92 | + writeStringUtf8(data) |
| 93 | + } |
| 94 | + |
| 95 | + val exception = assertFailsWith<IncompleteData> { |
| 96 | + decodeVersionDeltaStreamV2(channel).hashesWithDeltaObject.toList() |
| 97 | + } |
| 98 | + assertEquals( |
| 99 | + "Missing end of data line [dataLine=`CTVRw*a6KXJ4o7uzGlp`] [endOfDataLine=`null`]", |
| 100 | + exception.message, |
| 101 | + ) |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun failsToParseStreamWithWrongEndOfDataLine() = runTest { |
| 106 | + val data = "CTVRw*a6KXJ4o7uzGlp\n%" |
| 107 | + val channel = ByteChannel() |
| 108 | + channel.use { |
| 109 | + writeStringUtf8(data) |
| 110 | + } |
| 111 | + |
| 112 | + val exception = assertFailsWith<IncompleteData> { |
| 113 | + decodeVersionDeltaStreamV2(channel).hashesWithDeltaObject.toList() |
| 114 | + } |
| 115 | + assertEquals( |
| 116 | + "Missing end of data line [dataLine=`CTVRw*a6KXJ4o7uzGlp`] [endOfDataLine=`%`]", |
| 117 | + exception.message, |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + @Test |
| 122 | + fun failsToParseStreamWithoutVersionHash() = runTest { |
| 123 | + val data = "~" |
| 124 | + val channel = ByteChannel() |
| 125 | + channel.use { |
| 126 | + writeStringUtf8(data) |
| 127 | + } |
| 128 | + |
| 129 | + val exception = assertFailsWith<IncompleteData> { |
| 130 | + decodeVersionDeltaStreamV2(channel).hashesWithDeltaObject.toList() |
| 131 | + } |
| 132 | + assertEquals("Version hash missing.", exception.message) |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + fun failsParsingStreamWithMissingDataAfterVersionHash() = runTest { |
| 137 | + val data = "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A\n$\n" |
| 138 | + val channel = ByteChannel() |
| 139 | + channel.use { |
| 140 | + writeStringUtf8(data) |
| 141 | + } |
| 142 | + |
| 143 | + val exception = assertFailsWith<IncompleteData> { |
| 144 | + decodeVersionDeltaStreamV2(channel).hashesWithDeltaObject.toList() |
| 145 | + } |
| 146 | + assertEquals("Missing data line", exception.message) |
| 147 | + } |
| 148 | + |
| 149 | + @Test |
| 150 | + fun failsParsingStreamWithMissingObject() = runTest { |
| 151 | + val data = "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A\n$\n" + |
| 152 | + "r7k0y*p0mmIhhD46RvqLsmTEGuBQvAf9hw7aN0IzihLc\n$\n" + |
| 153 | + "L/100000017/xioDt*mnraICBf48DpWkvvtl2KuPixWn1p7yteYQ3XSg\n$\n" + |
| 154 | + "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A\n$\n" + |
| 155 | + "~" |
| 156 | + |
| 157 | + val channel = ByteChannel() |
| 158 | + channel.use { |
| 159 | + writeStringUtf8(data) |
| 160 | + } |
| 161 | + |
| 162 | + val emittedObjects = mutableListOf<Pair<String, String>>() |
| 163 | + val exception = assertFailsWith<IncompleteData> { |
| 164 | + decodeVersionDeltaStreamV2(channel).hashesWithDeltaObject.collect(emittedObjects::add) |
| 165 | + } |
| 166 | + assertEquals("Missing delta object.", exception.message) |
| 167 | + assertEquals( |
| 168 | + listOf("r7k0y*p0mmIhhD46RvqLsmTEGuBQvAf9hw7aN0IzihLc" to "L/100000017/xioDt*mnraICBf48DpWkvvtl2KuPixWn1p7yteYQ3XSg"), |
| 169 | + emittedObjects, |
| 170 | + ) |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + fun failsToParseAnySubstringOfAValidEncoding() = runTest { |
| 175 | + val channel = ByteChannel() |
| 176 | + channel.use { |
| 177 | + encodeVersionDeltaStreamV2( |
| 178 | + channel, |
| 179 | + "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A", |
| 180 | + flowOf( |
| 181 | + "r7k0y*p0mmIhhD46RvqLsmTEGuBQvAf9hw7aN0IzihLc" to "L/100000017/xioDt*mnraICBf48DpWkvvtl2KuPixWn1p7yteYQ3XSg", |
| 182 | + "CTVRw*a6KXJ4o7uzGlp-kUosxpyRf4fUpHnLokG9T86A" to "1/%00/0/%00///", |
| 183 | + ), |
| 184 | + ) |
| 185 | + } |
| 186 | + val fullByteArray = channel.toByteArray() |
| 187 | + |
| 188 | + for (newSize in fullByteArray.indices) { |
| 189 | + val subByteArray = fullByteArray.copyOf(newSize) |
| 190 | + val subChannel = ByteChannel() |
| 191 | + subChannel.use { |
| 192 | + subChannel.writeFully(subByteArray) |
| 193 | + } |
| 194 | + |
| 195 | + assertFailsWith<IncompleteData> { |
| 196 | + decodeVersionDeltaStreamV2(subChannel).hashesWithDeltaObject.toList() |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments