Skip to content

Commit

Permalink
Merge branch 'main' into tid-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtw authored Sep 22, 2023
2 parents 9bd1235 + 3855006 commit fc2dc49
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/davecgh/go-spew v1.1.1
github.com/go-kit/kit v0.12.0
github.com/go-kit/kit v0.13.0
github.com/go-kit/log v0.2.1
github.com/google/uuid v1.3.1
github.com/stretchr/testify v1.8.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ github.com/go-ini/ini v1.36.1-0.20180420150025-bda519ae5f4c/go.mod h1:ByCAeIL28u
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.10.0/go.mod h1:xUsJbQ/Fp4kEt7AFgCuvyX4a71u8h9jB8tj/ORgOZ7o=
github.com/go-kit/kit v0.12.0 h1:e4o3o3IsBfAKQh5Qbbiqyfu97Ku7jrO/JbohvztANh4=
github.com/go-kit/kit v0.12.0/go.mod h1:lHd+EkCZPIwYItmGDDRdhinkzX2A1sj+M9biaEaizzs=
github.com/go-kit/kit v0.13.0 h1:OoneCcHKHQ03LfBpoQCUfCluwd2Vt3ohz+kvbJneZAU=
github.com/go-kit/kit v0.13.0/go.mod h1:phqEHMMUbyrCFCTgH48JueqrM3md2HcAZ8N3XE4FKDg=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
github.com/go-ldap/ldap v3.0.2+incompatible/go.mod h1:qfd9rJvER9Q0/D/Sqn1DfHRoBp40uXYvFoEVrNEPqRc=
Expand Down
23 changes: 23 additions & 0 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,29 @@ func (id DeviceID) Bytes() []byte {
return []byte(id)
}

// String is a convenience function to obtain the string representation of the
// prefix portion of the ID.
func (id DeviceID) Prefix() string {
prefix, _ := id.split()
return prefix
}

// ID is a convenience function to obtain the string representation of the
// identifier portion of the ID.
func (id DeviceID) ID() string {
_, idPart := id.split()
return idPart
}

func (id DeviceID) split() (prefix, idPart string) {
parts := strings.SplitN(string(id), ":", 2)
if len(parts) != 2 {
return parts[0], ""
}

return parts[0], parts[1]
}

// ParseID parses a raw device name into a canonicalized identifier.
func ParseDeviceID(deviceName string) (DeviceID, error) {
match := DeviceIDPattern.FindStringSubmatch(deviceName)
Expand Down
101 changes: 85 additions & 16 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,91 @@ func TestParseDeviceID(t *testing.T) {
testData := []struct {
id string
expected DeviceID
prefix string
literalID string
expectsError bool
}{
{"MAC:11:22:33:44:55:66", "mac:112233445566", false},
{"MAC:11aaBB445566", "mac:11aabb445566", false},
{"mac:11-aa-BB-44-55-66", "mac:11aabb445566", false},
{"mac:11,aa,BB,44,55,66", "mac:11aabb445566", false},
{"uuid:anything Goes!", "uuid:anything Goes!", false},
{"dns:anything Goes!", "dns:anything Goes!", false},
{"serial:1234", "serial:1234", false},
{"mac:11-aa-BB-44-55-66/service", "mac:11aabb445566", false},
{"mac:11-aa-BB-44-55-66/service/", "mac:11aabb445566", false},
{"mac:11-aa-BB-44-55-66/service/ignoreMe", "mac:11aabb445566", false},
{"mac:11-aa-BB-44-55-66/service/foo/bar", "mac:11aabb445566", false},
{"invalid:a-BB-44-55", "", true},
{"mac:11-aa-BB-44-55", "", true},
{"MAC:invalid45566", "", true},
{"mac:481d70187fef", "mac:481d70187fef", false},
{"mac:481d70187fef/parodus/tag/test0", "mac:481d70187fef", false},
{
id: "MAC:11:22:33:44:55:66",
expected: "mac:112233445566",
prefix: "mac",
literalID: "112233445566",
}, {
id: "MAC:11aaBB445566",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11-aa-BB-44-55-66",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11,aa,BB,44,55,66",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "uuid:anything Goes!",
expected: "uuid:anything Goes!",
prefix: "uuid",
literalID: "anything Goes!",
}, {
id: "dns:anything Goes!",
expected: "dns:anything Goes!",
prefix: "dns",
literalID: "anything Goes!",
}, {
id: "serial:1234",
expected: "serial:1234",
prefix: "serial",
literalID: "1234",
}, {
id: "mac:11-aa-BB-44-55-66/service",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11-aa-BB-44-55-66/service/",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "MAC:11-aa-BB-44-55-66/service/ignoreMe",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "mac:11-aa-BB-44-55-66/service/foo/bar",
expected: "mac:11aabb445566",
prefix: "mac",
literalID: "11aabb445566",
}, {
id: "invalid:a-BB-44-55",
expectsError: true,
}, {
id: "mac:11-aa-BB-44-55",
expectsError: true,
}, {
id: "MAC:invalid45566",
expectsError: true,
}, {
id: "invalid:random stuff",
expectsError: true,
}, {
id: "mac:11223344556w",
expectsError: true,
}, {
id: "mac:481d70187fef",
expected: "mac:481d70187fef",
prefix: "mac",
literalID: "481d70187fef",
}, {
id: "mac:481d70187fef/parodus/tag/test0",
expected: "mac:481d70187fef",
prefix: "mac",
literalID: "481d70187fef",
},
}

for _, record := range testData {
Expand All @@ -54,6 +121,8 @@ func TestParseDeviceID(t *testing.T) {
assert.Equal(record.expected, id)
assert.Equal(record.expectsError, err != nil)
assert.Equal([]byte(record.expected), id.Bytes())
assert.Equal(record.prefix, id.Prefix())
assert.Equal(record.literalID, id.ID())
})
}
}
4 changes: 3 additions & 1 deletion wrpclient/wrpclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ func TestSendWRP(t *testing.T) {
client.httpClient = m
}

err = client.SendWRP(ctx, &tc.response, &tc.request)
response := tc.response
request := tc.request
err = client.SendWRP(ctx, &response, &request)
if tc.useMockHTTPClient {
m.AssertExpectations(t)
}
Expand Down
2 changes: 1 addition & 1 deletion wrphttp/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
// Constant HTTP header strings representing WRP fields
const (
MessageTypeHeader = "X-Xmidt-Message-Type"
TransactionUuidHeader = "X-Xmidt-Transaction-Uuid"
TransactionUuidHeader = "X-Xmidt-Transaction-Uuid" // nolint:gosec
StatusHeader = "X-Xmidt-Status"
RequestDeliveryResponseHeader = "X-Xmidt-Request-Delivery-Response"
IncludeSpansHeader = "X-Xmidt-Include-Spans"
Expand Down
3 changes: 2 additions & 1 deletion wrphttp/headers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ func TestAddMessageHeaders(t *testing.T) {

expected := record.expected
actual := make(http.Header)
AddMessageHeaders(actual, &record.message)
message := record.message
AddMessageHeaders(actual, &message)

for _, header := range regularHeaders {
assert.Equal(expected[header], actual[header])
Expand Down

0 comments on commit fc2dc49

Please sign in to comment.