Skip to content

Commit

Permalink
Integrate with Detekt code analyser; fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Jan 4, 2023
1 parent b7651c0 commit 3720ef0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 13 deletions.
26 changes: 26 additions & 0 deletions detekt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
config:
validation: true
warningsAsErrors: true

complexity:
LongParameterList:
active: false
TooManyFunctions:
active: false

exceptions:
TooGenericExceptionCaught:
active: false
SwallowedException:
active: false

style:
MagicNumber:
active: false
MaxLineLength:
active: false
ReturnCount:
active: false
UnusedPrivateMember:
active: true
allowedNames: '(_|ignored|ctx)'
7 changes: 7 additions & 0 deletions kotlin-mbedtls/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id("org.jetbrains.kotlin.jvm") version "1.7.22"
id("org.jlleitschuh.gradle.ktlint") version "11.0.0"
id("com.adarshr.test-logger") version "3.2.0"
id("io.gitlab.arturbosch.detekt") version "1.22.0"
}

dependencies {
Expand Down Expand Up @@ -35,3 +36,9 @@ tasks.withType<KotlinCompile> {
tasks.test {
useJUnitPlatform()
}

detekt {
source = files("src/main/kotlin")
config = files("../detekt.yml")
buildUponDefaultConfig = true
}
8 changes: 5 additions & 3 deletions kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/MbedtlsApi.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* Copyright (c) 2022-2023 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,8 @@
* limitations under the License.
*/

@file:Suppress("FunctionNaming")

package org.opencoap.ssl

import com.sun.jna.Callback
Expand Down Expand Up @@ -97,8 +99,8 @@ internal object MbedtlsApi {
external fun mbedtls_ssl_cookie_init(cookieCtx: Pointer)
external fun mbedtls_ssl_cookie_free(cookieCtx: Pointer)
external fun mbedtls_ssl_cookie_setup(cookieCtx: Pointer, fRng: Pointer, pRng: Pointer): Int
val mbedtls_ssl_cookie_write = LIB_MBEDTLS.getFunction("mbedtls_ssl_cookie_write")
val mbedtls_ssl_cookie_check = LIB_MBEDTLS.getFunction("mbedtls_ssl_cookie_check")
val mbedtls_ssl_cookie_write: Function = LIB_MBEDTLS.getFunction("mbedtls_ssl_cookie_write")
val mbedtls_ssl_cookie_check: Function = LIB_MBEDTLS.getFunction("mbedtls_ssl_cookie_check")

// -------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* Copyright (c) 2022-2023 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,12 +18,14 @@ package org.opencoap.ssl

import com.sun.jna.Callback
import com.sun.jna.Pointer
import org.slf4j.LoggerFactory
import java.nio.ByteBuffer

internal object ReceiveCallback : Callback {

private var buffer = ThreadLocal<ByteBuffer>()
private var timeout = ThreadLocal<Int>()
private val logger = LoggerFactory.getLogger(javaClass)
private val buffer = ThreadLocal<ByteBuffer>()
private val timeout = ThreadLocal<Int>()

operator fun <T> invoke(buf: ByteBuffer?, readFun: () -> T): T {
this.buffer.set(buf)
Expand Down Expand Up @@ -61,7 +63,7 @@ internal object ReceiveCallback : Callback {
}
} catch (e: Exception) {
// need to catch all exceptions to avoid crashing
e.printStackTrace()
logger.error(e.message, e)
}
return MbedtlsApi.MBEDTLS_ERR_NET_RECV_FAILED
}
Expand All @@ -70,6 +72,7 @@ internal object ReceiveCallback : Callback {
internal typealias SendBytes = (ByteBuffer) -> Unit

internal object SendCallback : Callback {
private val logger = LoggerFactory.getLogger(javaClass)
private val sendFunc = ThreadLocal<SendBytes>()

operator fun <T> invoke(send: SendBytes, run: () -> T): T {
Expand All @@ -95,7 +98,7 @@ internal object SendCallback : Callback {
}
} catch (e: java.lang.Exception) {
// need to catch all exceptions to avoid crashing
e.printStackTrace()
logger.error(e.message, e)
}
return MbedtlsApi.MBEDTLS_ERR_NET_SEND_FAILED
}
Expand Down
2 changes: 2 additions & 0 deletions kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/SslConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,14 @@ class SslConfig(
}

private object NoOpsSetDelayCallback : Callback {
@Suppress("UnusedPrivateMember")
fun callback(data: Pointer?, intermediateMs: Int, finalMs: Int) {
// do nothing
}
}

private object NoOpsGetDelayCallback : Callback {
@Suppress("FunctionOnlyReturningConstant", "UnusedPrivateMember")
fun callback(data: Pointer?): Int {
return 1
}
Expand Down
4 changes: 2 additions & 2 deletions kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/SslContext.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* Copyright (c) 2022-2023 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -64,7 +64,7 @@ class SslHandshakeContext internal constructor(
private val peerAdr: InetSocketAddress,
) : SslContext {
private val logger = LoggerFactory.getLogger(javaClass)
private var startTimestamp: Long = System.currentTimeMillis()
private val startTimestamp: Long = System.currentTimeMillis()
private var stepTimeout: Duration = Duration.ZERO

fun step(send: (ByteBuffer) -> Unit): SslContext {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* Copyright (c) 2022-2023 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
package org.opencoap.ssl

import com.sun.jna.Memory
import java.util.Locale

open class SslException(message: String) : Exception(message) {

Expand All @@ -25,7 +26,7 @@ open class SslException(message: String) : Exception(message) {
if (error == MbedtlsApi.MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) {
return CloseNotifyException
}
return SslException(String.format("%s [-0x%04X]", translateError(error), -error))
return SslException(String.format(Locale.US, "%s [-0x%04X]", translateError(error), -error))
}

internal fun translateError(error: Int): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* Copyright (c) 2022-2023 kotlin-mbedtls contributors (https://github.com/open-coap/kotlin-mbedtls)
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -101,6 +101,7 @@ class DtlsServer private constructor(
// no session, but dtls packet contains CID
cid != null -> {
val copyBuf = buf.copyDirect()
@Suppress("UnsafeCallOnNullableType") // smart casting does not work for lazy delegate
loadSession(cid!!, adr).thenCompose { isLoaded ->
if (isLoaded) {
handleReceived(adr, copyBuf, timeout)
Expand Down

0 comments on commit 3720ef0

Please sign in to comment.