diff --git a/src/dc_strencode.rs b/src/dc_strencode.rs index bc5ad7164..a0a73cf50 100644 --- a/src/dc_strencode.rs +++ b/src/dc_strencode.rs @@ -728,6 +728,7 @@ pub unsafe fn dc_decode_ext_header(to_decode: *const libc::c_char) -> *mut libc: #[cfg(test)] mod tests { use super::*; + use std::ffi::CStr; #[test] fn test_isalnum() { @@ -735,4 +736,159 @@ mod tests { assert_eq!(isalnum('5' as libc::c_int), 1); assert_eq!(isalnum('Q' as libc::c_int), 1); } + + #[test] + fn test_dc_decode_header_words() { + unsafe { + let mut buf1: *mut libc::c_char = dc_decode_header_words( + b"=?utf-8?B?dGVzdMOkw7bDvC50eHQ=?=\x00" as *const u8 as *const libc::c_char, + ); + assert_eq!( + strcmp( + buf1, + b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char + ), + 0 + ); + free(buf1 as *mut libc::c_void); + + buf1 = + dc_decode_header_words(b"just ascii test\x00" as *const u8 as *const libc::c_char); + assert_eq!(CStr::from_ptr(buf1).to_str().unwrap(), "just ascii test"); + free(buf1 as *mut libc::c_void); + + buf1 = dc_encode_header_words(b"abcdef\x00" as *const u8 as *const libc::c_char); + assert_eq!(CStr::from_ptr(buf1).to_str().unwrap(), "abcdef"); + free(buf1 as *mut libc::c_void); + + buf1 = dc_encode_header_words( + b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char, + ); + assert_eq!( + strncmp(buf1, b"=?utf-8\x00" as *const u8 as *const libc::c_char, 7), + 0 + ); + + let buf2: *mut libc::c_char = dc_decode_header_words(buf1); + assert_eq!( + strcmp( + buf2, + b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char + ), + 0 + ); + free(buf1 as *mut libc::c_void); + free(buf2 as *mut libc::c_void); + + buf1 = dc_decode_header_words( + b"=?ISO-8859-1?Q?attachment=3B=0D=0A_filename=3D?= =?ISO-8859-1?Q?=22test=E4=F6=FC=2Etxt=22=3B=0D=0A_size=3D39?=\x00" as *const u8 as *const libc::c_char + ); + assert_eq!( + strcmp( + buf1, + b"attachment;\r\n filename=\"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\";\r\n size=39\x00" as *const u8 as *const libc::c_char, + + ), + 0 + ); + free(buf1 as *mut libc::c_void); + } + } + + #[test] + fn test_dc_encode_ext_header() { + unsafe { + let mut buf1 = dc_encode_ext_header( + b"Bj\xc3\xb6rn Petersen\x00" as *const u8 as *const libc::c_char, + ); + assert_eq!( + CStr::from_ptr(buf1).to_str().unwrap(), + "utf-8\'\'Bj%C3%B6rn%20Petersen" + ); + let buf2 = dc_decode_ext_header(buf1); + assert_eq!( + strcmp( + buf2, + b"Bj\xc3\xb6rn Petersen\x00" as *const u8 as *const libc::c_char, + ), + 0 + ); + free(buf1 as *mut libc::c_void); + free(buf2 as *mut libc::c_void); + + buf1 = dc_decode_ext_header( + b"iso-8859-1\'en\'%A3%20rates\x00" as *const u8 as *const libc::c_char, + ); + assert_eq!( + strcmp( + buf1, + b"\xc2\xa3 rates\x00" as *const u8 as *const libc::c_char, + ), + 0 + ); + free(buf1 as *mut libc::c_void); + + buf1 = dc_decode_ext_header(b"wrong\'format\x00" as *const u8 as *const libc::c_char); + assert_eq!( + strcmp( + buf1, + b"wrong\'format\x00" as *const u8 as *const libc::c_char, + ), + 0 + ); + free(buf1 as *mut libc::c_void); + + buf1 = dc_decode_ext_header(b"\'\'\x00" as *const u8 as *const libc::c_char); + assert_eq!( + strcmp(buf1, b"\'\'\x00" as *const u8 as *const libc::c_char), + 0 + ); + free(buf1 as *mut libc::c_void); + + buf1 = dc_decode_ext_header(b"x\'\'\x00" as *const u8 as *const libc::c_char); + assert_eq!(strcmp(buf1, b"\x00" as *const u8 as *const libc::c_char), 0); + free(buf1 as *mut libc::c_void); + + buf1 = dc_decode_ext_header(b"\'\x00" as *const u8 as *const libc::c_char); + assert_eq!( + strcmp(buf1, b"\'\x00" as *const u8 as *const libc::c_char), + 0 + ); + free(buf1 as *mut libc::c_void); + + buf1 = dc_decode_ext_header(b"\x00" as *const u8 as *const libc::c_char); + assert_eq!(strcmp(buf1, b"\x00" as *const u8 as *const libc::c_char), 0); + free(buf1 as *mut libc::c_void); + } + } + + #[test] + fn test_dc_needs_ext_header() { + unsafe { + assert_eq!( + dc_needs_ext_header(b"Bj\xc3\xb6rn\x00" as *const u8 as *const libc::c_char), + true + ); + assert_eq!( + dc_needs_ext_header(b"Bjoern\x00" as *const u8 as *const libc::c_char), + false + ); + assert_eq!( + dc_needs_ext_header(b"\x00" as *const u8 as *const libc::c_char), + false + ); + assert_eq!( + dc_needs_ext_header(b" \x00" as *const u8 as *const libc::c_char), + true + ); + assert_eq!( + dc_needs_ext_header(b"a b\x00" as *const u8 as *const libc::c_char), + true + ); + assert_eq!( + dc_needs_ext_header(0 as *const u8 as *const libc::c_char), + false + ); + } + } } diff --git a/tests/stress.rs b/tests/stress.rs index 50bb3e462..ffaa84437 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -510,313 +510,6 @@ unsafe fn stress_functions(context: &dc_context_t) { } else { }; free(buf1 as *mut libc::c_void); - buf1 = dc_decode_header_words( - b"=?utf-8?B?dGVzdMOkw7bDvC50eHQ=?=\x00" as *const u8 as *const libc::c_char, - ); - if 0 != !(strcmp( - buf1, - b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 538i32, - b"strcmp(buf1, \"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\")==0\x00" as *const u8 - as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_decode_header_words(b"just ascii test\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp( - buf1, - b"just ascii test\x00" as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 542i32, - b"strcmp(buf1, \"just ascii test\")==0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_encode_header_words(b"abcdef\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp(buf1, b"abcdef\x00" as *const u8 as *const libc::c_char) == 0i32) - as libc::c_int as libc::c_long - { - __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, - 546i32, - b"strcmp(buf1, \"abcdef\")==0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_encode_header_words( - b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char, - ); - if 0 != !(strncmp(buf1, b"=?utf-8\x00" as *const u8 as *const libc::c_char, 7) == 0i32) - as libc::c_int as libc::c_long - { - __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, - 550i32, - b"strncmp(buf1, \"=?utf-8\", 7)==0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - buf2 = dc_decode_header_words(buf1); - if 0 != !(strcmp( - b"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\x00" as *const u8 as *const libc::c_char, - buf2, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 552i32, - b"strcmp(\"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\", buf2)==0\x00" as *const u8 - as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - free(buf2 as *mut libc::c_void); - buf1 = - dc_decode_header_words(b"=?ISO-8859-1?Q?attachment=3B=0D=0A_filename=3D?= =?ISO-8859-1?Q?=22test=E4=F6=FC=2Etxt=22=3B=0D=0A_size=3D39?=\x00" - as *const u8 as *const libc::c_char); - if 0 != !(strcmp( - buf1, - b"attachment;\r\n filename=\"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\";\r\n size=39\x00" - as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, 557i32, - b"strcmp(buf1, \"attachment;\\r\\n filename=\\\"test\xc3\xa4\xc3\xb6\xc3\xbc.txt\\\";\\r\\n size=39\")==0\x00" - as *const u8 as *const libc::c_char); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_encode_ext_header(b"Bj\xc3\xb6rn Petersen\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp( - buf1, - b"utf-8\'\'Bj%C3%B6rn%20Petersen\x00" as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 561i32, - b"strcmp(buf1, \"utf-8\'\'Bj%C3%B6rn%20Petersen\") == 0\x00" as *const u8 - as *const libc::c_char, - ); - } else { - }; - buf2 = dc_decode_ext_header(buf1); - if 0 != !(strcmp( - buf2, - b"Bj\xc3\xb6rn Petersen\x00" as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 563i32, - b"strcmp(buf2, \"Bj\xc3\xb6rn Petersen\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - free(buf2 as *mut libc::c_void); - buf1 = dc_decode_ext_header( - b"iso-8859-1\'en\'%A3%20rates\x00" as *const u8 as *const libc::c_char, - ); - if 0 != !(strcmp( - buf1, - b"\xc2\xa3 rates\x00" as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 568i32, - b"strcmp(buf1, \"\xc2\xa3 rates\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - if 0 != !(strcmp( - buf1, - b"\xc2\xa3 rates\x00" as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 569i32, - b"strcmp(buf1, \"\\xC2\\xA3 rates\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_decode_ext_header(b"wrong\'format\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp( - buf1, - b"wrong\'format\x00" as *const u8 as *const libc::c_char, - ) == 0i32) as libc::c_int as libc::c_long - { - __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, - 573i32, - b"strcmp(buf1, \"wrong\'format\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_decode_ext_header(b"\'\'\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp(buf1, b"\'\'\x00" as *const u8 as *const libc::c_char) == 0i32) as libc::c_int - as libc::c_long - { - __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, - 577i32, - b"strcmp(buf1, \"\'\'\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_decode_ext_header(b"x\'\'\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp(buf1, b"\x00" as *const u8 as *const libc::c_char) == 0i32) as libc::c_int - as libc::c_long - { - __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, - 581i32, - b"strcmp(buf1, \"\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_decode_ext_header(b"\'\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp(buf1, b"\'\x00" as *const u8 as *const libc::c_char) == 0i32) as libc::c_int - as libc::c_long - { - __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, - 585i32, - b"strcmp(buf1, \"\'\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - buf1 = dc_decode_ext_header(b"\x00" as *const u8 as *const libc::c_char); - if 0 != !(strcmp(buf1, b"\x00" as *const u8 as *const libc::c_char) == 0i32) as libc::c_int - as libc::c_long - { - __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, - 589i32, - b"strcmp(buf1, \"\") == 0\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - free(buf1 as *mut libc::c_void); - if 0 != (!dc_needs_ext_header(b"Bj\xc3\xb6rn\x00" as *const u8 as *const libc::c_char)) - as libc::c_int as libc::c_long - { - __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, - 592i32, - b"dc_needs_ext_header(\"Bj\xc3\xb6rn\")\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - if 0 != (dc_needs_ext_header(b"Bjoern\x00" as *const u8 as *const libc::c_char)) as libc::c_int - as libc::c_long - { - __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, - 593i32, - b"!dc_needs_ext_header(\"Bjoern\")\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - if 0 != (dc_needs_ext_header(b"\x00" as *const u8 as *const libc::c_char)) as libc::c_int - as libc::c_long - { - __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, - 594i32, - b"!dc_needs_ext_header(\"\")\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - if 0 != (!dc_needs_ext_header(b" \x00" as *const u8 as *const libc::c_char)) as libc::c_int - as libc::c_long - { - __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, - 595i32, - b"dc_needs_ext_header(\" \")\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - if 0 != (!dc_needs_ext_header(b"a b\x00" as *const u8 as *const libc::c_char)) as libc::c_int - as libc::c_long - { - __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, - 596i32, - b"dc_needs_ext_header(\"a b\")\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; - if 0 != (dc_needs_ext_header(0 as *const libc::c_char)) as libc::c_int as libc::c_long { - __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, - 597i32, - b"!dc_needs_ext_header(NULL)\x00" as *const u8 as *const libc::c_char, - ); - } else { - }; buf1 = dc_encode_modified_utf7( b"Bj\xc3\xb6rn Petersen\x00" as *const u8 as *const libc::c_char, 1i32,