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.

709 lines
19 KiB

  1. package s3api
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "github.com/aws/aws-sdk-go/aws"
  6. "github.com/aws/aws-sdk-go/service/s3"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/iam_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  10. "github.com/seaweedfs/seaweedfs/weed/s3api/s3err"
  11. "io"
  12. "net/http"
  13. "testing"
  14. )
  15. var accountManager *IdentityAccessManagement
  16. func init() {
  17. accountManager = &IdentityAccessManagement{}
  18. _ = accountManager.loadS3ApiConfiguration(&iam_pb.S3ApiConfiguration{
  19. Accounts: []*iam_pb.Account{
  20. {
  21. Id: "accountA",
  22. DisplayName: "accountAName",
  23. EmailAddress: "accountA@example.com",
  24. },
  25. {
  26. Id: "accountB",
  27. DisplayName: "accountBName",
  28. EmailAddress: "accountB@example.com",
  29. },
  30. },
  31. })
  32. }
  33. func TestGetAccountId(t *testing.T) {
  34. req := &http.Request{
  35. Header: make(map[string][]string),
  36. }
  37. //case1
  38. //accountId: "admin"
  39. req.Header.Set(s3_constants.AmzAccountId, s3_constants.AccountAdminId)
  40. if GetAccountId(req) != s3_constants.AccountAdminId {
  41. t.Fatal("expect accountId: admin")
  42. }
  43. //case2
  44. //accountId: "anoymous"
  45. req.Header.Set(s3_constants.AmzAccountId, s3_constants.AccountAnonymousId)
  46. if GetAccountId(req) != s3_constants.AccountAnonymousId {
  47. t.Fatal("expect accountId: anonymous")
  48. }
  49. //case3
  50. //accountId is nil => "anonymous"
  51. req.Header.Del(s3_constants.AmzAccountId)
  52. if GetAccountId(req) != s3_constants.AccountAnonymousId {
  53. t.Fatal("expect accountId: anonymous")
  54. }
  55. }
  56. func TestExtractAcl(t *testing.T) {
  57. type Case struct {
  58. id int
  59. resultErrCode, expectErrCode s3err.ErrorCode
  60. resultGrants, expectGrants []*s3.Grant
  61. }
  62. testCases := make([]*Case, 0)
  63. accountAdminId := "admin"
  64. {
  65. //case1 (good case)
  66. //parse acp from request body
  67. req := &http.Request{
  68. Header: make(map[string][]string),
  69. }
  70. req.Body = io.NopCloser(bytes.NewReader([]byte(`
  71. <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  72. <Owner>
  73. <ID>admin</ID>
  74. <DisplayName>admin</DisplayName>
  75. </Owner>
  76. <AccessControlList>
  77. <Grant>
  78. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
  79. <ID>admin</ID>
  80. </Grantee>
  81. <Permission>FULL_CONTROL</Permission>
  82. </Grant>
  83. <Grant>
  84. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
  85. <URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
  86. </Grantee>
  87. <Permission>FULL_CONTROL</Permission>
  88. </Grant>
  89. </AccessControlList>
  90. </AccessControlPolicy>
  91. `)))
  92. objectWriter := "accountA"
  93. grants, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, accountAdminId, objectWriter)
  94. testCases = append(testCases, &Case{
  95. 1,
  96. errCode, s3err.ErrNone,
  97. grants, []*s3.Grant{
  98. {
  99. Grantee: &s3.Grantee{
  100. Type: &s3_constants.GrantTypeCanonicalUser,
  101. ID: &accountAdminId,
  102. },
  103. Permission: &s3_constants.PermissionFullControl,
  104. },
  105. {
  106. Grantee: &s3.Grantee{
  107. Type: &s3_constants.GrantTypeGroup,
  108. URI: &s3_constants.GranteeGroupAllUsers,
  109. },
  110. Permission: &s3_constants.PermissionFullControl,
  111. },
  112. },
  113. })
  114. }
  115. {
  116. //case2 (good case)
  117. //parse acp from header (cannedAcl)
  118. req := &http.Request{
  119. Header: make(map[string][]string),
  120. }
  121. req.Body = nil
  122. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclPrivate)
  123. objectWriter := "accountA"
  124. grants, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, accountAdminId, objectWriter)
  125. testCases = append(testCases, &Case{
  126. 2,
  127. errCode, s3err.ErrNone,
  128. grants, []*s3.Grant{
  129. {
  130. Grantee: &s3.Grantee{
  131. Type: &s3_constants.GrantTypeCanonicalUser,
  132. ID: &objectWriter,
  133. },
  134. Permission: &s3_constants.PermissionFullControl,
  135. },
  136. },
  137. })
  138. }
  139. {
  140. //case3 (bad case)
  141. //parse acp from request body (content is invalid)
  142. req := &http.Request{
  143. Header: make(map[string][]string),
  144. }
  145. req.Body = io.NopCloser(bytes.NewReader([]byte("zdfsaf")))
  146. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclPrivate)
  147. objectWriter := "accountA"
  148. _, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, accountAdminId, objectWriter)
  149. testCases = append(testCases, &Case{
  150. id: 3,
  151. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  152. })
  153. }
  154. //case4 (bad case)
  155. //parse acp from header (cannedAcl is invalid)
  156. req := &http.Request{
  157. Header: make(map[string][]string),
  158. }
  159. req.Body = nil
  160. req.Header.Set(s3_constants.AmzCannedAcl, "dfaksjfk")
  161. objectWriter := "accountA"
  162. _, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, "", objectWriter)
  163. testCases = append(testCases, &Case{
  164. id: 4,
  165. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  166. })
  167. {
  168. //case5 (bad case)
  169. //parse acp from request body: owner is inconsistent
  170. req.Body = io.NopCloser(bytes.NewReader([]byte(`
  171. <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
  172. <Owner>
  173. <ID>admin</ID>
  174. <DisplayName>admin</DisplayName>
  175. </Owner>
  176. <AccessControlList>
  177. <Grant>
  178. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="CanonicalUser">
  179. <ID>admin</ID>
  180. </Grantee>
  181. <Permission>FULL_CONTROL</Permission>
  182. </Grant>
  183. <Grant>
  184. <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">
  185. <URI>http://acs.amazonaws.com/groups/global/AllUsers</URI>
  186. </Grantee>
  187. <Permission>FULL_CONTROL</Permission>
  188. </Grant>
  189. </AccessControlList>
  190. </AccessControlPolicy>
  191. `)))
  192. objectWriter = "accountA"
  193. _, errCode := ExtractAcl(req, accountManager, s3_constants.OwnershipObjectWriter, accountAdminId, objectWriter, objectWriter)
  194. testCases = append(testCases, &Case{
  195. id: 5,
  196. resultErrCode: errCode, expectErrCode: s3err.ErrAccessDenied,
  197. })
  198. }
  199. for _, tc := range testCases {
  200. if tc.resultErrCode != tc.expectErrCode {
  201. t.Fatalf("case[%d]: errorCode not expect", tc.id)
  202. }
  203. if !grantsEquals(tc.resultGrants, tc.expectGrants) {
  204. t.Fatalf("case[%d]: grants not expect", tc.id)
  205. }
  206. }
  207. }
  208. func TestParseAndValidateAclHeaders(t *testing.T) {
  209. type Case struct {
  210. id int
  211. resultOwner, expectOwner string
  212. resultErrCode, expectErrCode s3err.ErrorCode
  213. resultGrants, expectGrants []*s3.Grant
  214. }
  215. testCases := make([]*Case, 0)
  216. bucketOwner := "admin"
  217. {
  218. //case1 (good case)
  219. //parse custom acl
  220. req := &http.Request{
  221. Header: make(map[string][]string),
  222. }
  223. objectWriter := "accountA"
  224. req.Header.Set(s3_constants.AmzAclFullControl, `uri="http://acs.amazonaws.com/groups/global/AllUsers", id="anonymous", emailAddress="admin@example.com"`)
  225. ownerId, grants, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  226. testCases = append(testCases, &Case{
  227. 1,
  228. ownerId, objectWriter,
  229. errCode, s3err.ErrNone,
  230. grants, []*s3.Grant{
  231. {
  232. Grantee: &s3.Grantee{
  233. Type: &s3_constants.GrantTypeGroup,
  234. URI: &s3_constants.GranteeGroupAllUsers,
  235. },
  236. Permission: &s3_constants.PermissionFullControl,
  237. },
  238. {
  239. Grantee: &s3.Grantee{
  240. Type: &s3_constants.GrantTypeCanonicalUser,
  241. ID: aws.String(s3_constants.AccountAnonymousId),
  242. },
  243. Permission: &s3_constants.PermissionFullControl,
  244. },
  245. {
  246. Grantee: &s3.Grantee{
  247. Type: &s3_constants.GrantTypeCanonicalUser,
  248. ID: aws.String(s3_constants.AccountAdminId),
  249. },
  250. Permission: &s3_constants.PermissionFullControl,
  251. },
  252. },
  253. })
  254. }
  255. {
  256. //case2 (good case)
  257. //parse canned acl (ownership=ObjectWriter)
  258. req := &http.Request{
  259. Header: make(map[string][]string),
  260. }
  261. objectWriter := "accountA"
  262. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclBucketOwnerFullControl)
  263. ownerId, grants, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  264. testCases = append(testCases, &Case{
  265. 2,
  266. ownerId, objectWriter,
  267. errCode, s3err.ErrNone,
  268. grants, []*s3.Grant{
  269. {
  270. Grantee: &s3.Grantee{
  271. Type: &s3_constants.GrantTypeCanonicalUser,
  272. ID: &objectWriter,
  273. },
  274. Permission: &s3_constants.PermissionFullControl,
  275. },
  276. {
  277. Grantee: &s3.Grantee{
  278. Type: &s3_constants.GrantTypeCanonicalUser,
  279. ID: &bucketOwner,
  280. },
  281. Permission: &s3_constants.PermissionFullControl,
  282. },
  283. },
  284. })
  285. }
  286. {
  287. //case3 (good case)
  288. //parse canned acl (ownership=OwnershipBucketOwnerPreferred)
  289. req := &http.Request{
  290. Header: make(map[string][]string),
  291. }
  292. objectWriter := "accountA"
  293. req.Header.Set(s3_constants.AmzCannedAcl, s3_constants.CannedAclBucketOwnerFullControl)
  294. ownerId, grants, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipBucketOwnerPreferred, bucketOwner, objectWriter, false)
  295. testCases = append(testCases, &Case{
  296. 3,
  297. ownerId, bucketOwner,
  298. errCode, s3err.ErrNone,
  299. grants, []*s3.Grant{
  300. {
  301. Grantee: &s3.Grantee{
  302. Type: &s3_constants.GrantTypeCanonicalUser,
  303. ID: &bucketOwner,
  304. },
  305. Permission: &s3_constants.PermissionFullControl,
  306. },
  307. },
  308. })
  309. }
  310. {
  311. //case4 (bad case)
  312. //parse custom acl (grantee id not exists)
  313. req := &http.Request{
  314. Header: make(map[string][]string),
  315. }
  316. objectWriter := "accountA"
  317. req.Header.Set(s3_constants.AmzAclFullControl, `uri="http://acs.amazonaws.com/groups/global/AllUsers", id="notExistsAccount", emailAddress="admin@example.com"`)
  318. _, _, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  319. testCases = append(testCases, &Case{
  320. id: 4,
  321. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  322. })
  323. }
  324. {
  325. //case5 (bad case)
  326. //parse custom acl (invalid format)
  327. req := &http.Request{
  328. Header: make(map[string][]string),
  329. }
  330. objectWriter := "accountA"
  331. req.Header.Set(s3_constants.AmzAclFullControl, `uri="http:sfasf"`)
  332. _, _, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  333. testCases = append(testCases, &Case{
  334. id: 5,
  335. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  336. })
  337. }
  338. {
  339. //case6 (bad case)
  340. //parse canned acl (invalid value)
  341. req := &http.Request{
  342. Header: make(map[string][]string),
  343. }
  344. objectWriter := "accountA"
  345. req.Header.Set(s3_constants.AmzCannedAcl, `uri="http:sfasf"`)
  346. _, _, errCode := ParseAndValidateAclHeaders(req, accountManager, s3_constants.OwnershipObjectWriter, bucketOwner, objectWriter, false)
  347. testCases = append(testCases, &Case{
  348. id: 5,
  349. resultErrCode: errCode, expectErrCode: s3err.ErrInvalidRequest,
  350. })
  351. }
  352. for _, tc := range testCases {
  353. if tc.expectErrCode != tc.resultErrCode {
  354. t.Errorf("case[%d]: errCode unexpect", tc.id)
  355. }
  356. if tc.resultOwner != tc.expectOwner {
  357. t.Errorf("case[%d]: ownerId unexpect", tc.id)
  358. }
  359. if !grantsEquals(tc.resultGrants, tc.expectGrants) {
  360. t.Fatalf("case[%d]: grants not expect", tc.id)
  361. }
  362. }
  363. }
  364. func grantsEquals(a, b []*s3.Grant) bool {
  365. if len(a) != len(b) {
  366. return false
  367. }
  368. for i, grant := range a {
  369. if !GrantEquals(grant, b[i]) {
  370. return false
  371. }
  372. }
  373. return true
  374. }
  375. func TestDetermineReqGrants(t *testing.T) {
  376. {
  377. //case1: request account is anonymous
  378. accountId := s3_constants.AccountAnonymousId
  379. reqPermission := s3_constants.PermissionRead
  380. resultGrants := DetermineReqGrants(accountId, reqPermission)
  381. expectGrants := []*s3.Grant{
  382. {
  383. Grantee: &s3.Grantee{
  384. Type: &s3_constants.GrantTypeGroup,
  385. URI: &s3_constants.GranteeGroupAllUsers,
  386. },
  387. Permission: &reqPermission,
  388. },
  389. {
  390. Grantee: &s3.Grantee{
  391. Type: &s3_constants.GrantTypeGroup,
  392. URI: &s3_constants.GranteeGroupAllUsers,
  393. },
  394. Permission: &s3_constants.PermissionFullControl,
  395. },
  396. {
  397. Grantee: &s3.Grantee{
  398. Type: &s3_constants.GrantTypeCanonicalUser,
  399. ID: &accountId,
  400. },
  401. Permission: &reqPermission,
  402. },
  403. {
  404. Grantee: &s3.Grantee{
  405. Type: &s3_constants.GrantTypeCanonicalUser,
  406. ID: &accountId,
  407. },
  408. Permission: &s3_constants.PermissionFullControl,
  409. },
  410. }
  411. if !grantsEquals(resultGrants, expectGrants) {
  412. t.Fatalf("grants not expect")
  413. }
  414. }
  415. {
  416. //case2: request account is not anonymous (Iam authed)
  417. accountId := "accountX"
  418. reqPermission := s3_constants.PermissionRead
  419. resultGrants := DetermineReqGrants(accountId, reqPermission)
  420. expectGrants := []*s3.Grant{
  421. {
  422. Grantee: &s3.Grantee{
  423. Type: &s3_constants.GrantTypeGroup,
  424. URI: &s3_constants.GranteeGroupAllUsers,
  425. },
  426. Permission: &reqPermission,
  427. },
  428. {
  429. Grantee: &s3.Grantee{
  430. Type: &s3_constants.GrantTypeGroup,
  431. URI: &s3_constants.GranteeGroupAllUsers,
  432. },
  433. Permission: &s3_constants.PermissionFullControl,
  434. },
  435. {
  436. Grantee: &s3.Grantee{
  437. Type: &s3_constants.GrantTypeCanonicalUser,
  438. ID: &accountId,
  439. },
  440. Permission: &reqPermission,
  441. },
  442. {
  443. Grantee: &s3.Grantee{
  444. Type: &s3_constants.GrantTypeCanonicalUser,
  445. ID: &accountId,
  446. },
  447. Permission: &s3_constants.PermissionFullControl,
  448. },
  449. {
  450. Grantee: &s3.Grantee{
  451. Type: &s3_constants.GrantTypeGroup,
  452. URI: &s3_constants.GranteeGroupAuthenticatedUsers,
  453. },
  454. Permission: &reqPermission,
  455. },
  456. {
  457. Grantee: &s3.Grantee{
  458. Type: &s3_constants.GrantTypeGroup,
  459. URI: &s3_constants.GranteeGroupAuthenticatedUsers,
  460. },
  461. Permission: &s3_constants.PermissionFullControl,
  462. },
  463. }
  464. if !grantsEquals(resultGrants, expectGrants) {
  465. t.Fatalf("grants not expect")
  466. }
  467. }
  468. }
  469. func TestAssembleEntryWithAcp(t *testing.T) {
  470. defaultOwner := "admin"
  471. //case1
  472. //assemble with non-empty grants
  473. expectOwner := "accountS"
  474. expectGrants := []*s3.Grant{
  475. {
  476. Permission: &s3_constants.PermissionRead,
  477. Grantee: &s3.Grantee{
  478. Type: &s3_constants.GrantTypeGroup,
  479. ID: aws.String(s3_constants.AccountAdminId),
  480. URI: &s3_constants.GranteeGroupAllUsers,
  481. },
  482. },
  483. }
  484. entry := &filer_pb.Entry{}
  485. AssembleEntryWithAcp(entry, expectOwner, expectGrants)
  486. resultOwner := GetAcpOwner(entry.Extended, defaultOwner)
  487. if resultOwner != expectOwner {
  488. t.Fatalf("owner not expect")
  489. }
  490. resultGrants := GetAcpGrants(entry.Extended)
  491. if !grantsEquals(resultGrants, expectGrants) {
  492. t.Fatal("grants not expect")
  493. }
  494. //case2
  495. //assemble with empty grants (override)
  496. AssembleEntryWithAcp(entry, "", nil)
  497. resultOwner = GetAcpOwner(entry.Extended, defaultOwner)
  498. if resultOwner != defaultOwner {
  499. t.Fatalf("owner not expect")
  500. }
  501. resultGrants = GetAcpGrants(entry.Extended)
  502. if len(resultGrants) != 0 {
  503. t.Fatal("grants not expect")
  504. }
  505. }
  506. func TestGrantEquals(t *testing.T) {
  507. testCases := map[bool]bool{
  508. GrantEquals(nil, nil): true,
  509. GrantEquals(&s3.Grant{}, nil): false,
  510. GrantEquals(&s3.Grant{}, &s3.Grant{}): true,
  511. GrantEquals(&s3.Grant{
  512. Permission: &s3_constants.PermissionRead,
  513. }, &s3.Grant{}): false,
  514. GrantEquals(&s3.Grant{
  515. Permission: &s3_constants.PermissionRead,
  516. }, &s3.Grant{
  517. Permission: &s3_constants.PermissionRead,
  518. }): true,
  519. GrantEquals(&s3.Grant{
  520. Permission: &s3_constants.PermissionRead,
  521. Grantee: &s3.Grantee{},
  522. }, &s3.Grant{
  523. Permission: &s3_constants.PermissionRead,
  524. Grantee: &s3.Grantee{},
  525. }): true,
  526. GrantEquals(&s3.Grant{
  527. Permission: &s3_constants.PermissionRead,
  528. Grantee: &s3.Grantee{
  529. Type: &s3_constants.GrantTypeGroup,
  530. },
  531. }, &s3.Grant{
  532. Permission: &s3_constants.PermissionRead,
  533. Grantee: &s3.Grantee{},
  534. }): false,
  535. //type not present, compare other fields of grant is meaningless
  536. GrantEquals(&s3.Grant{
  537. Permission: &s3_constants.PermissionRead,
  538. Grantee: &s3.Grantee{
  539. ID: aws.String(s3_constants.AccountAdminId),
  540. //EmailAddress: &s3account.AccountAdmin.EmailAddress,
  541. },
  542. }, &s3.Grant{
  543. Permission: &s3_constants.PermissionRead,
  544. Grantee: &s3.Grantee{
  545. ID: aws.String(s3_constants.AccountAdminId),
  546. },
  547. }): true,
  548. GrantEquals(&s3.Grant{
  549. Permission: &s3_constants.PermissionRead,
  550. Grantee: &s3.Grantee{
  551. Type: &s3_constants.GrantTypeGroup,
  552. },
  553. }, &s3.Grant{
  554. Permission: &s3_constants.PermissionRead,
  555. Grantee: &s3.Grantee{
  556. Type: &s3_constants.GrantTypeGroup,
  557. },
  558. }): true,
  559. GrantEquals(&s3.Grant{
  560. Permission: &s3_constants.PermissionRead,
  561. Grantee: &s3.Grantee{
  562. Type: &s3_constants.GrantTypeGroup,
  563. URI: &s3_constants.GranteeGroupAllUsers,
  564. },
  565. }, &s3.Grant{
  566. Permission: &s3_constants.PermissionRead,
  567. Grantee: &s3.Grantee{
  568. Type: &s3_constants.GrantTypeGroup,
  569. URI: &s3_constants.GranteeGroupAllUsers,
  570. },
  571. }): true,
  572. GrantEquals(&s3.Grant{
  573. Permission: &s3_constants.PermissionWrite,
  574. Grantee: &s3.Grantee{
  575. Type: &s3_constants.GrantTypeGroup,
  576. URI: &s3_constants.GranteeGroupAllUsers,
  577. },
  578. }, &s3.Grant{
  579. Permission: &s3_constants.PermissionRead,
  580. Grantee: &s3.Grantee{
  581. Type: &s3_constants.GrantTypeGroup,
  582. URI: &s3_constants.GranteeGroupAllUsers,
  583. },
  584. }): false,
  585. GrantEquals(&s3.Grant{
  586. Permission: &s3_constants.PermissionRead,
  587. Grantee: &s3.Grantee{
  588. Type: &s3_constants.GrantTypeGroup,
  589. ID: aws.String(s3_constants.AccountAdminId),
  590. },
  591. }, &s3.Grant{
  592. Permission: &s3_constants.PermissionRead,
  593. Grantee: &s3.Grantee{
  594. Type: &s3_constants.GrantTypeGroup,
  595. ID: aws.String(s3_constants.AccountAdminId),
  596. },
  597. }): true,
  598. GrantEquals(&s3.Grant{
  599. Permission: &s3_constants.PermissionRead,
  600. Grantee: &s3.Grantee{
  601. Type: &s3_constants.GrantTypeGroup,
  602. ID: aws.String(s3_constants.AccountAdminId),
  603. URI: &s3_constants.GranteeGroupAllUsers,
  604. },
  605. }, &s3.Grant{
  606. Permission: &s3_constants.PermissionRead,
  607. Grantee: &s3.Grantee{
  608. Type: &s3_constants.GrantTypeGroup,
  609. ID: aws.String(s3_constants.AccountAdminId),
  610. },
  611. }): false,
  612. GrantEquals(&s3.Grant{
  613. Permission: &s3_constants.PermissionRead,
  614. Grantee: &s3.Grantee{
  615. Type: &s3_constants.GrantTypeGroup,
  616. ID: aws.String(s3_constants.AccountAdminId),
  617. URI: &s3_constants.GranteeGroupAllUsers,
  618. },
  619. }, &s3.Grant{
  620. Permission: &s3_constants.PermissionRead,
  621. Grantee: &s3.Grantee{
  622. Type: &s3_constants.GrantTypeGroup,
  623. URI: &s3_constants.GranteeGroupAllUsers,
  624. },
  625. }): true,
  626. }
  627. for tc, expect := range testCases {
  628. if tc != expect {
  629. t.Fatal("TestGrantEquals not expect!")
  630. }
  631. }
  632. }
  633. func TestSetAcpOwnerHeader(t *testing.T) {
  634. ownerId := "accountZ"
  635. req := &http.Request{
  636. Header: make(map[string][]string),
  637. }
  638. SetAcpOwnerHeader(req, ownerId)
  639. if req.Header.Get(s3_constants.ExtAmzOwnerKey) != ownerId {
  640. t.Fatalf("owner unexpect")
  641. }
  642. }
  643. func TestSetAcpGrantsHeader(t *testing.T) {
  644. req := &http.Request{
  645. Header: make(map[string][]string),
  646. }
  647. grants := []*s3.Grant{
  648. {
  649. Permission: &s3_constants.PermissionRead,
  650. Grantee: &s3.Grantee{
  651. Type: &s3_constants.GrantTypeGroup,
  652. ID: aws.String(s3_constants.AccountAdminId),
  653. URI: &s3_constants.GranteeGroupAllUsers,
  654. },
  655. },
  656. }
  657. SetAcpGrantsHeader(req, grants)
  658. grantsJson, _ := json.Marshal(grants)
  659. if req.Header.Get(s3_constants.ExtAmzAclKey) != string(grantsJson) {
  660. t.Fatalf("owner unexpect")
  661. }
  662. }