ic-backup 0.31.1

Host-side snapshot backup and same-release recovery for Internet Computer canisters
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
use super::*;
use crate::test_support::temp_dir;
use crate::test_support::{create_json_durable, write_json_durable};
use ic_host_fs::durable::NamedWriteError;
use serde::Serializer;
use std::fs;

struct FailingSerialize;

impl Serialize for FailingSerialize {
    fn serialize<S>(&self, _serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        Err(serde::ser::Error::custom(
            "intentional serialization failure",
        ))
    }
}

#[test]
fn bounded_json_size_uses_exact_pretty_encoding_and_original_limits() {
    for value in [
        serde_json::Value::Null,
        serde_json::json!({"escaped": "\n\t\"\\", "utf8": "é雪", "nested": [1, 2]}),
        serde_json::json!({"state": "a".repeat(100_001)}),
    ] {
        let size = serde_json::to_vec_pretty(&value)
            .expect("original encoding")
            .len() as u64;
        for limit in [size, size + 1, u64::MAX] {
            check_json_size(&value, limit).expect("inclusive exact encoding budget");
        }
        for limit in [0, size - 1] {
            assert!(matches!(
                check_json_size(&value, limit),
                Err(PersistenceError::RecordTooLarge { limit: actual }) if actual == limit
            ));
        }
    }
}

#[test]
fn bounded_json_size_preserves_serializer_errors() {
    assert!(matches!(
        check_json_size(&FailingSerialize, u64::MAX),
        Err(PersistenceError::Json(error)) if !error.is_io()
    ));
}

#[test]
fn durable_json_replaces_the_complete_document() {
    let root = temp_dir("ic-backup-durable-json-replace");
    let path = root.join("journal.json");
    fs::create_dir_all(&root).expect("create temp root");
    fs::write(&path, b"previous-document-with-more-bytes").expect("write previous document");

    write_json_durable(&path, &serde_json::json!({"state": "ready"})).expect("replace document");

    let written = fs::read_to_string(&path).expect("read replaced document");
    let decoded: serde_json::Value = serde_json::from_str(&written).expect("decode document");
    assert_eq!(decoded, serde_json::json!({"state": "ready"}));
    assert_no_staging_file(&root, "journal.json");
    fs::remove_dir_all(root).expect("remove temp root");
}

#[test]
fn serialization_failure_preserves_the_previous_document() {
    let root = temp_dir("ic-backup-durable-json-serialize");
    let path = root.join("journal.json");
    fs::create_dir_all(&root).expect("create temp root");
    fs::write(&path, b"previous-document").expect("write previous document");

    let error = write_json_durable(&path, &FailingSerialize)
        .expect_err("serialization failure should reject");

    assert!(matches!(error, PersistenceError::Json(_)));
    assert_eq!(
        fs::read(&path).expect("read previous document"),
        b"previous-document"
    );
    assert_no_staging_file(&root, "journal.json");
    fs::remove_dir_all(root).expect("remove temp root");
}

#[test]
fn bounded_encoding_publishes_its_exact_single_pass_and_refuses_before_effects() {
    use std::cell::Cell;

    struct Probe<'a>(&'a Cell<u8>);
    impl Serialize for Probe<'_> {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            self.0.set(self.0.get() + 1);
            serializer.serialize_str(if self.0.get() == 1 {
                "first"
            } else {
                "changed"
            })
        }
    }

    for create in [false, true] {
        for limit in [6, 7] {
            let root = temp_dir("ic-backup-bounded-encoding");
            let parent = root.join("private/nested");
            let path = parent.join("record.json");
            let calls = Cell::new(0);
            let encoded = encode_json(&Probe(&calls), limit);
            assert_eq!(calls.get(), 1);
            assert!(!parent.exists());
            if limit == 6 {
                assert!(matches!(
                    encoded,
                    Err(PersistenceError::RecordTooLarge { limit: 6 })
                ));
            } else {
                let bytes = encoded.unwrap();
                assert_eq!(bytes, b"\"first\"");
                if create {
                    create_encoded(&path, &bytes)
                } else {
                    write_encoded(&path, &bytes)
                }
                .unwrap();
                assert_eq!(fs::read(&path).unwrap(), bytes);
                assert_eq!(calls.get(), 1);
            }
            fs::remove_dir_all(root).unwrap();
        }
    }
}

#[test]
fn rename_failure_removes_the_staging_file() {
    let root = temp_dir("ic-backup-durable-json-rename");
    let path = root.join("journal.json");
    fs::create_dir_all(&path).expect("create conflicting target directory");

    let error = write_json_durable(&path, &serde_json::json!({"state": "ready"}))
        .expect_err("rename over directory should reject");

    assert!(matches!(
        error,
        PersistenceError::Publication(
            ic_host_fs::durable::NamedWriteError::BeforePublication { .. }
        )
    ));
    assert!(path.is_dir());
    assert_no_staging_file(&root, "journal.json");
    fs::remove_dir_all(root).expect("remove temp root");
}

