| 1 | // Copyright 2020 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package fakekms_testscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/ |
| 16 | |
| 17 | import ( |
| 18 | "bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/" |
| 19 | "contextscip-go gomod github.com/golang/go/src go1.24.0 context/" |
| 20 | "errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/" |
| 21 | "testingscip-go gomod github.com/golang/go/src go1.24.0 testing/" |
| 22 | |
| 23 | "github.com/tink-crypto/tink-go/v2/testing/fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/" |
| 24 | ) |
| 25 | |
| 26 | const keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI. = "fake-kms://CM2b3_MDElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIK75t5L-adlUwVhWvRuWUwYARABGM2b3_MDIAE" |
| 27 | const anotherKeyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/anotherKeyURI. = "fake-kms://CLHW_5cHElQKSAowdHlwZS5nb29nbGVhcGlzLmNvbS9nb29nbGUuY3J5cHRvLnRpbmsuQWVzR2NtS2V5EhIaEIZ-2h9InfZTbbkJjaJBsVgYARABGLHW_5cHIAE" |
| 28 | |
| 29 | func TestValidKeyURIsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestValidKeyURIs().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 30 | newKeyURI, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewKeyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewKeyURI().() |
| 31 | if err != nil { |
| 32 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatal().(err) |
| 33 | } |
| 34 | var testCases = []string{ |
| 35 | keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI., |
| 36 | anotherKeyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/anotherKeyURI., |
| 37 | newKeyURI, |
| 38 | } |
| 39 | for _, tc := range testCases { |
| 40 | t.Runscip-go gomod github.com/golang/go/src go1.24.0 testing/T#Run().(tc, func(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 41 | client, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewClient().(tc) |
| 42 | if err != nil { |
| 43 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("testutil.NewFakeKMSClient(keyURI) failed: %v", err) |
| 44 | } |
| 45 | if !client.Supportedscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#Supported.(tc) { |
| 46 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("client.Supported(keyURI) is false, want true") |
| 47 | } |
| 48 | primitive, err := client.GetAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#GetAEAD.(tc) |
| 49 | if err != nil { |
| 50 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("client.GetAEAD(keyURI) failed: %v", err) |
| 51 | } |
| 52 | |
| 53 | plaintext := []byte("some data to encrypt") |
| 54 | aad := []byte("extra data to authenticate") |
| 55 | ciphertext, err := primitive.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Encrypt.(plaintext, aad) |
| 56 | if err != nil { |
| 57 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Encrypt(plaintext, aad) failed: %v", err) |
| 58 | } |
| 59 | decrypted, err := primitive.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Decrypt.(ciphertext, aad) |
| 60 | if err != nil { |
| 61 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Decrypt(ciphertext, aad) failed: %v", err) |
| 62 | } |
| 63 | if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(plaintext, decrypted) { |
| 64 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("decrypted data doesn't match plaintext, got: %q, want: %q", decrypted, plaintext) |
| 65 | } |
| 66 | invalidAssociatedData := []byte("invalid") |
| 67 | _, err = primitive.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Decrypt.(ciphertext, invalidAssociatedData) |
| 68 | if err == nil { |
| 69 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Decrypt(ciphertext, invalidAssociatedData) err = nil, want error") |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | func TestBadUriPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestBadUriPrefix().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 76 | _, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewClient().("bad-prefix://encodedkeyset") |
| 77 | if err == nil { |
| 78 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.NewClient('bad-prefix://encodedkeyset') succeeded, want fail") |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func TestValidPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestValidPrefix().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 83 | uriPrefix := "fake-kms://CM2b" // is a prefix of keyURI |
| 84 | client, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewClient().(uriPrefix) |
| 85 | if err != nil { |
| 86 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.NewClient(uriPrefix) failed: %v", err) |
| 87 | } |
| 88 | if !client.Supportedscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#Supported.(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) { |
| 89 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("client with URI prefix %s should support key URI %s", uriPrefix, keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 90 | } |
| 91 | _, err = client.GetAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#GetAEAD.(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 92 | if err != nil { |
| 93 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("client.GetAEAD(anotherKeyURI) failed: %v", err) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestInvalidPrefixscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestInvalidPrefix().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 98 | uriPrefix := "fake-kms://CM2x" // is not a prefix of keyURI |
| 99 | client, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewClient().(uriPrefix) |
| 100 | if err != nil { |
| 101 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.NewClient(uriPrefix) failed: %v", err) |
| 102 | } |
| 103 | if client.Supportedscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#Supported.(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) { |
| 104 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("client with URI prefix %s should not support key URI %s", uriPrefix, keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 105 | } |
| 106 | _, err = client.GetAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#GetAEAD.(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 107 | if err == nil { |
| 108 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("client.GetAEAD(keyURI) succeeded, want fail") |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | func TestGetAeadFailsWithBadKeysetEncodingscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestGetAeadFailsWithBadKeysetEncoding().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 113 | client, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewClientscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewClient().("fake-kms://bad") |
| 114 | if err != nil { |
| 115 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.NewClient('fake-kms://bad') failed: %v", err) |
| 116 | } |
| 117 | _, err = client.GetAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/core/registry`/KMSClient#GetAEAD.("fake-kms://badencoding") |
| 118 | if err == nil { |
| 119 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("client.GetAEAD('fake-kms://badencoding') succeeded, want fail") |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func TestAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestAEAD().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 124 | primitive, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEAD().(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 125 | if err != nil { |
| 126 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakems.NewAEAD(keyURI) failed: %v", err) |
| 127 | } |
| 128 | plaintext := []byte("plaintext") |
| 129 | associatedData := []byte("associatedData") |
| 130 | ciphertext, err := primitive.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Encrypt.(plaintext, associatedData) |
| 131 | if err != nil { |
| 132 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Encrypt(ctx, plaintext, associatedData) failed: %v", err) |
| 133 | } |
| 134 | decrypted, err := primitive.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Decrypt.(ciphertext, associatedData) |
| 135 | if err != nil { |
| 136 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Decrypt(ctx, ciphertext, aad) failed: %v", err) |
| 137 | } |
| 138 | if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(decrypted, plaintext) { |
| 139 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("decrypted data doesn't match plaintext, got: %q, want: %q", decrypted, plaintext) |
| 140 | } |
| 141 | invalidAssociatedData := []byte("invalid") |
| 142 | _, err = primitive.Decryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Decrypt.(ciphertext, invalidAssociatedData) |
| 143 | if err == nil { |
| 144 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Decrypt(ciphertext, invalidAssociatedData) err = nil, want error") |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | func TestNewAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestNewAEADWithContext().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 149 | primitive, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEADWithContext().(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 150 | if err != nil { |
| 151 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.NewAEADWithContext(keyURI) failed: %v", err) |
| 152 | } |
| 153 | plaintext := []byte("plaintext") |
| 154 | associatedData := []byte("associatedData") |
| 155 | ctx := contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.24.0 context/Background().() |
| 156 | ciphertext, err := primitive.EncryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#EncryptWithContext.(ctx, plaintext, associatedData) |
| 157 | if err != nil { |
| 158 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Encrypt(ctx, plaintext, associatedData) failed: %v", err) |
| 159 | } |
| 160 | decrypted, err := primitive.DecryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#DecryptWithContext.(ctx, ciphertext, associatedData) |
| 161 | if err != nil { |
| 162 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Decrypt(ctx, ciphertext, aad) failed: %v", err) |
| 163 | } |
| 164 | if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(decrypted, plaintext) { |
| 165 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("decrypted data doesn't match plaintext, got: %q, want: %q", decrypted, plaintext) |
| 166 | } |
| 167 | invalidAssociatedData := []byte("invalid") |
| 168 | _, err = primitive.DecryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#DecryptWithContext.(ctx, ciphertext, invalidAssociatedData) |
| 169 | if err == nil { |
| 170 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.DecryptWithContext(ciphertext, invalidAssociatedData) err = nil, want error") |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | func TestCompatibility_AEAD_AEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestCompatibility_AEAD_AEADWithContext().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 175 | primitive, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewAEADscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEAD().(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 176 | if err != nil { |
| 177 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.GetAEAD(keyURI) failed: %v", err) |
| 178 | } |
| 179 | plaintext := []byte("plaintext") |
| 180 | associatedData := []byte("associatedData") |
| 181 | ciphertext, err := primitive.Encryptscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEAD#Encrypt.(plaintext, associatedData) |
| 182 | if err != nil { |
| 183 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.Encrypt(plaintext, associatedData) failed: %v", err) |
| 184 | } |
| 185 | remotePrimitive, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEADWithContext().(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 186 | if err != nil { |
| 187 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.NewAEADWithContext(keyURI) failed: %v", err) |
| 188 | } |
| 189 | ctx := contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.24.0 context/Background().() |
| 190 | decrypted, err := remotePrimitive.DecryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#DecryptWithContext.(ctx, ciphertext, associatedData) |
| 191 | if err != nil { |
| 192 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("remotePrimitive.DecryptWithContext(ctx, ciphertext, associatedData) failed: %v", err) |
| 193 | } |
| 194 | if !bytesscip-go gomod github.com/golang/go/src go1.24.0 bytes/.Equalscip-go gomod github.com/golang/go/src go1.24.0 bytes/Equal().(decrypted, plaintext) { |
| 195 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("decrypted data doesn't match plaintext, got: %q, want: %q", decrypted, plaintext) |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func TestAEADWithContext_canceledContextFailsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/TestAEADWithContext_canceledContextFails().(t *testingscip-go gomod github.com/golang/go/src go1.24.0 testing/.Tscip-go gomod github.com/golang/go/src go1.24.0 testing/T#) { |
| 200 | primitive, err := fakekmsscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/.NewAEADWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms`/NewAEADWithContext().(keyURIscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/testing/fakekms_test`/keyURI.) |
| 201 | if err != nil { |
| 202 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("fakekms.NewAEADWithContext(keyURI) failed: %v", err) |
| 203 | } |
| 204 | plaintext := []byte("plaintext") |
| 205 | associatedData := []byte("associatedData") |
| 206 | ctx := contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.24.0 context/Background().() |
| 207 | ciphertext, err := primitive.EncryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#EncryptWithContext.(ctx, plaintext, associatedData) |
| 208 | if err != nil { |
| 209 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.EncryptWithContext(ctx, plaintext, associatedData) failed: %v", err) |
| 210 | } |
| 211 | canceledCtx, cancel := contextscip-go gomod github.com/golang/go/src go1.24.0 context/.WithCancelCausescip-go gomod github.com/golang/go/src go1.24.0 context/WithCancelCause().(ctx) |
| 212 | causeErr := errorsscip-go gomod github.com/golang/go/src go1.24.0 errors/.Newscip-go gomod github.com/golang/go/src go1.24.0 errors/New().("cause error message") |
| 213 | cancel(causeErr) |
| 214 | _, err = primitive.EncryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#EncryptWithContext.(canceledCtx, plaintext, associatedData) |
| 215 | if err == nil { |
| 216 | t.Fatalscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatal().("primitive.EncryptWithContext(canceledCtx, plaintext, associatedData) err = nil, want error") |
| 217 | } |
| 218 | _, err = primitive.DecryptWithContextscip-go gomod github.com/tink-crypto/tink-go/v2 . `github.com/tink-crypto/tink-go/v2/tink`/AEADWithContext#DecryptWithContext.(canceledCtx, ciphertext, associatedData) |
| 219 | if err == nil { |
| 220 | t.Fatalfscip-go gomod github.com/golang/go/src go1.24.0 testing/common#Fatalf().("primitive.DecryptWithContext(canceledCtx, ciphertext, associatedData) err = nil, want error") |
| 221 | } |
| 222 | } |