Skip to content

test: add failing test for selfavatar #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 14 additions & 1 deletion src/dc_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -491,5 +505,4 @@ mod tests {
dc_array_unref(arr);
}
}

}
11 changes: 5 additions & 6 deletions src/dc_chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,15 @@ 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,
b"Files must be created in the blob-directory.\x00" as *const u8
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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
23 changes: 8 additions & 15 deletions src/dc_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator Author

@ralphtheninja ralphtheninja Jun 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing to do with these tests, but it came in my way and was easy to fix.

(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) {
Expand All @@ -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()
Expand All @@ -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;
}
}
}
Expand Down
Binary file added tests/fixtures/delta-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
93 changes: 63 additions & 30 deletions tests/stress.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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!(
Expand Down Expand Up @@ -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());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test works on windows and *nix.

}

#[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());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only asserting that we do have a real file in the test.


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());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resulting file in the blobdir should exist.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems windows manages to copy the file to the blobdir at least. This is good to know.

assert_eq!(to_string(selfavatar), image_path.to_str().unwrap());
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should fail on windows.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no totally sure, but dc_set_config() tries to copy the given file physically to the blob directory. i assume the file described by logo_path_c normally does not exist? maybe the behavior of copying unexistent files is slightly different at some level (dc_set_config() uses the source file when copying failed)

to have a reliable test, maybe ensure that the given file exists before calling dc_get_config()? no need to be a valid image, an empty file should do the job.

Copy link
Collaborator Author

@ralphtheninja ralphtheninja Jun 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to have a reliable test, maybe ensure that the given file exists before calling dc_get_config()? no need to be a valid image, an empty file should do the job.

That's what the test is doing, logo_path_c always points to an existing file in the fixtures folder.

}

#[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);
}
}