1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
|
package proton_api_bridge
import (
"bufio"
"bytes"
"context"
"io"
"os"
"testing"
"time"
"github.com/henrybear327/Proton-API-Bridge/common"
"github.com/henrybear327/Proton-API-Bridge/utility"
"github.com/henrybear327/go-proton-api"
mathrand "math/rand"
)
func setup(t *testing.T, replaceExistingDraft bool) (context.Context, context.CancelFunc, *ProtonDrive) {
utility.SetupLog()
config := common.NewConfigForIntegrationTests()
config.ReplaceExistingDraft = replaceExistingDraft
{
// pre-condition check
if !config.DestructiveIntegrationTest {
t.Fatalf("CAUTION: the integration test requires a clean proton drive")
}
if !config.EmptyTrashAfterIntegrationTest {
t.Fatalf("CAUTION: the integration test requires cleaning up the drive after running the tests")
}
}
ctx, cancel := context.WithCancel(context.Background())
protonDrive, auth, err := NewProtonDrive(ctx, config, func(auth proton.Auth) {}, func() {})
if err != nil {
t.Fatal(err)
}
err = protonDrive.EmptyRootFolder(ctx)
if err != nil {
t.Fatal(err)
}
if config.UseReusableLogin {
if auth != nil {
t.Fatalf("Auth should be nil")
}
} else {
if auth == nil {
t.Fatalf("Auth should not be nil")
}
}
err = protonDrive.EmptyTrash(ctx)
if err != nil {
t.Fatal(err)
}
return ctx, cancel, protonDrive
}
func tearDown(t *testing.T, ctx context.Context, protonDrive *ProtonDrive) {
if protonDrive.Config.EmptyTrashAfterIntegrationTest {
err := protonDrive.EmptyTrash(ctx)
if err != nil {
t.Fatal(err)
}
}
}
// Taken from: https://github.com/rclone/rclone/blob/e43b5ce5e59b5717a9819ff81805dd431f710c10/lib/random/random.go
//
// StringFn create a random string for test purposes using the random
// number generator function passed in.
//
// Do not use these for passwords.
func StringFn(n int, randIntn func(n int) int) string {
const (
vowel = "aeiou"
consonant = "bcdfghjklmnpqrstvwxyz"
digit = "0123456789"
)
pattern := []string{consonant, vowel, consonant, vowel, consonant, vowel, consonant, digit}
out := make([]byte, n)
p := 0
for i := range out {
source := pattern[p]
p = (p + 1) % len(pattern)
out[i] = source[randIntn(len(source))]
}
return string(out)
}
// String create a random string for test purposes.
//
// Do not use these for passwords.
func RandomString(n int) string {
return StringFn(n, mathrand.Intn)
}
func createFolderExpectError(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, parent, name string, expectedError error) {
parentLink := protonDrive.RootLink
if parent != "" {
targetFolderLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, parent, true, false)
if err != nil {
t.Fatal(err)
}
if targetFolderLink == nil {
t.Fatalf("Folder %v not found", parent)
}
parentLink = targetFolderLink
}
if parentLink.Type != proton.LinkTypeFolder {
t.Fatalf("parentLink is not of folder type")
}
_, err := protonDrive.CreateNewFolderByID(ctx, parentLink.LinkID, name)
if err != expectedError {
t.Fatal(err)
}
}
func createFolder(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, parent, name string) {
createFolderExpectError(t, ctx, protonDrive, parent, name, nil)
}
func uploadFileByReader(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, parent, name string, in io.Reader, testParam int) {
parentLink := protonDrive.RootLink
if parent != "" {
targetFolderLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, parent, true, false)
if err != nil {
t.Fatal(err)
}
if targetFolderLink == nil {
t.Fatalf("Folder %v not found", parent)
}
parentLink = targetFolderLink
}
if parentLink.Type != proton.LinkTypeFolder {
t.Fatalf("parentLink is not of folder type")
}
_, _, err := protonDrive.UploadFileByReader(ctx, parentLink.LinkID, name, time.Now(), in, testParam)
if err != nil {
t.Fatal(err)
}
}
func uploadFileByFilepathWithError(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, parent, name string, filepath string, testParam int, expectedError error) {
parentLink := protonDrive.RootLink
if parent != "" {
targetFolderLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, parent, true, false)
if err != nil {
t.Fatal(err)
}
if targetFolderLink == nil {
t.Fatalf("Folder %v not found", parent)
}
parentLink = targetFolderLink
}
if parentLink.Type != proton.LinkTypeFolder {
t.Fatalf("parentLink is not of folder type")
}
f, err := os.Open(filepath)
if err != nil {
t.Fatal(err)
}
defer f.Close()
info, err := os.Stat(filepath)
if err != nil {
t.Fatal(err)
}
in := bufio.NewReader(f)
_, _, err = protonDrive.UploadFileByReader(ctx, parentLink.LinkID, name, info.ModTime(), in, testParam)
if err != expectedError {
t.Fatal(err)
}
}
func uploadFileByFilepath(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, parent, name string, filepath string, testParam int) {
uploadFileByFilepathWithError(t, ctx, protonDrive, parent, name, filepath, testParam, nil)
}
func downloadFile(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, parent, name string, filepath string, data string) {
downloadFileWithOffset(t, ctx, protonDrive, parent, name, filepath, data, 0)
}
func downloadFileWithOffset(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, parent, name string, filepath string, data string, offset int64) {
parentLink := protonDrive.RootLink
if parent != "" {
targetFolderLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, parent, true, false)
if err != nil {
t.Fatal(err)
}
if targetFolderLink == nil {
t.Fatalf("Folder %v not found", parent)
}
parentLink = targetFolderLink
}
if parentLink.Type != proton.LinkTypeFolder {
t.Fatalf("parentLink is not of folder type")
}
targetFileLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, name, false, false)
if err != nil {
t.Fatal(err)
}
if targetFileLink == nil {
t.Fatalf("File %v not found", name)
} else {
reader, sizeOnServer, fileSystemAttr, err := protonDrive.DownloadFileByID(ctx, targetFileLink.LinkID, offset)
if err != nil {
t.Fatal(err)
}
downloadedData, err := io.ReadAll(reader)
if err != nil {
t.Fatal(err)
}
/* Check file metadata */
if fileSystemAttr == nil {
t.Fatalf("FileSystemAttr should not be nil")
} else {
if fileSystemAttr.Size != 0 && sizeOnServer == fileSystemAttr.Size {
t.Fatalf("Not possible due to encryption file overhead")
}
if offset == 0 && len(downloadedData) != int(fileSystemAttr.Size) {
t.Fatalf("Downloaded file size != uploaded file size: %#v vs %#v", len(downloadedData), int(fileSystemAttr.Size))
}
}
if filepath != "" {
originalData, err := os.ReadFile(filepath)
if err != nil {
t.Fatal(err)
}
originalData = originalData[offset:]
if !bytes.Equal(downloadedData, originalData) {
t.Fatalf("Downloaded content is different from the original content")
}
} else if data != "" {
if !bytes.Equal(downloadedData, []byte(data)) {
t.Fatalf("Downloaded content is different from the original content")
}
} else {
t.Fatalf("Nothing to verify against")
}
}
}
func checkRevisions(protonDrive *ProtonDrive, ctx context.Context, t *testing.T, name string, totalRevisions, activeRevisions, draftRevisions, obseleteRevisions int) {
targetFileLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, name, false, true)
if err != nil {
t.Fatal(err)
}
if targetFileLink == nil {
t.Fatalf("File %v not found", name)
} else {
revisions, err := protonDrive.c.ListRevisions(ctx, protonDrive.MainShare.ShareID, targetFileLink.LinkID)
if err != nil {
t.Fatal(err)
}
if len(revisions) != totalRevisions {
t.Fatalf("Missing revision")
}
for i := range revisions {
if revisions[i].State == proton.RevisionStateActive {
activeRevisions--
}
if revisions[i].State == proton.RevisionStateDraft {
draftRevisions--
}
if revisions[i].State == proton.RevisionStateObsolete {
obseleteRevisions--
}
}
if activeRevisions != 0 || draftRevisions != 0 || obseleteRevisions != 0 {
t.Fatalf("Wrong revision count (should be all 0 here) %v %v %v", activeRevisions, draftRevisions, obseleteRevisions)
}
}
}
// During the integration test, the name much be unique since the link is returned by recursively search for the name from root
func deleteBySearchingFromRoot(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, name string, isFolder bool, listAllActiveOrDraftFiles bool) {
targetLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, name, isFolder, listAllActiveOrDraftFiles)
if err != nil {
t.Fatal(err)
}
if targetLink == nil {
t.Fatalf("Target %v to be deleted not found", name)
} else {
if isFolder {
err = protonDrive.MoveFolderToTrashByID(ctx, targetLink.LinkID, false)
if err != nil {
t.Fatal(err)
}
} else {
err = protonDrive.MoveFileToTrashByID(ctx, targetLink.LinkID)
if err != nil {
t.Fatal(err)
}
}
}
}
func checkActiveFileListing(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, expectedPaths []string) {
{
paths := make([]string, 0)
err := protonDrive.listDirectoriesRecursively(ctx, protonDrive.MainShareKR, protonDrive.RootLink, false, -1, 0, true, "", &paths)
if err != nil {
t.Fatal(err)
}
if len(paths) != len(expectedPaths) {
t.Fatalf("Total path returned is differs from expected\nReturned %#v\nExpected: %#v\n", paths, expectedPaths)
}
for i := range paths {
if paths[i] != expectedPaths[i] {
t.Fatalf("The path returned is differs from the path expected\nReturned %#v\nExpected: %#v\n", paths, expectedPaths)
}
}
}
{
paths := make([]string, 0)
err := protonDrive.listDirectoriesRecursively(ctx, protonDrive.MainShareKR, protonDrive.RootLink, false, -1, 0, false, "", &paths)
if err != nil {
t.Fatal(err)
}
// transform
newExpectedPath := make([]string, 0)
newExpectedPath = append(newExpectedPath, "/root")
for i := range expectedPaths {
newExpectedPath = append(newExpectedPath, "/root"+expectedPaths[i])
}
if len(paths) != len(newExpectedPath) {
t.Fatalf("Total path returned is differs from expected\nReturned %#v\nExpected: %#v\n", paths, newExpectedPath)
}
for i := range paths {
if paths[i] != newExpectedPath[i] {
t.Fatalf("The path returned is differs from the path expected\nReturned %#v\nExpected: %#v\n", paths, newExpectedPath)
}
}
}
}
func moveFolder(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, srcFolderName, dstParentFolderName string) {
targetSrcFolderLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, srcFolderName, true, false)
if err != nil {
t.Fatal(err)
}
targetDestFolderLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, dstParentFolderName, true, false)
if err != nil {
t.Fatal(err)
}
if targetSrcFolderLink == nil || targetDestFolderLink == nil {
t.Fatalf("Folder %s or %s found", srcFolderName, dstParentFolderName)
} else {
err := protonDrive.MoveFolder(ctx, targetSrcFolderLink, targetDestFolderLink, srcFolderName)
if err != nil {
t.Fatal(err)
}
}
}
func moveFile(t *testing.T, ctx context.Context, protonDrive *ProtonDrive, srcFileName, dstParentFolderName string) {
targetSrcFileLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, srcFileName, false, false)
if err != nil {
t.Fatal(err)
}
targetDestFolderLink, err := protonDrive.searchByNameRecursivelyFromRoot(ctx, dstParentFolderName, true, false)
if err != nil {
t.Fatal(err)
}
if targetSrcFileLink == nil || targetDestFolderLink == nil {
t.Fatalf("File %s or folder %s found", srcFileName, dstParentFolderName)
} else {
err := protonDrive.MoveFile(ctx, targetSrcFileLink, targetDestFolderLink, srcFileName)
if err != nil {
t.Fatal(err)
}
}
}
|