1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#[macro_use]
extern crate log;

pub mod rato;
pub mod redis_cmd;
pub mod util;



#[cfg(test)]
mod tests {

    use super::*;
    use crate::rato::Rato;
    use crate::redis_cmd::RedisCommand;

    use hashbrown::HashMap;
    use std::sync::Arc;

    extern crate env_logger;

    struct RedisTest {}

    impl RedisCommand for RedisTest {
        fn on_cmd_ping(&self, key: &[u8]) {
            if !key.is_empty() {
                let test_key = b"test_key";
                assert_eq!(key, test_key);
            }
        }
        fn on_cmd_echo(&self, val: &[u8]) {}
        fn on_cmd_quit(&self) -> Result<(), String> {
            Err("Not implemented".to_string())
        }

        fn on_cmd_auth(&self, db: &[u8], password: &[u8]) -> Result<(), String> {
            if !password.is_empty() && password == b"secret" {
                return Ok(());
            }
            Err("Invalid password".to_string())
        }
        fn on_cmd_flushdb(&self, db: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_keys(&self, db: &[u8]) -> Result<Vec<Vec<u8>>, String> {
            Err("Not implemented".to_string())
        }

        fn on_cmd_del(&self, db: &[u8], key: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_get(&self, db: &[u8], key: &[u8]) -> Result<Option<Vec<u8>>, String> {
            Ok(Some(b"b".to_vec()))
        }
        fn on_cmd_hmget(
            &self,
            db: &[u8],
            hash: &[u8],
            keys: &[Vec<u8>],
        ) -> Result<Option<Vec<Vec<u8>>>, String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_hget(&self, db: &[u8], hash: &[u8], key: &[u8]) -> Result<Option<Vec<u8>>, String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_hgetall(
            &self,
            db: &[u8],
            hash: &[u8],
        ) -> Result<Option<Vec<Vec<u8>>>, String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_set(&self, db: &[u8], key: &[u8], val: &[u8]) -> Result<(), String> {
            Ok(())
        }
        fn on_cmd_hmset(
            &self,
            db: &[u8],
            hash: &[u8],
            kv:&[Vec<u8>],
        ) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_hset(
            &self,
            db: &[u8],
            hash: &[u8],
            key: &[u8],
            val: &[u8],
        ) -> Result<(), String> {
            Err("Not implemented".to_string())
        }

        fn on_cmd_msg(&self, channel: &[u8], val: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_subscribe(&self, val: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_subscribe_response(&self, channel: &[u8], status: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_cluster_nodes(&self) -> Result<Vec<u8>, String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_cluster_slots(&self) -> Result<Vec<u8>, String> {
            Err("Not implemented".to_string())
        }

        fn on_cmd_cluster_add_node(&self, kv: &HashMap<&Vec<u8>, &Vec<u8>>) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_cluster_remove_node(&self, id: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }

        #[inline]
        fn on_cmd_cluster_update_node(
            &self,
            kv: &HashMap<&Vec<u8>, &Vec<u8>>,
        ) -> Result<(), String> {
            Err("Not implemented".to_string())
        }

        fn on_cmd_backupdb(&self, db: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
        fn on_cmd_backup_lru_keys(&self, table_name: &[u8]) -> Result<(), String> {
            Err("Not implemented".to_string())
        }
    }

    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }

    #[test]
    fn on_cmd_auth_test() {
        let _ = env_logger::try_init();

        let redis_test = RedisTest {};
        //let redis_test= Arc::new(redis_test);

        //let mut mulo = Rato::<RedisTest>::new(redis_test);

        let mut tags: HashMap<String, String> = HashMap::with_capacity(1);
        tags.insert(Rato::DB_TAG.to_string(), "pan".to_string());

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"AUTH secret\r\n".to_vec());
        assert_eq!(String::from_utf8_lossy(&res.0), "+OK\r\n");

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"AUTH abc\r\n".to_vec());
        assert_ne!(String::from_utf8_lossy(&res.0), "+OK\r\n");
    }

    #[test]
    fn on_cmd_ping_test() {
        let _ = env_logger::try_init();

        let redis_test = RedisTest {};
        //let redis_test= Arc::new(redis_test);

        let mut tags: HashMap<String, String> = HashMap::with_capacity(1);
        tags.insert(Rato::DB_TAG.to_string(), "pan".to_string());
        tags.insert(Rato::AUTH_TAG.to_string(), "true".to_string());

        //let mut mulo = Rato::<RedisTest>::new(redis_test);

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"PING\r\n".to_vec());
        assert_eq!(String::from_utf8_lossy(&res.0), "+PONG\r\n");

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"PING test_key\r\n".to_vec());
        assert_eq!(String::from_utf8_lossy(&res.0), "$8\r\ntest_key\r\n");
    }

    #[test]
    fn on_cmd_set_test() {
        let _ = env_logger::try_init();

        let redis_test = RedisTest {};
        //let redis_test= Arc::new(redis_test);

        let mut tags: HashMap<String, String> = HashMap::with_capacity(1);
        tags.insert(Rato::DB_TAG.to_string(), "pan".to_string());
        tags.insert(Rato::AUTH_TAG.to_string(), "true".to_string());

        //let mut mulo = Rato::<RedisTest>::new(redis_test);

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"SET a b\r\n".to_vec());
        assert_eq!(String::from_utf8_lossy(&res.0), "+OK\r\n");
    }

    #[test]
    fn on_cmd_get_test() {
        let _ = env_logger::try_init();

        let redis_test = RedisTest {};
        //let redis_test= Arc::new(redis_test);

        let mut tags: HashMap<String, String> = HashMap::with_capacity(1);
        tags.insert(Rato::DB_TAG.to_string(), "pan".to_string());
        tags.insert(Rato::AUTH_TAG.to_string(), "true".to_string());

        //let mut mulo = Rato::<RedisTest>::new(redis_test);

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"GET a\r\n".to_vec());
        assert_eq!(String::from_utf8_lossy(&res.0), "$1\r\nb\r\n");
    }

    #[test]
    fn on_cmd_select_test() {
        let _ = env_logger::try_init();

        let redis_test = RedisTest {};
        //let redis_test= Arc::new(redis_test);

        let mut tags: HashMap<String, String> = HashMap::with_capacity(1);
        tags.insert(Rato::AUTH_TAG.to_string(), "true".to_string());
        //tags.insert(Rato::DB_TAG.to_string(), "pan".to_string());

        //let mut mulo = Rato::<RedisTest>::new(redis_test);

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"SELECT PAN_DB\r\n".to_vec());

        let res = Rato::parse_input(&redis_test, &mut tags, &mut b"GET a\r\n".to_vec());
        assert!(tags.contains_key(Rato::DB_TAG));
        let val = tags.get(Rato::DB_TAG);
        assert_eq!(val, Some(&"PAN_DB".to_string()));
    }
}