Skip to content

Commit

Permalink
Refinement for tests and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
DuduPan committed May 4, 2023
1 parent 07a97c2 commit aef173f
Show file tree
Hide file tree
Showing 4 changed files with 451 additions and 270 deletions.
5 changes: 3 additions & 2 deletions encode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gpp

import (
"errors"
"fmt"
"github.com/prebid/go-gpp/constants"
"github.com/prebid/go-gpp/util"
Expand All @@ -15,7 +16,7 @@ const (
)

var (
duplicatedSectionErr = fmt.Errorf("duplicated sections")
duplicatedSectionErr = errors.New("duplicated sections")
)

func Encode(sections []Section) (string, error) {
Expand Down Expand Up @@ -53,8 +54,8 @@ func Encode(sections []Section) (string, error) {
builder.Write(bs.Base64Encode())

for _, sec := range sections {
// TODO: add a parameter to decide whether a GPC segment should be included.
builder.WriteByte('~')
// By default, GPP is included.
builder.Write(sec.Encode(true))
}

Expand Down
262 changes: 134 additions & 128 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,32 @@ var testData = []gppEncodeTestData{
{
description: "USPCA GPP string encoding",
expected: "DBABh4A~BlgWEYCY.QA~BSFgmiU",
sections: []Section{uspca.USPCA{
CoreSegment: uspca.USPCACoreSegment{
Version: 1,
SaleOptOutNotice: 2,
SharingOptOutNotice: 1,
SensitiveDataLimitUseNotice: 1,
SaleOptOut: 2,
SharingOptOut: 0,
SensitiveDataProcessing: []byte{
0, 1, 1, 2, 0, 1, 0, 1, 2,
sections: []Section{
uspca.USPCA{
CoreSegment: uspca.USPCACoreSegment{
Version: 1,
SaleOptOutNotice: 2,
SharingOptOutNotice: 1,
SensitiveDataLimitUseNotice: 1,
SaleOptOut: 2,
SharingOptOut: 0,
SensitiveDataProcessing: []byte{
0, 1, 1, 2, 0, 1, 0, 1, 2,
},
KnownChildSensitiveDataConsents: []byte{
0, 0,
},
PersonalDataConsents: 0,
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 2,
},
KnownChildSensitiveDataConsents: []byte{
0, 0,
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: false,
},
PersonalDataConsents: 0,
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 2,
},
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: false,
},
SectionID: constants.SectionUSPCA,
Value: "BlgWEYCY.QA"},
SectionID: constants.SectionUSPCA,
Value: "BlgWEYCY.QA"},
uspva.USPVA{
CoreSegment: sections.CommonUSCoreSegment{
Version: 1,
Expand All @@ -71,138 +72,143 @@ var testData = []gppEncodeTestData{
{
description: "USPVA GPP string encoding",
expected: "DBABRg~bSFgmiU",
sections: []Section{uspva.USPVA{
CoreSegment: sections.CommonUSCoreSegment{
Version: 27,
SharingNotice: 1,
SaleOptOutNotice: 0,
TargetedAdvertisingOptOutNotice: 2,
SaleOptOut: 0,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2, 2,
sections: []Section{
uspva.USPVA{
CoreSegment: sections.CommonUSCoreSegment{
Version: 27,
SharingNotice: 1,
SaleOptOutNotice: 0,
TargetedAdvertisingOptOutNotice: 2,
SaleOptOut: 0,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2, 2,
},
KnownChildSensitiveDataConsents: []byte{0},
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
KnownChildSensitiveDataConsents: []byte{0},
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
SectionID: constants.SectionUSPVA,
Value: "bSFgmiU"},
SectionID: constants.SectionUSPVA,
Value: "bSFgmiU"},
},
},
{
description: "USPCO GPP string encoding",
expected: "DBABJg~bSFgmJQ.YA",
sections: []Section{uspco.USPCO{
CoreSegment: sections.CommonUSCoreSegment{
Version: 27,
SharingNotice: 1,
SaleOptOutNotice: 0,
TargetedAdvertisingOptOutNotice: 2,
SaleOptOut: 0,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2,
sections: []Section{
uspco.USPCO{
CoreSegment: sections.CommonUSCoreSegment{
Version: 27,
SharingNotice: 1,
SaleOptOutNotice: 0,
TargetedAdvertisingOptOutNotice: 2,
SaleOptOut: 0,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2,
},
KnownChildSensitiveDataConsents: []byte{0},
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
KnownChildSensitiveDataConsents: []byte{0},
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: true,
},
SectionID: constants.SectionUSPCO,
Value: "bSFgmJQ.YA"},
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: true,
},
SectionID: constants.SectionUSPCO,
Value: "bSFgmJQ.YA"},
},
},
{
description: "USPCT GPP string encoding",
expected: "DBABVg~bSFgmSZQ.YA",
sections: []Section{uspct.USPCT{
CoreSegment: sections.CommonUSCoreSegment{
Version: 27,
SharingNotice: 1,
SaleOptOutNotice: 0,
TargetedAdvertisingOptOutNotice: 2,
SaleOptOut: 0,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2, 1,
sections: []Section{
uspct.USPCT{
CoreSegment: sections.CommonUSCoreSegment{
Version: 27,
SharingNotice: 1,
SaleOptOutNotice: 0,
TargetedAdvertisingOptOutNotice: 2,
SaleOptOut: 0,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2, 1,
},
KnownChildSensitiveDataConsents: []byte{
0, 2, 1,
},
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
KnownChildSensitiveDataConsents: []byte{
0, 2, 1,
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: true,
},
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: true,
},
SectionID: constants.SectionUSPCT,
Value: "bSFgmSZQ.YA"},
SectionID: constants.SectionUSPCT,
Value: "bSFgmSZQ.YA"},
},
},
{
description: "USPNAT GPP string encoding",
expected: "DBABLA~DSJgmkoZJSA.YA",
sections: []Section{uspnat.USPNAT{
CoreSegment: uspnat.USPNATCoreSegment{
Version: 3,
SharingNotice: 1,
SaleOptOutNotice: 0,
SharingOptOutNotice: 2,
TargetedAdvertisingOptOutNotice: 0,
SensitiveDataProcessingOptOutNotice: 2,
SensitiveDataLimitUseNotice: 1,
SaleOptOut: 2,
SharingOptOut: 0,
TargetedAdvertisingOptOut: 0,
SensitiveDataProcessing: []byte{
2, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 1,
sections: []Section{
uspnat.USPNAT{
CoreSegment: uspnat.USPNATCoreSegment{
Version: 3,
SharingNotice: 1,
SaleOptOutNotice: 0,
SharingOptOutNotice: 2,
TargetedAdvertisingOptOutNotice: 0,
SensitiveDataProcessingOptOutNotice: 2,
SensitiveDataLimitUseNotice: 1,
SaleOptOut: 2,
SharingOptOut: 0,
TargetedAdvertisingOptOut: 0,
SensitiveDataProcessing: []byte{
2, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 1,
},
KnownChildSensitiveDataConsents: []byte{
0, 2,
},
PersonalDataConsents: 1,
MspaCoveredTransaction: 1,
MspaOptOutOptionMode: 0,
MspaServiceProviderMode: 2,
},
KnownChildSensitiveDataConsents: []byte{
0, 2,
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: true,
},
PersonalDataConsents: 1,
MspaCoveredTransaction: 1,
MspaOptOutOptionMode: 0,
MspaServiceProviderMode: 2,
},
GPCSegment: sections.CommonUSGPCSegment{
SubsectionType: 1,
Gpc: true,
},
SectionID: constants.SectionUSPNAT,
Value: "DSJgmkoZJSA.YA"},
SectionID: constants.SectionUSPNAT,
Value: "DSJgmkoZJSA.YA"},
},
},
{
description: "USPUT GPP string encoding",
expected: "DBADLO8~BSJgmkoZJSA.YA~BSFgmiU~BWJYJllA~BSFgmSZQ.YA",
sections: []Section{usput.USPUT{
CoreSegment: usput.USPUTCoreSegment{
Version: 1,
SharingNotice: 1,
SaleOptOutNotice: 1,
TargetedAdvertisingOptOutNotice: 2,
SensitiveDataProcessingOptOutNotice: 0,
SaleOptOut: 2,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2, 1,
sections: []Section{
usput.USPUT{
CoreSegment: usput.USPUTCoreSegment{
Version: 1,
SharingNotice: 1,
SaleOptOutNotice: 1,
TargetedAdvertisingOptOutNotice: 2,
SensitiveDataProcessingOptOutNotice: 0,
SaleOptOut: 2,
TargetedAdvertisingOptOut: 1,
SensitiveDataProcessing: []byte{
1, 2, 0, 0, 2, 1, 2, 1,
},
KnownChildSensitiveDataConsents: 1,
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
KnownChildSensitiveDataConsents: 1,
MspaCoveredTransaction: 2,
MspaOptOutOptionMode: 1,
MspaServiceProviderMode: 1,
},
SectionID: constants.SectionUSPUT,
Value: "BWJYJllA"},
SectionID: constants.SectionUSPUT,
Value: "BWJYJllA"},
uspnat.USPNAT{
CoreSegment: uspnat.USPNATCoreSegment{
Version: 1,
Expand Down
6 changes: 3 additions & 3 deletions util/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package util

import (
"encoding/base64"
"fmt"
"errors"
"unsafe"
)

var (
fibEncodeNumOutOfRangeErr = fmt.Errorf("the number to be encoded is out of range")
fibEncodeInvalidRange = fmt.Errorf("the range is invalid")
fibEncodeNumOutOfRangeErr = errors.New("the number to be encoded is out of range")
fibEncodeInvalidRange = errors.New("the range is invalid")
)

func getByteSlice() []byte {
Expand Down
Loading

0 comments on commit aef173f

Please sign in to comment.