github.com/opentofu/opentofu@v1.7.1/internal/states/statefile/read_test.go (about) 1 // SPDX-License-Identifier: MPL-2.0 2 3 package statefile 4 5 import ( 6 "bytes" 7 "errors" 8 "os" 9 "testing" 10 11 "github.com/opentofu/opentofu/internal/encryption" 12 "github.com/opentofu/opentofu/internal/encryption/enctest" 13 ) 14 15 func TestReadErrNoState_emptyFile(t *testing.T) { 16 emptyFile, err := os.Open("testdata/read/empty") 17 if err != nil { 18 t.Fatal(err) 19 } 20 defer emptyFile.Close() 21 22 _, err = Read(emptyFile, encryption.StateEncryptionDisabled()) 23 if !errors.Is(err, ErrNoState) { 24 t.Fatalf("expected ErrNoState, got %T", err) 25 } 26 } 27 28 func TestReadErrNoState_nilFile(t *testing.T) { 29 nilFile, err := os.Open("") 30 if err == nil { 31 t.Fatal("wrongly succeeded in opening non-existent file") 32 } 33 34 _, err = Read(nilFile, encryption.StateEncryptionDisabled()) 35 if !errors.Is(err, ErrNoState) { 36 t.Fatalf("expected ErrNoState, got %T", err) 37 } 38 } 39 func TestReadEmptyWithEncryption(t *testing.T) { 40 payload := bytes.NewBufferString("") 41 42 _, err := Read(payload, enctest.EncryptionRequired().State()) 43 if !errors.Is(err, ErrNoState) { 44 t.Fatalf("expected ErrNoState, got %T", err) 45 } 46 } 47 func TestReadEmptyJsonWithEncryption(t *testing.T) { 48 payload := bytes.NewBufferString("{}") 49 50 _, err := Read(payload, enctest.EncryptionRequired().State()) 51 52 if err == nil || err.Error() != "unable to determine data structure during decryption: Given payload is not a state file" { 53 t.Fatalf("expected encryption error, got %v", err) 54 } 55 }