github.com/zntrio/harp/v2@v2.0.9/pkg/tasks/container/unseal_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package container 19 20 import ( 21 "context" 22 "errors" 23 "io" 24 "testing" 25 26 "github.com/awnumar/memguard" 27 28 "github.com/zntrio/harp/v2/pkg/sdk/cmdutil" 29 "github.com/zntrio/harp/v2/pkg/tasks" 30 ) 31 32 func TestUnsealTask_Run(t *testing.T) { 33 type fields struct { 34 ContainerReader tasks.ReaderProvider 35 OutputWriter tasks.WriterProvider 36 ContainerKey *memguard.LockedBuffer 37 PreSharedKey *memguard.LockedBuffer 38 } 39 type args struct { 40 ctx context.Context 41 } 42 tests := []struct { 43 name string 44 fields fields 45 args args 46 wantErr bool 47 }{ 48 { 49 name: "nil", 50 wantErr: true, 51 }, 52 { 53 name: "nil containerReader", 54 fields: fields{ 55 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.bundle"), 56 }, 57 wantErr: true, 58 }, 59 { 60 name: "nil outputWriter", 61 fields: fields{ 62 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.bundle"), 63 OutputWriter: nil, 64 }, 65 wantErr: true, 66 }, 67 { 68 name: "nil containerKey", 69 fields: fields{ 70 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.bundle"), 71 OutputWriter: cmdutil.DiscardWriter(), 72 ContainerKey: nil, 73 }, 74 wantErr: true, 75 }, 76 { 77 name: "containerReader error", 78 fields: fields{ 79 ContainerReader: cmdutil.FileReader("non-existent.bundle"), 80 OutputWriter: cmdutil.DiscardWriter(), 81 ContainerKey: memguard.NewBuffer(32), 82 }, 83 wantErr: true, 84 }, 85 { 86 name: "containerReader not a bundle", 87 fields: fields{ 88 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.json"), 89 OutputWriter: cmdutil.DiscardWriter(), 90 ContainerKey: memguard.NewBuffer(32), 91 }, 92 wantErr: true, 93 }, 94 { 95 name: "invalid container key", 96 fields: fields{ 97 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.bundle"), 98 OutputWriter: cmdutil.DiscardWriter(), 99 ContainerKey: memguard.NewBuffer(32), 100 }, 101 wantErr: true, 102 }, 103 { 104 name: "outputWriter error", 105 fields: fields{ 106 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v1.sealed"), 107 OutputWriter: func(ctx context.Context) (io.Writer, error) { 108 return nil, errors.New("test") 109 }, 110 ContainerKey: memguard.NewBufferFromBytes([]byte("v1.ck.MiVGh4KOmdzZbej17BZGChkCPZ9uK9uBWdPNU0GlBNg")), 111 }, 112 wantErr: true, 113 }, 114 { 115 name: "outputWriter closed", 116 fields: fields{ 117 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v1.sealed"), 118 OutputWriter: func(ctx context.Context) (io.Writer, error) { 119 return cmdutil.NewClosedWriter(), nil 120 }, 121 ContainerKey: memguard.NewBufferFromBytes([]byte("v1.ck.MiVGh4KOmdzZbej17BZGChkCPZ9uK9uBWdPNU0GlBNg")), 122 }, 123 wantErr: true, 124 }, 125 { 126 name: "v2 without prefix", 127 fields: fields{ 128 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v2.sealed"), 129 OutputWriter: cmdutil.DiscardWriter(), 130 ContainerKey: memguard.NewBufferFromBytes([]byte("v2.ck.dAYx4CeTMRGKfpFHA7Q926qMz8imo1VJIToMw9uvH7HfPJTRpLUSMUS07JAdV-1R")), 131 }, 132 wantErr: true, 133 }, 134 // --------------------------------------------------------------------- 135 { 136 name: "valid - v1", 137 fields: fields{ 138 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v1.sealed"), 139 OutputWriter: cmdutil.DiscardWriter(), 140 ContainerKey: memguard.NewBufferFromBytes([]byte("v1.ck.MiVGh4KOmdzZbej17BZGChkCPZ9uK9uBWdPNU0GlBNg")), 141 }, 142 wantErr: false, 143 }, 144 { 145 name: "valid - v1 - with identity recovery key", 146 fields: fields{ 147 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v1.sealed"), 148 OutputWriter: cmdutil.DiscardWriter(), 149 ContainerKey: memguard.NewBufferFromBytes([]byte("v1.ck.IO6bCjACnqsCP0ahT--CVBhryzhe-ZFroVzn5Dx3D0U")), 150 }, 151 wantErr: false, 152 }, 153 { 154 name: "valid - v1 - with identity recovery key with prefix", 155 fields: fields{ 156 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v1.sealed"), 157 OutputWriter: cmdutil.DiscardWriter(), 158 ContainerKey: memguard.NewBufferFromBytes([]byte("v1.ck.IO6bCjACnqsCP0ahT--CVBhryzhe-ZFroVzn5Dx3D0U")), 159 }, 160 wantErr: false, 161 }, 162 { 163 name: "valid - v2", 164 fields: fields{ 165 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v2.sealed"), 166 OutputWriter: cmdutil.DiscardWriter(), 167 ContainerKey: memguard.NewBufferFromBytes([]byte("v2.ck.CLMEUoY-EgvMGKCcKeByPdJjQDod6fqTnqvxtD_Z0_SX4PMITu_emttDL91z_61D")), 168 }, 169 wantErr: false, 170 }, 171 { 172 name: "valid - v2 - with identity recovery key", 173 fields: fields{ 174 ContainerReader: cmdutil.FileReader("../../../test/fixtures/bundles/complete.v2.sealed"), 175 OutputWriter: cmdutil.DiscardWriter(), 176 ContainerKey: memguard.NewBufferFromBytes([]byte("v2.ck.8DwD8D-TUB9w-NzXBXySz4PkAIrWUc09TOJKdJ495MJ-AJ2lvDlj1Pnw1rSUAwVg")), 177 }, 178 wantErr: false, 179 }, 180 } 181 for _, tt := range tests { 182 t.Run(tt.name, func(t *testing.T) { 183 tr := &UnsealTask{ 184 ContainerReader: tt.fields.ContainerReader, 185 OutputWriter: tt.fields.OutputWriter, 186 ContainerKey: tt.fields.ContainerKey, 187 } 188 if err := tr.Run(tt.args.ctx); (err != nil) != tt.wantErr { 189 t.Errorf("UnsealTask.Run() error = %v, wantErr %v", err, tt.wantErr) 190 } 191 }) 192 } 193 }