Skip to content

Commit 7c06c2f

Browse files
committed
Refinement for tests and coding style
1 parent 467a30a commit 7c06c2f

File tree

4 files changed

+451
-270
lines changed

4 files changed

+451
-270
lines changed

encode.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package gpp
22

33
import (
4+
"errors"
45
"fmt"
56
"github.com/prebid/go-gpp/constants"
67
"github.com/prebid/go-gpp/util"
@@ -15,7 +16,7 @@ const (
1516
)
1617

1718
var (
18-
duplicatedSectionErr = fmt.Errorf("duplicated sections")
19+
duplicatedSectionErr = errors.New("duplicated sections")
1920
)
2021

2122
func Encode(sections []Section) (string, error) {
@@ -53,8 +54,8 @@ func Encode(sections []Section) (string, error) {
5354
builder.Write(bs.Base64Encode())
5455

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

encode_test.go

+134-128
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,32 @@ var testData = []gppEncodeTestData{
2323
{
2424
description: "USPCA GPP string encoding",
2525
expected: "DBABh4A~BlgWEYCY.QA~BSFgmiU",
26-
sections: []Section{uspca.USPCA{
27-
CoreSegment: uspca.USPCACoreSegment{
28-
Version: 1,
29-
SaleOptOutNotice: 2,
30-
SharingOptOutNotice: 1,
31-
SensitiveDataLimitUseNotice: 1,
32-
SaleOptOut: 2,
33-
SharingOptOut: 0,
34-
SensitiveDataProcessing: []byte{
35-
0, 1, 1, 2, 0, 1, 0, 1, 2,
26+
sections: []Section{
27+
uspca.USPCA{
28+
CoreSegment: uspca.USPCACoreSegment{
29+
Version: 1,
30+
SaleOptOutNotice: 2,
31+
SharingOptOutNotice: 1,
32+
SensitiveDataLimitUseNotice: 1,
33+
SaleOptOut: 2,
34+
SharingOptOut: 0,
35+
SensitiveDataProcessing: []byte{
36+
0, 1, 1, 2, 0, 1, 0, 1, 2,
37+
},
38+
KnownChildSensitiveDataConsents: []byte{
39+
0, 0,
40+
},
41+
PersonalDataConsents: 0,
42+
MspaCoveredTransaction: 2,
43+
MspaOptOutOptionMode: 1,
44+
MspaServiceProviderMode: 2,
3645
},
37-
KnownChildSensitiveDataConsents: []byte{
38-
0, 0,
46+
GPCSegment: sections.CommonUSGPCSegment{
47+
SubsectionType: 1,
48+
Gpc: false,
3949
},
40-
PersonalDataConsents: 0,
41-
MspaCoveredTransaction: 2,
42-
MspaOptOutOptionMode: 1,
43-
MspaServiceProviderMode: 2,
44-
},
45-
GPCSegment: sections.CommonUSGPCSegment{
46-
SubsectionType: 1,
47-
Gpc: false,
48-
},
49-
SectionID: constants.SectionUSPCA,
50-
Value: "BlgWEYCY.QA"},
50+
SectionID: constants.SectionUSPCA,
51+
Value: "BlgWEYCY.QA"},
5152
uspva.USPVA{
5253
CoreSegment: sections.CommonUSCoreSegment{
5354
Version: 1,
@@ -71,138 +72,143 @@ var testData = []gppEncodeTestData{
7172
{
7273
description: "USPVA GPP string encoding",
7374
expected: "DBABRg~bSFgmiU",
74-
sections: []Section{uspva.USPVA{
75-
CoreSegment: sections.CommonUSCoreSegment{
76-
Version: 27,
77-
SharingNotice: 1,
78-
SaleOptOutNotice: 0,
79-
TargetedAdvertisingOptOutNotice: 2,
80-
SaleOptOut: 0,
81-
TargetedAdvertisingOptOut: 1,
82-
SensitiveDataProcessing: []byte{
83-
1, 2, 0, 0, 2, 1, 2, 2,
75+
sections: []Section{
76+
uspva.USPVA{
77+
CoreSegment: sections.CommonUSCoreSegment{
78+
Version: 27,
79+
SharingNotice: 1,
80+
SaleOptOutNotice: 0,
81+
TargetedAdvertisingOptOutNotice: 2,
82+
SaleOptOut: 0,
83+
TargetedAdvertisingOptOut: 1,
84+
SensitiveDataProcessing: []byte{
85+
1, 2, 0, 0, 2, 1, 2, 2,
86+
},
87+
KnownChildSensitiveDataConsents: []byte{0},
88+
MspaCoveredTransaction: 2,
89+
MspaOptOutOptionMode: 1,
90+
MspaServiceProviderMode: 1,
8491
},
85-
KnownChildSensitiveDataConsents: []byte{0},
86-
MspaCoveredTransaction: 2,
87-
MspaOptOutOptionMode: 1,
88-
MspaServiceProviderMode: 1,
89-
},
90-
SectionID: constants.SectionUSPVA,
91-
Value: "bSFgmiU"},
92+
SectionID: constants.SectionUSPVA,
93+
Value: "bSFgmiU"},
9294
},
9395
},
9496
{
9597
description: "USPCO GPP string encoding",
9698
expected: "DBABJg~bSFgmJQ.YA",
97-
sections: []Section{uspco.USPCO{
98-
CoreSegment: sections.CommonUSCoreSegment{
99-
Version: 27,
100-
SharingNotice: 1,
101-
SaleOptOutNotice: 0,
102-
TargetedAdvertisingOptOutNotice: 2,
103-
SaleOptOut: 0,
104-
TargetedAdvertisingOptOut: 1,
105-
SensitiveDataProcessing: []byte{
106-
1, 2, 0, 0, 2, 1, 2,
99+
sections: []Section{
100+
uspco.USPCO{
101+
CoreSegment: sections.CommonUSCoreSegment{
102+
Version: 27,
103+
SharingNotice: 1,
104+
SaleOptOutNotice: 0,
105+
TargetedAdvertisingOptOutNotice: 2,
106+
SaleOptOut: 0,
107+
TargetedAdvertisingOptOut: 1,
108+
SensitiveDataProcessing: []byte{
109+
1, 2, 0, 0, 2, 1, 2,
110+
},
111+
KnownChildSensitiveDataConsents: []byte{0},
112+
MspaCoveredTransaction: 2,
113+
MspaOptOutOptionMode: 1,
114+
MspaServiceProviderMode: 1,
107115
},
108-
KnownChildSensitiveDataConsents: []byte{0},
109-
MspaCoveredTransaction: 2,
110-
MspaOptOutOptionMode: 1,
111-
MspaServiceProviderMode: 1,
112-
},
113-
GPCSegment: sections.CommonUSGPCSegment{
114-
SubsectionType: 1,
115-
Gpc: true,
116-
},
117-
SectionID: constants.SectionUSPCO,
118-
Value: "bSFgmJQ.YA"},
116+
GPCSegment: sections.CommonUSGPCSegment{
117+
SubsectionType: 1,
118+
Gpc: true,
119+
},
120+
SectionID: constants.SectionUSPCO,
121+
Value: "bSFgmJQ.YA"},
119122
},
120123
},
121124
{
122125
description: "USPCT GPP string encoding",
123126
expected: "DBABVg~bSFgmSZQ.YA",
124-
sections: []Section{uspct.USPCT{
125-
CoreSegment: sections.CommonUSCoreSegment{
126-
Version: 27,
127-
SharingNotice: 1,
128-
SaleOptOutNotice: 0,
129-
TargetedAdvertisingOptOutNotice: 2,
130-
SaleOptOut: 0,
131-
TargetedAdvertisingOptOut: 1,
132-
SensitiveDataProcessing: []byte{
133-
1, 2, 0, 0, 2, 1, 2, 1,
127+
sections: []Section{
128+
uspct.USPCT{
129+
CoreSegment: sections.CommonUSCoreSegment{
130+
Version: 27,
131+
SharingNotice: 1,
132+
SaleOptOutNotice: 0,
133+
TargetedAdvertisingOptOutNotice: 2,
134+
SaleOptOut: 0,
135+
TargetedAdvertisingOptOut: 1,
136+
SensitiveDataProcessing: []byte{
137+
1, 2, 0, 0, 2, 1, 2, 1,
138+
},
139+
KnownChildSensitiveDataConsents: []byte{
140+
0, 2, 1,
141+
},
142+
MspaCoveredTransaction: 2,
143+
MspaOptOutOptionMode: 1,
144+
MspaServiceProviderMode: 1,
134145
},
135-
KnownChildSensitiveDataConsents: []byte{
136-
0, 2, 1,
146+
GPCSegment: sections.CommonUSGPCSegment{
147+
SubsectionType: 1,
148+
Gpc: true,
137149
},
138-
MspaCoveredTransaction: 2,
139-
MspaOptOutOptionMode: 1,
140-
MspaServiceProviderMode: 1,
141-
},
142-
GPCSegment: sections.CommonUSGPCSegment{
143-
SubsectionType: 1,
144-
Gpc: true,
145-
},
146-
SectionID: constants.SectionUSPCT,
147-
Value: "bSFgmSZQ.YA"},
150+
SectionID: constants.SectionUSPCT,
151+
Value: "bSFgmSZQ.YA"},
148152
},
149153
},
150154
{
151155
description: "USPNAT GPP string encoding",
152156
expected: "DBABLA~DSJgmkoZJSA.YA",
153-
sections: []Section{uspnat.USPNAT{
154-
CoreSegment: uspnat.USPNATCoreSegment{
155-
Version: 3,
156-
SharingNotice: 1,
157-
SaleOptOutNotice: 0,
158-
SharingOptOutNotice: 2,
159-
TargetedAdvertisingOptOutNotice: 0,
160-
SensitiveDataProcessingOptOutNotice: 2,
161-
SensitiveDataLimitUseNotice: 1,
162-
SaleOptOut: 2,
163-
SharingOptOut: 0,
164-
TargetedAdvertisingOptOut: 0,
165-
SensitiveDataProcessing: []byte{
166-
2, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 1,
157+
sections: []Section{
158+
uspnat.USPNAT{
159+
CoreSegment: uspnat.USPNATCoreSegment{
160+
Version: 3,
161+
SharingNotice: 1,
162+
SaleOptOutNotice: 0,
163+
SharingOptOutNotice: 2,
164+
TargetedAdvertisingOptOutNotice: 0,
165+
SensitiveDataProcessingOptOutNotice: 2,
166+
SensitiveDataLimitUseNotice: 1,
167+
SaleOptOut: 2,
168+
SharingOptOut: 0,
169+
TargetedAdvertisingOptOut: 0,
170+
SensitiveDataProcessing: []byte{
171+
2, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 1,
172+
},
173+
KnownChildSensitiveDataConsents: []byte{
174+
0, 2,
175+
},
176+
PersonalDataConsents: 1,
177+
MspaCoveredTransaction: 1,
178+
MspaOptOutOptionMode: 0,
179+
MspaServiceProviderMode: 2,
167180
},
168-
KnownChildSensitiveDataConsents: []byte{
169-
0, 2,
181+
GPCSegment: sections.CommonUSGPCSegment{
182+
SubsectionType: 1,
183+
Gpc: true,
170184
},
171-
PersonalDataConsents: 1,
172-
MspaCoveredTransaction: 1,
173-
MspaOptOutOptionMode: 0,
174-
MspaServiceProviderMode: 2,
175-
},
176-
GPCSegment: sections.CommonUSGPCSegment{
177-
SubsectionType: 1,
178-
Gpc: true,
179-
},
180-
SectionID: constants.SectionUSPNAT,
181-
Value: "DSJgmkoZJSA.YA"},
185+
SectionID: constants.SectionUSPNAT,
186+
Value: "DSJgmkoZJSA.YA"},
182187
},
183188
},
184189
{
185190
description: "USPUT GPP string encoding",
186191
expected: "DBADLO8~BSJgmkoZJSA.YA~BSFgmiU~BWJYJllA~BSFgmSZQ.YA",
187-
sections: []Section{usput.USPUT{
188-
CoreSegment: usput.USPUTCoreSegment{
189-
Version: 1,
190-
SharingNotice: 1,
191-
SaleOptOutNotice: 1,
192-
TargetedAdvertisingOptOutNotice: 2,
193-
SensitiveDataProcessingOptOutNotice: 0,
194-
SaleOptOut: 2,
195-
TargetedAdvertisingOptOut: 1,
196-
SensitiveDataProcessing: []byte{
197-
1, 2, 0, 0, 2, 1, 2, 1,
192+
sections: []Section{
193+
usput.USPUT{
194+
CoreSegment: usput.USPUTCoreSegment{
195+
Version: 1,
196+
SharingNotice: 1,
197+
SaleOptOutNotice: 1,
198+
TargetedAdvertisingOptOutNotice: 2,
199+
SensitiveDataProcessingOptOutNotice: 0,
200+
SaleOptOut: 2,
201+
TargetedAdvertisingOptOut: 1,
202+
SensitiveDataProcessing: []byte{
203+
1, 2, 0, 0, 2, 1, 2, 1,
204+
},
205+
KnownChildSensitiveDataConsents: 1,
206+
MspaCoveredTransaction: 2,
207+
MspaOptOutOptionMode: 1,
208+
MspaServiceProviderMode: 1,
198209
},
199-
KnownChildSensitiveDataConsents: 1,
200-
MspaCoveredTransaction: 2,
201-
MspaOptOutOptionMode: 1,
202-
MspaServiceProviderMode: 1,
203-
},
204-
SectionID: constants.SectionUSPUT,
205-
Value: "BWJYJllA"},
210+
SectionID: constants.SectionUSPUT,
211+
Value: "BWJYJllA"},
206212
uspnat.USPNAT{
207213
CoreSegment: uspnat.USPNATCoreSegment{
208214
Version: 1,

util/encoding.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package util
22

33
import (
44
"encoding/base64"
5-
"fmt"
5+
"errors"
66
"unsafe"
77
)
88

99
var (
10-
fibEncodeNumOutOfRangeErr = fmt.Errorf("the number to be encoded is out of range")
11-
fibEncodeInvalidRange = fmt.Errorf("the range is invalid")
10+
fibEncodeNumOutOfRangeErr = errors.New("the number to be encoded is out of range")
11+
fibEncodeInvalidRange = errors.New("the range is invalid")
1212
)
1313

1414
func getByteSlice() []byte {

0 commit comments

Comments
 (0)