init
This commit is contained in:
65 files changed
+5782
No files matched your search
@@ -0,0 +1,66 @@
|
||||
package com.androidsmsgateway.security
|
||||
|
||||
import com.androidsmsgateway.domain.GatewaySettings
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertNotEquals
|
||||
import org.junit.Test
|
||||
|
||||
class SecurityPolicyTest {
|
||||
@Test
|
||||
fun validBearerKeyIsAccepted() {
|
||||
val settings = settings(apiKeyHash = SecurityPolicy.hashApiKey("a-secure-test-key"))
|
||||
val result = SecurityPolicy.evaluate(
|
||||
settings,
|
||||
clientIp = "192.168.1.10",
|
||||
headers = mapOf("authorization" to "Bearer a-secure-test-key"),
|
||||
)
|
||||
assertEquals(AccessDecision.ALLOWED, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun missingAndInvalidKeysAreDifferentStableDecisions() {
|
||||
val settings = settings(apiKeyHash = SecurityPolicy.hashApiKey("a-secure-test-key"))
|
||||
assertEquals(AccessDecision.MISSING_CREDENTIALS, SecurityPolicy.evaluate(settings, "127.0.0.1", emptyMap()))
|
||||
assertEquals(
|
||||
AccessDecision.INVALID_CREDENTIALS,
|
||||
SecurityPolicy.evaluate(settings, "127.0.0.1", mapOf("X-API-Key" to "wrong-key")),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun allowlistRejectsAddressBeforeAuthentication() {
|
||||
val settings = settings(
|
||||
apiKeyHash = SecurityPolicy.hashApiKey("a-secure-test-key"),
|
||||
allowlistEnabled = true,
|
||||
allowedIps = setOf("10.0.0.5"),
|
||||
)
|
||||
assertEquals(
|
||||
AccessDecision.IP_DENIED,
|
||||
SecurityPolicy.evaluate(settings, "10.0.0.6", mapOf("X-API-Key" to "a-secure-test-key")),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hashesDoNotRevealOriginalKey() {
|
||||
val hash = SecurityPolicy.hashApiKey("a-secure-test-key")
|
||||
assertNotEquals("a-secure-test-key", hash)
|
||||
assertEquals(64, hash.length)
|
||||
}
|
||||
|
||||
private fun settings(
|
||||
apiKeyHash: String,
|
||||
allowlistEnabled: Boolean = false,
|
||||
allowedIps: Set<String> = emptySet(),
|
||||
) = GatewaySettings(
|
||||
deviceId = "test-device",
|
||||
port = 8080,
|
||||
bindAddress = "127.0.0.1",
|
||||
serverEnabled = false,
|
||||
startOnBoot = false,
|
||||
authenticationEnabled = true,
|
||||
apiKeyHash = apiKeyHash,
|
||||
allowlistEnabled = allowlistEnabled,
|
||||
allowedIps = allowedIps,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.androidsmsgateway.server
|
||||
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class ApiContractValidationTest {
|
||||
@Test
|
||||
fun phoneValidationAcceptsInternationalAndLocalDigits() {
|
||||
assertTrue(ApiRouter.PHONE_REGEX.matches("+989121234567"))
|
||||
assertTrue(ApiRouter.PHONE_REGEX.matches("09121234567"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun phoneValidationRejectsFormattingAndInjectionCharacters() {
|
||||
assertFalse(ApiRouter.PHONE_REGEX.matches("+98 912 123 4567"))
|
||||
assertFalse(ApiRouter.PHONE_REGEX.matches("+98912;DROP"))
|
||||
assertFalse(ApiRouter.PHONE_REGEX.matches("12"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun requestLimitsRemainBounded() {
|
||||
assertTrue(ApiRouter.MAX_BODY_BYTES <= 16_384)
|
||||
assertEquals(160, ApiRouter.MAX_SMS_CHARACTERS)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.androidsmsgateway.sms
|
||||
|
||||
import android.app.Activity
|
||||
import android.telephony.SmsManager
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Test
|
||||
|
||||
class SmsStatusReceiverTest {
|
||||
@Test
|
||||
fun `activity result ok is successful`() {
|
||||
assertTrue(SmsStatusReceiver.isSuccessfulResult(Activity.RESULT_OK))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sms manager no error is successful`() {
|
||||
assertTrue(SmsStatusReceiver.isSuccessfulResult(SmsManager.RESULT_ERROR_NONE))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `sms manager error is not successful`() {
|
||||
assertFalse(SmsStatusReceiver.isSuccessfulResult(SmsManager.RESULT_ERROR_GENERIC_FAILURE))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user