| 1 |
package machineid |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto/hmac" |
| 6 |
"crypto/sha256" |
| 7 |
"encoding/hex" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func Test_protect(t *testing.T) { |
| 13 |
appID := "ms.azur.appX" |
| 14 |
machineID := "1a1238d601ad430cbea7efb0d1f3d92d" |
| 15 |
hash := protect(appID, machineID) |
| 16 |
if hash == "" { |
| 17 |
t.Error("hash is empty") |
| 18 |
} |
| 19 |
rawHash, err := hex.DecodeString(hash) |
| 20 |
if err != nil { |
| 21 |
t.Error(err) |
| 22 |
} |
| 23 |
eq := checkMAC([]byte(appID), rawHash, []byte(machineID)) |
| 24 |
if eq == false { |
| 25 |
t.Error("hashes do not match") |
| 26 |
} |
| 27 |
|
| 28 |
rawHash[0] = 0 |
| 29 |
eq = checkMAC([]byte(appID), rawHash, []byte(machineID)) |
| 30 |
if eq == true { |
| 31 |
t.Error("hashes do match, but shouldn't") |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
func checkMAC(message, messageMAC, key []byte) bool { |
| 36 |
mac := hmac.New(sha256.New, key) |
| 37 |
mac.Write(message) |
| 38 |
expectedMAC := mac.Sum(nil) |
| 39 |
return hmac.Equal(messageMAC, expectedMAC) |
| 40 |
} |
| 41 |
|
| 42 |
func Test_run(t *testing.T) { |
| 43 |
stdout := &bytes.Buffer{} |
| 44 |
stderr := &bytes.Buffer{} |
| 45 |
wantStdout := "hello" |
| 46 |
wantStderr := "" |
| 47 |
if err := run(stdout, stderr, "echo", "hello"); err != nil { |
| 48 |
t.Error(err) |
| 49 |
} |
| 50 |
gotStdout := strings.TrimRight(stdout.String(), "\r\n") |
| 51 |
if gotStdout != wantStdout { |
| 52 |
t.Errorf("run() = %v, want %v", gotStdout, wantStdout) |
| 53 |
} |
| 54 |
if gotStderr := stderr.String(); gotStderr != wantStderr { |
| 55 |
t.Errorf("run() = %v, want %v", gotStderr, wantStderr) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func Test_run_unknown(t *testing.T) { |
| 60 |
stdout := &bytes.Buffer{} |
| 61 |
stderr := &bytes.Buffer{} |
| 62 |
err := run(stdout, stderr, "echolo", "hello") |
| 63 |
if err == nil { |
| 64 |
t.Error("expected error, got none") |
| 65 |
} |
| 66 |
if strings.Contains(err.Error(), "executable file not found") == false { |
| 67 |
t.Error("unexpected error, expected exec not found") |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
func Test_trim(t *testing.T) { |
| 72 |
type args struct { |
| 73 |
s string |
| 74 |
} |
| 75 |
tests := []struct { |
| 76 |
name string |
| 77 |
args args |
| 78 |
want string |
| 79 |
}{ |
| 80 |
{ |
| 81 |
name: "nil", |
| 82 |
args: args{s: ""}, |
| 83 |
want: "", |
| 84 |
}, |
| 85 |
{ |
| 86 |
name: "space", |
| 87 |
args: args{s: " space "}, |
| 88 |
want: "space", |
| 89 |
}, |
| 90 |
{ |
| 91 |
name: "nl", |
| 92 |
args: args{s: "data\n"}, |
| 93 |
want: "data", |
| 94 |
}, |
| 95 |
{ |
| 96 |
name: "combined", |
| 97 |
args: args{s: " some data \n"}, |
| 98 |
want: "some data", |
| 99 |
}, |
| 100 |
} |
| 101 |
for _, tt := range tests { |
| 102 |
t.Run(tt.name, func(t *testing.T) { |
| 103 |
if got := trim(tt.args.s); got != tt.want { |
| 104 |
t.Errorf("trim() = %v, want %v", got, tt.want) |
| 105 |
} |
| 106 |
}) |
| 107 |
} |
| 108 |
} |