bump pyo3 and use anyhow for main functions

This commit is contained in:
Jean-Marie Mineau 2023-11-28 16:22:40 +01:00
parent 4e57289bab
commit b8b4e28f2d
Signed by: histausse
GPG key ID: B66AEEDA9B645AD2
10 changed files with 401 additions and 115 deletions

View file

@ -78,4 +78,108 @@ impl Serializable for EncodedAnnotation {
}
}
// TODO: add tests
// TODO: add more tests
#[cfg(test)]
mod test {
use super::*;
const ENCODED_ANNOTATION_RAW: &[u8] = &[
0xbe, 0x37, 0x01, 0xa8, 0x9f, 0x03, 0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f,
];
const ENCODED_ANNOTATION_RAW_2: &[u8] = &[0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f];
const ANNOTATION_ELEMENT_RAW: &[u8] = &[
0xa8, 0x9f, 0x03, 0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f,
];
const ENCODED_VALUE_RAW: &[u8] = &[0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f];
#[test]
fn deserialize_encoded_annotation() {
assert_eq!(
EncodedAnnotation::deserialize_from_slice(ENCODED_ANNOTATION_RAW_2).unwrap(),
EncodedAnnotation {
type_idx: Uleb128(6646),
elements: vec![AnnotationElement {
name_idx: Uleb128(46163),
value: EncodedValue::Boolean(true)
}],
}
);
assert_eq!(
EncodedAnnotation::deserialize_from_slice(ENCODED_ANNOTATION_RAW).unwrap(),
EncodedAnnotation {
type_idx: Uleb128(7102),
elements: vec![AnnotationElement {
name_idx: Uleb128(53160),
value: EncodedValue::Annotation(EncodedAnnotation {
type_idx: Uleb128(6646),
elements: vec![AnnotationElement {
name_idx: Uleb128(46163),
value: EncodedValue::Boolean(true)
}]
})
}],
}
);
}
#[test]
fn serialize_encoded_annotation() {
assert_eq!(
ENCODED_ANNOTATION_RAW_2,
EncodedAnnotation {
type_idx: Uleb128(6646),
elements: vec![AnnotationElement {
name_idx: Uleb128(46163),
value: EncodedValue::Boolean(true)
}],
}
.serialize_to_vec()
.unwrap()
.as_slice()
);
assert_eq!(
ENCODED_ANNOTATION_RAW,
EncodedAnnotation {
type_idx: Uleb128(7102),
elements: vec![AnnotationElement {
name_idx: Uleb128(53160),
value: EncodedValue::Annotation(EncodedAnnotation {
type_idx: Uleb128(6646),
elements: vec![AnnotationElement {
name_idx: Uleb128(46163),
value: EncodedValue::Boolean(true)
}]
})
}],
}
.serialize_to_vec()
.unwrap()
.as_slice()
);
}
#[test]
fn deserialize_annotation_element() {
assert_eq!(
AnnotationElement::deserialize_from_slice(ANNOTATION_ELEMENT_RAW).unwrap(),
AnnotationElement {
name_idx: Uleb128(53160),
value: EncodedValue::deserialize_from_slice(ENCODED_VALUE_RAW).unwrap(),
}
);
}
#[test]
fn serialize_annotation_element() {
assert_eq!(
ANNOTATION_ELEMENT_RAW,
AnnotationElement {
name_idx: Uleb128(53160),
value: EncodedValue::deserialize_from_slice(ENCODED_VALUE_RAW).unwrap(),
}
.serialize_to_vec()
.unwrap()
.as_slice(),
);
}
}

View file

@ -233,3 +233,39 @@ pub enum AnnotationVisibility {
#[prefix(0x02)]
System,
}
#[cfg(test)]
mod test {
use super::*;
const ANNOTATION_ITEM: &[u8] = &[
0x02, 0xbe, 0x37, 0x01, 0xa8, 0x9f, 0x03, 0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f,
];
#[test]
fn deserialize_annotation_item() {
assert_eq!(
AnnotationItem::deserialize_from_slice(ANNOTATION_ITEM).unwrap(),
AnnotationItem {
visibility: AnnotationVisibility::System,
annotation: EncodedAnnotation::deserialize_from_slice(&ANNOTATION_ITEM[1..])
.unwrap(),
}
);
}
#[test]
fn serialize_annotation_item() {
assert_eq!(
ANNOTATION_ITEM,
AnnotationItem {
visibility: AnnotationVisibility::System,
annotation: EncodedAnnotation::deserialize_from_slice(&ANNOTATION_ITEM[1..])
.unwrap(),
}
.serialize_to_vec()
.unwrap()
.as_slice()
);
}
}

View file

@ -197,7 +197,7 @@ impl<'a> DexFileReader<'a> {
fn sanity_check(&self) -> Result<()> {
if self.header.magic.version != [0x30, 0x33, 0x39] {
warn!(
"DEX 039 is the only verion currently supported, found {}",
"DEX 039 is the only version currently supported, found {}",
std::str::from_utf8(self.header.magic.version.as_slice())
.unwrap_or(&format!("{:x?}", self.header.magic.version))
);

View file

@ -539,7 +539,7 @@ impl Serializable for EncodedValue {
"Unexpected value argument 0x{arg:02x}: expected 0 for VALUE_ANNOTATION"
)))
} else {
Ok(Self::Array(EncodedArray::deserialize(input)?))
Ok(Self::Annotation(EncodedAnnotation::deserialize(input)?))
}
}
VALUE_NULL => {
@ -681,6 +681,7 @@ impl Serializable for Idx {
#[cfg(test)]
mod test {
use super::*;
use crate::*;
#[test]
fn boolean_deserialize() {
@ -706,4 +707,35 @@ mod test {
vec![0b0011_1111]
);
}
#[test]
fn annotation_deserialize() {
assert_eq!(
EncodedValue::deserialize_from_slice(&[0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f])
.unwrap(),
EncodedValue::Annotation(EncodedAnnotation {
type_idx: Uleb128(6646),
elements: vec![AnnotationElement {
name_idx: Uleb128(46163),
value: EncodedValue::Boolean(true)
}]
})
);
}
#[test]
fn annotation_serialize() {
assert_eq!(
vec![0x1d, 0xf6, 0x33, 0x01, 0xd3, 0xe8, 0x02, 0x3f],
EncodedValue::Annotation(EncodedAnnotation {
type_idx: Uleb128(6646),
elements: vec![AnnotationElement {
name_idx: Uleb128(46163),
value: EncodedValue::Boolean(true)
}]
})
.serialize_to_vec()
.unwrap()
);
}
}