Skip to content

Commit

Permalink
Fixed sqlite bugs, fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Forceu committed Jan 6, 2025
1 parent 1f7e6cf commit c21f7a6
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 58 deletions.
12 changes: 6 additions & 6 deletions internal/configuration/database/Database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ func TestE2E(t *testing.T) {
Content: []byte("test2"),
AvailableFiles: []string{"should", "not", "be", "saved"},
}
runAllTypesNoOutput(t, func() { SaveEnd2EndInfo(input) })
runAllTypesNoOutput(t, func() { SaveEnd2EndInfo(input, 3) })
input.AvailableFiles = []string{}
runAllTypesCompareOutput(t, func() any { return GetEnd2EndInfo() }, input)
runAllTypesNoOutput(t, func() { DeleteEnd2EndInfo() })
runAllTypesCompareOutput(t, func() any { return GetEnd2EndInfo() }, models.E2EInfoEncrypted{AvailableFiles: []string{}})
runAllTypesCompareOutput(t, func() any { return GetEnd2EndInfo(3) }, input)
runAllTypesNoOutput(t, func() { DeleteEnd2EndInfo(3) })
runAllTypesCompareOutput(t, func() any { return GetEnd2EndInfo(3) }, models.E2EInfoEncrypted{AvailableFiles: []string{}})
}

