blob: 348637cfb3ef2e567d7dc9c0fce7a894d007f552 [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use super::*;
use pretty_assertions::assert_eq;
use serde_json::json;
#[test]
pub fn basic_serialization_test() {
let expected = json!({
"request": {
"protocol": "3.0",
"updater": "some_updater",
"updaterversion": "1.0",
"installsource": "ondemand",
"ismachine": true,
"os": {
"platform": "some_platform",
"version": "4.5",
"sp": "0.1",
"arch": "some_architecture"
},
"app": [
{
"appid": "{00000000-0000-0000-0000-000000000001}",
"version": "1.2.3.4",
"updatecheck": {},
},
],
}
});
let request = RequestWrapper {
request: Request {
protocol_version: "3.0".to_string(),
updater: "some_updater".to_string(),
updater_version: "1.0".to_string(),
install_source: InstallSource::OnDemand,
is_machine: true,
os: OS {
platform: "some_platform".to_string(),
version: "4.5".to_string(),
service_pack: "0.1".to_string(),
arch: "some_architecture".to_string(),
},
apps: vec![App {
id: "{00000000-0000-0000-0000-000000000001}".to_string(),
version: "1.2.3.4".to_string(),
update_check: Some(UpdateCheck::default()),
..App::default()
}],
},
};
// serde_json::to_value() is used to ensure that the fields are ordered the same as the JSON
// generated by the json!() macro.
assert_eq!(expected, serde_json::to_value(&request).unwrap());
}
#[test]
pub fn basic_ping_serialization_test() {
let expected = json!({
"request":{
"protocol": "3.0",
"updater": "some_updater",
"updaterversion": "1.0",
"installsource": "scheduledtask",
"ismachine": true,
"os": {
"platform": "some_platform",
"version": "4.5",
"sp": "0.1",
"arch": "some_architecture"
},
"app": [
{
"appid": "{00000000-0000-0000-0000-000000000001}",
"version": "1.2.3.4",
"ping": {
"ad": 2000,
"rd": 2001,
}
},
],
}
});
let request = RequestWrapper {
request: Request {
protocol_version: "3.0".to_string(),
updater: "some_updater".to_string(),
updater_version: "1.0".to_string(),
install_source: InstallSource::ScheduledTask,
is_machine: true,
os: OS {
platform: "some_platform".to_string(),
version: "4.5".to_string(),
service_pack: "0.1".to_string(),
arch: "some_architecture".to_string(),
},
apps: vec![App {
id: "{00000000-0000-0000-0000-000000000001}".to_string(),
version: "1.2.3.4".to_string(),
ping: Some(Ping { date_last_active: Some(2000), date_last_roll_call: Some(2001) }),
..App::default()
}],
},
};
// serde_json::to_value() is used to ensure that the fields are ordered the same as the JSON
// generated by the json!() macro.
assert_eq!(expected, serde_json::to_value(&request).unwrap());
}
#[test]
pub fn basic_event_serialization_test() {
let expected = json!({
"request": {
"protocol": "3.0",
"updater": "some_updater",
"updaterversion": "1.0",
"installsource": "ondemand",
"ismachine": true,
"os": {
"platform": "some_platform",
"version": "4.5",
"sp": "0.1",
"arch": "some_architecture"
},
"app": [
{
"appid": "{00000000-0000-0000-0000-000000000001}",
"version": "1.2.3.4",
"event": [{
"eventtype": 2,
"eventresult": 1,
"errorcode": 0
}],
},
],
}
});
let request = RequestWrapper {
request: Request {
protocol_version: "3.0".to_string(),
updater: "some_updater".to_string(),
updater_version: "1.0".to_string(),
install_source: InstallSource::OnDemand,
is_machine: true,
os: OS {
platform: "some_platform".to_string(),
version: "4.5".to_string(),
service_pack: "0.1".to_string(),
arch: "some_architecture".to_string(),
},
apps: vec![App {
id: "{00000000-0000-0000-0000-000000000001}".to_string(),
version: "1.2.3.4".to_string(),
events: vec![Event {
event_type: EventType::InstallComplete,
event_result: EventResult::Success,
errorcode: Some(0),
..Event::default()
}],
..App::default()
}],
},
};
// serde_json::to_value() is used to ensure that the fields are ordered the same as the JSON
// generated by the json!() macro.
assert_eq!(expected, serde_json::to_value(&request).unwrap());
}
#[test]
pub fn multiple_event_serialization_test() {
let expected = json!({
"request": {
"protocol": "3.0",
"updater": "some_updater",
"updaterversion": "1.0",
"installsource": "ondemand",
"ismachine": true,
"os": {
"platform": "some_platform",
"version": "4.5",
"sp": "0.1",
"arch": "some_architecture"
},
"app": [
{
"appid": "{00000000-0000-0000-0000-000000000001}",
"version": "1.2.3.4",
"event": [{
"eventtype": 2,
"eventresult": 1,
"errorcode": 0
},
{
"eventtype": 3,
"eventresult": 2,
},
{
"eventtype": 54,
"eventresult": 1,
"previousversion": "3.4.5.6",
}],
},
],
}
});
let request = RequestWrapper {
request: Request {
protocol_version: "3.0".to_string(),
updater: "some_updater".to_string(),
updater_version: "1.0".to_string(),
install_source: InstallSource::OnDemand,
is_machine: true,
os: OS {
platform: "some_platform".to_string(),
version: "4.5".to_string(),
service_pack: "0.1".to_string(),
arch: "some_architecture".to_string(),
},
apps: vec![App {
id: "{00000000-0000-0000-0000-000000000001}".to_string(),
version: "1.2.3.4".to_string(),
events: vec![
Event {
event_type: EventType::InstallComplete,
event_result: EventResult::Success,
errorcode: Some(0),
..Event::default()
},
Event {
event_type: EventType::UpdateComplete,
event_result: EventResult::SuccessAndRestartRequired,
..Event::default()
},
Event {
event_type: EventType::RebootedAfterUpdate,
event_result: EventResult::Success,
previous_version: Some("3.4.5.6".to_string()),
..Event::default()
},
],
..App::default()
}],
},
};
// serde_json::to_value() is used to ensure that the fields are ordered the same as the JSON
// generated by the json!() macro.
assert_eq!(expected, serde_json::to_value(&request).unwrap());
}
#[test]
pub fn all_fields_serialization_test() {
let expected = json!({
"request": {
"protocol": "3.0",
"updater": "some_updater",
"updaterversion": "1.0",
"installsource": "ondemand",
"ismachine": true,
"os": {
"platform": "some_platform",
"version": "4.5",
"sp": "0.1",
"arch": "some_architecture"
},
"app": [
{
"appid": "{00000000-0000-0000-0000-000000000001}",
"version": "1.2.3.4",
"fp": "some_fingerprint",
"cohort": "1",
"cohorthint": "stable",
"cohortname": "Production",
"updatecheck": { "updatedisabled": true},
"ping": {
"ad": 300,
"rd": 45,
},
"event": [{
"eventtype": 2,
"eventresult": 1,
"errorcode": 0
},
{
"eventtype": 3,
"eventresult": 2,
},
{
"eventtype": 54,
"eventresult": 1,
"previousversion": "3.4.5.6",
}],
},
],
}
});
let request = RequestWrapper {
request: Request {
protocol_version: "3.0".to_string(),
updater: "some_updater".to_string(),
updater_version: "1.0".to_string(),
install_source: InstallSource::OnDemand,
is_machine: true,
os: OS {
platform: "some_platform".to_string(),
version: "4.5".to_string(),
service_pack: "0.1".to_string(),
arch: "some_architecture".to_string(),
},
apps: vec![App {
id: "{00000000-0000-0000-0000-000000000001}".to_string(),
version: "1.2.3.4".to_string(),
fingerprint: Some("some_fingerprint".to_string()),
cohort: Some(Cohort {
id: Some("1".to_string()),
hint: Some("stable".to_string()),
name: Some("Production".to_string()),
}),
update_check: Some(UpdateCheck::disabled()),
ping: Some(Ping { date_last_active: Some(300), date_last_roll_call: Some(45) }),
events: vec![
Event {
event_type: EventType::InstallComplete,
event_result: EventResult::Success,
errorcode: Some(0),
..Event::default()
},
Event {
event_type: EventType::UpdateComplete,
event_result: EventResult::SuccessAndRestartRequired,
..Event::default()
},
Event {
event_type: EventType::RebootedAfterUpdate,
event_result: EventResult::Success,
previous_version: Some("3.4.5.6".to_string()),
..Event::default()
},
],
..App::default()
}],
},
};
// serde_json::to_value() is used to ensure that the fields are ordered the same as the JSON
// generated by the json!() macro.
assert_eq!(expected, serde_json::to_value(&request).unwrap());
}