libupdated 1.0.0

Updated SOTA Updater
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: <text>
// Copyright(c) 2026 Liebherr-Digital Development Center GmbH
// Written by Thomas Witte <thomas.witte@liebherr.com>
// </text>

use core::panic;
use std::{path::PathBuf, time::Duration};

use hawkbit::ddi::{Execution, Finished, MaintenanceWindow, Type};
use hawkbit_mock::ddi::{
    ChunkProtocol, Deployment, DeploymentBuilder, Server, ServerBuilder, Target,
};
use libupdated::{
    hawkbit::{Attributes, Hawkbit, HawkbitOptions, HawkbitProgressReporter, HawkbitUpdate},
    progress_reporter::{ProgressMessage, ProgressReporter, UpdateState},
    update_source::{Update, UpdateInfo, UpdateSource, UpdateSourceError},
    update_workflow::UpdateError,
};
use serde_json::json;

async fn create_test_hawkbit() -> (Hawkbit, Server, Target) {
    let hawkbit_server = hawkbit_mock::ddi::ServerBuilder::default().build();
    let target = hawkbit_server.add_target("target1");

    let options = HawkbitOptions {
        handle_cancel: true,
        handle_config: true,
        default_polling_interval: Duration::from_secs(30),
        server_cert: None,
        client_cert: None,
        attributes_script: Attributes::Default,
        timeout: Duration::from_secs(5),
    };

    let hawkbit = Hawkbit::new(
        &hawkbit_server.base_url(),
        &hawkbit_server.tenant,
        &target.name,
        target.client_auth.clone(),
        options,
    )
    .await
    .unwrap();

    (hawkbit, hawkbit_server, target)
}

fn test_deployment(id: &str, needs_consent: bool) -> Deployment {
    let mut test_artifact = PathBuf::new();
    test_artifact.push("scripts");
    test_artifact.push("attributes_script.sh");

    DeploymentBuilder::new(id, Type::Forced, Type::Attempt)
        .maintenance_window(MaintenanceWindow::Available)
        .confirmation_required(needs_consent)
        .chunk_with_metadata(
            ChunkProtocol::HTTPS,
            "app-https",
            "1.0",
            "some-chunk",
            vec![(
                test_artifact,
                "10af3b3dfc80600cdf6db484058ed9f4",
                "7f0cfd5c24afc3fb9e3db952e54196ef462825b8",
                "c331355ebaed9615955af5053bde672957e51d567418ccd98f32e524d781bbf6",
            )],
            vec![
                ("version".to_string(), "1.0.0".to_string()),
                ("changelog".to_string(), "all the changes!".to_string()),
            ],
        )
        .build()
}

#[tokio::test]
async fn test_standard_attributes() {
    // mock hawkbit server
    let hawkbit_server = ServerBuilder::default().build();
    let hawkbit_url = hawkbit_server.base_url();
    let target = hawkbit_server.add_target("Target1");

    let options = HawkbitOptions {
        handle_cancel: true,
        handle_config: true,
        default_polling_interval: std::time::Duration::from_secs(1),
        server_cert: None,
        client_cert: None,
        attributes_script: Attributes::Default,
        timeout: Duration::from_secs(5),
    };
    let mut hawkbit = Hawkbit::new(
        &hawkbit_url,
        &hawkbit_server.tenant,
        "Target1",
        target.client_auth.clone(),
        options,
    )
    .await
    .unwrap();

    target.request_config(json!({
        "mode" : "replace",
        "data" : {
            "OSVersion": hawkbit.get_os_version().await.unwrap(),
            "Client": format!("libupdated v{}", env!("CARGO_PKG_VERSION")),
        },
        "status" : {
            "result" : {
            "finished" : "success"
            },
            "execution" : "closed",
            "details" : []
        }
    }));

    match hawkbit.check_for_updates().await {
        Ok(_) => panic!("There should be no updates"),
        Err(UpdateError::NoPendingUpdates) => (),
        Err(e) => panic!("Error: {:?}", e),
    };

    assert_eq!(target.config_data_hits(), 1);
}

#[tokio::test]
async fn test_attributes_script() {
    // mock hawkbit server
    let hawkbit_server = ServerBuilder::default().build();
    let hawkbit_url = hawkbit_server.base_url();
    let target = hawkbit_server.add_target("Target1");

    let options = HawkbitOptions {
        handle_cancel: true,
        handle_config: true,
        default_polling_interval: std::time::Duration::from_secs(1),
        server_cert: None,
        client_cert: None,
        attributes_script: Attributes::Script(format!(
            "{}/scripts/attributes_script.sh",
            env!("CARGO_MANIFEST_DIR")
        )),
        timeout: Duration::from_secs(5),
    };
    let mut hawkbit = Hawkbit::new(
        &hawkbit_url,
        &hawkbit_server.tenant,
        "Target1",
        target.client_auth.clone(),
        options,
    )
    .await
    .unwrap();

    target.request_config(json!({
        "mode" : "replace",
        "data" : {
            "OS_ID" : "liebherr os",
            "OS_VERSION_ID" : "1.0",
            "CURRENT_ROOT_PARTITION" : "2",
            "SWUPDATE_STATE" : "0",
        },
        "status" : {
            "result" : {
            "finished" : "success"
            },
            "execution" : "closed",
            "details" : []
        }
    }));

    match hawkbit.check_for_updates().await {
        Ok(_) => panic!("There should be no updates"),
        Err(UpdateError::NoPendingUpdates) => (),
        Err(e) => panic!("Error: {:?}", e),
    };

    assert_eq!(target.config_data_hits(), 1);
}

