2022-07-01 18:19:57 +00:00
|
|
|
# Nim-LibP2P
|
2023-01-20 14:47:40 +00:00
|
|
|
# Copyright (c) 2023 Status Research & Development GmbH
|
2022-07-01 18:19:57 +00:00
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
2021-08-18 07:40:12 +00:00
|
|
|
|
2023-06-07 11:12:49 +00:00
|
|
|
{.push raises: [].}
|
2021-08-18 07:40:12 +00:00
|
|
|
|
|
|
|
import
|
2023-05-18 08:24:17 +00:00
|
|
|
std/tables,
|
2021-08-18 07:40:12 +00:00
|
|
|
chronos, chronicles
|
|
|
|
|
|
|
|
import nameresolver
|
|
|
|
|
|
|
|
export tables
|
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "libp2p mockresolver"
|
|
|
|
|
|
|
|
type MockResolver* = ref object of NameResolver
|
|
|
|
txtResponses*: Table[string, seq[string]]
|
|
|
|
# key: address, isipv6?
|
|
|
|
ipResponses*: Table[(string, bool), seq[string]]
|
|
|
|
|
|
|
|
method resolveIp*(
|
|
|
|
self: MockResolver,
|
|
|
|
address: string,
|
|
|
|
port: Port,
|
|
|
|
domain: Domain = Domain.AF_UNSPEC): Future[seq[TransportAddress]] {.async.} =
|
|
|
|
if domain == Domain.AF_INET or domain == Domain.AF_UNSPEC:
|
|
|
|
for resp in self.ipResponses.getOrDefault((address, false)):
|
|
|
|
result.add(initTAddress(resp, port))
|
|
|
|
|
|
|
|
if domain == Domain.AF_INET6 or domain == Domain.AF_UNSPEC:
|
|
|
|
for resp in self.ipResponses.getOrDefault((address, true)):
|
|
|
|
result.add(initTAddress(resp, port))
|
|
|
|
|
|
|
|
method resolveTxt*(
|
|
|
|
self: MockResolver,
|
|
|
|
address: string): Future[seq[string]] {.async.} =
|
|
|
|
return self.txtResponses.getOrDefault(address)
|
|
|
|
|
|
|
|
proc new*(T: typedesc[MockResolver]): T = T()
|