Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add encode method to convert Sections into GPP String #9

Merged
merged 23 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@

# Dependency directories (remove the comment below to include it)
# vendor/

release/
.idea/
.vscode/
.DS_Store
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# go-gpp
Golang support for the IAB's GPP framework
Golang support for the IAB's GPP framework.

Provided the basic *Parse* and *Encode* methods for conversions between IAB-supported
sections and GPP strings, people other than CMPs are not encouraged to generate GPP strings
in production according to the IAB documentation.

For more information, please refer to https://github.com/InteractiveAdvertisingBureau/Global-Privacy-Platform
63 changes: 63 additions & 0 deletions encode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package gpp

import (
"errors"
"fmt"
"github.com/prebid/go-gpp/constants"
"github.com/prebid/go-gpp/util"
"sort"
"strings"
)

const (
// the first 6 bits of the header must always evaluate to the integer '3'.
gppType byte = 0x3
gppVersion byte = 0x1
)

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

func Encode(sections []Section) (string, error) {
bs := util.NewBitStreamForWrite()
builder := strings.Builder{}

bs.WriteByte6(gppType)
bs.WriteByte6(gppVersion)

sort.Slice(sections, func(i, j int) bool {
return sections[i].GetID() < sections[j].GetID()
})
// Generate int range object.
intRange := new(util.IntRange)
var prevID constants.SectionID = -1
for _, sec := range sections {
id := sec.GetID()
if id == prevID {
return "", duplicatedSectionErr
}
if prevID+1 == id {
intRange.Range[len(intRange.Range)-1].EndID = uint16(id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If id equals zero in the first loop of this for statement, we'll be referencing Range at position -1. The following corner case indeed makes Encode(sections []Section) panic:

 16   type gppEncodeTestData struct {
 17       description string
 18       sections    []Section
 19       expected    string
 20   }
 21  
    + type zeroSection struct{}
    + 
    + func (s zeroSection) Encode(b bool) []byte {
    +     return nil
    + }
    + 
    + func (s zeroSection) GetID() constants.SectionID {
    +     return constants.SectionID(0)
    + }
    + 
    + func (s zeroSection) GetValue() string {
    +     return "string"
    + }
    + 
 22   var testData = []gppEncodeTestData{
    +     {
    +         description: "Corner case of a section whose GetID returns 0",
    +         sections:    []Section{zeroSection{}},
    +     },
 23       {
 24           description: "USPCA GPP string encoding",
 25           expected:    "DBABh4A~BlgWEYCY.QA~BSFgmiU",
 26           sections: []Section{
 27               uspca.USPCA{
 28                   CoreSegment: uspca.USPCACoreSegment{
 29                       Version:                     1,
 30                       SaleOptOutNotice:            2,
 31                       SharingOptOutNotice:         1,
 32                       SensitiveDataLimitUseNotice: 1,
 33 *--251 lines: SaleOptOut:                  2,-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
284       },
285   }
286  
287   func TestEncode(t *testing.T) {
288       for _, test := range testData {
289           result, err := Encode(test.sections)
encode_test.go

Can we add a check?

} else {
intRange.Range = append(intRange.Range, util.IRange{StartID: uint16(id), EndID: uint16(id)})
}
prevID = id
}
intRange.Size = uint16(len(intRange.Range))

err := bs.WriteIntRange(intRange)
if err != nil {
return "", fmt.Errorf("write int range error: %v", err)
}

builder.Write(bs.Base64Encode())

for _, sec := range sections {
builder.WriteByte('~')
// By default, GPP is included.
builder.Write(sec.Encode(true))
}

return builder.String(), nil
}
Loading