/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.systemconfig
import android.content.Context
import android.util.Xml
import androidx.test.platform.app.InstrumentationRegistry
import com.android.server.SystemConfig
import com.google.common.truth.Truth.assertThat
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
class SystemConfigNamedActorTest {
companion object {
private const val NAMESPACE_TEST = "someTestNamespace"
private const val NAMESPACE_ANDROID = "android"
private const val ACTOR_ONE = "iconShaper"
private const val ACTOR_TWO = "colorChanger"
private const val PACKAGE_ONE = "com.test.actor.one"
private const val PACKAGE_TWO = "com.test.actor.two"
}
private val context: Context = InstrumentationRegistry.getInstrumentation().context
@get:Rule
val tempFolder = TemporaryFolder(context.filesDir)
private var uniqueCounter = 0
@Test
fun twoUnique() {
"""
""".write()
assertPermissions().containsExactlyEntriesIn(
mapOf(
NAMESPACE_TEST to mapOf(
ACTOR_ONE to PACKAGE_ONE,
ACTOR_TWO to PACKAGE_TWO
)
)
)
}
@Test
fun twoSamePackage() {
"""
""".write()
assertPermissions().containsExactlyEntriesIn(
mapOf(
NAMESPACE_TEST to mapOf(
ACTOR_ONE to PACKAGE_ONE,
ACTOR_TWO to PACKAGE_ONE
)
)
)
}
@Test
fun missingNamespace() {
"""
""".write()
assertPermissions().containsExactlyEntriesIn(
mapOf(
NAMESPACE_TEST to mapOf(
ACTOR_TWO to PACKAGE_TWO
)
)
)
}
@Test
fun missingName() {
"""
""".write()
assertPermissions().containsExactlyEntriesIn(
mapOf(
NAMESPACE_TEST to mapOf(
ACTOR_TWO to PACKAGE_TWO
)
)
)
}
@Test
fun missingPackage() {
"""
""".write()
assertPermissions().containsExactlyEntriesIn(
mapOf(
NAMESPACE_TEST to mapOf(
ACTOR_TWO to PACKAGE_TWO
)
)
)
}
@Test
fun androidNamespaceThrows() {
"""
""".write()
val exc = assertThrows(IllegalStateException::class.java) { assertPermissions() }
assertEquals(exc.message, "Defining $ACTOR_ONE as $PACKAGE_ONE " +
"for the android namespace is not allowed")
}
@Test
fun duplicateActorNameThrows() {
"""
""".write()
val exc = assertThrows(IllegalStateException::class.java) { assertPermissions() }
assertEquals(exc.message, "Duplicate actor definition for $NAMESPACE_TEST/$ACTOR_ONE;" +
" defined as both $PACKAGE_ONE and $PACKAGE_TWO")
}
private fun String.write() = tempFolder.root.resolve("${uniqueCounter++}.xml")
.writeText(this.trimIndent())
private fun assertPermissions() = SystemConfig(false).apply {
val parser = Xml.newPullParser()
readPermissions(parser, tempFolder.root, 0)
}.let { assertThat(it.namedActors) }
}