|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package environment |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "regexp" |
| 22 | + "testing" |
| 23 | + |
| 24 | + npb "github.com/google/tsunami-security-scanner/proto/go/network_go_proto" |
| 25 | + nspb "github.com/google/tsunami-security-scanner/proto/go/network_service_go_proto" |
| 26 | +) |
| 27 | + |
| 28 | +func TestEnvironment_InitializeFor(t *testing.T) { |
| 29 | + service := nspb.NetworkService_builder{ |
| 30 | + NetworkEndpoint: npb.NetworkEndpoint_builder{ |
| 31 | + Hostname: npb.Hostname_builder{Name: "example.com"}.Build(), |
| 32 | + Port: npb.Port_builder{PortNumber: 80}.Build(), |
| 33 | + IpAddress: npb.IpAddress_builder{ |
| 34 | + Address: "1.2.3.4", |
| 35 | + }.Build(), |
| 36 | + }.Build(), |
| 37 | + TransportProtocol: npb.TransportProtocol_TCP, |
| 38 | + SupportedHttpMethods: []string{"GET"}, |
| 39 | + }.Build() |
| 40 | + |
| 41 | + env := New() |
| 42 | + env.InitializeFor(context.Background(), service) |
| 43 | + |
| 44 | + tests := []struct { |
| 45 | + key string |
| 46 | + wantVal string |
| 47 | + wantExpr *regexp.Regexp |
| 48 | + }{ |
| 49 | + {key: "T_NS_HOSTNAME", wantVal: "example.com"}, |
| 50 | + {key: "T_NS_PORT", wantVal: "80"}, |
| 51 | + {key: "T_NS_IP", wantVal: "1.2.3.4"}, |
| 52 | + {key: "T_NS_PROTOCOL", wantVal: "TCP"}, |
| 53 | + {key: "T_NS_BASEURL", wantVal: "http://example.com:80"}, |
| 54 | + {key: "T_UTL_CURRENT_TIMESTAMP_MS", wantExpr: regexp.MustCompile(`^\d+$`)}, |
| 55 | + } |
| 56 | + |
| 57 | + for _, tc := range tests { |
| 58 | + got, ok := env.Get(tc.key) |
| 59 | + if !ok { |
| 60 | + t.Errorf("env.Get(%q) = _, false, want _, true", tc.key) |
| 61 | + continue |
| 62 | + } |
| 63 | + if tc.wantVal != "" && got != tc.wantVal { |
| 64 | + t.Errorf("env.Get(%q) = %q, want %q", tc.key, got, tc.wantVal) |
| 65 | + } |
| 66 | + if tc.wantExpr != nil && !tc.wantExpr.MatchString(got) { |
| 67 | + t.Errorf("env.Get(%q) = %q, doesn't match %v", tc.key, got, tc.wantExpr) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestEnvironment_Substitute(t *testing.T) { |
| 73 | + env := New() |
| 74 | + env.Set("var1", "val1") |
| 75 | + env.Set("var2", "val2") |
| 76 | + |
| 77 | + tests := []struct { |
| 78 | + name string |
| 79 | + template string |
| 80 | + want string |
| 81 | + }{ |
| 82 | + { |
| 83 | + name: "when_no_variables_returns_original", |
| 84 | + template: "hello world", |
| 85 | + want: "hello world", |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "when_single_variable_replaces_it", |
| 89 | + template: "hello {{ var1 }}", |
| 90 | + want: "hello val1", |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "when_multiple_variables_replaces_them", |
| 94 | + template: "{{ var1 }} and {{ var2 }}", |
| 95 | + want: "val1 and val2", |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "when_repeated_variable_replaces_all", |
| 99 | + template: "hello {{ var1 }} {{ var1 }}", |
| 100 | + want: "hello val1 val1", |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "when_variable_not_found_leaves_it", |
| 104 | + template: "hello {{ unknown }}", |
| 105 | + want: "hello {{ unknown }}", |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "when_malformed_variable_leaves_it", |
| 109 | + template: "hello {{var1}}", |
| 110 | + want: "hello {{var1}}", |
| 111 | + }, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tc := range tests { |
| 115 | + t.Run(tc.name, func(t *testing.T) { |
| 116 | + got := env.Substitute(context.Background(), tc.template) |
| 117 | + if got != tc.want { |
| 118 | + t.Errorf("Substitute(%q) = %q, want %q", tc.template, got, tc.want) |
| 119 | + } |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +func TestEnvironment_Extract(t *testing.T) { |
| 125 | + tests := []struct { |
| 126 | + name string |
| 127 | + content string |
| 128 | + pattern string |
| 129 | + varname string |
| 130 | + wantOk bool |
| 131 | + wantVars map[string]string |
| 132 | + }{ |
| 133 | + { |
| 134 | + name: "when_match_found_extracts_it", |
| 135 | + content: "token: abc-123", |
| 136 | + pattern: `token: ([a-z0-9-]+)`, |
| 137 | + varname: "token", |
| 138 | + wantOk: true, |
| 139 | + wantVars: map[string]string{ |
| 140 | + "token": "abc-123", |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + name: "when_no_match_found_returns_false", |
| 145 | + content: "hello world", |
| 146 | + pattern: `token: ([a-z0-9-]+)`, |
| 147 | + varname: "token", |
| 148 | + wantOk: false, |
| 149 | + wantVars: map[string]string{ |
| 150 | + "token": "", |
| 151 | + }, |
| 152 | + }, |
| 153 | + { |
| 154 | + name: "when_invalid_regexp_returns_false", |
| 155 | + content: "hello world", |
| 156 | + pattern: `(`, |
| 157 | + varname: "token", |
| 158 | + wantOk: false, |
| 159 | + wantVars: map[string]string{ |
| 160 | + "token": "", |
| 161 | + }, |
| 162 | + }, |
| 163 | + } |
| 164 | + |
| 165 | + for _, tc := range tests { |
| 166 | + t.Run(tc.name, func(t *testing.T) { |
| 167 | + env := New() |
| 168 | + gotOk := env.Extract(context.Background(), tc.content, tc.varname, tc.pattern) |
| 169 | + if gotOk != tc.wantOk { |
| 170 | + t.Errorf("Extract() = %v, want %v", gotOk, tc.wantOk) |
| 171 | + } |
| 172 | + |
| 173 | + for k, v := range tc.wantVars { |
| 174 | + val, ok := env.Get(k) |
| 175 | + if v == "" { |
| 176 | + if ok { |
| 177 | + t.Errorf("env.Get(%q) = %q, true, want _, false", k, val) |
| 178 | + } |
| 179 | + } else { |
| 180 | + if !ok || val != v { |
| 181 | + t.Errorf("env.Get(%q) = %q, %v, want %q, true", k, val, ok, v) |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + }) |
| 186 | + } |
| 187 | +} |
| 188 | + |
| 189 | +func TestEnvironment_SetGet(t *testing.T) { |
| 190 | + env := New() |
| 191 | + env.Set("key", "value") |
| 192 | + val, ok := env.Get("key") |
| 193 | + if !ok || val != "value" { |
| 194 | + t.Errorf("Get() = %q, %v, want %q, true", val, ok, "value") |
| 195 | + } |
| 196 | + |
| 197 | + _, ok = env.Get("unknown") |
| 198 | + if ok { |
| 199 | + t.Errorf("Get(unknown) = _, true, want _, false") |
| 200 | + } |
| 201 | +} |
0 commit comments