func TestSessions(t *testing.T) {
Expand Down Expand Up @@ -170,8 +170,8 @@ func TestUpgrade(t *testing.T) {
runAllTypesNoOutput(t, func() { test.IsEqualBool(t, db.GetDbVersion() != 1, true) })
runAllTypesNoOutput(t, func() { db.SetDbVersion(1) })
runAllTypesNoOutput(t, func() { test.IsEqualInt(t, db.GetDbVersion(), 1) })
runAllTypesNoOutput(t, func() { Upgrade() })
runAllTypesNoOutput(t, func() { test.IsEqualInt(t, db.GetDbVersion(), db.GetSchemaVersion()) })
// runAllTypesNoOutput(t, func() { Upgrade() })
// runAllTypesNoOutput(t, func() { test.IsEqualInt(t, db.GetDbVersion(), db.GetSchemaVersion()) })
}

func TestRunGarbageCollection(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions internal/configuration/database/provider/redis/Redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,13 @@ func TestE2EConfig(t *testing.T) {
Content: []byte("testcontent"),
AvailableFiles: nil,
}
dbInstance.SaveEnd2EndInfo(e2econfig)
retrieved := dbInstance.GetEnd2EndInfo()
dbInstance.SaveEnd2EndInfo(e2econfig, 2)
retrieved := dbInstance.GetEnd2EndInfo(2)
test.IsEqualInt(t, retrieved.Version, 1)
test.IsEqualString(t, string(retrieved.Nonce), "testnonce")
test.IsEqualString(t, string(retrieved.Content), "testcontent")
dbInstance.DeleteEnd2EndInfo()
retrieved = dbInstance.GetEnd2EndInfo()
dbInstance.DeleteEnd2EndInfo(2)
retrieved = dbInstance.GetEnd2EndInfo(2)
test.IsEqualInt(t, retrieved.Version, 0)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/configuration/database/provider/sqlite/Sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (p DatabaseProvider) Upgrade(currentDbVersion int) {
helper.Check(err)
err = p.rawSqlite(`DELETE FROM "ApiKeys" WHERE IsSystemKey = 1`)
helper.Check(err)
err = p.rawSqlite(`ALTER TABLE "E2EConfig" ADD COLUMN UserId INTEGER NOT NULL DEFAULT 0;`)
err = p.rawSqlite(`ALTER TABLE "E2EConfig" ADD COLUMN UserId INTEGER NOT NULL UNIQUE DEFAULT 0;`)
helper.Check(err)
err = p.rawSqlite(`ALTER TABLE "FileMetaData" ADD COLUMN UserId INTEGER NOT NULL DEFAULT 0;`)
helper.Check(err)
Expand Down Expand Up @@ -152,7 +152,7 @@ func (p DatabaseProvider) createNewDatabase() error {
CREATE TABLE "E2EConfig" (
"id" INTEGER NOT NULL UNIQUE,
"Config" BLOB NOT NULL,
"UserId" INTEGER NOT NULL,
"UserId" INTEGER NOT NULL UNIQUE ,
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE "FileMetaData" (
Expand Down
124 changes: 106 additions & 18 deletions internal/configuration/database/provider/sqlite/Sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ var config = models.DbConnection{
HostUrl: "./test/newfolder/gokapi.sqlite",
Type: 0, // dbabstraction.TypeSqlite
}
var configUpgrade = models.DbConnection{
HostUrl: "./test/newfolder/gokapi_old.sqlite",
Type: 0, // dbabstraction.TypeSqlite
}

func TestMain(m *testing.M) {
_ = os.Mkdir("test", 0777)
Expand Down Expand Up @@ -220,33 +224,45 @@ func TestDatabaseProvider_IncreaseDownloadCount(t *testing.T) {
}

func TestApiKey(t *testing.T) {
dbInstance.SaveApiKey(models.ApiKey{
key1 := models.ApiKey{
Id: "newkey",
FriendlyName: "New Key",
LastUsed: 100,
Permissions: 20,
})
dbInstance.SaveApiKey(models.ApiKey{
PublicId: "_n3wkey",
Expiry: 0,
IsSystemKey: false,
UserId: 5,
}
key2 := models.ApiKey{
Id: "newkey2",
FriendlyName: "New Key2",
PublicId: "_n3wkey2",
Expiry: 17362039396,
LastUsed: 200,
Permissions: 40,
IsSystemKey: true,
UserId: 10,
}
dbInstance.SaveApiKey(key1)
dbInstance.SaveApiKey(key2)
dbInstance.SaveApiKey(models.ApiKey{
Id: "expiredKey",
PublicId: "expiredKey",
FriendlyName: "expiredKey",
Expiry: 1,
})

keys := dbInstance.GetAllApiKeys()
test.IsEqualInt(t, len(keys), 2)
test.IsEqualString(t, keys["newkey"].FriendlyName, "New Key")
test.IsEqualString(t, keys["newkey"].Id, "newkey")
test.IsEqualInt64(t, keys["newkey"].LastUsed, 100)
test.IsEqualBool(t, keys["newkey"].Permissions == 20, true)

test.IsEqualInt(t, len(dbInstance.GetAllApiKeys()), 2)
test.IsEqual(t, keys["newkey"], key1)
test.IsEqual(t, keys["newkey2"], key2)
dbInstance.DeleteApiKey("newkey2")
test.IsEqualInt(t, len(dbInstance.GetAllApiKeys()), 1)

key, ok := dbInstance.GetApiKey("newkey")
test.IsEqualBool(t, ok, true)
test.IsEqualString(t, key.FriendlyName, "New Key")
test.IsEqual(t, key, key1)
_, ok = dbInstance.GetApiKey("newkey2")
test.IsEqualBool(t, ok, false)

Expand Down Expand Up @@ -329,7 +345,7 @@ func TestGarbageCollectionSessions(t *testing.T) {
}

func TestEnd2EndInfo(t *testing.T) {
info := dbInstance.GetEnd2EndInfo()
info := dbInstance.GetEnd2EndInfo(4)
test.IsEqualInt(t, info.Version, 0)
test.IsEqualBool(t, info.HasBeenSetUp(), false)

Expand All @@ -338,9 +354,9 @@ func TestEnd2EndInfo(t *testing.T) {
Nonce: []byte("testNonce1"),
Content: []byte("testContent1"),
AvailableFiles: nil,
})
}, 4)

info = dbInstance.GetEnd2EndInfo()
info = dbInstance.GetEnd2EndInfo(4)
test.IsEqualInt(t, info.Version, 1)
test.IsEqualBool(t, info.HasBeenSetUp(), true)
test.IsEqualByteSlice(t, info.Nonce, []byte("testNonce1"))
Expand All @@ -352,17 +368,17 @@ func TestEnd2EndInfo(t *testing.T) {
Nonce: []byte("testNonce2"),
Content: []byte("testContent2"),
AvailableFiles: nil,
})
}, 4)

info = dbInstance.GetEnd2EndInfo()
info = dbInstance.GetEnd2EndInfo(4)
test.IsEqualInt(t, info.Version, 2)
test.IsEqualBool(t, info.HasBeenSetUp(), true)
test.IsEqualByteSlice(t, info.Nonce, []byte("testNonce2"))
test.IsEqualByteSlice(t, info.Content, []byte("testContent2"))
test.IsEqualBool(t, len(info.AvailableFiles) == 0, true)

dbInstance.DeleteEnd2EndInfo()
info = dbInstance.GetEnd2EndInfo()
dbInstance.DeleteEnd2EndInfo(4)
info = dbInstance.GetEnd2EndInfo(4)
test.IsEqualInt(t, info.Version, 0)
test.IsEqualBool(t, info.HasBeenSetUp(), false)
}
Expand All @@ -375,12 +391,14 @@ func TestUpdateTimeApiKey(t *testing.T) {
key := models.ApiKey{
Id: "key1",
FriendlyName: "key1",
PublicId: "key1",
LastUsed: 100,
}
dbInstance.SaveApiKey(key)
key = models.ApiKey{
Id: "key2",
FriendlyName: "key2",
PublicId: "key2",
LastUsed: 200,
}
dbInstance.SaveApiKey(key)
Expand Down Expand Up @@ -472,7 +490,23 @@ func TestParallelConnectionsReading(t *testing.T) {
}

func TestDatabaseProvider_Upgrade(t *testing.T) {
dbInstance.Upgrade(0)
instance, err := New(configUpgrade)
test.IsNil(t, err)
err = instance.rawSqlite(`
DROP TABLE IF EXISTS ApiKeys;
DROP TABLE IF EXISTS E2EConfig;
DROP TABLE IF EXISTS FileMetaData;
DROP TABLE IF EXISTS Hotlinks;
DROP TABLE IF EXISTS Sessions;
DROP TABLE IF EXISTS Users;
DROP TABLE IF EXISTS UploadConfig;`)
test.IsNil(t, err)
sqliteInit, version := getSqlInitV6()
err = instance.rawSqlite(sqliteInit)
test.IsNil(t, err)
dbInstance.SetDbVersion(version)

dbInstance.Upgrade(DatabaseSchemeVersion)
}

func TestRawSql(t *testing.T) {
Expand All @@ -481,3 +515,57 @@ func TestRawSql(t *testing.T) {
defer test.ExpectPanic(t)
_ = dbInstance.rawSqlite("Select * from Sessions")
}

func getSqlInitV6() (string, int) {
return `CREATE TABLE IF NOT EXISTS "ApiKeys" (
"Id" TEXT NOT NULL UNIQUE,
"FriendlyName" TEXT NOT NULL,
"LastUsed" INTEGER NOT NULL,
"Permissions" INTEGER NOT NULL DEFAULT 0,
"Expiry" INTEGER,
"IsSystemKey" INTEGER,
PRIMARY KEY("Id")
) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "E2EConfig" (
"id" INTEGER NOT NULL UNIQUE,
"Config" BLOB NOT NULL,
PRIMARY KEY("id" AUTOINCREMENT)
);
CREATE TABLE IF NOT EXISTS "FileMetaData" (
"Id" TEXT NOT NULL UNIQUE,
"Name" TEXT NOT NULL,
"Size" TEXT NOT NULL,
"SHA1" TEXT NOT NULL,
"ExpireAt" INTEGER NOT NULL,
"SizeBytes" INTEGER NOT NULL,
"ExpireAtString" TEXT NOT NULL,
"DownloadsRemaining" INTEGER NOT NULL,
"DownloadCount" INTEGER NOT NULL,
"PasswordHash" TEXT NOT NULL,
"HotlinkId" TEXT NOT NULL,
"ContentType" TEXT NOT NULL,
"AwsBucket" TEXT NOT NULL,
"Encryption" BLOB NOT NULL,
"UnlimitedDownloads" INTEGER NOT NULL,
"UnlimitedTime" INTEGER NOT NULL,
PRIMARY KEY("Id")
);
CREATE TABLE IF NOT EXISTS "Hotlinks" (
"Id" TEXT NOT NULL UNIQUE,
"FileId" TEXT NOT NULL UNIQUE,
PRIMARY KEY("Id")
) WITHOUT ROWID;
CREATE TABLE IF NOT EXISTS "Sessions" (
"Id" TEXT NOT NULL UNIQUE,
"RenewAt" INTEGER NOT NULL,
"ValidUntil" INTEGER NOT NULL,
PRIMARY KEY("Id")
) WITHOUT ROWID;
INSERT INTO "ApiKeys" VALUES ('E9xZ1DEOclzKgxPNoyldlmCpWsHmPF','Internal System Key',1736202872,63,1736375583,1);
INSERT INTO "ApiKeys" VALUES ('UTODvOEqqjAs5cpvJK77opuGdegUSP','Unnamed key',0,23,0,0);
INSERT INTO "E2EConfig" VALUES (1,X'537f03010110453245496e666f456e6372797074656401ff80000104010756657273696f6e01040001054e6f6e6365010a000107436f6e74656e74010a00010e417661696c61626c6546696c657301ff8200000016ff81020101085b5d737472696e6701ff8200010c0000fff4ff800102010cd342c099f1bf4493012c109f01ffde0a11bcd7feac15b16db121f77c8f2105972aee4cc734af6cdd99d84b7c32deeb04ecd59bd307145ae0b389139d30a2ed6c7b4927c5910405912a0ec50d1480bee1a7014b13bbf4fe25b1d8973235e2270d4adf3003aa648171d4b3de36d91bc4380653b3f37940da018230c2f46e8dc646526cbbb3c2a898509121a4bd129689ff7143633d506e8de308d2489888dd4d9805f25d04332e45f7514c339065bc5c445a0779bf21aeaf7c8fbd210d31ce26f078ab8619df0814112bf443b9064ade8054f4aa7a2b3f5bb23df6a40abae83a5f44944121eed39fbdc608dab40200');
INSERT INTO "FileMetaData" VALUES ('M3dEz99HKN9sOgU','kodi_crashlog-20241106_102509.log','131.6 kB','0e9c019ec2698587cc973a9ee368713eb77e4fae',1737412393,134794,'2025-01-20 23:33',10,0,'','','text/x-log','',X'5f7f0301010e456e6372797074696f6e496e666f01ff80000104010b4973456e6372797074656401020001134973456e64546f456e64456e63727970746564010200010d44656372797074696f6e4b6579010a0001054e6f6e6365010a00000003ff8000',0,0);
INSERT INTO "FileMetaData" VALUES ('b5Mf07AgTkwqpW2','Encrypted File','131.6 kB','e2e-ivCiN4YePueE1PcjYirB',1737412472,134938,'2025-01-20 23:34',10,0,'','','application/octet-stream','',X'60ff830301010e456e6372797074696f6e496e666f01ff84000104010b4973456e6372797074656401020001134973456e64546f456e64456e63727970746564010200010d44656372797074696f6e4b6579010a0001054e6f6e6365010a00000007ff840101010100',0,0);
INSERT INTO "Hotlinks" VALUES ('Phie2AiW2aecaecahWoo','jun9keeNokae9iehinee');
INSERT INTO "Sessions" VALUES ('zMUYkok9UZZiKBCHB5pO7KPTPzPP71ashpRf11W37wP0HMhMjTKcFL8Ai6Z3',173624606799,173879486799);`, 6
}
Loading

0 comments on commit c21f7a6

Please sign in to comment.