Created
April 26, 2020 20:02
-
-
Save ivajloip/d63981e4caddaa68bd0b9c2390f4af90 to your computer and use it in GitHub Desktop.
Lorawan bench code
This file contains hidden or 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
package lorawan | |
import ( | |
"bytes" | |
"testing" | |
) | |
func BenchmarkDecode(b *testing.B) { | |
data := []byte{0x40, 0x04, 0x03, 0x02, 0x01, 0x80, 0x01, 0x00, 0x01, 0xa6, 0x94, 0x64, 0x26, 0x15, 0xd6, 0xc3, 0xb5, 0x82} | |
b.ResetTimer() | |
for n := 0; n < b.N*1000; n++ { | |
phy := PHYPayload{} | |
err := phy.UnmarshalBinary(data) | |
if err != nil { | |
b.FailNow() | |
} | |
macPL, ok := phy.MACPayload.(*MACPayload) | |
if !ok { | |
b.FailNow() | |
} | |
if macPL.FHDR.FCnt != 1 { | |
b.FailNow() | |
} | |
if len(macPL.FHDR.FOpts) != 0 { | |
b.FailNow() | |
} | |
} | |
} | |
func BenchmarkValidateMic(b *testing.B) { | |
nwkSKey := AES128Key{2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2} | |
data := []byte{0x40, 0x04, 0x03, 0x02, 0x01, 0x80, 0x01, 0x00, 0x01, 0xa6, 0x94, 0x64, 0x26, 0x15, 0xd6, 0xc3, 0xb5, 0x82} | |
b.ResetTimer() | |
for n := 0; n < b.N*1000; n++ { | |
phy := PHYPayload{} | |
err := phy.UnmarshalBinary(data) | |
if err != nil { | |
b.FailNow() | |
} | |
ok, err := phy.ValidateUplinkDataMIC(LoRaWAN1_0, 1, 0, 0, nwkSKey, AES128Key{}) | |
if err != nil || !ok { | |
b.FailNow() | |
} | |
} | |
} | |
func BenchmarkDecrypt(b *testing.B) { | |
appSKey := AES128Key{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} | |
data := []byte{0x40, 0x04, 0x03, 0x02, 0x01, 0x80, 0x01, 0x00, 0x01, 0xa6, 0x94, 0x64, 0x26, 0x15, 0xd6, 0xc3, 0xb5, 0x82} | |
b.ResetTimer() | |
for n := 0; n < b.N*1000; n++ { | |
phy := PHYPayload{} | |
err := phy.UnmarshalBinary(data) | |
if err != nil { | |
b.FailNow() | |
} | |
err = phy.DecryptFRMPayload(appSKey) | |
if err != nil { | |
b.FailNow() | |
} | |
macPL, ok := phy.MACPayload.(*MACPayload) | |
if !ok { | |
b.FailNow() | |
} | |
pl, ok := macPL.FRMPayload[0].(*DataPayload) | |
if !ok { | |
b.FailNow() | |
} | |
if !bytes.Equal(pl.Bytes, []byte("hello")) { | |
b.FailNow() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment