Skip to content

Commit 9075d3b

Browse files
Adding unit testcases for the change
1 parent 80e945d commit 9075d3b

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

internal/services/realtimeengine/ossrealtime/oss-realtime_test.go

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,3 +388,145 @@ func TestOssRealtimeScan_CsprojFile_ReturnsLocations(t *testing.T) {
388388
"Location %d endIndex should be %d", i, expected.endIndex)
389389
}
390390
}
391+
392+
// Tests for validateSupportedManifestFile function
393+
func TestValidateSupportedManifestFile_ValidExtensions(t *testing.T) {
394+
tests := []struct {
395+
name string
396+
filePath string
397+
isValid bool
398+
}{
399+
{
400+
name: "CsprojExtension",
401+
filePath: "project.csproj",
402+
isValid: true,
403+
},
404+
{
405+
name: "SbtExtension",
406+
filePath: "build.sbt",
407+
isValid: true,
408+
},
409+
{
410+
name: "CsprojWithPath",
411+
filePath: "/path/to/project.csproj",
412+
isValid: true,
413+
},
414+
{
415+
name: "SbtWithPath",
416+
filePath: "src/build.sbt",
417+
isValid: true,
418+
},
419+
}
420+
421+
for _, tt := range tests {
422+
t.Run(tt.name, func(t *testing.T) {
423+
err := validateSupportedManifestFile(tt.filePath)
424+
if tt.isValid {
425+
assert.Nil(t, err, "Should accept %s file", tt.filePath)
426+
} else {
427+
assert.NotNil(t, err, "Should reject %s file", tt.filePath)
428+
}
429+
})
430+
}
431+
}
432+
433+
func TestValidateSupportedManifestFile_ValidFilenames(t *testing.T) {
434+
tests := []struct {
435+
name string
436+
filePath string
437+
}{
438+
{name: "PomXml", filePath: "pom.xml"},
439+
{name: "PackageJson", filePath: "package.json"},
440+
{name: "GoMod", filePath: "go.mod"},
441+
{name: "BuildGradle", filePath: "build.gradle"},
442+
{name: "BuildGradleKts", filePath: "build.gradle.kts"},
443+
{name: "LibsVersionsToml", filePath: "libs.versions.toml"},
444+
{name: "SetupCfg", filePath: "setup.cfg"},
445+
{name: "SetupPy", filePath: "setup.py"},
446+
{name: "PyprojectToml", filePath: "pyproject.toml"},
447+
{name: "PackagesConfig", filePath: "packages.config"},
448+
{name: "DirectoryPackagesProps", filePath: "Directory.Packages.props"},
449+
{name: "PomXmlWithPath", filePath: "/path/to/pom.xml"},
450+
{name: "PackageJsonWithPath", filePath: "src/package.json"},
451+
{name: "GoModWithPath", filePath: "project/go.mod"},
452+
}
453+
454+
for _, tt := range tests {
455+
t.Run(tt.name, func(t *testing.T) {
456+
err := validateSupportedManifestFile(tt.filePath)
457+
assert.Nil(t, err, "Should accept %s file", tt.filePath)
458+
})
459+
}
460+
}
461+
462+
func TestValidateSupportedManifestFile_ValidTxtFilesWithPrefixes(t *testing.T) {
463+
tests := []struct {
464+
name string
465+
filePath string
466+
}{
467+
{name: "RequirementsTxt", filePath: "requirements.txt"},
468+
{name: "RequirementsDevTxt", filePath: "requirements-dev.txt"},
469+
{name: "PackagesTxt", filePath: "packages.txt"},
470+
{name: "ConstraintsTxt", filePath: "constraints.txt"},
471+
{name: "RequirementsWithPath", filePath: "/path/to/requirements.txt"},
472+
{name: "PackagesWithPath", filePath: "config/packages.txt"},
473+
{name: "ConstraintsWithPath", filePath: "src/constraints.txt"},
474+
}
475+
476+
for _, tt := range tests {
477+
t.Run(tt.name, func(t *testing.T) {
478+
err := validateSupportedManifestFile(tt.filePath)
479+
assert.Nil(t, err, "Should accept %s file", tt.filePath)
480+
})
481+
}
482+
}
483+
484+
func TestValidateSupportedManifestFile_InvalidTxtFilesWithoutPrefixes(t *testing.T) {
485+
tests := []struct {
486+
name string
487+
filePath string
488+
}{
489+
{name: "UnrelatedTxt", filePath: "readme.txt"},
490+
{name: "DataTxt", filePath: "data.txt"},
491+
{name: "ConfigTxt", filePath: "config.txt"},
492+
{name: "InfoTxt", filePath: "info.txt"},
493+
{name: "UnrelatedTxtWithPath", filePath: "/path/to/random.txt"},
494+
}
495+
496+
for _, tt := range tests {
497+
t.Run(tt.name, func(t *testing.T) {
498+
err := validateSupportedManifestFile(tt.filePath)
499+
assert.NotNil(t, err, "Should reject %s file (missing prefix)", tt.filePath)
500+
// Verify error message indicates unsupported format
501+
assert.Contains(t, err.Error(), "OSS Realtime scanner doesn't currently support scanning")
502+
})
503+
}
504+
}
505+
506+
func TestValidateSupportedManifestFile_UnsupportedFormats(t *testing.T) {
507+
tests := []struct {
508+
name string
509+
filePath string
510+
}{
511+
{name: "RubyGemfile", filePath: "Gemfile"},
512+
{name: "PHPComposer", filePath: "composer.json"},
513+
{name: "RustCargo", filePath: "Cargo.toml"},
514+
{name: "PythonPipfile", filePath: "Pipfile"},
515+
{name: "JavaGradleProperties", filePath: "gradle.properties"},
516+
{name: "NodeNpmLock", filePath: "package-lock.json"},
517+
{name: "YarnLock", filePath: "yarn.lock"},
518+
{name: "UnsupportedExtension", filePath: "manifest.xml"},
519+
{name: "RandomFile", filePath: "random.txt"},
520+
{name: "NoPyproject", filePath: "pyproject.yaml"},
521+
{name: "PomWithDifferentName", filePath: "maven.xml"},
522+
}
523+
524+
for _, tt := range tests {
525+
t.Run(tt.name, func(t *testing.T) {
526+
err := validateSupportedManifestFile(tt.filePath)
527+
assert.NotNil(t, err, "Should reject %s file (unsupported format)", tt.filePath)
528+
// Verify error message indicates unsupported format
529+
assert.Contains(t, err.Error(), "OSS Realtime scanner doesn't currently support scanning")
530+
})
531+
}
532+
}

test/integration/oss-realtime_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ func TestOssRealtimeScan_PackageJsonFile_Success(t *testing.T) {
7171
defer deleteCacheFile()
7272
}
7373

74+
func TestOssRealtimeScan_UnsupportedManifestFormat_ReturnsError(t *testing.T) {
75+
// Test with unsupported file format - should return error (using a .py file which is not a supported manifest)
76+
args := []string{
77+
"scan", "oss-realtime",
78+
flag(commonParams.SourcesFlag), "data/python-vul-file.py",
79+
}
80+
err, _ := executeCommand(t, args...)
81+
// Should fail with error
82+
assert.NotNil(t, err, "Should fail with unsupported manifest format")
83+
// Error message should indicate unsupported format
84+
assert.Contains(t, err.Error(), "OSS Realtime scanner doesn't currently support scanning",
85+
"Error message should indicate that format is not supported")
86+
}
87+
7488
func validateCacheFileExist() bool {
7589
cacheFilePath := os.TempDir() + "/oss-realtime-cache.json"
7690
if _, err := os.Stat(cacheFilePath); os.IsNotExist(err) {

0 commit comments

Comments
 (0)