@@ -44,9 +44,10 @@ groups() ->
44
44
test_failed_connection_with_a_token_with_insufficient_vhost_permission ,
45
45
test_failed_connection_with_a_token_with_insufficient_resource_permission ,
46
46
more_than_one_resource_server_id_not_allowed_in_one_token ,
47
+ mqtt_expired_token ,
47
48
mqtt_expirable_token ,
48
49
web_mqtt_expirable_token ,
49
- mqtt_expired_token
50
+ amqp_expirable_token
50
51
]},
51
52
52
53
{token_refresh , [], [
@@ -433,6 +434,18 @@ mqtt(Config) ->
433
434
ok = emqtt :disconnect (Sub ),
434
435
ok = emqtt :disconnect (Pub ).
435
436
437
+ mqtt_expired_token (Config ) ->
438
+ {_Algo , Token } = generate_expired_token (Config ),
439
+ Opts = [{port , rabbit_ct_broker_helpers :get_node_config (Config , 0 , tcp_port_mqtt )},
440
+ {proto_ver , v5 },
441
+ {username , <<" " >>},
442
+ {password , Token }],
443
+ ClientId = atom_to_binary (? FUNCTION_NAME ),
444
+ {ok , C } = emqtt :start_link ([{clientid , ClientId } | Opts ]),
445
+ true = unlink (C ),
446
+ ? assertMatch ({error , {bad_username_or_password , _ }},
447
+ emqtt :connect (C )).
448
+
436
449
mqtt_expirable_token (Config ) ->
437
450
mqtt_expirable_token0 (tcp_port_mqtt ,
438
451
[],
@@ -487,17 +500,49 @@ mqtt_expirable_token0(Port, AdditionalOpts, Connect, Config) ->
487
500
after Millis * 2 -> ct :fail (" missing DISCONNECT packet from server" )
488
501
end .
489
502
490
- mqtt_expired_token (Config ) ->
491
- {_Algo , Token } = generate_expired_token (Config ),
492
- Opts = [{port , rabbit_ct_broker_helpers :get_node_config (Config , 0 , tcp_port_mqtt )},
493
- {proto_ver , v5 },
494
- {username , <<" " >>},
495
- {password , Token }],
496
- ClientId = atom_to_binary (? FUNCTION_NAME ),
497
- {ok , C } = emqtt :start_link ([{clientid , ClientId } | Opts ]),
498
- true = unlink (C ),
499
- ? assertMatch ({error , {bad_username_or_password , _ }},
500
- emqtt :connect (C )).
503
+ amqp_expirable_token (Config ) ->
504
+ {ok , _ } = application :ensure_all_started (rabbitmq_amqp_client ),
505
+
506
+ Seconds = 4 ,
507
+ Millis = Seconds * 1000 ,
508
+ {_Algo , Token } = generate_expirable_token (Config ,
509
+ [<<" rabbitmq.configure:*/*" >>,
510
+ <<" rabbitmq.write:*/*" >>,
511
+ <<" rabbitmq.read:*/*" >>],
512
+ Seconds ),
513
+
514
+ % % Send and receive a message via AMQP 1.0.
515
+ QName = atom_to_binary (? FUNCTION_NAME ),
516
+ Address = rabbitmq_amqp_address :queue (QName ),
517
+ Host = ? config (rmq_hostname , Config ),
518
+ Port = rabbit_ct_broker_helpers :get_node_config (Config , 0 , tcp_port_amqp ),
519
+ OpnConf = #{address => Host ,
520
+ port => Port ,
521
+ container_id => <<" my container" >>,
522
+ sasl => {plain , <<" " >>, Token }},
523
+ {ok , Connection } = amqp10_client :open_connection (OpnConf ),
524
+ {ok , Session } = amqp10_client :begin_session_sync (Connection ),
525
+ {ok , LinkPair } = rabbitmq_amqp_client :attach_management_link_pair_sync (Session , <<" my link pair" >>),
526
+ {ok , _ } = rabbitmq_amqp_client :declare_queue (LinkPair , QName , #{}),
527
+ {ok , Sender } = amqp10_client :attach_sender_link (Session , <<" my sender" >>, Address ),
528
+ receive {amqp10_event , {link , Sender , credited }} -> ok
529
+ after 5000 -> ct :fail ({missing_event , ? LINE })
530
+ end ,
531
+ Body = <<" hey" >>,
532
+ Msg0 = amqp10_msg :new (<<" tag" >>, Body ),
533
+ ok = amqp10_client :send_msg (Sender , Msg0 ),
534
+ {ok , Receiver } = amqp10_client :attach_receiver_link (Session , <<" my receiver" >>, Address ),
535
+ {ok , Msg } = amqp10_client :get_msg (Receiver ),
536
+ ? assertEqual ([Body ], amqp10_msg :body (Msg )),
537
+
538
+ % % In 4 seconds from now, we expect that RabbitMQ disconnects us because our token expired.
539
+ receive {amqp10_event ,
540
+ {connection , Connection ,
541
+ {closed , {unauthorized_access , <<" credential expired" >>}}}} ->
542
+ ok
543
+ after Millis * 2 ->
544
+ ct :fail (" server did not close our connection" )
545
+ end .
501
546
502
547
test_successful_connection_with_complex_claim_as_a_map (Config ) ->
503
548
{_Algo , Token } = generate_valid_token_with_extra_fields (
0 commit comments