diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt b/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt index a9840138..b8c683d1 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppAttribute.kt @@ -70,17 +70,17 @@ data class IppAttribute(val name: String, val tag: IppTag) : IppAttributeBuil fun enumNameOrValue(value: Any) = if (tag == IppTag.Enum) getEnumName(name, value) else value - fun log(logger: Logger, level: Level = INFO, prefix: String = "") = logger.run { + fun log(logger: Logger, level: Level = INFO, prefix: String = "") = logger.also { val string = toString() if (string.length < 160) { - log(level) { "$prefix$string" } + it.log(level) { "$prefix$string" } } else { - log(level) { "$prefix$name ($tag) =" } + it.log(level) { "$prefix$name ($tag) =" } for (value in values) { if (value is IppCollection) { (value as IppCollection).log(logger, level, "$prefix ") } else { - log(level) { "$prefix ${enumNameOrValue(value as Any)}" } + it.log(level) { "$prefix ${enumNameOrValue(value as Any)}" } } } } diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppAttributesGroup.kt b/src/main/kotlin/de/gmuth/ipp/core/IppAttributesGroup.kt index 2e4815f3..b76ab946 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppAttributesGroup.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppAttributesGroup.kt @@ -6,7 +6,7 @@ package de.gmuth.ipp.core import java.io.File import java.time.Instant -import java.time.ZoneId +import java.time.ZoneOffset import java.time.ZonedDateTime import java.util.logging.Level import java.util.logging.Level.INFO @@ -53,8 +53,8 @@ class IppAttributesGroup(val tag: IppTag) : LinkedHashMap(name).text - fun getTimeValue(name: String, zoneId: ZoneId = ZoneId.systemDefault()): ZonedDateTime = - Instant.ofEpochSecond(getValue(name).toLong()).atZone(zoneId) + fun getTimeValue(name: String): ZonedDateTime = + Instant.ofEpochSecond(getValue(name).toLong()).atZone(ZoneOffset.UTC) fun put(attributesGroup: IppAttributesGroup) = attributesGroup.values.forEach { put(it) } diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppDateTime.kt b/src/main/kotlin/de/gmuth/ipp/core/IppDateTime.kt index 7040d612..c4c665a5 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppDateTime.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppDateTime.kt @@ -5,6 +5,7 @@ package de.gmuth.ipp.core */ import java.time.LocalDateTime +import java.time.ZoneId import java.time.ZoneOffset import java.time.ZonedDateTime import java.time.temporal.ChronoField @@ -137,6 +138,10 @@ data class IppDateTime( internal fun getTimeZoneId() = "GMT%c%02d%02d".format(directionFromUTC, hoursFromUTC, minutesFromUTC) + companion object { + fun now(zone: ZoneId = ZoneId.systemDefault()) = + IppDateTime(ZonedDateTime.now(zone)) + } } // Calendar extension diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppInputStream.kt b/src/main/kotlin/de/gmuth/ipp/core/IppInputStream.kt index feb5b978..f429853f 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppInputStream.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppInputStream.kt @@ -13,7 +13,7 @@ import java.util.logging.Logger.getLogger class IppInputStream(inputStream: BufferedInputStream) : DataInputStream(inputStream) { - private val log = getLogger(javaClass.name) + internal val log = getLogger(javaClass.name) // character encoding for text and name attributes, RFC 8011 4.1.4.1 internal lateinit var attributesCharset: Charset @@ -236,7 +236,7 @@ class IppInputStream(inputStream: BufferedInputStream) : DataInputStream(inputSt charStringBuilder.clear() } } - if (isNotEmpty()) dumpLine() + if (hexStringBuilder.isNotEmpty()) dumpLine() } } \ No newline at end of file diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt b/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt index 22d7c019..786ea934 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt @@ -13,7 +13,7 @@ import java.util.logging.Logger.getLogger abstract class IppMessage() { - private val log = getLogger(javaClass.name) + internal val log = getLogger(javaClass.name) var code: Int? = null // unsigned short (16 bits) var requestId: Int? = null var version: String? = null diff --git a/src/test/kotlin/de/gmuth/ipp/client/IppClientTests.kt b/src/test/kotlin/de/gmuth/ipp/client/IppClientTests.kt index ce3f2edb..85e671d3 100644 --- a/src/test/kotlin/de/gmuth/ipp/client/IppClientTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/client/IppClientTests.kt @@ -1,5 +1,9 @@ package de.gmuth.ipp.client +/** + * Copyright (c) 2023 Gerhard Muth + */ + import de.gmuth.ipp.core.IppOperation.GetPrinterAttributes import org.junit.Test import java.net.URI diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppExchangeExceptionTests.kt b/src/test/kotlin/de/gmuth/ipp/client/IppExchangeExceptionTests.kt similarity index 85% rename from src/test/kotlin/de/gmuth/ipp/core/IppExchangeExceptionTests.kt rename to src/test/kotlin/de/gmuth/ipp/client/IppExchangeExceptionTests.kt index 5d3f527f..d8c5eff5 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppExchangeExceptionTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/client/IppExchangeExceptionTests.kt @@ -1,10 +1,11 @@ -package de.gmuth.ipp.core +package de.gmuth.ipp.client /** * Copyright (c) 2020 Gerhard Muth */ -import de.gmuth.ipp.client.IppExchangeException +import de.gmuth.ipp.core.IppOperation +import de.gmuth.ipp.core.IppRequest import java.util.logging.Logger.getLogger import kotlin.test.Test import kotlin.test.assertEquals diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt index 3ee2874c..31e316a4 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppAttributeTests.kt @@ -5,14 +5,19 @@ package de.gmuth.ipp.core */ import de.gmuth.ipp.core.IppTag.* +import de.gmuth.log.Logging import java.io.File import java.util.logging.Logger.getLogger import kotlin.test.* class IppAttributeTests { - private val ippAttribute = IppAttribute("printer-state-reasons", Keyword, "none") - val tlog = getLogger(javaClass.name) + init { + Logging.configure() + } + + private val log = getLogger(javaClass.name) + private val attribute = IppAttribute("printer-state-reasons", Keyword, "none") @Test fun constructorFailsDueToDelimiterTag() { @@ -26,28 +31,28 @@ class IppAttributeTests { @Test fun accessingSetAsValueFails() { - assertFailsWith { ippAttribute.value } + assertFailsWith { attribute.value } } @Test fun additionalValue() { - ippAttribute.additionalValue(IppAttribute("", Keyword, "media-empty")) - assertEquals(2, ippAttribute.values.size) + attribute.additionalValue(IppAttribute("", Keyword, "media-empty")) + assertEquals(2, attribute.values.size) } @Test fun additionalValueIgnore1() { - ippAttribute.additionalValue(IppAttribute("", Integer, 2.1)) + attribute.additionalValue(IppAttribute("", Integer, 2.1)) } @Test fun additionalValueFails2() { - assertFailsWith { ippAttribute.additionalValue(IppAttribute("", Keyword)) } + assertFailsWith { attribute.additionalValue(IppAttribute("", Keyword)) } } @Test fun additionalValueFails3() { - assertFailsWith { ippAttribute.additionalValue(IppAttribute("invalid-name", Keyword, "wtf")) } + assertFailsWith { attribute.additionalValue(IppAttribute("invalid-name", Keyword, "wtf")) } } @Test @@ -60,13 +65,13 @@ class IppAttributeTests { @Test fun buildAttribute() { - assertEquals(ippAttribute, ippAttribute.buildIppAttribute(IppAttributesGroup(Printer))) + assertEquals(attribute, attribute.buildIppAttribute(IppAttributesGroup(Printer))) } @Test fun toStringNoValue() { - ippAttribute.values.clear() - assertTrue(ippAttribute.toString().endsWith("no-value")) + attribute.values.clear() + assertTrue(attribute.toString().endsWith("no-value")) } @Test @@ -81,21 +86,10 @@ class IppAttributeTests { } @Test - fun toStringTime1() { - IppAttribute("some-time", Integer, 1000).toString() + fun toStringTest() { + assertEquals("christmas-time (integer) = 1608160102", IppAttribute("christmas-time", Integer, 1608160102).toString()) } - @Test - fun toStringTime2() { - IppAttribute("christmas-time", Integer, 1608160102).toString() - } - - @Test - fun toStringTimeOut() { - IppAttribute("some-time-out", Integer, 1000).toString() - } - - @Test fun enumNameOrValue() { assertEquals("processing", IppAttribute("printer-state", IppTag.Enum, 0).enumNameOrValue(4)) @@ -104,7 +98,7 @@ class IppAttributeTests { @Test fun log() { // cover an output with more than 160 characters and a collection value - IppAttribute("media-col".padEnd(160, '-'), BegCollection, IppCollection()).log(tlog) + IppAttribute("media-col".padEnd(160, '-'), BegCollection, IppCollection()).log(log) } @Test @@ -122,4 +116,11 @@ class IppAttributeTests { assertEquals("foo (1setOf integer) = 1,2,3", IppAttribute("foo", Integer, 1, 2, 3).toString()) } + @Test + fun attributeWithDateTimeHasValidValueClass() { + IppAttribute("datetime-now", DateTime, IppDateTime.now()).run { + assertTrue(tag.valueHasValidClass(value)) + } + } + } \ No newline at end of file diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt index d744f7bf..de725e93 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppAttributesGroupTests.kt @@ -14,7 +14,7 @@ import kotlin.test.assertTrue class IppAttributesGroupTests { - val log = getLogger(javaClass.name) + private val log = getLogger(javaClass.name) private val group = IppAttributesGroup(Operation) @Test @@ -28,8 +28,9 @@ class IppAttributesGroupTests { } @Test - fun putWithReplacementAllowed() { - with(IppAttributesGroup(Printer)) { + fun putWithReplacement() { + group.run { + onReplaceWarn = false attribute("number", Integer, 0) attribute("number", Integer, 1, 2) assertEquals(1, size) @@ -39,11 +40,13 @@ class IppAttributesGroupTests { @Test fun putWithReplacementWarning() { - group.put(IppAttribute("number", Integer, 0)) - group.onReplaceWarn = true - group.put(IppAttribute("number", Integer, 1, 2)) - assertEquals(1, group.size) - assertEquals(group["number"]!!.values.size, 2) + group.run { + onReplaceWarn = true + put(IppAttribute("number", Integer, 0)) + put(IppAttribute("number", Integer, 1, 2)) + assertEquals(1, size) + assertEquals(get("number")!!.values.size, 2) + } } @Test @@ -70,7 +73,7 @@ class IppAttributesGroupTests { @Test fun getValue() { group.attribute("foo", Keyword, "bar") - assertEquals("bar", group.getValue("foo") as String) + assertEquals("bar", group.getValue("foo")) } @Test @@ -79,6 +82,12 @@ class IppAttributesGroupTests { assertEquals("bar", group.getTextValue("foo")) } + @Test + fun getEpochTimeValue() { + group.attribute("epoch-seconds", Integer, 62) + assertEquals("1970-01-01T00:01:02", group.getTimeValue("epoch-seconds").toLocalDateTime().toString()) + } + @Test fun getValueOrNull() { group.attribute("foo0", Keyword, "bar0") diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt index 8887410e..d4d8f14c 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppCollectionTests.kt @@ -1,7 +1,7 @@ package de.gmuth.ipp.core /** - * Copyright (c) 2020 Gerhard Muth + * Copyright (c) 2020-2023 Gerhard Muth */ import java.util.NoSuchElementException @@ -12,7 +12,7 @@ import kotlin.test.assertFailsWith class IppCollectionTests { - val tlog = getLogger(javaClass.name) + private val log = getLogger(javaClass.name) private val collection = IppCollection(IppAttribute("foo", IppTag.Keyword, "a", "b")) @Test @@ -21,7 +21,7 @@ class IppCollectionTests { } @Test - fun attribute() { + fun addAttribute() { collection.addAttribute("year", IppTag.Integer, 2021) assertEquals(2, collection.members.size) } @@ -41,13 +41,13 @@ class IppCollectionTests { @Test fun logNarrow() { - collection.log(tlog) + collection.log(log) } @Test fun logWide() { collection.addAll(listOf(IppAttribute("bar", IppTag.Keyword, "c".repeat(160)))) - collection.log(tlog) + collection.log(log) } } \ No newline at end of file diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppInputStreamTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppInputStreamTests.kt index d43334d5..79e1b89d 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppInputStreamTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppInputStreamTests.kt @@ -5,10 +5,9 @@ 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 +import java.util.logging.Level.ALL import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -16,10 +15,6 @@ import kotlin.test.assertTrue class IppInputStreamTest { - init { - Logging.configure() - } - private val message = object : IppMessage() { override val codeDescription: String get() = "codeDescription" @@ -175,23 +170,25 @@ class IppInputStreamTest { assertEquals(8, requestId) with(getSingleAttributesGroup(IppTag.Job)) { assertEquals(2, size) - with(get("1")!!) { - assertEquals(IppTag.Boolean, this.tag) + get("1")!!.run { + assertEquals(IppTag.Boolean, tag) assertEquals(listOf(true, false), values as List<*>) } - with(get("0")!!) { - assertEquals(IppTag.NoValue, this.tag) - } + assertEquals(IppTag.NoValue, get("0")!!.tag) } } } @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 { - encoded.toIppInputStream().readMessage(message) - } + val encoded = + "01 01 00 0B 00 00 00 08 01 47 00 01 61 00 01 66 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF AA" + assertFailsWith { + encoded.toIppInputStream().run { + log.level = ALL + readMessage(message) + } + } } @Test @@ -203,7 +200,9 @@ class IppInputStreamTest { @Test fun readOutOfBandAttribute() { val encoded = "00 01 65 00 01 02" - encoded.toIppInputStream().readAttribute(IppTag.NoValue) + encoded.toIppInputStream().run { + assertEquals(1, readAttribute(IppTag.NoValue).values.size) + } } @Test @@ -214,16 +213,14 @@ class IppInputStreamTest { with(attribute.value as ByteArray) { assertEquals(1, size) } } -} - -// hex utility extensions + // Hex utility extensions -fun String.toByteArray() = - ByteArray((length + 1) / 3) { substring(3 * it, 3 * it + 2).toInt(16).toByte() } + private fun String.toByteArray() = + ByteArray((length + 1) / 3) { substring(3 * it, 3 * it + 2).toInt(16).toByte() } -fun String.toIppInputStream() = IppInputStream(ByteArrayInputStream(toByteArray()).buffered()).apply { - attributesCharset = Charsets.US_ASCII -} + private fun String.toIppInputStream() = IppInputStream(ByteArrayInputStream(toByteArray()).buffered()) + .apply { attributesCharset = Charsets.US_ASCII } -fun String.readAttributeValue(tag: IppTag) = - toIppInputStream().readAttributeValue(tag) \ No newline at end of file + private fun String.readAttributeValue(tag: IppTag) = + toIppInputStream().readAttributeValue(tag) +} \ No newline at end of file diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppOutputStreamTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppOutputStreamTests.kt index ad50d428..753246d6 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppOutputStreamTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppOutputStreamTests.kt @@ -5,14 +5,20 @@ package de.gmuth.ipp.core */ import de.gmuth.ipp.core.IppResolution.Unit.DPI +import de.gmuth.ipp.core.IppTag.* +import de.gmuth.ipp.core.IppTag.Boolean as IppBoolean +import de.gmuth.ipp.core.IppTag.Enum as IppEnum import java.io.ByteArrayOutputStream import java.net.URI +import java.util.logging.Level +import java.util.logging.Logger import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith class IppOutputStreamTest { + private val log = Logger.getLogger(javaClass.name) private val byteArrayOutputStream = ByteArrayOutputStream() private val ippOutputStream = IppOutputStream(byteArrayOutputStream).apply { attributesCharset = Charsets.US_ASCII } @@ -21,7 +27,7 @@ class IppOutputStreamTest { get() = "codeDescription" init { - createAttributesGroup(IppTag.Operation).attribute("attributes-charset", IppTag.Charset, Charsets.UTF_8) + createAttributesGroup(Operation).attribute("attributes-charset", IppTag.Charset, Charsets.UTF_8) } } @@ -33,114 +39,114 @@ class IppOutputStreamTest { @Test fun writeAttribute() { - ippOutputStream.writeAttribute(IppAttribute("0", IppTag.NotSettable, ByteArray(0))) + ippOutputStream.writeAttribute(IppAttribute("0", NotSettable, ByteArray(0))) assertEquals("15 00 01 30 00 00", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueBooleanFalse() { - ippOutputStream.writeAttributeValue(IppTag.Boolean, false) + ippOutputStream.writeAttributeValue(IppBoolean, false) assertEquals("00 01 00", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueBooleanTrue() { - ippOutputStream.writeAttributeValue(IppTag.Boolean, true) + ippOutputStream.writeAttributeValue(IppBoolean, true) assertEquals("00 01 01", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueInteger() { - ippOutputStream.writeAttributeValue(IppTag.Integer, 0x12345678) + ippOutputStream.writeAttributeValue(Integer, 0x12345678) assertEquals("00 04 12 34 56 78", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueEnum() { - ippOutputStream.writeAttributeValue(IppTag.Enum, 0x01020304) + ippOutputStream.writeAttributeValue(IppEnum, 0x01020304) assertEquals("00 04 01 02 03 04", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueRangeOfInteger() { - ippOutputStream.writeAttributeValue(IppTag.RangeOfInteger, 16..1024) + ippOutputStream.writeAttributeValue(RangeOfInteger, 16..1024) assertEquals("00 08 00 00 00 10 00 00 04 00", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueResolution() { - ippOutputStream.writeAttributeValue(IppTag.Resolution, IppResolution(600, DPI)) + ippOutputStream.writeAttributeValue(Resolution, IppResolution(600, DPI)) assertEquals("00 09 00 00 02 58 00 00 02 58 03", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueCharset() { - ippOutputStream.writeAttributeValue(IppTag.Charset, Charsets.US_ASCII) + ippOutputStream.writeAttributeValue(Charset, Charsets.US_ASCII) assertEquals("00 08 75 73 2D 61 73 63 69 69", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueUri() { - ippOutputStream.writeAttributeValue(IppTag.Uri, URI.create("ipp://joelle")) + ippOutputStream.writeAttributeValue(Uri, URI.create("ipp://joelle")) assertEquals("00 0C 69 70 70 3A 2F 2F 6A 6F 65 6C 6C 65", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueKeyword() { - ippOutputStream.writeAttributeValue(IppTag.Keyword, "aKeyword") + ippOutputStream.writeAttributeValue(Keyword, "aKeyword") assertEquals("00 08 61 4B 65 79 77 6F 72 64", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueUriScheme() { - ippOutputStream.writeAttributeValue(IppTag.UriScheme, "ipps") + ippOutputStream.writeAttributeValue(UriScheme, "ipps") assertEquals("00 04 69 70 70 73", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueOctetString() { - ippOutputStream.writeAttributeValue(IppTag.OctetString, "anOctetString") + ippOutputStream.writeAttributeValue(OctetString, "anOctetString") assertEquals("00 0D 61 6E 4F 63 74 65 74 53 74 72 69 6E 67", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueMimeMediaType() { - ippOutputStream.writeAttributeValue(IppTag.MimeMediaType, "application/pdf") + ippOutputStream.writeAttributeValue(MimeMediaType, "application/pdf") assertEquals("00 0F 61 70 70 6C 69 63 61 74 69 6F 6E 2F 70 64 66", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueMemberAttrName() { - ippOutputStream.writeAttributeValue(IppTag.MemberAttrName, "a-member-attr-name") + ippOutputStream.writeAttributeValue(MemberAttrName, "a-member-attr-name") assertEquals("00 12 61 2D 6D 65 6D 62 65 72 2D 61 74 74 72 2D 6E 61 6D 65", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueNaturalLanguage() { - ippOutputStream.writeAttributeValue(IppTag.NaturalLanguage, "en-us") + ippOutputStream.writeAttributeValue(NaturalLanguage, "en-us") assertEquals("00 05 65 6E 2D 75 73", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueTextWithoutLanguage() { - ippOutputStream.writeAttributeValue(IppTag.TextWithoutLanguage, IppString("aTextWithoutLanguage")) + ippOutputStream.writeAttributeValue(TextWithoutLanguage, IppString("aTextWithoutLanguage")) assertEquals("00 14 61 54 65 78 74 57 69 74 68 6F 75 74 4C 61 6E 67 75 61 67 65", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueNameWithoutLanguage() { - ippOutputStream.writeAttributeValue(IppTag.NameWithoutLanguage, IppString("aNameWithoutLanguage")) + ippOutputStream.writeAttributeValue(NameWithoutLanguage, IppString("aNameWithoutLanguage")) assertEquals("00 14 61 4E 61 6D 65 57 69 74 68 6F 75 74 4C 61 6E 67 75 61 67 65", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueNameWithoutLanguageFails() { - assertFailsWith { ippOutputStream.writeAttributeValue(IppTag.NameWithoutLanguage, 0) } + assertFailsWith { ippOutputStream.writeAttributeValue(NameWithoutLanguage, 0) } } @Test fun writeAttributeValueTextWithLanguage() { - ippOutputStream.writeAttributeValue(IppTag.TextWithLanguage, IppString("aTextWithLanguage", "en")) + ippOutputStream.writeAttributeValue(TextWithLanguage, IppString("aTextWithLanguage", "en")) assertEquals( "00 17 00 02 65 6E 00 11 61 54 65 78 74 57 69 74 68 4C 61 6E 67 75 61 67 65", byteArrayOutputStream.toHex() @@ -149,7 +155,7 @@ class IppOutputStreamTest { @Test fun writeAttributeValueNameWithLanguage() { - ippOutputStream.writeAttributeValue(IppTag.NameWithLanguage, IppString("einNameMitSprache", "de")) + ippOutputStream.writeAttributeValue(NameWithLanguage, IppString("einNameMitSprache", "de")) assertEquals( "00 17 00 02 64 65 00 11 65 69 6E 4E 61 6D 65 4D 69 74 53 70 72 61 63 68 65", byteArrayOutputStream.toHex() @@ -159,21 +165,21 @@ class IppOutputStreamTest { @Test fun writeAttributeValueNameWithLanguageFails() { assertFailsWith { - ippOutputStream.writeAttributeValue(IppTag.NameWithLanguage, "text-without-language") + ippOutputStream.writeAttributeValue(NameWithLanguage, "text-without-language") } } @Test fun writeAttributeValueDateTime() { - ippOutputStream.writeAttributeValue(IppTag.DateTime, IppDateTime(2007, 3, 15, 2, 15, 37, 0, '+', 1, 0)) + ippOutputStream.writeAttributeValue(DateTime, IppDateTime(2007, 3, 15, 2, 15, 37, 0, '+', 1, 0)) assertEquals("00 0B 07 D7 03 0F 02 0F 25 00 2B 01 00", byteArrayOutputStream.toHex()) } @Test fun writeAttributeValueCollection() { ippOutputStream.writeAttributeValue( - IppTag.BegCollection, - IppCollection(IppAttribute("foo", IppTag.Keyword, "a", "b")) + BegCollection, + IppCollection(IppAttribute("foo", Keyword, "a", "b")) ) assertEquals( "00 00 4A 00 00 00 03 66 6F 6F 44 00 00 00 01 61 44 00 00 00 01 62 37 00 00 00 00", @@ -192,9 +198,9 @@ class IppOutputStreamTest { version = "2.1" code = IppOperation.GetPrinterAttributes.code requestId = 8 - with(createAttributesGroup(IppTag.Job)) { - attribute("1", IppTag.Boolean, true, false) // cover 1setOf - attribute("0", IppTag.NoValue) // cover OutOfBand + with(createAttributesGroup(Job)) { + attribute("1", IppBoolean, true, false) // cover 1setOf + attribute("0", NoValue) // cover OutOfBand } } ippOutputStream.writeMessage(message) @@ -226,20 +232,21 @@ class IppOutputStreamTest { } @Test - fun writeMessageFails() { + fun writeMessageTextWithLanguageFails() { with(message) { version = "2.1" code = IppOperation.GetPrinterAttributes.code requestId = 8 - operationGroup.attribute("foo", IppTag.TextWithLanguage, IppString("text-without-language")) + operationGroup.attribute("foo", TextWithLanguage, IppString("text-without-language")) + } + assertFailsWith { ippOutputStream.writeMessage(message) }.run { + assertEquals("failed to write attribute: foo (textWithLanguage) = text-without-language", message) + assertEquals("expecting IppString with language", cause!!.message) } - assertFailsWith { ippOutputStream.writeMessage(message) } } -} - -// hex utility extensions - -fun ByteArray.toHex() = joinToString(" ") { "%02X".format(it) } + // Hex utility extensions + private fun ByteArray.toHex() = joinToString(" ") { "%02X".format(it) } + private fun ByteArrayOutputStream.toHex() = toByteArray().toHex() -fun ByteArrayOutputStream.toHex() = toByteArray().toHex() \ No newline at end of file +} \ No newline at end of file diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppTagTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppTagTests.kt index 03973a86..4f97bf9f 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppTagTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppTagTests.kt @@ -1,42 +1,149 @@ package de.gmuth.ipp.core /** - * Copyright (c) 2020 Gerhard Muth + * Copyright (c) 2020-2023 Gerhard Muth */ import kotlin.test.* +import de.gmuth.ipp.core.IppTag.* +import java.net.URI class IppTagTests { + @Test + fun validInteger() { + assertFalse(Integer.valueHasValidClass("no-integer")) + assertTrue(Integer.valueHasValidClass(1)) + } + + @Test + fun validBoolean() { + assertFalse(IppTag.Boolean.valueHasValidClass("no-boolean")) + assertTrue(IppTag.Boolean.valueHasValidClass(true)) + } + + @Test + fun validEnum() { + assertFalse(IppTag.Enum.valueHasValidClass("no-enum")) + assertTrue(IppTag.Enum.valueHasValidClass(1)) + } + + @Test + fun validOctetString() { + assertFalse(OctetString.valueHasValidClass(0)) + assertTrue(OctetString.valueHasValidClass("string")) + } + + @Test + fun validDateTime() { + assertFalse(DateTime.valueHasValidClass(0)) + assertTrue(DateTime.valueHasValidClass(IppDateTime.now())) + } + + @Test + fun validResolution() { + assertFalse(Resolution.valueHasValidClass(0)) + assertTrue(Resolution.valueHasValidClass(IppResolution(600))) + } + + @Test + fun validRangeOfInteger() { + assertFalse(RangeOfInteger.valueHasValidClass(0)) + assertTrue(RangeOfInteger.valueHasValidClass(0..1)) + } + + @Test + fun validBegCollection() { + assertFalse(BegCollection.valueHasValidClass(0)) + assertTrue(BegCollection.valueHasValidClass(IppCollection())) + } + @Test fun validateTextWithLanguage() { - assertFalse(IppTag.TextWithoutLanguage.valueHasValidClass(0)) - assertTrue(IppTag.TextWithoutLanguage.valueHasValidClass("string")) + assertFalse(TextWithLanguage.valueHasValidClass(0)) + assertTrue(TextWithLanguage.valueHasValidClass(IppString(""))) } @Test fun validateNameWithLanguage() { - assertFalse(IppTag.NameWithoutLanguage.valueHasValidClass(0)) - assertTrue(IppTag.NameWithoutLanguage.valueHasValidClass("string")) + assertFalse(NameWithLanguage.valueHasValidClass(0)) + assertTrue(NameWithLanguage.valueHasValidClass(IppString(""))) } + @Test + fun validEndCollection() { + assertTrue(EndCollection.valueHasValidClass(0)) + assertTrue(EndCollection.valueHasValidClass("")) + } + + @Test + fun validateTextWithoutLanguage() { + assertFalse(TextWithoutLanguage.valueHasValidClass(0)) + assertTrue(TextWithoutLanguage.valueHasValidClass("string")) + assertTrue(TextWithoutLanguage.valueHasValidClass(IppString("ipp-string"))) + } + + @Test + fun validateNameWithoutLanguage() { + assertFalse(NameWithoutLanguage.valueHasValidClass(0)) + assertTrue(NameWithoutLanguage.valueHasValidClass("string")) + assertTrue(NameWithoutLanguage.valueHasValidClass(IppString("ipp-string"))) + } + + private fun validString(tag: IppTag) = tag.run { + assertFalse(valueHasValidClass(0)) + assertTrue(valueHasValidClass("string")) + } + + @Test + fun validKeyword() = + validString(Keyword) + + @Test + fun validUri() { + assertFalse(Uri.valueHasValidClass(0)) + assertTrue(Uri.valueHasValidClass(URI.create("ipp://0"))) + } + + @Test + fun validUriScheme() = + validString(UriScheme) + + @Test + fun validCharset() { + assertFalse(Charset.valueHasValidClass(0)) + assertTrue(Charset.valueHasValidClass(java.nio.charset.Charset.defaultCharset())) + } + + @Test + fun validNaturalLanguage() = + validString(NaturalLanguage) + + @Test + fun validMimeMediaType() = + validString(MimeMediaType) + + @Test + fun validMemberAttrName() = + validString(MemberAttrName) + @Test fun tagClassification() { - assertFalse(IppTag.Printer.isOutOfBandTag()) - assertFalse(IppTag.Printer.isMemberAttrValue()) - assertFalse(IppTag.MemberAttrName.isMemberAttrValue()) + assertFalse(Printer.isOutOfBandTag()) + assertFalse(Printer.isMemberAttrValue()) + assertFalse(MemberAttrName.isMemberAttrValue()) } @Test fun registeredSyntax() { - assertEquals("name", IppTag.NameWithoutLanguage.registeredSyntax()) - assertEquals("text", IppTag.TextWithoutLanguage.registeredSyntax()) - assertEquals("keyword", IppTag.Keyword.registeredSyntax()) + assertEquals("name", NameWithoutLanguage.registeredSyntax()) + assertEquals("text", TextWithoutLanguage.registeredSyntax()) + assertEquals("keyword", Keyword.registeredSyntax()) } @Test fun fromString() { - assertEquals(IppTag.Uri, IppTag.fromString("uri")) + assertEquals(Uri, IppTag.fromString("uri")) } @Test diff --git a/src/test/resources/logging.properties b/src/test/resources/logging.properties index 3d1345d3..fc483c4f 100644 --- a/src/test/resources/logging.properties +++ b/src/test/resources/logging.properties @@ -34,7 +34,7 @@ handlers=de.gmuth.log.StdoutHandler,java.util.logging.FileHandler #de.gmuth.log.StdoutHandler #de.gmuth.log.ConsoleHandler -de.gmuth.log.StdoutHandler.level=FINER +de.gmuth.log.StdoutHandler.level=ALL #java.util.logging.ConsoleHandler.level=ALL java.util.logging.FileHandler.level=FINER