|
| 1 | +package ndncert_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/named-data/ndnd/std/security/ndncert" |
| 7 | + "github.com/named-data/ndnd/std/types/optional" |
| 8 | +) |
| 9 | + |
| 10 | +func TestChallengeDns_Name(t *testing.T) { |
| 11 | + challenge := &ndncert.ChallengeDns{} |
| 12 | + if challenge.Name() != "dns" { |
| 13 | + t.Errorf("Expected challenge name 'dns', got '%s'", challenge.Name()) |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +func TestChallengeDns_InitialRequest(t *testing.T) { |
| 18 | + domainCalled := false |
| 19 | + expectedDomain := "example.com" |
| 20 | + |
| 21 | + challenge := &ndncert.ChallengeDns{ |
| 22 | + DomainCallback: func(status string) string { |
| 23 | + domainCalled = true |
| 24 | + return expectedDomain |
| 25 | + }, |
| 26 | + ConfirmationCallback: func(recordName, expectedValue, status string) string { |
| 27 | + return "ready" |
| 28 | + }, |
| 29 | + } |
| 30 | + |
| 31 | + params, err := challenge.Request(nil, optional.Optional[string]{}) |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Expected no error, got: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + if !domainCalled { |
| 37 | + t.Error("Expected domain callback to be called") |
| 38 | + } |
| 39 | + |
| 40 | + if string(params["domain"]) != expectedDomain { |
| 41 | + t.Errorf("Expected domain parameter '%s', got '%s'", expectedDomain, string(params["domain"])) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func TestChallengeDns_InvalidDomain(t *testing.T) { |
| 46 | + challenge := &ndncert.ChallengeDns{ |
| 47 | + DomainCallback: func(status string) string { |
| 48 | + return "invalid..domain" // Invalid domain format |
| 49 | + }, |
| 50 | + ConfirmationCallback: func(recordName, expectedValue, status string) string { |
| 51 | + return "ready" |
| 52 | + }, |
| 53 | + } |
| 54 | + |
| 55 | + _, err := challenge.Request(nil, optional.Optional[string]{}) |
| 56 | + if err == nil { |
| 57 | + t.Error("Expected error for invalid domain, got none") |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestChallengeDns_NeedRecordStatus(t *testing.T) { |
| 62 | + confirmationCalled := false |
| 63 | + expectedRecordName := "_ndncert-challenge.example.com" |
| 64 | + expectedValue := "test-token-hash" |
| 65 | + |
| 66 | + challenge := &ndncert.ChallengeDns{ |
| 67 | + DomainCallback: func(status string) string { |
| 68 | + return "example.com" |
| 69 | + }, |
| 70 | + ConfirmationCallback: func(recordName, expValue, status string) string { |
| 71 | + confirmationCalled = true |
| 72 | + if recordName != expectedRecordName { |
| 73 | + t.Errorf("Expected record name '%s', got '%s'", expectedRecordName, recordName) |
| 74 | + } |
| 75 | + if expValue != expectedValue { |
| 76 | + t.Errorf("Expected value '%s', got '%s'", expectedValue, expValue) |
| 77 | + } |
| 78 | + if status != "need-record" { |
| 79 | + t.Errorf("Expected status 'need-record', got '%s'", status) |
| 80 | + } |
| 81 | + return "ready" |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + // Simulate input parameters from CA response |
| 86 | + input := ndncert.ParamMap{ |
| 87 | + "record-name": []byte(expectedRecordName), |
| 88 | + "expected-value": []byte(expectedValue), |
| 89 | + } |
| 90 | + |
| 91 | + status := optional.Some("need-record") |
| 92 | + params, err := challenge.Request(input, status) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("Expected no error, got: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + if !confirmationCalled { |
| 98 | + t.Error("Expected confirmation callback to be called") |
| 99 | + } |
| 100 | + |
| 101 | + if string(params["confirmation"]) != "ready" { |
| 102 | + t.Errorf("Expected confirmation parameter 'ready', got '%s'", string(params["confirmation"])) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestChallengeDns_WrongRecordStatus(t *testing.T) { |
| 107 | + challenge := &ndncert.ChallengeDns{ |
| 108 | + DomainCallback: func(status string) string { |
| 109 | + return "example.com" |
| 110 | + }, |
| 111 | + ConfirmationCallback: func(recordName, expectedValue, status string) string { |
| 112 | + if status != "wrong-record" { |
| 113 | + t.Errorf("Expected status 'wrong-record', got '%s'", status) |
| 114 | + } |
| 115 | + return "ready" |
| 116 | + }, |
| 117 | + } |
| 118 | + |
| 119 | + input := ndncert.ParamMap{ |
| 120 | + "record-name": []byte("_ndncert-challenge.example.com"), |
| 121 | + "expected-value": []byte("test-token-hash"), |
| 122 | + } |
| 123 | + |
| 124 | + status := optional.Some("wrong-record") |
| 125 | + params, err := challenge.Request(input, status) |
| 126 | + if err != nil { |
| 127 | + t.Fatalf("Expected no error, got: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + if string(params["confirmation"]) != "ready" { |
| 131 | + t.Errorf("Expected confirmation parameter 'ready', got '%s'", string(params["confirmation"])) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func TestChallengeDns_ReadyForValidationStatus(t *testing.T) { |
| 136 | + challenge := &ndncert.ChallengeDns{ |
| 137 | + DomainCallback: func(status string) string { |
| 138 | + return "example.com" |
| 139 | + }, |
| 140 | + ConfirmationCallback: func(recordName, expectedValue, status string) string { |
| 141 | + return "ready" |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + input := ndncert.ParamMap{} |
| 146 | + status := optional.Some("ready-for-validation") |
| 147 | + params, err := challenge.Request(input, status) |
| 148 | + if err != nil { |
| 149 | + t.Fatalf("Expected no error, got: %v", err) |
| 150 | + } |
| 151 | + |
| 152 | + if string(params["verify"]) != "now" { |
| 153 | + t.Errorf("Expected verify parameter 'now', got '%s'", string(params["verify"])) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +func TestChallengeDns_UnknownStatus(t *testing.T) { |
| 158 | + challenge := &ndncert.ChallengeDns{ |
| 159 | + DomainCallback: func(status string) string { |
| 160 | + return "example.com" |
| 161 | + }, |
| 162 | + ConfirmationCallback: func(recordName, expectedValue, status string) string { |
| 163 | + return "ready" |
| 164 | + }, |
| 165 | + } |
| 166 | + |
| 167 | + input := ndncert.ParamMap{} |
| 168 | + status := optional.Some("unknown-status") |
| 169 | + _, err := challenge.Request(input, status) |
| 170 | + if err == nil { |
| 171 | + t.Error("Expected error for unknown status, got none") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +func TestChallengeDns_NotConfigured(t *testing.T) { |
| 176 | + // Test with missing domain callback |
| 177 | + challenge := &ndncert.ChallengeDns{ |
| 178 | + ConfirmationCallback: func(recordName, expectedValue, status string) string { |
| 179 | + return "ready" |
| 180 | + }, |
| 181 | + } |
| 182 | + |
| 183 | + _, err := challenge.Request(nil, optional.Optional[string]{}) |
| 184 | + if err == nil { |
| 185 | + t.Error("Expected error for missing domain callback, got none") |
| 186 | + } |
| 187 | + |
| 188 | + // Test with missing confirmation callback |
| 189 | + challenge2 := &ndncert.ChallengeDns{ |
| 190 | + DomainCallback: func(status string) string { |
| 191 | + return "example.com" |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + _, err = challenge2.Request(nil, optional.Optional[string]{}) |
| 196 | + if err == nil { |
| 197 | + t.Error("Expected error for missing confirmation callback, got none") |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// Test domain validation helper functions (if we need to expose them for testing) |
| 202 | +func TestIsValidDomainName(t *testing.T) { |
| 203 | + // Note: This test assumes isValidDomainName is exported |
| 204 | + // If not exported, we can test through the challenge Request method |
| 205 | + testCases := []struct { |
| 206 | + domain string |
| 207 | + valid bool |
| 208 | + }{ |
| 209 | + {"example.com", true}, |
| 210 | + {"sub.example.com", true}, |
| 211 | + {"test-domain.example.org", true}, |
| 212 | + {"a.b", true}, |
| 213 | + {"", false}, |
| 214 | + {"-example.com", false}, |
| 215 | + {"example-.com", false}, |
| 216 | + {"example..com", false}, |
| 217 | + {".example.com", false}, |
| 218 | + {"example.com.", false}, |
| 219 | + } |
| 220 | + |
| 221 | + for _, tc := range testCases { |
| 222 | + challenge := &ndncert.ChallengeDns{ |
| 223 | + DomainCallback: func(status string) string { |
| 224 | + return tc.domain |
| 225 | + }, |
| 226 | + ConfirmationCallback: func(recordName, expectedValue, status string) string { |
| 227 | + return "ready" |
| 228 | + }, |
| 229 | + } |
| 230 | + |
| 231 | + _, err := challenge.Request(nil, optional.Optional[string]{}) |
| 232 | + |
| 233 | + if tc.valid && err != nil { |
| 234 | + t.Errorf("Expected domain '%s' to be valid, got error: %v", tc.domain, err) |
| 235 | + } |
| 236 | + if !tc.valid && err == nil { |
| 237 | + t.Errorf("Expected domain '%s' to be invalid, got no error", tc.domain) |
| 238 | + } |
| 239 | + } |
| 240 | +} |
0 commit comments