1// Copyright 2019 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
89use serde::{Deserialize, Serialize};
1011pub mod request;
12pub mod response;
1314pub const PROTOCOL_V3: &str = "3.0";
1516/// The cohort identifies the update 'track' or 'channel', and is used to implement the tracking of
17/// membership in a fractional roll-out. This is per-application data.
18///
19/// This is sent to Omaha to identify the cohort that the application is in. This is returned (with
20/// possibly new values) by Omaha to indicate that the application is now in a different cohort. On
21/// the next update check for that application, the updater needs to use this newly returned cohort
22/// as the one that it sends to Omaha with that application.
23///
24/// For more information about cohorts, see the 'cohort', 'cohorthint', and 'cohortname' attributes
25/// of the Request.App object at:
26///
27/// https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#app-request
28#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
29pub struct Cohort {
30/// This is the cohort id itself.
31#[serde(rename = "cohort")]
32 #[serde(skip_serializing_if = "Option::is_none")]
33pub id: Option<String>,
3435#[serde(rename = "cohorthint")]
36 #[serde(skip_serializing_if = "Option::is_none")]
37pub hint: Option<String>,
3839#[serde(rename = "cohortname")]
40 #[serde(skip_serializing_if = "Option::is_none")]
41pub name: Option<String>,
42}
4344impl Cohort {
45/// Create a new Cohort instance from just a cohort id (channel name).
46pub fn new(id: &str) -> Cohort {
47 Cohort {
48 id: Some(id.to_string()),
49 hint: None,
50 name: None,
51 }
52 }
5354pub fn from_hint(hint: &str) -> Cohort {
55 Cohort {
56 id: None,
57 hint: Some(hint.to_string()),
58 name: None,
59 }
60 }
6162pub fn update_from_omaha(&mut self, omaha_cohort: Self) {
63// From Omaha spec:
64 // If this attribute is transmitted in the response (even if the value is empty-string),
65 // the client should overwrite the current cohort of this app with the sent value.
66if omaha_cohort.id.is_some() {
67self.id = omaha_cohort.id;
68 }
69if omaha_cohort.hint.is_some() {
70self.hint = omaha_cohort.hint;
71 }
72if omaha_cohort.name.is_some() {
73self.name = omaha_cohort.name;
74 }
75 }
7677/// A validation function to test that a given Cohort hint or name is valid per the Omaha spec:
78 /// 1-1024 ascii characters, with values in the range [\u20-\u7e].
79pub fn validate_name(name: &str) -> bool {
80 !name.is_empty()
81 && name.len() <= 1024
82&& name.chars().all(|c| ('\u{20}'..='\u{7e}').contains(&c))
83 }
84}
8586#[cfg(test)]
87mod tests {
88use super::*;
8990#[test]
91fn test_cohort_new() {
92let cohort = Cohort::new("my_cohort");
93assert_eq!(Some("my_cohort".to_string()), cohort.id);
94assert_eq!(None, cohort.hint);
95assert_eq!(None, cohort.name);
96 }
9798#[test]
99fn test_cohort_update_from_omaha() {
100let mut cohort = Cohort::from_hint("hint");
101let omaha_cohort = Cohort::new("my_cohort");
102 cohort.update_from_omaha(omaha_cohort);
103assert_eq!(Some("my_cohort".to_string()), cohort.id);
104assert_eq!(Some("hint".to_string()), cohort.hint);
105assert_eq!(None, cohort.name);
106 }
107108#[test]
109fn test_cohort_update_from_omaha_none() {
110let mut cohort = Cohort {
111 id: Some("id".to_string()),
112 hint: Some("hint".to_string()),
113 name: Some("name".to_string()),
114 };
115let expected_cohort = cohort.clone();
116 cohort.update_from_omaha(Cohort::default());
117assert_eq!(cohort, expected_cohort);
118 }
119120#[test]
121fn test_valid_cohort_names() {
122assert!(Cohort::validate_name("some-channel"));
123assert!(Cohort::validate_name("a"));
124125let max_len_name = "a".repeat(1024);
126assert!(Cohort::validate_name(&max_len_name));
127 }
128129#[test]
130fn test_invalid_cohort_name_length() {
131assert!(!Cohort::validate_name(""));
132133let too_long_name = "a".repeat(1025);
134assert!(!Cohort::validate_name(&too_long_name));
135 }
136137#[test]
138fn test_invalid_cohort_name_chars() {
139assert!(!Cohort::validate_name("some\u{09}channel"));
140assert!(!Cohort::validate_name("some\u{07f}channel"));
141assert!(!Cohort::validate_name("some\u{080}channel"));
142 }
143}