-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcocoapods_test.go
More file actions
130 lines (114 loc) · 5.58 KB
/
cocoapods_test.go
File metadata and controls
130 lines (114 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package cocoapods
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/tests"
"github.com/jfrog/jfrog-cli-security/sca/bom/buildinfo/technologies"
"github.com/jfrog/jfrog-cli-security/utils/techutils"
"github.com/owenrumney/go-sarif/v3/pkg/report/v210/sarif"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/stretchr/testify/assert"
)
func TestBuildCocoapodsDependencyList(t *testing.T) {
// Create and change directory to test workspace
_, cleanUp := technologies.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods", "cocoapods-project"))
defer cleanUp()
// Run getModulesDependencyTrees
server := &config.ServerDetails{
Url: "https://api.cocoapods.here",
ArtifactoryUrl: "https://api.cocoapods.here/artifactory",
User: "user",
AccessToken: "sdsdccs2232",
}
currentDir, err := coreutils.GetWorkingDirectory()
assert.NoError(t, err)
packageName := filepath.Base(currentDir)
packageInfo := fmt.Sprintf("%s:%s", packageName, VersionForMainModule)
expectedUniqueDeps := []string{
techutils.Cocoapods.GetXrayPackageTypeId() + "AppAuth:1.7.5",
techutils.Cocoapods.GetXrayPackageTypeId() + "AppAuth/Core:1.7.5",
techutils.Cocoapods.GetXrayPackageTypeId() + "AppAuth/ExternalUserAgent:1.7.5",
techutils.Cocoapods.GetXrayPackageTypeId() + "GoogleSignIn:6.2.4",
techutils.Cocoapods.GetXrayPackageTypeId() + "GTMAppAuth:1.3.1",
techutils.Cocoapods.GetXrayPackageTypeId() + "GTMSessionFetcher/Core:2.3.0",
techutils.Cocoapods.GetXrayPackageTypeId() + "nanopb:0.3.0",
techutils.Cocoapods.GetXrayPackageTypeId() + packageInfo,
}
auditBasicParams := technologies.BuildInfoBomGeneratorParams{ServerDetails: server}
rootNode, uniqueDeps, err := BuildDependencyTree(auditBasicParams)
assert.NoError(t, err)
assert.ElementsMatch(t, uniqueDeps, expectedUniqueDeps, "First is actual, Second is Expected")
assert.NotEmpty(t, rootNode)
assert.Equal(t, rootNode[0].Id, techutils.Cocoapods.GetXrayPackageTypeId()+packageInfo)
assert.Len(t, rootNode[0].Nodes, 2)
child1 := tests.GetAndAssertNode(t, rootNode[0].Nodes, "nanopb:0.3.0")
assert.Len(t, child1.Nodes, 0)
child2 := tests.GetAndAssertNode(t, rootNode[0].Nodes, "GoogleSignIn:6.2.4")
assert.Len(t, child2.Nodes, 3)
}
func TestGetTechDependencyLocation(t *testing.T) {
_, cleanUp := technologies.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods", "cocoapods-project"))
defer cleanUp()
currentDir, err := coreutils.GetWorkingDirectory()
assert.NoError(t, err)
locations, err := GetTechDependencyLocation("GoogleSignIn", "6.2.4", filepath.Join(currentDir, "Podfile"))
assert.NoError(t, err)
assert.Len(t, locations, 1)
assert.Equal(t, *locations[0].PhysicalLocation.Region.StartLine, 4)
assert.Equal(t, *locations[0].PhysicalLocation.Region.StartColumn, 4)
assert.Equal(t, *locations[0].PhysicalLocation.Region.EndLine, 5)
assert.Equal(t, *locations[0].PhysicalLocation.Region.EndColumn, 30)
assert.Contains(t, *locations[0].PhysicalLocation.Region.Snippet.Text, "GoogleSignIn', '~> 6.2.4'")
}
func TestPodLineParse(t *testing.T) {
var podPositions []*sarif.Location
foundDependency, _, startLine, startCol := parsePodLine("pod 'GoogleSignIn', '~> 6.2.4'", "GoogleSignIn", "6.2.4", "test", 0, 0, 0, 0, []string{"pod 'GoogleSignIn', '~> 6.2.4'"}, false, &podPositions)
assert.Equal(t, foundDependency, false)
assert.Equal(t, startLine, 0)
assert.Equal(t, startCol, 5)
}
func TestPodLineParseFoundOnlyDependencyName(t *testing.T) {
var podPositions []*sarif.Location
foundDependency, _, startLine, startCol := parsePodLine("pod 'GoogleSignIn', '~> 6.2.3'", "GoogleSignIn", "6.2.4", "test", 0, 0, 0, 0, []string{"pod 'GoogleSignIn', '~> 6.2.3'"}, false, &podPositions)
assert.Equal(t, foundDependency, true)
assert.Equal(t, startLine, 0)
assert.Equal(t, startCol, 5)
}
func TestFixTechDependencySingleLocation(t *testing.T) {
_, cleanUp := technologies.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods", "cocoapods-project"))
defer cleanUp()
currentDir, err := coreutils.GetWorkingDirectory()
assert.NoError(t, err)
err = FixTechDependency("GoogleSignIn", "6.2.4", "6.2.5", filepath.Join(currentDir, "Podfile"))
assert.NoError(t, err)
file, err := os.ReadFile(filepath.Join(currentDir, "Podfile"))
assert.NoError(t, err)
assert.Contains(t, string(file), "pod 'GoogleSignIn', '~> 6.2.5'")
}
func TestFixTechDependencyMultipleLocations(t *testing.T) {
_, cleanUp := technologies.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods", "cocoapods-project"))
defer cleanUp()
currentDir, err := coreutils.GetWorkingDirectory()
assert.NoError(t, err)
err = FixTechDependency("AppAuth", "1.7.5", "1.7.6", filepath.Join(currentDir, "Podfile"))
assert.NoError(t, err)
file, err := os.ReadFile(filepath.Join(currentDir, "Podfile"))
assert.NoError(t, err)
numAppearances := strings.Count(string(file), "pod 'AppAuth', '~> 1.7.6'")
assert.Equal(t, numAppearances, 2)
}
func TestFixTechDependencyNoLocations(t *testing.T) {
_, cleanUp := technologies.CreateTestWorkspace(t, filepath.Join("projects", "package-managers", "cocoapods", "cocoapods-project"))
defer cleanUp()
currentDir, err := coreutils.GetWorkingDirectory()
assert.NoError(t, err)
err = FixTechDependency("GoogleSignIn", "1.8.2", "1.8.3", filepath.Join(currentDir, "Podfile"))
assert.NoError(t, err)
file, err := os.ReadFile(filepath.Join(currentDir, "Podfile"))
assert.NoError(t, err)
assert.Contains(t, string(file), "pod 'GoogleSignIn', '~> 6.2.4'")
}