-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
message_test.go
111 lines (94 loc) · 3.39 KB
/
message_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/***
Copyright (c) 2018, Hector Sanjuan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
package ndef
import (
"bytes"
"fmt"
"testing"
"github.com/hsanjuan/go-ndef/types/wkt/uri"
)
func ExampleMessage() {
// Here we create an NDEF Message of type "U" (URI).
payload := uri.New("https://github.com/hsanjuan/go-ndef")
ndefMessage := NewMessage(NFCForumWellKnownType, "U", "", payload)
fmt.Println(ndefMessage)
// Output:
// urn:nfc:wkt:U:https://github.com/hsanjuan/go-ndef
}
func ExampleSmartPoster() {
// Here we create a SmartPoster Message with a title and a URL.
// A SmartPoster wraps an NDEF Message with several records.
title := NewTextRecord("The title", "en")
url := NewURIRecord("https://github.com/hsanjuan/go-ndef")
posterPayload := NewMessageFromRecords(title, url)
poster := NewSmartPosterMessage(posterPayload)
fmt.Println(poster)
// Output:
// urn:nfc:wkt:Sp:
// urn:nfc:wkt:T:The title
// urn:nfc:wkt:U:https://github.com/hsanjuan/go-ndef
}
func ExampleMessage_Unmarshal() {
ndefMessageBytes := []byte{0xd1, 0x01, 0x23, 0x54, 0x02, 0x65, 0x6e,
0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x61, 0x20,
0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x6f, 0x66,
0x20, 0x54, 0x5b, 0x65, 0x78, 0x74, 0x5d, 0x20, 0x74, 0x79,
0x70, 0x65}
ndefMessage := &Message{} // Create uninitialized message
_, err := ndefMessage.Unmarshal(ndefMessageBytes) // Parse bytes into it
if err != nil { // Your bytes don't look good
fmt.Println(err)
return
}
fmt.Println(ndefMessage) // Print the contents of every record
// Output:
// urn:nfc:wkt:T:This is a message of T[ext] type
}
func TestInspect(t *testing.T) {
ndefMessage := NewMediaMessage("text/plain", []byte("abc"))
t.Log(ndefMessage.Inspect())
}
func TestTypes(t *testing.T) {
ndefMessage := NewTextMessage("abc", "en_US")
t.Log(ndefMessage)
ndefMessage = NewURIMessage("http://a.b")
t.Log(ndefMessage)
ndefMessage = NewMediaMessage("text/json", []byte(`{ "a" : 3 }`))
t.Log(ndefMessage)
ndefMessage = NewAbsoluteURIMessage("http://a.b", []byte("payload"))
t.Log(ndefMessage)
ndefMessage = NewExternalMessage("exttype", []byte("payload"))
t.Log(ndefMessage)
ndefMessage2 := NewSmartPosterMessage(ndefMessage)
t.Log(ndefMessage2)
}
func TestMarhsal(t *testing.T) {
m := NewURIMessage("http://s.com")
mBytes, err := m.Marshal()
if err != nil {
t.Log(err)
t.FailNow()
}
expectedBytes := []byte{
0xd1, //Flags+TNF
0x01, // TypeLen
0x06, // Payload Len
0x55, // Type
0x03, // URI code
0x73, 0x2e, 0x63, 0x6f, 0x6d, // URI
}
if !bytes.Equal(mBytes, expectedBytes) {
t.Error("Unexpected marshal result")
}
}