Skip to content

Commit baf8cdd

Browse files
committed
test: ✅ add serde json roundtrip test
1 parent a33f599 commit baf8cdd

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

test-certs/src/configuration/certificates.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ use std::collections::HashMap;
55
use serde::{Deserialize, Serialize};
66

77
/// This is the root structure that contains all certificate chains.
8-
#[derive(Debug, Serialize, Deserialize)]
8+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
99
pub struct Certificates {
1010
/// All certificates
1111
#[serde(flatten)]
1212
pub certificates: HashMap<String, CertificateTypes>,
1313
}
1414

1515
/// The certificate authority to sign other certificates.
16-
#[derive(Debug, Default, Serialize, Deserialize)]
16+
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
1717
#[serde(default, deny_unknown_fields)]
1818
pub struct CertificateAuthority {
1919
/// Enables the export of the private key file
@@ -25,23 +25,23 @@ pub struct CertificateAuthority {
2525
}
2626

2727
/// A certificate used for client authentication
28-
#[derive(Debug, Serialize, Deserialize)]
28+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
2929
#[serde(default, deny_unknown_fields)]
3030
pub struct Client {
3131
/// Enables the export of the private key file
3232
pub export_key: bool,
3333
}
3434

3535
/// A certificate used for server authentication
36-
#[derive(Debug, Serialize, Deserialize)]
36+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
3737
#[serde(default, deny_unknown_fields)]
3838
pub struct Server {
3939
/// Enables the export of the private key file
4040
pub export_key: bool,
4141
}
4242

4343
/// All kinds of different certificates
44-
#[derive(Debug, Serialize, Deserialize)]
44+
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
4545
#[serde(tag = "type", rename_all = "lowercase")]
4646
pub enum CertificateTypes {
4747
/// A certificate that acts as a Certificate Authority
@@ -126,7 +126,7 @@ mod tests {
126126
}
127127
}
128128
}
129-
}
129+
}
130130
});
131131

132132
let ca: CertificateTypes = serde_json::from_value(json).unwrap();
@@ -136,6 +136,27 @@ mod tests {
136136

137137
assert_eq!(intermediate_ca.certificates.len(), 2);
138138
}
139+
140+
#[test]
141+
fn should_serde_roundtrip() {
142+
let certs = Certificates {
143+
certificates: HashMap::from_iter([(
144+
"my-ca".to_string(),
145+
CertificateTypes::CertificateAuthority(CertificateAuthority {
146+
export_key: true,
147+
certificates: HashMap::from_iter([(
148+
"client".to_string(),
149+
CertificateTypes::Client(Client { export_key: true }),
150+
)]),
151+
}),
152+
)]),
153+
};
154+
155+
let serialized = serde_json::to_string(&certs).unwrap();
156+
let deserialized: Certificates = serde_json::from_str(&serialized).unwrap();
157+
158+
assert_eq!(deserialized, certs)
159+
}
139160
}
140161

141162
mod yaml {

0 commit comments

Comments
 (0)