Debian Codegraph @

third_party/rust/glean/tests/collection_enabled_bin.rs

raw
1 // This Source Code Form is subject to the terms of the Mozilla Public
2 // License, v. 2.0. If a copy of the MPL was not distributed with this
3 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5 //! This integration test should model how the RLB is used when embedded in another Rust application
6 //! (e.g. FOG/Firefox Desktop).
7 //!
8 //! We write a single test scenario per file to avoid any state keeping across runs
9 //! (different files run as different processes).
10
11 mod common;
12
13 use std::fs;
14 use std::io::Read;
15
16 use crossbeam_channel::bounded;
17
18 use crossbeam_channel::Sender;
19 use crossbeam_channel::TryRecvError;
20 use flate2::read::GzDecoder;
21 use glean::net;
22 use glean::ClientInfoMetrics;
23 use glean::ConfigurationBuilder;
24 use pings::nofollows;
25 use serde_json::Value as JsonValue;
26
27 mod pings {
28 use super::*;
29 use glean::private::PingType;
30 use once_cell::sync::Lazy;
31
32 #[allow(non_upper_case_globals)]
33 pub static nofollows: Lazy<PingType> = Lazy::new(|| {
34 common::PingBuilder::new("nofollows")
35 .with_send_if_empty(true)
36 .with_include_info_sections(true) // WITH info sections
37 .with_enabled(false)
38 .with_follows_collection_enabled(false)
39 .with_include_client_id(true)
40 .build()
41 });
42
43 #[allow(non_upper_case_globals)]
44 pub static manual: Lazy<PingType> = Lazy::new(|| {
45 common::PingBuilder::new("manual")
46 .with_send_if_empty(true)
47 .build()
48 });
49 }
50
51 // Define a fake uploader that reports when and what it uploads.
52 #[derive(Debug)]
53 struct ReportingUploader {
54 sender: Sender<JsonValue>,
55 }
56
57 impl net::PingUploader for ReportingUploader {
58 fn upload(&self, upload_request: net::CapablePingUploadRequest) -> net::UploadResult {
59 let upload_request = upload_request.capable(|_| true).unwrap();
60 let body = upload_request.body;
61 let decode = |body: Vec<u8>| {
62 let mut gzip_decoder = GzDecoder::new(&body[..]);
63 let mut s = String::with_capacity(body.len());
64
65 gzip_decoder
66 .read_to_string(&mut s)
67 .ok()
68 .map(|_| &s[..])
69 .or_else(|| std::str::from_utf8(&body).ok())
70 .and_then(|payload| serde_json::from_str(payload).ok())
71 .unwrap()
72 };
73
74 self.sender.send(decode(body)).unwrap();
75 net::UploadResult::http_status(200)
76 }
77 }
78
79 /// Test scenario:
80 ///
81 /// * Glean has _some_ data already stored.
82 /// * Glean is started with collection-enabled=false.
83 /// * Most data is cleared, but not `client_info`` (except `client_id`)
84 /// * Pings with `follows_collection_enabled=false` still have the `client_info` filled in.
85 #[test]
86 fn nofollows_contains_client_info_when_collection_disabled() {
87 common::enable_test_logging();
88
89 // Create a custom configuration to use our reporting uploader.
90 let dir = tempfile::tempdir().unwrap();
91 let tmpname = dir.path().to_path_buf();
92
93 let db_dir = tmpname.join("db");
94 fs::create_dir_all(&db_dir).unwrap();
95 let db_path = db_dir.join("data.safe.bin");
96 fs::write(db_path, include_bytes!("./near-empty-c0ffee-db.safe.bin")).unwrap();
97
98 // collection-enabled = false
99 let (tx, rx) = bounded(1);
100 let cfg = ConfigurationBuilder::new(false, tmpname.clone(), "glean-fc")
101 .with_server_endpoint("invalid-test-host")
102 .with_use_core_mps(false)
103 .with_uploader(ReportingUploader { sender: tx })
104 .build();
105 // Same as `common::initialize`.
106 let client_info = ClientInfoMetrics {
107 app_build: "1.0.0".to_string(),
108 app_display_version: "1.0.0".to_string(),
109 channel: Some("testing".to_string()),
110 locale: Some("xx-XX".to_string()),
111 };
112 glean::test_reset_glean(cfg, client_info, false);
113
114 _ = &*pings::nofollows;
115 _ = &*pings::manual;
116 nofollows.set_enabled(true);
117
118 pings::manual.submit(None);
119 pings::nofollows.submit(None);
120
121 // Wait for the ping to arrive.
122 let payload = rx.recv().unwrap();
123
124 let client_info = payload["client_info"].as_object().unwrap();
125 // General client info is set
126 assert!(client_info["app_build"].is_string());
127 assert!(client_info["architecture"].is_string());
128 assert!(client_info["os"].is_string());
129 assert!(client_info["telemetry_sdk_build"].is_string());
130 // No client_id
131 assert_eq!(None, client_info.get("client_id"));
132
133 // No second ping received.
134 assert!(matches!(rx.try_recv(), Err(TryRecvError::Empty)));
135
136 // Now we enable collection.
137 // This should give us a client ID.
138 glean::set_collection_enabled(true);
139
140 pings::manual.submit(None);
141 let payload = rx.recv().unwrap();
142 let client_info = payload["client_info"].as_object().unwrap();
143 // General client info is set
144 assert!(client_info["app_build"].is_string());
145 assert!(client_info["architecture"].is_string());
146 assert!(client_info["os"].is_string());
147 assert!(client_info["telemetry_sdk_build"].is_string());
148 // No client_id
149 let client_id = client_info["client_id"].as_str().unwrap();
150
151 pings::nofollows.submit(None);
152 let payload = rx.recv().unwrap();
153 let client_info = payload["client_info"].as_object().unwrap();
154 // General client info is set
155 assert!(client_info["app_build"].is_string());
156 assert!(client_info["architecture"].is_string());
157 assert!(client_info["os"].is_string());
158 assert!(client_info["telemetry_sdk_build"].is_string());
159 // No client_id
160 let nf_client_id = client_info["client_id"].as_str().unwrap();
161
162 assert_eq!(client_id, nf_client_id);
163
164 glean::shutdown();
165 }