#[test]
fn serialization_runs_once_before_parent_creation_even_when_it_fails() {
    use std::cell::Cell;

    struct Probe<'a> {
        parent: &'a Path,
        calls: &'a Cell<u8>,
        reject: bool,
    }
    impl Serialize for Probe<'_> {
        fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
            assert!(!self.parent.exists());
            self.calls.set(self.calls.get() + 1);
            if self.reject {
                Err(serde::ser::Error::custom("failed preflight"))
            } else {
                serializer.serialize_u8(7)
            }
        }
    }

    for create in [false, true] {
        for reject in [false, true] {
            let root = temp_dir("ic-backup-json-preflight");
            let parent = root.join("private/nested");
            let path = parent.join("record.json");
            let calls = Cell::new(0);
            let value = Probe {
                parent: &parent,
                calls: &calls,
                reject,
            };
            let result = if create {
                create_json_durable(&path, &value)
            } else {
                write_json_durable(&path, &value)
            };
            assert_eq!(calls.get(), 1);
            if reject {
                assert!(matches!(result, Err(PersistenceError::Json(_))));
                assert!(!root.join("private").exists());
            } else {
                result.unwrap();
                assert_eq!(fs::read(&path).unwrap(), b"7");
                #[cfg(unix)]
                {
                    use std::os::unix::fs::PermissionsExt;
                    for directory in [root.join("private"), parent] {
                        assert_eq!(
                            fs::metadata(directory).unwrap().permissions().mode() & 0o777,
                            0o700
                        );
                    }
                }
            }
            fs::remove_dir_all(root).unwrap();
        }
    }
}

#[cfg(unix)]
#[test]
fn private_json_parents_refuse_aliases_and_traversal_before_effects() {
    use std::os::unix::fs::{PermissionsExt, symlink};

    let root = temp_dir("ic-backup-json-private-parents");
    let physical = root.join("physical");
    fs::create_dir(&physical).unwrap();
    fs::create_dir(physical.join("existing")).unwrap();
    let original = physical.join("record.json");
    fs::write(&original, b"original bytes").unwrap();
    fs::set_permissions(&original, fs::Permissions::from_mode(0o600)).unwrap();

    let alias = root.join("alias");
    symlink(&physical, &alias).unwrap();
    let value = serde_json::json!({"state": "new"});
    for path in [
        alias.join("record.json"),
        alias.join("existing/record.json"),
        alias.join("missing/record.json"),
    ] {
        for create in [false, true] {
            let result = if create {
                create_json_durable(&path, &value)
            } else {
                write_json_durable(&path, &value)
            };
            assert!(matches!(
                result,
                Err(PersistenceError::Io(error)) if error.kind() == io::ErrorKind::NotADirectory
            ));
        }
    }
    assert!(!physical.join("missing").exists());
    assert!(!physical.join("existing/record.json").exists());
    assert_eq!(fs::read(&original).unwrap(), b"original bytes");
    assert_eq!(
        fs::metadata(&original).unwrap().permissions().mode() & 0o777,
        0o600
    );

    let missing = root.join("missing");
    assert!(matches!(
        create_json_durable(&missing.join("../physical/new.json"), &value),
        Err(PersistenceError::Io(error)) if error.kind() == io::ErrorKind::InvalidInput
    ));
    assert!(!missing.exists());
    assert!(!physical.join("new.json").exists());

    // An explicitly selected root is resolved by its owning boundary once.
    let selected = alias.canonicalize().unwrap();
    create_json_durable(&selected.join("existing/new.json"), &value).unwrap();
    assert_eq!(
        fs::read(selected.join("existing/new.json")).unwrap(),
        serde_json::to_vec_pretty(&value).unwrap()
    );
    assert_eq!(
        fs::metadata(selected.join("existing/new.json"))
            .unwrap()
            .permissions()
            .mode()
            & 0o777,
        0o600
    );
    fs::remove_dir_all(root).unwrap();
}

