cheatah
Source

tests/purrc/app_integrity_test.cpp

1// Copyright (c) 2026 BigBrain LLC. MIT-licensed (see LICENSE).
2// Original work; see ACKNOWLEDGMENTS.md for the open-source ideas we build upon.
3// System-level "application" test: a small file-integrity / dedup tool written
4// in cheatah, compiled with purrc and run under the runtime. Unlike the
5// single-module e2e tests in stdlib_e2e_test.cpp, this program only produces the
6// expected output if FIVE stdlib modules cooperate end to end:
7//
8// - os — os.path.join builds the temp paths, os.makedirs/os.remove/
9// os.rmdir manage the scratch dir, os.path.getsize/os.path.exists
10// report on the written files.
11// - io — io.open + File.write writes each file, io.read_file reads it
12// back, io.str renders integers, io.print emits the report.
13// - hashlib — hashlib.sha256 fingerprints each file's bytes.
14// - string — string.upper formats the per-file labels.
15// - builtins — len() drives the loops, and string indexing (digest[i]) slices
16// the hex prefix character by character (the language has no slice
17// syntax), plus boolean comparison for the cleanup check.
18//
19// The pipeline: write three fixed files (file2 is a byte-for-byte duplicate of
20// file1), hash each, then detect duplicates by comparing the 64-char hex digests.
21// Output is fully deterministic — the sha256 prefixes are the real digests of
22// the fixed contents (verified against sha256sum):
23// sha256("the quick brown fox\n") = 6e459fed18dd...
24// sha256("lazy dog sleeps\n") = 86f4c766f75c...
25// The scratch directory is cleaned up and the final line asserts it is gone.
27#include "e2e_harness.hpp"
29TEST(SystemApps, Integrity) {
30 e2e::expect_e2e("app_integrity", R"PURR(# app_integrity.purr — a file integrity / dedup pipeline.
31# Exercises hashlib + io + string + os + builtins together.
33import io
34import os
35import string
36import hashlib
38let dir = os.path.join("/tmp", "cheatah_integrity")
39os.makedirs(dir)
41# First 12 hex chars of a digest, built one char at a time (the language has no
42# slice syntax, so index char-by-char via builtins-backed string indexing).
43fn prefix12(digest) {
44 let p = ""
45 for i in range(0, 12) {
46 p = p + digest[i]
47 }
48 return p
51# Fixed contents: file2 deliberately duplicates file1.
52let names = ["file1.txt", "file2.txt", "file3.txt"]
53let bodies = ["the quick brown fox\n", "the quick brown fox\n", "lazy dog sleeps\n"]
54let digests = ["", "", ""]
56# Write each file via io.open / File.write, then read it back and hash it.
57for i in range(0, len(names)) {
58 let path = os.path.join(dir, names[i])
59 let f = io.open(path, "w")
60 f.write(bodies[i])
61 f.close()
63 let data = io.read_file(path)
64 let d = hashlib.sha256(data)
65 digests[i] = d
66 io.print(string.upper("file") + io.str(i + 1) + ":", os.path.getsize(path), "bytes", prefix12(d))
69# Dedup: for each file, find the first earlier file with an identical digest.
70for i in range(0, len(digests)) {
71 let dup = -1
72 for j in range(0, i) {
73 if dup < 0 {
74 if digests[i] == digests[j] {
75 dup = j
76 }
77 }
78 }
79 if dup >= 0 {
80 io.print("dup: file" + io.str(i + 1) + " == file" + io.str(dup + 1))
81 } else {
82 io.print("unique: file" + io.str(i + 1))
83 }
86# Clean up the temp files and directory.
87for i in range(0, len(names)) {
88 os.remove(os.path.join(dir, names[i]))
90os.rmdir(dir)
91io.print("cleaned:", os.path.exists(dir) == false)
92)PURR",
93 "FILE1: 20 bytes 6e459fed18dd\n"
94 "FILE2: 20 bytes 6e459fed18dd\n"
95 "FILE3: 16 bytes 86f4c766f75c\n"
96 "unique: file1\n"
97 "dup: file2 == file1\n"
98 "unique: file3\n"
99 "cleaned: True\n");