2007-05-10 Nagappan A <anagappan@novell.com>
[mono.git] / mcs / class / FirebirdSql.Data.Firebird / Test / FbBlobTest.cs
1 /*
2  *  Firebird ADO.NET Data provider for .NET and Mono 
3  * 
4  *     The contents of this file are subject to the Initial 
5  *     Developer's Public License Version 1.0 (the "License"); 
6  *     you may not use this file except in compliance with the 
7  *     License. You may obtain a copy of the License at 
8  *     http://www.firebirdsql.org/index.php?op=doc&id=idpl
9  *
10  *     Software distributed under the License is distributed on 
11  *     an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either 
12  *     express or implied.  See the License for the specific 
13  *     language governing rights and limitations under the License.
14  * 
15  *  Copyright (c) 2002, 2004 Carlos Guzman Alvarez
16  *  All Rights Reserved.
17  */
18
19 using NUnit.Framework;
20 using System;
21 using System.Collections;
22 using System.Security.Cryptography;
23 using FirebirdSql.Data.Firebird;
24
25 namespace FirebirdSql.Data.Firebird.Tests
26 {
27         [TestFixture]
28         public class FbBlobTest : BaseTest 
29         {
30                 public FbBlobTest() : base(false)
31                 {               
32                 }
33                 
34                 [Test]
35                 public void BinaryBlobTest()
36                 {
37                         int id_value = this.GetId();
38                         
39                         string selectText = "SELECT blob_field FROM TEST WHERE int_field = " + id_value.ToString();
40                         string insertText = "INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)";
41                         
42                         Console.WriteLine("\r\n\r\nBinary Blob Test");
43                         
44                         Console.WriteLine("Generating an array of temp data");
45                         // Generate an array of temp data
46                         byte[] insert_values = new byte[100000*4];
47                         RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
48                         rng.GetBytes(insert_values);
49                         
50                         Console.WriteLine("Executing insert command");
51
52                         // Execute insert command
53                         FbTransaction transaction = Connection.BeginTransaction();
54
55                         FbCommand insert = new FbCommand(insertText, Connection, transaction);
56                         insert.Parameters.Add("@int_field", FbDbType.Integer).Value = id_value;
57                         insert.Parameters.Add("@blob_field", FbDbType.Binary).Value = insert_values;
58                         insert.ExecuteNonQuery();
59
60                         transaction.Commit();
61
62                         Console.WriteLine("Checking inserted values");
63
64                         // Check that inserted values are correct
65                         FbCommand select = new FbCommand(selectText, Connection);
66                         byte[] select_values = (byte[])select.ExecuteScalar();                  
67
68                         for (int i = 0; i < insert_values.Length; i++)
69                         {
70                                 if (insert_values[i] != select_values[i])
71                                 {
72                                         throw new Exception("differences at index " + i.ToString());
73                                 }
74                         }
75
76                         Console.WriteLine("Finishing test");
77                 }
78
79                 [Test]
80                 public void ReaderGetBytes()
81                 {
82                         int id_value = this.GetId();
83                         
84                         string selectText = "SELECT blob_field FROM TEST WHERE int_field = " + id_value.ToString();
85                         string insertText = "INSERT INTO TEST (int_field, blob_field) values(@int_field, @blob_field)";
86                         
87                         Console.WriteLine("\r\n\r\nFbDataReader.GetBytes with Binary Blob Test");
88                         
89                         Console.WriteLine("Generating an array of temp data");
90                         // Generate an array of temp data
91                         byte[] insert_values = new byte[100000*4];
92                         RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
93                         rng.GetBytes(insert_values);
94                         
95                         Console.WriteLine("Executing insert command");
96
97                         // Execute insert command
98                         FbTransaction transaction = Connection.BeginTransaction();
99
100                         FbCommand insert = new FbCommand(insertText, Connection, transaction);
101                         insert.Parameters.Add("@int_field", FbDbType.Integer).Value = id_value;
102                         insert.Parameters.Add("@blob_field", FbDbType.Binary).Value = insert_values;
103                         insert.ExecuteNonQuery();
104
105                         transaction.Commit();
106
107                         Console.WriteLine("Checking inserted values");
108
109                         // Check that inserted values are correct
110                         FbCommand select = new FbCommand(selectText, Connection);
111
112                         FbDataReader reader = select.ExecuteReader();
113
114                         int             index           = 0;
115                         int             segmentSize     = 1000;
116                         byte[]  select_values = new byte[100000*4];
117                         while (reader.Read())
118                         {
119                                 while (index < 400000)
120                                 {
121                                         reader.GetBytes(0, index, select_values, index, segmentSize);
122
123                                         index += segmentSize;
124                                 }
125                         }
126
127                         for (int i = 0; i < insert_values.Length; i++)
128                         {
129                                 if (insert_values[i] != select_values[i])
130                                 {
131                                         throw new Exception("differences at index " + i.ToString());
132                                 }
133                         }
134                 }
135         }
136 }