Fix FIXMEs in stress.rs (#35)

* Start by comparing strings instead of result of strcmp()

* Add failing tests for dc_trim, dc_ltrim and dc_rtrim

* Fix failing tests (use libc:isspace() which counts \r, \n etc)

* Remove FIXME for first dc_simplify_simplify()

* Fix formatting

* Fix tests for dc_param_set()

* Fix remaining FIXMEs in stress.rs

* Don't wrap libc::isspace()

* Wrap unit tests with mod tests

* Fix format
This commit is contained in:
Lars-Magnus Skog
2019-05-03 11:13:06 +02:00
committed by GitHub
parent a33eb4b715
commit 95e1cc67b9
5 changed files with 152 additions and 136 deletions

View File

@@ -205,7 +205,7 @@ pub unsafe fn dc_saxparser_parse(
if p != beg_tag_name_0 { if p != beg_tag_name_0 {
let mut after_tag_name: *mut libc::c_char = p; let mut after_tag_name: *mut libc::c_char = p;
let mut attr_index: libc::c_int = 0i32; let mut attr_index: libc::c_int = 0i32;
while 0 != isspace(*p as libc::c_int) { while 0 != libc::isspace(*p as libc::c_int) {
p = p.offset(1isize) p = p.offset(1isize)
} }
while 0 != *p as libc::c_int while 0 != *p as libc::c_int
@@ -301,7 +301,7 @@ pub unsafe fn dc_saxparser_parse(
attr_index += 2i32 attr_index += 2i32
} }
} }
while 0 != isspace(*p as libc::c_int) { while 0 != libc::isspace(*p as libc::c_int) {
p = p.offset(1isize) p = p.offset(1isize)
} }
} }
@@ -444,7 +444,7 @@ unsafe fn xml_decode(mut s: *mut libc::c_char, mut type_0: libc::c_char) -> *mut
loop { loop {
while 0 != *s as libc::c_int while 0 != *s as libc::c_int
&& *s as libc::c_int != '&' as i32 && *s as libc::c_int != '&' as i32
&& 0 == isspace(*s as libc::c_int) && 0 == libc::isspace(*s as libc::c_int)
{ {
s = s.offset(1isize) s = s.offset(1isize)
} }
@@ -539,7 +539,7 @@ unsafe fn xml_decode(mut s: *mut libc::c_char, mut type_0: libc::c_char) -> *mut
} else { } else {
s = s.offset(1isize) s = s.offset(1isize)
} }
} else if type_0 as libc::c_int == ' ' as i32 && 0 != isspace(*s as libc::c_int) { } else if type_0 as libc::c_int == ' ' as i32 && 0 != libc::isspace(*s as libc::c_int) {
let fresh6 = s; let fresh6 = s;
s = s.offset(1); s = s.offset(1);
*fresh6 = ' ' as i32 as libc::c_char *fresh6 = ' ' as i32 as libc::c_char

View File

@@ -22,14 +22,6 @@ pub fn isalnum(mut _c: libc::c_int) -> libc::c_int {
} }
} }
#[cfg(test)]
#[test]
fn test_isalnum() {
assert_eq!(isalnum(0), 0);
assert_eq!(isalnum('5' as libc::c_int), 1);
assert_eq!(isalnum('Q' as libc::c_int), 1);
}
#[inline] #[inline]
pub fn isdigit(mut _c: libc::c_int) -> libc::c_int { pub fn isdigit(mut _c: libc::c_int) -> libc::c_int {
if _c < std::u8::MAX as libc::c_int { if _c < std::u8::MAX as libc::c_int {
@@ -715,3 +707,15 @@ pub unsafe fn dc_decode_ext_header(mut to_decode: *const libc::c_char) -> *mut l
dc_strdup(to_decode) dc_strdup(to_decode)
}; };
} }
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_isalnum() {
assert_eq!(isalnum(0), 0);
assert_eq!(isalnum('5' as libc::c_int), 1);
assert_eq!(isalnum('Q' as libc::c_int), 1);
}
}

