Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OdbBackendFixture.cs « LibGit2Sharp.Tests - github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7420c2f4b5b0a9a13d0efecce11cfb73f242c7b6 (plain)
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
    public class OdbBackendFixture : BaseFixture
    {
        [Fact]
        public void SimpleOdbBackendFixtureTest()
        {
            var scd = new SelfCleaningDirectory(this);

            using (Repository repository = Repository.Init(scd.RootedDirectoryPath))
            {
                repository.ObjectDatabase.AddBackend(new MockOdbBackend(), priority: 5);

                String filePath = Path.Combine(scd.RootedDirectoryPath, "file.txt");
                String fileContents = "Hello!";

                // Exercises read, write, writestream, exists
                File.WriteAllText(filePath, fileContents);
                repository.Index.Stage(filePath);

                var signature = new Signature("SimpleOdbBackendFixtureTest", "user@example.com", DateTimeOffset.Now);
                repository.Commit(String.Empty, signature, signature);

                // Exercises read
                var blob = repository.Lookup<Blob>(new ObjectId("69342c5c39e5ae5f0077aecc32c0f81811fb8193"));

                Assert.NotNull(blob);
                Assert.True(fileContents.Length == blob.Size);
            }
        }

        #region MockOdbBackend

        private class MockOdbBackend : OdbBackend
        {
            protected override OdbBackendOperations SupportedOperations
            {
                get
                {
                    return OdbBackendOperations.Read |
                        OdbBackendOperations.ReadPrefix |
                        OdbBackendOperations.Write |
                        OdbBackendOperations.WriteStream |
                        OdbBackendOperations.Exists;
                }
            }

            public override int Read(byte[] oid, out Stream data, out GitObjectType objectType)
            {
                data = null;
                objectType = GitObjectType.Bad;

                MockGitObject gitObject;

                if (m_objectIdToContent.TryGetValue(oid, out gitObject))
                {
                    data = Allocate(gitObject.Data.LongLength);
                    data.Write(gitObject.Data, 0, gitObject.Data.Length);

                    objectType = gitObject.ObjectType;

                    return GIT_OK;
                }

                return GIT_ENOTFOUND;
            }

            public override int ReadPrefix(byte[] shortOid, out byte[] oid, out Stream data, out GitObjectType objectType)
            {
                oid = null;
                data = null;
                objectType = GitObjectType.Bad;

                MockGitObject gitObjectAlreadyFound = null;

                foreach (MockGitObject gitObject in m_objectIdToContent.Values)
                {
                    bool match = true;

                    for (int i = 0; i < shortOid.Length; i++)
                    {
                        if (gitObject.ObjectId[i] != shortOid[i])
                        {
                            match = false;
                            break;
                        }
                    }

                    if (!match)
                    {
                        continue;
                    }

                    if (null != gitObjectAlreadyFound)
                    {
                        return GIT_EAMBIGUOUS;
                    }

                    gitObjectAlreadyFound = gitObject;
                }

                if (null != gitObjectAlreadyFound)
                {
                    oid = gitObjectAlreadyFound.ObjectId;
                    objectType = gitObjectAlreadyFound.ObjectType;

                    data = Allocate(gitObjectAlreadyFound.Data.LongLength);
                    data.Write(gitObjectAlreadyFound.Data, 0, gitObjectAlreadyFound.Data.Length);

                    return GIT_OK;
                }

                return GIT_ENOTFOUND;
            }

            public override int Write(byte[] oid, Stream dataStream, long length, GitObjectType objectType, out byte[] finalOid)
            {
                using (var sha1 = new SHA1CryptoServiceProvider())
                {
                    finalOid = sha1.ComputeHash(dataStream);

                    dataStream.Seek(0, SeekOrigin.Begin);
                }

                if (m_objectIdToContent.ContainsKey(finalOid))
                {
                    return GIT_EEXISTS;
                }

                if (length > (long)int.MaxValue)
                {
                    return GIT_ERROR;
                }

                byte[] buffer = new byte[length];
                int bytesRead = dataStream.Read(buffer, 0, (int)length);

                if (bytesRead != (int)length)
                {
                    return GIT_ERROR;
                }

                m_objectIdToContent.Add(finalOid, new MockGitObject(finalOid, objectType, buffer));

                return GIT_OK;
            }

            public override int WriteStream(long length, GitObjectType objectType, out OdbBackendStream stream)
            {
                stream = new MockOdbBackendStream(this, objectType, length);

                return GIT_OK;
            }

            public override bool Exists(byte[] oid)
            {
                return m_objectIdToContent.ContainsKey(oid);
            }

            private Dictionary<byte[], MockGitObject> m_objectIdToContent = new Dictionary<byte[], MockGitObject>(MockGitObjectComparer.Instance);

            private const int GIT_OK = 0;
            private const int GIT_ERROR = -1;
            private const int GIT_ENOTFOUND = -3;
            private const int GIT_EEXISTS = -4;
            private const int GIT_EAMBIGUOUS = -5;

            #region Unimplemented

            public override int ReadHeader(byte[] oid, out int length, out GitObjectType objectType)
            {
                throw new NotImplementedException();
            }

            public override int ReadStream(byte[] oid, out OdbBackendStream stream)
            {
                throw new NotImplementedException();
            }

            public override int ForEach(ForEachCallback callback)
            {
                throw new NotImplementedException();
            }

            #endregion

            #region MockOdbBackendStream

            private class MockOdbBackendStream : OdbBackendStream
            {
                public MockOdbBackendStream(MockOdbBackend backend, GitObjectType objectType, long length)
                    : base(backend)
                {
                    m_type = objectType;
                    m_length = length;
                    m_hash = new SHA1CryptoServiceProvider();
                }

                protected override void Dispose()
                {
                    ((IDisposable)m_hash).Dispose();

                    base.Dispose();
                }

                public override bool CanRead
                {
                    get
                    {
                        return false;
                    }
                }

                public override bool CanWrite
                {
                    get
                    {
                        return true;
                    }
                }

                public override int Write(Stream dataStream, long length)
                {
                    if (null == m_buffer)
                    {
                        m_buffer = new byte[length];

                        if (length > (long)int.MaxValue)
                            return GIT_ERROR;

                        int bytesRead = dataStream.Read(m_buffer, 0, (int)length);

                        if (bytesRead != (int)length)
                            return GIT_ERROR;

                        m_hash.TransformBlock(m_buffer, 0, (int)length, null, 0);
                    }
                    else
                    {
                        long newLength = m_buffer.LongLength + length;

                        if (newLength > (long)int.MaxValue)
                            return GIT_ERROR;

                        byte[] newBuffer = new byte[newLength];
                        Array.Copy(m_buffer, newBuffer, m_buffer.Length);

                        int bytesRead = dataStream.Read(newBuffer, m_buffer.Length, (int)length);

                        if (bytesRead != (int)length)
                            return GIT_ERROR;

                        m_hash.TransformBlock(newBuffer, m_buffer.Length, (int)length, null, 0);

                        m_buffer = newBuffer;
                    }

                    return GIT_OK;
                }

                public override int FinalizeWrite(out byte[] oid)
                {
                    m_hash.TransformFinalBlock(m_buffer, 0, 0);
                    oid = m_hash.Hash;

                    if (m_buffer.Length != (int)m_length)
                    {
                        return GIT_ERROR;
                    }

                    var backend = (MockOdbBackend)Backend;

                    if (!backend.m_objectIdToContent.ContainsKey(oid))
                    {
                        backend.m_objectIdToContent.Add(oid, new MockGitObject(oid, m_type, m_buffer));
                    }

                    return GIT_OK;
                }

                private byte[] m_buffer;

                private readonly GitObjectType m_type;
                private readonly long m_length;
                private readonly HashAlgorithm m_hash;

                #region Unimplemented

                public override int Read(Stream dataStream, long length)
                {
                    throw new NotImplementedException();
                }

                #endregion
            }

            #endregion

            #region MockGitObject

            private class MockGitObject
            {
                public MockGitObject(byte[] objectId, GitObjectType objectType, byte[] data)
                {
                    if (objectId.Length != 20)
                    {
                        throw new InvalidOperationException();
                    }

                    this.ObjectId = objectId;
                    this.ObjectType = objectType;
                    this.Data = data;
                }

                public byte[] ObjectId;
                public GitObjectType ObjectType;
                public byte[] Data;

                public int Length
                {
                    get
                    {
                        return this.Data.Length;
                    }
                }
            }

            #endregion

            #region MockGitObjectComparer

            private class MockGitObjectComparer : IEqualityComparer<byte[]>
            {
                public bool Equals(byte[] x, byte[] y)
                {
                    for (int i = 0; i < 20; i++)
                    {
                        if (x[i] != y[i])
                        {
                            return false;
                        }
                    }

                    return true;
                }

                public int GetHashCode(byte[] obj)
                {
                    int toReturn = 0;

                    for (int i = 0; i < obj.Length / 4; i++)
                    {
                        toReturn ^= (int)obj[4 * i] << 24 +
                                    (int)obj[4 * i + 1] << 16 +
                                    (int)obj[4 * i + 2] << 8 +
                                    (int)obj[4 * i + 3];
                    }

                    return toReturn;
                }

                public static MockGitObjectComparer Instance
                {
                    get
                    {
                        if (null == s_instance)
                        {
                            s_instance = new MockGitObjectComparer();
                        }

                        return s_instance;
                    }
                }

                private static MockGitObjectComparer s_instance;
            }

            #endregion
        }

        #endregion
    }
}