Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tests): fix regex parsing passphrase from .env file #1931

Merged
merged 9 commits into from
Sep 8, 2023
4 changes: 2 additions & 2 deletions docs/DEV_ENVIRONMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
[Unix/Linux](https://github.com/KomodoPlatform/komodo/blob/master/zcutil/fetch-params.sh)
5. Create `.env.client` file with the following content
```
PASSPHRASE=spice describe gravity federal blast come thank unfair canal monkey style afraid
ALICE_PASSPHRASE=spice describe gravity federal blast come thank unfair canal monkey style afraid
```
6. Create `.env.seed` file with the following content
```
PASSPHRASE=also shoot benefit prefer juice shell elder veteran woman mimic image kidney
BOB_PASSPHRASE=also shoot benefit prefer juice shell elder veteran woman mimic image kidney
```
7. MacOS specific: run script (required after each reboot)
```shell
Expand Down
2 changes: 1 addition & 1 deletion mm2src/coins/eth/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub fn jst_distributor() -> EthCoin {
"urls": ETH_DEV_NODES,
"swap_contract_address": ETH_DEV_SWAP_CONTRACT,
});
let seed = get_passphrase!(".env.client", "BOB_PASSPHRASE").unwrap();
let seed = get_passphrase!(".env.seed", "BOB_PASSPHRASE").unwrap();
let keypair = key_pair_from_seed(&seed).unwrap();
let priv_key_policy = PrivKeyBuildPolicy::IguanaPrivKey(keypair.private().secret);
block_on(eth_coin_from_conf_and_request(
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/tests/docker_tests/swap_watcher_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,7 +1313,7 @@ fn test_watcher_validate_taker_payment_eth() {
let taker_keypair = taker_coin.derive_htlc_key_pair(&[]);
let taker_pub = taker_keypair.public();

let maker_seed = get_passphrase!(".env.client", "BOB_PASSPHRASE").unwrap();
let maker_seed = get_passphrase!(".env.seed", "BOB_PASSPHRASE").unwrap();
let maker_keypair = key_pair_from_seed(&maker_seed).unwrap();
let maker_pub = maker_keypair.public();

Expand Down Expand Up @@ -1557,7 +1557,7 @@ fn test_watcher_validate_taker_payment_erc20() {
let taker_keypair = taker_coin.derive_htlc_key_pair(&[]);
let taker_pub = taker_keypair.public();

let maker_seed = get_passphrase!(".env.client", "BOB_PASSPHRASE").unwrap();
let maker_seed = get_passphrase!(".env.seed", "BOB_PASSPHRASE").unwrap();
let maker_keypair = key_pair_from_seed(&maker_seed).unwrap();
let maker_pub = maker_keypair.public();

Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/tests/mm2_tests/best_orders_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ fn test_best_orders_v2_exclude_mine() {
assert!(status.is_success(), "!setprice: {}", data);
}

let alice_passphrase = get_passphrase(&".env.seed", "ALICE_PASSPHRASE").unwrap();
let alice_passphrase = get_passphrase(&".env.client", "ALICE_PASSPHRASE").unwrap();
let mm_alice = MarketMakerIt::start(
json! ({
"gui": "nogui",
Expand Down
4 changes: 2 additions & 2 deletions mm2src/mm2_main/tests/mm2_tests/eth_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async fn enable_eth_with_tokens_without_balance(
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_disable_eth_coin_with_token() {
let passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();
let coins = json!([eth_testnet_conf(), eth_jst_testnet_conf(),]);
let conf = Mm2TestConf::seednode(&passphrase, &coins);
let mm = block_on(MarketMakerIt::start_async(conf.conf, conf.rpc_password, None)).unwrap();
Expand Down Expand Up @@ -123,7 +123,7 @@ fn test_disable_eth_coin_with_token() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_disable_eth_coin_with_token_without_balance() {
let passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();
let coins = json!([eth_testnet_conf(), eth_jst_testnet_conf(),]);
let conf = Mm2TestConf::seednode(&passphrase, &coins);
let mm = block_on(MarketMakerIt::start_async(conf.conf, conf.rpc_password, None)).unwrap();
Expand Down
38 changes: 19 additions & 19 deletions mm2src/mm2_main/tests/mm2_tests/mm2_tests_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ fn test_withdraw_legacy() {
let alice_passphrase = var("ALICE_PASSPHRASE")
.ok()
.or(alice_file_passphrase)
.expect("No ALICE_PASSPHRASE or .env.client/PASSPHRASE");
.expect("No ALICE_PASSPHRASE or .env.client/ALICE_PASSPHRASE");

let coins = json!([
{"coin":"RICK","asset":"RICK","rpcport":8923,"txversion":4,"overwintered":1,"txfee":1000,"protocol":{"type":"UTXO"}},
Expand Down Expand Up @@ -2535,7 +2535,7 @@ fn setprice_buy_sell_too_low_volume() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_fill_or_kill_taker_order_should_not_transform_to_maker() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -2603,7 +2603,7 @@ fn test_fill_or_kill_taker_order_should_not_transform_to_maker() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_gtc_taker_order_should_transform_to_maker() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -2676,7 +2676,7 @@ fn test_gtc_taker_order_should_transform_to_maker() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_set_price_must_save_order_to_db() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -2729,7 +2729,7 @@ fn test_set_price_must_save_order_to_db() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_set_price_response_format() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -3881,7 +3881,7 @@ fn test_validateaddress() {
let bob_passphrase = var("BOB_PASSPHRASE")
.ok()
.or(bob_file_passphrase)
.expect("No BOB_PASSPHRASE or .env.seed/PASSPHRASE");
.expect("No BOB_PASSPHRASE or .env.seed/BOB_PASSPHRASE");

let mm = MarketMakerIt::start(
json!({
Expand Down Expand Up @@ -4835,7 +4835,7 @@ fn test_tx_history_tbtc_non_segwit() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_buy_conf_settings() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(),
{"coin":"JST","name":"jst","protocol":{"type":"ERC20","protocol_data":{"platform":"ETH","contract_address":ETH_DEV_TOKEN_CONTRACT}},"required_confirmations":2},]);
Expand Down Expand Up @@ -4908,7 +4908,7 @@ fn test_buy_conf_settings() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_buy_response_format() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -4958,7 +4958,7 @@ fn test_buy_response_format() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_sell_response_format() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -5008,7 +5008,7 @@ fn test_sell_response_format() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_my_orders_response_format() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -5168,7 +5168,7 @@ fn test_my_orders_after_matched() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_sell_conf_settings() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(),
{"coin":"JST","name":"jst","protocol":{"type":"ERC20","protocol_data":{"platform":"ETH","contract_address": ETH_DEV_TOKEN_CONTRACT}},"required_confirmations":2},]);
Expand Down Expand Up @@ -5241,7 +5241,7 @@ fn test_sell_conf_settings() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_set_price_conf_settings() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(),
{"coin":"JST","name":"jst","protocol":{"type":"ERC20","protocol_data":{"platform":"ETH","contract_address": ETH_DEV_TOKEN_CONTRACT}},"required_confirmations":2},]);
Expand Down Expand Up @@ -5314,7 +5314,7 @@ fn test_set_price_conf_settings() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_update_maker_order() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json! ([
{"coin":"RICK","asset":"RICK","required_confirmations":0,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
Expand Down Expand Up @@ -5454,7 +5454,7 @@ fn test_update_maker_order() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_update_maker_order_fail() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json! ([
{"coin":"RICK","asset":"RICK","required_confirmations":0,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
Expand Down Expand Up @@ -5987,7 +5987,7 @@ fn test_orderbook_is_mine_orders() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_sell_min_volume() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -6060,7 +6060,7 @@ fn test_sell_min_volume() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_sell_min_volume_dust() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json! ([
{"coin":"RICK","asset":"RICK","dust":10000000,"required_confirmations":0,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
Expand Down Expand Up @@ -6111,7 +6111,7 @@ fn test_sell_min_volume_dust() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_setprice_min_volume_dust() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json! ([
{"coin":"RICK","asset":"RICK","dust":10000000,"required_confirmations":0,"txversion":4,"overwintered":1,"protocol":{"type":"UTXO"}},
Expand Down Expand Up @@ -6159,7 +6159,7 @@ fn test_setprice_min_volume_dust() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_buy_min_volume() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down Expand Up @@ -6368,7 +6368,7 @@ fn test_orderbook_depth() {
#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_mm2_db_migration() {
let bob_passphrase = get_passphrase(&".env.client", "BOB_PASSPHRASE").unwrap();
let bob_passphrase = get_passphrase(&".env.seed", "BOB_PASSPHRASE").unwrap();

let coins = json!([rick_conf(), morty_conf(), eth_testnet_conf(), eth_jst_testnet_conf(),]);

Expand Down
31 changes: 30 additions & 1 deletion mm2src/mm2_test_helpers/src/for_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ pub async fn enable_qrc20(
pub fn from_env_file(env: Vec<u8>) -> (Option<String>, Option<String>) {
use regex::bytes::Regex;
let (mut passphrase, mut userpass) = (None, None);
for cap in Regex::new(r"(?m)^(PASSPHRASE|USERPASS)=(\w[\w ]+)$")
for cap in Regex::new(r"^\w+_(PASSPHRASE|USERPASS)=(\w+( \w+)+)\s*")
.unwrap()
.captures_iter(&env)
{
Expand Down Expand Up @@ -1581,6 +1581,9 @@ macro_rules! get_passphrase {
}

/// Reads passphrase from file or environment.
/// Note that if you try to read the passphrase file from the current directory
/// the current directory could be different depending on how you run tests
/// (it could be either the workspace directory or the module source directory)
#[cfg(not(target_arch = "wasm32"))]
pub fn get_passphrase(path: &dyn AsRef<Path>, env: &str) -> Result<String, String> {
if let (Some(file_passphrase), _file_userpass) = from_env_file(try_s!(slurp(path))) {
Expand Down Expand Up @@ -2987,3 +2990,29 @@ pub async fn get_locked_amount(mm: &MarketMakerIt, coin: &str) -> GetLockedAmoun
let response: RpcV2Response<GetLockedAmountResponse> = json::from_str(&request.1).unwrap();
response.result
}

#[test]
#[cfg(not(target_arch = "wasm32"))]
fn test_parse_env_file() {
let env_client =
b"ALICE_PASSPHRASE=spice describe gravity federal blast come thank unfair canal monkey style afraid";
let env_client_new_line =
b"ALICE_PASSPHRASE=spice describe gravity federal blast come thank unfair canal monkey style afraid\n";
let env_client_space =
b"ALICE_PASSPHRASE=spice describe gravity federal blast come thank unfair canal monkey style afraid ";

let parsed1 = from_env_file(env_client.to_vec());
let parsed2 = from_env_file(env_client_new_line.to_vec());
let parsed3 = from_env_file(env_client_space.to_vec());
assert_eq!(parsed1, parsed2);
assert_eq!(parsed1, parsed3);
assert_eq!(
parsed1,
(
Some(String::from(
"spice describe gravity federal blast come thank unfair canal monkey style afraid"
)),
None
)
);
}
Loading