|
| 1 | +// This file is part of the happyDeliver (R) project. |
| 2 | +// Copyright (c) 2025 happyDomain |
| 3 | +// Authors: Pierre-Olivier Mercier, et al. |
| 4 | +// |
| 5 | +// This program is offered under a commercial and under the AGPL license. |
| 6 | +// For commercial licensing, contact us at <contact@happydomain.org>. |
| 7 | +// |
| 8 | +// For AGPL licensing: |
| 9 | +// This program is free software: you can redistribute it and/or modify |
| 10 | +// it under the terms of the GNU Affero General Public License as published by |
| 11 | +// the Free Software Foundation, either version 3 of the License, or |
| 12 | +// (at your option) any later version. |
| 13 | +// |
| 14 | +// This program is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +// GNU Affero General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU Affero General Public License |
| 20 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +package analyzer |
| 23 | + |
| 24 | +import ( |
| 25 | + "context" |
| 26 | + "testing" |
| 27 | +) |
| 28 | + |
| 29 | +func TestIsDNSSECEnabled(t *testing.T) { |
| 30 | + resolver := NewStandardDNSResolver() |
| 31 | + ctx := context.Background() |
| 32 | + |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + domain string |
| 36 | + expectDNSSEC bool |
| 37 | + }{ |
| 38 | + { |
| 39 | + name: "ietf.org has DNSSEC", |
| 40 | + domain: "ietf.org", |
| 41 | + expectDNSSEC: true, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "google.com doesn't have DNSSEC", |
| 45 | + domain: "google.com", |
| 46 | + expectDNSSEC: false, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + for _, tt := range tests { |
| 51 | + t.Run(tt.name, func(t *testing.T) { |
| 52 | + enabled, err := resolver.IsDNSSECEnabled(ctx, tt.domain) |
| 53 | + if err != nil { |
| 54 | + t.Errorf("IsDNSSECEnabled() error = %v", err) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + if enabled != tt.expectDNSSEC { |
| 59 | + t.Errorf("IsDNSSECEnabled() for %s = %v, want %v", tt.domain, enabled, tt.expectDNSSEC) |
| 60 | + } else { |
| 61 | + // Log the result even if we're not validating |
| 62 | + if enabled { |
| 63 | + t.Logf("%s: DNSSEC is enabled ✅", tt.domain) |
| 64 | + } else { |
| 65 | + t.Logf("%s: DNSSEC is NOT enabled ⚠️", tt.domain) |
| 66 | + } |
| 67 | + } |
| 68 | + }) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestIsDNSSECEnabled_NonExistentDomain(t *testing.T) { |
| 73 | + resolver := NewStandardDNSResolver() |
| 74 | + ctx := context.Background() |
| 75 | + |
| 76 | + // Test with a domain that doesn't exist |
| 77 | + enabled, err := resolver.IsDNSSECEnabled(ctx, "this-domain-definitely-does-not-exist-12345.com") |
| 78 | + if err != nil { |
| 79 | + // Error is acceptable for non-existent domains |
| 80 | + t.Logf("Non-existent domain returned error (expected): %v", err) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + // If no error, DNSSEC should be disabled |
| 85 | + if enabled { |
| 86 | + t.Error("IsDNSSECEnabled() for non-existent domain should return false") |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestIsDNSSECEnabled_WithTrailingDot(t *testing.T) { |
| 91 | + resolver := NewStandardDNSResolver() |
| 92 | + ctx := context.Background() |
| 93 | + |
| 94 | + // Test that both formats work |
| 95 | + domain1 := "cloudflare.com" |
| 96 | + domain2 := "cloudflare.com." |
| 97 | + |
| 98 | + enabled1, err1 := resolver.IsDNSSECEnabled(ctx, domain1) |
| 99 | + if err1 != nil { |
| 100 | + t.Errorf("IsDNSSECEnabled() without trailing dot error = %v", err1) |
| 101 | + } |
| 102 | + |
| 103 | + enabled2, err2 := resolver.IsDNSSECEnabled(ctx, domain2) |
| 104 | + if err2 != nil { |
| 105 | + t.Errorf("IsDNSSECEnabled() with trailing dot error = %v", err2) |
| 106 | + } |
| 107 | + |
| 108 | + if enabled1 != enabled2 { |
| 109 | + t.Errorf("IsDNSSECEnabled() results differ: without dot = %v, with dot = %v", enabled1, enabled2) |
| 110 | + } |
| 111 | +} |
0 commit comments