<commit_before>extern crate indy;

// Workaround to share some utils code based on indy sdk types between tests and indy sdk
use indy::api as api;

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate rust_base58;
#[macro_use]
extern crate serde_derive;
extern crate zmq_pw as zmq;

use std::sync::mpsc::channel;
use std::thread;

use indy::api::ErrorCode;

#[macro_use]
mod utils;

use utils::agent::AgentUtils;
use utils::ledger::LedgerUtils;
use utils::pool::PoolUtils;
use utils::signus::SignusUtils;
use utils::test::TestUtils;
use utils::timeout::TimeoutUtils;
use utils::wallet::WalletUtils;

use std::sync::mpsc::RecvTimeoutError;

static ENDPOINT: &str = "127.0.0.1:9700";
static TRUSTEE_SEED: &str = "000000000000000000000000Trustee1";

mod high_cases {
    use super::*;

    #[test]
    fn indy_agent_listen_works_with_indy_agent_connect() {
        TestUtils::cleanup_storage();
        
        let wallet_handle = WalletUtils::create_and_open_wallet("pool3", None).unwrap();
        let (did, ver_key, pub_key): (String, String, String) = SignusUtils::create_and_store_my_did(wallet_handle, None).unwrap();

        let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
        AgentUtils::add_identity(listener_handle, -1, wallet_handle, did.as_str()).unwrap();

        SignusUtils::store_their_did_from_parts(wallet_handle, did.as_str(), pub_key.as_str(), ver_key.as_str(), ENDPOINT).unwrap();

        AgentUtils::connect(0, wallet_handle, did.as_str(), did.as_str(), None).unwrap();

        AgentUtils::close_listener(listener_handle).unwrap();
        WalletUtils::close_wallet(wallet_handle).unwrap();

        TestUtils::cleanup_storage();
    }

    mod indy_agent_connect {
        use super::*;
        use rust_base58::FromBase58;

        #[test]
        fn indy_agent_connect_works_for_remote_data() {
            TestUtils::cleanup_storage();

            let pool_handle = PoolUtils::create_and_open_pool_ledger("pool_1").unwrap();

            let listener_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();
            let trustee_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();
            let (listener_did, listener_ver_key, listener_pub_key) = SignusUtils::create_and_store_my_did(listener_wallet, None).unwrap();
            let (trustee_did, _, _) = SignusUtils::create_and_store_my_did(trustee_wallet, Some(TRUSTEE_SEED)).unwrap();
            let sender_did = trustee_did.clone();
            let sender_wallet = trustee_wallet;

            let listener_nym_json = LedgerUtils::build_nym_request(trustee_did.as_str(), listener_did.as_str(), Some(listener_ver_key.as_str()), None, None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, trustee_wallet, trustee_did.as_str(), listener_nym_json.as_str()).unwrap();

            let listener_attrib_json =
                LedgerUtils::build_attrib_request(listener_did.as_str(), listener_did.as_str(), None,
                                                  Some(format!("{{\"endpoint\":{{\"ha\":\"{}\", \"verkey\":\"{}\"}}}}", ENDPOINT, listener_pub_key).as_str()),
                                                  None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, listener_wallet, listener_did.as_str(), listener_attrib_json.as_str()).unwrap();

            let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
            AgentUtils::add_identity(listener_handle, pool_handle, listener_wallet, listener_did.as_str()).unwrap();

            AgentUtils::connect(pool_handle, sender_wallet, sender_did.as_str(), listener_did.as_str(), None).unwrap();

            AgentUtils::close_listener(listener_handle).unwrap();
            WalletUtils::close_wallet(listener_wallet).unwrap();
            WalletUtils::close_wallet(trustee_wallet).unwrap();
            PoolUtils::close(pool_handle).unwrap();

            TestUtils::cleanup_storage();
        }

