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
/*!
#ua-parser for rust
This is a user agent parser for Rust based on
[ua-parser](https://github.com/ua-parser).

##Usage example

```rust
use uap_rust::parser::Parser;
let agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3".to_string();
let p = Parser::new().unwrap();
let c = p.parse(agent);

println!("{:?}",c);
 //Output: Client { user_agent: UserAgent { family: "Mobile Safari", major: Some("5"), minor: Some("1"), patch: None }, os: OS { family: "iOS", major: Some("5"), minor: Some("1"), patch: Some("1"), patch_minor: None }, device: Device { family: "iPhone", brand: Some("Apple"), model: Some("iPhone") } }
```
*/

extern crate yaml_rust;
extern crate regex;

pub mod parser;
pub mod client;
pub mod ua;
pub mod os;
pub mod device;
mod result;
mod yaml;

#[cfg(test)]
mod test {
    use parser;
    use client::Client;
    use ua::UserAgent;
    use device::Device;
    use os::OS;
    use yaml::*;
    use yaml_rust::{YamlLoader, Yaml};
    use std::io::prelude::*;
    use std::fs::{File};

    #[test]
    fn basic_au_test() {
        let agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B206 Safari/7534.48.3".to_string();
        let p = parser::Parser::new().unwrap();
        let c = p.parse(agent);
        println!("{:?}",c);
        assert_eq!(Client {
            user_agent: UserAgent {
                family: "Mobile Safari".to_string(),
                major: Some("5".to_string()),
                minor: Some("1".to_string()),
                patch: None,
            },
            device: Device {
                family: "iPhone".to_string(),
                brand: Some("Apple".to_string()),
                model: Some("iPhone".to_string()),
                regex: Some("(iPhone)(?:;| Simulator;)".to_string()),
            },
            os: OS {
                family: "iOS".to_string(),
                major: Some("5".to_string()),
                minor: Some("1".to_string()),
                patch: Some("1".to_string()),
                patch_minor: None,
            }
        }, c);
    }

    #[test]
    fn test_device() {
        let p = parser::Parser::new().unwrap();
        assert!(p.devices_regex.len() > 0);
        let mut test_file = File::open("src/uap-core/tests/test_device.yaml").unwrap();
        let mut yaml_str = String::new();
        let _ = test_file.read_to_string(&mut yaml_str).unwrap();
        let y = YamlLoader::load_from_str(&yaml_str).unwrap();
        let cases = from_map(&y[0], "test_cases").unwrap();
        let failed = filter_map_over_arr(cases, |c| {
            let uas = from_map(c, "user_agent_string").unwrap().as_str().unwrap();
            let client = p.parse(uas.to_string());

            let family = compare_with_parsed(&Some(client.device.family.clone()), "family", c, &client);
            if family.is_some() {
                return family;
            }

            let brand = compare_with_parsed(&client.device.brand, "brand", c, &client);
            if brand.is_some() {
                return brand;
            }

            let model = compare_with_parsed(&client.device.model, "model", c, &client);
            if model.is_some() {
                return model;
            }
            None
        });

        //for f in failed.clone() {
        //    println!("{}", f);
        //}

        assert_eq!(0, failed.len());
    }

    #[test]
    fn test_user_agent() {
        let p = parser::Parser::new().unwrap();
        assert!(p.devices_regex.len() > 0);
        let mut test_file = File::open("src/uap-core/tests/test_ua.yaml").unwrap();
        let mut yaml_str = String::new();
        let _ = test_file.read_to_string(&mut yaml_str).unwrap();
        let y = YamlLoader::load_from_str(&yaml_str).unwrap();
        let cases = from_map(&y[0], "test_cases").unwrap();
        let failed = filter_map_over_arr(cases, |c| {
            let uas = from_map(c, "user_agent_string").unwrap().as_str().unwrap();
            let client = p.parse(uas.to_string());

            let family = compare_with_parsed(&Some(client.user_agent.family.clone()), "family", c, &client);
            if family.is_some() {
                return family;
            }

            let major = compare_with_parsed(&client.user_agent.major, "major", c, &client);
            if major.is_some() {
                return major;
            }

            let minor = compare_with_parsed(&client.user_agent.minor, "minor", c, &client);
            if minor.is_some() {
                return minor;
            }

            let patch = compare_with_parsed(&client.user_agent.patch, "patch", c, &client);
            if patch.is_some() {
                return patch;
            }

            None
        });

        //for f in failed.clone() {
        //    println!("{}", f);
        //}

        assert_eq!(0, failed.len());
    }

    #[test]
    fn test_os() {
        let p = parser::Parser::new().unwrap();
        assert!(p.devices_regex.len() > 0);
        let mut test_file = File::open("src/uap-core/tests/test_os.yaml").unwrap();
        let mut yaml_str = String::new();
        let _ = test_file.read_to_string(&mut yaml_str).unwrap();
        let y = YamlLoader::load_from_str(&yaml_str).unwrap();
        let cases = from_map(&y[0], "test_cases").unwrap();
        let failed = filter_map_over_arr(cases, |c| {
            let uas = from_map(c, "user_agent_string").unwrap().as_str().unwrap();
            let client = p.parse(uas.to_string());

            let family = compare_with_parsed(&Some(client.os.family.clone()), "family", c, &client);
            if family.is_some() {
                return family;
            }

            let major = compare_with_parsed(&client.os.major, "major", c, &client);
            if major.is_some() {
                return major;
            }

            let minor = compare_with_parsed(&client.os.minor, "minor", c, &client);
            if minor.is_some() {
                return minor;
            }

            let patch = compare_with_parsed(&client.os.patch, "patch", c, &client);
            if patch.is_some() {
                return patch;
            }

            let patch_minor = compare_with_parsed(&client.os.patch_minor, "patch_minor", c, &client);
            if patch_minor.is_some() {
                return patch_minor;
            }

            None
        });

        //for f in failed.clone() {
        //    println!("{}", f);
        //}

        assert_eq!(0, failed.len());
    }

    fn compare_with_parsed(actual: &Option<String>, mapkey: &str, c: &Yaml, client: &Client) -> Option<String> {
        let opt = from_map(c, mapkey).unwrap();
        if !opt.is_null() {
            let value = opt.as_str().unwrap().to_string();
            let parsed = actual.clone().unwrap_or(String::new());
            if parsed != value { 
                return Some(format!("{} does not match: {:?}, actual: {:?}", mapkey, c, client));
            }
        }
        None
    }

}