arguments_test.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. package executor
  2. import (
  3. "fmt"
  4. "strings"
  5. config "github.com/OliveTin/OliveTin/internal/config"
  6. "github.com/OliveTin/OliveTin/internal/entities"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestSanitizeUnsafe(t *testing.T) {
  11. assert.Nil(t, TypeSafetyCheck("", "_zomg_ c:/ haxxor ' bobby tables && rm -rf ", "very_dangerous_raw_string"))
  12. }
  13. func TestSanitizeUnimplemented(t *testing.T) {
  14. err := TypeSafetyCheck("", "I am a happy little argument", "greeting_type")
  15. assert.NotNil(t, err, "Test an argument type that does not exist")
  16. }
  17. func TestArgumentValueNullable(t *testing.T) {
  18. a1 := config.Action{
  19. Title: "Release the hounds",
  20. Shell: "echo 'Releasing {{ count }} hounds'",
  21. Arguments: []config.ActionArgument{
  22. {
  23. Name: "count",
  24. Type: "int",
  25. },
  26. },
  27. }
  28. values := map[string]string{
  29. "count": "",
  30. }
  31. out, err := parseActionArguments(values, &a1, nil)
  32. assert.Equal(t, "echo 'Releasing hounds'", out)
  33. assert.Nil(t, err)
  34. a1.Arguments[0].RejectNull = true
  35. _, err = parseActionArguments(values, &a1, nil)
  36. assert.NotNil(t, err)
  37. }
  38. func TestArgumentNameNumbers(t *testing.T) {
  39. a1 := config.Action{
  40. Title: "Do some tickles",
  41. Shell: "echo 'Tickling {{ person1name }}'",
  42. Arguments: []config.ActionArgument{
  43. {
  44. Name: "person1name",
  45. Type: "ascii",
  46. },
  47. },
  48. }
  49. values := map[string]string{
  50. "person1name": "Fred",
  51. }
  52. out, err := parseActionArguments(values, &a1, nil)
  53. assert.Equal(t, "echo 'Tickling Fred'", out)
  54. assert.Nil(t, err)
  55. }
  56. func TestArgumentNotProvided(t *testing.T) {
  57. a1 := config.Action{
  58. Title: "Do some tickles",
  59. Shell: "echo 'Tickling {{ personName }}'",
  60. Arguments: []config.ActionArgument{
  61. {
  62. Name: "person",
  63. Type: "ascii",
  64. },
  65. },
  66. }
  67. values := map[string]string{}
  68. out, err := parseActionArguments(values, &a1, nil)
  69. assert.Equal(t, "", out)
  70. assert.Equal(t, err.Error(), "required arg not provided: personName")
  71. }
  72. func TestExecArrayParsing(t *testing.T) {
  73. a1 := config.Action{
  74. Title: "List files",
  75. Exec: []string{"ls", "-alh"},
  76. Arguments: []config.ActionArgument{},
  77. }
  78. values := map[string]string{}
  79. out, err := parseActionExec(values, &a1, nil)
  80. assert.Nil(t, err)
  81. assert.Equal(t, []string{"ls", "-alh"}, out)
  82. }
  83. func TestExecArrayWithTemplateReplacement(t *testing.T) {
  84. a1 := config.Action{
  85. Title: "List specific path",
  86. Exec: []string{"ls", "-alh", "{{path}}"},
  87. Arguments: []config.ActionArgument{
  88. {
  89. Name: "path",
  90. Type: "ascii_identifier",
  91. },
  92. },
  93. }
  94. values := map[string]string{
  95. "path": "tmp",
  96. }
  97. out, err := parseActionExec(values, &a1, nil)
  98. assert.Nil(t, err)
  99. assert.Equal(t, []string{"ls", "-alh", "tmp"}, out)
  100. }
  101. func TestCheckShellArgumentSafetyWithURL(t *testing.T) {
  102. a1 := config.Action{
  103. Title: "Download file",
  104. Shell: "curl {{url}}",
  105. Arguments: []config.ActionArgument{
  106. {
  107. Name: "url",
  108. Type: "url",
  109. },
  110. },
  111. }
  112. err := checkShellArgumentSafety(&a1)
  113. assert.NotNil(t, err)
  114. assert.Contains(t, err.Error(), "unsafe argument type 'url' cannot be used with Shell execution")
  115. assert.Contains(t, err.Error(), "https://docs.olivetin.app/action_execution/shellvsexec.html")
  116. }
  117. func TestCheckShellArgumentSafetyWithEmail(t *testing.T) {
  118. a1 := config.Action{
  119. Title: "Send email",
  120. Shell: "sendmail {{email}}",
  121. Arguments: []config.ActionArgument{
  122. {
  123. Name: "email",
  124. Type: "email",
  125. },
  126. },
  127. }
  128. err := checkShellArgumentSafety(&a1)
  129. assert.NotNil(t, err)
  130. assert.Contains(t, err.Error(), "unsafe argument type 'email' cannot be used with Shell execution")
  131. }
  132. func TestCheckShellArgumentSafetyWithExec(t *testing.T) {
  133. a1 := config.Action{
  134. Title: "Download file",
  135. Exec: []string{"curl", "{{url}}"},
  136. Arguments: []config.ActionArgument{
  137. {
  138. Name: "url",
  139. Type: "url",
  140. },
  141. },
  142. }
  143. err := checkShellArgumentSafety(&a1)
  144. assert.Nil(t, err)
  145. }
  146. func TestCheckShellArgumentSafetyWithSafeTypes(t *testing.T) {
  147. a1 := config.Action{
  148. Title: "List files",
  149. Shell: "ls {{path}}",
  150. Arguments: []config.ActionArgument{
  151. {
  152. Name: "path",
  153. Type: "ascii_identifier",
  154. },
  155. },
  156. }
  157. err := checkShellArgumentSafety(&a1)
  158. assert.Nil(t, err)
  159. }
  160. func TestTypeSafetyCheckUrl(t *testing.T) {
  161. assert.Nil(t, TypeSafetyCheck("test1", "http://google.com", "url"), "Test URL: google.com")
  162. assert.Nil(t, TypeSafetyCheck("test2", "http://technowax.net:80?foo=bar", "url"), "Test URL: technowax.net with query arguments")
  163. assert.Nil(t, TypeSafetyCheck("test3", "http://localhost:80?foo=bar", "url"), "Test URL: localhost with query arguments")
  164. assert.NotNil(t, TypeSafetyCheck("test4", "http://lo host:80", "url"), "Test a badly formed URL")
  165. assert.NotNil(t, TypeSafetyCheck("test5", "12345", "url"), "Test a badly formed URL")
  166. assert.NotNil(t, TypeSafetyCheck("test6", "_!23;", "url"), "Test a badly formed URL")
  167. }
  168. func TestTypeSafetyCheckRegex(t *testing.T) {
  169. tests := []struct {
  170. name string
  171. field string
  172. pattern string
  173. value string
  174. hasError bool
  175. }{
  176. {
  177. name: "Issue #578 - Domain",
  178. field: "domain",
  179. pattern: "regex:^(?:[a-zA-Z0-9-]{1,63}.)+[a-zA-Z]{2,63}$",
  180. value: "immich.example.dev",
  181. hasError: false,
  182. },
  183. {
  184. name: "Don't allow numbers in username",
  185. field: "Username",
  186. pattern: "regex:^[a-zA-Z]$",
  187. value: "James1234",
  188. hasError: true,
  189. },
  190. }
  191. for _, tt := range tests {
  192. t.Run(tt.name, func(t *testing.T) {
  193. err := typeSafetyCheckRegex(tt.field, tt.value, tt.pattern)
  194. if tt.hasError {
  195. assert.NotNil(t, err, "Expected error for value %s with pattern %s, but got no error", tt.value, tt.pattern)
  196. } else {
  197. assert.Nil(t, err, "Expected no error for value %s with pattern %s, but got error: %v", tt.value, tt.pattern, err)
  198. }
  199. })
  200. }
  201. }
  202. func TestRedactShellCommand(t *testing.T) {
  203. cmd := "echo 'The password for Fred is toomanysecrets'"
  204. args := []config.ActionArgument{
  205. {
  206. Name: "personName",
  207. Type: "ascii",
  208. },
  209. {
  210. Name: "password",
  211. Type: "password",
  212. },
  213. }
  214. values := map[string]string{
  215. "personName": "Fred",
  216. "password": "toomanysecrets",
  217. }
  218. res := redactShellCommand(cmd, args, values)
  219. assert.Equal(t, "echo 'The password for Fred is <redacted>'", res, "Redacted shell command should mask the password argument")
  220. // Test with empty password
  221. values["password"] = ""
  222. res = redactShellCommand(cmd, args, values)
  223. assert.Equal(t, cmd, res, "Empty password should not change the command")
  224. // Test with missing password argument
  225. delete(values, "password")
  226. res = redactShellCommand(cmd, args, values)
  227. assert.Equal(t, cmd, res, "Missing password argument should not change the command")
  228. }
  229. func TestTypeSafetyCheckEmail(t *testing.T) {
  230. tests := []struct {
  231. name string
  232. field string
  233. value string
  234. hasError bool
  235. }{
  236. {"Valid simple email", "email", "user@example.com", false},
  237. {"Valid email with subdomain", "email", "user@mail.example.com", false},
  238. {"Valid email with plus", "email", "user+test@example.com", false},
  239. {"Valid email with dash", "email", "user-name@example.com", false},
  240. {"Valid email with numbers", "email", "user123@example123.com", false},
  241. {"Invalid email no @", "email", "userexample.com", true},
  242. {"Invalid email no domain", "email", "user@", true},
  243. {"Invalid email no user", "email", "@example.com", true},
  244. {"Invalid email spaces", "email", "user name@example.com", true},
  245. {"Invalid email double @", "email", "user@@example.com", true},
  246. }
  247. for _, tt := range tests {
  248. t.Run(tt.name, func(t *testing.T) {
  249. err := TypeSafetyCheck(tt.field, tt.value, "email")
  250. if tt.hasError {
  251. assert.NotNil(t, err, "Expected error for value '%s'", tt.value)
  252. } else {
  253. assert.Nil(t, err, "Expected no error for value '%s', but got: %v", tt.value, err)
  254. }
  255. })
  256. }
  257. }
  258. func TestTypeSafetyCheckDatetime(t *testing.T) {
  259. tests := []struct {
  260. name string
  261. field string
  262. value string
  263. hasError bool
  264. }{
  265. {"Valid datetime", "datetime", "2023-12-25T15:30:45", false},
  266. {"Valid datetime morning", "datetime", "2023-01-01T00:00:00", false},
  267. {"Valid datetime evening", "datetime", "2023-12-31T23:59:59", false},
  268. {"Invalid format missing T", "datetime", "2023-12-25 15:30:45", true},
  269. {"Invalid format missing seconds", "datetime", "2023-12-25T15:30", true},
  270. {"Invalid date", "datetime", "2023-13-25T15:30:45", true},
  271. {"Invalid time", "datetime", "2023-12-25T25:30:45", true},
  272. {"Random string", "datetime", "not-a-date", true},
  273. }
  274. for _, tt := range tests {
  275. t.Run(tt.name, func(t *testing.T) {
  276. err := TypeSafetyCheck(tt.field, tt.value, "datetime")
  277. if tt.hasError {
  278. assert.NotNil(t, err, "Expected error for value '%s'", tt.value)
  279. } else {
  280. assert.Nil(t, err, "Expected no error for value '%s', but got: %v", tt.value, err)
  281. }
  282. })
  283. }
  284. }
  285. func TestTypeSafetyCheckRawStringMultiline(t *testing.T) {
  286. tests := []struct {
  287. name string
  288. field string
  289. value string
  290. }{
  291. {"Simple string", "content", "hello world"},
  292. {"Multiline string", "content", "line1\nline2\nline3"},
  293. {"String with special chars", "content", "!@#$%^&*()"},
  294. {"Unicode string", "content", "héllo wörld 🌍"},
  295. {"Very long string", "content", strings.Repeat("a", 1000)},
  296. }
  297. for _, tt := range tests {
  298. t.Run(tt.name, func(t *testing.T) {
  299. err := TypeSafetyCheck(tt.field, tt.value, "raw_string_multiline")
  300. assert.Nil(t, err, "raw_string_multiline should accept any value")
  301. })
  302. }
  303. }
  304. func TestTypeSafetyCheckUnicodeIdentifier(t *testing.T) {
  305. tests := []struct {
  306. name string
  307. field string
  308. value string
  309. expectsError bool
  310. }{
  311. {"Valid unicode identifier", "name", "hello_world", false},
  312. {"Valid with numbers", "name", "test123", false},
  313. {"Valid with dots", "name", "file.txt", false},
  314. {"Valid with underscores", "name", "my_file_name", false},
  315. {"Invalid with special chars", "name", "hello@world", true},
  316. {"Invalid with brackets", "name", "hello[world]", true},
  317. {"Invalid with spaces", "name", "hello world", true},
  318. {"Invalid with path separators", "name", "path/to/file", true},
  319. {"Invalid with backslashes", "name", "path\\to\\file", true},
  320. }
  321. for _, tt := range tests {
  322. t.Run(tt.name, func(t *testing.T) {
  323. err := TypeSafetyCheck(tt.field, tt.value, "unicode_identifier")
  324. validateTypeSafetyResult(t, tt.value, tt.expectsError, err)
  325. })
  326. }
  327. }
  328. func validateTypeSafetyResult(t *testing.T, value string, expectsError bool, err error) {
  329. if expectsError {
  330. assertErrorExpected(t, value, err)
  331. } else {
  332. assertNoErrorExpected(t, value, err)
  333. }
  334. }
  335. func assertErrorExpected(t *testing.T, value string, err error) {
  336. if err == nil {
  337. t.Errorf("Expected error for value '%s', but got none", value)
  338. } else {
  339. t.Logf("Received expected error for value '%s': %v", value, err)
  340. }
  341. }
  342. func assertNoErrorExpected(t *testing.T, value string, err error) {
  343. if err != nil {
  344. t.Errorf("Expected no error for value '%s', but got: %v", value, err)
  345. } else {
  346. t.Logf("No error for valid value '%s' as expected", value)
  347. }
  348. }
  349. func TestTypeSafetyCheckAsciiIdentifier(t *testing.T) {
  350. tests := []struct {
  351. name string
  352. field string
  353. value string
  354. hasError bool
  355. }{
  356. {"Valid identifier", "name", "hello_world", false},
  357. {"Valid with numbers", "name", "test123", false},
  358. {"Valid with dots", "name", "file.txt", false},
  359. {"Valid with dashes", "name", "my-file", false},
  360. {"Valid with underscores", "name", "my_file", false},
  361. {"Invalid with spaces", "name", "hello world", true},
  362. {"Invalid with special chars", "name", "hello@world", true},
  363. {"Invalid unicode", "name", "héllo", true},
  364. }
  365. for _, tt := range tests {
  366. t.Run(tt.name, func(t *testing.T) {
  367. err := TypeSafetyCheck(tt.field, tt.value, "ascii_identifier")
  368. if tt.hasError {
  369. assert.NotNil(t, err, "Expected error for value '%s'", tt.value)
  370. } else {
  371. assert.Nil(t, err, "Expected no error for value '%s', but got: %v", tt.value, err)
  372. }
  373. })
  374. }
  375. }
  376. func TestTypeSafetyCheckAsciiSentence(t *testing.T) {
  377. tests := []struct {
  378. name string
  379. field string
  380. value string
  381. hasError bool
  382. }{
  383. {"Valid sentence", "text", "Hello world", false},
  384. {"Valid with numbers", "text", "Test 123", false},
  385. {"Valid with commas", "text", "Hello, world", false},
  386. {"Valid with periods", "text", "Hello world.", false},
  387. {"Valid with multiple spaces", "text", "Hello world", false},
  388. {"Invalid with special chars", "text", "Hello@world", true},
  389. {"Invalid with parentheses", "text", "Hello (world)", true},
  390. {"Invalid unicode", "text", "Héllo world", true},
  391. }
  392. for _, tt := range tests {
  393. t.Run(tt.name, func(t *testing.T) {
  394. err := TypeSafetyCheck(tt.field, tt.value, "ascii_sentence")
  395. if tt.hasError {
  396. assert.NotNil(t, err, "Expected error for value '%s'", tt.value)
  397. } else {
  398. assert.Nil(t, err, "Expected no error for value '%s', but got: %v", tt.value, err)
  399. }
  400. })
  401. }
  402. }
  403. func TestTypecheckActionArgumentEmptyName(t *testing.T) {
  404. arg := config.ActionArgument{
  405. Name: "",
  406. Type: "ascii",
  407. }
  408. action := config.Action{Title: "Test"}
  409. err := typecheckActionArgument(&arg, "test", &action)
  410. assert.NotNil(t, err)
  411. assert.Contains(t, err.Error(), "argument name cannot be empty")
  412. }
  413. func TestTypecheckActionArgumentConfirmation(t *testing.T) {
  414. arg := config.ActionArgument{
  415. Name: "confirm",
  416. Type: "confirmation",
  417. }
  418. action := config.Action{Title: "Test"}
  419. err := typecheckActionArgument(&arg, "any_value", &action)
  420. assert.Nil(t, err, "Confirmation type should always pass validation")
  421. }
  422. func TestParseCommandForReplacements(t *testing.T) {
  423. tests := []struct {
  424. name string
  425. shellCommand string
  426. values map[string]string
  427. expectedOutput string
  428. expectError bool
  429. errorContains string
  430. }{
  431. {
  432. name: "Simple replacement",
  433. shellCommand: "echo {{ name }}",
  434. values: map[string]string{"name": "John"},
  435. expectedOutput: "echo John",
  436. expectError: false,
  437. },
  438. {
  439. name: "Multiple replacements",
  440. shellCommand: "echo {{ first }} {{ last }}",
  441. values: map[string]string{"first": "John", "last": "Doe"},
  442. expectedOutput: "echo John Doe",
  443. expectError: false,
  444. },
  445. {
  446. name: "Replacement with spaces in template",
  447. shellCommand: "echo {{ name }}",
  448. values: map[string]string{"name": "John"},
  449. expectedOutput: "echo John",
  450. expectError: false,
  451. },
  452. {
  453. name: "Missing argument",
  454. shellCommand: "echo {{ missing }}",
  455. values: map[string]string{},
  456. expectedOutput: "",
  457. expectError: true,
  458. errorContains: "required arg not provided: missing",
  459. },
  460. {
  461. name: "No replacements needed",
  462. shellCommand: "echo hello",
  463. values: map[string]string{},
  464. expectedOutput: "echo hello",
  465. expectError: false,
  466. },
  467. {
  468. name: "Multiple same argument",
  469. shellCommand: "echo {{ name }} says hello {{ name }}",
  470. values: map[string]string{"name": "Alice"},
  471. expectedOutput: "echo Alice says hello Alice",
  472. expectError: false,
  473. },
  474. }
  475. for _, tt := range tests {
  476. t.Run(tt.name, func(t *testing.T) {
  477. output, err := parseCommandForReplacements(tt.shellCommand, tt.values, nil)
  478. if tt.expectError {
  479. assert.NotNil(t, err, "Expected error but got none")
  480. if tt.errorContains != "" {
  481. assert.Contains(t, err.Error(), tt.errorContains)
  482. }
  483. } else {
  484. assert.Nil(t, err, "Expected no error but got: %v", err)
  485. assert.Equal(t, tt.expectedOutput, output)
  486. }
  487. })
  488. }
  489. }
  490. func TestArgumentChoicesValidation(t *testing.T) {
  491. tests := []struct {
  492. name string
  493. action config.Action
  494. values map[string]string
  495. expectError bool
  496. description string
  497. }{
  498. {
  499. name: "Valid choice",
  500. action: config.Action{
  501. Title: "Test choices",
  502. Shell: "echo {{ option }}",
  503. Arguments: []config.ActionArgument{
  504. {
  505. Name: "option",
  506. Type: "ascii",
  507. Choices: []config.ActionArgumentChoice{
  508. {Value: "option1", Title: "Option 1"},
  509. {Value: "option2", Title: "Option 2"},
  510. },
  511. },
  512. },
  513. },
  514. values: map[string]string{"option": "option1"},
  515. expectError: false,
  516. description: "Should accept valid choice",
  517. },
  518. {
  519. name: "Invalid choice",
  520. action: config.Action{
  521. Title: "Test choices",
  522. Shell: "echo {{ option }}",
  523. Arguments: []config.ActionArgument{
  524. {
  525. Name: "option",
  526. Type: "ascii",
  527. Choices: []config.ActionArgumentChoice{
  528. {Value: "option1", Title: "Option 1"},
  529. {Value: "option2", Title: "Option 2"},
  530. },
  531. },
  532. },
  533. },
  534. values: map[string]string{"option": "invalid_option"},
  535. expectError: true,
  536. description: "Should reject invalid choice",
  537. },
  538. }
  539. for _, tt := range tests {
  540. t.Run(tt.name, func(t *testing.T) {
  541. _, err := parseActionArguments(tt.values, &tt.action, nil)
  542. if tt.expectError {
  543. assert.NotNil(t, err, tt.description)
  544. assert.Contains(t, err.Error(), "predefined choices")
  545. } else {
  546. assert.Nil(t, err, tt.description)
  547. }
  548. })
  549. }
  550. }
  551. func TestTypeSafetyCheckVeryDangerousRawString(t *testing.T) {
  552. // This type should allow anything without validation
  553. tests := []string{
  554. "normal text",
  555. "_zomg_ c:/ haxxor ' bobby tables && rm -rf /",
  556. "$(rm -rf /)",
  557. "; DROP TABLE users; --",
  558. "../../../../etc/passwd",
  559. "",
  560. "unicode: 你好世界",
  561. "emojis: 🔥💀☠️",
  562. }
  563. for _, value := range tests {
  564. t.Run(fmt.Sprintf("Value: %s", value), func(t *testing.T) {
  565. err := TypeSafetyCheck("test", value, "very_dangerous_raw_string")
  566. assert.Nil(t, err, "very_dangerous_raw_string should accept any value including: %s", value)
  567. })
  568. }
  569. }
  570. func TestParseActionArgumentsWithEntityPrefix(t *testing.T) {
  571. action := config.Action{
  572. Title: "Test entity prefix",
  573. Shell: "echo 'Processing {{ name }} for entity'",
  574. Arguments: []config.ActionArgument{
  575. {Name: "name", Type: "ascii"},
  576. },
  577. }
  578. values := map[string]string{
  579. "name": "testuser",
  580. }
  581. ent := &entities.Entity{
  582. Title: "entity_123",
  583. }
  584. // Test with entity prefix
  585. output, err := parseActionArguments(values, &action, ent)
  586. assert.Nil(t, err)
  587. assert.Contains(t, output, "testuser")
  588. }
  589. func TestComplexRegexPatterns(t *testing.T) {
  590. tests := []struct {
  591. name string
  592. pattern string
  593. value string
  594. hasError bool
  595. }{
  596. {
  597. name: "Phone number pattern",
  598. pattern: "regex:^\\+?[1-9]\\d{1,14}$",
  599. value: "+1234567890",
  600. hasError: false,
  601. },
  602. {
  603. name: "Invalid phone number",
  604. pattern: "regex:^\\+?[1-9]\\d{1,14}$",
  605. value: "123abc",
  606. hasError: true,
  607. },
  608. {
  609. name: "Semantic version pattern",
  610. pattern: "regex:^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$",
  611. value: "1.2.3",
  612. hasError: false,
  613. },
  614. {
  615. name: "Invalid semantic version",
  616. pattern: "regex:^(0|[1-9]\\d*)\\.(0|[1-9]\\d*)\\.(0|[1-9]\\d*)$",
  617. value: "1.2",
  618. hasError: true,
  619. },
  620. }
  621. for _, tt := range tests {
  622. t.Run(tt.name, func(t *testing.T) {
  623. err := typeSafetyCheckRegex("test", tt.value, tt.pattern)
  624. if tt.hasError {
  625. assert.NotNil(t, err)
  626. } else {
  627. assert.Nil(t, err)
  628. }
  629. })
  630. }
  631. }