        #[test]
        fn indy_agent_connect_works_for_all_data_in_wallet_present() {
            TestUtils::cleanup_storage();

            let wallet_handle = WalletUtils::create_and_open_wallet("pool1", None).expect("create wallet");

            let (did, ver_key, pub_key) = SignusUtils::create_and_store_my_did(wallet_handle, Some("sovrin_agent_connect_works_for_a")).unwrap();

            SignusUtils::store_their_did_from_parts(wallet_handle, did.as_str(), pub_key.as_str(), ver_key.as_str(), ENDPOINT).unwrap();

            //FIXME temporary code: replace by indy_agent_listen
            thread::spawn(move || {
                let secret_key = "<KEY>();
                let public_key = "<KEY>();
                let socket: zmq::Socket = zmq::Context::new().socket(zmq::SocketType::ROUTER).unwrap();
                socket.set_curve_server(true).unwrap();
                socket.add_curve_keypair([public_key, secret_key].concat().as_slice()).unwrap();
                socket.bind(format!("tcp://{}", ENDPOINT).as_str()).unwrap();
                socket.poll(zmq::POLLIN, -1).unwrap();
                let identity = socket.recv_string(zmq::DONTWAIT).unwrap().unwrap();
                let msg = socket.recv_string(zmq::DONTWAIT).unwrap().unwrap();
                info!("Fake agent socket - recv - from {}, msg {}", identity, msg);
                if msg.eq(r#"{"did":{"sender_did":"L1Xk2qCV6uxEEsYhP7B4EP","receiver_did":"L1Xk2qCV6uxEEsYhP7B4EP"}}"#) {
                    info!("Fake agent socket send ACK");
                    socket.send_multipart(&[identity.as_bytes(), "DID_ACK".as_bytes()], zmq::DONTWAIT).unwrap();
                }
            });
            //FIXME /temporary code

            AgentUtils::connect(0, wallet_handle, did.as_str(), did.as_str(), None).unwrap();

            WalletUtils::close_wallet(wallet_handle).unwrap();

            TestUtils::cleanup_storage();
        }
    }

    mod indy_agent_listen {
        use super::*;

        #[test]
        fn indy_agent_listen_works_for_all_data_in_wallet_present() {
            TestUtils::cleanup_storage();

            let pool_handle = PoolUtils::create_and_open_pool_ledger("pool_1").unwrap();

            let listener_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();
            let sender_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();

            let (listener_did, listener_verkey, listener_pk) = SignusUtils::create_and_store_my_did(listener_wallet, Some(TRUSTEE_SEED)).unwrap();
            let (sender_did, sender_ver_key, sender_pub_key) = SignusUtils::create_and_store_my_did(sender_wallet, None).unwrap();

            SignusUtils::store_their_did_from_parts(listener_wallet, sender_did.as_str(), sender_pub_key.as_str(), sender_ver_key.as_str(), ENDPOINT).unwrap();

            let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
            AgentUtils::add_identity(listener_handle, pool_handle, listener_wallet, listener_did.as_str()).unwrap();

            SignusUtils::store_their_did_from_parts(sender_wallet, listener_did.as_str(), listener_pk.as_str(), listener_verkey.as_str(), ENDPOINT).unwrap();

            AgentUtils::connect(pool_handle, sender_wallet, sender_did.as_str(), listener_did.as_str(), None).unwrap();

            AgentUtils::close_listener(listener_handle).unwrap();
            WalletUtils::close_wallet(listener_wallet).unwrap();
            WalletUtils::close_wallet(sender_wallet).unwrap();
            PoolUtils::close(pool_handle).unwrap();

            TestUtils::cleanup_storage();
        }

