// +build unit

package auth

import (
	"reflect"
	"testing"
)

// TestEncode tests the encode function
func TestEncode(t *testing.T) {
	var tests = []struct {
		input    []byte
		expected []byte
		pass     bool
	}{
		{[]byte{18, 144}, []byte{69, 112, 65, 61}, true},
		{[]byte{18, 144}, []byte{69, 112, 65, 8}, false},
		{[]byte{2, 144}, []byte{69, 112, 65, 8}, false},
		{[]byte{28, 139, 66, 249}, []byte{72, 73, 116, 67, 45, 81, 61, 61}, true},
		{[]byte{47, 5, 71, 14, 100, 105, 224, 166}, []byte{76, 119, 86, 72, 68, 109, 82, 112, 52, 75, 89, 61}, true},
		{[]byte{47, 5, 71, 14, 100, 105, 224, 166}, []byte{76, 119, 86, 72, 68, 109, 82, 112, 52, 75, 89, 8}, false},
		{[]byte{2, 5, 71, 14, 100, 105, 224, 166}, []byte{76, 119, 86, 72, 68, 109, 82, 112, 52, 75, 89, 61}, false},
		{[]byte{126, 139, 249, 41, 122, 74, 67, 128, 232, 100, 76, 62, 126, 68, 136, 228}, []byte{102, 111, 118, 53, 75, 88, 112, 75, 81, 52, 68, 111, 90, 69, 119, 45, 102, 107, 83, 73, 53, 65, 61, 61}, true},
		{[]byte{165, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{112, 90, 100, 80, 102, 115, 116, 86, 86, 80, 65, 107, 100, 65, 69, 105, 70, 76, 111, 78, 104, 122, 107, 89, 120, 106, 113, 81, 52, 97, 69, 104, 79, 117, 90, 120, 100, 66, 110, 117, 102, 69, 99, 61}, true},
		{[]byte{233, 205, 185, 131, 206, 50, 24, 128, 178, 172, 45, 67, 200, 64, 109, 30, 212, 115, 192, 108, 35, 89, 100, 131, 145, 62, 201, 55, 84, 251, 193, 146, 107, 213, 81, 191, 105, 198, 12, 88, 57, 150, 160, 214, 22, 63, 115, 188, 154, 72, 152, 49, 31, 43, 27, 1, 21, 154, 29, 228, 223, 250, 57, 133}, []byte{54, 99, 50, 53, 103, 56, 52, 121, 71, 73, 67, 121, 114, 67, 49, 68, 121, 69, 66, 116, 72, 116, 82, 122, 119, 71, 119, 106, 87, 87, 83, 68, 107, 84, 55, 74, 78, 49, 84, 55, 119, 90, 74, 114, 49, 86, 71, 95, 97, 99, 89, 77, 87, 68, 109, 87, 111, 78, 89, 87, 80, 51, 79, 56, 109, 107, 105, 89, 77, 82, 56, 114, 71, 119, 69, 86, 109, 104, 51, 107, 51, 95, 111, 53, 104, 81, 61, 61}, true},
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{118, 90, 106, 111, 100, 117, 74, 119, 99, 70, 45, 107, 73, 79, 45, 67, 121, 116, 80, 80, 66, 102, 83, 122, 102, 71, 100, 108, 78, 85, 76, 110, 69, 82, 76, 49, 101, 67, 57, 117, 50, 122, 102, 95, 116, 99, 110, 119, 121, 99, 80, 122, 75, 78, 118, 107, 70, 120, 69, 77, 51, 107, 105, 112, 56, 111, 106, 121, 84, 99, 84, 97, 114, 85, 113, 119, 109, 76, 56, 109, 78, 104, 107, 106, 95, 111, 83, 52, 65, 84, 76, 77, 65, 68, 72, 120, 81, 79, 98, 49, 84, 109, 105, 79, 86, 98, 105, 55, 112, 89, 103, 112, 76, 113, 89, 52, 57, 105, 103, 72, 101, 54, 75, 49, 119, 80, 112, 88, 120, 103, 50, 56, 102, 88, 68, 78, 122, 99, 68, 51, 102, 80, 49, 101, 68, 115, 114, 53, 51, 106, 115, 109, 114, 106, 121, 107, 113, 78, 99, 79, 72, 111, 52, 65, 54, 86, 56, 116, 67, 103, 52, 61}, true},
		{[]byte{2, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{118, 90, 106, 111, 100, 117, 74, 119, 99, 70, 45, 107, 73, 79, 45, 67, 121, 116, 80, 80, 66, 102, 83, 122, 102, 71, 100, 108, 78, 85, 76, 110, 69, 82, 76, 49, 101, 67, 57, 117, 50, 122, 102, 95, 116, 99, 110, 119, 121, 99, 80, 122, 75, 78, 118, 107, 70, 120, 69, 77, 51, 107, 105, 112, 56, 111, 106, 121, 84, 99, 84, 97, 114, 85, 113, 119, 109, 76, 56, 109, 78, 104, 107, 106, 95, 111, 83, 52, 65, 84, 76, 77, 65, 68, 72, 120, 81, 79, 98, 49, 84, 109, 105, 79, 86, 98, 105, 55, 112, 89, 103, 112, 76, 113, 89, 52, 57, 105, 103, 72, 101, 54, 75, 49, 119, 80, 112, 88, 120, 103, 50, 56, 102, 88, 68, 78, 122, 99, 68, 51, 102, 80, 49, 101, 68, 115, 114, 53, 51, 106, 115, 109, 114, 106, 121, 107, 113, 78, 99, 79, 72, 111, 52, 65, 54, 86, 56, 116, 67, 103, 52, 61}, false},
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{118, 90, 106, 111, 100, 117, 74, 119, 99, 70, 45, 107, 73, 79, 45, 67, 121, 116, 80, 80, 66, 102, 83, 122, 102, 71, 100, 108, 78, 85, 76, 110, 69, 82, 76, 49, 101, 67, 57, 117, 50, 122, 102, 95, 116, 99, 110, 119, 121, 99, 80, 122, 75, 78, 118, 107, 70, 120, 69, 77, 51, 107, 105, 112, 56, 111, 106, 121, 84, 99, 84, 97, 114, 85, 113, 119, 109, 76, 56, 109, 78, 104, 107, 106, 95, 111, 83, 52, 65, 84, 76, 77, 65, 68, 72, 120, 81, 79, 98, 49, 84, 109, 105, 79, 86, 98, 105, 55, 112, 89, 103, 112, 76, 113, 89, 52, 57, 105, 103, 72, 101, 54, 75, 49, 119, 80, 112, 88, 120, 103, 50, 56, 102, 88, 68, 78, 122, 99, 68, 51, 102, 80, 49, 101, 68, 115, 114, 53, 51, 106, 115, 109, 114, 106, 121, 107, 113, 78, 99, 79, 72, 111, 52, 65, 54, 86, 56, 116, 67, 103, 52, 8}, false},
	}

	for idx, tt := range tests {
		z := encode(tt.input)
		assert := reflect.DeepEqual(tt.expected, z)

		if tt.pass && !assert {
			t.Errorf("test #%d failed; pass: %t, assert: %t, expected: %v, received: %v", idx+1, tt.pass, assert, tt.expected, z)
		} else if !tt.pass && assert {
			t.Errorf("test #%d failed; pass: %t, assert: %t, expected: %v, received: %v", idx+1, tt.pass, assert, tt.expected, z)
		}
	}
}

