1#!/usr/bin/env python3
2# Copyright (c) 2019 The Bitcoin Core developers
3# Distributed under the MIT software license, see the accompanying
4# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5"""Test RPC misc output."""
6import xml.etree.ElementTree as ET
7
8from test_framework.test_framework import BitcoinTestFramework
9from test_framework.util import (
10    assert_raises_rpc_error,
11    assert_equal,
12    assert_greater_than,
13    assert_greater_than_or_equal,
14)
15
16from test_framework.authproxy import JSONRPCException
17
18
19class RpcMiscTest(BitcoinTestFramework):
20    def set_test_params(self):
21        self.num_nodes = 1
22
23    def run_test(self):
24        node = self.nodes[0]
25
26        self.log.info("test getmemoryinfo")
27        memory = node.getmemoryinfo()['locked']
28        assert_greater_than(memory['used'], 0)
29        assert_greater_than(memory['free'], 0)
30        assert_greater_than(memory['total'], 0)
31        # assert_greater_than_or_equal() for locked in case locking pages failed at some point
32        assert_greater_than_or_equal(memory['locked'], 0)
33        assert_greater_than(memory['chunks_used'], 0)
34        assert_greater_than(memory['chunks_free'], 0)
35        assert_equal(memory['used'] + memory['free'], memory['total'])
36
37        self.log.info("test mallocinfo")
38        try:
39            mallocinfo = node.getmemoryinfo(mode="mallocinfo")
40            self.log.info('getmemoryinfo(mode="mallocinfo") call succeeded')
41            tree = ET.fromstring(mallocinfo)
42            assert_equal(tree.tag, 'malloc')
43        except JSONRPCException:
44            self.log.info('getmemoryinfo(mode="mallocinfo") not available')
45            assert_raises_rpc_error(-8, 'mallocinfo is only available when compiled with glibc 2.10+', node.getmemoryinfo, mode="mallocinfo")
46
47        assert_raises_rpc_error(-8, "unknown mode foobar", node.getmemoryinfo, mode="foobar")
48
49if __name__ == '__main__':
50    RpcMiscTest().main()
51