acr_validation_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. https://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package acr
  14. import (
  15. "testing"
  16. )
  17. func TestValidateACRRegistry(t *testing.T) {
  18. tests := []struct {
  19. name string
  20. registry string
  21. wantError bool
  22. }{
  23. {
  24. name: "valid registry - .io",
  25. registry: "myregistry.azurecr.io",
  26. wantError: false,
  27. },
  28. {
  29. name: "valid registry - .cn",
  30. registry: "myregistry.azurecr.cn",
  31. wantError: false,
  32. },
  33. {
  34. name: "valid registry - .de",
  35. registry: "myregistry.azurecr.de",
  36. wantError: false,
  37. },
  38. {
  39. name: "valid registry - .us",
  40. registry: "myregistry.azurecr.us",
  41. wantError: false,
  42. },
  43. {
  44. name: "valid registry - with dashes",
  45. registry: "my-registry-name.azurecr.io",
  46. wantError: false,
  47. },
  48. {
  49. name: "invalid registry - empty",
  50. registry: "",
  51. wantError: true,
  52. },
  53. {
  54. name: "invalid registry - SSRF attempt localhost",
  55. registry: "localhost",
  56. wantError: true,
  57. },
  58. {
  59. name: "invalid registry - SSRF attempt IP",
  60. registry: "192.168.1.1",
  61. wantError: true,
  62. },
  63. {
  64. name: "invalid registry - wrong domain",
  65. registry: "myregistry.example.com",
  66. wantError: true,
  67. },
  68. {
  69. name: "invalid registry - wrong TLD",
  70. registry: "myregistry.azurecr.com",
  71. wantError: true,
  72. },
  73. {
  74. name: "invalid registry - missing domain",
  75. registry: "azurecr.io",
  76. wantError: true,
  77. },
  78. {
  79. name: "invalid registry - uppercase",
  80. registry: "MyRegistry.azurecr.io",
  81. wantError: true,
  82. },
  83. }
  84. for _, tt := range tests {
  85. t.Run(tt.name, func(t *testing.T) {
  86. err := validateACRRegistry(tt.registry)
  87. if tt.wantError && err == nil {
  88. t.Errorf("validateACRRegistry() expected error but got none for registry: %s", tt.registry)
  89. }
  90. if !tt.wantError && err != nil {
  91. t.Errorf("validateACRRegistry() unexpected error: %v for registry: %s", err, tt.registry)
  92. }
  93. })
  94. }
  95. }