xref: /aosp_15_r20/external/okio/okio-testing-support/src/commonMain/kotlin/okio/TestingCommon.kt (revision f9742813c14b702d71392179818a9e591da8620c)
1 /*
2  * Copyright (C) 2019 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package okio
17 
18 import kotlin.random.Random
19 import kotlin.test.assertEquals
20 import kotlin.time.Duration
21 import okio.ByteString.Companion.toByteString
22 
Charnull23 fun Char.repeat(count: Int): String {
24   return toString().repeat(count)
25 }
26 
assertArrayEqualsnull27 fun assertArrayEquals(a: ByteArray, b: ByteArray) {
28   assertEquals(a.contentToString(), b.contentToString())
29 }
30 
randomBytesnull31 fun randomBytes(length: Int): ByteString {
32   val random = Random(0)
33   val randomBytes = ByteArray(length)
34   random.nextBytes(randomBytes)
35   return ByteString.of(*randomBytes)
36 }
37 
randomTokennull38 fun randomToken(length: Int) = Random.nextBytes(length).toByteString(0, length).hex()
39 
40 expect fun isBrowser(): Boolean
41 
42 expect fun isWasm(): Boolean
43 
44 val FileMetadata.createdAt: Instant?
45   get() {
46     val createdAt = createdAtMillis ?: return null
47     return fromEpochMilliseconds(createdAt)
48   }
49 
50 val FileMetadata.lastModifiedAt: Instant?
51   get() {
52     val lastModifiedAt = lastModifiedAtMillis ?: return null
53     return fromEpochMilliseconds(lastModifiedAt)
54   }
55 
56 val FileMetadata.lastAccessedAt: Instant?
57   get() {
58     val lastAccessedAt = lastAccessedAtMillis ?: return null
59     return fromEpochMilliseconds(lastAccessedAt)
60   }
61 
62 /*
63  * This file contains some declarations from kotlinx.datetime used by [AbstractFileSystemTest], but
64  * that we can't use because that library isn't yet available for WASM. We should delete these when
65  * WASM is supported in kotlinx.datetime.
66  */
67 
68 expect interface Clock {
nownull69   fun now(): Instant
70 }
71 
72 expect class Instant : Comparable<Instant> {
73   val epochSeconds: Long
74 
75   operator fun plus(duration: Duration): Instant
76 
77   operator fun minus(duration: Duration): Instant
78 }
79 
fromEpochSecondsnull80 expect fun fromEpochSeconds(
81   epochSeconds: Long,
82 ): Instant
83 
84 expect fun fromEpochMilliseconds(epochMilliseconds: Long): Instant
85 
86 expect val FileSystem.isFakeFileSystem: Boolean
87 
88 expect val FileSystem.allowSymlinks: Boolean
89 
90 expect val FileSystem.allowReadsWhileWriting: Boolean
91 
92 expect var FileSystem.workingDirectory: Path
93