github.com/iosif02/goja_nodejs@v1.0.1/buffer/buffer_test.go (about) 1 package buffer 2 3 import ( 4 "testing" 5 6 "github.com/iosif02/goja" 7 "github.com/iosif02/goja_nodejs/require" 8 ) 9 10 func TestBufferFrom(t *testing.T) { 11 vm := goja.New() 12 new(require.Registry).Enable(vm) 13 14 _, err := vm.RunString(` 15 const Buffer = require("node:buffer").Buffer; 16 17 function checkBuffer(buf) { 18 if (!(buf instanceof Buffer)) { 19 throw new Error("instanceof Buffer"); 20 } 21 22 if (!(buf instanceof Uint8Array)) { 23 throw new Error("instanceof Uint8Array"); 24 } 25 } 26 27 checkBuffer(Buffer.from(new ArrayBuffer(16))); 28 checkBuffer(Buffer.from(new Uint16Array(8))); 29 30 { 31 const b = Buffer.from("\xff\xfe\xfd"); 32 const h = b.toString("hex") 33 if (h !== "c3bfc3bec3bd") { 34 throw new Error(h); 35 } 36 } 37 38 { 39 const b = Buffer.from("0102fffdXXX", "hex"); 40 checkBuffer(b); 41 if (b.toString("hex") !== "0102fffd") { 42 throw new Error(b.toString("hex")); 43 } 44 } 45 46 { 47 const b = Buffer.from('1ag123', 'hex'); 48 if (b.length !== 1 || b[0] !== 0x1a) { 49 throw new Error(b); 50 } 51 } 52 53 { 54 const b = Buffer.from('1a7', 'hex'); 55 if (b.length !== 1 || b[0] !== 0x1a) { 56 throw new Error(b); 57 } 58 } 59 60 { 61 const b = Buffer.from("\uD801", "utf-8"); 62 if (b.length !== 3 || b[0] !== 0xef || b[1] !== 0xbf || b[2] !== 0xbd) { 63 throw new Error(b); 64 } 65 } 66 `) 67 68 if err != nil { 69 t.Fatal(err) 70 } 71 } 72 73 func TestFromBase64(t *testing.T) { 74 vm := goja.New() 75 new(require.Registry).Enable(vm) 76 77 _, err := vm.RunString(` 78 const Buffer = require("node:buffer").Buffer; 79 80 { 81 let b = Buffer.from("AAA_", "base64"); 82 if (b.length !== 3 || b[0] !== 0 || b[1] !== 0 || b[2] !== 0x3f) { 83 throw new Error(b.toString("hex")); 84 } 85 86 let r = b.toString("base64"); 87 if (r !== "AAA/") { 88 throw new Error("to base64: " + r); 89 } 90 for (let i = 0; i < 20; i++) { 91 let s = "A".repeat(i) + "_" + "A".repeat(20-i); 92 let s1 = "A".repeat(i) + "/" + "A".repeat(20-i); 93 let b = Buffer.from(s, "base64"); 94 let b1 = Buffer.from(s1, "base64"); 95 if (!b.equals(b1)) { 96 throw new Error(s); 97 } 98 } 99 } 100 101 { 102 let b = Buffer.from("SQ==???", "base64"); 103 if (b.length !== 1 || b[0] != 0x49) { 104 throw new Error(b.toString("hex")); 105 } 106 } 107 108 `) 109 110 if err != nil { 111 t.Fatal(err) 112 } 113 } 114 115 func TestWrapBytes(t *testing.T) { 116 vm := goja.New() 117 new(require.Registry).Enable(vm) 118 b := []byte{1, 2, 3} 119 buffer := GetApi(vm) 120 vm.Set("b", buffer.WrapBytes(b)) 121 Enable(vm) 122 _, err := vm.RunString(` 123 if (typeof Buffer !== "function") { 124 throw new Error("Buffer is not a function: " + typeof Buffer); 125 } 126 if (!(b instanceof Buffer)) { 127 throw new Error("instanceof Buffer"); 128 } 129 if (b.toString("hex") !== "010203") { 130 throw new Error(b); 131 } 132 `) 133 134 if err != nil { 135 t.Fatal(err) 136 } 137 } 138 139 func TestBuffer_alloc(t *testing.T) { 140 vm := goja.New() 141 new(require.Registry).Enable(vm) 142 143 _, err := vm.RunString(` 144 const Buffer = require("node:buffer").Buffer; 145 146 { 147 const b = Buffer.alloc(2, "abc"); 148 if (b.toString() !== "ab") { 149 throw new Error(b); 150 } 151 } 152 153 { 154 const b = Buffer.alloc(16, "abc"); 155 if (b.toString() !== "abcabcabcabcabca") { 156 throw new Error(b); 157 } 158 } 159 160 { 161 const fill = { 162 valueOf() { 163 return 0xac; 164 } 165 } 166 const b = Buffer.alloc(8, fill); 167 if (b.toString("hex") !== "acacacacacacacac") { 168 throw new Error(b); 169 } 170 } 171 172 { 173 const fill = { 174 valueOf() { 175 return Infinity; 176 } 177 } 178 const b = Buffer.alloc(2, fill); 179 if (b.toString("hex") !== "0000") { 180 throw new Error(b); 181 } 182 } 183 184 { 185 const fill = { 186 valueOf() { 187 return "ac"; 188 } 189 } 190 const b = Buffer.alloc(2, fill); 191 if (b.toString("hex") !== "0000") { 192 throw new Error(b); 193 } 194 } 195 196 { 197 const b = Buffer.alloc(2, -257.4); 198 if (b.toString("hex") !== "ffff") { 199 throw new Error(b); 200 } 201 } 202 203 { 204 const b = Buffer.alloc(2, Infinity); 205 if (b.toString("hex") !== "0000") { 206 throw new Error("Infinity: " + b.toString("hex")); 207 } 208 } 209 210 { 211 const b = Buffer.alloc(2, null); 212 if (b.toString("hex") !== "0000") { 213 throw new Error("Infinity: " + b.toString("hex")); 214 } 215 } 216 217 `) 218 219 if err != nil { 220 t.Fatal(err) 221 } 222 }