|
9 | 9 | "io/ioutil"
|
10 | 10 | "net/http"
|
11 | 11 | "net/http/httptest"
|
| 12 | + "strings" |
12 | 13 | "testing"
|
13 | 14 | "time"
|
14 | 15 | )
|
@@ -95,3 +96,117 @@ func TestToken(t *testing.T) {
|
95 | 96 | }
|
96 | 97 |
|
97 | 98 | }
|
| 99 | + |
| 100 | +func TestValidateURLTokenURL(t *testing.T) { |
| 101 | + var urlValidityTests = []struct { |
| 102 | + tokURL string |
| 103 | + expectSuccess bool |
| 104 | + }{ |
| 105 | + {"https://east.sts.googleapis.com", true}, |
| 106 | + {"https://sts.googleapis.com", true}, |
| 107 | + {"https://sts.asfeasfesef.googleapis.com", true}, |
| 108 | + {"https://us-east-1-sts.googleapis.com", true}, |
| 109 | + {"https://sts.googleapis.com/your/path/here", true}, |
| 110 | + {"https://.sts.googleapis.com", false}, |
| 111 | + {"https://badsts.googleapis.com", false}, |
| 112 | + {"https://sts.asfe.asfesef.googleapis.com", false}, |
| 113 | + {"https://sts..googleapis.com", false}, |
| 114 | + {"https://-sts.googleapis.com", false}, |
| 115 | + {"https://us-ea.st-1-sts.googleapis.com", false}, |
| 116 | + {"https://sts.googleapis.com.evil.com/whatever/path", false}, |
| 117 | + {"https://us-eas\\t-1.sts.googleapis.com", false}, |
| 118 | + {"https:/us-ea/st-1.sts.googleapis.com", false}, |
| 119 | + {"https:/us-east 1.sts.googleapis.com", false}, |
| 120 | + {"https://", false}, |
| 121 | + {"http://us-east-1.sts.googleapis.com", false}, |
| 122 | + {"https://us-east-1.sts.googleapis.comevil.com", false}, |
| 123 | + } |
| 124 | + ctx := context.Background() |
| 125 | + for _, tt := range urlValidityTests { |
| 126 | + t.Run(" "+tt.tokURL, func(t *testing.T) { // We prepend a space ahead of the test input when outputting for sake of readability. |
| 127 | + config := testConfig |
| 128 | + config.TokenURL = tt.tokURL |
| 129 | + _, err := config.TokenSource(ctx) |
| 130 | + |
| 131 | + if tt.expectSuccess && err != nil { |
| 132 | + t.Errorf("got %v but want nil", err) |
| 133 | + } else if !tt.expectSuccess && err == nil { |
| 134 | + t.Errorf("got nil but expected an error") |
| 135 | + } |
| 136 | + }) |
| 137 | + } |
| 138 | + for _, el := range urlValidityTests { |
| 139 | + el.tokURL = strings.ToUpper(el.tokURL) |
| 140 | + } |
| 141 | + for _, tt := range urlValidityTests { |
| 142 | + t.Run(" "+tt.tokURL, func(t *testing.T) { // We prepend a space ahead of the test input when outputting for sake of readability. |
| 143 | + config := testConfig |
| 144 | + config.TokenURL = tt.tokURL |
| 145 | + _, err := config.TokenSource(ctx) |
| 146 | + |
| 147 | + if tt.expectSuccess && err != nil { |
| 148 | + t.Errorf("got %v but want nil", err) |
| 149 | + } else if !tt.expectSuccess && err == nil { |
| 150 | + t.Errorf("got nil but expected an error") |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +func TestValidateURLImpersonateURL(t *testing.T) { |
| 157 | + var urlValidityTests = []struct { |
| 158 | + impURL string |
| 159 | + expectSuccess bool |
| 160 | + }{ |
| 161 | + {"https://east.iamcredentials.googleapis.com", true}, |
| 162 | + {"https://iamcredentials.googleapis.com", true}, |
| 163 | + {"https://iamcredentials.asfeasfesef.googleapis.com", true}, |
| 164 | + {"https://us-east-1-iamcredentials.googleapis.com", true}, |
| 165 | + {"https://iamcredentials.googleapis.com/your/path/here", true}, |
| 166 | + {"https://.iamcredentials.googleapis.com", false}, |
| 167 | + {"https://badiamcredentials.googleapis.com", false}, |
| 168 | + {"https://iamcredentials.asfe.asfesef.googleapis.com", false}, |
| 169 | + {"https://iamcredentials..googleapis.com", false}, |
| 170 | + {"https://-iamcredentials.googleapis.com", false}, |
| 171 | + {"https://us-ea.st-1-iamcredentials.googleapis.com", false}, |
| 172 | + {"https://iamcredentials.googleapis.com.evil.com/whatever/path", false}, |
| 173 | + {"https://us-eas\\t-1.iamcredentials.googleapis.com", false}, |
| 174 | + {"https:/us-ea/st-1.iamcredentials.googleapis.com", false}, |
| 175 | + {"https:/us-east 1.iamcredentials.googleapis.com", false}, |
| 176 | + {"https://", false}, |
| 177 | + {"http://us-east-1.iamcredentials.googleapis.com", false}, |
| 178 | + {"https://us-east-1.iamcredentials.googleapis.comevil.com", false}, |
| 179 | + } |
| 180 | + ctx := context.Background() |
| 181 | + for _, tt := range urlValidityTests { |
| 182 | + t.Run(" "+tt.impURL, func(t *testing.T) { // We prepend a space ahead of the test input when outputting for sake of readability. |
| 183 | + config := testConfig |
| 184 | + config.TokenURL = "https://sts.googleapis.com" // Setting the most basic acceptable tokenURL |
| 185 | + config.ServiceAccountImpersonationURL = tt.impURL |
| 186 | + _, err := config.TokenSource(ctx) |
| 187 | + |
| 188 | + if tt.expectSuccess && err != nil { |
| 189 | + t.Errorf("got %v but want nil", err) |
| 190 | + } else if !tt.expectSuccess && err == nil { |
| 191 | + t.Errorf("got nil but expected an error") |
| 192 | + } |
| 193 | + }) |
| 194 | + } |
| 195 | + for _, el := range urlValidityTests { |
| 196 | + el.impURL = strings.ToUpper(el.impURL) |
| 197 | + } |
| 198 | + for _, tt := range urlValidityTests { |
| 199 | + t.Run(" "+tt.impURL, func(t *testing.T) { // We prepend a space ahead of the test input when outputting for sake of readability. |
| 200 | + config := testConfig |
| 201 | + config.TokenURL = "https://sts.googleapis.com" // Setting the most basic acceptable tokenURL |
| 202 | + config.ServiceAccountImpersonationURL = tt.impURL |
| 203 | + _, err := config.TokenSource(ctx) |
| 204 | + |
| 205 | + if tt.expectSuccess && err != nil { |
| 206 | + t.Errorf("got %v but want nil", err) |
| 207 | + } else if !tt.expectSuccess && err == nil { |
| 208 | + t.Errorf("got nil but expected an error") |
| 209 | + } |
| 210 | + }) |
| 211 | + } |
| 212 | +} |
0 commit comments