#[tokio::test]
async fn test_invalid_attributes_script() {
    // mock hawkbit server
    let hawkbit_server = ServerBuilder::default().build();
    let hawkbit_url = hawkbit_server.base_url();
    let target = hawkbit_server.add_target("Target1");

    let options = HawkbitOptions {
        handle_cancel: true,
        handle_config: true,
        default_polling_interval: std::time::Duration::from_secs(1),
        server_cert: None,
        client_cert: None,
        attributes_script: Attributes::Script("ls".to_string()),
        timeout: Duration::from_secs(5),
    };
    let mut hawkbit = Hawkbit::new(
        &hawkbit_url,
        &hawkbit_server.tenant,
        "Target1",
        target.client_auth.clone(),
        options,
    )
    .await
    .unwrap();

    target.request_config(json!({
        "mode" : "replace",
        "data" : {
            "OSVersion": hawkbit.get_os_version().await.unwrap(),
            "Client": format!("libupdated v{}", env!("CARGO_PKG_VERSION")),
        },
        "status" : {
            "result" : {
            "finished" : "success"
            },
            "execution" : "closed",
            "details" : []
        }
    }));

    match hawkbit.check_for_updates().await {
        Ok(_) => panic!("There should be no updates"),
        Err(UpdateError::NoPendingUpdates) => (),
        Err(e) => panic!("Error: {:?}", e),
    };

    assert_eq!(target.config_data_hits(), 1);
}

#[tokio::test]
async fn test_hawkbit_poll() {
    let (mut hawkbit, _hawkbit_server, target) = create_test_hawkbit().await;

    assert_eq!(
        hawkbit.preferred_polling_interval(),
        &Duration::from_secs(30)
    );

    match hawkbit.check_for_updates().await {
        Ok(_) => panic!("No updates should be available"),
        Err(UpdateError::NoPendingUpdates) => (),
        Err(err) => panic!("Unexpected error: {:?}", err),
    };
    assert_eq!(target.poll_hits(), 1);

    let deployment = DeploymentBuilder::new("123", Type::Forced, Type::Attempt).build();
    target.push_deployment(deployment);

    let _ = hawkbit.check_for_updates().await.unwrap();

    assert_eq!(target.poll_hits(), 1);

    // the server sets the polling interval to 60 seconds
    assert_eq!(
        hawkbit.preferred_polling_interval(),
        &Duration::from_secs(60)
    );
}

#[tokio::test]
async fn test_hawkbit_update_info() {
    let (mut hawkbit, _, target) = create_test_hawkbit().await;

    // update, that does not need user consent
    target.push_deployment(test_deployment("1234", false));

    let update_info = hawkbit.check_for_updates().await.unwrap();

    assert_eq!(update_info.version(), "1234");
    assert_eq!(
        update_info.metadata().get("changelog").unwrap(),
        "all the changes!"
    );

    assert_eq!(target.poll_hits(), 1);
    assert_eq!(target.deployment_hits(), 1);

    assert!(!update_info.needs_consent());

    let update = update_info.update().unwrap();
    let _hawkbit_update = update.as_any().downcast_ref::<HawkbitUpdate>().unwrap();

    // update, that needs user consent
    target.push_deployment(test_deployment("1235", true));

    let mut update_info = hawkbit.check_for_updates().await.unwrap();

    // we can access the metadata without giving consent
    assert_eq!(update_info.version(), "1235");
    assert_eq!(
        update_info.metadata().get("changelog").unwrap(),
        "all the changes!"
    );

    assert_eq!(target.poll_hits(), 1);
    assert_eq!(target.confirmation_hits(), 1);

    assert!(update_info.needs_consent());

    assert!(update_info.update().is_err_and(|e| {
        if let UpdateError::UpdateSourceError(UpdateSourceError::ConsentRequired) = e {
            true
        } else {
            false
        }
    }));

    // when giving consent, the client immediately polls and fetches the (now confirmed) deployment
    let confirmation_mock = target.expect_confirmation_feedback(
        "1235",
        Some(1),
        hawkbit::ddi::ConfirmationResponse::Confirmed,
        vec![],
    );
    target.push_deployment(test_deployment("1235", false));
    update_info.give_consent().await.unwrap();
    assert!(confirmation_mock.hits() == 1);
    assert!(target.poll_hits() == 1);
    assert_eq!(target.deployment_hits(), 1);

    // now fetching the update should work
    assert!(!update_info.needs_consent());

    let update = update_info.update().unwrap();
    let _hawkbit_update = update.as_any().downcast_ref::<HawkbitUpdate>().unwrap();
}

