github.com/aavshr/aws-sdk-go@v1.41.3/service/s3/s3crypto/envelope_test.go (about) 1 //go:build go1.7 2 // +build go1.7 3 4 package s3crypto 5 6 import ( 7 "encoding/json" 8 "reflect" 9 "testing" 10 ) 11 12 func TestEnvelope_UnmarshalJSON(t *testing.T) { 13 cases := map[string]struct { 14 content []byte 15 expected Envelope 16 actual Envelope 17 }{ 18 "string json numbers": { 19 content: []byte(`{ 20 "x-amz-iv": "iv", 21 "x-amz-key-v2": "key", 22 "x-amz-matdesc": "{\"aws:x-amz-cek-alg\":\"AES/GCM/NoPadding\"}", 23 "x-amz-wrap-alg": "kms+context", 24 "x-amz-cek-alg": "AES/GCM/NoPadding", 25 "x-amz-tag-len": "128", 26 "x-amz-unencrypted-content-length": "1024" 27 } 28 `), 29 expected: Envelope{ 30 IV: "iv", 31 CipherKey: "key", 32 MatDesc: `{"aws:x-amz-cek-alg":"AES/GCM/NoPadding"}`, 33 WrapAlg: "kms+context", 34 CEKAlg: "AES/GCM/NoPadding", 35 TagLen: "128", 36 UnencryptedContentLen: "1024", 37 }, 38 }, 39 "integer json numbers": { 40 content: []byte(`{ 41 "x-amz-iv": "iv", 42 "x-amz-key-v2": "key", 43 "x-amz-matdesc": "{\"aws:x-amz-cek-alg\":\"AES/GCM/NoPadding\"}", 44 "x-amz-wrap-alg": "kms+context", 45 "x-amz-cek-alg": "AES/GCM/NoPadding", 46 "x-amz-tag-len": 128, 47 "x-amz-unencrypted-content-length": 1024 48 } 49 `), 50 expected: Envelope{ 51 IV: "iv", 52 CipherKey: "key", 53 MatDesc: `{"aws:x-amz-cek-alg":"AES/GCM/NoPadding"}`, 54 WrapAlg: "kms+context", 55 CEKAlg: "AES/GCM/NoPadding", 56 TagLen: "128", 57 UnencryptedContentLen: "1024", 58 }, 59 }, 60 "null json numbers": { 61 content: []byte(`{ 62 "x-amz-iv": "iv", 63 "x-amz-key-v2": "key", 64 "x-amz-matdesc": "{\"aws:x-amz-cek-alg\":\"AES/GCM/NoPadding\"}", 65 "x-amz-wrap-alg": "kms+context", 66 "x-amz-cek-alg": "AES/GCM/NoPadding", 67 "x-amz-tag-len": null, 68 "x-amz-unencrypted-content-length": null 69 } 70 `), 71 expected: Envelope{ 72 IV: "iv", 73 CipherKey: "key", 74 MatDesc: `{"aws:x-amz-cek-alg":"AES/GCM/NoPadding"}`, 75 WrapAlg: "kms+context", 76 CEKAlg: "AES/GCM/NoPadding", 77 }, 78 }, 79 "no json numbers": { 80 content: []byte(`{ 81 "x-amz-iv": "iv", 82 "x-amz-key-v2": "key", 83 "x-amz-matdesc": "{\"aws:x-amz-cek-alg\":\"AES/GCM/NoPadding\"}", 84 "x-amz-wrap-alg": "kms+context", 85 "x-amz-cek-alg": "AES/GCM/NoPadding" 86 } 87 `), 88 expected: Envelope{ 89 IV: "iv", 90 CipherKey: "key", 91 MatDesc: `{"aws:x-amz-cek-alg":"AES/GCM/NoPadding"}`, 92 WrapAlg: "kms+context", 93 CEKAlg: "AES/GCM/NoPadding", 94 }, 95 }, 96 } 97 98 for name, tt := range cases { 99 t.Run(name, func(t *testing.T) { 100 err := json.Unmarshal(tt.content, &tt.actual) 101 if err != nil { 102 t.Errorf("expected no error, got %v", err) 103 } 104 if !reflect.DeepEqual(tt.expected, tt.actual) { 105 t.Errorf("expected %v, got %v", tt.expected, tt.actual) 106 } 107 }) 108 } 109 }