// TestDecode tests the decode function
func TestDecode(t *testing.T) {
	var tests = []struct {
		expected    []byte
		input       []byte
		pass        bool
		expectedErr error
	}{
		{[]byte{18, 144}, []byte{69, 112, 65, 61}, true, nil},
		{[]byte{18, 144}, []byte{69, 112, 65, 8}, false, ErrBase64Decode},
		{[]byte{2, 144}, []byte{69, 112, 65, 8}, false, ErrBase64Decode},
		{[]byte{28, 139, 66, 249}, []byte{72, 73, 116, 67, 45, 81, 61, 61}, true, nil},
		{[]byte{47, 5, 71, 14, 100, 105, 224, 166}, []byte{76, 119, 86, 72, 68, 109, 82, 112, 52, 75, 89, 61}, true, nil},
		{[]byte{47, 5, 71, 14, 100, 105, 224, 166}, []byte{76, 119, 86, 72, 68, 109, 82, 112, 52, 75, 89, 8}, false, ErrBase64Decode},
		{[]byte{2, 5, 71, 14, 100, 105, 224, 166}, []byte{76, 119, 86, 72, 68, 109, 82, 112, 52, 75, 89, 61}, false, nil}, // note: I guess this is a valid base64 byte slice?
		{[]byte{126, 139, 249, 41, 122, 74, 67, 128, 232, 100, 76, 62, 126, 68, 136, 228}, []byte{102, 111, 118, 53, 75, 88, 112, 75, 81, 52, 68, 111, 90, 69, 119, 45, 102, 107, 83, 73, 53, 65, 61, 61}, true, nil},
		{[]byte{165, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{112, 90, 100, 80, 102, 115, 116, 86, 86, 80, 65, 107, 100, 65, 69, 105, 70, 76, 111, 78, 104, 122, 107, 89, 120, 106, 113, 81, 52, 97, 69, 104, 79, 117, 90, 120, 100, 66, 110, 117, 102, 69, 99, 61}, true, nil},
		{[]byte{233, 205, 185, 131, 206, 50, 24, 128, 178, 172, 45, 67, 200, 64, 109, 30, 212, 115, 192, 108, 35, 89, 100, 131, 145, 62, 201, 55, 84, 251, 193, 146, 107, 213, 81, 191, 105, 198, 12, 88, 57, 150, 160, 214, 22, 63, 115, 188, 154, 72, 152, 49, 31, 43, 27, 1, 21, 154, 29, 228, 223, 250, 57, 133}, []byte{54, 99, 50, 53, 103, 56, 52, 121, 71, 73, 67, 121, 114, 67, 49, 68, 121, 69, 66, 116, 72, 116, 82, 122, 119, 71, 119, 106, 87, 87, 83, 68, 107, 84, 55, 74, 78, 49, 84, 55, 119, 90, 74, 114, 49, 86, 71, 95, 97, 99, 89, 77, 87, 68, 109, 87, 111, 78, 89, 87, 80, 51, 79, 56, 109, 107, 105, 89, 77, 82, 56, 114, 71, 119, 69, 86, 109, 104, 51, 107, 51, 95, 111, 53, 104, 81, 61, 61}, true, nil},
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{118, 90, 106, 111, 100, 117, 74, 119, 99, 70, 45, 107, 73, 79, 45, 67, 121, 116, 80, 80, 66, 102, 83, 122, 102, 71, 100, 108, 78, 85, 76, 110, 69, 82, 76, 49, 101, 67, 57, 117, 50, 122, 102, 95, 116, 99, 110, 119, 121, 99, 80, 122, 75, 78, 118, 107, 70, 120, 69, 77, 51, 107, 105, 112, 56, 111, 106, 121, 84, 99, 84, 97, 114, 85, 113, 119, 109, 76, 56, 109, 78, 104, 107, 106, 95, 111, 83, 52, 65, 84, 76, 77, 65, 68, 72, 120, 81, 79, 98, 49, 84, 109, 105, 79, 86, 98, 105, 55, 112, 89, 103, 112, 76, 113, 89, 52, 57, 105, 103, 72, 101, 54, 75, 49, 119, 80, 112, 88, 120, 103, 50, 56, 102, 88, 68, 78, 122, 99, 68, 51, 102, 80, 49, 101, 68, 115, 114, 53, 51, 106, 115, 109, 114, 106, 121, 107, 113, 78, 99, 79, 72, 111, 52, 65, 54, 86, 56, 116, 67, 103, 52, 61}, true, nil},
		{[]byte{2, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{118, 90, 106, 111, 100, 117, 74, 119, 99, 70, 45, 107, 73, 79, 45, 67, 121, 116, 80, 80, 66, 102, 83, 122, 102, 71, 100, 108, 78, 85, 76, 110, 69, 82, 76, 49, 101, 67, 57, 117, 50, 122, 102, 95, 116, 99, 110, 119, 121, 99, 80, 122, 75, 78, 118, 107, 70, 120, 69, 77, 51, 107, 105, 112, 56, 111, 106, 121, 84, 99, 84, 97, 114, 85, 113, 119, 109, 76, 56, 109, 78, 104, 107, 106, 95, 111, 83, 52, 65, 84, 76, 77, 65, 68, 72, 120, 81, 79, 98, 49, 84, 109, 105, 79, 86, 98, 105, 55, 112, 89, 103, 112, 76, 113, 89, 52, 57, 105, 103, 72, 101, 54, 75, 49, 119, 80, 112, 88, 120, 103, 50, 56, 102, 88, 68, 78, 122, 99, 68, 51, 102, 80, 49, 101, 68, 115, 114, 53, 51, 106, 115, 109, 114, 106, 121, 107, 113, 78, 99, 79, 72, 111, 52, 65, 54, 86, 56, 116, 67, 103, 52, 61}, false, nil}, // I guess this is a valid base64 byte slice?
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{118, 90, 106, 111, 100, 117, 74, 119, 99, 70, 45, 107, 73, 79, 45, 67, 121, 116, 80, 80, 66, 102, 83, 122, 102, 71, 100, 108, 78, 85, 76, 110, 69, 82, 76, 49, 101, 67, 57, 117, 50, 122, 102, 95, 116, 99, 110, 119, 121, 99, 80, 122, 75, 78, 118, 107, 70, 120, 69, 77, 51, 107, 105, 112, 56, 111, 106, 121, 84, 99, 84, 97, 114, 85, 113, 119, 109, 76, 56, 109, 78, 104, 107, 106, 95, 111, 83, 52, 65, 84, 76, 77, 65, 68, 72, 120, 81, 79, 98, 49, 84, 109, 105, 79, 86, 98, 105, 55, 112, 89, 103, 112, 76, 113, 89, 52, 57, 105, 103, 72, 101, 54, 75, 49, 119, 80, 112, 88, 120, 103, 50, 56, 102, 88, 68, 78, 122, 99, 68, 51, 102, 80, 49, 101, 68, 115, 114, 53, 51, 106, 115, 109, 114, 106, 121, 107, 113, 78, 99, 79, 72, 111, 52, 65, 54, 86, 56, 116, 67, 103, 52, 8}, false, ErrBase64Decode},
	}

	for idx, tt := range tests {
		z, err := decode(tt.input)
		assert := reflect.DeepEqual(tt.expected, z)

		if tt.expectedErr != err {
			t.Errorf("test #%d failed; expected err: %v, received err: %v", idx+1, tt.expectedErr, err)
		}

		if tt.pass && !assert {
			t.Errorf("test #%d failed; pass: %t, assert: %t, expected: %v, received: %v", idx+1, tt.pass, assert, tt.expected, z)
		} else if !tt.pass && assert {
			t.Errorf("test #%d failed; pass: %t, assert: %t, expected: %v, received: %v", idx+1, tt.pass, assert, tt.expected, z)
		}
	}
}

