File: obfuscate_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (82 lines) | stat: -rw-r--r-- 1,825 bytes parent folder | download
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
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package internal

import (
	"testing"
)

func TestDeobfuscate(t *testing.T) {
	var out []byte
	var err error

	for _, in := range []string{"", "foo"} {
		out, err = Deobfuscate(in, []byte(""))
		if err == nil {
			t.Error("error is nil for an empty key")
		}
		if out != nil {
			t.Errorf("out is not nil; got: %s", out)
		}
	}

	for _, in := range []string{"invalid_base64", "=moreinvalidbase64", "xx"} {
		out, err = Deobfuscate(in, []byte(""))
		if err == nil {
			t.Error("error is nil for invalid base64")
		}
		if out != nil {
			t.Errorf("out is not nil; got: %s", out)
		}
	}

	for _, test := range []struct {
		input    string
		key      string
		expected string
	}{
		{"", "BLAHHHH", ""},
		{"NikyPBs8OisiJg==", "BLAHHHH", "testString"},
	} {
		out, err = Deobfuscate(test.input, []byte(test.key))
		if err != nil {
			t.Errorf("error expected to be nil; got: %v", err)
		}
		if string(out) != test.expected {
			t.Errorf("output mismatch; expected: %s; got: %s", test.expected, out)
		}
	}
}

func TestObfuscate(t *testing.T) {
	var out string
	var err error

	for _, in := range []string{"", "foo"} {
		out, err = Obfuscate([]byte(in), []byte(""))
		if err == nil {
			t.Error("error is nil for an empty key")
		}
		if out != "" {
			t.Errorf("out is not an empty string; got: %s", out)
		}
	}

	for _, test := range []struct {
		input    string
		key      string
		expected string
	}{
		{"", "BLAHHHH", ""},
		{"testString", "BLAHHHH", "NikyPBs8OisiJg=="},
	} {
		out, err = Obfuscate([]byte(test.input), []byte(test.key))
		if err != nil {
			t.Errorf("error expected to be nil; got: %v", err)
		}
		if out != test.expected {
			t.Errorf("output mismatch; expected: %s; got: %s", test.expected, out)
		}
	}
}