Skip to content

Commit

Permalink
converage tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Sep 16, 2023
1 parent 4ab90a1 commit e1c268f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/main/kotlin/de/gmuth/ipp/core/IppInputStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ class IppInputStream(inputStream: BufferedInputStream) : DataInputStream(inputSt
}
}
} while (tag != End)
} catch (exception: Exception) {
readBytes().apply {
} catch (throwable: Throwable) {
if (throwable !is EOFException) readBytes().apply {
if (isNotEmpty()) {
log.warning { "skipped $size unparsed bytes" }
hexdump { log.warning { it } }
}
}
throw exception
throw throwable
}
}

Expand All @@ -75,9 +75,8 @@ class IppInputStream(inputStream: BufferedInputStream) : DataInputStream(inputSt
val name = readString()
val value = try {
readAttributeValue(tag)
} catch (exception: Exception) {
if (exception !is EOFException) readBytes().hexdump { log.info { it } }
throw IppException("failed to read attribute value of '$name' ($tag)", exception)
} catch (throwable: Throwable) {
throw IppException("failed to read attribute value of '$name' ($tag)", throwable)
}
// remember attributes-charset for name and text value decoding
if (name == "attributes-charset") attributesCharset = value as Charset
Expand Down
3 changes: 0 additions & 3 deletions src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,6 @@ abstract class IppMessage() {
toByteArray()
}

fun encodeAsInputStream() =
ByteArrayInputStream(encode())

// --------
// DECODING
// --------
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/de/gmuth/ipp/core/IppTag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package de.gmuth.ipp.core
* Copyright (c) 2020 Gerhard Muth
*/

// [RFC 8010] and [RFC 3380]
// [RFC 8010] and [RFC 3380]
enum class IppTag(
val code: Byte,
val registeredName: String,
Expand Down
6 changes: 6 additions & 0 deletions src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ class IppAttributesGroupTests {
assertEquals("bar", group.getValue("foo") as String)
}

@Test
fun getTextValue() {
group.attribute("foo", TextWithoutLanguage, "bar".toIppString())
assertEquals("bar", group.getTextValue("foo"))
}

@Test
fun getValueOrNull() {
group.attribute("foo0", Keyword, "bar0")
Expand Down
13 changes: 13 additions & 0 deletions src/test/kotlin/de/gmuth/ipp/core/IppInputStreamTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package de.gmuth.ipp.core
*/

import de.gmuth.ipp.core.IppResolution.Unit.DPI
import de.gmuth.log.Logging
import java.io.ByteArrayInputStream
import java.net.URI
import java.util.logging.Logger
Expand All @@ -15,6 +16,10 @@ import kotlin.test.assertTrue

class IppInputStreamTest {

init {
Logging.configure()
}

private val message = object : IppMessage() {
override val codeDescription: String
get() = "codeDescription"
Expand Down Expand Up @@ -181,6 +186,14 @@ class IppInputStreamTest {
}
}

@Test
fun readMessageFails() {
val encoded = "01 01 00 0B 00 00 00 08 01 47 00 01 61 00 01 66 0A 0B 0C 0D"
assertFailsWith<IppException> {
encoded.toIppInputStream().readMessage(message)
}
}

@Test
fun readMessageReadAttributeFails() {
val encoded = "00 03 66 6F 6F 00 03 62 61 72"
Expand Down
22 changes: 21 additions & 1 deletion src/test/kotlin/de/gmuth/ipp/core/IppRequestTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package de.gmuth.ipp.core
* Copyright (c) 2020-2023 Gerhard Muth
*/

import de.gmuth.ipp.core.IppOperation.CreateJobSubscriptions
import java.net.URI
import java.time.Duration
import java.util.logging.Logger.getLogger
import kotlin.test.Test
import kotlin.test.assertEquals
Expand Down Expand Up @@ -65,9 +67,27 @@ class IppRequestTests {
assertEquals(URI.create("ipp://printer"), getValue("printer-uri"))
assertEquals(0, getValue("job-id"))
assertEquals(listOf("one", "two"), getValues("requested-attributes"))
assertEquals("user".toIppString(), getValue("requesting-user-name"))
//assertEquals("user".toIppString(), getValue("requesting-user-name"))
}
assertEquals("user", requestDecoded.requestingUserName)
assertEquals("pdl-content", String(requestDecoded.documentInputStream!!.readBytes()))
}

@Test
fun createSubscriptionAttributesGroup() {
IppRequest(CreateJobSubscriptions, URI.create("ipp://foo"))
.createSubscriptionAttributesGroup(
listOf("all"),
Duration.ofHours(1),
Duration.ofMinutes(1),
999
)
}

@Test
fun createSubscriptionAttributesGroupWithNullDefaults() {
IppRequest(CreateJobSubscriptions, URI.create("ipp://null"))
.createSubscriptionAttributesGroup()
}

}
7 changes: 7 additions & 0 deletions src/test/kotlin/de/gmuth/ipp/core/IppResponseTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ class IppResponseTests {
}
}

@Test
fun createReponse() {
IppResponse(IppStatus.SuccessfulOk).run {
assertTrue(isSuccessful())
}
}

}

0 comments on commit e1c268f

Please sign in to comment.