        #[test]
        fn indy_agent_listen_works_for_get_sender_data_from_ledger() {
            TestUtils::cleanup_storage();

            let pool_handle = PoolUtils::create_and_open_pool_ledger("pool_1").unwrap();

            let trustee_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();
            let sender_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();

            let (trustee_did, trustee_verkey, trustee_pk) = SignusUtils::create_and_store_my_did(trustee_wallet, Some(TRUSTEE_SEED)).unwrap();
            let (sender_did, sender_ver_key, sender_pub_key) = SignusUtils::create_and_store_my_did(sender_wallet, None).unwrap();
            let (listener_did, listener_verkey, listener_pk) = (trustee_did.clone(), trustee_verkey.clone(), trustee_pk.clone());
            let listener_wallet = trustee_wallet;

            let sender_nym_json = LedgerUtils::build_nym_request(trustee_did.as_str(), sender_did.as_str(),
                                                                 Some(sender_ver_key.as_str()), None, None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, trustee_wallet, trustee_did.as_str(), sender_nym_json.as_str()).unwrap();

            let sender_attrib_json =
                LedgerUtils::build_attrib_request(sender_did.as_str(), sender_did.as_str(), None,
                                                  Some(format!("{{\"endpoint\":{{\"ha\":\"{}\", \"verkey\":\"{}\"}}}}", ENDPOINT, sender_pub_key).as_str()),
                                                  None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, sender_wallet, sender_did.as_str(), sender_attrib_json.as_str()).unwrap();

            let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
            AgentUtils::add_identity(listener_handle, pool_handle, listener_wallet, listener_did.as_str()).unwrap();

            SignusUtils::store_their_did_from_parts(sender_wallet, listener_did.as_str(), listener_pk.as_str(), listener_verkey.as_str(), ENDPOINT).unwrap();


            AgentUtils::connect(pool_handle, sender_wallet, sender_did.as_str(), listener_did.as_str(), None).unwrap();

            AgentUtils::close_listener(listener_handle).unwrap();
            WalletUtils::close_wallet(listener_wallet).unwrap();
            WalletUtils::close_wallet(sender_wallet).unwrap();
            PoolUtils::close(pool_handle).unwrap();

            TestUtils::cleanup_storage();
        }

