@@ -43,6 +43,7 @@ import (
43
43
"github.com/ethereum/go-ethereum/crypto"
44
44
"github.com/ethereum/go-ethereum/log"
45
45
"github.com/ethersphere/swarm/api"
46
+ "github.com/ethersphere/swarm/chunk"
46
47
"github.com/ethersphere/swarm/storage"
47
48
"github.com/ethersphere/swarm/storage/feed"
48
49
"github.com/ethersphere/swarm/storage/feed/lookup"
@@ -68,6 +69,125 @@ func newTestSigner() (*feed.GenericSigner, error) {
68
69
return feed .NewGenericSigner (privKey ), nil
69
70
}
70
71
72
+ // TestGetTag uploads a file, retrieves the tag using http GET and check if it matches
73
+ func TestGetTagUsingHash (t * testing.T ) {
74
+ srv := NewTestSwarmServer (t , serverFunc , nil , nil )
75
+ defer srv .Close ()
76
+
77
+ // upload a file
78
+ data := testutil .RandomBytes (1 , 10000 )
79
+ resp , err := http .Post (fmt .Sprintf ("%s/bzz-raw:/" , srv .URL ), "text/plain" , bytes .NewReader (data ))
80
+ if err != nil {
81
+ t .Fatal (err )
82
+ }
83
+ defer resp .Body .Close ()
84
+ if resp .StatusCode != http .StatusOK {
85
+ t .Fatalf ("err %s" , resp .Status )
86
+ }
87
+ rootHash , err := ioutil .ReadAll (resp .Body )
88
+ if err != nil {
89
+ t .Fatal (err )
90
+ }
91
+
92
+ // get the tag for the above upload using root hash of the file
93
+ getBzzURL := fmt .Sprintf ("%s/bzz-tag:/%s" , srv .URL , string (rootHash ))
94
+ getResp , err := http .Get (getBzzURL )
95
+ if err != nil {
96
+ t .Fatal (err )
97
+ }
98
+ defer getResp .Body .Close ()
99
+ if getResp .StatusCode != http .StatusOK {
100
+ t .Fatalf ("err %s" , getResp .Status )
101
+ }
102
+ retrievedData , err := ioutil .ReadAll (getResp .Body )
103
+ if err != nil {
104
+ t .Fatal (err )
105
+ }
106
+ tag := & chunk.Tag {}
107
+ err = json .Unmarshal (retrievedData , & tag )
108
+ if err != nil {
109
+ t .Fatal (err )
110
+ }
111
+
112
+ // check if the tag has valid values
113
+ rcvdAddress , err := hex .DecodeString (string (rootHash ))
114
+ if err != nil {
115
+ t .Fatal (err )
116
+ }
117
+ if ! bytes .Equal (tag .Address , rcvdAddress ) {
118
+ t .Fatalf ("retrieved address mismatch, expected %x, got %x" , string (rootHash ), tag .Address )
119
+ }
120
+
121
+ if tag .TotalCounter () != 4 {
122
+ t .Fatalf ("retrieved total tag count mismatch, expected %x, got %x" , 4 , tag .TotalCounter ())
123
+ }
124
+
125
+ if ! strings .HasPrefix (tag .Name , "unnamed_tag_" ) {
126
+ t .Fatalf ("retrieved name prefix mismatch, expected %x, got %x" , "unnamed_tag_" , tag .Name )
127
+ }
128
+
129
+ }
130
+
131
+ // TestGetTag uploads a file, retrieves the tag using http GET and check if it matches
132
+ func TestGetTagUsingTagId (t * testing.T ) {
133
+ srv := NewTestSwarmServer (t , serverFunc , nil , nil )
134
+ defer srv .Close ()
135
+
136
+ // upload a file
137
+ data := testutil .RandomBytes (1 , 10000 )
138
+ resp , err := http .Post (fmt .Sprintf ("%s/bzz-raw:/" , srv .URL ), "text/plain" , bytes .NewReader (data ))
139
+ if err != nil {
140
+ t .Fatal (err )
141
+ }
142
+ defer resp .Body .Close ()
143
+ if resp .StatusCode != http .StatusOK {
144
+ t .Fatalf ("err %s" , resp .Status )
145
+ }
146
+ rootHash , err := ioutil .ReadAll (resp .Body )
147
+ if err != nil {
148
+ t .Fatal (err )
149
+ }
150
+ tidString := resp .Header .Get (TagHeaderName )
151
+
152
+ // get the tag of the above upload using the tagId
153
+ getBzzURL := fmt .Sprintf ("%s/bzz-tag:/?Id=%s" , srv .URL , tidString )
154
+ getResp , err := http .Get (getBzzURL )
155
+ if err != nil {
156
+ t .Fatal (err )
157
+ }
158
+ defer getResp .Body .Close ()
159
+ if getResp .StatusCode != http .StatusOK {
160
+ t .Fatalf ("err %s" , getResp .Status )
161
+ }
162
+ retrievedData , err := ioutil .ReadAll (getResp .Body )
163
+ if err != nil {
164
+ t .Fatal (err )
165
+ }
166
+ tag := & chunk.Tag {}
167
+ err = json .Unmarshal (retrievedData , & tag )
168
+ if err != nil {
169
+ t .Fatal (err )
170
+ }
171
+
172
+ // check if the received tags has valid values
173
+ rcvdAddress , err := hex .DecodeString (string (rootHash ))
174
+ if err != nil {
175
+ t .Fatal (err )
176
+ }
177
+ if ! bytes .Equal (tag .Address , rcvdAddress ) {
178
+ t .Fatalf ("retrieved address mismatch, expected %x, got %x" , string (rootHash ), tag .Address )
179
+ }
180
+
181
+ if tag .TotalCounter () != 4 {
182
+ t .Fatalf ("retrieved total tag count mismatch, expected %x, got %x" , 4 , tag .TotalCounter ())
183
+ }
184
+
185
+ if ! strings .HasPrefix (tag .Name , "unnamed_tag_" ) {
186
+ t .Fatalf ("retrieved name prefix mismatch, expected %x, got %x" , "unnamed_tag_" , tag .Name )
187
+ }
188
+
189
+ }
190
+
71
191
// TestPinUnpinAPI function tests the pinning and unpinning through HTTP API.
72
192
// It does the following
73
193
// 1) upload a file
@@ -125,6 +245,7 @@ func TestPinUnpinAPI(t *testing.T) {
125
245
if len (listInfosUnpin ) != 0 {
126
246
t .Fatalf ("roothash is in list of pinned files" )
127
247
}
248
+
128
249
}
129
250
130
251
// Test the transparent resolving of feed updates with bzz:// scheme
@@ -814,7 +935,7 @@ func testBzzTar(encrypted bool, t *testing.T) {
814
935
t .Fatal (err )
815
936
}
816
937
req .Header .Add ("Content-Type" , "application/x-tar" )
817
- req .Header .Add (SwarmTagHeaderName , "test-upload" )
938
+ req .Header .Add (TagHeaderName , "test-upload" )
818
939
client := & http.Client {}
819
940
resp2 , err := client .Do (req )
820
941
if err != nil {
@@ -933,7 +1054,7 @@ func TestBzzCorrectTagEstimate(t *testing.T) {
933
1054
934
1055
req = req .WithContext (ctx )
935
1056
req .ContentLength = 1000000
936
- req .Header .Add (SwarmTagHeaderName , "1000000" )
1057
+ req .Header .Add (TagHeaderName , "1000000" )
937
1058
938
1059
go func () {
939
1060
for {
0 commit comments