<commit_before>#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <boost/test/unit_test.hpp>

#include "base58.h"
#include "util.h"
#include "main.h"
#include "key.h"

using namespace std;

BOOST_AUTO_TEST_SUITE(darksend_tests)

BOOST_AUTO_TEST_CASE(darksend_sign)
{

    std::string errorMessage = "";
    std::string strBase64;
    CKey key;
    CPubKey pubkey;
    std::vector<unsigned char> vchSig;

    CDarkSendSigner dss;
    dss.SetKey("<KEY>", errorMessage, key, pubkey);
    BOOST_CHECK(dss.SignMessage("hello", errorMessage, vchSig, key) == true);
    BOOST_CHECK(dss.VerifyMessage(pubkey, vchSig, "hello", errorMessage) == true);
    BOOST_CHECK(dss.VerifyMessage(pubkey, vchSig, "hello2", errorMessage) == false);

}
BOOST_AUTO_TEST_CASE(darksend_vote)
{
    CPubKey key;
    CMasterNodeVote mnv;
    mnv.Set(key, 1);
    mnv.Vote();
    BOOST_CHECK(mnv.GetVotes() == 2);
    mnv.Vote();
    BOOST_CHECK(mnv.GetVotes() == 3);
}

BOOST_AUTO_TEST_CASE(darksend_masternode_voting)
{
    uint256 n1 = 10000;
    uint256 n2 = 10001;
    CTxIn fromMn1 = CTxIn(n2, 0);
    CTxIn testVin1 = CTxIn(n1, 0);
    CTxIn testVin2 = CTxIn(n2, 0);
    CService addr;
    int i = 0;
    std::vector<unsigned char> vchSig;

    //setup a couple fake masternodes
    CMasterNode mn1(addr, testVin1, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn1);

    CMasterNode mn2(addr, testVin2, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn2);

    // return -1 if nothing present
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == -1);

    //block 1000
    for(i = 0; i <= 2; i++)
        darkSendPool.SubmitMasternodeVote(testVin1, fromMn1,1000);

    // not enough votes
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == -1); // 

    for(i = 0; i <= 4; i++)
        darkSendPool.SubmitMasternodeVote(testVin2, fromMn1, 1000);

    // not enough votes
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == -1); // 

    for(i = 0; i <= 4; i++)
        darkSendPool.SubmitMasternodeVote(testVin2, fromMn1, 1000);
    
    // should have 8 votes now
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == 1); // vin2
}


BOOST_AUTO_TEST_CASE(darksend_masternode_search_by_vin)
{
    uint256 n1 = 10000;
    uint256 n2 = 10001;
    uint256 n3 = 99999;
    CTxIn testVin1 = CTxIn(n1, 0);
    CTxIn testVin2 = CTxIn(n2, 0);
    CTxIn testVinNotFound = CTxIn(n3, 0);
    CService addr;
    std::vector<unsigned char> vchSig;

    //setup a couple fake masternodes
    CMasterNode mn1(addr, testVin1, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn1);

    CMasterNode mn2(addr, testVin2, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn2);

    BOOST_CHECK(darkSendPool.GetMasternodeByVin(testVinNotFound) == -1);
    BOOST_CHECK(darkSendPool.GetMasternodeByVin(testVin1) == 0);
    BOOST_CHECK(darkSendPool.GetMasternodeByVin(testVin2) == 1);
}

BOOST_AUTO_TEST_CASE(darksend_add_entry)
{
    std::vector<CTxIn> vin;
    vin.push_back( CTxIn(1000, 0) );
    vin.push_back( CTxIn(1001, 0) );

    std::vector<CTxOut> vout;
    vout.push_back( CTxOut(1, CScript()) );
    vout.push_back( CTxOut(1, CScript()) );

    //try added entries
    CDarkSendEntry e;
    BOOST_CHECK(e.sev.size() == 0);
    BOOST_CHECK(e.Add(vin, 1, CTransaction(), vout) == true);
    BOOST_CHECK(e.sev.size() == 2);
    BOOST_CHECK(e.Add(vin, 1, CTransaction(), vout) == false);
    BOOST_CHECK(e.sev.size() == 2);

    //sign one of the vin
    BOOST_CHECK(e.AddSig(CScript(), CScript(), CTxIn(1001, 0)) == true);
    BOOST_CHECK(e.AddSig(CScript(), CScript(), CTxIn(1001, 0)) == false);

    //sign non-existant
    BOOST_CHECK(e.AddSig(CScript(), CScript(), CTxIn(9999001, 0)) == false);

}

BOOST_AUTO_TEST_CASE(darksend_masternode_class)
{
    std::string strPubKey = "<KEY>";
    CPubKey pubkey(ParseHex(strPubKey));

    std::vector<unsigned char> newSig;
    int64 newNow = GetTimeMicros();

    CMasterNode mn(CService("10.10.10.10:9999"), CTxIn(1000, 0), pubkey, newSig, newNow, pubkey);
    mn.UpdateLastSeen();
    mn.Check();
    BOOST_CHECK(mn.enabled == 3); // bad vin
    mn.lastTimeSeen -= MASTERNODE_EXPIRATION_MICROSECONDS;
    mn.Check();
    BOOST_CHECK(mn.enabled == 2); // hasn't pinged
    mn.lastTimeSeen -= MASTERNODE_EXPIRATION_MICROSECONDS;
    mn.Check();
    BOOST_CHECK(mn.enabled == 4); // expired
}

BOOST_AUTO_TEST_CASE(darksend_pool_add_entry)
{
    std::vector<CTxIn> vin;
    vin.push_back( CTxIn(1000, 0) );
    vin.push_back( CTxIn(1001, 0) );

    std::vector<CTxOut> vout;
    vout.push_back( CTxOut(1, CScript()) );
    vout.push_back( CTxOut(1, CScript()) );

    //try added entries
    CDarkSendEntry e;
    BOOST_CHECK(e.Add(vin, 1, CTransaction(), vout) == true);

    // add first entry
    darkSendPool.entries.push_back(e);
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 1);
    darkSendPool.Check();
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 1);
    
    // add second entry
    darkSendPool.entries.push_back(e);
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 2);
    darkSendPool.Check();
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 2);

    // add third entry
    darkSendPool.entries.push_back(e);
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 3);
    darkSendPool.Check();
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_FINALIZE_TRANSACTION);
    BOOST_CHECK(darkSendPool.entries.size() == 3);

}


BOOST_AUTO_TEST_SUITE_END()<commit_msg>unit tests<commit_after>#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <boost/test/unit_test.hpp>

#include "base58.h"
#include "util.h"
#include "main.h"
#include "key.h"
#include "wallet.h"

using namespace std;

typedef set<pair<const CWalletTx*,unsigned int> > CoinSet;

static CWallet wallet;
static std::vector<CTxIn> vCoins;


static void add_coin(int64 nValue, int nAge = 6*24, bool fIsFromMe = false, int nInput=0)
{
    static int nextLockTime = 0;
    CTransaction tx;
    tx.nLockTime = nextLockTime++;        // so all transactions get different hashes
    tx.vout.resize(nInput+1);
    tx.vout[nInput].nValue = nValue;
    CWalletTx* wtx = new CWalletTx(&wallet, tx);
    if (fIsFromMe)
    {
        // IsFromMe() returns (GetDebit() > 0), and GetDebit() is 0 if vin.empty(),
        // so stop vin being empty, and cache a non-zero Debit to fake out IsFromMe()
        wtx->vin.resize(1);
        wtx->fDebitCached = true;
        wtx->nDebitCached = 1;
    }
    COutput output(wtx, nInput, nAge);
    vCoins.push_back(output);
}

static void empty_wallet(void)
{
    BOOST_FOREACH(COutput output, vCoins)
        delete output.tx;
    vCoins.clear();
}


BOOST_AUTO_TEST_SUITE(darksend_tests)

BOOST_AUTO_TEST_CASE(darksend_sign)
{

    std::string errorMessage = "";
    std::string strBase64;
    CKey key;
    CPubKey pubkey;
    std::vector<unsigned char> vchSig;

    CDarkSendSigner dss;
    dss.SetKey("<KEY>", errorMessage, key, pubkey);
    BOOST_CHECK(dss.SignMessage("hello", errorMessage, vchSig, key) == true);
    BOOST_CHECK(dss.VerifyMessage(pubkey, vchSig, "hello", errorMessage) == true);
    BOOST_CHECK(dss.VerifyMessage(pubkey, vchSig, "hello2", errorMessage) == false);

}
BOOST_AUTO_TEST_CASE(darksend_vote)
{
    CPubKey key;
    CMasterNodeVote mnv;
    mnv.Set(key, 1);
    mnv.Vote();
    BOOST_CHECK(mnv.GetVotes() == 2);
    mnv.Vote();
    BOOST_CHECK(mnv.GetVotes() == 3);
}

BOOST_AUTO_TEST_CASE(darksend_masternode_voting)
{
    uint256 n1 = 10000;
    uint256 n2 = 10001;
    CTxIn fromMn1 = CTxIn(n2, 0);
    CTxIn testVin1 = CTxIn(n1, 0);
    CTxIn testVin2 = CTxIn(n2, 0);
    CService addr;
    int i = 0;
    std::vector<unsigned char> vchSig;

    //setup a couple fake masternodes
    CMasterNode mn1(addr, testVin1, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn1);

    CMasterNode mn2(addr, testVin2, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn2);

    // return -1 if nothing present
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == -1);

    //block 1000
    for(i = 0; i <= 2; i++)
        darkSendPool.SubmitMasternodeVote(testVin1, fromMn1,1000);

    // not enough votes
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == -1); // 

    for(i = 0; i <= 4; i++)
        darkSendPool.SubmitMasternodeVote(testVin2, fromMn1, 1000);

    // not enough votes
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == -1); // 

    for(i = 0; i <= 4; i++)
        darkSendPool.SubmitMasternodeVote(testVin2, fromMn1, 1000);
    
    // should have 8 votes now
    BOOST_CHECK(darkSendPool.GetCurrentMasterNodeConsessus(1000) == 1); // vin2
}


