-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for BootstrapToken changes
This commit refactors the MySQL tests, and puts some common code into the storage test library. In particular, we add a new dependency on "github.com/google/uuid" in order to generate gen4 UUIDs for test devices. This should make the MySQL tests pass even on a dirty database, and isolate storage tests a little bit more. The pgsql changes should also be refactored to use these changes, but I think that deserves another PR.
- Loading branch information
Calvin Lee
committed
Jan 12, 2023
1 parent
be1171e
commit 0287471
Showing
6 changed files
with
166 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io/ioutil" | ||
|
||
"github.com/google/uuid" | ||
"github.com/micromdm/nanomdm/mdm" | ||
"github.com/micromdm/nanomdm/storage" | ||
) | ||
|
||
type DeviceInterfaces interface { | ||
storage.CheckinStore | ||
} | ||
|
||
type Device struct { | ||
Uuid string | ||
} | ||
|
||
func NewDevice() Device { | ||
return Device{Uuid: uuid.NewString()} | ||
} | ||
|
||
func (d *Device) EnrollID() *mdm.EnrollID { | ||
return &mdm.EnrollID{Type: mdm.Device, ID: d.Uuid} | ||
} | ||
|
||
func loadAuthMsg() (*mdm.Authenticate, error) { | ||
b, err := ioutil.ReadFile("../../mdm/testdata/Authenticate.2.plist") | ||
if err != nil { | ||
return nil, err | ||
} | ||
r, err := mdm.DecodeCheckin(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
a, ok := r.(*mdm.Authenticate) | ||
if !ok { | ||
return nil, errors.New("not an Authenticate message") | ||
} | ||
return a, nil | ||
} | ||
|
||
func loadTokenMsg() (*mdm.TokenUpdate, error) { | ||
b, err := ioutil.ReadFile("../../mdm/testdata/TokenUpdate.2.plist") | ||
if err != nil { | ||
return nil, err | ||
} | ||
r, err := mdm.DecodeCheckin(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
a, ok := r.(*mdm.TokenUpdate) | ||
if !ok { | ||
return nil, errors.New("not a TokenUpdate message") | ||
} | ||
return a, nil | ||
} | ||
|
||
func (d *Device) newMdmReq() *mdm.Request { | ||
return &mdm.Request{ | ||
Context: context.Background(), | ||
EnrollID: &mdm.EnrollID{ | ||
Type: mdm.Device, | ||
ID: d.Uuid, | ||
}, | ||
} | ||
} | ||
|
||
func EnrollTestDevice(storage DeviceInterfaces) (Device, error) { | ||
d := NewDevice() | ||
authMsg, err := loadAuthMsg() | ||
if err != nil { | ||
return d, err | ||
} | ||
err = storage.StoreAuthenticate(d.newMdmReq(), authMsg) | ||
if err != nil { | ||
return d, err | ||
} | ||
tokenMsg, err := loadTokenMsg() | ||
if err != nil { | ||
return d, err | ||
} | ||
err = storage.StoreTokenUpdate(d.newMdmReq(), tokenMsg) | ||
if err != nil { | ||
return d, err | ||
} | ||
return d, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package mysql | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"testing" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
"github.com/micromdm/nanomdm/storage/internal/test" | ||
) | ||
|
||
func TestBSToken(t *testing.T) { | ||
if *flDSN == "" { | ||
t.Fatal("MySQL DSN flag not provided to test") | ||
} | ||
|
||
storage, err := New(WithDSN(*flDSN), WithDeleteCommands()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var d test.Device | ||
d, err = test.EnrollTestDevice(storage) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
t.Run("BSToken nil", func(t *testing.T) { | ||
tok, err := storage.RetrieveBootstrapToken(&mdm.Request{Context: ctx, EnrollID: d.EnrollID()}, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if tok != nil { | ||
t.Fatal("Token for new device was nonnull") | ||
} | ||
}) | ||
t.Run("BSToken set/get", func(t *testing.T) { | ||
data := []byte("test token") | ||
bsToken := mdm.BootstrapToken{BootstrapToken: make([]byte, base64.StdEncoding.EncodedLen(len(data)))} | ||
base64.StdEncoding.Encode(bsToken.BootstrapToken, data) | ||
testReq := &mdm.Request{Context: ctx, EnrollID: d.EnrollID()} | ||
err := storage.StoreBootstrapToken(testReq, &mdm.SetBootstrapToken{BootstrapToken: bsToken}) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
tok, err := storage.RetrieveBootstrapToken(testReq, nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if !bytes.Equal(bsToken.BootstrapToken, tok.BootstrapToken) { | ||
t.Fatalf("Bootstap tokens disequal after roundtrip: %v!=%v", bsToken, tok) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package mysql | ||
|
||
import "flag" | ||
|
||
var flDSN = flag.String("dsn", "", "DSN of test MySQL instance") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters