You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

legacy_test.go 1.2KB

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