2013-09-18 20:01:46 +04:00
#!/usr/bin/env gjs
2013-09-19 00:34:57 +04:00
//
// Copyright (C) 2013 Colin Walters <walters@verbum.org>
//
2018-01-30 22:26:26 +03:00
// SPDX-License-Identifier: LGPL-2.0+
//
2013-09-19 00:34:57 +04:00
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
// License along with this library. If not, see <https://www.gnu.org/licenses/>.
2013-09-18 20:01:46 +04:00
2021-04-10 00:54:21 +03:00
const ByteArray = imports . byteArray ;
2013-09-18 20:01:46 +04:00
const Gio = imports . gi . Gio ;
const OSTree = imports . gi . OSTree ;
function assertEquals ( a , b ) {
if ( a != b )
throw new Error ( "assertion failed " + JSON . stringify ( a ) + " == " + JSON . stringify ( b ) ) ;
}
2021-04-09 03:35:54 +03:00
function assertThrows ( s , f ) {
let success = false ;
try {
f ( ) ;
success = true ;
} catch ( e ) {
let msg = e . toString ( ) ;
if ( msg . indexOf ( s ) == - 1 ) {
throw new Error ( "Error message didn't match '" + s + "': " + msg )
}
}
if ( success ) {
throw new Error ( "Function was expected to throw, but didn't" )
}
}
2017-07-20 17:32:44 +03:00
print ( '1..1' )
2013-09-18 20:01:46 +04:00
let testDataDir = Gio . File . new _for _path ( 'test-data' ) ;
testDataDir . make _directory ( null ) ;
testDataDir . get _child ( 'some-file' ) . replace _contents ( "hello world!" , null , false , 0 , null ) ;
let repoPath = Gio . File . new _for _path ( 'repo' ) ;
let repo = OSTree . Repo . new ( repoPath ) ;
repo . create ( OSTree . RepoMode . ARCHIVE _Z2 , null ) ;
repo . open ( null ) ;
assertEquals ( repo . get _mode ( ) , OSTree . RepoMode . ARCHIVE _Z2 ) ;
repo . prepare _transaction ( null ) ;
let mtree = OSTree . MutableTree . new ( ) ;
repo . write _directory _to _mtree ( testDataDir , mtree , null , null ) ;
let [ , dirTree ] = repo . write _mtree ( mtree , null ) ;
let [ , commit ] = repo . write _commit ( null , 'Some subject' , 'Some body' , null , dirTree , null ) ;
print ( "commit => " + commit ) ;
2021-04-08 00:03:15 +03:00
// Test direct write APIs
let inline _content = "default 0.0.0.0\nloopback 127.0.0.0\nlink-local 169.254.0.0\n" ;
2021-04-09 03:35:54 +03:00
let networks _checksum = "8aaa9dc13a0c5839fe4a277756798c609c53fac6fa2290314ecfef9041065873" ;
2021-04-08 00:03:15 +03:00
let regfile _mode = 33188 ; // 0o100000 | 0o644 (but in decimal so old gjs works)
let inline _checksum = repo . write _regfile _inline ( null , 0 , 0 , regfile _mode , null , inline _content , null ) ;
2021-04-10 00:54:21 +03:00
assertEquals ( inline _checksum , networks _checksum ) ;
2021-04-09 03:35:54 +03:00
assertThrows ( "Corrupted file object" , function ( ) {
2021-04-08 00:03:15 +03:00
// Changed an a to b from above to make the checksum not match
2021-04-08 00:03:15 +03:00
repo . write _regfile _inline ( "8baa9dc13a0c5839fe4a277756798c609c53fac6fa2290314ecfef9041065873" , 0 , 0 , regfile _mode , null , inline _content , null ) ;
2021-04-09 03:35:54 +03:00
} ) ;
2021-04-08 00:03:15 +03:00
2021-04-10 00:54:21 +03:00
{
let [ _ , instream , info , xattrs ] = repo . load _file ( networks _checksum , null ) ;
// Read the whole content into a string via this amazing rube goldberg machine
let datain = Gio . DataInputStream . new ( instream ) ;
let bufw = Gio . MemoryOutputStream . new _resizable ( ) ;
bufw . splice ( datain , 0 , null ) ;
bufw . close ( null ) ;
let contents = bufw . steal _as _bytes ( ) ;
let contentsStr = ByteArray . toString ( contents . get _data ( ) )
assertEquals ( contentsStr , inline _content ) ;
let uid = info . get _attribute _uint32 ( "unix::uid" ) ;
assertEquals ( uid , 0 ) ;
let mode = info . get _attribute _uint32 ( "unix::mode" ) ;
assertEquals ( mode , regfile _mode ) ;
assertEquals ( xattrs . n _children ( ) , 0 ) ;
}
2013-09-18 20:01:46 +04:00
repo . commit _transaction ( null , null ) ;
let [ , root , checksum ] = repo . read _commit ( commit , null ) ;
let child = root . get _child ( 'some-file' ) ;
let info = child . query _info ( "standard::name,standard::type,standard::size" , 0 , null ) ;
assertEquals ( info . get _size ( ) , 12 ) ;
2017-05-04 22:10:57 +03:00
// Write a ref and read it back
repo . prepare _transaction ( null ) ;
repo . transaction _set _refspec ( 'someref' , commit ) ;
repo . commit _transaction ( null , null ) ;
let [ , readCommit ] = repo . resolve _rev ( 'someref' , false ) ;
assertEquals ( readCommit , commit ) ;
// Delete a ref
repo . prepare _transaction ( null ) ;
repo . transaction _set _refspec ( 'someref' , null ) ;
repo . commit _transaction ( null , null ) ;
[ , readCommit ] = repo . resolve _rev ( 'someref' , true ) ;
assertEquals ( readCommit , null ) ;
2021-04-09 03:35:54 +03:00
// Test direct write API for regular files
let clen = inline _content . length ;
assertThrows ( "Cannot currently use" , function ( ) {
let w = repo . write _regfile ( null , 0 , 0 , regfile _mode , clen , null ) ;
} ) ;
let bareRepoPath = Gio . File . new _for _path ( 'repo' ) ;
let repo _bareu = OSTree . Repo . new ( Gio . File . new _for _path ( 'repo-bare' ) ) ;
repo _bareu . create ( OSTree . RepoMode . BARE _USER _ONLY , null ) ;
let w = repo _bareu . write _regfile ( null , 0 , 0 , regfile _mode , clen , null ) ;
// Test multiple write() calls
w . write ( inline _content . slice ( 0 , 4 ) , null )
w . write ( inline _content . slice ( 4 , 10 ) , null )
w . write ( inline _content . slice ( 10 ) , null )
let actual _checksum = w . finish ( null )
assertEquals ( actual _checksum , networks _checksum )
2021-04-15 03:43:53 +03:00
// Basic locking API sanity test
repo . lock _push ( OSTree . RepoLockType . SHARED , null ) ;
repo . lock _push ( OSTree . RepoLockType . SHARED , null ) ;
2021-04-29 06:13:15 +03:00
repo . lock _pop ( OSTree . RepoLockType . SHARED , null ) ;
repo . lock _pop ( OSTree . RepoLockType . SHARED , null ) ;
2021-04-15 03:43:53 +03:00
repo . lock _push ( OSTree . RepoLockType . EXCLUSIVE , null ) ;
2021-04-29 06:13:15 +03:00
repo . lock _pop ( OSTree . RepoLockType . EXCLUSIVE , null ) ;
2021-04-15 03:43:53 +03:00
2017-07-20 17:32:44 +03:00
print ( "ok test-core" ) ;