1 #![cfg(feature = "acl")]
2
3 use std::collections::HashSet;
4
5 use redis::acl::{AclInfo, Rule};
6 use redis::{Commands, Value};
7
8 mod support;
9 use crate::support::*;
10
11 #[test]
test_acl_whoami()12 fn test_acl_whoami() {
13 let ctx = TestContext::new();
14 let mut con = ctx.connection();
15 assert_eq!(con.acl_whoami(), Ok("default".to_owned()));
16 }
17
18 #[test]
test_acl_help()19 fn test_acl_help() {
20 let ctx = TestContext::new();
21 let mut con = ctx.connection();
22 let res: Vec<String> = con.acl_help().expect("Got help manual");
23 assert!(!res.is_empty());
24 }
25
26 #[test]
test_acl_getsetdel_users()27 fn test_acl_getsetdel_users() {
28 let ctx = TestContext::new();
29 let mut con = ctx.connection();
30 assert_eq!(
31 con.acl_list(),
32 Ok(vec!["user default on nopass ~* +@all".to_owned()])
33 );
34 assert_eq!(con.acl_users(), Ok(vec!["default".to_owned()]));
35 // bob
36 assert_eq!(con.acl_setuser("bob"), Ok(()));
37 assert_eq!(
38 con.acl_users(),
39 Ok(vec!["bob".to_owned(), "default".to_owned()])
40 );
41
42 // ACL SETUSER bob on ~redis:* +set
43 assert_eq!(
44 con.acl_setuser_rules(
45 "bob",
46 &[
47 Rule::On,
48 Rule::AddHashedPass(
49 "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2".to_owned()
50 ),
51 Rule::Pattern("redis:*".to_owned()),
52 Rule::AddCommand("set".to_owned())
53 ],
54 ),
55 Ok(())
56 );
57 let acl_info: AclInfo = con.acl_getuser("bob").expect("Got user");
58 assert_eq!(
59 acl_info,
60 AclInfo {
61 flags: vec![Rule::On],
62 passwords: vec![Rule::AddHashedPass(
63 "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2".to_owned()
64 )],
65 commands: vec![
66 Rule::RemoveCategory("all".to_owned()),
67 Rule::AddCommand("set".to_owned())
68 ],
69 keys: vec![Rule::Pattern("redis:*".to_owned())],
70 }
71 );
72 assert_eq!(
73 con.acl_list(),
74 Ok(vec![
75 "user bob on #c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2 ~redis:* -@all +set".to_owned(),
76 "user default on nopass ~* +@all".to_owned(),
77 ])
78 );
79
80 // ACL SETUSER eve
81 assert_eq!(con.acl_setuser("eve"), Ok(()));
82 assert_eq!(
83 con.acl_users(),
84 Ok(vec![
85 "bob".to_owned(),
86 "default".to_owned(),
87 "eve".to_owned()
88 ])
89 );
90 assert_eq!(con.acl_deluser(&["bob", "eve"]), Ok(2));
91 assert_eq!(con.acl_users(), Ok(vec!["default".to_owned()]));
92 }
93
94 #[test]
test_acl_cat()95 fn test_acl_cat() {
96 let ctx = TestContext::new();
97 let mut con = ctx.connection();
98 let res: HashSet<String> = con.acl_cat().expect("Got categories");
99 let expects = vec![
100 "keyspace",
101 "read",
102 "write",
103 "set",
104 "sortedset",
105 "list",
106 "hash",
107 "string",
108 "bitmap",
109 "hyperloglog",
110 "geo",
111 "stream",
112 "pubsub",
113 "admin",
114 "fast",
115 "slow",
116 "blocking",
117 "dangerous",
118 "connection",
119 "transaction",
120 "scripting",
121 ];
122 for cat in expects.iter() {
123 assert!(
124 res.contains(*cat),
125 format!("Category `{}` does not exist", cat)
126 );
127 }
128
129 let expects = vec!["pfmerge", "pfcount", "pfselftest", "pfadd"];
130 let res: HashSet<String> = con
131 .acl_cat_categoryname("hyperloglog")
132 .expect("Got commands of a category");
133 for cmd in expects.iter() {
134 assert!(
135 res.contains(*cmd),
136 format!("Command `{}` does not exist", cmd)
137 );
138 }
139 }
140
141 #[test]
test_acl_genpass()142 fn test_acl_genpass() {
143 let ctx = TestContext::new();
144 let mut con = ctx.connection();
145 let pass: String = con.acl_genpass().expect("Got password");
146 assert_eq!(pass.len(), 64);
147
148 let pass: String = con.acl_genpass_bits(1024).expect("Got password");
149 assert_eq!(pass.len(), 256);
150 }
151
152 #[test]
test_acl_log()153 fn test_acl_log() {
154 let ctx = TestContext::new();
155 let mut con = ctx.connection();
156 let logs: Vec<Value> = con.acl_log(1).expect("Got logs");
157 assert_eq!(logs.len(), 0);
158 assert_eq!(con.acl_log_reset(), Ok(()));
159 }
160