Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/installer/template_test.go
2070 views
1
package installer
2
3
import (
4
"os"
5
"path/filepath"
6
"strings"
7
"testing"
8
9
"github.com/projectdiscovery/nuclei/v3/pkg/catalog/config"
10
"github.com/stretchr/testify/require"
11
)
12
13
func TestTemplateInstallation(t *testing.T) {
14
// test that the templates are installed correctly
15
// along with necessary changes that are made
16
HideProgressBar = true
17
18
tm := &TemplateManager{}
19
dir, err := os.MkdirTemp("", "nuclei-templates-*")
20
require.Nil(t, err)
21
cfgdir, err := os.MkdirTemp("", "nuclei-config-*")
22
require.Nil(t, err)
23
defer func() {
24
_ = os.RemoveAll(dir)
25
_ = os.RemoveAll(cfgdir)
26
}()
27
28
// set the config directory to a temporary directory
29
config.DefaultConfig.SetConfigDir(cfgdir)
30
// set the templates directory to a temporary directory
31
templatesTempDir := filepath.Join(dir, "templates")
32
config.DefaultConfig.SetTemplatesDir(templatesTempDir)
33
34
err = tm.FreshInstallIfNotExists()
35
if err != nil {
36
if strings.Contains(err.Error(), "rate limit") {
37
t.Skip("Skipping test due to github rate limit")
38
}
39
require.Nil(t, err)
40
}
41
42
// we should switch to more fine granular tests for template
43
// integrity, but for now, we just check that the templates are installed
44
counter := 0
45
err = filepath.Walk(templatesTempDir, func(path string, info os.FileInfo, err error) error {
46
if err != nil {
47
return err
48
}
49
if !info.IsDir() {
50
counter++
51
}
52
return nil
53
})
54
require.Nil(t, err)
55
56
// we should have at least 1000 templates
57
require.Greater(t, counter, 1000)
58
// every time we install templates, it should override the ignore file with latest one
59
require.FileExists(t, config.DefaultConfig.GetIgnoreFilePath())
60
t.Logf("Installed %d templates", counter)
61
}
62
63
func TestIsOutdatedVersion(t *testing.T) {
64
testCases := []struct {
65
current string
66
latest string
67
expected bool
68
desc string
69
}{
70
// Test the empty latest version case (main bug fix)
71
{"v10.2.7", "", false, "Empty latest version should not trigger update"},
72
73
// Test same versions
74
{"v10.2.7", "v10.2.7", false, "Same versions should not trigger update"},
75
76
// Test outdated version
77
{"v10.2.6", "v10.2.7", true, "Older version should trigger update"},
78
79
// Test newer current version (edge case)
80
{"v10.2.8", "v10.2.7", false, "Newer current version should not trigger update"},
81
82
// Test dev versions
83
{"v10.2.7-dev", "v10.2.7", false, "Dev version matching release should not trigger update"},
84
{"v10.2.6-dev", "v10.2.7", true, "Outdated dev version should trigger update"},
85
86
// Test invalid semver fallback
87
{"invalid-version", "v10.2.7", true, "Invalid current version should trigger update (fallback)"},
88
{"v10.2.7", "invalid-version", true, "Invalid latest version should trigger update (fallback)"},
89
{"same-invalid", "same-invalid", false, "Same invalid versions should not trigger update (fallback)"},
90
}
91
92
for _, tc := range testCases {
93
t.Run(tc.desc, func(t *testing.T) {
94
result := config.IsOutdatedVersion(tc.current, tc.latest)
95
require.Equal(t, tc.expected, result,
96
"IsOutdatedVersion(%q, %q) = %t, expected %t",
97
tc.current, tc.latest, result, tc.expected)
98
})
99
}
100
}
101
102