SafePathTest.vala 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * SafePathTest - Unit tests for SafePath
  3. *
  4. * Comprehensive tests for the SafePath factory class which creates
  5. * URL-encoded EntityPath instances.
  6. */
  7. using Implexus.Core;
  8. public static int main(string[] args) {
  9. int passed = 0;
  10. int failed = 0;
  11. // ========================================
  12. // 1. Basic Path Construction Tests
  13. // ========================================
  14. // Test: Single segment path
  15. if (test_basic_single_segment()) {
  16. passed++;
  17. stdout.puts("PASS: test_basic_single_segment\n");
  18. } else {
  19. failed++;
  20. stdout.puts("FAIL: test_basic_single_segment\n");
  21. }
  22. // Test: Multi-segment path
  23. if (test_basic_multi_segment()) {
  24. passed++;
  25. stdout.puts("PASS: test_basic_multi_segment\n");
  26. } else {
  27. failed++;
  28. stdout.puts("FAIL: test_basic_multi_segment\n");
  29. }
  30. // Test: Empty segments handling
  31. if (test_basic_empty_segments()) {
  32. passed++;
  33. stdout.puts("PASS: test_basic_empty_segments\n");
  34. } else {
  35. failed++;
  36. stdout.puts("FAIL: test_basic_empty_segments\n");
  37. }
  38. // ========================================
  39. // 2. URL Encoding Tests
  40. // ========================================
  41. // Test: Spaces in segments
  42. if (test_encoding_spaces()) {
  43. passed++;
  44. stdout.puts("PASS: test_encoding_spaces\n");
  45. } else {
  46. failed++;
  47. stdout.puts("FAIL: test_encoding_spaces\n");
  48. }
  49. // Test: Special characters (/, ?, #, =, &)
  50. if (test_encoding_special_chars()) {
  51. passed++;
  52. stdout.puts("PASS: test_encoding_special_chars\n");
  53. } else {
  54. failed++;
  55. stdout.puts("FAIL: test_encoding_special_chars\n");
  56. }
  57. // Test: Unicode characters (Japanese, emoji)
  58. if (test_encoding_unicode()) {
  59. passed++;
  60. stdout.puts("PASS: test_encoding_unicode\n");
  61. } else {
  62. failed++;
  63. stdout.puts("FAIL: test_encoding_unicode\n");
  64. }
  65. // Test: Reserved characters that should be encoded
  66. if (test_encoding_reserved_chars()) {
  67. passed++;
  68. stdout.puts("PASS: test_encoding_reserved_chars\n");
  69. } else {
  70. failed++;
  71. stdout.puts("FAIL: test_encoding_reserved_chars\n");
  72. }
  73. // ========================================
  74. // 3. Variadic API Tests
  75. // ========================================
  76. // Test: Null as first argument (should return root path)
  77. if (test_variadic_null_first()) {
  78. passed++;
  79. stdout.puts("PASS: test_variadic_null_first\n");
  80. } else {
  81. failed++;
  82. stdout.puts("FAIL: test_variadic_null_first\n");
  83. }
  84. // Test: Single segment + null
  85. if (test_variadic_single_segment()) {
  86. passed++;
  87. stdout.puts("PASS: test_variadic_single_segment\n");
  88. } else {
  89. failed++;
  90. stdout.puts("FAIL: test_variadic_single_segment\n");
  91. }
  92. // Test: Multiple segments + null
  93. if (test_variadic_multiple_segments()) {
  94. passed++;
  95. stdout.puts("PASS: test_variadic_multiple_segments\n");
  96. } else {
  97. failed++;
  98. stdout.puts("FAIL: test_variadic_multiple_segments\n");
  99. }
  100. // ========================================
  101. // 4. Array API Tests
  102. // ========================================
  103. // Test: Normal array of segments
  104. if (test_array_normal()) {
  105. passed++;
  106. stdout.puts("PASS: test_array_normal\n");
  107. } else {
  108. failed++;
  109. stdout.puts("FAIL: test_array_normal\n");
  110. }
  111. // Test: Empty array (should return root path)
  112. if (test_array_empty()) {
  113. passed++;
  114. stdout.puts("PASS: test_array_empty\n");
  115. } else {
  116. failed++;
  117. stdout.puts("FAIL: test_array_empty\n");
  118. }
  119. // Test: Array with special characters
  120. if (test_array_special_chars()) {
  121. passed++;
  122. stdout.puts("PASS: test_array_special_chars\n");
  123. } else {
  124. failed++;
  125. stdout.puts("FAIL: test_array_special_chars\n");
  126. }
  127. // ========================================
  128. // 5. Decode Tests
  129. // ========================================
  130. // Test: Decoding %20 to space
  131. if (test_decode_space()) {
  132. passed++;
  133. stdout.puts("PASS: test_decode_space\n");
  134. } else {
  135. failed++;
  136. stdout.puts("FAIL: test_decode_space\n");
  137. }
  138. // Test: Decoding %2F to /
  139. if (test_decode_slash()) {
  140. passed++;
  141. stdout.puts("PASS: test_decode_slash\n");
  142. } else {
  143. failed++;
  144. stdout.puts("FAIL: test_decode_slash\n");
  145. }
  146. // Test: Decoding unicode percent-encoded strings
  147. if (test_decode_unicode()) {
  148. passed++;
  149. stdout.puts("PASS: test_decode_unicode\n");
  150. } else {
  151. failed++;
  152. stdout.puts("FAIL: test_decode_unicode\n");
  153. }
  154. // Test: Invalid encoding error handling
  155. if (test_decode_invalid_encoding()) {
  156. passed++;
  157. stdout.puts("PASS: test_decode_invalid_encoding\n");
  158. } else {
  159. failed++;
  160. stdout.puts("FAIL: test_decode_invalid_encoding\n");
  161. }
  162. // ========================================
  163. // 6. Integration Tests
  164. // ========================================
  165. // Test: Verify returned EntityPath works correctly
  166. if (test_integration_entity_path()) {
  167. passed++;
  168. stdout.puts("PASS: test_integration_entity_path\n");
  169. } else {
  170. failed++;
  171. stdout.puts("FAIL: test_integration_entity_path\n");
  172. }
  173. // Test: to_string() on resulting paths
  174. if (test_integration_to_string()) {
  175. passed++;
  176. stdout.puts("PASS: test_integration_to_string\n");
  177. } else {
  178. failed++;
  179. stdout.puts("FAIL: test_integration_to_string\n");
  180. }
  181. // Test: Path operations (parent, append_child)
  182. if (test_integration_path_operations()) {
  183. passed++;
  184. stdout.puts("PASS: test_integration_path_operations\n");
  185. } else {
  186. failed++;
  187. stdout.puts("FAIL: test_integration_path_operations\n");
  188. }
  189. // Test: Round-trip encode/decode
  190. if (test_round_trip()) {
  191. passed++;
  192. stdout.puts("PASS: test_round_trip\n");
  193. } else {
  194. failed++;
  195. stdout.puts("FAIL: test_round_trip\n");
  196. }
  197. stdout.printf("\nResults: %d passed, %d failed\n", passed, failed);
  198. return failed > 0 ? 1 : 0;
  199. }
  200. // ========================================
  201. // 1. Basic Path Construction Tests
  202. // ========================================
  203. // Test: Single segment path
  204. bool test_basic_single_segment() {
  205. var path = SafePath.path("catalogue", null);
  206. if (path.is_root) return false;
  207. if (path.depth != 1) return false;
  208. if (path.name != "catalogue") return false;
  209. if (path.to_string() != "/catalogue") return false;
  210. return true;
  211. }
  212. // Test: Multi-segment path
  213. bool test_basic_multi_segment() {
  214. var path = SafePath.path("catalogue", "category", "document", null);
  215. if (path.is_root) return false;
  216. if (path.depth != 3) return false;
  217. if (path.name != "document") return false;
  218. if (path.to_string() != "/catalogue/category/document") return false;
  219. return true;
  220. }
  221. // Test: Empty segments handling
  222. bool test_basic_empty_segments() {
  223. // Empty string segment should be preserved as empty encoded segment
  224. var path = SafePath.path("a", "", "c", null);
  225. // Empty segments should still be counted
  226. if (path.depth != 3) return false;
  227. return true;
  228. }
  229. // ========================================
  230. // 2. URL Encoding Tests
  231. // ========================================
  232. // Test: Spaces in segments (should be encoded as %20)
  233. bool test_encoding_spaces() {
  234. var path = SafePath.path("users", "john doe", null);
  235. string path_str = path.to_string();
  236. // Space should be encoded as %20
  237. if (!path_str.contains("%20")) return false;
  238. if (path_str.contains(" ")) return false; // No literal space
  239. if (path_str != "/users/john%20doe") return false;
  240. // Test multiple spaces
  241. var path2 = SafePath.path("hello world test", null);
  242. if (path2.to_string() != "/hello%20world%20test") return false;
  243. return true;
  244. }
  245. // Test: Special characters (/, ?, #, =, &)
  246. bool test_encoding_special_chars() {
  247. // Forward slash should be encoded
  248. var path1 = SafePath.path("a/b", null);
  249. if (!path1.to_string().contains("%2F")) return false;
  250. // Question mark should be encoded
  251. var path2 = SafePath.path("query?param", null);
  252. if (!path2.to_string().contains("%3F")) return false;
  253. // Hash should be encoded
  254. var path3 = SafePath.path("anchor#link", null);
  255. if (!path3.to_string().contains("%23")) return false;
  256. // Equals should be encoded
  257. var path4 = SafePath.path("key=value", null);
  258. if (!path4.to_string().contains("%3D")) return false;
  259. // Ampersand should be encoded
  260. var path5 = SafePath.path("a&b", null);
  261. if (!path5.to_string().contains("%26")) return false;
  262. return true;
  263. }
  264. // Test: Unicode characters (Japanese, emoji)
  265. // Note: GLib.Uri.escape_string preserves unicode characters as-is (IRIs)
  266. // This is correct behavior for modern URIs
  267. bool test_encoding_unicode() {
  268. // Japanese characters - preserved as-is in the path
  269. var path1 = SafePath.path("ユーザー", null);
  270. string str1 = path1.to_string();
  271. // Should contain the unicode characters
  272. if (!str1.has_prefix("/")) return false;
  273. if (!str1.contains("ユーザー")) return false; // Unicode preserved
  274. // Emoji - also preserved
  275. var path2 = SafePath.path("hello🎉world", null);
  276. string str2 = path2.to_string();
  277. if (!str2.contains("🎉")) return false;
  278. // Chinese characters - also preserved
  279. var path3 = SafePath.path("用户", null);
  280. string str3 = path3.to_string();
  281. if (!str3.contains("用户")) return false;
  282. // But special URI characters within unicode strings should still be encoded
  283. var path4 = SafePath.path("ユーザー/名前", null);
  284. string str4 = path4.to_string();
  285. if (!str4.contains("%2F")) return false; // Slash should be encoded
  286. return true;
  287. }
  288. // Test: Reserved characters that should be encoded
  289. bool test_encoding_reserved_chars() {
  290. // Test various reserved characters
  291. string[] reserved_chars = { ":", "/", "?", "#", "[", "]", "@", "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "=" };
  292. foreach (var ch in reserved_chars) {
  293. var path = SafePath.path("test" + ch + "value", null);
  294. string result = path.to_string();
  295. // Reserved chars should be encoded (contain %)
  296. // The character itself should not appear literally (except for special cases)
  297. if (ch != "/" && result.contains(ch) && !result.contains("%")) {
  298. return false;
  299. }
  300. }
  301. return true;
  302. }
  303. // ========================================
  304. // 3. Variadic API Tests
  305. // ========================================
  306. // Test: Null as first argument (should return root path)
  307. bool test_variadic_null_first() {
  308. var path = SafePath.path(null);
  309. if (!path.is_root) return false;
  310. if (path.depth != 0) return false;
  311. if (path.to_string() != "/") return false;
  312. return true;
  313. }
  314. // Test: Single segment + null
  315. bool test_variadic_single_segment() {
  316. var path = SafePath.path("catalogue", null);
  317. if (path.is_root) return false;
  318. if (path.depth != 1) return false;
  319. if (path.name != "catalogue") return false;
  320. return true;
  321. }
  322. // Test: Multiple segments + null
  323. bool test_variadic_multiple_segments() {
  324. var path = SafePath.path("a", "b", "c", "d", "e", null);
  325. if (path.depth != 5) return false;
  326. if (path.to_string() != "/a/b/c/d/e") return false;
  327. // Verify each segment
  328. if (path.name != "e") return false;
  329. if (path.parent.name != "d") return false;
  330. return true;
  331. }
  332. // ========================================
  333. // 4. Array API Tests
  334. // ========================================
  335. // Test: Normal array of segments
  336. bool test_array_normal() {
  337. string[] segments = { "catalogue", "category", "document" };
  338. var path = SafePath.from_array(segments);
  339. if (path.depth != 3) return false;
  340. if (path.to_string() != "/catalogue/category/document") return false;
  341. if (path.name != "document") return false;
  342. return true;
  343. }
  344. // Test: Empty array (should return root path)
  345. bool test_array_empty() {
  346. string[] segments = { };
  347. var path = SafePath.from_array(segments);
  348. if (!path.is_root) return false;
  349. if (path.depth != 0) return false;
  350. if (path.to_string() != "/") return false;
  351. return true;
  352. }
  353. // Test: Array with special characters
  354. bool test_array_special_chars() {
  355. string[] segments = { "user name", "doc/with/slashes", "query?test" };
  356. var path = SafePath.from_array(segments);
  357. if (path.depth != 3) return false;
  358. string path_str = path.to_string();
  359. // Verify encoding
  360. if (!path_str.contains("%20")) return false; // Space encoded
  361. if (!path_str.contains("%2F")) return false; // Slash encoded
  362. if (!path_str.contains("%3F")) return false; // Question mark encoded
  363. return true;
  364. }
  365. // ========================================
  366. // 5. Decode Tests
  367. // ========================================
  368. // Test: Decoding %20 to space
  369. bool test_decode_space() {
  370. try {
  371. string decoded = SafePath.decode_segment("john%20doe");
  372. if (decoded != "john doe") return false;
  373. // Multiple spaces
  374. string decoded2 = SafePath.decode_segment("hello%20world%20test");
  375. if (decoded2 != "hello world test") return false;
  376. return true;
  377. } catch (EntityError e) {
  378. stdout.printf("Unexpected error: %s\n", e.message);
  379. return false;
  380. }
  381. }
  382. // Test: Decoding %2F to /
  383. bool test_decode_slash() {
  384. try {
  385. string decoded = SafePath.decode_segment("path%2Fwith%2Fslashes");
  386. if (decoded != "path/with/slashes") return false;
  387. return true;
  388. } catch (EntityError e) {
  389. stdout.printf("Unexpected error: %s\n", e.message);
  390. return false;
  391. }
  392. }
  393. // Test: Decoding unicode percent-encoded strings
  394. bool test_decode_unicode() {
  395. try {
  396. // Japanese characters encoded
  397. string decoded = SafePath.decode_segment("%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC");
  398. if (decoded != "ユーザー") return false;
  399. // Emoji encoded
  400. string decoded2 = SafePath.decode_segment("hello%F0%9F%8E%89world");
  401. if (decoded2 != "hello🎉world") return false;
  402. return true;
  403. } catch (EntityError e) {
  404. stdout.printf("Unexpected error: %s\n", e.message);
  405. return false;
  406. }
  407. }
  408. // Test: Invalid encoding error handling
  409. bool test_decode_invalid_encoding() {
  410. // Invalid percent encoding - incomplete sequence
  411. try {
  412. SafePath.decode_segment("invalid%");
  413. return false; // Should have thrown
  414. } catch (EntityError e) {
  415. // Expected - continue to test more cases
  416. }
  417. // Invalid hex digits
  418. try {
  419. SafePath.decode_segment("invalid%GG");
  420. return false; // Should have thrown
  421. } catch (EntityError e) {
  422. // Expected
  423. }
  424. // Incomplete percent sequence at end
  425. try {
  426. SafePath.decode_segment("test%2");
  427. return false; // Should have thrown
  428. } catch (EntityError e) {
  429. // Expected
  430. }
  431. return true;
  432. }
  433. // ========================================
  434. // 6. Integration Tests
  435. // ========================================
  436. // Test: Verify returned EntityPath works correctly
  437. bool test_integration_entity_path() {
  438. var path = SafePath.path("catalogue", "category", null);
  439. // Test is_root
  440. if (path.is_root) return false;
  441. // Test depth
  442. if (path.depth != 2) return false;
  443. // Test name
  444. if (path.name != "category") return false;
  445. // Test parent
  446. var parent = path.parent;
  447. if (parent.depth != 1) return false;
  448. if (parent.name != "catalogue") return false;
  449. // Test is_ancestor_of / is_descendant_of
  450. if (!parent.is_ancestor_of(path)) return false;
  451. if (!path.is_descendant_of(parent)) return false;
  452. return true;
  453. }
  454. // Test: to_string() on resulting paths
  455. bool test_integration_to_string() {
  456. // Simple path
  457. var path1 = SafePath.path("a", null);
  458. if (path1.to_string() != "/a") return false;
  459. // Multi-segment path
  460. var path2 = SafePath.path("a", "b", "c", null);
  461. if (path2.to_string() != "/a/b/c") return false;
  462. // Root path
  463. var path3 = SafePath.path(null);
  464. if (path3.to_string() != "/") return false;
  465. // Path with encoded characters
  466. var path4 = SafePath.path("hello world", null);
  467. if (path4.to_string() != "/hello%20world") return false;
  468. // Array API
  469. string[] segments = { "x", "y", "z" };
  470. var path5 = SafePath.from_array(segments);
  471. if (path5.to_string() != "/x/y/z") return false;
  472. return true;
  473. }
  474. // Test: Path operations (parent, append_child)
  475. bool test_integration_path_operations() {
  476. var path = SafePath.path("catalogue", "category", null);
  477. // Test parent
  478. var parent = path.parent;
  479. if (parent.to_string() != "/catalogue") return false;
  480. // Test append_child - note: EntityPath.append_child does NOT encode
  481. // It expects pre-encoded segment names
  482. var child = path.append_child("document");
  483. if (child.to_string() != "/catalogue/category/document") return false;
  484. if (child.name != "document") return false;
  485. // Test parent chain
  486. if (!child.parent.equals(path)) return false;
  487. // To add a child with special chars, use SafePath to encode first
  488. // or manually encode the segment
  489. var child_with_space = path.append_child("my%20document");
  490. if (!child_with_space.to_string().contains("%20")) return false;
  491. return true;
  492. }
  493. // Test: Round-trip encode/decode
  494. bool test_round_trip() {
  495. try {
  496. // Test with spaces
  497. string original1 = "john doe";
  498. var path1 = SafePath.path(original1, null);
  499. string encoded_name1 = path1.name;
  500. string decoded1 = SafePath.decode_segment(encoded_name1);
  501. if (decoded1 != original1) return false;
  502. // Test with special characters
  503. string original2 = "a/b?c#d=e&f";
  504. var path2 = SafePath.path(original2, null);
  505. string encoded_name2 = path2.name;
  506. string decoded2 = SafePath.decode_segment(encoded_name2);
  507. if (decoded2 != original2) return false;
  508. // Test with unicode
  509. string original3 = "ユーザー🎉";
  510. var path3 = SafePath.path(original3, null);
  511. string encoded_name3 = path3.name;
  512. string decoded3 = SafePath.decode_segment(encoded_name3);
  513. if (decoded3 != original3) return false;
  514. return true;
  515. } catch (EntityError e) {
  516. stdout.printf("Unexpected error in round-trip: %s\n", e.message);
  517. return false;
  518. }
  519. }