LoginCommandTest.kt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2019-2022 Mamoe Technologies and contributors.
  3. *
  4. * 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
  5. * Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
  6. *
  7. * https://github.com/mamoe/mirai/blob/dev/LICENSE
  8. */
  9. @file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
  10. package net.mamoe.mirai.console.command
  11. import kotlinx.coroutines.CompletableDeferred
  12. import kotlinx.coroutines.test.runTest
  13. import net.mamoe.mirai.Bot
  14. import net.mamoe.mirai.console.command.CommandManager.INSTANCE.register
  15. import net.mamoe.mirai.console.command.descriptor.ExperimentalCommandDescriptors
  16. import net.mamoe.mirai.console.internal.command.builtin.LoginCommandImpl
  17. import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig
  18. import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account
  19. import net.mamoe.mirai.console.internal.data.builtins.AutoLoginConfig.Account.PasswordKind
  20. import net.mamoe.mirai.internal.QQAndroidBot
  21. import net.mamoe.mirai.utils.BotConfiguration
  22. import net.mamoe.mirai.utils.md5
  23. import net.mamoe.mirai.utils.toUHexString
  24. import kotlin.test.Test
  25. import kotlin.test.assertContentEquals
  26. import kotlin.test.assertEquals
  27. import kotlin.test.assertNotNull
  28. @OptIn(ExperimentalCommandDescriptors::class)
  29. internal class LoginCommandTest : AbstractCommandTest() {
  30. @Test
  31. fun `login with provided password`() = runTest {
  32. val myId = 123L
  33. val myPwd = "password001"
  34. val bot = awaitDeferred { cont ->
  35. val command = object : LoginCommandImpl() {
  36. override suspend fun doLogin(bot: Bot) {
  37. cont.complete(bot as QQAndroidBot)
  38. }
  39. }
  40. command.register(true)
  41. command.execute(consoleSender, "$myId $myPwd")
  42. }
  43. val account = bot.account
  44. assertContentEquals(myPwd.md5(), account.passwordMd5)
  45. assertEquals(myId, account.id)
  46. }
  47. @Test
  48. fun `login with saved plain password`() = runTest {
  49. val myId = 123L
  50. val myPwd = "password001"
  51. dataScope.set(AutoLoginConfig().apply {
  52. accounts.add(
  53. Account(
  54. account = myId.toString(),
  55. password = Account.Password(PasswordKind.PLAIN, myPwd)
  56. )
  57. )
  58. })
  59. val bot = awaitDeferred { cont ->
  60. val command = object : LoginCommandImpl() {
  61. override suspend fun doLogin(bot: Bot) {
  62. cont.complete(bot as QQAndroidBot)
  63. }
  64. }
  65. command.register(true)
  66. command.execute(consoleSender, "$myId")
  67. }
  68. val account = bot.account
  69. assertContentEquals(myPwd.md5(), account.passwordMd5)
  70. assertEquals(myId, account.id)
  71. }
  72. @Test
  73. fun `login with saved md5 password`() = runTest {
  74. val myId = 123L
  75. val myPwd = "password001"
  76. dataScope.set(AutoLoginConfig().apply {
  77. accounts.add(
  78. Account(
  79. account = myId.toString(),
  80. password = Account.Password(PasswordKind.MD5, myPwd.md5().toUHexString(""))
  81. )
  82. )
  83. })
  84. val bot = awaitDeferred<QQAndroidBot> { cont ->
  85. val command = object : LoginCommandImpl() {
  86. override suspend fun doLogin(bot: Bot) {
  87. cont.complete(bot as QQAndroidBot)
  88. }
  89. }
  90. command.register(true)
  91. command.execute(consoleSender, "$myId")
  92. }
  93. val account = bot.account
  94. assertContentEquals(myPwd.md5(), account.passwordMd5)
  95. assertEquals(myId, account.id)
  96. }
  97. @Test
  98. fun `login with saved configuration`() = runTest {
  99. val myId = 123L
  100. val myPwd = "password001"
  101. dataScope.set(AutoLoginConfig().apply {
  102. accounts.add(
  103. Account(
  104. account = myId.toString(),
  105. password = Account.Password(PasswordKind.MD5, myPwd.md5().toUHexString("")),
  106. configuration = mapOf(
  107. Account.ConfigurationKey.protocol to BotConfiguration.MiraiProtocol.ANDROID_PAD.name,
  108. Account.ConfigurationKey.device to "device.new.json",
  109. Account.ConfigurationKey.heartbeatStrategy to BotConfiguration.HeartbeatStrategy.REGISTER.name
  110. )
  111. )
  112. )
  113. })
  114. val bot = awaitDeferred<QQAndroidBot> { cont ->
  115. val command = object : LoginCommandImpl() {
  116. override suspend fun doLogin(bot: Bot) {
  117. cont.complete(bot as QQAndroidBot)
  118. }
  119. }
  120. command.register(true)
  121. command.execute(consoleSender, "$myId")
  122. }
  123. val configuration = bot.configuration
  124. assertEquals(BotConfiguration.MiraiProtocol.ANDROID_PAD, configuration.protocol)
  125. assertEquals(BotConfiguration.HeartbeatStrategy.REGISTER, configuration.heartbeatStrategy)
  126. assertNotNull(configuration.deviceInfo)
  127. }
  128. }
  129. @BuilderInference
  130. internal suspend inline fun <T> awaitDeferred(
  131. @BuilderInference
  132. crossinline block: suspend (CompletableDeferred<T>) -> Unit
  133. ): T {
  134. val deferred = CompletableDeferred<T>()
  135. block(deferred)
  136. return deferred.await()
  137. }