|
| 1 | +package local |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/base64" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strconv" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/OpenListTeam/OpenList/v4/internal/driver" |
| 14 | + "github.com/OpenListTeam/OpenList/v4/internal/model" |
| 15 | + "github.com/OpenListTeam/OpenList/v4/internal/stream" |
| 16 | + "github.com/OpenListTeam/OpenList/v4/pkg/utils" |
| 17 | +) |
| 18 | + |
| 19 | +func TestLocalPutGenerateCAS(t *testing.T) { |
| 20 | + root := t.TempDir() |
| 21 | + drv := &Local{ |
| 22 | + Addition: Addition{ |
| 23 | + RootPath: driver.RootPath{RootFolderPath: root}, |
| 24 | + GenerateCAS: true, |
| 25 | + }, |
| 26 | + } |
| 27 | + if err := drv.Init(context.Background()); err != nil { |
| 28 | + t.Fatalf("init local driver: %v", err) |
| 29 | + } |
| 30 | + |
| 31 | + data := []byte("hello openlist cas") |
| 32 | + modTime := time.Unix(1710000000, 0) |
| 33 | + file := &stream.FileStream{ |
| 34 | + Ctx: context.Background(), |
| 35 | + Obj: &model.Object{ |
| 36 | + Name: "hello.txt", |
| 37 | + Size: int64(len(data)), |
| 38 | + Modified: modTime, |
| 39 | + Ctime: modTime, |
| 40 | + }, |
| 41 | + Reader: bytes.NewReader(data), |
| 42 | + } |
| 43 | + dstDir := &model.Object{ |
| 44 | + Path: root, |
| 45 | + Name: filepath.Base(root), |
| 46 | + IsFolder: true, |
| 47 | + } |
| 48 | + |
| 49 | + if err := drv.Put(context.Background(), dstDir, file, func(float64) {}); err != nil { |
| 50 | + t.Fatalf("put file: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + if _, err := os.Stat(filepath.Join(root, "hello.txt")); err != nil { |
| 54 | + t.Fatalf("stat source file: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + casContent, err := os.ReadFile(filepath.Join(root, "hello.txt.cas")) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("read cas file: %v", err) |
| 60 | + } |
| 61 | + rawPayload, err := base64.StdEncoding.DecodeString(string(casContent)) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("decode cas file: %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + var payload casPayload |
| 67 | + if err = utils.Json.Unmarshal(rawPayload, &payload); err != nil { |
| 68 | + t.Fatalf("unmarshal cas payload: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + if payload.Name != "hello.txt" { |
| 72 | + t.Fatalf("unexpected cas name: %s", payload.Name) |
| 73 | + } |
| 74 | + if payload.Size != int64(len(data)) { |
| 75 | + t.Fatalf("unexpected cas size: %d", payload.Size) |
| 76 | + } |
| 77 | + |
| 78 | + expectMD5 := utils.HashData(utils.MD5, data) |
| 79 | + if payload.MD5 != expectMD5 { |
| 80 | + t.Fatalf("unexpected cas md5: %s", payload.MD5) |
| 81 | + } |
| 82 | + if payload.SliceMD5 != expectMD5 { |
| 83 | + t.Fatalf("unexpected cas slice md5: %s", payload.SliceMD5) |
| 84 | + } |
| 85 | + if _, err = strconv.ParseInt(payload.CreateTime, 10, 64); err != nil { |
| 86 | + t.Fatalf("unexpected cas create time: %v", err) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestLocalPutGenerateCASAndDeleteSource(t *testing.T) { |
| 91 | + root := t.TempDir() |
| 92 | + drv := &Local{ |
| 93 | + Addition: Addition{ |
| 94 | + RootPath: driver.RootPath{RootFolderPath: root}, |
| 95 | + GenerateCAS: true, |
| 96 | + DeleteSource: true, |
| 97 | + }, |
| 98 | + } |
| 99 | + if err := drv.Init(context.Background()); err != nil { |
| 100 | + t.Fatalf("init local driver: %v", err) |
| 101 | + } |
| 102 | + |
| 103 | + data := []byte("hello openlist cas delete source") |
| 104 | + file := &stream.FileStream{ |
| 105 | + Ctx: context.Background(), |
| 106 | + Obj: &model.Object{ |
| 107 | + Name: "delete.txt", |
| 108 | + Size: int64(len(data)), |
| 109 | + Modified: time.Unix(1710000000, 0), |
| 110 | + }, |
| 111 | + Reader: bytes.NewReader(data), |
| 112 | + } |
| 113 | + dstDir := &model.Object{ |
| 114 | + Path: root, |
| 115 | + Name: filepath.Base(root), |
| 116 | + IsFolder: true, |
| 117 | + } |
| 118 | + |
| 119 | + if err := drv.Put(context.Background(), dstDir, file, func(float64) {}); err != nil { |
| 120 | + t.Fatalf("put file: %v", err) |
| 121 | + } |
| 122 | + |
| 123 | + if _, err := os.Stat(filepath.Join(root, "delete.txt")); !os.IsNotExist(err) { |
| 124 | + t.Fatalf("source file should be removed, got err=%v", err) |
| 125 | + } |
| 126 | + if _, err := os.Stat(filepath.Join(root, "delete.txt.cas")); err != nil { |
| 127 | + t.Fatalf("cas file should exist: %v", err) |
| 128 | + } |
| 129 | +} |
0 commit comments