1616 */
1717package com .alibaba .dubbo .rpc .protocol .redis ;
1818
19+ import com .alibaba .dubbo .common .Constants ;
1920import com .alibaba .dubbo .common .URL ;
2021import com .alibaba .dubbo .common .extension .ExtensionLoader ;
22+ import com .alibaba .dubbo .common .serialize .ObjectInput ;
23+ import com .alibaba .dubbo .common .serialize .Serialization ;
2124import com .alibaba .dubbo .common .utils .NetUtils ;
2225import com .alibaba .dubbo .rpc .Invoker ;
2326import com .alibaba .dubbo .rpc .Protocol ;
2427import com .alibaba .dubbo .rpc .ProxyFactory ;
2528import com .alibaba .dubbo .rpc .RpcException ;
29+
30+ import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
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+
3145import static org .hamcrest .CoreMatchers .is ;
3246import static org .hamcrest .CoreMatchers .nullValue ;
3347import static org .junit .Assert .assertThat ;
@@ -38,12 +52,21 @@ public class RedisProtocolTest {
3852 private RedisServer redisServer ;
3953 private URL registryUrl ;
4054
55+ @ Rule
56+ public TestName name = new TestName ();
57+
4158 @ Before
4259 public void setUp () throws Exception {
4360 int redisPort = NetUtils .getAvailablePort ();
44- this .redisServer = new RedisServer (redisPort );
61+ if (name .getMethodName ().equals ("testAuthRedis" ) || name .getMethodName ().equals ("testWrongAuthRedis" )) {
62+ String password = "123456" ;
63+ this .redisServer = RedisServer .builder ().port (redisPort ).setting ("requirepass " + password ).build ();
64+ this .registryUrl = URL .valueOf ("redis://username:" +password +"@localhost:" +redisPort +"?db.index=0" );
65+ } else {
66+ this .redisServer = RedisServer .builder ().port (redisPort ).build ();
67+ this .registryUrl = URL .valueOf ("redis://localhost:" + redisPort );
68+ }
4569 this .redisServer .start ();
46- this .registryUrl = URL .valueOf ("redis://localhost:" + redisPort );
4770 }
4871
4972 @ After
@@ -109,4 +132,90 @@ public void testWrongRedis() {
109132 public void testExport () {
110133 protocol .export (protocol .refer (IDemoService .class , registryUrl ));
111134 }
135+
136+ @ Test
137+ public void testAuthRedis () {
138+ // default db.index=0
139+ Invoker <IDemoService > refer = protocol .refer (IDemoService .class ,
140+ registryUrl
141+ .addParameter ("max.idle" , 10 )
142+ .addParameter ("max.active" , 20 ));
143+ IDemoService demoService = this .proxy .getProxy (refer );
144+
145+ String value = demoService .get ("key" );
146+ assertThat (value , is (nullValue ()));
147+
148+ demoService .set ("key" , "newValue" );
149+ value = demoService .get ("key" );
150+ assertThat (value , is ("newValue" ));
151+
152+ demoService .delete ("key" );
153+ value = demoService .get ("key" );
154+ assertThat (value , is (nullValue ()));
155+
156+ refer .destroy ();
157+
158+ //change db.index=1
159+ String password = "123456" ;
160+ int database = 1 ;
161+ this .registryUrl = this .registryUrl .setPassword (password ).addParameter ("db.index" , database );
162+ refer = protocol .refer (IDemoService .class ,
163+ registryUrl
164+ .addParameter ("max.idle" , 10 )
165+ .addParameter ("max.active" , 20 ));
166+ demoService = this .proxy .getProxy (refer );
167+
168+ demoService .set ("key" , "newValue" );
169+ value = demoService .get ("key" );
170+ assertThat (value , is ("newValue" ));
171+
172+ // jedis gets the result comparison
173+ JedisPool pool = new JedisPool (new GenericObjectPoolConfig (), "localhost" , registryUrl .getPort (), 2000 , password , database , (String )null );
174+ Jedis jedis = null ;
175+ try {
176+ jedis = pool .getResource ();
177+ byte [] valueByte = jedis .get ("key" .getBytes ());
178+ Serialization serialization = ExtensionLoader .getExtensionLoader (Serialization .class ).getExtension (this .registryUrl .getParameter (Constants .SERIALIZATION_KEY , "java" ));
179+ ObjectInput oin = serialization .deserialize (this .registryUrl , new ByteArrayInputStream (valueByte ));
180+ String actual = (String ) oin .readObject ();
181+ assertThat (value , is (actual ));
182+ } catch (Exception e ) {
183+ Assert .fail ("jedis gets the result comparison is error!" );
184+ } finally {
185+ if (jedis != null ) {
186+ jedis .close ();
187+ }
188+ pool .destroy ();
189+ }
190+
191+ demoService .delete ("key" );
192+ value = demoService .get ("key" );
193+ assertThat (value , is (nullValue ()));
194+
195+ refer .destroy ();
196+ }
197+
198+ @ Test
199+ public void testWrongAuthRedis () {
200+ String password = "1234567" ;
201+ this .registryUrl = this .registryUrl .setPassword (password );
202+ Invoker <IDemoService > refer = protocol .refer (IDemoService .class ,
203+ registryUrl
204+ .addParameter ("max.idle" , 10 )
205+ .addParameter ("max.active" , 20 ));
206+ IDemoService demoService = this .proxy .getProxy (refer );
207+
208+ try {
209+ String value = demoService .get ("key" );
210+ assertThat (value , is (nullValue ()));
211+ } catch (RpcException e ) {
212+ if (e .getCause () instanceof JedisConnectionException && e .getCause ().getCause () instanceof JedisDataException ) {
213+ Assert .assertEquals ("ERR invalid password" , e .getCause ().getCause ().getMessage ());
214+ } else {
215+ Assert .fail ("no invalid password exception!" );
216+ }
217+ }
218+
219+ refer .destroy ();
220+ }
112221}
0 commit comments