-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error on MySQL backend when bsToken null (#63)
- Loading branch information
Calvin Lee
authored
Feb 28, 2023
1 parent
c771e92
commit 0011b0f
Showing
6 changed files
with
172 additions
and
80 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,60 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package mysql | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/base64" | ||
"testing" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
) | ||
|
||
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 Device | ||
d, err = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package mysql | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io/ioutil" | ||
|
||
"github.com/micromdm/nanomdm/mdm" | ||
"github.com/micromdm/nanomdm/storage" | ||
) | ||
|
||
type DeviceInterfaces interface { | ||
storage.CheckinStore | ||
} | ||
|
||
type Device struct { | ||
UDID string | ||
} | ||
|
||
func (d *Device) EnrollID() *mdm.EnrollID { | ||
return &mdm.EnrollID{Type: mdm.Device, ID: d.UDID} | ||
} | ||
|
||
func loadAuthMsg() (*mdm.Authenticate, Device, error) { | ||
var d Device | ||
b, err := ioutil.ReadFile("../../mdm/testdata/Authenticate.2.plist") | ||
if err != nil { | ||
return nil, d, err | ||
} | ||
r, err := mdm.DecodeCheckin(b) | ||
if err != nil { | ||
return nil, d, err | ||
} | ||
a, ok := r.(*mdm.Authenticate) | ||
if !ok { | ||
return nil, d, errors.New("not an Authenticate message") | ||
} | ||
d = Device{UDID: a.UDID} | ||
return a, d, 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.UDID, | ||
}, | ||
} | ||
} | ||
|
||
func enrollTestDevice(storage DeviceInterfaces) (Device, error) { | ||
authMsg, d, 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