#[tokio::test]
async fn test_hawkbit_update() {
    let (mut hawkbit, _, target) = create_test_hawkbit().await;

    // update, that does not need user consent
    target.push_deployment(test_deployment("1234", false));

    let update_info = hawkbit.check_for_updates().await.unwrap();
    let update = update_info.update().unwrap();

    let filesize = update.size();
    assert_eq!(
        filesize,
        tokio::fs::metadata("scripts/attributes_script.sh")
            .await
            .unwrap()
            .len()
    );

    // download the file and assert that its content and filename match
    let filenames = update.save_to_disk(None).await.unwrap();
    let update_file = filenames.first().unwrap();
    assert_eq!(
        &update_file.filename,
        &PathBuf::from("/tmp/updated/update_1234/attributes_script.sh")
    );
    assert_eq!(
        tokio::fs::read_to_string(&update_file.filename)
            .await
            .unwrap(),
        tokio::fs::read_to_string("scripts/attributes_script.sh")
            .await
            .unwrap()
    );

    // stream the file to a vector and assert the content matches
    let mut stream = update.stream().unwrap();
    let mut buffer = Vec::<u8>::new();
    while let Some(data) = stream.recv().await {
        match data {
            Ok(bytes) => buffer.append(bytes.to_vec().as_mut()),
            Err(e) => panic!("Error: {:?}", e),
        }
    }
    assert_eq!(
        buffer,
        tokio::fs::read("scripts/attributes_script.sh")
            .await
            .unwrap()
    );
}

#[tokio::test]
async fn test_hawkbit_hashes() {
    let (mut hawkbit, _, target) = create_test_hawkbit().await;

    // update, that does not need user consent
    let mut test_artifact = PathBuf::new();
    test_artifact.push("scripts");
    test_artifact.push("attributes_script.sh");

    let deployment = DeploymentBuilder::new("1234", Type::Forced, Type::Attempt)
        .maintenance_window(MaintenanceWindow::Available)
        .confirmation_required(false)
        .chunk_with_metadata(
            ChunkProtocol::HTTPS,
            "app-https",
            "1.0",
            "some-chunk",
            vec![(test_artifact, "0", "0", "0")], // invalid hashes
            vec![
                ("version".to_string(), "1.0.0".to_string()),
                ("changelog".to_string(), "all the changes!".to_string()),
            ],
        )
        .build();

    target.push_deployment(deployment);

    let update_info = hawkbit.check_for_updates().await.unwrap();
    let update = update_info.update().unwrap();

    let filesize = update.size();
    assert_eq!(
        filesize,
        tokio::fs::metadata("scripts/attributes_script.sh")
            .await
            .unwrap()
            .len()
    );

    // download the file and assert that its content and filename match
    update
        .save_to_disk(None)
        .await
        .map(|_| ())
        .expect_err("Invalid hashes should cause an error");
    // stream the file to a vector and assert the content matches
    let mut stream = update.stream().unwrap();
    let mut buffer = Vec::<u8>::new();
    while let Some(data) = stream.recv().await {
        match data {
            Ok(bytes) => buffer.append(bytes.to_vec().as_mut()),
            Err(_) => {
                return; /* expect checksum error eventually */
            }
        }
    }
    panic!("Expected checksum error, but got: {:?}", buffer);
}

#[tokio::test]
async fn test_hawkbit_update_progress() {
    let (mut hawkbit, _, target) = create_test_hawkbit().await;

    // update, that does not need user consent
    target.push_deployment(test_deployment("1234", false));

    let update_info = hawkbit.check_for_updates().await.unwrap();
    let update = update_info.update().unwrap();

    let filesize = update.size();
    assert_eq!(
        filesize,
        tokio::fs::metadata("scripts/attributes_script.sh")
            .await
            .unwrap()
            .len()
    );

    let mut hawkbit_progress = HawkbitProgressReporter::new();

    // download the file and assert that its content and filename match
    let update_file = update
        .save_to_disk_by_name(
            "attributes_script.sh".to_string(),
            Some(PathBuf::from("/tmp/test_hawkbit_update_progress")),
        )
        .await
        .unwrap();

    target.expect_deployment_feedback(
        "1234",
        Execution::Download,
        Finished::None,
        Some(json!({"cnt": 1, "of": 2})),
        vec!["test1"],
    );
    let _ = hawkbit_progress
        .report_retry(
            &update,
            &ProgressMessage {
                state: UpdateState::Downloading,
                cnt_of: (1, 2),
                message: "test1".to_string(),
            },
            1,
            Duration::from_secs(1),
        )
        .await
        .unwrap();
    target.expect_deployment_feedback(
        "1234",
        Execution::Download,
        Finished::None,
        Some(json!({"cnt": 2, "of": 2})),
        vec!["test2"],
    );
    let _ = hawkbit_progress
        .report(
            &update_file,
            &ProgressMessage {
                state: UpdateState::Downloading,
                cnt_of: (2, 2),
                message: "test2".to_string(),
            },
        )
        .await
        .unwrap();
}