code.gitea.io/gitea@v1.19.3/modules/util/legacy_test.go (about)

     1  // Copyright 2022 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package util
     5  
     6  import (
     7  	"crypto/aes"
     8  	"crypto/rand"
     9  	"fmt"
    10  	"os"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestCopyFile(t *testing.T) {
    18  	testContent := []byte("hello")
    19  
    20  	tmpDir := os.TempDir()
    21  	now := time.Now()
    22  	srcFile := fmt.Sprintf("%s/copy-test-%d-src.txt", tmpDir, now.UnixMicro())
    23  	dstFile := fmt.Sprintf("%s/copy-test-%d-dst.txt", tmpDir, now.UnixMicro())
    24  
    25  	_ = os.Remove(srcFile)
    26  	_ = os.Remove(dstFile)
    27  	defer func() {
    28  		_ = os.Remove(srcFile)
    29  		_ = os.Remove(dstFile)
    30  	}()
    31  
    32  	err := os.WriteFile(srcFile, testContent, 0o777)
    33  	assert.NoError(t, err)
    34  	err = CopyFile(srcFile, dstFile)
    35  	assert.NoError(t, err)
    36  	dstContent, err := os.ReadFile(dstFile)
    37  	assert.NoError(t, err)
    38  	assert.Equal(t, testContent, dstContent)
    39  }
    40  
    41  func TestAESGCM(t *testing.T) {
    42  	t.Parallel()
    43  
    44  	key := make([]byte, aes.BlockSize)
    45  	_, err := rand.Read(key)
    46  	assert.NoError(t, err)
    47  
    48  	plaintext := []byte("this will be encrypted")
    49  
    50  	ciphertext, err := AESGCMEncrypt(key, plaintext)
    51  	assert.NoError(t, err)
    52  
    53  	decrypted, err := AESGCMDecrypt(key, ciphertext)
    54  	assert.NoError(t, err)
    55  
    56  	assert.Equal(t, plaintext, decrypted)
    57  }