diff --git a/src/context.rs b/src/context.rs index 2079c79121..9a45770c9c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -367,7 +367,7 @@ pub unsafe fn dc_set_config( } if strcmp(key, b"selfavatar\x00" as *const u8 as *const libc::c_char) == 0 && !value.is_null() { rel_path = dc_strdup(value); - if !(0 == dc_make_rel_and_copy(context, &mut rel_path)) { + if dc_make_rel_and_copy(context, &mut rel_path) { ret = dc_sqlite3_set_config(context, &context.sql.clone().read().unwrap(), key, rel_path) } diff --git a/src/dc_array.rs b/src/dc_array.rs index be10d86ec2..a079dc03cc 100644 --- a/src/dc_array.rs +++ b/src/dc_array.rs @@ -404,6 +404,20 @@ mod tests { use super::*; use std::ffi::CStr; + #[test] + fn test_dc_arr_to_string() { + let arr2: [uint32_t; 4] = [ + 0i32 as uint32_t, + 12i32 as uint32_t, + 133i32 as uint32_t, + 1999999i32 as uint32_t, + ]; + + let str_0 = unsafe { dc_arr_to_string(arr2.as_ptr(), 4i32) }; + assert_eq!(to_string(str_0), "0,12,133,1999999"); + unsafe { free(str_0 as *mut _) }; + } + #[test] fn test_dc_array() { unsafe { @@ -491,5 +505,4 @@ mod tests { dc_array_unref(arr); } } - } diff --git a/src/dc_chat.rs b/src/dc_chat.rs index 77f6aebbd7..4463b0a59f 100644 --- a/src/dc_chat.rs +++ b/src/dc_chat.rs @@ -443,7 +443,7 @@ unsafe fn prepare_msg_common<'a>( (*msg).type_0 as libc::c_int, ); current_block = 2171833246886114521; - } else if (*msg).state == 18i32 && 0 == dc_is_blobdir_path(context, pathNfilename) { + } else if (*msg).state == 18i32 && !dc_is_blobdir_path(context, pathNfilename) { dc_log_error( context, 0i32, @@ -451,7 +451,7 @@ unsafe fn prepare_msg_common<'a>( as *const libc::c_char, ); current_block = 2171833246886114521; - } else if 0 == dc_make_rel_and_copy(context, &mut pathNfilename) { + } else if !dc_make_rel_and_copy(context, &mut pathNfilename) { current_block = 2171833246886114521; } else { dc_param_set((*msg).param, 'f' as i32, pathNfilename); @@ -1105,11 +1105,10 @@ unsafe fn set_draft_raw(context: &Context, chat_id: uint32_t, msg: *mut dc_msg_t pathNfilename = dc_param_get((*msg).param, 'f' as i32, 0 as *const libc::c_char); if pathNfilename.is_null() { current_block = 14513523936503887211; - } else if 0 != dc_msg_is_increation(msg) - && 0 == dc_is_blobdir_path(context, pathNfilename) + } else if 0 != dc_msg_is_increation(msg) && !dc_is_blobdir_path(context, pathNfilename) { current_block = 14513523936503887211; - } else if 0 == dc_make_rel_and_copy(context, &mut pathNfilename) { + } else if !dc_make_rel_and_copy(context, &mut pathNfilename) { current_block = 14513523936503887211; } else { dc_param_set((*msg).param, 'f' as i32, pathNfilename); @@ -2068,7 +2067,7 @@ pub unsafe fn dc_set_chat_profile_image( /* we shoud respect this - whatever we send to the group, it gets discarded anyway! */ if !new_image.is_null() { new_image_rel = dc_strdup(new_image); - if 0 == dc_make_rel_and_copy(context, &mut new_image_rel) { + if !dc_make_rel_and_copy(context, &mut new_image_rel) { current_block = 14766584022300871387; } else { current_block = 1856101646708284338; diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 48fb75aa3a..aa7736a7b6 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -1386,15 +1386,9 @@ pub unsafe fn dc_get_fine_pathNfilename( ret } -// TODO should return bool /rtn -pub unsafe fn dc_is_blobdir_path(context: &Context, path: *const libc::c_char) -> libc::c_int { - if strncmp(path, context.get_blobdir(), strlen(context.get_blobdir())) == 0i32 - || strncmp(path, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, 8) == 0i32 - { - return 1i32; - } - - 0 +pub unsafe fn dc_is_blobdir_path(context: &Context, path: *const libc::c_char) -> bool { + (strncmp(path, context.get_blobdir(), strlen(context.get_blobdir())) == 0 + || strncmp(path, b"$BLOBDIR\x00" as *const u8 as *const libc::c_char, 8) == 0) } pub unsafe fn dc_make_rel_path(context: &Context, path: *mut *mut libc::c_char) { @@ -1410,15 +1404,14 @@ pub unsafe fn dc_make_rel_path(context: &Context, path: *mut *mut libc::c_char) }; } -// TODO should return bool /rtn -pub unsafe fn dc_make_rel_and_copy(context: &Context, path: *mut *mut libc::c_char) -> libc::c_int { - let mut success: libc::c_int = 0i32; +pub unsafe fn dc_make_rel_and_copy(context: &Context, path: *mut *mut libc::c_char) -> bool { + let mut success: bool = false; let mut filename: *mut libc::c_char = 0 as *mut libc::c_char; let mut blobdir_path: *mut libc::c_char = 0 as *mut libc::c_char; if !(path.is_null() || (*path).is_null()) { - if 0 != dc_is_blobdir_path(context, *path) { + if dc_is_blobdir_path(context, *path) { dc_make_rel_path(context, path); - success = 1i32 + success = true; } else { filename = dc_get_filename(*path); if !(filename.is_null() @@ -1436,7 +1429,7 @@ pub unsafe fn dc_make_rel_and_copy(context: &Context, path: *mut *mut libc::c_ch *path = blobdir_path; blobdir_path = 0 as *mut libc::c_char; dc_make_rel_path(context, path); - success = 1i32 + success = true; } } } diff --git a/tests/fixtures/delta-logo.png b/tests/fixtures/delta-logo.png new file mode 100644 index 0000000000..9408ad90b4 Binary files /dev/null and b/tests/fixtures/delta-logo.png differ diff --git a/tests/stress.rs b/tests/stress.rs index cb245e21e5..1e4181a464 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -1,7 +1,9 @@ //! Stress some functions for testing; if used as a lib, this file is obsolete. use std::collections::HashSet; -use std::ffi::{CStr, CString}; +use std::env::current_dir; +use std::ffi::CString; +use std::path::PathBuf; use mmime::mailimf_types::*; use tempfile::{tempdir, TempDir}; @@ -121,21 +123,15 @@ unsafe fn stress_functions(context: &Context) { context.get_blobdir(), b"foobar\x00" as *const u8 as *const libc::c_char, ); - assert_ne!(0, dc_is_blobdir_path(context, abs_path)); - assert_ne!( - 0, - dc_is_blobdir_path( - context, - b"$BLOBDIR/fofo\x00" as *const u8 as *const libc::c_char, - ) - ); - assert_eq!( - 0, - dc_is_blobdir_path( - context, - b"/BLOBDIR/fofo\x00" as *const u8 as *const libc::c_char, - ) - ); + assert!(dc_is_blobdir_path(context, abs_path)); + assert!(dc_is_blobdir_path( + context, + b"$BLOBDIR/fofo\x00" as *const u8 as *const libc::c_char, + )); + assert!(!dc_is_blobdir_path( + context, + b"/BLOBDIR/fofo\x00" as *const u8 as *const libc::c_char, + )); assert_ne!(0, dc_file_exist(context, abs_path)); free(abs_path as *mut libc::c_void); assert_ne!( @@ -946,23 +942,60 @@ fn test_dc_get_oauth2_token() { } #[test] -fn test_stress_tests() { +fn test_dc_get_abs_path() { + let ctx = unsafe { create_test_context() }; + + let blobdir_c = unsafe { dc_get_blobdir(&ctx.ctx) }; + let mut image_path = PathBuf::from(to_string(blobdir_c)); + image_path.push("image.png"); + + let mut image_path_2 = PathBuf::from("$BLOBDIR"); + image_path_2.push("image.png"); + + let image_c = CString::new(image_path_2.to_str().unwrap()).unwrap(); + let abs_path = unsafe { dc_get_abs_path(&ctx.ctx, image_c.as_ptr()) }; + + assert_eq!(to_string(abs_path), image_path.to_str().unwrap()); +} + +#[test] +fn test_selfavatar_config() { + let ctx = unsafe { create_test_context() }; + + let mut logo_path = current_dir().unwrap(); + logo_path.push("tests"); + logo_path.push("fixtures"); + logo_path.push("delta-logo.png"); + assert!(logo_path.as_path().exists()); + + let logo_path_c = CString::new(logo_path.to_str().unwrap()).unwrap(); unsafe { - let context = create_test_context(); - stress_functions(&context.ctx); + dc_set_config( + &ctx.ctx, + b"selfavatar\x00" as *const u8 as *const libc::c_char, + logo_path_c.as_ptr(), + ); } + + let selfavatar = unsafe { + dc_get_config( + &ctx.ctx, + b"selfavatar\x00" as *const u8 as *const libc::c_char, + ) + }; + + let blobdir_c = unsafe { dc_get_blobdir(&ctx.ctx) }; + let mut image_path = PathBuf::from(to_string(blobdir_c)); + image_path.push("delta-logo.png"); + + assert!(image_path.as_path().exists()); + assert_eq!(to_string(selfavatar), image_path.to_str().unwrap()); } #[test] -fn test_arr_to_string() { - let arr2: [uint32_t; 4] = [ - 0i32 as uint32_t, - 12i32 as uint32_t, - 133i32 as uint32_t, - 1999999i32 as uint32_t, - ]; - - let str_0 = unsafe { dc_arr_to_string(arr2.as_ptr(), 4i32) }; - assert_eq!(to_string(str_0), "0,12,133,1999999"); - unsafe { free(str_0 as *mut _) }; +fn test_stress_tests() { + unsafe { + let context = create_test_context(); + stress_functions(&context.ctx); + } }