Path: blob/dev/pkg/catalog/config/template_test.go
4538 views
package config12import (3"os"4"path/filepath"5"testing"67osutils "github.com/projectdiscovery/utils/os"8"github.com/stretchr/testify/require"9)1011func toAbs(p string) string {12if osutils.IsWindows() {13// Infer the drive letter from the Windows system path14// SystemRoot or WINDIR typically points to something like "C:\Windows"15systemRoot := os.Getenv("SystemRoot")16if systemRoot == "" {17systemRoot = os.Getenv("WINDIR")18}19// Extract volume name (e.g., "C:") from the system path20volumeName := filepath.VolumeName(systemRoot)21if volumeName == "" {22// Fallback to C: if we can't determine the volume23volumeName = "C:"24}25return filepath.FromSlash(volumeName + p)26}27return filepath.FromSlash(p)28}2930func TestIsTemplate(t *testing.T) {31tests := []struct {32name string33fpath string34rootDir string35want bool36}{37{38name: "valid template",39fpath: "dns/cname.yaml",40rootDir: "",41want: true,42},43{44name: "excluded directory relative",45fpath: "helpers/data.txt",46rootDir: "",47want: false,48},49{50name: "excluded directory .git",51fpath: ".git/config",52rootDir: "",53want: false,54},55{56name: "absolute path with excluded parent dir (bug fix)",57fpath: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates/dns/cname.yaml"),58rootDir: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates"),59want: true,60},61{62name: "absolute path with excluded dir inside root",63fpath: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates/helpers/data.txt"),64rootDir: toAbs("/path/to/somewhere/that/has/helpers/dir/nuclei/nuclei-templates"),65want: false,66},67{68name: "absolute path without root (skip check)",69fpath: toAbs("/opt/helpers/foo.yaml"),70rootDir: "",71want: true,72},73{74name: "valid template with different extension",75fpath: "dns/cname.json",76rootDir: "",77want: true,78},79{80name: "invalid extension",81fpath: "dns/cname.txt",82rootDir: "",83want: false,84},85{86name: "excluded config file",87fpath: "cves.json",88rootDir: "",89want: false,90},91}9293for _, tt := range tests {94t.Run(tt.name, func(t *testing.T) {95got := IsTemplateWithRoot(tt.fpath, tt.rootDir)96require.Equal(t, tt.want, got, "IsTemplateWithRoot(%q, %q)", tt.fpath, tt.rootDir)97})98}99}100101102