Skip to content

Commit 55eb796

Browse files
committed
refactor(mmds): Rename new() to try_new()
The constructor for TokenAuthority can fail and returns Result. Signed-off-by: Takahiro Itazuri <[email protected]>
1 parent 1fef547 commit 55eb796

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/vmm/src/mmds/data_store.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl Mmds {
100100
}
101101
MmdsVersion::V2 => {
102102
if self.token_authority.is_none() {
103-
self.token_authority = Some(TokenAuthority::new()?);
103+
self.token_authority = Some(TokenAuthority::try_new()?);
104104
}
105105
Ok(())
106106
}

src/vmm/src/mmds/token.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl fmt::Debug for TokenAuthority {
9292

9393
impl TokenAuthority {
9494
/// Create a new token authority entity.
95-
pub fn new() -> Result<TokenAuthority, MmdsTokenError> {
95+
pub fn try_new() -> Result<TokenAuthority, MmdsTokenError> {
9696
let mut file = File::open(Path::new(RANDOMNESS_POOL))?;
9797

9898
Ok(TokenAuthority {
@@ -334,7 +334,7 @@ mod tests {
334334

335335
#[test]
336336
fn test_set_aad() {
337-
let mut token_authority = TokenAuthority::new().unwrap();
337+
let mut token_authority = TokenAuthority::try_new().unwrap();
338338
assert_eq!(token_authority.aad, "".to_string());
339339

340340
token_authority.set_aad("foo");
@@ -343,7 +343,7 @@ mod tests {
343343

344344
#[test]
345345
fn test_create_token() {
346-
let mut token_authority = TokenAuthority::new().unwrap();
346+
let mut token_authority = TokenAuthority::try_new().unwrap();
347347

348348
// Test invalid time to live value.
349349
assert_eq!(
@@ -384,7 +384,7 @@ mod tests {
384384

385385
#[test]
386386
fn test_encrypt_decrypt() {
387-
let mut token_authority = TokenAuthority::new().unwrap();
387+
let mut token_authority = TokenAuthority::try_new().unwrap();
388388
let mut file = File::open(Path::new(RANDOMNESS_POOL)).unwrap();
389389
let mut iv = [0u8; IV_LEN];
390390
file.read_exact(&mut iv).unwrap();
@@ -445,7 +445,7 @@ mod tests {
445445

446446
#[test]
447447
fn test_generate_token_secret() {
448-
let mut token_authority = TokenAuthority::new().unwrap();
448+
let mut token_authority = TokenAuthority::try_new().unwrap();
449449

450450
// Test time to live value too small.
451451
assert_eq!(
@@ -484,7 +484,7 @@ mod tests {
484484

485485
#[test]
486486
fn test_is_valid() {
487-
let mut token_authority = TokenAuthority::new().unwrap();
487+
let mut token_authority = TokenAuthority::try_new().unwrap();
488488

489489
// Test token with size bigger than expected.
490490
assert!(!token_authority.is_valid(str::repeat("a", TOKEN_LENGTH_LIMIT + 1).as_str()));
@@ -496,7 +496,7 @@ mod tests {
496496

497497
#[test]
498498
fn test_token_authority() {
499-
let mut token_authority = TokenAuthority::new().unwrap();
499+
let mut token_authority = TokenAuthority::try_new().unwrap();
500500

501501
// Generate token with lifespan of 60 seconds.
502502
let token0 = token_authority.generate_token_secret(60).unwrap();

0 commit comments

Comments
 (0)