You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

426 lines
10 KiB

  1. package s3api
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
  5. "net/http"
  6. "reflect"
  7. "sort"
  8. "strings"
  9. "testing"
  10. )
  11. type H map[string]string
  12. func (h H) String() string {
  13. pairs := make([]string, 0, len(h))
  14. for k, v := range h {
  15. pairs = append(pairs, fmt.Sprintf("%s : %s", k, v))
  16. }
  17. sort.Strings(pairs)
  18. join := strings.Join(pairs, "\n")
  19. return "\n" + join + "\n"
  20. }
  21. var processMetadataTestCases = []struct {
  22. caseId int
  23. request H
  24. existing H
  25. getTags H
  26. want H
  27. }{
  28. {
  29. 201,
  30. H{
  31. "User-Agent": "firefox",
  32. "X-Amz-Meta-My-Meta": "request",
  33. "X-Amz-Tagging": "A=B&a=b&type=request",
  34. },
  35. H{
  36. "X-Amz-Meta-My-Meta": "existing",
  37. "X-Amz-Tagging-A": "B",
  38. "X-Amz-Tagging-Type": "existing",
  39. },
  40. H{
  41. "A": "B",
  42. "a": "b",
  43. "type": "existing",
  44. },
  45. H{
  46. "User-Agent": "firefox",
  47. "X-Amz-Meta-My-Meta": "existing",
  48. "X-Amz-Tagging": "A=B&a=b&type=existing",
  49. },
  50. },
  51. {
  52. 202,
  53. H{
  54. "User-Agent": "firefox",
  55. "X-Amz-Meta-My-Meta": "request",
  56. "X-Amz-Tagging": "A=B&a=b&type=request",
  57. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  58. },
  59. H{
  60. "X-Amz-Meta-My-Meta": "existing",
  61. "X-Amz-Tagging-A": "B",
  62. "X-Amz-Tagging-Type": "existing",
  63. },
  64. H{
  65. "A": "B",
  66. "a": "b",
  67. "type": "existing",
  68. },
  69. H{
  70. "User-Agent": "firefox",
  71. "X-Amz-Meta-My-Meta": "request",
  72. "X-Amz-Tagging": "A=B&a=b&type=existing",
  73. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  74. },
  75. },
  76. {
  77. 203,
  78. H{
  79. "User-Agent": "firefox",
  80. "X-Amz-Meta-My-Meta": "request",
  81. "X-Amz-Tagging": "A=B&a=b&type=request",
  82. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  83. },
  84. H{
  85. "X-Amz-Meta-My-Meta": "existing",
  86. "X-Amz-Tagging-A": "B",
  87. "X-Amz-Tagging-Type": "existing",
  88. },
  89. H{
  90. "A": "B",
  91. "a": "b",
  92. "type": "existing",
  93. },
  94. H{
  95. "User-Agent": "firefox",
  96. "X-Amz-Meta-My-Meta": "existing",
  97. "X-Amz-Tagging": "A=B&a=b&type=request",
  98. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  99. },
  100. },
  101. {
  102. 204,
  103. H{
  104. "User-Agent": "firefox",
  105. "X-Amz-Meta-My-Meta": "request",
  106. "X-Amz-Tagging": "A=B&a=b&type=request",
  107. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  108. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  109. },
  110. H{
  111. "X-Amz-Meta-My-Meta": "existing",
  112. "X-Amz-Tagging-A": "B",
  113. "X-Amz-Tagging-a": "b",
  114. "X-Amz-Tagging-Type": "existing",
  115. },
  116. H{
  117. "A": "B",
  118. "a": "b",
  119. "type": "existing",
  120. },
  121. H{
  122. "User-Agent": "firefox",
  123. "X-Amz-Meta-My-Meta": "request",
  124. "X-Amz-Tagging": "A=B&a=b&type=request",
  125. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  126. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  127. },
  128. },
  129. {
  130. 205,
  131. H{
  132. "User-Agent": "firefox",
  133. "X-Amz-Meta-My-Meta": "request",
  134. "X-Amz-Tagging": "A=B&a=b&type=request",
  135. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  136. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  137. },
  138. H{},
  139. H{},
  140. H{
  141. "User-Agent": "firefox",
  142. "X-Amz-Meta-My-Meta": "request",
  143. "X-Amz-Tagging": "A=B&a=b&type=request",
  144. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  145. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  146. },
  147. },
  148. {
  149. 206,
  150. H{
  151. "User-Agent": "firefox",
  152. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  153. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  154. },
  155. H{
  156. "X-Amz-Meta-My-Meta": "existing",
  157. "X-Amz-Tagging-A": "B",
  158. "X-Amz-Tagging-a": "b",
  159. "X-Amz-Tagging-Type": "existing",
  160. },
  161. H{
  162. "A": "B",
  163. "a": "b",
  164. "type": "existing",
  165. },
  166. H{
  167. "User-Agent": "firefox",
  168. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  169. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  170. },
  171. },
  172. {
  173. 207,
  174. H{
  175. "User-Agent": "firefox",
  176. "X-Amz-Meta-My-Meta": "request",
  177. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  178. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  179. },
  180. H{
  181. "X-Amz-Meta-My-Meta": "existing",
  182. "X-Amz-Tagging-A": "B",
  183. "X-Amz-Tagging-a": "b",
  184. "X-Amz-Tagging-Type": "existing",
  185. },
  186. H{
  187. "A": "B",
  188. "a": "b",
  189. "type": "existing",
  190. },
  191. H{
  192. "User-Agent": "firefox",
  193. "X-Amz-Meta-My-Meta": "request",
  194. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  195. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  196. },
  197. },
  198. }
  199. var processMetadataBytesTestCases = []struct {
  200. caseId int
  201. request H
  202. existing H
  203. want H
  204. }{
  205. {
  206. 101,
  207. H{
  208. "User-Agent": "firefox",
  209. "X-Amz-Meta-My-Meta": "request",
  210. "X-Amz-Tagging": "A=B&a=b&type=request",
  211. },
  212. H{
  213. "X-Amz-Meta-My-Meta": "existing",
  214. "X-Amz-Tagging-A": "B",
  215. "X-Amz-Tagging-a": "b",
  216. "X-Amz-Tagging-type": "existing",
  217. },
  218. H{
  219. "X-Amz-Meta-My-Meta": "existing",
  220. "X-Amz-Tagging-A": "B",
  221. "X-Amz-Tagging-a": "b",
  222. "X-Amz-Tagging-type": "existing",
  223. },
  224. },
  225. {
  226. 102,
  227. H{
  228. "User-Agent": "firefox",
  229. "X-Amz-Meta-My-Meta": "request",
  230. "X-Amz-Tagging": "A=B&a=b&type=request",
  231. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  232. },
  233. H{
  234. "X-Amz-Meta-My-Meta": "existing",
  235. "X-Amz-Tagging-A": "B",
  236. "X-Amz-Tagging-a": "b",
  237. "X-Amz-Tagging-type": "existing",
  238. },
  239. H{
  240. "X-Amz-Meta-My-Meta": "request",
  241. "X-Amz-Tagging-A": "B",
  242. "X-Amz-Tagging-a": "b",
  243. "X-Amz-Tagging-type": "existing",
  244. },
  245. },
  246. {
  247. 103,
  248. H{
  249. "User-Agent": "firefox",
  250. "X-Amz-Meta-My-Meta": "request",
  251. "X-Amz-Tagging": "A=B&a=b&type=request",
  252. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  253. },
  254. H{
  255. "X-Amz-Meta-My-Meta": "existing",
  256. "X-Amz-Tagging-A": "B",
  257. "X-Amz-Tagging-a": "b",
  258. "X-Amz-Tagging-type": "existing",
  259. },
  260. H{
  261. "X-Amz-Meta-My-Meta": "existing",
  262. "X-Amz-Tagging-A": "B",
  263. "X-Amz-Tagging-a": "b",
  264. "X-Amz-Tagging-type": "request",
  265. },
  266. },
  267. {
  268. 104,
  269. H{
  270. "User-Agent": "firefox",
  271. "X-Amz-Meta-My-Meta": "request",
  272. "X-Amz-Tagging": "A=B&a=b&type=request",
  273. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  274. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  275. },
  276. H{
  277. "X-Amz-Meta-My-Meta": "existing",
  278. "X-Amz-Tagging-A": "B",
  279. "X-Amz-Tagging-a": "b",
  280. "X-Amz-Tagging-type": "existing",
  281. },
  282. H{
  283. "X-Amz-Meta-My-Meta": "request",
  284. "X-Amz-Tagging-A": "B",
  285. "X-Amz-Tagging-a": "b",
  286. "X-Amz-Tagging-type": "request",
  287. },
  288. },
  289. {
  290. 105,
  291. H{
  292. "User-Agent": "firefox",
  293. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  294. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  295. },
  296. H{
  297. "X-Amz-Meta-My-Meta": "existing",
  298. "X-Amz-Tagging-A": "B",
  299. "X-Amz-Tagging-a": "b",
  300. "X-Amz-Tagging-type": "existing",
  301. },
  302. H{},
  303. },
  304. {
  305. 107,
  306. H{
  307. "User-Agent": "firefox",
  308. "X-Amz-Meta-My-Meta": "request",
  309. "X-Amz-Tagging": "A=B&a=b&type=request",
  310. s3_constants.AmzUserMetaDirective: DirectiveReplace,
  311. s3_constants.AmzObjectTaggingDirective: DirectiveReplace,
  312. },
  313. H{},
  314. H{
  315. "X-Amz-Meta-My-Meta": "request",
  316. "X-Amz-Tagging-A": "B",
  317. "X-Amz-Tagging-a": "b",
  318. "X-Amz-Tagging-type": "request",
  319. },
  320. },
  321. }
  322. func TestProcessMetadata(t *testing.T) {
  323. for _, tc := range processMetadataTestCases {
  324. reqHeader := transferHToHeader(tc.request)
  325. existing := transferHToHeader(tc.existing)
  326. replaceMeta, replaceTagging := replaceDirective(reqHeader)
  327. err := processMetadata(reqHeader, existing, replaceMeta, replaceTagging, func(_ string, _ string) (tags map[string]string, err error) {
  328. return tc.getTags, nil
  329. }, "", "")
  330. if err != nil {
  331. t.Error(err)
  332. }
  333. result := transferHeaderToH(reqHeader)
  334. fmtTagging(result, tc.want)
  335. if !reflect.DeepEqual(result, tc.want) {
  336. t.Error(fmt.Errorf("\n### CaseID: %d ###"+
  337. "\nRequest:%v"+
  338. "\nExisting:%v"+
  339. "\nGetTags:%v"+
  340. "\nWant:%v"+
  341. "\nActual:%v",
  342. tc.caseId, tc.request, tc.existing, tc.getTags, tc.want, result))
  343. }
  344. }
  345. }
  346. func TestProcessMetadataBytes(t *testing.T) {
  347. for _, tc := range processMetadataBytesTestCases {
  348. reqHeader := transferHToHeader(tc.request)
  349. existing := transferHToBytesArr(tc.existing)
  350. replaceMeta, replaceTagging := replaceDirective(reqHeader)
  351. extends := processMetadataBytes(reqHeader, existing, replaceMeta, replaceTagging)
  352. result := transferBytesArrToH(extends)
  353. fmtTagging(result, tc.want)
  354. if !reflect.DeepEqual(result, tc.want) {
  355. t.Error(fmt.Errorf("\n### CaseID: %d ###"+
  356. "\nRequest:%v"+
  357. "\nExisting:%v"+
  358. "\nWant:%v"+
  359. "\nActual:%v",
  360. tc.caseId, tc.request, tc.existing, tc.want, result))
  361. }
  362. }
  363. }
  364. func fmtTagging(maps ...map[string]string) {
  365. for _, m := range maps {
  366. if tagging := m[s3_constants.AmzObjectTagging]; len(tagging) > 0 {
  367. split := strings.Split(tagging, "&")
  368. sort.Strings(split)
  369. m[s3_constants.AmzObjectTagging] = strings.Join(split, "&")
  370. }
  371. }
  372. }
  373. func transferHToHeader(data map[string]string) http.Header {
  374. header := http.Header{}
  375. for k, v := range data {
  376. header.Add(k, v)
  377. }
  378. return header
  379. }
  380. func transferHToBytesArr(data map[string]string) map[string][]byte {
  381. m := make(map[string][]byte, len(data))
  382. for k, v := range data {
  383. m[k] = []byte(v)
  384. }
  385. return m
  386. }
  387. func transferBytesArrToH(data map[string][]byte) H {
  388. m := make(map[string]string, len(data))
  389. for k, v := range data {
  390. m[k] = string(v)
  391. }
  392. return m
  393. }
  394. func transferHeaderToH(data map[string][]string) H {
  395. m := make(map[string]string, len(data))
  396. for k, v := range data {
  397. m[k] = v[len(v)-1]
  398. }
  399. return m
  400. }