        #[test]
        fn indy_agent_listen_works_for_passed_on_connect_callback() {
            TestUtils::cleanup_storage();
<commit_msg>Fixed compile error<commit_after>extern crate indy;

// Workaround to share some utils code based on indy sdk types between tests and indy sdk
use indy::api as api;

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate rust_base58;
#[macro_use]
extern crate serde_derive;
extern crate zmq_pw as zmq;

use std::sync::mpsc::channel;
use std::thread;

use indy::api::ErrorCode;

#[macro_use]
mod utils;

use utils::agent::AgentUtils;
use utils::ledger::LedgerUtils;
use utils::pool::PoolUtils;
use utils::signus::SignusUtils;
use utils::test::TestUtils;
use utils::timeout::TimeoutUtils;
use utils::wallet::WalletUtils;

use std::sync::mpsc::RecvTimeoutError;

static ENDPOINT: &'static str = "127.0.0.1:9700";
static TRUSTEE_SEED: &'static str = "000000000000000000000000Trustee1";

mod high_cases {
    use super::*;

    #[test]
    fn indy_agent_listen_works_with_indy_agent_connect() {
        TestUtils::cleanup_storage();
        
        let wallet_handle = WalletUtils::create_and_open_wallet("pool3", None).unwrap();
        let (did, ver_key, pub_key): (String, String, String) = SignusUtils::create_and_store_my_did(wallet_handle, None).unwrap();

        let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
        AgentUtils::add_identity(listener_handle, -1, wallet_handle, did.as_str()).unwrap();

        SignusUtils::store_their_did_from_parts(wallet_handle, did.as_str(), pub_key.as_str(), ver_key.as_str(), ENDPOINT).unwrap();

        AgentUtils::connect(0, wallet_handle, did.as_str(), did.as_str(), None).unwrap();

        AgentUtils::close_listener(listener_handle).unwrap();
        WalletUtils::close_wallet(wallet_handle).unwrap();

        TestUtils::cleanup_storage();
    }

    mod indy_agent_connect {
        use super::*;
        use rust_base58::FromBase58;

        #[test]
        fn indy_agent_connect_works_for_remote_data() {
            TestUtils::cleanup_storage();

            let pool_handle = PoolUtils::create_and_open_pool_ledger("pool_1").unwrap();

            let listener_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();
            let trustee_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();

            let (listener_did, listener_ver_key, listener_pub_key) = SignusUtils::create_and_store_my_did(listener_wallet, None).unwrap();
            let (trustee_did, _, _) = SignusUtils::create_and_store_my_did(trustee_wallet, Some(TRUSTEE_SEED)).unwrap();
            let sender_did = trustee_did.clone();
            let sender_wallet = trustee_wallet;

            let listener_nym_json = LedgerUtils::build_nym_request(trustee_did.as_str(), listener_did.as_str(), Some(listener_ver_key.as_str()), None, None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, trustee_wallet, trustee_did.as_str(), listener_nym_json.as_str()).unwrap();

            let listener_attrib_json =
                LedgerUtils::build_attrib_request(listener_did.as_str(), listener_did.as_str(), None,
                                                  Some(format!("{{\"endpoint\":{{\"ha\":\"{}\", \"verkey\":\"{}\"}}}}", ENDPOINT, listener_pub_key).as_str()),
                                                  None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, listener_wallet, listener_did.as_str(), listener_attrib_json.as_str()).unwrap();

            let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
            AgentUtils::add_identity(listener_handle, pool_handle, listener_wallet, listener_did.as_str()).unwrap();

            AgentUtils::connect(pool_handle, sender_wallet, sender_did.as_str(), listener_did.as_str(), None).unwrap();

            AgentUtils::close_listener(listener_handle).unwrap();
            WalletUtils::close_wallet(listener_wallet).unwrap();
            WalletUtils::close_wallet(trustee_wallet).unwrap();
            PoolUtils::close(pool_handle).unwrap();

            TestUtils::cleanup_storage();
        }

        #[test]
        fn indy_agent_connect_works_for_all_data_in_wallet_present() {
            TestUtils::cleanup_storage();

            let wallet_handle = WalletUtils::create_and_open_wallet("pool1", None).expect("create wallet");

            let (did, ver_key, pub_key) = SignusUtils::create_and_store_my_did(wallet_handle, Some("sovrin_agent_connect_works_for_a")).unwrap();

            SignusUtils::store_their_did_from_parts(wallet_handle, did.as_str(), pub_key.as_str(), ver_key.as_str(), ENDPOINT).unwrap();

            //FIXME temporary code: replace by indy_agent_listen
            thread::spawn(move || {
                let secret_key = "<KEY>();
                let public_key = "<KEY>();
                let socket: zmq::Socket = zmq::Context::new().socket(zmq::SocketType::ROUTER).unwrap();
                socket.set_curve_server(true).unwrap();
                socket.add_curve_keypair([public_key, secret_key].concat().as_slice()).unwrap();
                socket.bind(format!("tcp://{}", ENDPOINT).as_str()).unwrap();
                socket.poll(zmq::POLLIN, -1).unwrap();
                let identity = socket.recv_string(zmq::DONTWAIT).unwrap().unwrap();
                let msg = socket.recv_string(zmq::DONTWAIT).unwrap().unwrap();
                info!("Fake agent socket - recv - from {}, msg {}", identity, msg);
                if msg.eq(r#"{"did":{"sender_did":"L1Xk2qCV6uxEEsYhP7B4EP","receiver_did":"L1Xk2qCV6uxEEsYhP7B4EP"}}"#) {
                    info!("Fake agent socket send ACK");
                    socket.send_multipart(&[identity.as_bytes(), "DID_ACK".as_bytes()], zmq::DONTWAIT).unwrap();
                }
            });
            //FIXME /temporary code

            AgentUtils::connect(0, wallet_handle, did.as_str(), did.as_str(), None).unwrap();

            WalletUtils::close_wallet(wallet_handle).unwrap();

            TestUtils::cleanup_storage();
        }
    }

    mod indy_agent_listen {
        use super::*;

        #[test]
        fn indy_agent_listen_works_for_all_data_in_wallet_present() {
            TestUtils::cleanup_storage();

            let pool_handle = PoolUtils::create_and_open_pool_ledger("pool_1").unwrap();

            let listener_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();
            let sender_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();

            let (listener_did, listener_verkey, listener_pk) = SignusUtils::create_and_store_my_did(listener_wallet, Some(TRUSTEE_SEED)).unwrap();
            let (sender_did, sender_ver_key, sender_pub_key) = SignusUtils::create_and_store_my_did(sender_wallet, None).unwrap();

            SignusUtils::store_their_did_from_parts(listener_wallet, sender_did.as_str(), sender_pub_key.as_str(), sender_ver_key.as_str(), ENDPOINT).unwrap();

            let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
            AgentUtils::add_identity(listener_handle, pool_handle, listener_wallet, listener_did.as_str()).unwrap();

            SignusUtils::store_their_did_from_parts(sender_wallet, listener_did.as_str(), listener_pk.as_str(), listener_verkey.as_str(), ENDPOINT).unwrap();

            AgentUtils::connect(pool_handle, sender_wallet, sender_did.as_str(), listener_did.as_str(), None).unwrap();

            AgentUtils::close_listener(listener_handle).unwrap();
            WalletUtils::close_wallet(listener_wallet).unwrap();
            WalletUtils::close_wallet(sender_wallet).unwrap();
            PoolUtils::close(pool_handle).unwrap();

            TestUtils::cleanup_storage();
        }

        #[test]
        fn indy_agent_listen_works_for_get_sender_data_from_ledger() {
            TestUtils::cleanup_storage();

            let pool_handle = PoolUtils::create_and_open_pool_ledger("pool_1").unwrap();

            let trustee_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();
            let sender_wallet = WalletUtils::create_and_open_wallet("pool_1", None).unwrap();

            let (trustee_did, trustee_verkey, trustee_pk) = SignusUtils::create_and_store_my_did(trustee_wallet, Some(TRUSTEE_SEED)).unwrap();
            let (sender_did, sender_ver_key, sender_pub_key) = SignusUtils::create_and_store_my_did(sender_wallet, None).unwrap();
            let (listener_did, listener_verkey, listener_pk) = (trustee_did.clone(), trustee_verkey.clone(), trustee_pk.clone());
            let listener_wallet = trustee_wallet;

            let sender_nym_json = LedgerUtils::build_nym_request(trustee_did.as_str(), sender_did.as_str(),
                                                                 Some(sender_ver_key.as_str()), None, None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, trustee_wallet, trustee_did.as_str(), sender_nym_json.as_str()).unwrap();

            let sender_attrib_json =
                LedgerUtils::build_attrib_request(sender_did.as_str(), sender_did.as_str(), None,
                                                  Some(format!("{{\"endpoint\":{{\"ha\":\"{}\", \"verkey\":\"{}\"}}}}", ENDPOINT, sender_pub_key).as_str()),
                                                  None).unwrap();
            LedgerUtils::sign_and_submit_request(pool_handle, sender_wallet, sender_did.as_str(), sender_attrib_json.as_str()).unwrap();

            let listener_handle = AgentUtils::listen(ENDPOINT, None, None).unwrap();
            AgentUtils::add_identity(listener_handle, pool_handle, listener_wallet, listener_did.as_str()).unwrap();

            SignusUtils::store_their_did_from_parts(sender_wallet, listener_did.as_str(), listener_pk.as_str(), listener_verkey.as_str(), ENDPOINT).unwrap();

            AgentUtils::connect(pool_handle, sender_wallet, sender_did.as_str(), listener_did.as_str(), None).unwrap();

            AgentUtils::close_listener(listener_handle).unwrap();
            WalletUtils::close_wallet(listener_wallet).unwrap();
            WalletUtils::close_wallet(sender_wallet).unwrap();
            PoolUtils::close(pool_handle).unwrap();

            TestUtils::cleanup_storage();
        }

        #[test]
        fn indy_agent_listen_works_for_passed_on_connect_callback() {
            TestUtils::cleanup_storage();

{'fraction_non_alphanumeric': 0.13321008076286855, 'fraction_numerical': 0.006422107618954948, 'mean_word_length': 3.400556625990152, 'pattern_counts': {'":': 18, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1321015', 'n_tokens_mistral': 6265, 'n_tokens_neox': 6318, 'n_words': 860}