1616 */
1717package org .apache .dubbo .rpc .protocol .redis ;
1818
19+ import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
20+ import org .apache .dubbo .common .Constants ;
1921import org .apache .dubbo .common .URL ;
2022import org .apache .dubbo .common .extension .ExtensionLoader ;
23+ import org .apache .dubbo .common .serialize .ObjectInput ;
24+ import org .apache .dubbo .common .serialize .Serialization ;
2125import org .apache .dubbo .common .utils .NetUtils ;
2226import org .apache .dubbo .rpc .Invoker ;
2327import org .apache .dubbo .rpc .Protocol ;
2428import org .apache .dubbo .rpc .ProxyFactory ;
2529import org .apache .dubbo .rpc .RpcException ;
30+ import org .apache .dubbo .rpc .RpcResult ;
2631import org .junit .After ;
32+ import org .junit .Assert ;
2733import org .junit .Before ;
34+ import org .junit .Rule ;
2835import org .junit .Test ;
36+ import org .junit .rules .TestName ;
37+ import redis .clients .jedis .Jedis ;
38+ import redis .clients .jedis .JedisPool ;
39+ import redis .clients .jedis .exceptions .JedisConnectionException ;
40+ import redis .clients .jedis .exceptions .JedisDataException ;
2941import redis .embedded .RedisServer ;
3042
43+ import java .io .ByteArrayInputStream ;
44+ import java .io .IOException ;
45+
3146import static org .hamcrest .CoreMatchers .is ;
3247import static org .hamcrest .CoreMatchers .nullValue ;
3348import static org .junit .Assert .assertThat ;
@@ -38,12 +53,21 @@ public class RedisProtocolTest {
3853 private RedisServer redisServer ;
3954 private URL registryUrl ;
4055
56+ @ Rule
57+ public TestName name = new TestName ();
58+
4159 @ Before
4260 public void setUp () throws Exception {
4361 int redisPort = NetUtils .getAvailablePort ();
44- this .redisServer = new RedisServer (redisPort );
62+ if (name .getMethodName ().equals ("testAuthRedis" ) || name .getMethodName ().equals ("testWrongAuthRedis" )) {
63+ String password = "123456" ;
64+ this .redisServer = RedisServer .builder ().port (redisPort ).setting ("requirepass " + password ).build ();
65+ this .registryUrl = URL .valueOf ("redis://username:" +password +"@localhost:" +redisPort +"?db.index=0" );
66+ } else {
67+ this .redisServer = RedisServer .builder ().port (redisPort ).build ();
68+ this .registryUrl = URL .valueOf ("redis://localhost:" + redisPort );
69+ }
4570 this .redisServer .start ();
46- this .registryUrl = URL .valueOf ("redis://localhost:" + redisPort );
4771 }
4872
4973 @ After
@@ -109,4 +133,90 @@ public void testWrongRedis() {
109133 public void testExport () {
110134 protocol .export (protocol .refer (IDemoService .class , registryUrl ));
111135 }
136+
137+ @ Test
138+ public void testAuthRedis () {
139+ // default db.index=0
140+ Invoker <IDemoService > refer = protocol .refer (IDemoService .class ,
141+ registryUrl
142+ .addParameter ("max.idle" , 10 )
143+ .addParameter ("max.active" , 20 ));
144+ IDemoService demoService = this .proxy .getProxy (refer );
145+
146+ String value = demoService .get ("key" );
147+ assertThat (value , is (nullValue ()));
148+
149+ demoService .set ("key" , "newValue" );
150+ value = demoService .get ("key" );
151+ assertThat (value , is ("newValue" ));
152+
153+ demoService .delete ("key" );
154+ value = demoService .get ("key" );
155+ assertThat (value , is (nullValue ()));
156+
157+ refer .destroy ();
158+
159+ //change db.index=1
160+ String password = "123456" ;
161+ int database = 1 ;
162+ this .registryUrl = this .registryUrl .setPassword (password ).addParameter ("db.index" , database );
163+ refer = protocol .refer (IDemoService .class ,
164+ registryUrl
165+ .addParameter ("max.idle" , 10 )
166+ .addParameter ("max.active" , 20 ));
167+ demoService = this .proxy .getProxy (refer );
168+
169+ demoService .set ("key" , "newValue" );
170+ value = demoService .get ("key" );
171+ assertThat (value , is ("newValue" ));
172+
173+ // jedis gets the result comparison
174+ JedisPool pool = new JedisPool (new GenericObjectPoolConfig (), "localhost" , registryUrl .getPort (), 2000 , password , database , (String )null );
175+ Jedis jedis = null ;
176+ try {
177+ jedis = pool .getResource ();
178+ byte [] valueByte = jedis .get ("key" .getBytes ());
179+ Serialization serialization = ExtensionLoader .getExtensionLoader (Serialization .class ).getExtension (this .registryUrl .getParameter (Constants .SERIALIZATION_KEY , "java" ));
180+ ObjectInput oin = serialization .deserialize (this .registryUrl , new ByteArrayInputStream (valueByte ));
181+ String actual = (String ) oin .readObject ();
182+ assertThat (value , is (actual ));
183+ } catch (Exception e ) {
184+ Assert .fail ("jedis gets the result comparison is error!" );
185+ } finally {
186+ if (jedis != null ) {
187+ jedis .close ();
188+ }
189+ pool .destroy ();
190+ }
191+
192+ demoService .delete ("key" );
193+ value = demoService .get ("key" );
194+ assertThat (value , is (nullValue ()));
195+
196+ refer .destroy ();
197+ }
198+
199+ @ Test
200+ public void testWrongAuthRedis () {
201+ String password = "1234567" ;
202+ this .registryUrl = this .registryUrl .setPassword (password );
203+ Invoker <IDemoService > refer = protocol .refer (IDemoService .class ,
204+ registryUrl
205+ .addParameter ("max.idle" , 10 )
206+ .addParameter ("max.active" , 20 ));
207+ IDemoService demoService = this .proxy .getProxy (refer );
208+
209+ try {
210+ String value = demoService .get ("key" );
211+ assertThat (value , is (nullValue ()));
212+ } catch (RpcException e ) {
213+ if (e .getCause () instanceof JedisConnectionException && e .getCause ().getCause () instanceof JedisDataException ) {
214+ Assert .assertEquals ("ERR invalid password" , e .getCause ().getCause ().getMessage ());
215+ } else {
216+ Assert .fail ("no invalid password exception!" );
217+ }
218+ }
219+
220+ refer .destroy ();
221+ }
112222}
0 commit comments