|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2025 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package jwt |
| 20 | + |
| 21 | +import ( |
| 22 | + "encoding/base64" |
| 23 | + "encoding/json" |
| 24 | + "errors" |
| 25 | + "fmt" |
| 26 | + "strings" |
| 27 | + "testing" |
| 28 | + "time" |
| 29 | +) |
| 30 | + |
| 31 | +func (s) TestJWTFileReader_ReadToken_FileErrors(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + create bool |
| 35 | + contents string |
| 36 | + wantErr error |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "nonexistent_file", |
| 40 | + create: false, |
| 41 | + contents: "", |
| 42 | + wantErr: errTokenFileAccess, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "empty_file", |
| 46 | + create: true, |
| 47 | + contents: "", |
| 48 | + wantErr: errJWTValidation, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "file_with_whitespace_only", |
| 52 | + create: true, |
| 53 | + contents: " \n\t ", |
| 54 | + wantErr: errJWTValidation, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for _, tt := range tests { |
| 59 | + t.Run(tt.name, func(t *testing.T) { |
| 60 | + var tokenFile string |
| 61 | + if !tt.create { |
| 62 | + tokenFile = "/does-not-exist" |
| 63 | + } else { |
| 64 | + tokenFile = writeTempFile(t, "token", tt.contents) |
| 65 | + } |
| 66 | + |
| 67 | + reader := jwtFileReader{tokenFilePath: tokenFile} |
| 68 | + if _, _, err := reader.readToken(); err == nil { |
| 69 | + t.Fatal("ReadToken() expected error, got nil") |
| 70 | + } else if !errors.Is(err, tt.wantErr) { |
| 71 | + t.Fatalf("ReadToken() error = %v, want error %v", err, tt.wantErr) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func (s) TestJWTFileReader_ReadToken_InvalidJWT(t *testing.T) { |
| 78 | + now := time.Now().Truncate(time.Second) |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + tokenContent string |
| 82 | + wantErr error |
| 83 | + }{ |
| 84 | + { |
| 85 | + name: "valid_token_without_expiration", |
| 86 | + tokenContent: createTestJWT(t, time.Time{}), |
| 87 | + wantErr: errJWTValidation, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "expired_token", |
| 91 | + tokenContent: createTestJWT(t, now.Add(-time.Hour)), |
| 92 | + wantErr: errJWTValidation, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "malformed_JWT_not_enough_parts", |
| 96 | + tokenContent: "invalid.jwt", |
| 97 | + wantErr: errJWTValidation, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "malformed_JWT_invalid_base64", |
| 101 | + tokenContent: "header.invalid_base64!@#.signature", |
| 102 | + wantErr: errJWTValidation, |
| 103 | + }, |
| 104 | + { |
| 105 | + name: "malformed_JWT_invalid_JSON", |
| 106 | + tokenContent: createInvalidJWT(t), |
| 107 | + wantErr: errJWTValidation, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + tokenFile := writeTempFile(t, "token", tt.tokenContent) |
| 114 | + |
| 115 | + reader := jwtFileReader{tokenFilePath: tokenFile} |
| 116 | + if _, _, err := reader.readToken(); err == nil { |
| 117 | + t.Fatal("ReadToken() expected error, got nil") |
| 118 | + } else if !errors.Is(err, tt.wantErr) { |
| 119 | + t.Fatalf("ReadToken() error = %v, want error %v", err, tt.wantErr) |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func (s) TestJWTFileReader_ReadToken_ValidToken(t *testing.T) { |
| 126 | + now := time.Now().Truncate(time.Second) |
| 127 | + tokenExp := now.Add(time.Hour) |
| 128 | + token := createTestJWT(t, tokenExp) |
| 129 | + tokenFile := writeTempFile(t, "token", token) |
| 130 | + |
| 131 | + reader := jwtFileReader{tokenFilePath: tokenFile} |
| 132 | + readToken, expiry, err := reader.readToken() |
| 133 | + if err != nil { |
| 134 | + t.Fatalf("ReadToken() unexpected error: %v", err) |
| 135 | + } |
| 136 | + |
| 137 | + if readToken != token { |
| 138 | + t.Errorf("ReadToken() token = %q, want %q", readToken, token) |
| 139 | + } |
| 140 | + |
| 141 | + if !expiry.Equal(tokenExp) { |
| 142 | + t.Errorf("ReadToken() expiry = %v, want %v", expiry, tokenExp) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// createInvalidJWT creates a JWT with invalid JSON in the payload. |
| 147 | +func createInvalidJWT(t *testing.T) string { |
| 148 | + t.Helper() |
| 149 | + |
| 150 | + header := map[string]any{ |
| 151 | + "typ": "JWT", |
| 152 | + "alg": "HS256", |
| 153 | + } |
| 154 | + |
| 155 | + headerBytes, err := json.Marshal(header) |
| 156 | + if err != nil { |
| 157 | + t.Fatalf("Failed to marshal header: %v", err) |
| 158 | + } |
| 159 | + |
| 160 | + headerB64 := base64.URLEncoding.EncodeToString(headerBytes) |
| 161 | + headerB64 = strings.TrimRight(headerB64, "=") |
| 162 | + |
| 163 | + // Create invalid JSON payload |
| 164 | + invalidJSON := "invalid json content" |
| 165 | + payloadB64 := base64.URLEncoding.EncodeToString([]byte(invalidJSON)) |
| 166 | + payloadB64 = strings.TrimRight(payloadB64, "=") |
| 167 | + |
| 168 | + signature := base64.URLEncoding.EncodeToString([]byte("fake_signature")) |
| 169 | + signature = strings.TrimRight(signature, "=") |
| 170 | + |
| 171 | + return fmt.Sprintf("%s.%s.%s", headerB64, payloadB64, signature) |
| 172 | +} |
0 commit comments