-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_guards_test.cpp
More file actions
155 lines (127 loc) · 5.21 KB
/
Copy pathpath_guards_test.cpp
File metadata and controls
155 lines (127 loc) · 5.21 KB
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
#include "reliability/path_guards.hpp"
#include <cassert>
#include <iostream>
int main()
{
using namespace droidcli::reliability;
const std::string desktop = "C:\\Users\\luisarandas\\Desktop";
// --- substitute_bare_desktop_token ---
// Bare token preceded by nothing (Phase 7's original case).
{
const std::string result = substitute_bare_desktop_token("desktop/red.png", desktop);
assert(result == desktop + "/red.png");
}
{
const std::string result = substitute_bare_desktop_token("desktop\\red.png", desktop);
assert(result == desktop + "\\red.png");
}
// Preceded by a space, inside a longer command line.
{
const std::string result = substitute_bare_desktop_token("-y -i in.mp4 desktop/out.mp4", desktop);
assert(result == "-y -i in.mp4 " + desktop + "/out.mp4");
}
// Real transcript (Phase 18): a single leading "/" - write_file's path
// argument used exactly this shape and previously landed in a bogus
// "C:\Desktop\..." instead of the real Desktop.
{
const std::string result = substitute_bare_desktop_token("/Desktop/probe_test.png", desktop);
assert(result == desktop + "/probe_test.png");
}
// Real transcript (Phase 23): a leading "./" - "desktop" starts at index
// 2 there, one past the single-leading-separator case above.
{
const std::string result = substitute_bare_desktop_token("./desktop/new_folder", desktop);
assert(result == desktop + "/new_folder");
}
// Deliberately NOT touched: an already-correct absolute path where
// "Desktop" is preceded by more path, not a word boundary.
{
const std::string already_correct = desktop + "\\existing_file.txt";
const std::string result = substitute_bare_desktop_token(already_correct, desktop);
assert(result == already_correct);
}
// Empty desktop_path means resolution failed upstream - never touch the
// input, never guess.
{
const std::string result = substitute_bare_desktop_token("desktop/red.png", "");
assert(result == "desktop/red.png");
}
// --- default_bare_filename_to_desktop ---
// Real transcript (Phase 20): a bare filename with no directory at all -
// "output.png" - previously landed in droidcli's own working directory.
{
const std::string result = default_bare_filename_to_desktop("output.png", desktop);
assert(result == desktop + "\\output.png");
}
// Anything with any directory information at all is left untouched, even
// a relative one - the caller already specified where.
{
assert(default_bare_filename_to_desktop("subdir/output.png", desktop) == "subdir/output.png");
assert(default_bare_filename_to_desktop("./output.png", desktop) == "./output.png");
}
// --- looks_like_placeholder_path ---
// Real transcript (Phase 16): a documentation-convention template path.
{
assert(looks_like_placeholder_path("/path/to/Desktop/green_image.png"));
}
// Real transcript (Phase 23): "/Users/username/..." - the same
// documentation-convention placeholder, a different specific word.
{
assert(looks_like_placeholder_path("/Users/username/Desktop/folder_name"));
}
// A real, legitimate resolved path must never be flagged.
{
assert(!looks_like_placeholder_path(desktop + "\\real_file.txt"));
}
// --- looks_like_invented_desktop_path ---
// Real transcript (Phase 25): a literal root-level "C:\desktop\hello" -
// looks absolute, but isn't the real per-user Desktop.
{
assert(looks_like_invented_desktop_path("C:\\desktop\\hello", desktop));
}
// The real, correctly-resolved Desktop path must never be flagged - this
// is the exact false-positive risk the "bounded by separators, rooted at
// the real prefix" logic exists to avoid.
{
assert(!looks_like_invented_desktop_path(desktop + "\\hello", desktop));
}
// A real folder that merely contains "desktop" as part of a longer name
// (not a standalone path segment) must not false-positive.
{
assert(!looks_like_invented_desktop_path("C:\\Users\\luisarandas\\my_desktop_notes\\hello", desktop));
}
// Empty real_desktop_path means Desktop resolution itself failed - never
// flag anything in that case, there's no known-good prefix to compare
// against.
{
assert(!looks_like_invented_desktop_path("C:\\desktop\\hello", ""));
}
// --- looks_like_mismatched_binary_content ---
// Real transcript (Phase 19): the literal ffmpeg command line written as
// "content" for a .png path.
{
assert(looks_like_mismatched_binary_content(
desktop + "\\output.png",
"ffmpeg -y -f lavfi -i color=blue:s=512x512 -frames:v 1 -update 1 output.png"));
}
// Real transcript (Phase 19): the literal placeholder string used as
// "content" for a .png path.
{
assert(looks_like_mismatched_binary_content(
desktop + "\\blue.png",
"<base64 encoded blue 512x512 pixel PNG image data>"));
}
// Real PNG magic bytes must be accepted, not rejected.
{
const std::string real_png_header = "\x89PNG\r\n\x1a\n" "restofdata";
assert(!looks_like_mismatched_binary_content(desktop + "\\real.png", real_png_header));
}
// A path with no recognized binary/image extension is never checked -
// this guard only catches the specific "claims to be a real media file,
// isn't" shape.
{
assert(!looks_like_mismatched_binary_content(desktop + "\\notes.txt", "just plain text"));
}
std::cout << "path_guards_test passed" << std::endl;
return 0;
}