#[cfg(unix)]
#[test]
fn foreign_staging_replacement_retains_typed_cleanup_evidence() {
    for mode in [PublicationMode::Replace, PublicationMode::CreateNew] {
        let root = temp_dir("ic-backup-json-stage-custody");
        let path = root.join("record.json");
        let displaced = root.join("displaced");
        if mode == PublicationMode::Replace {
            fs::write(&path, b"previous").unwrap();
        }
        let mut foreign = None;
        let error = publish_bytes_at_barriers(&path, b"new", mode, |barrier| {
            if barrier == DurableWriteBarrier::BeforeRename {
                let stage = fs::read_dir(&root)
                    .unwrap()
                    .map(Result::unwrap)
                    .find(|entry| entry.path() != path)
                    .unwrap()
                    .path();
                fs::rename(&stage, &displaced).unwrap();
                fs::write(&stage, b"foreign").unwrap();
                foreign = Some(stage);
            }
        })
        .unwrap_err();
        assert!(matches!(error, PersistenceError::Publication(
            NamedWriteError::BeforePublication { source, cleanup_error: Some(cleanup) }
        ) if source.kind() == io::ErrorKind::InvalidData && cleanup.kind() == io::ErrorKind::InvalidData));
        assert_eq!(fs::read(displaced).unwrap(), b"new");
        assert_eq!(fs::read(foreign.unwrap()).unwrap(), b"foreign");
        if mode == PublicationMode::Replace {
            assert_eq!(fs::read(path).unwrap(), b"previous");
        } else {
            assert!(!path.exists());
        }
        fs::remove_dir_all(root).unwrap();
    }
}

#[cfg(unix)]
#[test]
fn held_parent_publication_does_not_switch_to_a_replaced_path() {
    let root = temp_dir("ic-backup-json-parent-custody");
    let parent = root.join("parent");
    let moved = root.join("moved");
    let path = parent.join("record.json");
    let mut durable = false;
    let bytes = encode_json(&serde_json::json!({"new": true}), 16 * 1024 * 1024).unwrap();
    publish_bytes_at_barriers(&path, &bytes, PublicationMode::Replace, |barrier| {
        if barrier == DurableWriteBarrier::BeforeRename {
            fs::rename(&parent, &moved).unwrap();
            fs::create_dir(&parent).unwrap();
            fs::write(&path, b"foreign").unwrap();
        } else {
            durable = true;
        }
    })
    .unwrap();
    assert!(durable);
    assert_eq!(fs::read(path).unwrap(), b"foreign");
    assert_eq!(
        read_json::<serde_json::Value>(&moved.join("record.json"), 1024).unwrap(),
        serde_json::json!({"new": true})
    );
    fs::remove_dir_all(root).unwrap();
}

fn assert_no_staging_file(root: &Path, target_name: &str) {
    let staging_files = fs::read_dir(root)
        .expect("read temp root")
        .filter_map(Result::ok)
        .filter(|entry| entry.file_name() != target_name)
        .collect::<Vec<_>>();
    assert!(staging_files.is_empty(), "staging files remain");
}

#[test]
fn host_publication_failures_keep_exact_phase_source_and_cleanup_evidence() {
    use std::error::Error;
    let producer = PersistenceError::from(NamedWriteError::Producer {
        source: io::Error::new(io::ErrorKind::BrokenPipe, "producer failed"),
        cleanup_error: Some(io::Error::new(
            io::ErrorKind::PermissionDenied,
            "stage held",
        )),
    });
    assert!(matches!(
        &producer,
        PersistenceError::Publication(NamedWriteError::Producer { source, cleanup_error: Some(cleanup) })
            if source.kind() == io::ErrorKind::BrokenPipe
                && cleanup.kind() == io::ErrorKind::PermissionDenied
    ));
    assert!(
        producer
            .to_string()
            .contains("staging cleanup failed: stage held")
    );
    assert_eq!(producer.source().unwrap().to_string(), "producer failed");

    let before = PersistenceError::from(NamedWriteError::<io::Error>::BeforePublication {
        source: io::Error::new(io::ErrorKind::AlreadyExists, "original remains"),
        cleanup_error: Some(io::Error::new(
            io::ErrorKind::PermissionDenied,
            "stage held",
        )),
    });
    assert!(matches!(
        &before,
        PersistenceError::Publication(NamedWriteError::BeforePublication { source, cleanup_error: Some(cleanup) })
            if source.kind() == io::ErrorKind::AlreadyExists
                && cleanup.kind() == io::ErrorKind::PermissionDenied
    ));
    assert!(
        before
            .to_string()
            .contains("staging cleanup failed: stage held")
    );
    assert_eq!(before.source().unwrap().to_string(), "original remains");

    let after = PersistenceError::from(NamedWriteError::<io::Error>::AfterPublication {
        source: io::Error::other("sync uncertain"),
    });
    assert!(matches!(
        &after,
        PersistenceError::Publication(NamedWriteError::AfterPublication { source }) if source.to_string() == "sync uncertain"
    ));
    assert!(
        after
            .to_string()
            .contains("output published but completion failed")
    );
    assert_eq!(after.source().unwrap().to_string(), "sync uncertain");
}