mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-13 11:35:40 +01:00
Merge pull request #599 from torkelrogstad/2019-07-11-cookie
Fix bug where cookie auth threw on isStartedF
This commit is contained in:
commit
8297a709bf
2 changed files with 46 additions and 32 deletions
|
@ -17,6 +17,8 @@ import org.bitcoins.rpc.config.BitcoindAuthCredentials
|
|||
import org.bitcoins.rpc.util.RpcUtil
|
||||
import org.bitcoins.core.config.RegTest
|
||||
import java.net.URI
|
||||
import scala.concurrent.Future
|
||||
import org.scalatest.compatible.Assertion
|
||||
|
||||
class BitcoindInstanceTest extends BitcoindRpcTest {
|
||||
|
||||
|
@ -34,6 +36,24 @@ class BitcoindInstanceTest extends BitcoindRpcTest {
|
|||
pw.close()
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that the client can call the isStartedF method
|
||||
* without throwing and then start
|
||||
*/
|
||||
private def testClientStart(client: BitcoindRpcClient): Future[Assertion] = {
|
||||
clientAccum += client
|
||||
for {
|
||||
firstStarted <- client.isStartedF
|
||||
_ <- client.start()
|
||||
secondStarted <- client.isStartedF
|
||||
|
||||
_ <- client.getBalance
|
||||
} yield {
|
||||
assert(!firstStarted)
|
||||
assert(secondStarted)
|
||||
}
|
||||
}
|
||||
|
||||
behavior of "BitcoindInstance"
|
||||
|
||||
it should "start a bitcoind with cookie based authentication" in {
|
||||
|
@ -49,12 +69,9 @@ class BitcoindInstanceTest extends BitcoindRpcTest {
|
|||
assert(
|
||||
instance.authCredentials
|
||||
.isInstanceOf[BitcoindAuthCredentials.CookieBased])
|
||||
for {
|
||||
cli <- BitcoindRpcTestUtil.startedBitcoindRpcClient(instance,
|
||||
clientAccum =
|
||||
clientAccum)
|
||||
_ <- cli.getBalance
|
||||
} yield succeed
|
||||
|
||||
val cli = new BitcoindRpcClient(instance)
|
||||
testClientStart(cli)
|
||||
}
|
||||
|
||||
it should "start a bitcoind with user and password based authentication" in {
|
||||
|
@ -72,12 +89,7 @@ class BitcoindInstanceTest extends BitcoindRpcTest {
|
|||
assert(
|
||||
instance.authCredentials
|
||||
.isInstanceOf[BitcoindAuthCredentials.PasswordBased])
|
||||
for {
|
||||
cli <- BitcoindRpcTestUtil.startedBitcoindRpcClient(instance,
|
||||
clientAccum =
|
||||
clientAccum)
|
||||
_ <- cli.getBalance
|
||||
} yield succeed
|
||||
testClientStart(new BitcoindRpcClient(instance))
|
||||
}
|
||||
|
||||
// the values in this conf was generated by executing
|
||||
|
@ -112,12 +124,7 @@ class BitcoindInstanceTest extends BitcoindRpcTest {
|
|||
datadir = conf.datadir
|
||||
)
|
||||
|
||||
for {
|
||||
cli <- BitcoindRpcTestUtil.startedBitcoindRpcClient(instance,
|
||||
clientAccum =
|
||||
clientAccum)
|
||||
_ <- cli.getBalance
|
||||
} yield succeed
|
||||
testClientStart(new BitcoindRpcClient(instance))
|
||||
}
|
||||
|
||||
it should "parse a bitcoin.conf file, start bitcoind, mine some blocks and quit" in {
|
||||
|
|
|
@ -175,24 +175,31 @@ trait Client extends BitcoinSLogger {
|
|||
* Checks whether the underlying bitcoind daemon is running
|
||||
*/
|
||||
def isStartedF: Future[Boolean] = {
|
||||
val request = buildRequest(instance, "ping", JsArray.empty)
|
||||
val responseF = sendRequest(request)
|
||||
def tryPing: Future[Boolean] = {
|
||||
val request = buildRequest(instance, "ping", JsArray.empty)
|
||||
val responseF = sendRequest(request)
|
||||
|
||||
val payloadF: Future[JsValue] = responseF.flatMap(getPayload)
|
||||
val payloadF: Future[JsValue] = responseF.flatMap(getPayload)
|
||||
|
||||
// Ping successful if no error can be parsed from the payload
|
||||
val parsedF = payloadF.map { payload =>
|
||||
(payload \ errorKey).validate[RpcError] match {
|
||||
case _: JsSuccess[RpcError] => false
|
||||
case _: JsError => true
|
||||
// Ping successful if no error can be parsed from the payload
|
||||
val parsedF = payloadF.map { payload =>
|
||||
(payload \ errorKey).validate[RpcError] match {
|
||||
case _: JsSuccess[RpcError] => false
|
||||
case _: JsError => true
|
||||
}
|
||||
}
|
||||
|
||||
parsedF.recover {
|
||||
case exc: StreamTcpException
|
||||
if exc.getMessage.contains("Connection refused") =>
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
parsedF.recover {
|
||||
case exc: StreamTcpException
|
||||
if exc.getMessage.contains("Connection refused") =>
|
||||
false
|
||||
|
||||
instance.authCredentials match {
|
||||
case cookie: CookieBased if Files.notExists(cookie.cookiePath) =>
|
||||
// if the cookie file doesn't exist we're not started
|
||||
Future.successful(false)
|
||||
case (CookieBased(_, _) | PasswordBased(_, _)) => tryPing
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue