Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
stboot
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
glasklar
infra
stboot
Commits
fcc9d9b5
Commit
fcc9d9b5
authored
2 years ago
by
Jens Drenhaus
Browse files
Options
Downloads
Patches
Plain Diff
Add trust.NewPolicy()
Signed-off-by:
Jens Drenhaus
<
jens.drenhaus@9elements.com
>
parent
2cae1098
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
trust/policy.go
+14
-0
14 additions, 0 deletions
trust/policy.go
trust/policy_test.go
+87
-0
87 additions, 0 deletions
trust/policy_test.go
with
101 additions
and
0 deletions
trust/policy.go
+
14
−
0
View file @
fcc9d9b5
...
...
@@ -16,6 +16,20 @@ type Policy struct {
FetchMethod
ospkg
.
FetchMethod
`json:"ospkg_fetch_method"`
}
// NewPolicy creates a Policy from template.
// If the template is not a valid Policy, the returned error wrapps ErrInvalidPolicy.
func
NewPolicy
(
template
Policy
)
(
Policy
,
error
)
{
var
ret
Policy
if
err
:=
template
.
validate
();
err
!=
nil
{
return
ret
,
fmt
.
Errorf
(
"%w: %v"
,
ErrInvalidPolicy
,
err
)
}
ret
.
SignatureThreshold
=
template
.
SignatureThreshold
ret
.
FetchMethod
=
template
.
FetchMethod
return
ret
,
nil
}
// policy is used as an alias in Policy.UnmarshalJSON.
type
policy
struct
{
SignatureThreshold
int
`json:"ospkg_signature_threshold"`
...
...
This diff is collapsed.
Click to expand it.
trust/policy_test.go
+
87
−
0
View file @
fcc9d9b5
...
...
@@ -9,6 +9,93 @@ import (
"system-transparency.org/stboot/ospkg"
)
func
TestPolicyNew
(
t
*
testing
.
T
)
{
validtests
:=
[]
struct
{
name
string
template
Policy
want
Policy
}{
{
name
:
"All set"
,
template
:
Policy
{
SignatureThreshold
:
1
,
FetchMethod
:
ospkg
.
FetchFromNetwork
,
},
want
:
Policy
{
SignatureThreshold
:
1
,
FetchMethod
:
ospkg
.
FetchFromNetwork
,
},
},
}
invalidtests
:=
[]
struct
{
name
string
template
Policy
}{
{
name
:
"Empty Policy"
,
template
:
Policy
{},
},
{
name
:
"SignaturesThreshold missing"
,
template
:
Policy
{
FetchMethod
:
ospkg
.
FetchFromNetwork
,
},
},
{
name
:
"SignaturesThreshold 0"
,
template
:
Policy
{
SignatureThreshold
:
0
,
FetchMethod
:
ospkg
.
FetchFromNetwork
,
},
},
{
name
:
"SignaturesThreshold negative"
,
template
:
Policy
{
SignatureThreshold
:
-
1
,
FetchMethod
:
ospkg
.
FetchFromNetwork
,
},
},
{
name
:
"FetchMethod missing"
,
template
:
Policy
{
SignatureThreshold
:
1
,
},
},
{
name
:
"FetchMethod unknown"
,
template
:
Policy
{
SignatureThreshold
:
1
,
FetchMethod
:
100
,
},
},
}
for
_
,
tt
:=
range
validtests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
got
,
err
:=
NewPolicy
(
tt
.
template
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error: %v"
,
err
)
}
if
!
reflect
.
DeepEqual
(
got
,
tt
.
want
)
{
t
.
Errorf
(
"got %+v, want %+v"
,
got
,
tt
.
want
)
}
})
}
for
_
,
tt
:=
range
invalidtests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
_
,
err
:=
NewPolicy
(
tt
.
template
)
if
err
==
nil
{
t
.
Fatalf
(
"expect an error"
)
}
if
!
errors
.
Is
(
err
,
ErrInvalidPolicy
)
{
t
.
Errorf
(
"expect error to wrap ErrInvalidPolicy"
)
}
})
}
}
func
TestPolicyUnmarshalJSON
(
t
*
testing
.
T
)
{
validtests
:=
[]
struct
{
name
string
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment