-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathobfuscate_test.go
76 lines (60 loc) · 2.32 KB
/
obfuscate_test.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package sqlserverreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/sqlserverreceiver"
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestObfuscateSQL(t *testing.T) {
expected, err := os.ReadFile(filepath.Join("testdata", "expectedSQL.sql"))
assert.NoError(t, err)
expectedSQL := strings.TrimSpace(string(expected))
input, err := os.ReadFile(filepath.Join("testdata", "inputSQL.sql"))
assert.NoError(t, err)
result, err := obfuscateSQL(string(input))
assert.NoError(t, err)
assert.Equal(t, expectedSQL, result)
}
func TestObfuscateQueryPlan(t *testing.T) {
expected, err := os.ReadFile(filepath.Join("testdata", "expectedQueryPlan.xml"))
assert.NoError(t, err)
expectedQueryPlan := strings.TrimSpace(string(expected))
input, err := os.ReadFile(filepath.Join("testdata", "inputQueryPlan.xml"))
assert.NoError(t, err)
result, err := obfuscateXMLPlan(string(input))
assert.NoError(t, err)
assert.Equal(t, expectedQueryPlan, result)
}
func TestInvalidQueryPlans(t *testing.T) {
plan := `<ShowPlanXml</ShowPlanXML>`
result, err := obfuscateXMLPlan(plan)
assert.Empty(t, result)
assert.Error(t, err)
plan = `<ShowPlanXML></ShowPlanXML`
result, err = obfuscateXMLPlan(plan)
assert.Empty(t, result)
assert.Error(t, err)
plan = `<ShowPlanXML></ShowPlan>`
result, err = obfuscateXMLPlan(plan)
assert.Empty(t, result)
assert.Error(t, err)
// obfuscate failure, but no error
plan = `<ShowPlanXML StatementText="[msdb].[dbo].[sysjobhistory].[run_duration] as [sjh].[run_duration]/(10000)*(3600)+[msdb].[dbo].[sysjobhistory].[run_duration] as [sjh].[run_duration]%(10000)/(100)*(60)+[msdb].[dbo].[sysjobhistory].[run_duration] as [sjh].[run_duration]%(100)"></ShowPlanXML>`
result, err = obfuscateXMLPlan(plan)
assert.Equal(t, plan, result)
assert.NoError(t, err)
}
func TestValidQueryPlans(t *testing.T) {
plan := `<ShowPlanXML value="abc"></ShowPlanXML>`
_, err := obfuscateXMLPlan(plan)
assert.NoError(t, err)
plan = `<ShowPlanXML StatementText=""></ShowPlanXML>`
_, err = obfuscateXMLPlan(plan)
assert.NoError(t, err)
plan = `<ShowPlanXML StatementText="SELECT * FROM table"><!-- comment --></ShowPlanXML>`
_, err = obfuscateXMLPlan(plan)
assert.NoError(t, err)
}