1
1
use crate :: adapter:: { Adapter , AdapterError , Config } ;
2
2
use crate :: sanity:: SanityChecker ;
3
3
use hex:: encode;
4
- use std:: collections:: HashSet ;
5
- use std:: hash:: { Hash , Hasher } ;
4
+ use std:: collections:: HashMap ;
6
5
7
- #[ derive( Eq , Debug ) ]
6
+ #[ derive( Debug ) ]
8
7
pub struct DummyParticipant {
9
8
pub identity : String ,
10
9
pub token : String ,
11
10
}
12
11
13
- impl PartialEq for DummyParticipant {
14
- fn eq ( & self , other : & Self ) -> bool {
15
- self . identity == other. identity || self . token == other. token
16
- }
17
- }
18
-
19
- impl Hash for DummyParticipant {
20
- fn hash < H : Hasher > ( & self , state : & mut H ) {
21
- self . identity . hash ( state) ;
22
- self . token . hash ( state) ;
23
- }
24
- }
25
-
26
- pub struct DummyAdapter {
12
+ pub struct DummyAdapter < ' a > {
27
13
pub config : Config ,
28
14
/// Dummy participants which will be used for
29
15
/// Creator, Validator Leader, Validator Follower and etc.
30
- pub participants : HashSet < DummyParticipant > ,
16
+ pub participants : HashMap < & ' a str , DummyParticipant > ,
31
17
}
32
18
33
- impl SanityChecker for DummyAdapter { }
19
+ impl SanityChecker for DummyAdapter < ' _ > { }
34
20
35
- impl Adapter for DummyAdapter {
21
+ impl Adapter for DummyAdapter < ' _ > {
36
22
fn config ( & self ) -> & Config {
37
23
& self . config
38
24
}
@@ -42,10 +28,10 @@ impl Adapter for DummyAdapter {
42
28
/// ```
43
29
/// use adapter::{ConfigBuilder, Adapter};
44
30
/// use adapter::dummy::DummyAdapter;
45
- /// use std::collections::HashSet ;
31
+ /// use std::collections::HashMap ;
46
32
///
47
33
/// let config = ConfigBuilder::new("identity").build();
48
- /// let adapter = DummyAdapter { config, participants: HashSet ::new() };
34
+ /// let adapter = DummyAdapter { config, participants: HashMap ::new() };
49
35
///
50
36
/// let actual = adapter.sign("abcdefghijklmnopqrstuvwxyz012345");
51
37
/// let expected = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity";
@@ -64,10 +50,10 @@ impl Adapter for DummyAdapter {
64
50
/// ```
65
51
/// use adapter::{ConfigBuilder, Adapter};
66
52
/// use adapter::dummy::DummyAdapter;
67
- /// use std::collections::HashSet ;
53
+ /// use std::collections::HashMap ;
68
54
///
69
55
/// let config = ConfigBuilder::new("identity").build();
70
- /// let adapter = DummyAdapter { config, participants: HashSet ::new() };
56
+ /// let adapter = DummyAdapter { config, participants: HashMap ::new() };
71
57
///
72
58
/// let signature = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity";
73
59
/// assert!(adapter.verify("identity", "doesn't matter", signature) )
@@ -85,9 +71,9 @@ impl Adapter for DummyAdapter {
85
71
match self
86
72
. participants
87
73
. iter ( )
88
- . find ( |& participant| participant. identity == validator)
74
+ . find ( |& ( _ , participant) | participant. identity == validator)
89
75
{
90
- Some ( participant) => Ok ( participant. token . to_string ( ) ) ,
76
+ Some ( ( _ , participant) ) => Ok ( participant. token . to_string ( ) ) ,
91
77
None => Err ( AdapterError :: Authentication ( "Identity not found" ) ) ,
92
78
}
93
79
}
@@ -99,11 +85,11 @@ mod test {
99
85
use crate :: adapter:: ConfigBuilder ;
100
86
101
87
#[ test]
102
- fn sings_state_root_and_verifies_it ( ) {
88
+ fn dummy_adapter_sings_state_root_and_verifies_it ( ) {
103
89
let config = ConfigBuilder :: new ( "identity" ) . build ( ) ;
104
90
let adapter = DummyAdapter {
105
91
config,
106
- participants : HashSet :: new ( ) ,
92
+ participants : HashMap :: new ( ) ,
107
93
} ;
108
94
109
95
let expected_signature = "Dummy adapter signature for 6162636465666768696a6b6c6d6e6f707172737475767778797a303132333435 by identity" ;
@@ -113,4 +99,54 @@ mod test {
113
99
114
100
assert ! ( adapter. verify( "identity" , "doesn't matter" , & actual_signature) )
115
101
}
102
+
103
+ #[ test]
104
+ fn get_auth_with_empty_participators ( ) {
105
+ let adapter = DummyAdapter {
106
+ config : ConfigBuilder :: new ( "identity" ) . build ( ) ,
107
+ participants : HashMap :: new ( ) ,
108
+ } ;
109
+
110
+ assert_eq ! (
111
+ AdapterError :: Authentication ( "Identity not found" ) ,
112
+ adapter. get_auth( "non-existing" ) . unwrap_err( )
113
+ ) ;
114
+
115
+ let mut participants = HashMap :: new ( ) ;
116
+ participants. insert (
117
+ "identity_key" ,
118
+ DummyParticipant {
119
+ identity : "identity" . to_string ( ) ,
120
+ token : "token" . to_string ( ) ,
121
+ } ,
122
+ ) ;
123
+ let adapter = DummyAdapter {
124
+ config : ConfigBuilder :: new ( "identity" ) . build ( ) ,
125
+ participants,
126
+ } ;
127
+
128
+ assert_eq ! (
129
+ AdapterError :: Authentication ( "Identity not found" ) ,
130
+ adapter. get_auth( "non-existing" ) . unwrap_err( )
131
+ ) ;
132
+ }
133
+
134
+ #[ test]
135
+ fn get_auth_with_existing_participator ( ) {
136
+ let mut participants = HashMap :: new ( ) ;
137
+ participants. insert (
138
+ "identity_key" ,
139
+ DummyParticipant {
140
+ identity : "identity" . to_string ( ) ,
141
+ token : "token" . to_string ( ) ,
142
+ } ,
143
+ ) ;
144
+
145
+ let adapter = DummyAdapter {
146
+ config : ConfigBuilder :: new ( "identity" ) . build ( ) ,
147
+ participants,
148
+ } ;
149
+
150
+ assert_eq ! ( Ok ( "token" . to_string( ) ) , adapter. get_auth( "identity" ) ) ;
151
+ }
116
152
}
0 commit comments