// TestSignHMAC tests the signHMAC function
func TestSignHMAC(t *testing.T) {
	key := []byte("DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw==")
	var tests = []struct {
		input    []byte
		expected []byte
		pass     bool
	}{
		{[]byte{18, 144}, []byte{188, 172, 214, 59, 3, 185, 57, 113, 68, 244, 55, 50, 30, 114, 166, 217, 178, 131, 203, 179, 164, 23, 95, 22, 155, 150, 204, 234, 204, 61, 215, 227, 34, 239, 116, 239, 239, 135, 104, 98, 220, 111, 227, 149, 217, 122, 101, 157, 37, 116, 120, 116, 13, 237, 238, 125, 173, 38, 116, 228, 0, 5, 109, 161}, true},
		{[]byte{18, 144}, []byte{188, 172, 214, 59, 3, 185, 57, 113, 68, 244, 55, 50, 30, 114, 166, 217, 178, 131, 203, 179, 164, 23, 95, 22, 155, 150, 204, 234, 204, 61, 215, 227, 34, 239, 116, 239, 239, 135, 104, 98, 220, 111, 227, 149, 217, 122, 101, 157, 37, 116, 120, 116, 13, 237, 238, 125, 173, 38, 116, 228, 0, 5, 109, 8}, false},
		{[]byte{2, 144}, []byte{188, 172, 214, 59, 3, 185, 57, 113, 68, 244, 55, 50, 30, 114, 166, 217, 178, 131, 203, 179, 164, 23, 95, 22, 155, 150, 204, 234, 204, 61, 215, 227, 34, 239, 116, 239, 239, 135, 104, 98, 220, 111, 227, 149, 217, 122, 101, 157, 37, 116, 120, 116, 13, 237, 238, 125, 173, 38, 116, 228, 0, 5, 109, 161}, false},
		{[]byte{28, 139, 66, 249}, []byte{142, 144, 179, 31, 86, 98, 236, 244, 138, 217, 39, 119, 119, 176, 66, 89, 251, 183, 167, 151, 191, 219, 171, 197, 77, 133, 242, 207, 137, 249, 80, 66, 242, 245, 200, 105, 202, 49, 153, 189, 3, 231, 153, 125, 165, 14, 34, 169, 151, 50, 226, 129, 97, 118, 95, 121, 37, 216, 248, 176, 254, 54, 15, 134}, true},
		{[]byte{47, 5, 71, 14, 100, 105, 224, 166}, []byte{52, 196, 147, 117, 93, 31, 250, 6, 178, 226, 16, 150, 145, 74, 38, 33, 26, 132, 158, 8, 157, 50, 223, 232, 110, 217, 67, 38, 46, 222, 4, 33, 78, 89, 7, 179, 26, 107, 79, 88, 189, 175, 51, 181, 127, 12, 183, 220, 127, 218, 53, 12, 233, 196, 186, 127, 248, 127, 8, 228, 209, 236, 124, 239}, true},
		{[]byte{126, 139, 249, 41, 122, 74, 67, 128, 232, 100, 76, 62, 126, 68, 136, 228}, []byte{158, 9, 177, 189, 9, 148, 175, 217, 151, 219, 116, 206, 188, 229, 46, 124, 181, 176, 243, 111, 52, 171, 179, 134, 68, 217, 49, 226, 97, 93, 17, 157, 36, 189, 51, 14, 220, 75, 189, 224, 206, 1, 84, 173, 193, 68, 195, 52, 110, 157, 5, 113, 45, 88, 164, 137, 115, 11, 104, 227, 17, 188, 203, 149}, true},
		{[]byte{165, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{124, 178, 171, 82, 57, 21, 12, 193, 209, 0, 234, 236, 137, 26, 59, 71, 250, 199, 29, 2, 179, 4, 95, 124, 188, 36, 105, 223, 230, 249, 248, 29, 155, 9, 129, 90, 125, 117, 189, 235, 212, 149, 38, 155, 112, 182, 211, 241, 158, 150, 252, 208, 138, 165, 136, 202, 10, 7, 194, 157, 228, 234, 131, 47}, true},
		{[]byte{165, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{124, 178, 171, 82, 57, 21, 12, 193, 209, 0, 234, 236, 137, 26, 59, 71, 250, 199, 29, 2, 179, 4, 95, 124, 188, 36, 105, 223, 230, 249, 248, 29, 155, 9, 129, 90, 125, 117, 189, 235, 212, 149, 38, 155, 112, 182, 211, 241, 158, 150, 252, 208, 138, 165, 136, 202, 10, 7, 194, 157, 228, 234, 131, 8}, false},
		{[]byte{2, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{124, 178, 171, 82, 57, 21, 12, 193, 209, 0, 234, 236, 137, 26, 59, 71, 250, 199, 29, 2, 179, 4, 95, 124, 188, 36, 105, 223, 230, 249, 248, 29, 155, 9, 129, 90, 125, 117, 189, 235, 212, 149, 38, 155, 112, 182, 211, 241, 158, 150, 252, 208, 138, 165, 136, 202, 10, 7, 194, 157, 228, 234, 131, 47}, false},
		{[]byte{233, 205, 185, 131, 206, 50, 24, 128, 178, 172, 45, 67, 200, 64, 109, 30, 212, 115, 192, 108, 35, 89, 100, 131, 145, 62, 201, 55, 84, 251, 193, 146, 107, 213, 81, 191, 105, 198, 12, 88, 57, 150, 160, 214, 22, 63, 115, 188, 154, 72, 152, 49, 31, 43, 27, 1, 21, 154, 29, 228, 223, 250, 57, 133}, []byte{67, 3, 175, 22, 32, 15, 99, 67, 58, 13, 137, 218, 184, 137, 129, 93, 106, 55, 177, 141, 139, 124, 33, 80, 205, 18, 102, 151, 31, 172, 221, 145, 144, 5, 49, 228, 147, 167, 208, 207, 105, 245, 121, 208, 38, 23, 58, 105, 172, 171, 102, 177, 86, 84, 229, 134, 222, 41, 35, 139, 229, 215, 4, 107}, true},
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{58, 44, 38, 68, 20, 120, 29, 236, 163, 237, 239, 134, 16, 173, 228, 170, 170, 80, 86, 91, 211, 42, 177, 87, 217, 30, 27, 148, 49, 224, 28, 101, 139, 43, 11, 252, 77, 13, 78, 13, 149, 135, 165, 208, 108, 78, 42, 80, 60, 174, 47, 39, 41, 131, 170, 63, 231, 6, 84, 30, 79, 98, 26, 91}, true},
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{58, 44, 38, 68, 20, 120, 29, 236, 163, 237, 239, 134, 16, 173, 228, 170, 170, 80, 86, 91, 211, 42, 177, 87, 217, 30, 27, 148, 49, 224, 28, 101, 139, 43, 11, 252, 77, 13, 78, 13, 149, 135, 165, 208, 108, 78, 42, 80, 60, 174, 47, 39, 41, 131, 170, 63, 231, 6, 84, 30, 79, 98, 26, 8}, false},
		{[]byte{2, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{58, 44, 38, 68, 20, 120, 29, 236, 163, 237, 239, 134, 16, 173, 228, 170, 170, 80, 86, 91, 211, 42, 177, 87, 217, 30, 27, 148, 49, 224, 28, 101, 139, 43, 11, 252, 77, 13, 78, 13, 149, 135, 165, 208, 108, 78, 42, 80, 60, 174, 47, 39, 41, 131, 170, 63, 231, 6, 84, 30, 79, 98, 26, 91}, false},
	}

	for idx, tt := range tests {
		z := signHMAC(&tt.input, &key)
		assert := reflect.DeepEqual(tt.expected, z)

		if tt.pass && !assert {
			t.Errorf("test #%d failed; pass: %t, assert: %t, expected: %v, received: %v", idx+1, tt.pass, assert, tt.expected, z)
		} else if !tt.pass && assert {
			t.Errorf("test #%d failed; pass: %t, assert: %t, expected: %v, received: %v", idx+1, tt.pass, assert, tt.expected, z)
		}
	}
}

// TestVerifyHMAC tests the verifyHMAC function
func TestVerifyHMAC(t *testing.T) {
	key := []byte("DOZDgBdMhGLImnk0BGYgOUI+h1n7U+OdxcZPctMbeFCsuAom2aFU4JPV4Qj11hbcb5yaM4WDuNP/3B7b+BnFhw==")
	var tests = []struct {
		input    []byte
		expected []byte
		pass     bool
	}{
		{[]byte{18, 144}, []byte{188, 172, 214, 59, 3, 185, 57, 113, 68, 244, 55, 50, 30, 114, 166, 217, 178, 131, 203, 179, 164, 23, 95, 22, 155, 150, 204, 234, 204, 61, 215, 227, 34, 239, 116, 239, 239, 135, 104, 98, 220, 111, 227, 149, 217, 122, 101, 157, 37, 116, 120, 116, 13, 237, 238, 125, 173, 38, 116, 228, 0, 5, 109, 161}, true},
		{[]byte{18, 144}, []byte{188, 172, 214, 59, 3, 185, 57, 113, 68, 244, 55, 50, 30, 114, 166, 217, 178, 131, 203, 179, 164, 23, 95, 22, 155, 150, 204, 234, 204, 61, 215, 227, 34, 239, 116, 239, 239, 135, 104, 98, 220, 111, 227, 149, 217, 122, 101, 157, 37, 116, 120, 116, 13, 237, 238, 125, 173, 38, 116, 228, 0, 5, 109, 8}, false},
		{[]byte{2, 144}, []byte{188, 172, 214, 59, 3, 185, 57, 113, 68, 244, 55, 50, 30, 114, 166, 217, 178, 131, 203, 179, 164, 23, 95, 22, 155, 150, 204, 234, 204, 61, 215, 227, 34, 239, 116, 239, 239, 135, 104, 98, 220, 111, 227, 149, 217, 122, 101, 157, 37, 116, 120, 116, 13, 237, 238, 125, 173, 38, 116, 228, 0, 5, 109, 161}, false},
		{[]byte{28, 139, 66, 249}, []byte{142, 144, 179, 31, 86, 98, 236, 244, 138, 217, 39, 119, 119, 176, 66, 89, 251, 183, 167, 151, 191, 219, 171, 197, 77, 133, 242, 207, 137, 249, 80, 66, 242, 245, 200, 105, 202, 49, 153, 189, 3, 231, 153, 125, 165, 14, 34, 169, 151, 50, 226, 129, 97, 118, 95, 121, 37, 216, 248, 176, 254, 54, 15, 134}, true},
		{[]byte{47, 5, 71, 14, 100, 105, 224, 166}, []byte{52, 196, 147, 117, 93, 31, 250, 6, 178, 226, 16, 150, 145, 74, 38, 33, 26, 132, 158, 8, 157, 50, 223, 232, 110, 217, 67, 38, 46, 222, 4, 33, 78, 89, 7, 179, 26, 107, 79, 88, 189, 175, 51, 181, 127, 12, 183, 220, 127, 218, 53, 12, 233, 196, 186, 127, 248, 127, 8, 228, 209, 236, 124, 239}, true},
		{[]byte{126, 139, 249, 41, 122, 74, 67, 128, 232, 100, 76, 62, 126, 68, 136, 228}, []byte{158, 9, 177, 189, 9, 148, 175, 217, 151, 219, 116, 206, 188, 229, 46, 124, 181, 176, 243, 111, 52, 171, 179, 134, 68, 217, 49, 226, 97, 93, 17, 157, 36, 189, 51, 14, 220, 75, 189, 224, 206, 1, 84, 173, 193, 68, 195, 52, 110, 157, 5, 113, 45, 88, 164, 137, 115, 11, 104, 227, 17, 188, 203, 149}, true},
		{[]byte{165, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{124, 178, 171, 82, 57, 21, 12, 193, 209, 0, 234, 236, 137, 26, 59, 71, 250, 199, 29, 2, 179, 4, 95, 124, 188, 36, 105, 223, 230, 249, 248, 29, 155, 9, 129, 90, 125, 117, 189, 235, 212, 149, 38, 155, 112, 182, 211, 241, 158, 150, 252, 208, 138, 165, 136, 202, 10, 7, 194, 157, 228, 234, 131, 47}, true},
		{[]byte{165, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{124, 178, 171, 82, 57, 21, 12, 193, 209, 0, 234, 236, 137, 26, 59, 71, 250, 199, 29, 2, 179, 4, 95, 124, 188, 36, 105, 223, 230, 249, 248, 29, 155, 9, 129, 90, 125, 117, 189, 235, 212, 149, 38, 155, 112, 182, 211, 241, 158, 150, 252, 208, 138, 165, 136, 202, 10, 7, 194, 157, 228, 234, 131, 8}, false},
		{[]byte{2, 151, 79, 126, 203, 85, 84, 240, 36, 116, 1, 34, 20, 186, 13, 135, 57, 24, 198, 58, 144, 225, 161, 33, 58, 230, 113, 116, 25, 238, 124, 71}, []byte{124, 178, 171, 82, 57, 21, 12, 193, 209, 0, 234, 236, 137, 26, 59, 71, 250, 199, 29, 2, 179, 4, 95, 124, 188, 36, 105, 223, 230, 249, 248, 29, 155, 9, 129, 90, 125, 117, 189, 235, 212, 149, 38, 155, 112, 182, 211, 241, 158, 150, 252, 208, 138, 165, 136, 202, 10, 7, 194, 157, 228, 234, 131, 47}, false},
		{[]byte{233, 205, 185, 131, 206, 50, 24, 128, 178, 172, 45, 67, 200, 64, 109, 30, 212, 115, 192, 108, 35, 89, 100, 131, 145, 62, 201, 55, 84, 251, 193, 146, 107, 213, 81, 191, 105, 198, 12, 88, 57, 150, 160, 214, 22, 63, 115, 188, 154, 72, 152, 49, 31, 43, 27, 1, 21, 154, 29, 228, 223, 250, 57, 133}, []byte{67, 3, 175, 22, 32, 15, 99, 67, 58, 13, 137, 218, 184, 137, 129, 93, 106, 55, 177, 141, 139, 124, 33, 80, 205, 18, 102, 151, 31, 172, 221, 145, 144, 5, 49, 228, 147, 167, 208, 207, 105, 245, 121, 208, 38, 23, 58, 105, 172, 171, 102, 177, 86, 84, 229, 134, 222, 41, 35, 139, 229, 215, 4, 107}, true},
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{58, 44, 38, 68, 20, 120, 29, 236, 163, 237, 239, 134, 16, 173, 228, 170, 170, 80, 86, 91, 211, 42, 177, 87, 217, 30, 27, 148, 49, 224, 28, 101, 139, 43, 11, 252, 77, 13, 78, 13, 149, 135, 165, 208, 108, 78, 42, 80, 60, 174, 47, 39, 41, 131, 170, 63, 231, 6, 84, 30, 79, 98, 26, 91}, true},
		{[]byte{189, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{58, 44, 38, 68, 20, 120, 29, 236, 163, 237, 239, 134, 16, 173, 228, 170, 170, 80, 86, 91, 211, 42, 177, 87, 217, 30, 27, 148, 49, 224, 28, 101, 139, 43, 11, 252, 77, 13, 78, 13, 149, 135, 165, 208, 108, 78, 42, 80, 60, 174, 47, 39, 41, 131, 170, 63, 231, 6, 84, 30, 79, 98, 26, 8}, false},
		{[]byte{2, 152, 232, 118, 226, 112, 112, 95, 164, 32, 239, 130, 202, 211, 207, 5, 244, 179, 124, 103, 101, 53, 66, 231, 17, 18, 245, 120, 47, 110, 219, 55, 255, 181, 201, 240, 201, 195, 243, 40, 219, 228, 23, 17, 12, 222, 72, 169, 242, 136, 242, 77, 196, 218, 173, 74, 176, 152, 191, 38, 54, 25, 35, 254, 132, 184, 1, 50, 204, 0, 49, 241, 64, 230, 245, 78, 104, 142, 85, 184, 187, 165, 136, 41, 46, 166, 56, 246, 40, 7, 123, 162, 181, 192, 250, 87, 198, 13, 188, 125, 112, 205, 205, 192, 247, 124, 253, 94, 14, 202, 249, 222, 59, 38, 174, 60, 164, 168, 215, 14, 30, 142, 0, 233, 95, 45, 10, 14}, []byte{58, 44, 38, 68, 20, 120, 29, 236, 163, 237, 239, 134, 16, 173, 228, 170, 170, 80, 86, 91, 211, 42, 177, 87, 217, 30, 27, 148, 49, 224, 28, 101, 139, 43, 11, 252, 77, 13, 78, 13, 149, 135, 165, 208, 108, 78, 42, 80, 60, 174, 47, 39, 41, 131, 170, 63, 231, 6, 84, 30, 79, 98, 26, 91}, false},
	}

	for idx, tt := range tests {
		z := verifyHMAC(&tt.input, &tt.expected, &key)

		if tt.pass != z {
			t.Errorf("test #%d failed; pass: %t, verifyHMAC: %t, input: %v, expected: %v", idx+1, tt.pass, z, tt.input, tt.expected)
		}
	}
}