BOOST_AUTO_TEST_CASE(darksend_masternode_search_by_vin)
{
    uint256 n1 = 10000;
    uint256 n2 = 10001;
    uint256 n3 = 99999;
    CTxIn testVin1 = CTxIn(n1, 0);
    CTxIn testVin2 = CTxIn(n2, 0);
    CTxIn testVinNotFound = CTxIn(n3, 0);
    CService addr;
    std::vector<unsigned char> vchSig;

    //setup a couple fake masternodes
    CMasterNode mn1(addr, testVin1, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn1);

    CMasterNode mn2(addr, testVin2, CPubKey(), vchSig, 0, CPubKey());
    darkSendMasterNodes.push_back(mn2);

    BOOST_CHECK(darkSendPool.GetMasternodeByVin(testVinNotFound) == -1);
    BOOST_CHECK(darkSendPool.GetMasternodeByVin(testVin1) == 0);
    BOOST_CHECK(darkSendPool.GetMasternodeByVin(testVin2) == 1);
}

BOOST_AUTO_TEST_CASE(darksend_add_entry)
{
    std::vector<CTxIn> vin;
    vin.push_back( CTxIn(1000, 0) );
    vin.push_back( CTxIn(1001, 0) );

    std::vector<CTxOut> vout;
    vout.push_back( CTxOut(1, CScript()) );
    vout.push_back( CTxOut(1, CScript()) );

    //try added entries
    CDarkSendEntry e;
    BOOST_CHECK(e.sev.size() == 0);
    BOOST_CHECK(e.Add(vin, 1, CTransaction(), vout) == true);
    BOOST_CHECK(e.sev.size() == 2);
    BOOST_CHECK(e.Add(vin, 1, CTransaction(), vout) == false);
    BOOST_CHECK(e.sev.size() == 2);

    //sign one of the vin
    BOOST_CHECK(e.AddSig(CScript(), CScript(), CTxIn(1001, 0)) == true);
    BOOST_CHECK(e.AddSig(CScript(), CScript(), CTxIn(1001, 0)) == false);

    //sign non-existant
    BOOST_CHECK(e.AddSig(CScript(), CScript(), CTxIn(9999001, 0)) == false);

}

BOOST_AUTO_TEST_CASE(darksend_masternode_class)
{
    std::string strPubKey = "<KEY>";
    CPubKey pubkey(ParseHex(strPubKey));

    std::vector<unsigned char> newSig;
    int64 newNow = GetTimeMicros();

    CMasterNode mn(CService("10.10.10.10:9999"), CTxIn(1000, 0), pubkey, newSig, newNow, pubkey);
    mn.UpdateLastSeen();
    mn.Check();
    BOOST_CHECK(mn.enabled == 3); // bad vin
    mn.lastTimeSeen -= MASTERNODE_EXPIRATION_MICROSECONDS;
    mn.Check();
    BOOST_CHECK(mn.enabled == 2); // hasn't pinged
    mn.lastTimeSeen -= MASTERNODE_EXPIRATION_MICROSECONDS;
    mn.Check();
    BOOST_CHECK(mn.enabled == 4); // expired
}

BOOST_AUTO_TEST_CASE(darksend_pool_add_entry)
{
    std::vector<CTxIn> vin;
    vin.push_back( CTxIn(1000, 0) );
    vin.push_back( CTxIn(1001, 0) );

    std::vector<CTxOut> vout;
    vout.push_back( CTxOut(1, CScript()) );
    vout.push_back( CTxOut(1, CScript()) );

    //try added entries
    CDarkSendEntry e;
    BOOST_CHECK(e.Add(vin, 1, CTransaction(), vout) == true);

    // add first entry
    darkSendPool.entries.push_back(e);
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 1);
    darkSendPool.Check();
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 1);
    
    // add second entry
    darkSendPool.entries.push_back(e);
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 2);
    darkSendPool.Check();
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 2);

    // add third entry
    darkSendPool.entries.push_back(e);
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_ACCEPTING_ENTRIES);
    BOOST_CHECK(darkSendPool.entries.size() == 3);
    darkSendPool.Check();
    BOOST_CHECK(darkSendPool.state == POOL_STATUS_FINALIZE_TRANSACTION);
    BOOST_CHECK(darkSendPool.entries.size() == 3);

}


BOOST_AUTO_TEST_CASE(darksend_coin_selection)
{
    CCoinControl *coinControl=NULL;
    CoinSet setCoinsRet, setCoinsRet2;
    int64 nValueRet;

    empty_wallet();

    BOOST_CHECK(!wallet.SelectCoinsDark(100*COIN, vCoins, nValueRet, coinControl));

    add_coin(100*CENT, 4);

    BOOST_CHECK(wallet.SelectCoinsDark(100*COIN, vCoins, nValueRet, coinControl));

 

}

BOOST_AUTO_TEST_SUITE_END()
{'fraction_non_alphanumeric': 0.1393118148599269, 'fraction_numerical': 0.03524665042630938, 'mean_word_length': 3.955488494907582, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4710627', 'n_tokens_mistral': 5429, 'n_tokens_neox': 4971, 'n_words': 875}