@@ -1098,70 +1098,78 @@ mod tests {
10981098 use super :: * ;
10991099
11001100 #[ test]
1101- fn parse_derivation_path ( ) {
1102- assert ! ( matches!(
1103- "n/0'/0" . parse:: <DerivationPath >( ) ,
1104- Err ( ParseChildNumberError :: ParseInt ( ..) ) ,
1105- ) ) ;
1106- assert ! ( matches!(
1107- "4/m/5" . parse:: <DerivationPath >( ) ,
1108- Err ( ParseChildNumberError :: ParseInt ( ..) ) ,
1109- ) ) ;
1110- assert ! ( matches!(
1111- "//3/0'" . parse:: <DerivationPath >( ) ,
1112- Err ( ParseChildNumberError :: ParseInt ( ..) ) ,
1113- ) ) ;
1114- assert ! ( matches!(
1115- "0h/0x" . parse:: <DerivationPath >( ) ,
1116- Err ( ParseChildNumberError :: ParseInt ( ..) ) ,
1117- ) ) ;
1101+ fn parse_derivation_path_invalid_format ( ) {
1102+ let invalid_paths = [
1103+ "n/0'/0" ,
1104+ "4/m/5" ,
1105+ "//3/0'" ,
1106+ "0h/0x" ,
1107+ ] ;
1108+ for path in & invalid_paths {
1109+ assert ! ( matches!(
1110+ path. parse:: <DerivationPath >( ) ,
1111+ Err ( ParseChildNumberError :: ParseInt ( ..) ) ,
1112+ ) ) ;
1113+ }
1114+ }
1115+
1116+ #[ test]
1117+ fn parse_derivation_path_out_of_range ( ) {
1118+ let invalid_path = "2147483648" ;
11181119 assert_eq ! (
1119- "2147483648" . parse:: <DerivationPath >( ) ,
1120+ invalid_path . parse:: <DerivationPath >( ) ,
11201121 Err ( ParseChildNumberError :: IndexOutOfRange ( IndexOutOfRangeError { index: 2147483648 } ) ) ,
11211122 ) ;
1123+ }
11221124
1125+ #[ test]
1126+ fn parse_derivation_path_valid_empty_master ( ) {
1127+ // Sanity checks.
1128+ assert_eq ! ( DerivationPath :: master( ) , DerivationPath ( vec![ ] ) ) ;
11231129 assert_eq ! ( DerivationPath :: master( ) , "" . parse:: <DerivationPath >( ) . unwrap( ) ) ;
11241130 assert_eq ! ( DerivationPath :: master( ) , DerivationPath :: default ( ) ) ;
11251131
1126- // Acceptable forms for a master path.
1132+ // Empty is the same as with an `m`.
1133+ assert_eq ! ( "" . parse:: <DerivationPath >( ) . unwrap( ) , DerivationPath ( vec![ ] ) ) ;
11271134 assert_eq ! ( "m" . parse:: <DerivationPath >( ) . unwrap( ) , DerivationPath ( vec![ ] ) ) ;
11281135 assert_eq ! ( "m/" . parse:: <DerivationPath >( ) . unwrap( ) , DerivationPath ( vec![ ] ) ) ;
1129- assert_eq ! ( "" . parse :: < DerivationPath > ( ) . unwrap ( ) , DerivationPath ( vec! [ ] ) ) ;
1136+ }
11301137
1131- assert_eq ! ( "0'" . parse:: <DerivationPath >( ) , Ok ( vec![ ChildNumber :: ZERO_HARDENED ] . into( ) ) ) ;
1132- assert_eq ! (
1133- "0'/1" . parse:: <DerivationPath >( ) ,
1134- Ok ( vec![ ChildNumber :: ZERO_HARDENED , ChildNumber :: ONE_NORMAL ] . into( ) )
1135- ) ;
1136- assert_eq ! (
1137- "0h/1/2'" . parse:: <DerivationPath >( ) ,
1138- Ok ( vec![
1138+ #[ test]
1139+ fn parse_derivation_path_valid ( ) {
1140+ let valid_paths = [
1141+ ( "0'" , vec ! [ ChildNumber :: ZERO_HARDENED ] ) ,
1142+ ( "0'/1" , vec ! [ ChildNumber :: ZERO_HARDENED , ChildNumber :: ONE_NORMAL ] ) ,
1143+ ( "0h/1/2'" , vec ! [
11391144 ChildNumber :: ZERO_HARDENED ,
11401145 ChildNumber :: ONE_NORMAL ,
11411146 ChildNumber :: from_hardened_idx( 2 ) . unwrap( ) ,
1142- ]
1143- . into( ) )
1144- ) ;
1145- assert_eq ! (
1146- "0'/1/2h/2" . parse:: <DerivationPath >( ) ,
1147- Ok ( vec![
1147+ ] ) ,
1148+ ( "0'/1/2h/2" , vec ! [
11481149 ChildNumber :: ZERO_HARDENED ,
11491150 ChildNumber :: ONE_NORMAL ,
11501151 ChildNumber :: from_hardened_idx( 2 ) . unwrap( ) ,
11511152 ChildNumber :: from_normal_idx( 2 ) . unwrap( ) ,
1152- ]
1153- . into( ) )
1154- ) ;
1155- let want = DerivationPath :: from ( vec ! [
1156- ChildNumber :: ZERO_HARDENED ,
1157- ChildNumber :: ONE_NORMAL ,
1158- ChildNumber :: from_hardened_idx( 2 ) . unwrap( ) ,
1159- ChildNumber :: from_normal_idx( 2 ) . unwrap( ) ,
1160- ChildNumber :: from_normal_idx( 1000000000 ) . unwrap( ) ,
1161- ] ) ;
1162- assert_eq ! ( "0'/1/2'/2/1000000000" . parse:: <DerivationPath >( ) . unwrap( ) , want) ;
1163- assert_eq ! ( "m/0'/1/2'/2/1000000000" . parse:: <DerivationPath >( ) . unwrap( ) , want) ;
1153+ ] ) ,
1154+ ( "0'/1/2'/2/1000000000" , vec ! [
1155+ ChildNumber :: ZERO_HARDENED ,
1156+ ChildNumber :: ONE_NORMAL ,
1157+ ChildNumber :: from_hardened_idx( 2 ) . unwrap( ) ,
1158+ ChildNumber :: from_normal_idx( 2 ) . unwrap( ) ,
1159+ ChildNumber :: from_normal_idx( 1000000000 ) . unwrap( ) ,
1160+ ] ) ,
1161+ ] ;
1162+ for ( path, expected) in valid_paths {
1163+ // Access the inner private field so we don't have to clone expected.
1164+ assert_eq ! ( path. parse:: <DerivationPath >( ) . unwrap( ) . 0 , expected) ;
1165+ // Test with the leading `m` for good measure.
1166+ let prefixed = format ! ( "m/{}" , path) ;
1167+ assert_eq ! ( prefixed. parse:: <DerivationPath >( ) . unwrap( ) . 0 , expected) ;
1168+ }
1169+ }
11641170
1171+ #[ test]
1172+ fn parse_derivation_path_same_as_into_derivation_path ( ) {
11651173 let s = "0'/50/3'/5/545456" ;
11661174 assert_eq ! ( s. parse:: <DerivationPath >( ) , s. into_derivation_path( ) ) ;
11671175 assert_eq ! ( s. parse:: <DerivationPath >( ) , s. to_string( ) . into_derivation_path( ) ) ;
0 commit comments