github.com/lulzWill/go-agent@v2.1.2+incompatible/internal/security_policies_test.go (about) 1 package internal 2 3 import ( 4 "encoding/json" 5 "testing" 6 ) 7 8 func testBool(t *testing.T, name string, expected, got bool) { 9 if expected != got { 10 t.Errorf("%v: expected=%v got=%v", name, expected, got) 11 } 12 } 13 14 func TestSecurityPoliciesPresent(t *testing.T) { 15 inputJSON := []byte(`{ 16 "record_sql": { "enabled": false, "required": false }, 17 "attributes_include": { "enabled": false, "required": false }, 18 "allow_raw_exception_messages": { "enabled": false, "required": false }, 19 "custom_events": { "enabled": false, "required": false }, 20 "custom_parameters": { "enabled": false, "required": false }, 21 "custom_instrumentation_editor": { "enabled": false, "required": false }, 22 "message_parameters": { "enabled": false, "required": false }, 23 "job_arguments": { "enabled": false, "required": false } 24 }`) 25 var policies SecurityPolicies 26 err := json.Unmarshal(inputJSON, &policies) 27 if nil != err { 28 t.Fatal(err) 29 } 30 connectJSON, err := json.Marshal(policies) 31 if nil != err { 32 t.Fatal(err) 33 } 34 expectJSON := CompactJSONString(`{ 35 "record_sql": { "enabled": false }, 36 "attributes_include": { "enabled": false }, 37 "allow_raw_exception_messages": { "enabled": false }, 38 "custom_events": { "enabled": false }, 39 "custom_parameters": { "enabled": false } 40 }`) 41 if string(connectJSON) != expectJSON { 42 t.Error(string(connectJSON), expectJSON) 43 } 44 testBool(t, "PointerIfPopulated", true, nil != policies.PointerIfPopulated()) 45 testBool(t, "RecordSQLEnabled", false, policies.RecordSQL.Enabled()) 46 testBool(t, "AttributesIncludeEnabled", false, policies.AttributesInclude.Enabled()) 47 testBool(t, "AllowRawExceptionMessages", false, policies.AllowRawExceptionMessages.Enabled()) 48 testBool(t, "CustomEventsEnabled", false, policies.CustomEvents.Enabled()) 49 testBool(t, "CustomParametersEnabled", false, policies.CustomParameters.Enabled()) 50 } 51 52 func TestNilSecurityPolicies(t *testing.T) { 53 var policies SecurityPolicies 54 testBool(t, "PointerIfPopulated", false, nil != policies.PointerIfPopulated()) 55 testBool(t, "RecordSQLEnabled", true, policies.RecordSQL.Enabled()) 56 testBool(t, "AttributesIncludeEnabled", true, policies.AttributesInclude.Enabled()) 57 testBool(t, "AllowRawExceptionMessages", true, policies.AllowRawExceptionMessages.Enabled()) 58 testBool(t, "CustomEventsEnabled", true, policies.CustomEvents.Enabled()) 59 testBool(t, "CustomParametersEnabled", true, policies.CustomParameters.Enabled()) 60 } 61 62 func TestUnknownRequiredPolicy(t *testing.T) { 63 inputJSON := []byte(`{ 64 "record_sql": { "enabled": false, "required": false }, 65 "attributes_include": { "enabled": false, "required": false }, 66 "allow_raw_exception_messages": { "enabled": false, "required": false }, 67 "custom_events": { "enabled": false, "required": false }, 68 "custom_parameters": { "enabled": false, "required": false }, 69 "custom_instrumentation_editor": { "enabled": false, "required": false }, 70 "message_parameters": { "enabled": false, "required": false }, 71 "job_arguments": { "enabled": false, "required": false }, 72 "unknown_policy": { "enabled": false, "required": true } 73 }`) 74 var policies SecurityPolicies 75 err := json.Unmarshal(inputJSON, &policies) 76 if nil == err { 77 t.Fatal(err) 78 } 79 testBool(t, "PointerIfPopulated", false, nil != policies.PointerIfPopulated()) 80 testBool(t, "unknown required policy should be disconnect", true, IsDisconnect(err)) 81 } 82 83 func TestSecurityPolicyMissing(t *testing.T) { 84 inputJSON := []byte(`{ 85 "record_sql": { "enabled": false, "required": false }, 86 "attributes_include": { "enabled": false, "required": false }, 87 "allow_raw_exception_messages": { "enabled": false, "required": false }, 88 "custom_events": { "enabled": false, "required": false }, 89 "request_parameters": { "enabled": false, "required": false } 90 }`) 91 var policies SecurityPolicies 92 err := json.Unmarshal(inputJSON, &policies) 93 _, ok := err.(errUnsetPolicy) 94 if !ok { 95 t.Fatal(err) 96 } 97 testBool(t, "PointerIfPopulated", false, nil != policies.PointerIfPopulated()) 98 testBool(t, "missing policy should be disconnect", true, IsDisconnect(err)) 99 } 100 101 func TestMalformedPolicies(t *testing.T) { 102 inputJSON := []byte(`{`) 103 var policies SecurityPolicies 104 err := json.Unmarshal(inputJSON, &policies) 105 if nil == err { 106 t.Fatal(err) 107 } 108 testBool(t, "malformed policies should not be disconnect", false, IsDisconnect(err)) 109 }