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
|
//! GRASP-01 Repository Creation Tests
//!
//! Tests that verify bare Git repositories are created when repository announcements
//! are accepted by the relay.
//!
//! ## Test Coverage
//!
//! - Repository creation on valid announcement
//! - Idempotent creation (no error if repo already exists)
//! - Proper directory structure (<npub>/<identifier>.git)
//! - Bare repository validation (has HEAD, config, objects, refs)
//!
//! ## Running Tests
//!
//! ```bash
//! cd grasp-audit && nix develop -c bash test-ngit-relay.sh --mode test
//! ```
use crate::{AuditClient, FixtureKind, TestContext, TestResult};
use nostr_sdk::prelude::*;
use std::path::Path;
/// Test suite for repository creation
pub struct RepositoryCreationTests;
impl RepositoryCreationTests {
/// Run all repository creation tests
pub async fn run_all(
client: &AuditClient,
git_data_dir: &Path,
) -> crate::AuditResult {
let mut results = crate::AuditResult::new("GRASP-01 Repository Creation Tests");
results.add(Self::test_bare_repo_created_on_announcement(client, git_data_dir).await);
results.add(Self::test_repo_creation_idempotent(client, git_data_dir).await);
results.add(Self::test_bare_repo_structure(client, git_data_dir).await);
results
}
/// Test that a bare repository is created when a valid announcement is accepted
///
/// This test:
/// 1. Sends a valid repository announcement via TestContext
/// 2. Verifies the announcement was accepted
/// 3. Checks that a bare git repository was created at the expected path
pub async fn test_bare_repo_created_on_announcement(
client: &AuditClient,
git_data_dir: &Path,
) -> TestResult {
let test_name = "test_bare_repo_created_on_announcement";
let ctx = TestContext::new(client);
// Use TestContext to create and send repository announcement
let repo = match ctx.get_fixture(FixtureKind::ValidRepo).await {
Ok(r) => r,
Err(e) => {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must be created when announcement is accepted",
)
.fail(&format!("Failed to create repo fixture: {}", e))
}
};
// Wait a bit for repository creation
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
// Extract repo identifier and npub from announcement
let repo_id = match repo
.tags
.iter()
.find(|t| t.kind() == TagKind::d())
.and_then(|t| t.content())
{
Some(id) => id.to_string(),
None => {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must be created when announcement is accepted",
)
.fail("Repository announcement missing d tag")
}
};
let npub = match repo.pubkey.to_bech32() {
Ok(n) => n,
Err(e) => {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must be created when announcement is accepted",
)
.fail(&format!("Failed to convert pubkey to npub: {}", e))
}
};
// Check if repository was created
let repo_path = git_data_dir.join(&npub).join(format!("{}.git", repo_id));
if !is_bare_repository(&repo_path) {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must be created when announcement is accepted",
)
.fail(&format!(
"Bare repository not found at: {}",
repo_path.display()
));
}
TestResult::new(
test_name,
"GRASP-01",
"Bare repository must be created when announcement is accepted",
)
.pass()
}
/// Test that repository creation is idempotent
///
/// This test:
/// 1. Sends a repository announcement (creates repo) via TestContext
/// 2. Sends the same announcement again
/// 3. Verifies no error occurs and repo still exists
pub async fn test_repo_creation_idempotent(
client: &AuditClient,
git_data_dir: &Path,
) -> TestResult {
let test_name = "test_repo_creation_idempotent";
let ctx = TestContext::new(client);
// Create and send repository announcement first time via TestContext
let repo = match ctx.get_fixture(FixtureKind::ValidRepo).await {
Ok(r) => r,
Err(e) => {
return TestResult::new(
test_name,
"GRASP-01",
"Repository creation must be idempotent",
)
.fail(&format!("Failed to create repo fixture: {}", e))
}
};
// Wait for repository creation
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
// Send the same announcement again (should be idempotent)
if let Err(e) = client.send_event(repo.clone()).await {
return TestResult::new(
test_name,
"GRASP-01",
"Repository creation must be idempotent",
)
.fail(&format!("Second send failed (not idempotent): {}", e));
}
// Wait again
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
// Verify repository still exists and is valid
let repo_id = repo
.tags
.iter()
.find(|t| t.kind() == TagKind::d())
.and_then(|t| t.content())
.ok_or("Missing d tag")
.unwrap()
.to_string();
let npub = repo.pubkey.to_bech32().unwrap();
let repo_path = git_data_dir.join(&npub).join(format!("{}.git", repo_id));
if !is_bare_repository(&repo_path) {
return TestResult::new(
test_name,
"GRASP-01",
"Repository creation must be idempotent",
)
.fail("Repository not found after second send");
}
TestResult::new(
test_name,
"GRASP-01",
"Repository creation must be idempotent",
)
.pass()
}
/// Test that the repository has the correct structure
///
/// This test verifies:
/// 1. Repository is at <git_data_path>/<npub>/<identifier>.git
/// 2. Repository is bare (no working directory)
/// 3. Repository has required git structure (HEAD, config, objects/, refs/)
pub async fn test_bare_repo_structure(client: &AuditClient, git_data_dir: &Path) -> TestResult {
let test_name = "test_bare_repo_structure";
let ctx = TestContext::new(client);
// Create and send repository announcement via TestContext
let repo = match ctx.get_fixture(FixtureKind::ValidRepo).await {
Ok(r) => r,
Err(e) => {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.fail(&format!("Failed to create repo fixture: {}", e))
}
};
// Wait for repository creation
tokio::time::sleep(std::time::Duration::from_millis(200)).await;
// Extract repo identifier and npub
let repo_id = repo
.tags
.iter()
.find(|t| t.kind() == TagKind::d())
.and_then(|t| t.content())
.ok_or("Missing d tag")
.unwrap()
.to_string();
let npub = repo.pubkey.to_bech32().unwrap();
// Verify correct path structure: <git_data_path>/<npub>/<identifier>.git
let expected_path = git_data_dir.join(&npub).join(format!("{}.git", repo_id));
if !expected_path.exists() {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.fail(&format!(
"Repository not at expected path: {}",
expected_path.display()
));
}
// Verify it's a bare repository with correct structure
if !expected_path.join("HEAD").is_file() {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.fail("Missing HEAD file");
}
if !expected_path.join("config").is_file() {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.fail("Missing config file");
}
if !expected_path.join("objects").is_dir() {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.fail("Missing objects/ directory");
}
if !expected_path.join("refs").is_dir() {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.fail("Missing refs/ directory");
}
// Verify the helper function agrees
if !is_bare_repository(&expected_path) {
return TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.fail("Helper function does not recognize repository as bare");
}
TestResult::new(
test_name,
"GRASP-01",
"Bare repository must have correct structure",
)
.pass()
}
}
/// Helper function to check if a path is a valid bare git repository
///
/// A bare repository must have:
/// - HEAD file
/// - config file
/// - objects/ directory
/// - refs/ directory
pub fn is_bare_repository(path: &Path) -> bool {
if !path.exists() {
return false;
}
// Check for required bare repository components
let has_head = path.join("HEAD").is_file();
let has_config = path.join("config").is_file();
let has_objects = path.join("objects").is_dir();
let has_refs = path.join("refs").is_dir();
has_head && has_config && has_objects && has_refs
}
#[cfg(test)]
mod tests {
use super::*;
use std::process::Command;
#[test]
fn test_is_bare_repository_detects_valid_repo() {
// Create a temporary bare repository for testing
let temp_dir = tempfile::tempdir().unwrap();
let repo_path = temp_dir.path().join("test.git");
// Initialize a bare repository
Command::new("git")
.args(&["init", "--bare", repo_path.to_str().unwrap()])
.output()
.expect("Failed to create test repository");
// Verify our helper function detects it
assert!(
is_bare_repository(&repo_path),
"Should detect valid bare repository"
);
}
#[test]
fn test_is_bare_repository_rejects_non_repo() {
let temp_dir = tempfile::tempdir().unwrap();
assert!(
!is_bare_repository(temp_dir.path()),
"Should reject non-repository directory"
);
}
#[test]
fn test_is_bare_repository_rejects_nonexistent() {
let path = Path::new("/nonexistent/path/to/repo.git");
assert!(!is_bare_repository(path), "Should reject nonexistent path");
}
}
|