View File

@@ -110,6 +110,7 @@ pub unsafe fn dc_str_replace(
} }
return replacements; return replacements;
} }
pub unsafe fn dc_ftoa(mut f: libc::c_double) -> *mut libc::c_char { pub unsafe fn dc_ftoa(mut f: libc::c_double) -> *mut libc::c_char {
// hack around printf(%f) that may return `,` as decimal point on mac // hack around printf(%f) that may return `,` as decimal point on mac
let mut test: *mut libc::c_char = let mut test: *mut libc::c_char =
@@ -124,13 +125,14 @@ pub unsafe fn dc_ftoa(mut f: libc::c_double) -> *mut libc::c_char {
free(test as *mut libc::c_void); free(test as *mut libc::c_void);
return str; return str;
} }
pub unsafe fn dc_ltrim(mut buf: *mut libc::c_char) { pub unsafe fn dc_ltrim(mut buf: *mut libc::c_char) {
let mut len: size_t = 0i32 as size_t; let mut len: size_t = 0i32 as size_t;
let mut cur: *const libc::c_uchar = 0 as *const libc::c_uchar; let mut cur: *const libc::c_uchar = 0 as *const libc::c_uchar;
if !buf.is_null() && 0 != *buf as libc::c_int { if !buf.is_null() && 0 != *buf as libc::c_int {
len = strlen(buf); len = strlen(buf);
cur = buf as *const libc::c_uchar; cur = buf as *const libc::c_uchar;
while 0 != *cur as libc::c_int && 0 != isspace(*cur as libc::c_int) { while 0 != *cur as libc::c_int && 0 != libc::isspace(*cur as libc::c_int) {
cur = cur.offset(1isize); cur = cur.offset(1isize);
len = len.wrapping_sub(1) len = len.wrapping_sub(1)
} }
@@ -143,6 +145,7 @@ pub unsafe fn dc_ltrim(mut buf: *mut libc::c_char) {
} }
}; };
} }
pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) { pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) {
let mut len: size_t = 0i32 as size_t; let mut len: size_t = 0i32 as size_t;
let mut cur: *mut libc::c_uchar = 0 as *mut libc::c_uchar; let mut cur: *mut libc::c_uchar = 0 as *mut libc::c_uchar;
@@ -151,12 +154,12 @@ pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) {
cur = (buf as *mut libc::c_uchar) cur = (buf as *mut libc::c_uchar)
.offset(len as isize) .offset(len as isize)
.offset(-1isize); .offset(-1isize);
while cur != buf as *mut libc::c_uchar && 0 != isspace(*cur as libc::c_int) { while cur != buf as *mut libc::c_uchar && 0 != libc::isspace(*cur as libc::c_int) {
cur = cur.offset(-1isize); cur = cur.offset(-1isize);
len = len.wrapping_sub(1) len = len.wrapping_sub(1)
} }
*cur.offset( *cur.offset(
(if 0 != isspace(*cur as libc::c_int) { (if 0 != libc::isspace(*cur as libc::c_int) {
0i32 0i32
} else { } else {
1i32 1i32
@@ -164,10 +167,12 @@ pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) {
) = '\u{0}' as i32 as libc::c_uchar ) = '\u{0}' as i32 as libc::c_uchar
}; };
} }
pub unsafe fn dc_trim(mut buf: *mut libc::c_char) { pub unsafe fn dc_trim(mut buf: *mut libc::c_char) {
dc_ltrim(buf); dc_ltrim(buf);
dc_rtrim(buf); dc_rtrim(buf);
} }
/* the result must be free()'d */ /* the result must be free()'d */
pub unsafe fn dc_strlower(mut in_0: *const libc::c_char) -> *mut libc::c_char { pub unsafe fn dc_strlower(mut in_0: *const libc::c_char) -> *mut libc::c_char {
let mut out: *mut libc::c_char = dc_strdup(in_0); let mut out: *mut libc::c_char = dc_strdup(in_0);
@@ -1652,3 +1657,60 @@ pub unsafe fn dc_make_rel_and_copy(
free(filename as *mut libc::c_void); free(filename as *mut libc::c_void);
return success; return success;
} }
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CStr;
#[test]
fn test_dc_ltrim() {
unsafe {
let mut html: *const libc::c_char =
b"\r\r\nline1<br>\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char;
let mut out: *mut libc::c_char = 0 as *mut libc::c_char;
out = strndup(html, strlen(html) as libc::c_ulong);
dc_ltrim(out);
assert_eq!(
CStr::from_ptr(out as *const libc::c_char).to_str().unwrap(),
"line1<br>\r\n\r\n\r\rline2\n\r"
);
}
}
#[test]
fn test_dc_rtrim() {
unsafe {
let mut html: *const libc::c_char =
b"\r\r\nline1<br>\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char;
let mut out: *mut libc::c_char = 0 as *mut libc::c_char;
out = strndup(html, strlen(html) as libc::c_ulong);
dc_rtrim(out);
assert_eq!(
CStr::from_ptr(out as *const libc::c_char).to_str().unwrap(),
"\r\r\nline1<br>\r\n\r\n\r\rline2"
);
}
}
#[test]
fn test_dc_trim() {
unsafe {
let mut html: *const libc::c_char =
b"\r\r\nline1<br>\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char;
let mut out: *mut libc::c_char = 0 as *mut libc::c_char;
out = strndup(html, strlen(html) as libc::c_ulong);
dc_trim(out);
assert_eq!(
CStr::from_ptr(out as *const libc::c_char).to_str().unwrap(),
"line1<br>\r\n\r\n\r\rline2"
);
}
}
}

View File

@@ -1205,15 +1205,6 @@ pub unsafe fn isascii(mut _c: libc::c_int) -> libc::c_int {
return (_c & !0x7fi32 == 0i32) as libc::c_int; return (_c & !0x7fi32 == 0i32) as libc::c_int;
} }
#[inline]
pub unsafe fn isspace(mut _c: libc::c_int) -> libc::c_int {
if _c < std::u8::MAX as libc::c_int {
((_c as u8 as char) == ' ') as libc::c_int
} else {
0
}
}
#[inline] #[inline]
pub unsafe fn tolower(mut _c: libc::c_int) -> libc::c_int { pub unsafe fn tolower(mut _c: libc::c_int) -> libc::c_int {
return __tolower(_c); return __tolower(_c);

View File

@@ -63,14 +63,14 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) {
let mut plain: *mut libc::c_char = let mut plain: *mut libc::c_char =
dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1i32, 0i32); dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1i32, 0i32);
// FIXME assert_eq!(
// assert_eq!( CStr::from_ptr(plain as *const libc::c_char)
// strcmp(plain, b"line1\nline2\x00" as *const u8 as *const libc::c_char), .to_str()
// 0, .unwrap(),
// "{:?}", "line1\nline2",
// std::ffi::CStr::from_ptr(plain), );
// ); free(plain as *mut libc::c_void);
// free(plain as *mut libc::c_void);
html = b"<a href=url>text</a\x00" as *const u8 as *const libc::c_char; html = b"<a href=url>text</a\x00" as *const u8 as *const libc::c_char;
plain = dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1i32, 0i32); plain = dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1i32, 0i32);
if 0 != !(strcmp( if 0 != !(strcmp(
@@ -2711,40 +2711,28 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) {
); );
} else { } else {
}; };
dc_param_set(p1, 'b' as i32, 0 as *const libc::c_char); dc_param_set(p1, 'b' as i32, 0 as *const libc::c_char);
// FIXME
// if 0 != !(strcmp( assert_eq!(
// (*p1).packed, CStr::from_ptr((*p1).packed as *const libc::c_char)
// b"a=foo\nd=4\x00" as *const u8 as *const libc::c_char, .to_str()
// ) == 0i32) as libc::c_int as libc::c_long .unwrap(),
// { "a=foo\nd=4",
// println!("{:?}", std::ffi::CStr::from_ptr((*p1).packed)); );
// __assert_rtn(
// (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00"))
// .as_ptr(),
// b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char,
// 706i32,
// b"strcmp(p1->packed, \"a=foo\\nd=4\")==0\x00" as *const u8 as *const libc::c_char,
// );
// } else {
// };
dc_param_set(p1, 'a' as i32, 0 as *const libc::c_char); dc_param_set(p1, 'a' as i32, 0 as *const libc::c_char);
dc_param_set(p1, 'd' as i32, 0 as *const libc::c_char); dc_param_set(p1, 'd' as i32, 0 as *const libc::c_char);
// FIXME assert_eq!(
// if 0 != !(strcmp((*p1).packed, b"\x00" as *const u8 as *const libc::c_char) == 0i32) CStr::from_ptr((*p1).packed as *const libc::c_char)
// as libc::c_int as libc::c_long .to_str()
// { .unwrap(),
// __assert_rtn( "",
// (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) );
// .as_ptr(),
// b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char,
// 710i32,
// b"strcmp(p1->packed, \"\")==0\x00" as *const u8 as *const libc::c_char,
// );
// } else {
// };
dc_param_unref(p1); dc_param_unref(p1);
let mut keys: *mut libc::c_char = dc_get_config( let mut keys: *mut libc::c_char = dc_get_config(
context, context,
b"sys.config_keys\x00" as *const u8 as *const libc::c_char, b"sys.config_keys\x00" as *const u8 as *const libc::c_char,
@@ -3457,21 +3445,17 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) {
); );
} else { } else {
}; };
// FIXME
// if 0 != !(!base64.is_null() assert!(!base64.is_null());
// && strcmp(base64, b"data\x00" as *const u8 as *const libc::c_char) == 0i32) assert_eq!(
// as libc::c_int as libc::c_long CStr::from_ptr(base64 as *const libc::c_char)
// { .to_str()
// __assert_rtn( .unwrap(),
// (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) "data",
// .as_ptr(), );
// b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char,
// 823i32,
// b"base64 && strcmp(base64, \"data\") == 0\x00" as *const u8 as *const libc::c_char,
// );
// } else {
// };
free(buf_0 as *mut libc::c_void); free(buf_0 as *mut libc::c_void);
buf_0 = buf_0 =
strdup(b"-----BEGIN PGP MESSAGE-----\n\ndat1\n-----END PGP MESSAGE-----\n-----BEGIN PGP MESSAGE-----\n\ndat2\n-----END PGP MESSAGE-----\x00" strdup(b"-----BEGIN PGP MESSAGE-----\n\ndat1\n-----END PGP MESSAGE-----\n-----BEGIN PGP MESSAGE-----\n\ndat2\n-----END PGP MESSAGE-----\x00"
as *const u8 as *const libc::c_char); as *const u8 as *const libc::c_char);
@@ -3509,21 +3493,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) {
} else { } else {
}; };
// FIXME assert!(!base64.is_null());
// if 0 != !(!base64.is_null() assert_eq!(
// && strcmp(base64, b"dat1\x00" as *const u8 as *const libc::c_char) == 0i32) CStr::from_ptr(base64 as *const libc::c_char)
// as libc::c_int as libc::c_long .to_str()
// { .unwrap(),
// __assert_rtn( "dat1",
// (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) );
// .as_ptr(),
// b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char,
// 830i32,
// b"base64 && strcmp(base64, \"dat1\") == 0\x00" as *const u8 as *const libc::c_char,
// );
// } else {
// };
free(buf_0 as *mut libc::c_void); free(buf_0 as *mut libc::c_void);
buf_0 = strdup( buf_0 = strdup(
b"foo \n -----BEGIN PGP MESSAGE----- \n base64-123 \n -----END PGP MESSAGE-----\x00" b"foo \n -----BEGIN PGP MESSAGE----- \n base64-123 \n -----END PGP MESSAGE-----\x00"
as *const u8 as *const libc::c_char, as *const u8 as *const libc::c_char,
@@ -3572,23 +3551,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) {
} else { } else {
}; };
// FIXME assert!(!base64.is_null());
// if 0 != !(!base64.is_null() assert_eq!(
// && strcmp( CStr::from_ptr(base64 as *const libc::c_char)
// base64, .to_str()
// b"base64-123\x00" as *const u8 as *const libc::c_char, .unwrap(),
// ) == 0i32) as libc::c_int as libc::c_long "base64-123",
// { );
// __assert_rtn(
// (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00"))
// .as_ptr(),
// b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char,
// 838i32,
// b"base64 && strcmp(base64, \"base64-123\")==0\x00" as *const u8 as *const libc::c_char,
// );
// } else {
// };
free(buf_0 as *mut libc::c_void); free(buf_0 as *mut libc::c_void);
buf_0 = strdup(b"foo-----BEGIN PGP MESSAGE-----\x00" as *const u8 as *const libc::c_char); buf_0 = strdup(b"foo-----BEGIN PGP MESSAGE-----\x00" as *const u8 as *const libc::c_char);
ok = dc_split_armored_data( ok = dc_split_armored_data(
buf_0, buf_0,
@@ -3661,24 +3633,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) {
} else { } else {
}; };
// FIXME assert!(!base64.is_null());
// if 0 != !(!base64.is_null() assert_eq!(
// && strcmp( CStr::from_ptr(base64 as *const libc::c_char)
// base64, .to_str()
// b"base64-567 \n abc\x00" as *const u8 as *const libc::c_char, .unwrap(),
// ) == 0i32) as libc::c_int as libc::c_long "base64-567 \n abc",
// { );
// __assert_rtn(
// (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00"))
// .as_ptr(),
// b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char,
// 851i32,
// b"base64 && strcmp(base64, \"base64-567 \\n abc\")==0\x00" as *const u8
// as *const libc::c_char,
// );
// } else {
// };
free(buf_0 as *mut libc::c_void); free(buf_0 as *mut libc::c_void);
buf_0 = buf_0 =
strdup(b"-----BEGIN PGP PRIVATE KEY BLOCK-----\n Autocrypt-Prefer-Encrypt : mutual \n\nbase64\n-----END PGP PRIVATE KEY BLOCK-----\x00" strdup(b"-----BEGIN PGP PRIVATE KEY BLOCK-----\n Autocrypt-Prefer-Encrypt : mutual \n\nbase64\n-----END PGP PRIVATE KEY BLOCK-----\x00"
as *const u8 as *const libc::c_char); as *const u8 as *const libc::c_char);
@@ -3732,21 +3696,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) {
} else { } else {
}; };
// FIXME assert!(!base64.is_null());
// if 0 != !(!base64.is_null() assert_eq!(
// && strcmp(base64, b"base64\x00" as *const u8 as *const libc::c_char) == 0i32) CStr::from_ptr(base64 as *const libc::c_char)
// as libc::c_int as libc::c_long .to_str()
// { .unwrap(),
// __assert_rtn( "base64",
// (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) );
// .as_ptr(),
// b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char,
// 859i32,
// b"base64 && strcmp(base64, \"base64\")==0\x00" as *const u8 as *const libc::c_char,
// );
// } else {
// };
free(buf_0 as *mut libc::c_void); free(buf_0 as *mut libc::c_void);
let mut norm: *mut libc::c_char = dc_normalize_setup_code( let mut norm: *mut libc::c_char = dc_normalize_setup_code(
context, context,
b"123422343234423452346234723482349234\x00" as *const u8 as *const libc::c_char, b"123422343234423452346234723482349234\x00" as *const u8 as *const libc::c_char,