| 1 | use std::mem::{align_of, size_of}; |
| 2 | use std::str::FromStr; |
| 3 | |
| 4 | use debugid::DebugId; |
| 5 | use uuid::Uuid; |
| 6 | |
| 7 | #[test] |
| 8 | fn test_is_nil() { |
| 9 | assert!(DebugId::default().is_nil()); |
| 10 | } |
| 11 | |
| 12 | #[test] |
| 13 | fn test_parse_zero() { |
| 14 | assert_eq!( |
| 15 | DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 16 | DebugId::from_parts( |
| 17 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 18 | 0, |
| 19 | ) |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | #[test] |
| 24 | fn test_parse_short() { |
| 25 | assert_eq!( |
| 26 | DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75-a").unwrap(), |
| 27 | DebugId::from_parts( |
| 28 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 29 | 0xa, |
| 30 | ) |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | #[test] |
| 35 | fn test_parse_long() { |
| 36 | assert_eq!( |
| 37 | DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75-feedface").unwrap(), |
| 38 | DebugId::from_parts( |
| 39 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 40 | 0xfeed_face, |
| 41 | ) |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | #[test] |
| 46 | fn test_parse_compact() { |
| 47 | assert_eq!( |
| 48 | DebugId::from_str("dfb8e43af2423d73a453aeb6a777ef75feedface").unwrap(), |
| 49 | DebugId::from_parts( |
| 50 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 51 | 0xfeed_face, |
| 52 | ) |
| 53 | ); |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn test_parse_upper() { |
| 58 | assert_eq!( |
| 59 | DebugId::from_str("DFB8E43A-F242-3D73-A453-AEB6A777EF75-FEEDFACE").unwrap(), |
| 60 | DebugId::from_parts( |
| 61 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 62 | 0xfeed_face, |
| 63 | ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | #[test] |
| 68 | fn test_parse_ignores_tail() { |
| 69 | assert_eq!( |
| 70 | DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75-feedface-1-2-3").unwrap(), |
| 71 | DebugId::from_parts( |
| 72 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 73 | 0xfeed_face, |
| 74 | ) |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn test_to_string_zero() { |
| 80 | let id = DebugId::from_parts( |
| 81 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 82 | 0, |
| 83 | ); |
| 84 | |
| 85 | assert_eq!(id.to_string(), "dfb8e43a-f242-3d73-a453-aeb6a777ef75"); |
| 86 | } |
| 87 | |
| 88 | #[test] |
| 89 | fn test_to_string_short() { |
| 90 | let id = DebugId::from_parts( |
| 91 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 92 | 10, |
| 93 | ); |
| 94 | |
| 95 | assert_eq!(id.to_string(), "dfb8e43a-f242-3d73-a453-aeb6a777ef75-a"); |
| 96 | } |
| 97 | |
| 98 | #[test] |
| 99 | fn test_to_string_long() { |
| 100 | let id = DebugId::from_parts( |
| 101 | Uuid::parse_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75").unwrap(), |
| 102 | 0xfeed_face, |
| 103 | ); |
| 104 | |
| 105 | assert_eq!( |
| 106 | id.to_string(), |
| 107 | "dfb8e43a-f242-3d73-a453-aeb6a777ef75-feedface" |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn test_parse_error_short() { |
| 113 | assert!(DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef7").is_err()); |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn test_parse_error_trailing_dash() { |
| 118 | assert!(DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75-").is_err()); |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn test_parse_error_unicode() { |
| 123 | assert!(DebugId::from_str("아이쿱 조합원 앱카드").is_err()); |
| 124 | } |
| 125 | |
| 126 | #[test] |
| 127 | fn test_from_guid_age() { |
| 128 | let guid = [ |
| 129 | 0x98, 0xd1, 0xef, 0xe8, 0x6e, 0xf8, 0xfe, 0x45, 0x9d, 0xdb, 0xe1, 0x13, 0x82, 0xb5, 0xd1, |
| 130 | 0xc9, |
| 131 | ]; |
| 132 | |
| 133 | assert_eq!( |
| 134 | DebugId::from_guid_age(&guid[..], 1).unwrap(), |
| 135 | DebugId::from_str("e8efd198-f86e-45fe-9ddb-e11382b5d1c9-1").unwrap() |
| 136 | ) |
| 137 | } |
| 138 | |
| 139 | #[test] |
| 140 | fn test_parse_breakpad_zero() { |
| 141 | assert_eq!( |
| 142 | DebugId::from_breakpad("DFB8E43AF2423D73A453AEB6A777EF750").unwrap(), |
| 143 | DebugId::from_parts( |
| 144 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 145 | 0, |
| 146 | ) |
| 147 | ); |
| 148 | } |
| 149 | |
| 150 | #[test] |
| 151 | fn test_parse_breakpad_short() { |
| 152 | assert_eq!( |
| 153 | DebugId::from_breakpad("DFB8E43AF2423D73A453AEB6A777EF75a").unwrap(), |
| 154 | DebugId::from_parts( |
| 155 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 156 | 10, |
| 157 | ) |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | #[test] |
| 162 | fn test_parse_breakpad_long() { |
| 163 | assert_eq!( |
| 164 | DebugId::from_breakpad("DFB8E43AF2423D73A453AEB6A777EF75feedface").unwrap(), |
| 165 | DebugId::from_parts( |
| 166 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 167 | 0xfeed_face, |
| 168 | ) |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | #[test] |
| 173 | fn test_parse_breakpad_error_tail() { |
| 174 | assert!(DebugId::from_breakpad("DFB8E43AF2423D73A453AEB6A777EF75feedface123").is_err()); |
| 175 | } |
| 176 | |
| 177 | #[test] |
| 178 | fn test_parse_breakpad_error_missing_age() { |
| 179 | assert!(DebugId::from_breakpad("DFB8E43AF2423D73A453AEB6A777EF75").is_err()); |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn test_parse_breakpad_error_short() { |
| 184 | assert!(DebugId::from_breakpad("DFB8E43AF2423D73A453AEB6A777EF7").is_err()); |
| 185 | } |
| 186 | |
| 187 | #[test] |
| 188 | fn test_parse_breakpad_error_dashes() { |
| 189 | assert!(DebugId::from_breakpad("e8efd198-f86e-45fe-9ddb-e11382b5d1c9-1").is_err()); |
| 190 | } |
| 191 | |
| 192 | #[test] |
| 193 | fn test_to_string_breakpad_zero() { |
| 194 | let id = DebugId::from_parts( |
| 195 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 196 | 0, |
| 197 | ); |
| 198 | |
| 199 | assert_eq!( |
| 200 | id.breakpad().to_string(), |
| 201 | "DFB8E43AF2423D73A453AEB6A777EF750" |
| 202 | ); |
| 203 | } |
| 204 | |
| 205 | #[test] |
| 206 | fn test_to_string_breakpad_short() { |
| 207 | let id = DebugId::from_parts( |
| 208 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 209 | 10, |
| 210 | ); |
| 211 | |
| 212 | assert_eq!( |
| 213 | id.breakpad().to_string(), |
| 214 | "DFB8E43AF2423D73A453AEB6A777EF75a" |
| 215 | ); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn test_to_string_breakpad_long() { |
| 220 | let id = DebugId::from_parts( |
| 221 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 222 | 0xfeed_face, |
| 223 | ); |
| 224 | |
| 225 | assert_eq!( |
| 226 | id.breakpad().to_string(), |
| 227 | "DFB8E43AF2423D73A453AEB6A777EF75feedface" |
| 228 | ); |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn test_debug_id_debug() { |
| 233 | let id = DebugId::from_parts( |
| 234 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 235 | 10, |
| 236 | ); |
| 237 | |
| 238 | assert_eq!( |
| 239 | format!("{:?}", id), |
| 240 | "DebugId { uuid: \"dfb8e43a-f242-3d73-a453-aeb6a777ef75\", appendix: 10 }" |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | #[test] |
| 245 | #[cfg(feature = "with_serde")] |
| 246 | fn test_serde_serialize() { |
| 247 | let id = DebugId::from_parts( |
| 248 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 249 | 10, |
| 250 | ); |
| 251 | |
| 252 | assert_eq!( |
| 253 | serde_json::to_string(&id).expect("could not serialize"), |
| 254 | "\"dfb8e43a-f242-3d73-a453-aeb6a777ef75-a\"" |
| 255 | ) |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | #[cfg(feature = "with_serde")] |
| 260 | fn test_serde_deserialize() { |
| 261 | let id: DebugId = serde_json::from_str("\"dfb8e43a-f242-3d73-a453-aeb6a777ef75-a\"") |
| 262 | .expect("could not deserialize"); |
| 263 | |
| 264 | assert_eq!( |
| 265 | id, |
| 266 | DebugId::from_parts( |
| 267 | Uuid::parse_str("DFB8E43AF2423D73A453AEB6A777EF75").unwrap(), |
| 268 | 10, |
| 269 | ) |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | #[test] |
| 274 | fn test_pdb20() { |
| 275 | let timestamp: u32 = 0x418e89c3; |
| 276 | let age: u32 = 1; |
| 277 | let debug_id = DebugId::from_pdb20(timestamp, age); |
| 278 | |
| 279 | assert!(debug_id.is_pdb20()); |
| 280 | assert_eq!( |
| 281 | debug_id.uuid(), |
| 282 | Uuid::parse_str("418e89c3-0000-0000-0000-000000000000").unwrap() |
| 283 | ); |
| 284 | } |
| 285 | |
| 286 | #[test] |
| 287 | fn test_pdb20_format() { |
| 288 | let timestamp: u32 = 0x418e89c3; |
| 289 | let age: u32 = 1; |
| 290 | let debug_id = DebugId::from_pdb20(timestamp, age); |
| 291 | |
| 292 | assert_eq!(debug_id.to_string(), "418E89C3-1".to_string()); |
| 293 | assert_eq!(debug_id.breakpad().to_string(), "418E89C31"); |
| 294 | } |
| 295 | |
| 296 | #[test] |
| 297 | fn test_pdb20_parse() { |
| 298 | let timestamp: u32 = 0x418e89c3; |
| 299 | let age: u32 = 1; |
| 300 | let debug_id = DebugId::from_pdb20(timestamp, age); |
| 301 | |
| 302 | let s = "418E89C3-1"; |
| 303 | let parsed = DebugId::from_str(s).unwrap(); |
| 304 | assert_eq!(parsed, debug_id); |
| 305 | |
| 306 | let s = "418E89C31"; |
| 307 | let parsed = DebugId::from_str(s).unwrap(); |
| 308 | assert_eq!(parsed, debug_id); |
| 309 | |
| 310 | let s = "418E89C31"; |
| 311 | let parsed = DebugId::from_breakpad(s).unwrap(); |
| 312 | assert_eq!(parsed, debug_id); |
| 313 | |
| 314 | let s = "418E89C3-1"; |
| 315 | assert!(DebugId::from_breakpad(s).is_err()); |
| 316 | } |
| 317 | |
| 318 | /// The version of `DebugId` up to 0.7.2. |
| 319 | #[repr(C, packed)] |
| 320 | struct OldDebugId { |
| 321 | uuid: Uuid, |
| 322 | appendix: u32, |
| 323 | _padding: [u8; 12], |
| 324 | } |
| 325 | |
| 326 | #[test] |
| 327 | fn test_mem() { |
| 328 | // The size of this struct needs to be exactly aligned and can not change for backwards |
| 329 | // compatibility. |
| 330 | assert_eq!(size_of::<OldDebugId>(), 32); |
| 331 | assert_eq!(size_of::<OldDebugId>(), size_of::<DebugId>()); |
| 332 | assert_eq!(align_of::<DebugId>(), 1); |
| 333 | } |
| 334 | |
| 335 | #[test] |
| 336 | fn test_to_from_raw() { |
| 337 | let debug_id = DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75-a").unwrap(); |
| 338 | |
| 339 | // Create &[u8] from DebugId. |
| 340 | let slice = &[debug_id]; |
| 341 | let ptr = slice.as_ptr() as *const u8; |
| 342 | let len = std::mem::size_of_val(slice); |
| 343 | let buf: &[u8] = unsafe { std::slice::from_raw_parts(ptr, len) }; |
| 344 | |
| 345 | // Copy bytes to new location. |
| 346 | let mut new_buf: Vec<u8> = Vec::new(); |
| 347 | std::io::copy(&mut std::io::Cursor::new(buf), &mut new_buf).unwrap(); |
| 348 | |
| 349 | // Create DebugId from &[u8]. |
| 350 | let ptr = new_buf.as_ptr() as *const DebugId; |
| 351 | let new_debug_id = unsafe { &*ptr }; |
| 352 | |
| 353 | assert_eq!(*new_debug_id, debug_id); |
| 354 | } |
| 355 | |
| 356 | #[test] |
| 357 | fn test_from_old_raw() { |
| 358 | // Ensures we can still read previous in-memory representations into the new format. |
| 359 | let debug_id = DebugId::from_str("dfb8e43a-f242-3d73-a453-aeb6a777ef75-a").unwrap(); |
| 360 | let old_debug_id = OldDebugId { |
| 361 | uuid: debug_id.uuid(), |
| 362 | appendix: debug_id.appendix(), |
| 363 | _padding: [0; 12], |
| 364 | }; |
| 365 | |
| 366 | // Create &[u8] from OldDebugId. |
| 367 | let slice = &[old_debug_id]; |
| 368 | let ptr = slice.as_ptr() as *const u8; |
| 369 | let len = std::mem::size_of_val(slice); |
| 370 | let buf: &[u8] = unsafe { std::slice::from_raw_parts(ptr, len) }; |
| 371 | |
| 372 | // Copy bytes to new location. |
| 373 | let mut new_buf: Vec<u8> = Vec::new(); |
| 374 | std::io::copy(&mut std::io::Cursor::new(buf), &mut new_buf).unwrap(); |
| 375 | |
| 376 | // Create DebugId from &[u8]. |
| 377 | let ptr = new_buf.as_ptr() as *const DebugId; |
| 378 | let new_debug_id = unsafe { &*ptr }; |
| 379 | |
| 380 | assert_eq!(*new_debug_id, debug_id); |
| 381 | } |
| 382 | |
| 383 | #[test] |
| 384 | fn test_default() { |
| 385 | let debug_id = DebugId::default(); |
| 386 | assert_eq!(debug_id.uuid(), Uuid::nil()); |
| 387 | assert_eq!(debug_id.appendix(), 0); |
| 388 | } |